Overview

EXPLAIN shows the plan PostgreSQL chose; EXPLAIN ANALYZE also runs the query and reports actual rows, time and loops per node. It is the primary tool for query tuning (query-planning explains the model behind it).

Core concepts

EXPLAIN (ANALYZE, BUFFERS, VERBOSE, SETTINGS) SELECT ...;
  • Cost cost=startup..total is in arbitrary units (sequential page = seq_page_cost 1.0, random page = random_page_cost 4.0 by default). rows is the estimate; actual rows is the truth.
  • Loops: actual time and rows are per loop; multiply by loops for the node’s total.
  • BUFFERS: shared hit (in cache) vs read (from OS/disk), dirtied, written; temp read/written means a sort or hash spilled to disk because of work_mem.
  • Read the tree bottom-up, inner to outer. A node’s time includes its children.
  • Common nodes: Seq Scan, Index Scan, Index Only Scan, Bitmap Index Scan + Bitmap Heap Scan, Nested Loop, Hash Join, Merge Join, Sort, HashAggregate, GroupAggregate, Gather (parallel).

Worked example

From the 500,000-row big table (indexing-theory):

Gather (actual rows=72 loops=1)
  Workers Planned: 2  Workers Launched: 2
  Buffers: shared hit=3378
  -> Parallel Seq Scan on big (actual rows=24 loops=3)
       Filter: (customer_id = 42)
       Rows Removed by Filter: 166643

Interpretation: three processes each scanned a third of the table (loops=3, 24 rows each, 72 total) and discarded about 500,000 rows to keep 72. That ratio is the signal for an index. After CREATE INDEX the plan becomes a Bitmap Index Scan and Buffers drops to about 75.

A checklist for a slow plan:

  1. Find the node with the largest actual time x loops (exclusive of children).
  2. Compare rows with actual rows. An error of 10x or more is the usual root cause; run ANALYZE, add extended statistics (CREATE STATISTICS for correlated columns), or rewrite the predicate.
  3. Rows Removed by Filter much larger than rows kept: missing or unusable index.
  4. Sort Method: external merge Disk or Batches > 1 on a hash: raise work_mem for that query (SET LOCAL work_mem = '64MB').
  5. Nested Loop with a large outer side and loops in the thousands: consider an index on the inner join column, or a hash join.
  6. High read counts mean cold cache; run twice and compare.

Pitfalls and production notes

  • EXPLAIN ANALYZE on INSERT/UPDATE/DELETE executes the change. Wrap it in BEGIN; ... ROLLBACK;.
  • Timing overhead can distort fast nodes; use TIMING OFF for cheap queries.
  • Plans for prepared statements may use a generic plan after five executions; compare with plan_cache_mode.
  • Production visibility: auto_explain (log plans of slow statements) and pg_stat_statements (pg-monitoring).
  • Paste plans into a visualiser such as explain.depesz.com or explain.dalibo.com when they are large.

Practice questions

  1. A node shows actual time=0.05..0.06 rows=1 loops=20000. What is its real cost?
  2. Why can the planner choose a sequential scan although an index exists?
  3. What does Heap Fetches: 40000 on an Index Only Scan indicate?

References

query-planning · pg-index-types · pg-tuning · pg-monitoring