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..totalis in arbitrary units (sequential page =seq_page_cost1.0, random page =random_page_cost4.0 by default). rows is the estimate;actual rowsis the truth. - Loops: actual time and rows are per loop; multiply by
loopsfor the node’s total. - BUFFERS:
shared hit(in cache) vsread(from OS/disk),dirtied,written;temp read/writtenmeans a sort or hash spilled to disk because ofwork_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: 166643Interpretation: 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:
- Find the node with the largest actual time x loops (exclusive of children).
- Compare
rowswithactual rows. An error of 10x or more is the usual root cause; runANALYZE, add extended statistics (CREATE STATISTICSfor correlated columns), or rewrite the predicate. Rows Removed by Filtermuch larger than rows kept: missing or unusable index.Sort Method: external merge DiskorBatches > 1on a hash: raisework_memfor that query (SET LOCAL work_mem = '64MB').Nested Loopwith a large outer side andloopsin the thousands: consider an index on the inner join column, or a hash join.- High
readcounts mean cold cache; run twice and compare.
Pitfalls and production notes
EXPLAIN ANALYZEonINSERT/UPDATE/DELETEexecutes the change. Wrap it inBEGIN; ... ROLLBACK;.- Timing overhead can distort fast nodes; use
TIMING OFFfor 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) andpg_stat_statements(pg-monitoring). - Paste plans into a visualiser such as explain.depesz.com or explain.dalibo.com when they are large.
Practice questions
- A node shows
actual time=0.05..0.06 rows=1 loops=20000. What is its real cost? - Why can the planner choose a sequential scan although an index exists?
- What does
Heap Fetches: 40000on an Index Only Scan indicate?
References
- PostgreSQL manual, Using EXPLAIN: https://www.postgresql.org/docs/current/using-explain.html
- PostgreSQL manual, auto_explain: https://www.postgresql.org/docs/current/auto-explain.html