Skip to content

Lesson 2 of 13 · 15m

Reading EXPLAIN Like a Mechanic

EXPLAIN (ANALYZE, BUFFERS) is a plan tree read inside-out, where estimated-versus-actual rows and hit-versus-read buffers point at the real problem.

EXPLAIN is the only window Postgres gives you into the planner's head. The command most people know — plain EXPLAIN — prints the plan Postgres would run, with estimated costs and rows. The command you actually want is EXPLAIN (ANALYZE, BUFFERS), which runs the query for real and annotates every node with measured time, measured rows, and which 8kB buffers it had to fetch from disk versus find in shared memory.

The output is a tree, read it like one

Nested Loop  (cost=0.85..1689.00 rows=6000 width=44) (actual time=0.031..1820.441 rows=6000 loops=1)
  ->  Seq Scan on customers c (cost=0.00..35.50 rows=2000 width=8) (actual rows=2000 loops=1)
  ->  Index Scan using orders_customer_idx on orders o (cost=0.42..8.45 rows=3 width=36) (actual rows=3 loops=2000)

Read inside-out, bottom-up. Leaf nodes touch data: scans and index lookups. Interior nodes combine streams: nested loop, hash join, merge join, sort, aggregate. Each parent consumes its children's output. The cost=0.85..1689.00 is two numbers — startup cost before the first row can emerge, and total cost if the node runs to completion — measured in abstract units anchored to seq_page_cost = 1.0. Cost is not milliseconds. It's a relative scale the planner uses to rank candidate plans; comparing cost to wall-clock time is the first misreading everyone makes.

The second misreading: rows=6000 on the top line looks like a promise the query returns 6,000 rows. Sometimes it does. More often it's the planner's estimate of the surviving rows, and the estimate is what the entire plan was built around. On the inner Index Scan two counters do different jobs: rows=3 is the estimate per execution — about three orders per customer — and loops=2000 is how many times the node actually ran, once per row the outer Seq Scan produced. If the outer side had produced 3 rows, the nested loop was a great choice. If it produces 2 million, you just watched the engine do 2 million index descents it never should have started.

The three checks that catch 90% of bad plans

Estimate versus actual. Walk the tree comparing rows= (estimated) to rows= (actual after ANALYZE). The first node where they diverge by an order of magnitude is where the plan went wrong, and everything above inherited the mistake. Usual cause: stale pg_statistic from ANALYZE, or a predicate the stats can't express — correlated columns (WHERE city='Springfield' AND state='IL' gets multiplied as independent probabilities and undercounts badly; CREATE STATISTICS on the pair fixes it).

Where the time actually went. actual time accumulates up the tree. Find the deepest node burning most of the total. A Seq Scan showing Rows Removed by Filter: 4800000 to return 900 rows is the engine telling you it read the whole table for almost nothing — that predicate wants an index or a partition boundary.

Hit versus read. With BUFFERS, every node reports shared hit (found in shared_buffers) and read (went to the OS/disk). A plan that looks cheap but shows read=800000 is I/O-bound and will be slow every cold run. Turn on track_io_timing = on to get I/O Timings per node and see whether you're waiting on storage or on CPU.

The fleet-level version

EXPLAIN is for one query. pg_stat_statements is for all of them: CREATE EXTENSION pg_stat_statements, then group by queryid and sort by total_exec_time. The query that matters is rarely the one that's slow once — it's the 40ms query that runs 400,000 times a day. For plan drift in production, auto_explain — it has to be loaded via shared_preload_libraries — with auto_explain.log_min_duration logs the full plan of any query exceeding a threshold, no manual run required.

What to actually do

  • Use EXPLAIN (ANALYZE, BUFFERS) as your default. ANALYZE without BUFFERS hides the I/O story.
  • Check estimate-vs-actual at the deepest node first. Fix the statistics (ANALYZE, SET STATISTICS, CREATE STATISTICS for correlated columns) before touching the query.
  • Rows Removed by Filter bigger than returned rows is the smell for a missing index or a badly-shaped one.
  • Install pg_stat_statements on every production database. It is the difference between guessing which query hurts and knowing.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.