Supabase Postgres Best Practices
OpenAIOfficialCreated by supabaseDistributed by OpenAI
- Skill
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured set of guidelines for optimizing Postgres database performance, schema design, and connection management. It is organized into eight priority-based categories, ranging from critical query performance and connection handling to advanced features. Each rule follows a standardized format featuring concrete transformation patterns, error-first examples, and quantified performance impacts to assist developers in writing efficient, scalable SQL.
What it does
- Provides actionable SQL transformation patterns to replace inefficient anti-patterns.
- Categorizes performance rules by impact level from critical to incremental.
- Offers guidance on connection pooling, RLS, and transaction management.
- Includes standardized templates for analyzing query plans and performance metrics.
- Focuses on semantic naming and clear, portable SQL examples.
- Supplies best practices for schema design and indexing strategies.
When to use it
- When writing or refactoring SQL queries for better performance.
- During database schema design to ensure long-term scalability.
- When troubleshooting slow queries or high database load.
- When configuring connection limits or implementing row-level security.
- When auditing existing database code for common performance anti-patterns.
Advantages
- Standardized format makes it easy for AI agents to parse and apply rules.
- Prioritized impact levels help developers focus on the most critical bottlenecks first.
- Concrete before-and-after examples reduce ambiguity in optimization tasks.
- Maintained by experts with a focus on real-world Postgres performance.
- Promotes clean, maintainable, and efficient SQL coding standards.
Usage examples
- “How can I optimize my N+1 query issue in Postgres?”
- The agent identifies the N+1 pattern and suggests replacing sequential queries with a single batched query using the ANY operator or JOINs, referencing the data-n-plus-one guide.
- “Review this SQL query for potential performance improvements.”
- The agent analyzes the query against the provided guidelines, identifying missing indexes or inefficient scan patterns and proposing a corrected SQL version with an explanation of the expected performance gain.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/openai/supabase-postgres-best-practices-lhfb/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip supabase-postgres-best-practices.zip -d .agents/skills
Documentation by supabase
Supabase Postgres Best Practices
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.
When to Apply
Reference these guidelines when:
Writing SQL queries or designing schemas
Implementing indexes or query optimization
Reviewing database performance issues
Configuring connection pooling or scaling
Optimizing for Postgres-specific features
Working with Row-Level Security (RLS)
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Query Performance | CRITICAL | query- |
| 2 | Connection Management | CRITICAL | conn- |
| 3 | Security & RLS | CRITICAL | security- |
| 4 | Schema Design | HIGH | schema- |
| 5 | Concurrency & Locking | MEDIUM-HIGH | lock- |
| 6 | Data Access Patterns | MEDIUM | data- |
| 7 | Monitoring & Diagnostics | LOW-MEDIUM | monitor- |
| 8 | Advanced Features | LOW | advanced- |
How to Use
Read individual rule files for detailed explanations and SQL examples:
references/query-missing-indexes.md
references/query-partial-indexes.md
references/_sections.mdEach rule file contains:
Brief explanation of why it matters
Incorrect SQL example with explanation
Correct SQL example with explanation
Optional EXPLAIN output or metrics
Additional context and references
Supabase-specific notes (when applicable)
References
Files
- ATTRIBUTION.mdAdded by AutoGineer981 B
- CHANGELOG.md2.0 KiB
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md2.5 KiB
- references/_contributing.md4.0 KiB
- references/_sections.md1.6 KiB
- references/_template.md1.0 KiB
- references/advanced-full-text-search.md1.4 KiB
- references/advanced-jsonb-indexing.md1.4 KiB
- references/conn-idle-timeout.md1.3 KiB
- references/conn-limits.md1.3 KiB
- references/conn-pooling.md1.3 KiB
- references/conn-prepared-statements.md1.5 KiB
- references/data-batch-inserts.md1.3 KiB
- references/data-n-plus-one.md1.3 KiB
- references/data-pagination.md1.3 KiB
- references/data-upsert.md1.4 KiB
- references/lock-advisory.md1.5 KiB
- references/lock-deadlock-prevention.md1.8 KiB
- references/lock-short-transactions.md1.3 KiB
- references/lock-skip-locked.md1.3 KiB
- references/monitor-explain-analyze.md1.4 KiB
- references/monitor-pg-stat-statements.md1.4 KiB
- references/monitor-vacuum-analyze.md1.5 KiB
- references/query-composite-indexes.md1.4 KiB
- references/query-covering-indexes.md1.3 KiB
- references/query-index-types.md1.5 KiB
- references/query-missing-indexes.md1.2 KiB
- references/query-partial-indexes.md1.3 KiB
- references/schema-constraints.md1.8 KiB
- references/schema-data-types.md1.4 KiB
- references/schema-foreign-key-indexes.md1.6 KiB
- references/schema-lowercase-identifiers.md1.7 KiB
- references/schema-partitioning.md1.5 KiB
- references/schema-primary-keys.md1.8 KiB
- references/security-privileges.md1.5 KiB
- references/security-rls-basics.md1.3 KiB
- references/security-rls-performance.md2.1 KiB
references/_contributing.md
Writing Guidelines for Postgres References
This document provides guidelines for creating effective Postgres best practice references that work well with AI agents and LLMs.
Key Principles
1. Concrete Transformation Patterns
Show exact SQL rewrites. Avoid philosophical advice.
Good: "Use WHERE id = ANY(ARRAY[...]) instead of
WHERE id IN (SELECT ...)" Bad: "Design good schemas"
2. Error-First Structure
Always show the problematic pattern first, then the solution. This trains agents to recognize anti-patterns.
**Incorrect (sequential queries):** [bad example]
**Correct (batched query):** [good example]3. Quantified Impact
Include specific metrics. Helps agents prioritize fixes.
Good: "10x faster queries", "50% smaller index", "Eliminates N+1" Bad: "Faster", "Better", "More efficient"
4. Self-Contained Examples
Examples should be complete and runnable (or close to it). Include CREATE TABLE
if context is needed.
-- Include table definition when needed for clarity
CREATE TABLE users (
id bigint PRIMARY KEY,
email text NOT NULL,
deleted_at timestamptz
);
-- Now show the index
CREATE INDEX users_active_email_idx ON users(email) WHERE deleted_at IS NULL;5. Semantic Naming
Use meaningful table/column names. Names carry intent for LLMs.
Good: users, email, created_at, is_active
Bad: table1, col1, field, flag
Code Example Standards
SQL Formatting
-- Use lowercase keywords, clear formatting
CREATE INDEX CONCURRENTLY users_email_idx
ON users(email)
WHERE deleted_at IS NULL;
-- Not cramped or ALL CAPS
CREATE INDEX CONCURRENTLY USERS_EMAIL_IDX ON USERS(EMAIL) WHERE DELETED_AT IS NULL;Comments
Explain why, not what
Highlight performance implications
Point out common pitfalls
Language Tags
sql- Standard SQL queriesplpgsql- Stored procedures/functionstypescript- Application code (when needed)python- Application code (when needed)
When to Include Application Code
Default: SQL Only
Most references should focus on pure SQL patterns. This keeps examples portable.
Include Application Code When:
Connection pooling configuration
Transaction management in application context
ORM anti-patterns (N+1 in Prisma/TypeORM)
Prepared statement usage
Format for Mixed Examples:
**Incorrect (N+1 in application):**
```typescript
for (const user of users) {
const posts = await db.query("SELECT * FROM posts WHERE user_id = $1", [
user.id,
]);
}
```Correct (batch query):
const posts = await db.query("SELECT * FROM posts WHERE user_id = ANY($1)", [
userIds,
]);Impact Level Guidelines
| Level | Improvement | Use When |
|---|---|---|
| CRITICAL | 10-100x | Missing indexes, connection exhaustion, sequential scans on large tables |
| HIGH | 5-20x | Wrong index types, poor partitioning, missing covering indexes |
| MEDIUM-HIGH | 2-5x | N+1 queries, inefficient pagination, RLS optimization |
| MEDIUM | 1.5-3x | Redundant indexes, query plan instability |
| LOW-MEDIUM | 1.2-2x | VACUUM tuning, configuration tweaks |
| LOW | Incremental | Advanced patterns, edge cases |
Reference Standards
Primary Sources:
Official Postgres documentation
Supabase documentation
Postgres wiki
Established blogs (2ndQuadrant, Crunchy Data)
Format:
Reference:
[Postgres Indexes](https://www.postgresql.org/docs/current/indexes.html)Review Checklist
Before submitting a reference:
[ ] Title is clear and action-oriented
[ ] Impact level matches the performance gain
[ ] impactDescription includes quantification
[ ] Explanation is concise (1-2 sentences)
[ ] Has at least 1 Incorrect SQL example
[ ] Has at least 1 Correct SQL example
[ ] SQL uses semantic naming
[ ] Comments explain why, not what
[ ] Trade-offs mentioned if applicable
[ ] Reference links included
[ ]
pnpm testpasses
Requirements and compatibility
Written for: OpenAI Codex
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- supabase
- Publisher
- OpenAI (Official)
- Distributed by
- OpenAI
- Source
- openai/plugins · plugins/supabase/skills/supabase-postgres-best-practices · commit d420d13
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from plugins/supabase/LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by supabase or OpenAI.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download supabase-postgres-best-practices.zip