liveBook · Manning

EXPLAIN

As explained in Just Use Postgres!

EXPLAIN ANALYZE is a PostgreSQL command form that executes a statement and displays its execution plan with actual runtime details.

EXPLAIN ANALYZE executes a PostgreSQL statement and displays its execution plan together with actual runtime details. Unlike EXPLAIN alone, it reports Planning Time, Execution Time, actual rows, and loop counts. section 4.4

Inspect an indexed lookup

Run EXPLAIN ANALYZE SELECT username, level, score FROM game.player_stats WHERE player_id = 250;. The plan uses the player_stats_pkey Index Scan, reports actual timing and rows, then shows Planning Time and Execution Time. Repeat the query until timing values become consistent. Because ANALYZE executes the statement, use caution with INSERT, UPDATE, and DELETE. section 4.4

EXPLAIN ANALYZE 
SELECT username, level, score 
FROM game.player_stats
WHERE player_id = 250;

Runnable SQL for the indexed lookup, paired with its execution plan and timing output. section 4.4

                            QUERY PLAN
-----------------------------------------------------------------
 Index Scan using player_stats_pkey on player_stats  
 ➥  (cost=0.29..8.30 rows=1 width=18)
 ➥  (actual time=0.042..0.044 rows=1 loops=1)
   Index Cond: (player_id = 250)
   Buffers: shared hit=3
 Planning Time: 0.119 ms
 Execution Time: 0.076 ms
(5 rows)

Extended output shows buffer usage alongside the plan and runtime measurements. section 4.4

How Just Use Postgres! covers it

How deep it goes
Distinguishes estimated plans from executed plans and adds timing, row, loop, buffer, and statement-effect details.
Examples
Demonstrates an indexed player lookup and an extended plan reporting shared buffer hits.
What it assumes
Assumes familiarity with PostgreSQL queries, tables, and indexes; distinguishes EXPLAIN from execution-based analysis.
What you can do after
Diagnose/debug with it: verify access methods and compare planning, execution, row, loop, and buffer measurements.
How it connects
Connects execution plans with indexes, index scans, query planning, and B-tree primary-key indexes.

Also explained in

Compare how 3 books explain EXPLAIN →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.