Reading EXPLAIN ANALYZE
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.
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.
- 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.
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:
| Field | Bracket | Meaning |
|---|---|---|
| cost=4.45..77.18 | estimate | Planner cost units, not milliseconds: before the first row, then for all rows. Only useful for comparing plans. |
| rows=20 | estimate | How many rows the planner expected this node to return, per run. |
| width=10 | estimate | Expected average row size in bytes. |
| actual time=0.010..0.024 | measured | Milliseconds until the first row, then until the last row. An average per loop. |
| rows=21 | measured | Rows actually returned. Also an average per loop. |
| loops=100 | measured | How 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:
rows=5 in the estimate bracket.rows=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.
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.
| Node | time × loops | minus children | self time |
|---|---|---|---|
| Nested Loop | 3.042 × 1 = 3.04 | − 0.31 − 2.40 | ≈ 0.33 ms |
| Seq Scan on customers | 0.305 × 1 = 0.31 | none | ≈ 0.31 ms |
| Bitmap Heap Scan on orders | 0.024 × 100 = 2.40 | − 0.80 | ≈ 1.60 ms |
| Bitmap Index Scan | 0.008 × 100 = 0.80 | none | ≈ 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.
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.