Memoize: caching NL join inner results
When the inner side of an NL join is looked up repeatedly with the same key, a Memoize node remembers the results from PostgreSQL 14 on. Joining 20,000 orders of 100 heavy customers to customers cuts customer lookups from 20,000 to 100.
SQL
Situation
A list of orders since June for B2B heavy customers (1–100) is shown with customer name and grade. There are about 24,000 orders but only 100 customers. In an environment where this query runs as an NL join (e.g. a session with hash join turned off), the same customer is looked up hundreds of times over.
Tables
| Table | Columns | Description |
|---|---|---|
orders | cust_id, ord_dt, amt | Customers 1–100 have 2,000 or more orders each |
customers | cust_id, cust_nm, grade | 100,000 customers |
| Index | Columns |
|---|---|
orders_x1 | cust_id, ord_dt |
customers_pk | cust_id |
Query
BEFORE · NL join, no Memoize
SET LOCAL enable_hashjoin = off;
SET LOCAL enable_mergejoin = off;
SET LOCAL enable_memoize = off;
SELECT o.ord_id, o.ord_dt, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.cust_id <= 100
AND o.ord_dt >= DATE '2025-06-01'
Run it and compare the Starts of the customers_pk Index Scan with how many distinct customers there are among them.
Approach
1. Count the kinds of keys in the inner lookup
The inner side of an NL join runs once per outer row. If the 24,000 outer rows have only 100 kinds of cust_id, the same customer is looked up again 240 times on average.
2. Confirm that the same input gives the same result
Looking up customers by cust_id always gives the same result for the same key. Remembering a result once found removes the need to look it up again.
3. See whether the DB can remember it
Oracle caches scalar subquery results (Oracle scenario 4). From PostgreSQL 14 on, a Memoize node on the inner side of an NL join does the same.
Explanation
Turn Memoize on (default)
AFTER · NL join + Memoize
SET LOCAL enable_hashjoin = off;
SET LOCAL enable_mergejoin = off;
SELECT o.ord_id, o.ord_dt, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.cust_id <= 100
AND o.ord_dt >= DATE '2025-06-01'
Why it got faster
- In Before, the
customers_pkIndex Scanhas Starts 23,803 and Buffers about 74,400. It looks up the customer again for every order. - In After, a
Memoizeappears on the inner side ofNested Loop.Memoizehas Starts 23,803, but theIndex Scanunder it has Starts 100. Only customers seen for the first time are actually looked up; the rest use the remembered result. Buffers about 3,300. - The Raw tab shows
Hits(cache hits) andMisses(actual lookups).
Why this is optimal
- The remaining cost is reading the 24,000 orders (about 3,000 blocks); the customer side reads only 100 customers' worth (about 300 blocks).
- The cache is kept only within
work_mem. If there are too many kinds of keys to fit in the cache, the hit rate drops.
Variation: leaving it to the planner
TRY · Default settings
SELECT o.ord_id, o.ord_dt, o.amt, c.cust_nm, c.grade
FROM orders o
JOIN customers c ON c.cust_id = o.cust_id
WHERE o.cust_id <= 100
AND o.ord_dt >= DATE '2025-06-01'
- With every setting at default, the planner picks a
Hash Jointhat builds a hash table from all ofcustomers(Buffers about 3,800). It reads all 100,000 customers but has no repeated lookups. Memoize is a device that further cuts the cost in situations that favor an NL join (a small outer side and repeated inner keys).
Variation: correlated subqueries are not cached
TRY · Scalar subquery
SELECT o.ord_id,
(SELECT c.cust_nm FROM customers c WHERE c.cust_id = o.cust_id) AS cust_nm
FROM orders o
WHERE o.cust_id <= 100
AND o.ord_dt >= DATE '2025-06-01'
- Writing the same thing as a scalar subquery runs
SubPlanfor every order. PostgreSQL does not cache subquery results, so customer lookups are back to 23,803. This is the opposite of Oracle.
Key points
- Memoize remembers the inner results of an NL join per key. The fewer kinds of inner keys, the bigger the effect.
- It works only in PostgreSQL 14 or later (
enable_memoize). So this scenario is for PostgreSQL 14+ only. - DB difference: Oracle reduces repeated lookups with scalar subquery caching; PostgreSQL does it with Memoize in joins. Even with the same SQL, which side gets cached differs by DB.