Postgres Best Practices
Comprehensive performance optimization guide for Postgres. 31 rules across 8 categories, prioritized by impact — from critical (query performance, connection management) to incremental (advanced features).
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- |
Quick Reference
1. Query Performance (CRITICAL)
query-missing-indexes- Add indexes on WHERE and JOIN columnsquery-composite-indexes- Create composite indexes for multi-column queriesquery-covering-indexes- Use covering indexes to avoid table lookupsquery-partial-indexes- Use partial indexes for filtered queriesquery-index-types- Choose the right index type for your data
2. Connection Management (CRITICAL)
conn-pooling- Use connection pooling for all applicationsconn-limits- Set appropriate connection limitsconn-idle-timeout- Configure idle connection timeoutsconn-prepared-statements- Use prepared statements correctly with pooling
3. Security & RLS (CRITICAL)
security-rls-basics- Enable Row Level Security for multi-tenant datasecurity-rls-performance- Optimize RLS policies for performancesecurity-privileges- Apply principle of least privilege
4. Schema Design (HIGH)
schema-data-types- Choose appropriate data typesschema-constraints- Add constraints safely in migrationsschema-primary-keys- Select optimal primary key strategyschema-foreign-key-indexes- Index foreign key columnsschema-partitioning- Partition large tables for better performanceschema-lowercase-identifiers- Use lowercase identifiers for compatibility
5. Concurrency & Locking (MEDIUM-HIGH)
lock-short-transactions- Keep transactions short to reduce lock contentionlock-deadlock-prevention- Prevent deadlocks with consistent lock orderinglock-advisory- Use advisory locks for application-level lockinglock-skip-locked- Use SKIP LOCKED for non-blocking queue processing
6. Data Access Patterns (MEDIUM)
data-n-plus-one- Eliminate N+1 queries with batch loadingdata-pagination- Use cursor-based pagination instead of OFFSETdata-batch-inserts- Batch INSERT statements for bulk datadata-upsert- Use UPSERT for insert-or-update operations
7. Monitoring & Diagnostics (LOW-MEDIUM)
monitor-explain-analyze- Use EXPLAIN ANALYZE to diagnose slow queriesmonitor-pg-stat-statements- Enable pg_stat_statements for query analysismonitor-vacuum-analyze- Maintain table statistics with VACUUM and ANALYZE
8. Advanced Features (LOW)
advanced-full-text-search- Use tsvector for full-text searchadvanced-jsonb-indexing- Index JSONB columns for efficient querying
How to Use
Each rule file in references/ contains: explanation, incorrect/correct SQL examples, EXPLAIN output, and context. Read individual files as needed.