NL join vs hash join
See NL join lookups explode in a large aggregation and switch to a hash join. PostgreSQL has no hints, so planner settings force the join method.
SQL
Situation
A batch aggregates 2025 monthly sales by region. In production someone turned off hash join and merge join, saying "hash join uses a lot of memory", and the batch has been slow since. PostgreSQL has no hints, so such setting changes are the means of forcing a join method.
Tables
| Table | Columns | Type | Description |
|---|---|---|---|
customers | cust_id, region | 100,000 customers, 5 regions | |
orders | cust_id, ord_dt, amt | 2 million orders |
| Index | Columns |
|---|---|
customers_pk | cust_id |
orders_x1 | cust_id, ord_dt |
Query
BEFORE · Forced NL join
SET LOCAL enable_hashjoin = off;
SET LOCAL enable_mergejoin = off;
SET LOCAL enable_memoize = off;
SELECT c.region, date_trunc('month', o.ord_dt) AS mm, sum(o.amt) AS amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY c.region, date_trunc('month', o.ord_dt)
SET LOCAL is valid only within this run (transaction). Run it and check the Starts and Buffers of the inner node under Nested Loop.
Approach
1. Look at the inner node's Starts
An NL join looks up the inner side once per outer row. If the inner Index Scan's Starts equals the outer row count, the index and table were visited separately that many times.
2. Ask whether the whole result must be built
The aggregation produces a result only after reading every 2025 order. The NL join's strength of returning the first few rows quickly is useless here.
3. Check why the settings were changed
The planner weighs costs to pick a plan. enable_* = off forces by removing that option. See what the planner picks when the settings are restored.
Explanation
NL join → hash join (settings back to default)
AFTER · Default settings (hash join)
SELECT c.region, date_trunc('month', o.ord_dt) AS mm, sum(o.amt) AS amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY c.region, date_trunc('month', o.ord_dt)
Why it got faster
- Before puts 100,000 customers on the outer side and looks up
orders_x1per customer (Index ScanStarts 100,000). Buffers about 840,000. - After builds a hash table from
customers(Hash) and matches pairs while readingordersonce (Hash Join). Buffers about 19,600.
Why this is optimal
- Reading each table once is the minimum this aggregation needs.
- The remaining cost is reading all of
orders(about 18,750 blocks). There is no index starting withord_dt, and looking up 538,000 rows one by one through an index costs more than a full read.
Variation: when memory runs short
TRY · work_mem 64kB
SET LOCAL work_mem = '64kB';
SELECT c.region, date_trunc('month', o.ord_dt) AS mm, sum(o.amt) AS amt
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.ord_dt >= DATE '2025-01-01'
GROUP BY c.region, date_trunc('month', o.ord_dt)
- When the hash table and aggregation do not fit in
work_mem, PostgreSQL splits the hash into several pieces (batches) and writes temp files, or switches the aggregation to a sort-based one. ASortappears in the execution plan, and the temp blocks and execution time in the Trace tab grow. Note that Buffers (shared buffers) stays the same.
Key points
| NL join | Hash join | |
|---|---|---|
| Suited to | Small volume, first N rows, OLTP | Large volume, whole result, batch |
| Requires | Join column index on the inner side | Equality condition, work_mem |
| Repeated lookups | Per outer row | None |
- PostgreSQL has no hints.
enable_*settings are for labs or root-cause analysis; leaving them off in production degrades other queries too. If you must change one in production, apply it only to that transaction withSET LOCAL. - DB difference: Oracle changes just one query with the
USE_NLandUSE_HASHhints (Oracle scenario 6).