the executor and the iterator model
every plan node answers one question: next row, please? press next() and watch one row climb.
- address
- everything.execution.iterator_model
- part
- part 9, how a query runs
- chapter
- chapter 6 of 13
- reading time
- 9 min
every plan node is an iterator that answers one question: next row, please? the client asks the top node, the top node asks its child, and so on down to the scans. rows are pulled up the tree one at a time, so a LIMIT can stop the whole thing early.
except at blocking nodes: a Sort, a Hash or an aggregate has to read all of its input before it can answer the first time.
the picture
you know how LIMIT 3 on a huge table comes back instantly, but ORDER BY price DESC LIMIT 3 on the same table takes ages? same three rows. it's not the LIMIT doing the work, it's who's asking.
think of a bucket brigade up a ladder. nobody passes a bucket until the person above holds out an empty hand. the client at the top holds out a hand (calls next()), the Limit below turns and holds out its own, and so on down to the scan at the bottom, which scoops one row and passes it up. when the person at the top has three buckets, they stop holding out their hand, and the whole ladder goes still. nobody fills buckets just in case.
the Limit asks for a row, the scan reads until one passes the filter, hands it up, and waits. after the third, the Limit stops asking.
| id | name | price |
|---|
startthe client asks the root for a row. every node only ever asks its child for the next one.
with LIMIT 3, the scan reads 7 of the 24 rows: 3 that pass price > 50 and 4 that don't. then the Limit says "no more", and the other 17 rows are never touched. that's the whole trick behind fast LIMIT queries, and it's why the model's counts match postgres's exactly: they're both just counting calls.
the same thing, for real
a toy table, small enough to watch: 24 products, built by a hidden setup block.
SELECT id, name, price FROM products ORDER BY id;| idint4 | nametext | pricenumeric |
|---|---|---|
| 1 | spade-1 | 42.13 |
| 2 | kettle-2 | 79.26 |
| 3 | chess-3 | 26.39 |
| 4 | atlas-4 | 63.52 |
| 5 | rake-5 | 10.65 |
| 6 | kite-6 | 47.78 |
| 7 | mug-7 | 84.91 |
| 8 | teapot-8 | 31.04 |
ask for three expensive ones, with no order:
EXPLAIN (ANALYZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, name, price FROM products WHERE price > 50 LIMIT 3;actual rows=3.00 on the scan, and Rows Removed by Filter: 4. seven rows looked at. the scan is a cursor parked on row 7, and nobody will ever ask it to move again.
SELECT id, name, price FROM products
WHERE price > 50
ORDER BY price DESC
LIMIT 3;EXPLAIN (ANALYZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, name, price FROM products WHERE price > 50 ORDER BY price DESC LIMIT 3;12 rows passed, 12 were removed: 24, the whole table. the Limit still stopped the Sort after 3 rows (actual rows=3.00), but by then the work was done. postgres knows this, so its sort only keeps the best 3 while it reads (a top-N heapsort, more in sorting), which saves memory, not reading.
streaming and blocking
so there are two kinds of node. streaming nodes return a row as soon as they have one: scans, filters, Limit, the probe side of a join. blocking nodes have to consume all of their input before they can return the first row:
| node | kind | why |
|---|---|---|
| Seq Scan, Index Scan, Filter | streaming | each row is ready the moment it's read |
| Limit | streaming | passes rows through, then stops asking |
| Nested Loop, Merge Join | streaming | a match is ready when both sides have a row |
| Hash Join | blocking, then streaming | builds the whole hash table first, then streams the other side |
| Sort | blocking | the first row out could be the last row in |
| HashAggregate, plain Aggregate | blocking | a group isn't finished until the input is |
| GroupAggregate | streaming | input sorted by the key: a group is done when the key changes |
blocking is exactly what a high startup cost means in reading EXPLAIN: the work before the first row. with a LIMIT on top, the planner prefers plans with low startup cost, even when their total is higher, because it knows the pipeline will stop early.
the interface
in postgres's source, that question is three functions, one set per node type, in src/backend/executor/:
hover a stage.
a node never sees the whole plan. a Hash Join doesn't know whether its outer side is a table, an index or another join; it just calls next() on it. that's why plans compose so freely, and why the joins and sorts in this part are each one small machine.
you can be the client
the client at the top of the ladder is usually your driver, asking for everything. a cursor lets you hold out your hand one row at a time yourself:
BEGIN;
DECLARE c CURSOR FOR SELECT id, name FROM customers ORDER BY id;
FETCH 3 FROM c;
FETCH 2 FROM c;
COMMIT;| idint4 | nametext |
|---|---|
| 1 | Maya Chen |
| 2 | Grace Okafor |
| 3 | Sam Lee |
| idint4 | nametext |
|---|---|
| 4 | Zoë Martin |
| 5 | Zoe Martin |
each FETCH runs the plan just far enough to return that many rows, then the plan sits there, paused, scan position and all, until the next one. that's the iterator model with you as the parent node.
one level deeperwhy not push rows instead?
the pull model is Goetz Graefe's, from the Volcano system (1994), which is why it's also called the volcano model. it costs one function call per row per node, which was fine when disks were the bottleneck. newer engines flip it around: a scan pushes a whole batch of rows (a vector of a thousand values per column) through a pipeline of tight loops, and the CPU stays busy instead of jumping between nodes. duckdb works that way. postgres still pulls one row at a time; its JIT (jit = on past a cost threshold) compiles expressions and tuple deforming to machine code, not the pull loop itself. column stores and vectorized execution has the comparison.
keylines
- every plan node answers one question: next row, please?
- rows are pulled up the tree one at a time; nothing runs until someone asks.
- LIMIT stops asking, so streaming plans stop early.
- Sort, Hash and aggregates block: they read all their input before their first row.
- blocking work is what startup cost measures.