How to read PostgreSQL EXPLAIN ANALYZE output
Understand PostgreSQL query plans: what EXPLAIN and EXPLAIN ANALYZE show, the common plan nodes, and how to find the slow step in a query.
Updated · 6 min read · By Piyush Gupta
EXPLAIN vs EXPLAIN ANALYZE
EXPLAIN shows the plan PostgreSQL intends to use and its estimates, without running the query. EXPLAIN ANALYZE actually runs the query and adds real timings and row counts to each step.
EXPLAIN ANALYZE
SELECT * FROM orders WHERE customer_id = 42;Note: EXPLAIN ANALYZE really executes the statement, so an UPDATE or DELETE will change data. Wrap it in BEGIN; … ROLLBACK; to be safe.
Reading a plan node
Seq Scan on orders (cost=0.00..1834.00 rows=98 width=64)
(actual time=0.015..12.401 rows=104 loops=1)
Filter: (customer_id = 42)
Rows Removed by Filter: 99896- cost=start..total: the planner's estimate in arbitrary units. Compare costs between plans, not to milliseconds.
- rows (estimate) vs actual rows: a big gap means the planner's statistics are off. Running ANALYZE on the table often helps.
- actual time=first..last: milliseconds to the first row and to the last row, per loop.
- loops: how many times the step ran. Multiply time and rows by loops for the real total.
- Rows Removed by Filter: rows read and then thrown away. A large number here often means a missing index.
Common plan nodes
- Seq Scan: reads the whole table. Fine for small tables; slow on big ones when you only need a few rows.
- Index Scan / Index Only Scan: uses an index to find rows. Index Only Scan doesn't need to visit the table at all.
- Bitmap Heap Scan: collects matching row locations from an index first, then reads them in bulk. Good for medium result sets.
- Nested Loop: for each row on one side, looks up matches on the other. Fast when the outer side is small and the inner side is indexed.
- Hash Join: builds a hash table from one side. Good for joining larger sets without useful indexes.
- Sort / HashAggregate: sorting and grouping. Watch for 'external merge Disk' in a Sort, which means it ran out of work_mem.
Finding the slow step
- Look for the node with the largest actual time multiplied by loops; parents include their children's time.
- Compare estimated and actual rows. Estimates off by 10x or more point to stale statistics or a complex filter.
- Check Seq Scans with many Rows Removed by Filter; an index on the filtered column usually fixes them.
- Add BUFFERS (EXPLAIN (ANALYZE, BUFFERS)) to see how much data came from disk versus cache.
PostCompass shows EXPLAIN ANALYZE as a visual tree with timing bars, flags the slowest step, highlights estimate-versus-actual mismatches, and runs ANALYZE inside a transaction that's rolled back, so it's safe on writes.