skip to content

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.

algorithm =
join =
data =
orders
idcustomer_id
before first
11
124
471
925
5113
5292
5662
15966
customers
idname
before first
1Maya Chen
2Grace Okafor
3Sam Lee
4Zoë Martin
5Zoe Martin
comparisons
0
rows read
0
memory
·
batches
·
output
idcustomer_idcustomers.idname
(0 rows)

scanfor each orders row, scan all of customers. no index, no memory, just two loops.

step 1/42
model of the join algorithms · checked against postgres 18.6
switch the algorithm and step through. the nested loop compares every order with every customer; the hash join hashes the five customers once and checks each order against one bucket; the merge join sorts both sides first. with hash join picked, slide work_mem below 5 rows and the table no longer fits: it splits into batches. the bars at the end compare the comparisons.

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

sqldb=shop
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;
output, from postgres 18.6
EXPLAIN
Hash Join (actual rows=10000.00 loops=1) Hash Cond: (o.customer_id = c.id) Buffers: shared read=107 -> Seq Scan on orders o (actual rows=10000.00 loops=1) Buffers: shared read=83 -> Hash (actual rows=2000.00 loops=1) Buckets: 2048 Batches: 1 Memory Usage: 90kB Buffers: shared read=24 -> Seq Scan on customers c (actual rows=2000.00 loops=1) Buffers: shared read=24 Planning: Buffers: shared hit=165 read=12 dirtied=1

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.

predict the output
swap the tables in FROM. which one gets hashed now?
sql
SELECT o.id, c.name
FROM customers AS c JOIN orders AS o ON o.customer_id = c.id;
FROM =
query.sql
  1. 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
  2. 2SELECT o.id, c.name FROM orders AS o JOIN customers AS c ON c.id = o.customer_id
what postgres printed
  1. Hash Join (actual rows=10000.00 loops=1)
  2. Hash Cond: (o.customer_id = c.id)
  3. Buffers: shared hit=4 read=103
  4. -> Seq Scan on orders o (actual rows=10000.00 loops=1)
  5. Buffers: shared hit=3 read=80
  6. -> Hash (actual rows=2000.00 loops=1)
  7. Buckets: 2048 Batches: 1 Memory Usage: 90kB
  8. Buffers: shared hit=1 read=23
  9. -> Seq Scan on customers c (actual rows=2000.00 loops=1)
  10. Buffers: shared hit=1 read=23

orders first in FROM: postgres hashes customers, the smaller side, and streams orders past it.

real output, postgres 18.6 (PGlite 0.5.12) · hash-join
the same join, written both ways round. the plans are identical: customers is under the Hash both times. (the buffers differ because the first run read the pages in and the second found them in memory.)

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:

sqldb=shop
EXPLAIN (COSTS OFF)
SELECT c.name, o.id
FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id;
output, from postgres 18.6
EXPLAIN
Hash Right Join Hash Cond: (o.customer_id = c.id) -> Seq Scan on orders o -> Hash -> Seq Scan on customers c

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):

work_mem =
batches
1
buckets
16,384
hash table
647kB / 8MB
temp pages written
0
what postgres printed
  1. Hash Join (actual rows=24942.00 loops=1)
  2. Hash Cond: (oi.order_id = o.id)
  3. Buffers: shared hit=83 read=159
  4. -> Seq Scan on order_items oi (actual rows=24942.00 loops=1)
  5. Buffers: shared read=159
  6. -> Hash (actual rows=10000.00 loops=1)
  7. Buckets: 16384 Batches: 1 Memory Usage: 647kB
  8. Buffers: shared hit=83
  9. -> Seq Scan on orders o (actual rows=10000.00 loops=1)
  10. Buffers: shared hit=83

the default work_mem, 4MB, times hash_mem_multiplier 2: all 10,000 orders fit in one hash table.

real output, postgres 18.6 (PGlite 0.5.12) · hash-join
pick a work_mem. each box is a batch of one input: the solid one is joined in memory, the dashed ones go to temp files and come back later. every halving of work_mem doubles the batches, and the temp traffic grows with them.

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:

sqldb=shop
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;
output, from postgres 18.6
#1 · SET
#2 · SET
#3 · EXPLAIN
Hash Join (actual rows=24942.00 loops=1) Hash Cond: (oi.order_id = o.id) Buffers: shared read=242, temp read=128 written=128 -> Seq Scan on order_items oi (actual rows=24942.00 loops=1) Buffers: shared read=159 -> Hash (actual rows=10000.00 loops=1) Buckets: 4096 Batches: 4 Memory Usage: 161kB Buffers: shared read=83, temp written=49 -> Seq Scan on orders o (actual rows=10000.00 loops=1) Buffers: shared read=83 Planning: Buffers: shared hit=167 read=15 dirtied=1

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.

quick check
which ON can a hash join use?
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.