The Postgres EXPLAIN tricks we use before calling something slow

Before you blame the database, ask EXPLAIN what it thinks is happening. Here are the five EXPLAIN habits that have saved more performance fires at Vankode than any indexing strategy.

AI-generated editorial scene of the PostgreSQL elephant outline on a slate-blue wall beside repeating stone portals

Before you blame the database, ask EXPLAIN what it thinks is happening. Five habits that have saved more performance fires than any indexing strategy.

1. Always use EXPLAIN (ANALYZE, BUFFERS)

EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT TEXT)
SELECT id, title, excerpt
FROM posts
WHERE category = 'AI'
ORDER BY date DESC
LIMIT 10;

Plain EXPLAIN shows the estimated plan. ANALYZE runs the query and shows actual times and row counts. BUFFERS shows cache hits vs disk reads — the single best signal of whether your data fits in memory. Always use both.

2. Compare rows= to actual rows=

Look at a line like:

Seq Scan on posts  (cost=0.00..45.30 rows=50 width=180)
                   (actual time=0.012..0.340 rows=3812 loops=1)

Estimated: 50 rows. Actual: 3812 rows. That gap is why the planner chose a seq scan. Running ANALYZE posts will update the statistics and usually fix it. If it doesn't, you need a better index.

3. Watch for Rows Removed by Filter

Seq Scan on posts  (actual rows=12 loops=1)
    Filter: (draft = false)
    Rows Removed by Filter: 9988

The query returns 12 rows but Postgres read 10,000. That is a missing partial index. Add:

CREATE INDEX posts_published_idx ON posts (date DESC) WHERE draft = false;

Re-run EXPLAIN. The filter line disappears because the index already excludes drafts.

4. Look for Hash Join vs Nested Loop

Nested Loop  (actual rows=5000 loops=1)
    ->  Seq Scan on authors  (actual rows=50 loops=1)
    ->  Index Scan on posts_by_author  (actual rows=100 loops=50)

50 authors × 100 posts each = 5000 rows. Fine.

Nested Loop  (actual rows=5000 loops=1)
    ->  Seq Scan on authors  (actual rows=5000 loops=1)
    ->  Index Scan on posts_by_author  (actual rows=1 loops=5000)

5000 authors × 1 post each = still 5000 rows, but Postgres did 5000 index lookups instead of 50. If the planner switches to a Hash Join at this size it will be faster. A common cause is stale stats — run ANALYZE.

5. Check work_mem when you see external merge or on disk

Sort  (cost=1234.56..1500.00 rows=100000 width=120)
    Sort Method: external merge  Disk: 45MB

external merge Disk means the sort spilled to disk because it did not fit in work_mem. Either the query needs an index that avoids the sort, or work_mem is too low for this workload. Raise it per-session for the query:

SET LOCAL work_mem = '64MB';
SELECT /* expensive sort */;

Bonus: auto_explain for slow production queries

Turn on auto_explain:

-- In postgresql.conf
shared_preload_libraries = 'auto_explain'
auto_explain.log_min_duration = 500     -- log anything > 500ms
auto_explain.log_analyze = true
auto_explain.log_buffers = true

Now every slow query shows up in the log with its full plan. The production version of EXPLAIN. You stop having to guess which query is slow.

The right mental model

Postgres is not a black box. EXPLAIN tells you exactly what it is going to do, and EXPLAIN ANALYZE tells you what it actually did.

Read it like code. Fix what is expensive. Trust the planner when the stats are fresh. When the stats are wrong, fix the stats, not the query.

Nine times out of ten the slow query becomes fast with a single new index or a VACUUM ANALYZE. The tenth time, you restructure the query. Almost never is the answer "buy more hardware".