Sub topic 8
EXPLAIN ANALYZE: actual time and actual rows
Assume you can write SELECT statements with WHERE, JOIN, GROUP BY, and ORDER BY, and that you know an index can help find rows. This chapter uses PostgreSQL syntax. EXPLAIN ANALYZE adds measurements from a real execution to the planner's predicted plan.
What EXPLAIN ANALYZE adds
Plain EXPLAIN predicts how PostgreSQL will run a statement. It does not run the statement.
EXPLAIN ANALYZE runs the statement and reports what happened. Each plan node usually includes:
actual time: elapsed time in millisecondsactual rows: rows produced by the nodeloops: how many times PostgreSQL ran the nodePlanning Time: time spent choosing the planExecution Time: time spent executing the plan
EXPLAIN (ANALYZE, BUFFERS)
SELECT c.name, count(*) AS order_count
FROM customers AS c
JOIN orders AS 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;The output is still a tree. The parent node consumes rows from its child nodes. The actual time values describe when a node first produced a row and when it finished producing rows.
Reading actual time and rows
Consider a line like this:
Index Scan using orders_customer_id_idx on orders
(cost=0.43..12.50 rows=8 width=64)
(actual time=0.030..0.080 rows=12 loops=1)The first group is the estimate:
costis PostgreSQL's internal work estimate, not milliseconds.rows=8is the predicted number of rows.width=64is the predicted average row size in bytes.
The second group is the measurement:
- The first row arrived after
0.030milliseconds. - The node finished after
0.080milliseconds. - It produced
12rows. - PostgreSQL executed it once.
When loops is greater than one, actual time and actual rows are normally averages per loop. To estimate the node's total output, multiply actual rows by loops. A nested loop can therefore show a small number of rows per loop but substantial total work.
Estimates versus measurements
A close estimate helps PostgreSQL choose an efficient join order and scan method. A large difference can lead to a poor plan. For example, an estimate of 8 rows followed by 12 actual rows is usually harmless. An estimate of 8 followed by 800,000 actual rows can cause PostgreSQL to choose a nested loop when a hash join would be better.
Measuring data changes safely
For INSERT, UPDATE, and DELETE, run the statement inside a transaction and roll it back after measurement:
BEGIN;
EXPLAIN (ANALYZE)
UPDATE orders
SET status = 'archived'
WHERE created_at < DATE '2020-01-01';
ROLLBACK;The statement still executes, so triggers and other side effects can occur. The rollback only prevents ordinary transaction changes from being committed.
A practical reading order
When reviewing output:
- Find the node with the largest total actual time.
- Compare estimated rows with actual rows.
- Check whether a high
loopscount multiplies a small per-loop cost. - Inspect the child nodes that feed the expensive node.
- Test a possible fix, then run the same measurement again.
Exercise
A node shows estimated rows=10, actual rows=10,000, and loops=1. What does this suggest?
Show answer
The planner badly underestimated the result size. This can lead to a poor scan or join choice, so inspect statistics, predicates, and the query plan around that node.