Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/_contributing.md
1# Writing Guidelines for Postgres References23This document provides guidelines for creating effective Postgres best4practice references that work well with AI agents and LLMs.56## Key Principles78### 1. Concrete Transformation Patterns910Show exact SQL rewrites. Avoid philosophical advice.1112**Good:** "Use `WHERE id = ANY(ARRAY[...])` instead of13`WHERE id IN (SELECT ...)`" **Bad:** "Design good schemas"1415### 2. Error-First Structure1617Always show the problematic pattern first, then the solution. This trains agents18to recognize anti-patterns.1920```markdown21**Incorrect (sequential queries):** [bad example]2223**Correct (batched query):** [good example]24```2526### 3. Quantified Impact2728Include specific metrics. Helps agents prioritize fixes.2930**Good:** "10x faster queries", "50% smaller index", "Eliminates N+1"31**Bad:** "Faster", "Better", "More efficient"3233### 4. Self-Contained Examples3435Examples should be complete and runnable (or close to it). Include `CREATE TABLE`36if context is needed.3738```sql39-- Include table definition when needed for clarity40CREATE TABLE users (41id bigint PRIMARY KEY,42email text NOT NULL,43deleted_at timestamptz44);4546-- Now show the index47CREATE INDEX users_active_email_idx ON users(email) WHERE deleted_at IS NULL;48```4950### 5. Semantic Naming5152Use meaningful table/column names. Names carry intent for LLMs.5354**Good:** `users`, `email`, `created_at`, `is_active`55**Bad:** `table1`, `col1`, `field`, `flag`5657---5859## Code Example Standards6061### SQL Formatting6263```sql64-- Use lowercase keywords, clear formatting65CREATE INDEX CONCURRENTLY users_email_idx66ON users(email)67WHERE deleted_at IS NULL;6869-- Not cramped or ALL CAPS70CREATE INDEX CONCURRENTLY USERS_EMAIL_IDX ON USERS(EMAIL) WHERE DELETED_AT IS NULL;71```7273### Comments7475- Explain _why_, not _what_76- Highlight performance implications77- Point out common pitfalls7879### Language Tags8081- `sql` - Standard SQL queries82- `plpgsql` - Stored procedures/functions83- `typescript` - Application code (when needed)84- `python` - Application code (when needed)8586---8788## When to Include Application Code8990**Default: SQL Only**9192Most references should focus on pure SQL patterns. This keeps examples portable.9394**Include Application Code When:**9596- Connection pooling configuration97- Transaction management in application context98- ORM anti-patterns (N+1 in Prisma/TypeORM)99- Prepared statement usage100101**Format for Mixed Examples:**102103````markdown104**Incorrect (N+1 in application):**105106```typescript107for (const user of users) {108const posts = await db.query("SELECT * FROM posts WHERE user_id = $1", [109user.id,110]);111}112```113````114115**Correct (batch query):**116117```typescript118const posts = await db.query("SELECT * FROM posts WHERE user_id = ANY($1)", [119userIds,120]);121```122123---124125## Impact Level Guidelines126127| Level | Improvement | Use When |128|-------|-------------|----------|129| **CRITICAL** | 10-100x | Missing indexes, connection exhaustion, sequential scans on large tables |130| **HIGH** | 5-20x | Wrong index types, poor partitioning, missing covering indexes |131| **MEDIUM-HIGH** | 2-5x | N+1 queries, inefficient pagination, RLS optimization |132| **MEDIUM** | 1.5-3x | Redundant indexes, query plan instability |133| **LOW-MEDIUM** | 1.2-2x | VACUUM tuning, configuration tweaks |134| **LOW** | Incremental | Advanced patterns, edge cases |135136---137138## Reference Standards139140**Primary Sources:**141142- Official Postgres documentation143- Supabase documentation144- Postgres wiki145- Established blogs (2ndQuadrant, Crunchy Data)146147**Format:**148149```markdown150Reference:151[Postgres Indexes](https://www.postgresql.org/docs/current/indexes.html)152```153154---155156## Review Checklist157158Before submitting a reference:159160- [ ] Title is clear and action-oriented161- [ ] Impact level matches the performance gain162- [ ] impactDescription includes quantification163- [ ] Explanation is concise (1-2 sentences)164- [ ] Has at least 1 **Incorrect** SQL example165- [ ] Has at least 1 **Correct** SQL example166- [ ] SQL uses semantic naming167- [ ] Comments explain _why_, not _what_168- [ ] Trade-offs mentioned if applicable169- [ ] Reference links included170- [ ] `pnpm test` passes171