postgres-query-tuning
Skill1 starsChanged 2 months agoMITView on GitHub
Diagnose and fix slow or pathological PostgreSQL queries using real EXPLAIN output instead of guesses - covering missing and wrong indexes, bad join strategies, N+1 loops, work_mem spills, and lock waits. Use when a query is slow, when the database is under load, when someone pastes EXPLAIN (ANALYZE, BUFFERS) output, or when a migration is about to lock a table. Triggers on "slow query", "seq scan", "table bloat", "deadlock", "N+1", "index not used", "query timeout".
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 6 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 48
Read the settings line first.
- Very specific: line 66
Read top-down: the most expensive node is the one with the largest actual time × loops, not the one that appears first.
- Distinctive: line 101
Every index is a write cost on the whole table, including ones only used for a nightly report.
- Distinctive: line 103
Check for redundancy before adding:
- Distinctive: line 123
If the plan improved but latency did not, the query was not your bottleneck.
- Distinctive: line 144
Use keyset pagination instead.
- Very specific: line 194
Never add a NOT NULL column with a volatile default, or a NOT NULL column at all, in one step on a large table.
- Distinctive: line 231
Stop and change the design when:
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
PostgreSQL Query Tuning
Find out what a query is actually doing before changing anything. Almost every Postgres performance problem is visible in the plan, and almost every bad "fix" makes a different query worse.
Workflow
Progress:
- 1. Reproduce with the real query and real parameters
- 2. Get the plan:
EXPLAIN (ANALYZE, BUFFERS, VERBOSE, SETTINGS) - 3. Read the plan for the actual cost driver (see the table below)
- 4. Apply one fix
- 5. Re-run and compare — keep it only if the plan and the numbers improved
- 6. If you added an index, verify it is used and is not redundant
Step 1 — Reproduce honestly
Tuning WHERE id = 42 tells you nothing about a query that scans. Get the
production-shaped query:
- Real parameter values. Postgres picks plans per parameter; a plan for
id = 1can be wildly different fromid = 9999999. - The real row counts. Statistics lying about a table is the single most common cause of a bad plan — if the plan estimates are off by orders of magnitude, fix that first, before touching indexes.
- The real transaction.
EXPLAINin autocommit can produce a different plan than the same query inside a transaction that has already written.
ANALYZE the table first if you suspect stale stats. For a long-lived
database, autovacuum not keeping up is a real and common cause.
Step 2 — Get the plan
EXPLAIN (ANALYZE, BUFFERS, VERBOSE, SETTINGS) SELECT ...;Read the settings line first. It shows non-default settings in effect for this session, which is often the whole answer (see Gotchas).
Note that EXPLAIN ANALYZE executes the query. For INSERT/UPDATE/
DELETE, wrap it in a transaction you roll back:
BEGIN;
EXPLAIN (ANALYZE, BUFFERS) UPDATE ...;
ROLLBACK;For a helper that flags the usual suspects in a plan, run
python scripts/plan_review.py and paste the EXPLAIN (ANALYZE, BUFFERS)
output in. It is advisory, not a substitute for your own reading.
Step 3 — Read the plan for the cost driver
Read top-down: the most expensive node is the one with the largest
actual time × loops, not the one that appears first.
| What the plan says | What it means | Fix |
Seq Scan on a large table with low selectivity | No usable index, or stats say the filter is unselective | Index the filter columns; check selectivity first |
rows=10 estimated, actual rows=900000 | Stats are wrong — every downstream decision is suspect | ANALYZE, raise default_statistics_target, or add extended stats |
Nested Loop with loops=500000 | The classic N+1 shape | Join once, or batch the lookups |
Sort Method: external merge Disk | Sort spilled to disk | Raise work_mem for this session, or add an index that avoids the sort |
Hash Batches: 16 | Hash join spilled to disk | Raise work_mem, or reduce the join input |
Recheck Cond + Heap Blocks: lossy=… | Bitmap index scan lost heap rows, re-checking many | Raise work_mem so the bitmap fits |
Rows Removed by Filter: 9000000 | Filter applied after reading everything | Index the filter column |
Heap Fetches: 12000 on an index-only scan | Visibility map is stale | VACUUM the table |
| Same query, different plan per parameter | Plan cache / generic vs custom plan | plan_cache_mode=force_custom_plan for that statement |
Sort above a Limit that takes few rows | Sort could be index-ordered | Index matching ORDER BY ... LIMIT |
loops is where N+1 hides. A node with actual time=0.05 and
loops=200000 is 10 seconds. Always multiply.
Step 4 — Apply one fix, then re-measure
One change at a time. If you change three things you cannot attribute the win, and you cannot undo the bad part.
Order of preference:
- Fix the query or schema. Cheapest, most durable, no write cost.
- Add the index. Costs write throughput and storage; gets it back on read.
- Change a setting. Session-level first (
SET), then database, thenpostgresql.conflast.
Index rules that matter
- Composite index column order is equality columns first, then range/sort
columns.
(a, b)servesa = 1; it does not serveb = 2. - Every index is a write cost on the whole table, including ones only used for a nightly report.
- Check for redundancy before adding:
SELECT indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_stat_user_indexes ORDER BY idx_scan ASC;idx_scan = 0since the last stats reset is a candidate for removal — after confirming the reset was long enough ago to be meaningful. CREATE INDEX CONCURRENTLYavoids locking writes, but cannot run in a transaction block, can fail and leave anINVALIDindex, and requires two table scans. If a failure leaves one behind, drop it before retrying.
Step 5 — Verify
A fix is real when the plan changed and the numbers moved:
- The expensive node is gone or much cheaper.
- Buffers dropped (
Buffers: shared hit=… read=…) — this is the most honest measure, because it reflects I/O rather than planner estimates. - p95 latency for the endpoint, not just the one query.
If the plan improved but latency did not, the query was not your bottleneck. Go back to the caller — it may be issuing the query in a loop.
Gotchas
work_memis per node, per parallel worker, per sort/hash operation, not per query. A query with 4 sorts can use 4×work_mem. Raising it globally to fix one query can OOM the machine. UseSET work_memfor the session or statement.EXPLAINwithoutANALYZEis a guess. It uses estimates, so it will happily show a great plan for a query that is actually slow. Never tune from a plan withoutANALYZE.- Sequential scans are not inherently bad. For a query returning a large
fraction of a table, a seq scan is faster than an index scan plus heap
fetches. Chasing every
Seq Scanmeans adding useless indexes. LIKE 'foo%'uses a btree index;LIKE '%foo'does not. For contains-search, usepg_trgm(GIN) — but it cannot help a plain btree.- Functions on the indexed column disable the index.
WHERE date(created_at) = '2024-01-01'ignores an index oncreated_at. Rewrite as a range:created_at >= '2024-01-01' AND created_at < '2024-01-02'. OFFSET 100000scans and discards every skipped row. It is O(offset). Use keyset pagination instead.- A transaction left open holds back
xminand can block vacuum across the whole database, causing bloat everywhere.pg_stat_activity+state = 'idle in transaction'is the check. work_memspills show asDiskinSort Method/Hash Batches, but a clean plan with highBuffers: readis disk I/O, a different problem.- JSONB
->>on a computed expression is not indexed unless you add a matching expression index.
Locking and contention
Different failure mode: the query is fine, the query is waiting. Check before tuning anything:
SELECT pid, state, wait_event_type, wait_event, now() - query_start AS duration, left(query, 100)
FROM pg_stat_activity
WHERE state <> 'idle' ORDER BY duration DESC;wait_event_type = 'Lock'— something else holds a conflicting lock. Long transactions and idle-in-transaction sessions are the usual cause. Fix the holder, not the waiter.wait_event_type = 'IO'— disk. Look atEXPLAIN (ANALYZE, BUFFERS).wait_event = 'IPC'on a parallel query — workers contending; usuallymax_parallel_workers_per_gatherorparallel_setup_costmis-tuned.
Deadlocks: SELECT * FROM pg_stat_activity WHERE query LIKE '%deadlock%', or
pg_locks. Deadlocks are usually inconsistent lock ordering across code paths —
fix by always taking locks in the same order, not by raising
deadlock_timeout.
Bloat and autovacuum
Symptom: scans get slower over time, tables are large on disk, Seq Scan reads
more pages than the row count implies.
SELECT relname, n_dead_tup, last_autovacuum, last_autoanalyze
FROM pg_stat_user_tables WHERE n_dead_tup > 10000 ORDER BY n_dead_tup DESC;Fix the cause, not the symptom. A high-churn table under default autovacuum
settings needs per-table autovacuum_vacuum_scale_factor and
autovacuum_analyze_scale_factor lowered, or VACUUM (ANALYZE) scheduled.
VACUUM FULL takes an ACCESS EXCLUSIVE lock and rewrites the table — it is a
last resort, and it needs a maintenance window.
Schema changes without downtime
Never add a NOT NULL column with a volatile default, or a NOT NULL column at
all, in one step on a large table. Expand-contract:
- Expand — add the column as nullable, or with a constant default
(Postgres 11+ adds a constant default without a rewrite; a volatile default
like
now()still rewrites the whole table). - Migrate — backfill in batches, in separate transactions, with a small
pg_sleepbetween batches so you never hold a long lock. - Contract — once reads/writes are all on the new column and the backfill
is verified, enforce the constraint:
NOT NULLviaALTER TABLE ... ADD CONSTRAINT ... NOT VALIDthenVALIDATE CONSTRAINT, which takes only aSHARE UPDATE EXCLUSIVElock.
Backfill pattern that keeps the table responsive:
UPDATE t SET new_col = old_col
WHERE id IN (SELECT id FROM t WHERE new_col IS NULL LIMIT 5000);
-- repeat in a loop, separate transactions, pause between batchesAdding an index on a live table: CREATE INDEX CONCURRENTLY, never in the
same transaction as other DDL.
Migrations safety checklist
- Does it take a lock stronger than
ACCESS EXCLUSIVEon a hot table? - Does it rewrite the table (new column with volatile default, changed type,
VACUUM FULL)? - Is it reversible, and is the rollback written down?
- Does it need a long transaction, and what does that block?
- Is
CONCURRENTLYrequired, and is the code path free ofBEGIN/COMMITaround it? - Has it been run against a copy of production data, at production size?
When to stop tuning
Stop and change the design when:
- The query is fast but called thousands of times per request. Batch it.
- The join spans a service boundary. Denormalise, or cache.
- The plan is good and the data is simply large. Archive, partition, or materialise.
- You are tempted to raise
shared_bufferspast available RAM. It will not help and will cause swapping.
Files
references/plan-nodes.md— every common plan node, whatactual time,rows, andloopsmean for it, and what to do about it.references/indexing.md— index types, when each is right, expression and partial indexes, covering indexes, and how to find redundant ones.scripts/plan_review.py— parseEXPLAIN (ANALYZE, BUFFERS)output and flag the usual suspects.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

