Skip to content

Reading EXPLAIN ANALYZE

Working knowledgeConcept

EXPLAIN ANALYZE prints a dense block of numbers, and it is hard to know which ones matter. Here is how to read the plan tree, compare the planner's guesses with what happened, and find the node where the time went.

01 A slow query and a wall of text

A report lists every order placed by customers in Amsterdam. You put EXPLAIN ANALYZE in front of it, and Postgres runs the query and prints how it did it. The output below is real, from Postgres 16 on a toy database: 10,000 customers in 100 cities, and 200,000 orders with an index on orders.customer_id.

report.sql
EXPLAIN ANALYZE
SELECT c.name, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.city = 'Amsterdam' AND c.country = 'NL';
Text
Nested Loop (cost=4.45..610.91 rows=100 width=11) (actual time=0.022..3.042 rows=2086 loops=1)
-> Seq Scan on customers c (cost=0.00..224.00 rows=5 width=9) (actual time=0.005..0.305 rows=100 loops=1)
Filter: ((city = 'Amsterdam'::text) AND (country = 'NL'::text))
Rows Removed by Filter: 9900
-> Bitmap Heap Scan on orders o (cost=4.45..77.18 rows=20 width=10) (actual time=0.010..0.024 rows=21 loops=100)
Recheck Cond: (c.id = customer_id)
Heap Blocks: exact=2069
-> Bitmap Index Scan on orders_customer_id_idx (cost=0.00..4.45 rows=20 width=0) (actual time=0.008..0.008 rows=21 loops=100)
Index Cond: (customer_id = c.id)
Planning Time: 0.720 ms
Execution Time: 3.118 ms

Every line that starts with -> is a plan node: one small job, such as scanning a table or joining two inputs. The lines under a node, such as Filter:, add detail about that node. To read the output you need three things: the shape of the tree, the two sets of numbers on each node, and how to turn them into time.

02 A plan is a tree that pulls rows upward

Indentation is the tree. A node's children are the -> lines indented under it. Here Nested Loop has two children: a Seq Scan on customers (the outer side, listed first) and a Bitmap Heap Scan on orders (the inner side), which has its own child, the index scan.

Rows flow from the leaves up. Each node asks its children for rows one at a time, does its job, and hands rows to its parent. A Nested Loop runs its inner side once for every row the outer side gives it. Watch it on a toy with three matching customers.

How a Nested Loop pulls rows
Step 1 of 8
Inner loops 0Rows out 0
Plan tree · a toy with 3 Amsterdam customers
Nested Loop
Seq Scan customers
Bitmap Heap Scan orders
Bitmap Index Scan
Rows the Nested Loop hands up
Empty
  • running now
  • waiting on a child
  • row just handed up
  • finished
  • not started

Nothing has run yet. The top node, Nested Loop, will ask its children for rows when someone asks it for one.

0 outer rows

Read a plan from the most indented lines outward. The leaves are where rows come from; the top line is where they leave.

03 What the numbers on one line mean

Each node has two brackets. The first is the planner's guess, made before running. The second is what happened, and only ANALYZE adds it. Take the inner node from the real plan:

Text
Bitmap Heap Scan on orders o (cost=4.45..77.18 rows=20 width=10)
(actual time=0.010..0.024 rows=21 loops=100)
The fields on a plan node
FieldBracketMeaning
cost=4.45..77.18estimatePlanner cost units, not milliseconds: before the first row, then for all rows. Only useful for comparing plans.
rows=20estimateHow many rows the planner expected this node to return, per run.
width=10estimateExpected average row size in bytes.
actual time=0.010..0.024measuredMilliseconds until the first row, then until the last row. An average per loop.
rows=21measuredRows actually returned. Also an average per loop.
loops=100measuredHow many times the node ran. Multiply time and rows by this.

actual time and rows are per loop. This node did not take 0.024 ms in total: it ran 100 times, so it took about 2.4 ms and returned about 2,100 rows.

04 Estimated vs actual rows

Every choice the planner makes, such as which join to use or whether to touch an index, comes from its row estimates. So the first thing to check on each node is how far rows= in the first bracket is from rows= in the second. Look at the scan on customers:

Planner expected5 rowsrows=5 in the estimate bracket.
Scan returned100 rowsrows=100 in the actual bracket: 20 times more.

The planner keeps statistics on each column separately. It knows 1% of customers live in Amsterdam and 5% live in the Netherlands, and by default it assumes the two conditions are unrelated, so it multiplies: 10,000 × 0.01 × 0.05 = 5. But every Amsterdam customer is in the Netherlands, so the second condition removes nobody. The error then travels up: the join expected 100 rows and got 2,086.

You can tell the planner that two columns move together with extended statistics (Postgres 10 and later). After ANALYZE, the same scan is estimated at rows=100.

SQL
CREATE STATISTICS customers_city_country (dependencies)
ON city, country FROM customers;
ANALYZE customers;

A gap of a few times is normal. Look for gaps of ten times or more, starting from the leaves: the lowest bad estimate is usually the cause, and the ones above it are knock-on effects.

05 Where the time went

A node's actual time includes the time spent in its children, because a parent waits while a child makes its rows. To find the node that did the work, take each node's last time, multiply by loops, and subtract what its children took. That leftover is its self time.

Self time per node, from the plan above
Nodetime × loopsminus childrenself time
Nested Loop3.042 × 1 = 3.04− 0.31 − 2.40≈ 0.33 ms
Seq Scan on customers0.305 × 1 = 0.31none≈ 0.31 ms
Bitmap Heap Scan on orders0.024 × 100 = 2.40− 0.80≈ 1.60 ms
Bitmap Index Scan0.008 × 100 = 0.80none≈ 0.80 ms

The self times add up to the 3.04 ms at the top. Most of it went into the inner side of the join, fetching orders 100 times. If this query were slow, that is where to look: fewer outer rows, a cheaper inner lookup, or a different join.

Planning Time is how long the planner took to choose the plan. Execution Time is the whole run, including starting up and shutting down the nodes, so it is a little more than the top node's time.

06 Ask for more, safely

Add BUFFERS to see how many 8 kB pages each node touched. shared hit pages came from Postgres's cache; read pages had to come from the operating system or disk. Page counts do not change with machine load the way milliseconds do, so they are good for comparing two versions of a query. From Postgres 18, EXPLAIN ANALYZE includes buffers by default.

SQL
EXPLAIN (ANALYZE, BUFFERS)
SELECT c.name, o.total FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.city = 'Amsterdam' AND c.country = 'NL';

EXPLAIN ANALYZE really runs the statement. On an UPDATE or DELETE, the rows change. Wrap it: BEGIN; EXPLAIN ANALYZE DELETE ...; ROLLBACK;

07 When the output misleads you

loops=100Forgetting to multiply by loopsA node with a tiny actual time can be the slowest part of the plan if it ran thousands of times. Always multiply.
Rows Removed by Filter: 9900A filter throwing most rows awayThe scan read 10,000 rows to keep 100. On a big table, that usually means a missing index on the filtered column.
Limit -> Index Scan ... rows=10A child stopping early under LimitUnder a Limit, a child may show far fewer actual rows than estimated because the parent stopped asking. That is not a bad estimate.
random_page_cost = 4Right estimates, slower planWith the extended statistics added, this toy switched to a Hash Join that ran in about 15 ms instead of 3 ms. The default cost settings assume random reads are 4 times dearer than sequential ones; with everything cached, they are not. Lowering random_page_cost (often to about 1.1 on SSDs) brought back the nested loop.
first run vs second runCold cache, warm cacheThe first run may read pages from disk and the next from memory. Run it a few times and compare BUFFERS, not only milliseconds.

08 Check yourself

Question

A node shows actual time=0.050..0.200 rows=3 loops=500. Roughly how long did it take in total?

Question

A node says cost=0.00..224.00. What does 224 mean?

Question

The leaf scan estimates rows=5 and returns 100; the join above estimates 100 and returns 2,086. Which estimate do you fix first?

09 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?