Sub topic 8
EXPLAIN ANALYZE: actual time and actual rows
This chapter assumes you can read a basic plan tree from plain EXPLAIN, know what cost and estimated rows mean, and can run SQL in psql or another client. Plain EXPLAIN shows what the planner expects. EXPLAIN ANALYZE shows what happened.
What EXPLAIN ANALYZE adds
Plain EXPLAIN never runs the query. It prints the plan and the planner's estimates of cost and rows. Those estimates can be wrong, and they say nothing about elapsed time.
EXPLAIN ANALYZE executes the statement, then prints the same plan with measurements attached to each node. You get real timings, real row counts, and the number of times each node ran.
EXPLAIN (ANALYZE, BUFFERS)
SELECT c.name, count(*) AS order_count
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.created_at >= DATE '2026-01-01'
GROUP BY c.name
ORDER BY order_count DESC
LIMIT 10;This query joins two tables, aggregates, sorts and limits. The plan contains one node for each of those steps, so it is a good one to practice on.
Reading one plan node
Here is a simplified plan for a join and aggregate query. Your numbers and node choices will differ.
Limit (cost=4210.55..4210.58 rows=10 width=40) (actual time=58.120..58.124 rows=10 loops=1)
-> Sort (cost=4210.55..4223.05 rows=5000 width=40) (actual time=58.118..58.120 rows=10 loops=1)
Sort Key: (count(*)) DESC
Sort Method: top-N heapsort Memory: 26kB
-> HashAggregate (cost=4050.00..4100.00 rows=5000 width=40) (actual time=56.900..57.700 rows=4871 loops=1)
Group Key: c.name
-> Hash Join (cost=180.00..3800.00 rows=50000 width=32) (actual time=2.400..41.300 rows=48210 loops=1)
Hash Cond: (o.customer_id = c.id)
-> Seq Scan on orders o (cost=0.00..3100.00 rows=50000 width=8) (actual time=0.020..28.500 rows=48210 loops=1)
Filter: (created_at >= '2026-01-01'::date)
Rows Removed by Filter: 151790
-> Hash (cost=120.00..120.00 rows=5000 width=32) (actual time=2.300..2.300 rows=5000 loops=1)
-> Seq Scan on customers c (cost=0.00..120.00 rows=5000 width=32) (actual time=0.010..1.100 rows=5000 loops=1)
Planning Time: 0.420 ms
Execution Time: 58.300 msEach node line has two parenthesized groups. The first is the planner's estimate: cost, rows and width. The second, added by ANALYZE, is what the executor measured:
actual time=first..lastgives two times in milliseconds.rowsinside the actual group is the number of rows the node returned per execution.loopsis how many times the node ran.
The footer reports Planning Time and Execution Time separately. Planning time is spent choosing the plan. Execution time covers running it, including executor startup and shutdown, and any triggers.
The two actual times
The first number in actual time is the time until the node produced its first row. The second is the time until it finished returning all of its rows. Both are measured from when the node started.
For a Seq Scan the first value is small, because rows begin to flow immediately. For a node that must consume all of its input before it can emit anything, the first value is large. Sort, HashAggregate and the Hash build side of a hash join all behave this way. In the plan above, HashAggregate shows 56.900..57.700. It spent about 56.9 ms collecting input before it returned its first group.
These times are cumulative. A node's time includes the time spent in its children. To find the time a node spent by itself, subtract its children's totals. The Hash Join finishes at 41.3 ms and its probe-side child, the Seq Scan on orders, finishes at 28.5 ms. The join's own work is therefore not 41.3 ms. A rough estimate of the join's own cost is the difference, along with the time spent building the hash table.
Loops: multiply before you compare
When a node runs more than once, the actual time and rows values are averages per loop. The total time spent in the node is roughly actual time (second value) × loops. The total rows are rows × loops.
This most often appears on the inner side of a nested loop join.
Nested Loop (cost=0.43..950.00 rows=100 width=48) (actual time=0.050..21.000 rows=100 loops=1)
-> Seq Scan on customers c (cost=0.00..20.00 rows=100 width=36) (actual time=0.010..0.200 rows=100 loops=1)
Filter: (region = 'EU'::text)
-> Index Scan using orders_customer_id_idx on orders o (cost=0.43..8.45 rows=1 width=12) (actual time=0.180..0.200 rows=1 loops=100)
Index Cond: (customer_id = c.id)The inner Index Scan reports time=0.180..0.200 and rows=1. That looks cheap. But it ran 100 times, so it consumed about 0.200 × 100 = 20 ms of the join's 21 ms. A node that looks fast per loop can still dominate the plan. Always check loops before you decide a node is harmless.
If a node reports loops=0, the executor never ran it. That is normal when an outer side returned no rows, or when a LIMIT stopped the query early.
Actual rows and the filter counters
The actual rows value is what the node returned to its parent. Rows it examined and discarded do not count. PostgreSQL reports discarded rows on a separate line:
Seq Scan on orders o (actual time=0.020..28.500 rows=48210 loops=1)
Filter: (created_at >= '2026-01-01'::date)
Rows Removed by Filter: 151790This node read 200,000 rows, kept 48,210 and discarded 151,790. A large Rows Removed by Filter next to a small rows is a sign that an index could avoid reading most of the table. Rows Removed by Join Filter and Rows Removed by Index Recheck are the same idea for joins and for lossy index scans.
Estimated versus actual rows
Every node line lets you compare the planner's rows= estimate with the rows= actual. Remember to multiply the actual value by loops when loops is greater than one. A close match means the planner had good information. A large gap means the planner chose the plan on false assumptions. The next chapters cover how to read that gap and what to do about it.
Useful options
ANALYZE is one option in a parenthesized list. Options combine freely.
| Option | Effect |
|---|---|
| `ANALYZE` | Runs the statement and reports actual time, rows and loops. |
| `BUFFERS` | Adds shared, local and temp buffer counts to each node. |
| `VERBOSE` | Adds output column lists and schema-qualified names. |
| `TIMING OFF` | Skips per-node timing but keeps row counts. |
| `SUMMARY` | Controls the planning and execution time footer. |
| `FORMAT JSON` | Produces machine-readable output for tools. |
In recent PostgreSQL versions (18 and later), BUFFERS is turned on automatically with ANALYZE. On older versions, include it yourself. Writing it out does no harm.
Timing overhead and its limits
To measure each node, the executor reads the system clock before and after every row a node produces. On some platforms that is slow. For queries that process millions of rows, the timing calls themselves can add noticeable time. The reported Execution Time can then be higher than the query's real runtime.
If you suspect this, run the same statement with timing disabled:
EXPLAIN (ANALYZE, TIMING OFF)
SELECT count(*)
FROM orders
WHERE created_at >= DATE '2026-01-01';You still get actual row counts and loops, which is often what you need. Compare the Execution Time with and without timing. If they differ a lot, trust the version without timing for the total.
Other caveats apply when you read the numbers:
- Cache state changes results. The first run may read from disk. A second run may find the same pages in memory. Run the statement more than once, and use
BUFFERSto see which case you are looking at. - Output transfer is not included.
EXPLAIN ANALYZEdiscards the result rows. It does not measure the time to send a large result to the client over the network. - Parameters matter. A plan for one literal value may differ from the plan for another. Use the values that are slow in practice.
- Your data should look like production. A plan on a tiny development table often differs from the plan on the real one.
Common mistakes
- Reading time as if it were not cumulative. A parent node always includes its children. Find the expensive node by looking for the large jump between a node and its children.
- Ignoring
loops. Per-loop numbers on the inner side of a nested loop can hide the true cost. - Comparing estimated cost to actual time. Cost is in arbitrary planner units, not milliseconds. Compare estimated rows with actual rows, and judge time using the actual times.
- Running a write statement without a safety net.
EXPLAIN ANALYZE UPDATEchanges data. - Trusting one run. A single timing is affected by cache, concurrent load and timing overhead.
Putting it into practice
Take a query you already run and follow these steps:
- Run plain
EXPLAINand note the estimated rows on each node. - Run
EXPLAIN (ANALYZE, BUFFERS)on the same statement. - Starting at the top, look at
Execution Time, then work down to find the node where the actual time jumps. - For each suspicious node, multiply by
loopsand compare actual rows with estimated rows. - Check
Rows Removed by Filteron scans.
Exercise
A plan shows an inner Index Scan with actual time=0.050..0.060 rows=2 loops=5000, and the parent Nested Loop shows actual time=0.100..310.000 rows=10000 loops=1. How much time did the inner scan spend in total, how many rows did it return in total, and is it a significant part of the join?
Show answer
Total time is about 0.060 ms × 5000 = 300 ms. Total rows are 2 × 5000 = 10,000, which matches the join's output. It accounts for about 300 of the join's 310 ms, so it is the dominant cost even though each loop looks fast.
Exercise
Why can a node's second actual time value be larger than the second value of its child, and what does it mean when a Sort node's first value is almost equal to its second value?
Show answer
Actual times are cumulative, so a parent includes the time spent in its children plus its own work. A Sort must read all of its input before returning any row, so its first value is already large and close to its second value. The remaining difference is the time spent returning the sorted rows.