I’ve reviewed a lot of slow-query incidents over the years. The same five indexing mistakes account for most of them. None of them are obscure. All of them are easy to make when you are moving quickly and the table has ten thousand rows instead of a hundred million.
Every example below is a real pattern from a real incident, with the names changed. The fix is rarely more than one CREATE INDEX. The hard part is noticing.
1. Indexing the column, not the query
The classic. The query filters on two columns and sorts on a third:
SELECT * FROM ordersWHERE customer_id = $1 AND status = 'open'ORDER BY created_at DESCLIMIT 20;Someone adds an index on customer_id and calls it done. Postgres uses it, finds all 40,000 orders for that customer, then filters and sorts them in memory:
Limit (cost=2841.22..2841.27 rows=20) -> Sort (cost=2841.22..2943.11 rows=40756) Sort Key: created_at DESC -> Index Scan using orders_customer_id_idx on orders Index Cond: (customer_id = 8812) Filter: (status = 'open') Rows Removed by Filter: 39104That Rows Removed by Filter line is the tell. The index did a fraction of the work and the rest happened the slow way.
A composite index that matches the query shape lets the planner satisfy the filter and the order from the index alone:
CREATE INDEX CONCURRENTLY orders_customer_status_created_idx ON orders (customer_id, status, created_at DESC);Limit (cost=0.56..24.18 rows=20) -> Index Scan using orders_customer_status_created_idx on orders Index Cond: ((customer_id = 8812) AND (status = 'open'))No sort node, no filter, twenty index entries read. The query went from 180ms to 0.3ms.
2. Forgetting partial indexes
A status column with five values where 97% of rows are completed. Queries only ever look for the other 3%. A full index on status is mostly dead weight: 97% of its entries will never be read, but every write still has to maintain them.
-- Indexes 3% of the table, serves 100% of the queriesCREATE INDEX CONCURRENTLY orders_open_idx ON orders (customer_id, created_at DESC) WHERE status IN ('open', 'processing');The planner uses it whenever the query’s WHERE clause implies the index predicate. The payoff is twofold: the index is 30× smaller (it fits in memory now), and writes to completed orders no longer touch it.
| Index | Size | Entries | Used by hot queries |
|---|---|---|---|
orders_status_idx | 1.9 GB | 88,000,000 | Yes, slowly |
orders_open_idx | 61 MB | 2,600,000 | Yes, from memory |
3. Not reading EXPLAIN (ANALYZE, BUFFERS)
Plain EXPLAIN shows you what the planner thinks will happen. ANALYZE runs the query and shows what did happen. BUFFERS shows how much of it came from disk:
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;Index Scan using orders_customer_id_idx on orders (actual time=0.041..312.774 rows=40756 loops=1) Buffers: shared hit=1204 read=38911Three things to look for, in order:
- Estimated vs actual rows.
rows=100planned butrows=2000000actual means stale statistics. RunANALYZE ordersand checkdefault_statistics_targetfor skewed columns. readvshitbuffers. A highreadcount means the working set does not fit inshared_buffers. That is a sizing or indexing problem, not a query problem.loops=. A nested loop that runs the inner side 50,000 times multiplies every cost on that line. The per-loop time looks fine; the total is not.
I keep explain.dalibo.com bookmarked. Paste the plan, and the slow node lights up in red.
4. Index bloat from high-churn tables
Postgres indexes do not shrink. A jobs table with a million inserts and deletes a day keeps an index whose pages are half empty. Lookups touch twice the pages they should, and the whole thing takes twice the memory.
Check it:
SELECT indexrelname, pg_size_pretty(pg_relation_size(indexrelid)) AS size, idx_scanFROM pg_stat_user_indexesWHERE schemaname = 'public'ORDER BY pg_relation_size(indexrelid) DESCLIMIT 10;And fix it without locking:
REINDEX INDEX CONCURRENTLY jobs_queue_idx;REINDEX CONCURRENTLY builds a new index alongside the old one and swaps them atomically. Reads and writes continue throughout. Schedule it for low-traffic windows anyway, since it doubles the index’s disk usage while it runs.
Create a new index with a temporary name using CREATE INDEX CONCURRENTLY, drop the old one, then rename. The pg_repack extension automates this and handles tables too.
Tune autovacuum per table so the index stops bloating in the first place:
ALTER TABLE jobs SET ( autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_cost_delay = 2);5. Unused indexes
Every index is a write amplifier. An INSERT into a table with eight indexes is nine writes. Find the ones nobody uses:
SELECT indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS sizeFROM pg_stat_user_indexesWHERE idx_scan = 0 AND indexrelname NOT LIKE '%_pkey'ORDER BY pg_relation_size(indexrelid) DESC;On one system this found 14 indexes, 23 GB, zero scans since the last stats reset. Dropping them cut write latency by 18%, and that was the easy win, because the deploy did not change a single line of application code.
The habits that prevent all five
None of these mistakes survive contact with a team that does three things consistently:
| Habit | Catches |
|---|---|
EXPLAIN (ANALYZE, BUFFERS) in every PR that adds a query | Mistakes 1, 2, 3 |
Weekly pg_stat_user_indexes report in Slack | Mistakes 4, 5 |
| Indexes live in migrations, reviewed like code | All of them, eventually |
The theme here is that Postgres tells you everything. It writes the plan down, it counts every index scan, it tracks every dead tuple. The incidents happen when nobody reads what it wrote.