hash joins
build a hash table on the small side, probe it with the big one. and what happens when it doesn't fit.
- address
- everything.execution.hash_join
- part
- part 9, how a query runs
- chapter
- chapter 9 of 13
- reading time
- 9 min
a hash join reads the smaller input once and builds a hash table on the join key (the build side, under the Hash node). then it streams the bigger input past it (the probe side), and each row checks one bucket instead of every row. n + m work instead of n × m.
it only works for =, and the hash table has to fit in work_mem × hash_mem_multiplier. when it doesn't, postgres splits both inputs into batches and parks all but one of them in temp files.
the picture
you know how a lookup in a hash map is one step, however big the map is? a hash join is that, at table scale. the only question is what goes in the map.
think of a post office sorting mail. first, every letter for one street goes into the pigeonhole for its house number (the build). then each carrier walks the street, and at every door they only look in that door's pigeonhole (the probe). nobody compares a letter with every house on the street.
here's the shop's version: eight orders, five customers, and three ways to join them.
| id | customer_id | |
|---|---|---|
| before first | ||
| 1 | 1 | |
| 12 | 4 | |
| 47 | 1 | |
| 92 | 5 | |
| 511 | 3 | |
| 529 | 2 | |
| 566 | 2 | |
| 1596 | 6 | |
| id | name | |
|---|---|---|
| before first | ||
| 1 | Maya Chen | |
| 2 | Grace Okafor | |
| 3 | Sam Lee | |
| 4 | Zoë Martin | |
| 5 | Zoe Martin | |
| id | customer_id | customers.id | name |
|---|
scanfor each orders row, scan all of customers. no index, no memory, just two loops.
40 comparisons for the nested loop (8 × 5), and a handful for the hash join, since a key's bucket usually holds just that key. on the real tables that's the difference between 20 million comparisons and about 10,000 lookups.
build, then probe
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT o.id, c.name
FROM orders AS o JOIN customers AS c ON c.id = o.customer_id;the Hash node is the build. it pulls all 2,000 customers out of its scan before the join returns anything (a blocking node), and reports the table it built: Buckets: 2048 (a power of two at least as big as the row count, so a bucket holds about one key), Batches: 1 (it fit), Memory Usage: 90kB. then the Hash Join reads orders, one row at a time, hashes o.customer_id, and looks in that one bucket. that's the probe, and it streams.
SELECT o.id, c.name
FROM customers AS c JOIN orders AS o ON o.customer_id = c.id;- 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
- 2SELECT o.id, c.name FROM orders AS o JOIN customers AS c ON c.id = o.customer_id
- Hash Join (actual rows=10000.00 loops=1)
- Hash Cond: (o.customer_id = c.id)
- Buffers: shared hit=4 read=103
- -> Seq Scan on orders o (actual rows=10000.00 loops=1)
- Buffers: shared hit=3 read=80
- -> Hash (actual rows=2000.00 loops=1)
- Buckets: 2048 Batches: 1 Memory Usage: 90kB
- Buffers: shared hit=1 read=23
- -> Seq Scan on customers c (actual rows=2000.00 loops=1)
- Buffers: shared hit=1 read=23
orders first in FROM: postgres hashes customers, the smaller side, and streams orders past it.
which side, for outer joins
for an inner join the sides are interchangeable. for an outer join they aren't, and postgres still wants the smaller side in the hash table. so it flips the join's name instead:
EXPLAIN (COSTS OFF)
SELECT c.name, o.id
FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id;Hash Right Join. you asked for every customer, customers is the smaller table, so customers is hashed, and orders probes. a customer nobody probed for (one with no orders) is found at the end, by walking the hash table for entries that never matched, and comes out padded with NULLs. "Right" because, from the join's point of view, the side being kept is now the inner one. a full join works the same way, keeping score on both sides, which is why a hash join can do FULL JOIN and a nested loop can't.
when it doesn't fit
the hash table gets work_mem × hash_mem_multiplier: 4MB × 2 by default, 8MB. a bigger build side has to be split. postgres picks a number of batches (a power of two), and every row of both inputs goes to batch hash(key) mod batches. batch 0 is joined straight away, in memory. the others are written to temp files, and once the probe side is used up, they're joined one batch at a time: read a batch of the build side back, hash it, read the matching batch of the probe side back, probe. matching keys always land in the same batch number on both sides, so no match is ever split across batches.
here's orders (10,000 rows, a wider row this time) hashed for order_items (24,942):
- Hash Join (actual rows=24942.00 loops=1)
- Hash Cond: (oi.order_id = o.id)
- Buffers: shared hit=83 read=159
- -> Seq Scan on order_items oi (actual rows=24942.00 loops=1)
- Buffers: shared read=159
- -> Hash (actual rows=10000.00 loops=1)
- Buckets: 16384 Batches: 1 Memory Usage: 647kB
- Buffers: shared hit=83
- -> Seq Scan on orders o (actual rows=10000.00 loops=1)
- Buffers: shared hit=83
the default work_mem, 4MB, times hash_mem_multiplier 2: all 10,000 orders fit in one hash table.
at 4MB: one batch, a 647kB table, no temp files. at 256kB: 2 batches, 82 temp pages written and read back. at 64kB: 8 batches and 152. the answer is the same 24,942 rows every time; what changes is how many of them make a round trip through a file. temp pages count in Buffers: as temp read and temp written, next to the shared ones.
it's the product that matters, not work_mem alone. 256kB with the multiplier at 1 is a 256kB budget, the same as 128kB at the default 2:
SET work_mem = '256kB';
SET hash_mem_multiplier = 1;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT o.customer_id, o.status, o.ordered_at, o.shipped_at, oi.product_id, oi.qty
FROM order_items AS oi JOIN orders AS o ON o.id = oi.order_id;4 batches, same as the 128kB run. hash_mem_multiplier exists so that hashing can be given more memory than sorting: a sort that spills degrades gently, a hash table that spills costs more. its default went from 1 to 2 in postgres 15.
one level deeperthe same hash, two jobs
each row's join key is hashed once. part of the hash value picks the bucket (which slot of the in-memory table), another part picks the batch (which pass of the join). that's why the number of batches can double halfway through without rehashing everything: a row in batch 3 of 4 is in batch 3 or 7 of 8, decided by one more bit of the hash it already has. EXPLAIN says Batches: 8 (originally 4) when that happens, meaning the planner's guess at the size was too small.
keylines
- build a hash table on the smaller side, stream the bigger side past it: n + m, not n × m.
- the Hash node blocks; the probe streams.
- equality only. inequalities go to nested loops.
- the table gets work_mem × hash_mem_multiplier (4MB × 2 by default).
- too big: both sides split into batches, all but one in temp files. Batches > 1 means it spilled.