Part 16 / Topic 2 of 5
EXPLAIN ANALYZE and BUFFERS
Add ANALYZE and the query actually runs, with reality printed next to every estimate:
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM big_orders WHERE customer_id = 42;
QUERY PLAN
-------------------------------------------------
Seq Scan on big_orders (actual rows=40 loops=1)
Filter: (customer_id = 42)
Rows Removed by Filter: 199960
(3 rows)
The lines that matter: actual rows=40 (the estimate was right), and Rows Removed by Filter: 199960 — this scan read everything and threw away 99.98% of it. That one line is the most common "an index would help" smell in all of Postgres, and section 3 acts on it.
Two options you'll flip constantly in real work (left off in these lessons only because their numbers change run to run):
TIMING ON(the default): actual milliseconds per node — where the time went.BUFFERS ON: pages touched per node, split intoshared hit(found in Part 15's shared_buffers) vsread(had to come from OS/disk) — whether the time went to cache or to I/O.
And the warning that keeps audit scripts safe: EXPLAIN ANALYZE executes the statement — an UPDATE or DELETE really happens. Wrap writes in BEGIN; EXPLAIN ANALYZE ...; ROLLBACK; — Part 11's safety net, repurposed for diagnostics.
🧪 Quick quiz — answer from memory, then check.
- What does
Rows Removed by Filter: 199960suggest? - How do you EXPLAIN ANALYZE a DELETE safely?
Show the answers
- The scan read vastly more than it kept — a candidate for an index on the filtered column.
- Inside BEGIN ... ROLLBACK — ANALYZE really executes.
Done here? The practice set for Part 16 — 4 exercises with solutions — is on the part page.