2 · Reading a plan
In one sentence: a plan is a tree of nodes that pass rows upwards; read it from the leaves up, subtract each node’s children from its time, and the node with the most time of its own is where your query is slow.
The problem
The shop’s finance page asks for October’s revenue per country:
SELECT c.country, count(*) AS orders, sum(o.total_cents) AS revenue_cents
FROM orders o JOIN customers c ON c.id = o.customer_id
WHERE o.created_at >= '2026-10-01' AND o.created_at < '2026-11-01'
GROUP BY c.country
ORDER BY revenue_cents DESC;
It returns 8 rows in 584 to 683 ms (five runs; below, the 683 ms one). Which node is slow?
This lesson switches parallel workers off for the session (SET max_parallel_workers_per_gather = 0;) so each node
runs once. With them on, the query took 283 ms, with extra Gather and Partial nodes to read.
The tree
Sort (cost=55534.99..55535.01 rows=8 width=19) (actual time=682.952..682.959 rows=8.00 loops=1)
Sort Key: (sum(o.total_cents)) DESC
-> HashAggregate (cost=55534.79..55534.87 rows=8 width=19) (actual time=682.881..682.891 rows=8.00 loops=1)
Group Key: c.country
-> Hash Join (cost=47399.41..55095.34 rows=58593 width=7) (actual time=501.354..650.863 rows=56987.00 loops=1)
Hash Cond: (c.id = o.customer_id)
-> Seq Scan on customers c (cost=0.00..3860.00 rows=200000 width=11) (actual time=0.055..38.196 rows=200000.00 loops=1)
-> Hash (cost=46667.00..46667.00 rows=58593 width=12) (actual time=500.686..500.688 rows=56987.00 loops=1)
-> Seq Scan on orders o (cost=0.00..46667.00 rows=58593 width=12) (actual time=427.528..472.121 rows=56987.00 loops=1)
Filter: ((created_at >= '2026-10-01 00:00:00+00'::timestamp with time zone) AND (created_at < '2026-11-01 00:00:00+00'::timestamp with time zone))
Rows Removed by Filter: 1943013
...
Execution Time: 683.404 ms
The first line and each line starting with -> is a node; the lines under a node without an arrow are its details. Indentation is the tree: a node’s
children are the nodes indented one level deeper under it, and rows flow up from them.
Sort 8 rows
└── HashAggregate 8 rows: one per country
└── Hash Join 56,987 rows: orders with their customer's country
├── Seq Scan on customers 200,000 rows
└── Hash 56,987 rows, in a lookup table
└── Seq Scan on orders 56,987 kept, 1,943,013 removed
The numbers on each node line
| Part | Estimate or real | Means |
|---|---|---|
cost=47399.41..55095.34 | estimate | the planner’s price: before the first row ..to the last, in arbitrary units (Go deeper) |
rows=58593 | estimate | how many rows the planner expects the node to return |
actual time=501.354..650.863 | real | ms until the first row ..until the last |
rows=56987.00 | real | rows the node returned |
width=7 | estimate | average size of a row, in bytes |
loops=1 | real | how many times the node ran |
Compare estimate with real first. Here the planner expected 58,593 October orders and got 56,987: close. Lesson 4 is about when they’re far apart.
Finding the slow node: subtract the children
A node’s actual time includes its children. Sort says 683 ms because everything below it ran before Sort
could finish. To find what a node cost on its own, take its last-row time and subtract its children’s:
| Node | Last row at | Minus children | Own time |
|---|---|---|---|
| Seq Scan on orders | 472.1 | nothing | 472 ms |
| Hash | 500.7 | 472.1 | 29 ms |
| Seq Scan on customers | 38.2 | nothing | 38 ms |
| Hash Join | 650.9 | 500.7 + 38.2 | 112 ms |
| HashAggregate | 682.9 | 650.9 | 32 ms |
| Sort | 683.0 | 682.9 | 0.1 ms |
The Seq Scan on orders is 69% of the query. Its own lines say why: it threw away 1,943,013 rows to keep 56,987. Its first row came out only after 427 ms: October’s orders start on page 15,380 of 16,667.
loops: when a node runs many times
A Nested Loop join runs its inner side once per outer row. Customers 4240 to 4249 with their orders:
SELECT c.email, o.id, o.total_cents FROM customers c JOIN orders o ON o.customer_id = c.id
WHERE c.id BETWEEN 4240 AND 4249;
Nested Loop (cost=4.93..487.74 rows=100 width=34) (actual time=0.156..1.499 rows=88.00 loops=1)
-> Index Scan using customers_pkey on customers c (cost=0.42..8.62 rows=10 width=30) (actual time=0.088..0.100 rows=10.00 loops=1)
-> Bitmap Heap Scan on orders o (cost=4.51..47.80 rows=11 width=20) (actual time=0.029..0.130 rows=8.80 loops=10)
It ran the Bitmap Heap Scan 10 times. With loops above 1, actual time and rows are averages
per run (Using EXPLAIN): multiply them by loops.
0.130 ms × 10 = 1.3 ms of the Nested Loop’s 1.5 ms, and 8.80 × 10 = 88 rows.
Parallel loops run at the same time: under a Gather, multiply rows but not time (in Practical 1,
170 ms × 3 would exceed the whole 204 ms query).
Buffers: pages, per node
Each node’s Buffers line (cut above) includes its children. The
Hash Join’s shared hit=15402 read=3125 is the customers scan’s 1,860 hits plus the Hash’s 13,542 hits and 3,125
reads.
| Count | Means |
|---|---|
hit | pages found in Postgres’s own cache |
read | pages not in its cache, fetched from the operating system |
dirtied | pages this query changed that had no unsaved changes before |
written | changed pages this query had to save to disk to make room in the cache |
written= is about what ran before, not about your query. Postgres’s cache (shared_buffers, 128 MB here)
holds pages other statements changed and nobody has saved yet. To make room for its own pages, your query may have
to save some first
(EXPLAIN, BUFFERS). After a 2,000,000-row UPDATE of
another table, a plain count of orders showed:
Buffers: shared read=16667 written=14
The same count straight after showed no written= at all. In Practical 0, written=147 appeared right after
loading the data: a note about the server’s state at that moment, not about the query.
Try it on your computer
Practical 1 · Where the time goes: find the slow node in “the 20 newest pending orders”, try two indexes, measure each, and break it with one function call.
Go deeper: how the planner priced the Seq Scan on orders
cost is in made-up units where reading one page in order costs 1 (seq_page_cost). The settings, from
pg_settings: seq_page_cost 1, cpu_tuple_cost 0.01 per row, cpu_operator_cost 0.0025 per comparison. The
Seq Scan on orders reads 16,667 pages, looks at 2,000,000 rows, and makes two comparisons per row
(created_at >= and <):
16,667 × 1 + 2,000,000 × 0.01 + 2,000,000 × 2 × 0.0025
= 16,667 + 20,000 + 10,000 = 46,667
That’s the cost=0.00..46667.00 on the node. customers: 1,860 pages + 200,000 × 0.01 = 3,860. The formula
is in Using EXPLAIN. Every other node adds its own
estimate on top of its children’s, which is why costs, like times, grow towards the top.
Common misunderstandings
- “The top node is the slowest.” Its time includes everything below it. Subtract the children first.
- “
costis in milliseconds.” It’s in planner units: never compare it withactual time.
Check yourself
1. A Nested Loop's inner Index Scan shows actual time=0.020..0.025 rows=1.00 loops=50000. The query takes 1.4 s. Is the Index Scan a problem?
0.025 ms × 50,000 = 1,250 ms: most of the query. Each lookup is fast, but there are 50,000 of them. The fix is
usually not a faster lookup but fewer of them (another join method, or fewer outer rows), which lesson 4 covers.
2. A Hash node shows actual time=900.1..900.1, its only child actual time=0.1..880.0. Which node do you look at?
The child: it took 880 ms of its own. The Hash took 900.1 − 880.0 = 20.1 ms.
3. You run EXPLAIN ANALYZE twice. The first run shows written=300, the second none. Is your query writing data?
No. It saved pages that earlier statements had changed, to free room in the cache. A read-only query can do that.
The second run found no such pages to save.
Next: Practical 1 · Where the time goes, then lesson 3 · How rows are found.