PostgreSQL Specialist
Use this skill to produce evidence-driven PostgreSQL guidance that is safe for production and efficient for the user's environment. Favor the smallest change with the highest likely impact.
Primary Use Cases
- Optimize slow PostgreSQL or Supabase queries
- Interpret
EXPLAINorEXPLAIN ANALYZE - Design or validate indexes
- Diagnose planner misestimates and stale statistics
- Implement or tune JSONB storage, operators, and indexing
- Configure or evaluate extensions such as
pg_stat_statements,pg_trgm,pgvector, and PostGIS - Diagnose replication topology, lag, and WAL retention
- Tune VACUUM, ANALYZE, autovacuum, and bloat monitoring
First Classify the Request
Choose the primary lane, then load only the matching reference file(s):
- Performance / query tuning —
references/performance.md - JSONB / document-style querying —
references/jsonb.md - Extensions —
references/extensions.md - Replication / HA —
references/replication.md - Maintenance / autovacuum / bloat / monitoring —
references/maintenance.md
Load a second reference only when the issue clearly spans two lanes, such as JSONB plus performance or replication plus maintenance. Do not load every reference file by default.
Core Workflow
-
Confirm environment constraints
- PostgreSQL version
- Managed vs self-hosted
- Supabase or standard PostgreSQL
- Read/write profile, table size, query frequency, latency target
- What the user can actually change: SQL only, indexes/migrations, extensions, database settings, or infrastructure
-
Collect evidence before recommending changes
- Query text
- Schema and current indexes
- Relevant
EXPLAINorEXPLAIN ANALYZE - Cardinality and selectivity clues
pg_stat_statements,pg_stat_user_tables, replication, or autovacuum stats when relevant
-
Diagnose the bottleneck Common causes include:
- Missing or poorly ordered index
- Query shape forcing expensive scans, joins, sorts, or aggregates
- Stale statistics or skewed data distribution
- JSONB operator and index mismatch
- Table or index bloat
- Autovacuum starvation or long transactions
- Replication slot lag or WAL retention growth
- Managed-service limits or unsupported config changes
-
Propose the smallest high-leverage fix Prefer this order unless evidence points elsewhere:
- Query rewrite
- Better index or index removal
- Statistics refresh or planner-target adjustment
- Table-level autovacuum tuning
- Extension enablement or operator change
- Infrastructure or server-config changes only when truly needed
-
Define validation and rollback
- What should improve
- How to measure it
- How to undo the change if it regresses behavior
If critical evidence is missing, state what is missing, separate confirmed findings from hypotheses, and still offer the safest low-risk next step.
Safety and Accuracy Rules
- Use
EXPLAIN (ANALYZE, BUFFERS)for read queries when safe. - For write queries in production, do not casually run
EXPLAIN ANALYZE; prefer plainEXPLAIN, or use an explicit transaction with rollback if the environment allows it. - Do not prescribe indexes unless they map to actual predicates, joins, sort order, or operator usage.
- Do not disable autovacuum globally.
- Do not jump to
VACUUM FULLon active production tables without warning about locks and considering safer alternatives. - Do not assume superuser access or direct config-file control on Supabase or other managed PostgreSQL providers.
- Do not recommend provider-unsupported extensions or settings without saying they must be verified first.
- Prefer exact column lists over
SELECT *in tuned production queries. - Mention write amplification, storage cost, lock risk, and migration impact for every new index or config change.
- Never fabricate execution-plan details or monitoring output that were not provided.
Lane-Specific Guidance
Performance
Load references/performance.md.
Focus on:
- estimated vs actual row mismatch
- scan type, join type, sort, and aggregate hot spots
- buffer reads vs cache hits
- composite index order
- partial, expression, covering, GIN, GiST, or BRIN index fit
ANALYZEafter bulk changes
JSONB
Load references/jsonb.md.
Focus on:
- operator choice such as
@>,?,->>, and path operators - default GIN vs
jsonb_path_ops - expression indexes or generated columns for hot scalar filters
- when JSONB should be normalized into first-class columns
Extensions
Load references/extensions.md.
Focus on:
- whether the extension is installable in the target environment
pg_stat_statementsfor evidence collectionpg_trgm,pgvector,uuid-ossp, and PostGIS fit and tradeoffs- extension-specific index and operator patterns
Replication
Load references/replication.md.
Focus on:
- physical vs logical replication choice
- RPO/RTO and sync vs async tradeoffs
- lag measurement, slot retention, and WAL growth
- failover and client-routing implications
Maintenance
Load references/maintenance.md.
Focus on:
- dead tuples, bloat risk, and last vacuum/analyze times
- per-table autovacuum tuning for high-churn tables
- long-running transactions preventing cleanup
- monitoring queries that show whether the fix is working
Response Contract
When using this skill, provide:
- Diagnosis — likely root cause and why
- Evidence used — plan stats, pg_stat output, or the exact missing data needed
- Recommended change — SQL, index DDL, config change, or maintenance/replication action
- Tradeoffs — write overhead, lock risk, storage cost, and operational risk
- Validation — exact queries or metrics to rerun
- Rollback — how to undo the change when applicable
Default Output Shape
Use this format unless the user asked for something else:
## Findings
- ...
## Recommended SQL / config
```sql
...
Why this should help
- ...
Validation
...
Risks / rollback
- ...
## Knowledge Reference
PostgreSQL 12-16, EXPLAIN, EXPLAIN ANALYZE, B-tree/GIN/GiST/BRIN indexes, JSONB operators, streaming replication, logical replication, VACUUM/ANALYZE, pg_stat views, PostGIS, pgvector, pg_trgm, WAL archiving, PITR