Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

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 into shared hit (found in Part 15's shared_buffers) vs read (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.

  1. What does Rows Removed by Filter: 199960 suggest?
  2. How do you EXPLAIN ANALYZE a DELETE safely?
Show the answers
  1. The scan read vastly more than it kept — a candidate for an index on the filtered column.
  2. Inside BEGIN ... ROLLBACK — ANALYZE really executes.

Done here? The practice set for Part 16 — 4 exercises with solutions — is on the part page.

Search the guide