The idea · Lab 3.3.3A plain JOIN cannot reference the outer row while it is being planned — every table in a FROM clause is resolved independently of the others' actual row values. LATERAL breaks that restriction on purpose: a subquery marked LATERAL is allowed to reference columns from tables that appear earlier in the same FROM clause, and PostgreSQL genuinely executes it once per row those tables produce — which is exactly the mechanism "the 3 most recent orders for each customer" needs, since that is fundamentally a per-customer question, not a single set-based one.
A window function with ROW_NUMBER() can solve the identical top-N-per-group problem in a single pass over a full join, without executing anything once per row — and its EXPLAIN cost estimate can genuinely come out lower than LATERAL's. That does not automatically make it faster. This lab measures both approaches for real, on the same data, and the result is a useful reminder from earlier in this certificate: EXPLAIN's cost is the planner's best estimate before running anything, and EXPLAIN ANALYZE's execution time is what actually happened — the two do not always point to the same winner.
LATERAL: A Subquery, Re-Executed Per Outer Row
SELECT c.id, c.name, o.id AS order_id, o.order_date, o.amount
FROM sql_customers c
JOIN LATERAL (
SELECT * FROM sql_orders o WHERE o.customer_id = c.id ORDER BY o.order_date DESC LIMIT 3
) o ON true
WHERE c.id IN (1,2) ORDER BY c.id, o.order_date DESC;
id | name | order_id | order_date | amount
----+-----------+----------+---------------------+--------
1 | Customer1 | 20000 | 2025-01-14 21:20:00 | 10
1 | Customer1 | 19500 | 2025-01-14 13:00:00 | 110
1 | Customer1 | 19000 | 2025-01-14 04:40:00 | 10
2 | Customer2 | 19501 | 2025-01-14 13:01:00 | 111
c.id inside the LATERAL subquery references the outer customer row directly — something a plain, non-LATERAL subquery cannot do — and LIMIT 3 inside it means exactly 3 rows per customer, genuinely.
Two Real Plans for the Identical Problem
LATERAL: Nested Loop (cost=0.29..5605.96) — Index Scan per customer, loops=500
Window fn: Subquery Scan (cost=0.52..1907.03) over WindowAgg over Merge Join
The window-function version's estimated cost is roughly a third of LATERAL's. On paper, it looks like the clear winner.
Measured, Not Estimated
| Approach | Estimated Cost | Measured Execution Time |
|---|
| LATERAL | 5605.96 | 161.057 ms |
| Window function (ROW_NUMBER) | 1907.03 | 371.099 ms |
LATERAL is measurably more than twice as fast here, despite its higher cost estimate. The reason is visible in the buffer counts: LATERAL touches 2,503 buffers total — 500 small, index-backed lookups of exactly 3 rows each — while the window-function version has to Merge Join and buffer all 20,071 rows of both tables before the window function can even begin filtering down to the top 3 per customer. The cost estimate reflects the planner's model of CPU and I/O cost; it is not a promise about which query will actually run faster on real, specific data.
LATERAL With a Set-Returning Function
SELECT p.order_id, i.sku, i.qty
FROM sql_order_payloads p, LATERAL jsonb_to_recordset(p.items) AS i(sku text, qty int)
ORDER BY p.order_id, i.sku;
order_id | sku | qty
----------+-----+-----
1 | ABC | 2
1 | XYZ | 5
2 | DEF | 1
jsonb_to_recordset is a set-returning function — it needs p.items from the outer row to know what to expand, which is exactly what LATERAL enables: each order's own JSONB array is unnested into its own rows, in the same query.
Key concepts
LATERAL
A modifier on a subquery or set-returning function in a FROM clause that allows it to reference columns from tables listed earlier in the same FROM clause. PostgreSQL genuinely evaluates a LATERAL subquery once for every row produced by those earlier tables, which is what makes per-row-dependent computations — like "the top 3 rows for this specific outer row" or "unnest this specific row's own JSONB array" — expressible at all; a non-LATERAL subquery cannot see the outer row's values.
Top-N per group
The general problem of finding the N highest (or most recent, or otherwise best-ranked) rows within each group in a table, rather than one overall top-N across everything. LATERAL with a per-group subquery and LIMIT, and a window function like ROW_NUMBER() with PARTITION BY, are two structurally different ways to express this same problem — one re-executing a small query per group, the other making one pass with a per-row rank computed and then filtered.
What to remember
- LATERAL lets a subquery or set-returning function reference columns from earlier tables in the same FROM clause, genuinely re-executing once per outer row
- A window function with ROW_NUMBER() and PARTITION BY solves the same top-N-per-group problem in one pass, without per-row re-execution
- A lower EXPLAIN cost estimate is not a guarantee of faster real execution — measuring with EXPLAIN (ANALYZE, BUFFERS) on representative data is what a real performance decision should be based on
- LATERAL performs best specifically when a real index backs the correlation column and only a small number of rows are needed per outer row — check both conditions, not just the syntax fit
- jsonb_to_recordset needs LATERAL specifically because it is a set-returning function that depends on the current outer row's own JSONB value