ablevarghese/ios-claude-toolkit/supabase-postgres-best-practices
Claude Code and agent skill by ablevarghese/ios-claude-toolkit
- community
Checking that the SKILL.md file is still in the repository.
Query is slow, optimize database, add an index, RLS policy. Postgres query optimization, schema design, indexing, RLS policy performance, and connection pooling best practices. Trigger on: 'slow query', 'EXPLAIN ANALYZE', 'add an index', 'RLS is slow', 'optimize this query', 'N+1 problem', 'connection pooling', 'Supavisor', 'SELECT * performance', 'database health', 'Edge Function database access', or any question about making Postgres faster. For creating/repairing/pushing migrations, use atlas-database instead.
Install
npx vskill@latest install ablevarghese/ios-claude-toolkit/supabase-postgres-best-practicesSKILL.md
The same SKILL.md was published earlier as ablevarghese/ios-claude-toolkit/supabase-postgres-best-practices.
Supabase Postgres Best Practices
Iron Law: "Every query touching user data must go through RLS. Service role keys never reach the client."
Comprehensive performance optimization guide for Postgres, maintained by Supabase. Contains rules across 8 categories, prioritized by impact to guide automated query optimization and schema design.
Source: github.com/supabase/agent-skills
Rationalizations (Do Not Skip)
| Rationalization | Why It's Wrong | Required Action | |
|---|---|---|---|
| "The query is fast enough" | "Fast enough" today is slow tomorrow. Database performance degrades non-linearly: a query that's 50ms at 10K rows becomes 5 seconds at 1M rows. By the time you notice, it's a production incident. | Run EXPLAIN ANALYZE on every query that touches a table expected to exceed 10K rows. Add indexes proactively. | |
| "We don't need indexes yet" | Sequential scans on unindexed columns are the #1 cause of Postgres performance issues. Adding indexes retroactively on large tables locks the table for writes (unless you use CONCURRENTLY, which you probably won't remember under pressure). | Add indexes at table creation time for every column used in WHERE, JOIN, and ORDER BY clauses. | |
| "RLS is too slow" | RLS adds 0.1-0.5ms per query when policies use direct column comparisons with auth.uid(). That's negligible. RLS being "slow" almost always means the policy uses a subquery or function call that triggers per-row evaluation. The fix is better policies, not disabling RLS. | Write efficient RLS policies using direct comparisons. Index the columns used in RLS policies. Never disable RLS on user-facing tables. | |
| "We'll optimize when we scale" | Database optimization at scale is 10x harder because you can't easily run migrations, you can't test with production-scale data locally, and every change risks downtime. Build the right patterns now when they're cheap. | Follow the patterns in this guide from the start. Proper schema design, indexing, and RLS policies are not premature optimization -- they're baseline engineering. | |
| "SELECT * is fine for our use case" | SELECT * fetches all columns including large JSONB blobs, text fields, and columns you added "just in case." It also breaks if you add a column with sensitive data later -- suddenly your API returns it. | Select only the columns you need. Create specific views for different access patterns. | |
| "Service role key is safe in our Edge Function" | Edge Functions run server-side, so service_role is acceptable there. But if you're passing the service_role key from a client to an Edge Function, or if your Edge Function uses service_role when anon would suffice, you've expanded your attack surface unnecessarily. | Use anon key (RLS-enforced) for client-initiated operations. Use service_role only when explicitly bypassing RLS for admin operations in trusted server-side code. |
Red Flags -- STOP
- SELECT * in production queries: Fetches unnecessary data, breaks when schema changes, may expose sensitive columns. Always specify columns.
- Missing RLS on user-facing tables: Any table accessible through the Supabase client (anon key) without RLS is a complete data breach. Enable RLS and add policies immediately.
- N+1 query patterns: Querying in a loop (for each user, fetch their transfers) instead of a single JOIN query. This turns 1 query into N+1 queries and destroys performance.
- Sequential scans on large tables: EXPLAIN ANALYZE showing
Seq Scanon a table with >10K rows means you're scanning every row. Add an index. - Service role key in client code: The service_role key bypasses ALL RLS. If it's in your iOS app bundle, JavaScript, or any client-accessible code, every user can access every row in every table.
- Missing connection pooling: Direct connections from serverless functions exhaust the connection pool in minutes. Always use Supavisor (port 6543) for pooled connections.
When NOT to Use This Skill
- Migration file creation and management: For creating, repairing, and pushing Supabase migrations, see atlas-database.
- Stripe webhook data storage: For designing webhook event storage with idempotent processing, see stripe-best-practices (but use schema patterns from this skill).
- Application-level data access patterns: This skill covers database-level optimization. For service/repository layer patterns in Swift, see forge-development.
- Database backup and disaster recovery: For backup strategies and point-in-time recovery, consult Supabase documentation directly.
Decision Framework
Query Optimization Decision Tree
Index Selection Decision Tree
EXPLAIN ANALYZE Reading Guide
Understanding query plans is the single most important database optimization skill.
Running EXPLAIN ANALYZE
Reading the Output
What Each Field Means
| Field | Meaning | What to Look For | |
|---|---|---|---|
cost=X..Y | Estimated startup..total cost (arbitrary units) | Higher = more expensive | |
rows=N | Estimated number of rows output | Compare with actual rows | |
actual time=X..Y | Real execution time in ms | Total time of operation | |
actual rows=N | Real rows returned | If >> estimated, run ANALYZE | |
loops=N | How many times this node executed | High loops in nested loops = expensive | |
Buffers: shared hit=N | Pages read from cache | Higher = more I/O | |
Buffers: shared read=N | Pages read from disk | High = needs more cache or index |
Warning Signs in EXPLAIN ANALYZE
| Sign | Problem | Solution | |
|---|---|---|---|
Seq Scan on large table | Full table scan, no index used | Add index on filtered column | |
actual rows >> estimated rows | Stale statistics | Run ANALYZE table_name; | |
Nested Loop with high loops | N+1 join pattern | Add index on JOIN column | |
Sort with external merge | Sort spilling to disk | Add index matching ORDER BY, or increase work_mem | |
Hash Join with high memory | Large hash table in memory | May need more work_mem or different join strategy | |
Filter: Rows Removed (large) | Index fetch then discard | Use partial index or refine WHERE clause |
Rule Categories by Priority
| Priority | Category | Impact | Reference | |
|---|---|---|---|---|
| 1 | Query Performance | CRITICAL | references/query-patterns.md | |
| 2 | Connection Management | CRITICAL | references/query-patterns.md | |
| 3 | Security & RLS | CRITICAL | Below | |
| 4 | Schema Design | HIGH | references/schema-patterns.md | |
| 5 | Concurrency & Locking | MEDIUM-HIGH | references/schema-patterns.md | |
| 6 | Migration Best Practices | MEDIUM | references/migration-patterns.md | |
| 7 | Monitoring & Diagnostics | LOW-MEDIUM | Below | |
| 8 | Edge Functions Patterns | LOW | Below |
3. Security & RLS (CRITICAL)
Always Enable RLS
Write Efficient RLS Policies
RLS Policy Performance Patterns
Use Security Definer Functions Carefully
Service Role Key Safety
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/supabase-postgres-best-practices/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/ablevarghese/ios-claude-toolkit/supabase-postgres-best-practices)