Sub topic 2
Reading a plan from the inside out
This chapter assumes you can write SELECT queries with WHERE, JOIN, GROUP BY and ORDER BY, and that you know what an index is. It also assumes you can run SQL in psql or another client. The examples use PostgreSQL.
A plan is a tree
EXPLAIN prints the plan as indented text. Each plan node is one operation, such as scanning a table, joining two inputs, sorting, or aggregating. Nodes nest. A node receives rows from its children, does its work, and passes rows up to its parent. The root node at the top of the output produces the final result.
Take this query:
EXPLAIN
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;A plausible plan for it looks like this:
Limit (cost=4821.40..4821.43 rows=10 width=40)
-> Sort (cost=4821.40..4846.40 rows=10000 width=40)
Sort Key: (count(*)) DESC
-> HashAggregate (cost=4520.10..4620.10 rows=10000 width=40)
Group Key: c.name
-> Hash Join (cost=290.00..4270.10 rows=50000 width=32)
Hash Cond: (o.customer_id = c.id)
-> Seq Scan on orders o (cost=0.00..3600.00 rows=50000 width=4)
Filter: (created_at >= '2026-01-01'::date)
-> Hash (cost=165.00..165.00 rows=10000 width=36)
-> Seq Scan on customers c (cost=0.00..165.00 rows=10000 width=36)The exact numbers on your system will differ. The shape is what matters.
How to read the layout
Three visual rules cover most of what you need.
- Indentation shows nesting. A node indented under another is its child.
->marks a node. The first line (hereLimit) has no arrow because it is the root.- Lines without an arrow are details of the node above them.
Hash Cond,Filter,Sort KeyandGroup Keyare properties of the nearest node, not separate steps.
Here is the same plan as a diagram of how rows move.
Reading from the inside out
"Inside out" means you start at the most deeply indented nodes, the leaves, and work up toward the root. Leaves read the tables. Each step up transforms the rows further.
For the plan above, the story goes like this:
Seq Scan on customersreads every customer row.Hashbuilds an in-memory hash table from them, keyed byid.Seq Scan on ordersreads the orders and keeps those wherecreated_atis on or after 2026-01-01.Hash Joinmatches each surviving order against the hash table usingo.customer_id = c.id.HashAggregategroups the joined rows byc.nameand computescount(*).Sortorders the groups by that count, descending.Limitkeeps the first 10 rows.
Read the leaves first to learn where the data comes from, then follow the parents to see what happens to it. When a plan is slow, this order tells you which step first produces too many rows or does too much work.
Siblings: which child runs first?
When a node has two children, the order matters, and it is not always top to bottom.
For a Hash Join, the first child is the outer (probe) side and the second is the inner (build) side. PostgreSQL must finish building the hash table from the second child before it can probe it. In the example, Seq Scan on customers therefore runs to completion before the first order is matched. The plan text lists orders first, but the hash build happens first.
The same convention applies to other joins. In a Nested Loop, the first child is the outer side, read once. The second child is the inner side, run once per outer row. That is why you often see an inner index scan with loops= greater than 1 in EXPLAIN ANALYZE output, which a later chapter covers.
Rows are pulled, not pushed
PostgreSQL executes a plan with a pull model. The root asks its child for a row, that child asks its own children, and so on down to the scans. Each node produces rows on demand. This has two consequences.
- Pipelined nodes stream rows. A
Seq Scan, aNested Loop, or the probe side of aHash Joincan hand a row upward as soon as it has one. - Blocking nodes must finish their input first. A
Sortcannot emit its first row until it has read all its input.HashAggregatebehaves the same way. AHashnode must finish building its table before the join can start probing.
This is what the two cost numbers in each node's parentheses reflect. The first number is the startup cost, which is the work before the first row can be returned. The second is the total cost. In the example, Sort has a startup cost of 4821.40, which is higher than the HashAggregate total of 4620.10 below it. Sorting could not start returning rows until the aggregate had finished. The next chapter explains what these numbers mean.
The pull model also explains why a Limit can make a plan cheap. If a Limit sits above a node that streams rows, execution stops after enough rows have been pulled, and the children never read the rest of their input. A Limit above a Sort gets no such saving, because the sort must read everything first. (PostgreSQL does use a cheaper "top-N heapsort" in that case, but it still reads all input rows.)
Reading the details under a node
Many nodes carry extra lines. These are the ones you will meet most often.
| Line | Appears on | What it tells you |
|---|---|---|
| `Filter:` | Scans, joins | A condition checked on each row after it is read. Rows that fail are discarded. |
| `Index Cond:` | Index scans | A condition the index itself used to find rows. |
| `Hash Cond:` / `Merge Cond:` | Joins | The join condition that matches rows. |
| `Sort Key:` | Sort | The columns or expressions being sorted on. |
| `Group Key:` | Aggregates | The columns being grouped on. |
Index Cond and Filter look similar but do different work. A condition in Index Cond narrows which rows are fetched. A condition in Filter is checked after rows are already read, so it saves no reading. Later chapters on scan types come back to this.
Special nodes that change the shape
A few node types break the simple "children feed parents" picture.
InitPlan and SubPlan
A subquery that does not depend on the outer query is run once and appears as an InitPlan. A subquery that depends on the outer row can appear as a SubPlan, which is evaluated per row. Both sit under the node that uses them, and the result is referenced as $0, $1 and so on.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = (SELECT id FROM customers WHERE email = '[email protected]');Seq Scan on orders (cost=8.30..3608.30 rows=5 width=48)
Filter: (customer_id = $0)
InitPlan 1 (returns $0)
-> Index Scan using customers_email_key on customers (cost=0.29..8.30 rows=1 width=4)
Index Cond: (email = '[email protected]'::text)Here the InitPlan is read first, and the scan on orders uses its single result as a constant. A SubPlan is a warning sign when it holds a large scan, because it may run once per outer row.
Gather and parallel nodes
When PostgreSQL uses parallel workers, a Gather node appears above nodes named Parallel Seq Scan and similar. Read it like any other parent. The subtree below Gather is what each worker runs, and Gather collects their rows.
Common mistakes when reading plans
- Reading top to bottom as a list of steps. The plan is a tree, not a script. Siblings do not run strictly in the order printed.
- Treating a detail line as a node.
Filter:andHash Cond:have no arrow, so they are not separate steps. - Judging a node by its own cost alone. A node's cost includes all of its children. To see what a node adds, subtract the costs of its children. A later chapter on finding the expensive node uses this.
- Ignoring blocking nodes. A
SortorHashhigh in the tree means everything below it must complete first.
Exercise
In the plan from this chapter, which scan runs to completion first, Seq Scan on orders or Seq Scan on customers, and why? Also, which line in the plan tells you the orders scan discards rows after reading them?
Show answer
Seq Scan on customers finishes first. It is the second child of the Hash Join, so it feeds the Hash node that builds the hash table. The join cannot start probing until that table is complete. The Filter: (created_at >= '2026-01-01'::date) line under Seq Scan on orders shows that rows are read and then discarded when they fail the condition.