skip to content

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.

query =
SELECT id, name, price FROM products WHERE price > 50 LIMIT 3

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.

rows the client has received
idnameprice
(0 rows)

startthe client asks the root for a row. every node only ever asks its child for the next one.

call 1/15
model of the executor's pull loop, simplified · checked against postgres 18.6
real output, postgres 18.6 (PGlite 0.5.12) · volcano
pick a query, then press step for one call at a time or to the next row for one result row. the hot edge is the call in flight: next() going down, a row coming up. under each node, how many rows it has returned so far next to what EXPLAIN ANALYZE reported for it.

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.

sqlempty db · session toy
continues session toy (1 earlier block run first)
SELECT id, name, price FROM products ORDER BY id;
output, from postgres 18.6
SELECT 24
idint4nametextpricenumeric
1spade-142.13
2kettle-279.26
3chess-326.39
4atlas-463.52
5rake-510.65
6kite-647.78
7mug-784.91
8teapot-831.04
(24 rows)showing 8 ·

ask for three expensive ones, with no order:

sqlempty db · session toy
continues session toy (2 earlier blocks run first)
EXPLAIN (ANALYZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT id, name, price FROM products WHERE price > 50 LIMIT 3;
output, from postgres 18.6
EXPLAIN
Limit (actual rows=3.00 loops=1) -> Seq Scan on products (actual rows=3.00 loops=1) Filter: (price > '50'::numeric) Rows Removed by Filter: 4

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.

predict the output
now with ORDER BY price DESC. how many rows does the scan look at?
sql
SELECT id, name, price FROM products
WHERE price > 50
ORDER BY price DESC
LIMIT 3;
sqlempty db · session toy
continues session toy (3 earlier blocks run first)
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;
output, from postgres 18.6
EXPLAIN
Limit (actual rows=3.00 loops=1) -> Sort (actual rows=3.00 loops=1) Sort Key: price DESC Sort Method: top-N heapsort Memory: 17kB -> Seq Scan on products (actual rows=12.00 loops=1) Filter: (price > '50'::numeric) Rows Removed by Filter: 12

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:

LIMIT 37 rows
ORDER BY + LIMIT 324 rows
hash join (both scans)40 rows
rows each plan's scans looked at, from EXPLAIN ANALYZE (rows returned plus rows removed by the filter). only the streaming plan stops early.
nodekindwhy
Seq Scan, Index Scan, Filterstreamingeach row is ready the moment it's read
Limitstreamingpasses rows through, then stops asking
Nested Loop, Merge Joinstreaminga match is ready when both sides have a row
Hash Joinblocking, then streamingbuilds the whole hash table first, then streams the other side
Sortblockingthe first row out could be the last row in
HashAggregate, plain Aggregateblockinga group isn't finished until the input is
GroupAggregatestreaminginput 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.

quick check
which of these has to read all of its input before it returns its first row?

the interface

in postgres's source, that question is three functions, one set per node type, in src/backend/executor/:

hover a stage.

the life of one plan node. hover a stage. every node type (a Seq Scan, a Hash Join, a Sort) fills in these same steps, and only ever talks to its children through them.

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:

sqldb=shop
BEGIN;
DECLARE c CURSOR FOR SELECT id, name FROM customers ORDER BY id;
FETCH 3 FROM c;
FETCH 2 FROM c;
COMMIT;
output, from postgres 18.6
#1 · BEGIN
#2 · DECLARE CURSOR
#3 · FETCH 3
idint4nametext
1Maya Chen
2Grace Okafor
3Sam Lee
(3 rows)
#4 · FETCH 2
idint4nametext
4Zoë Martin
5Zoe Martin
(2 rows)
#5 · COMMIT

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.