EXPLAIN & ANALYZE
Postgres · Reference cheat sheet
EXPLAIN & ANALYZE
Postgres · Reference cheat sheet
📋 Overview
EXPLAIN shows the planner’s chosen plan; EXPLAIN ANALYZE executes the query and adds actual timings/rows. Essential for diagnosing slow queries and verifying indexes.
🔧 Core concepts
| Option | Adds |
|---|---|
ANALYZE | Actual runtime stats |
BUFFERS | Shared/hit/read buffer counts |
VERBOSE | Extra detail |
WAL | WAL generation (newer versions) |
SETTINGS | Relevant GUCs |
| Node tip | Meaning |
|---|---|
| Seq Scan | Full table read |
| Index Scan / Only | Using index |
| Nested Loop / Hash / Merge | Join strategies |
| rows vs actual rows | Estimator accuracy |
💡 Examples
Basic:
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42;With buffers:
EXPLAIN (ANALYZE, BUFFERS)
SELECT o.*
FROM orders o
JOIN users u ON u.id = o.user_id
WHERE u.email = 'ada@example.com';Compare plans:
SET enable_seqscan = off; -- diagnostic only
EXPLAIN ANALYZE SELECT ...;⚠️ Pitfalls
EXPLAIN ANALYZEruns the query — careful with writes (use a transaction +ROLLBACKwhen testing DML).- Stale statistics → bad plans —
ANALYZEthe tables. - Reading plans takes practice — start with the most expensive node (time).