BUG #19945: Partitionwise join omits lateral dependency from Memoize cache key

Started by PG Bug reporting form3 days ago2 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19945
Logged by: Milo
Email address: 1548140642@qq.com
PostgreSQL version: 19.0
Operating system: Linux/amd64
Description:

Hi,

A partitionwise join can omit a lateral dependency from a Memoize cache key.
In this example, each partition's inner scan depends on both o.k and o.x,
but its Memoize node uses only o.k. Alternating x values therefore cause
repeated cache purges instead of hits.

The following can be run as a superuser with psql -X -f repro.sql:

\set ON_ERROR_STOP on
SET client_min_messages = warning;

CREATE TABLE outer_t (k int, x int) PARTITION BY RANGE (k);
CREATE TABLE outer_t_p1 PARTITION OF outer_t FOR VALUES FROM (0) TO (10);
CREATE TABLE outer_t_p2 PARTITION OF outer_t FOR VALUES FROM (10) TO (20);
CREATE TABLE inner_t (k int, y int) PARTITION BY RANGE (k);
CREATE TABLE inner_t_p1 PARTITION OF inner_t FOR VALUES FROM (0) TO (10);
CREATE TABLE inner_t_p2 PARTITION OF inner_t FOR VALUES FROM (10) TO (20);

INSERT INTO outer_t VALUES
(1, 1), (1, 2), (1, 1), (1, 2),
(11, 11), (11, 12), (11, 11), (11, 12);
INSERT INTO inner_t VALUES
(1, 1), (1, 2), (11, 11), (11, 12);
CREATE INDEX ON inner_t (k);
ANALYZE outer_t;
ANALYZE inner_t;

SET enable_hashjoin = off;
SET enable_mergejoin = off;
SET enable_material = off;
SET enable_memoize = on;
SET enable_partitionwise_join = on;

EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
SELECT o.k, o.x, s.y
FROM outer_t o
LEFT JOIN LATERAL
(SELECT o.x AS c1, i.y, i.k FROM inner_t i) s ON o.k = s.k
WHERE s.c1 = s.y
ORDER BY 1, 2, 3;

SET enable_partitionwise_join = off;

EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
SELECT o.k, o.x, s.y
FROM outer_t o
LEFT JOIN LATERAL
(SELECT o.x AS c1, i.y, i.k FROM inner_t i) s ON o.k = s.k
WHERE s.c1 = s.y
ORDER BY 1, 2, 3;

SET enable_partitionwise_join = on;
CREATE TEMP TABLE memoize_on AS
SELECT o.k, o.x, s.y
FROM outer_t o
LEFT JOIN LATERAL
(SELECT o.x AS c1, i.y, i.k FROM inner_t i) s ON o.k = s.k
WHERE s.c1 = s.y;

SET enable_memoize = off;
CREATE TEMP TABLE memoize_off AS
SELECT o.k, o.x, s.y
FROM outer_t o
LEFT JOIN LATERAL
(SELECT o.x AS c1, i.y, i.k FROM inner_t i) s ON o.k = s.k
WHERE s.c1 = s.y;

SELECT count(*) AS diff_count
FROM (
(TABLE memoize_on EXCEPT ALL TABLE memoize_off)
UNION ALL
(TABLE memoize_off EXCEPT ALL TABLE memoize_on)
) d;

With partitionwise join enabled, the relevant parts of the plan are:

-> Memoize (actual rows=1.00 loops=4)
Cache Key: o_1.k
Cache Mode: logical
Hits: 0 Misses: 4 Evictions: 3 Overflows: 0 Memory Usage: 1kB
-> Seq Scan on inner_t_p1 i_1 (actual rows=1.00 loops=4)
Filter: ((o_1.x = y) AND (o_1.k = k))

-> Memoize (actual rows=1.00 loops=4)
Cache Key: o_2.k
Cache Mode: logical
Hits: 0 Misses: 4 Evictions: 3 Overflows: 0 Memory Usage: 1kB
-> Seq Scan on inner_t_p2 i_2 (actual rows=1.00 loops=4)
Filter: ((o_2.x = y) AND (o_2.k = k))

On 18.6, 19.0, and 20devel, disabling partitionwise join demonstrates the
available Memoize key and reuse:

-> Memoize (actual rows=1.00 loops=8)
Cache Key: o.k, o.x
Cache Mode: binary
Hits: 4 Misses: 4 Evictions: 0 Overflows: 0 Memory Usage: 1kB

The query results remain correct: the comparison with Memoize disabled
prints:

diff_count
------------
0
(1 row)

The partitionwise plan should account for the o.x dependency as well as o.k,
allowing repeated (k,x) values to produce cache hits instead of purging the
cache whenever x changes.

#2Nitin Motiani
nitinmotiani@google.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19945: Partitionwise join omits lateral dependency from Memoize cache key

Hi,

Thanks for the report.

On Wed, Nov 25, 2026 at 11:23 PM PG Bug reporting form
<noreply@postgresql.org> wrote:

A partitionwise join can omit a lateral dependency from a Memoize cache key.
In this example, each partition's inner scan depends on both o.k and o.x,
but its Memoize node uses only o.k. Alternating x values therefore cause
repeated cache purges instead of hits.

With partitionwise join enabled, the relevant parts of the plan are:

-> Memoize (actual rows=1.00 loops=4)
Cache Key: o_1.k
Cache Mode: logical
Hits: 0 Misses: 4 Evictions: 3 Overflows: 0 Memory Usage: 1kB
-> Seq Scan on inner_t_p1 i_1 (actual rows=1.00 loops=4)
Filter: ((o_1.x = y) AND (o_1.k = k))

-> Memoize (actual rows=1.00 loops=4)
Cache Key: o_2.k
Cache Mode: logical
Hits: 0 Misses: 4 Evictions: 3 Overflows: 0 Memory Usage: 1kB
-> Seq Scan on inner_t_p2 i_2 (actual rows=1.00 loops=4)
Filter: ((o_2.x = y) AND (o_2.k = k))

I was able to reproduce this. I used LLM to find some other queries
which might be affected. Any non-strict expression in the lateral
output that mixes in an outer column hits it too. For example, with
order_lines and products both partitioned by tenant_id, the below
query also ran into the same issue.

SELECT ol.order_id, pr.list_price, pr.price_currency
FROM order_lines ol
LEFT JOIN LATERAL (
SELECT p.list_price, COALESCE(ol.currency, p.currency) AS price_currency
FROM products p
WHERE p.tenant_id = ol.tenant_id AND p.product_id = ol.product_id
) pr ON true;

With partitionwise joins, there were no cache hits.

This stems from the fact that for a partitionwise join,
extract_lateral_vars_from_PHVs() compares the PHV's ph_eval_at, which
uses parent relids, with the child rel's relids. This check never
matches, so lateral references inside PHVs are left out of the memoize
cache key. So memoize then purges its cache on every rescan.

The attached patch fixes this inside extract_lateral_vars_from_PHVs()
by comparing against the child's top_parent_relids. When I ran this,
an existing test involving multiple levels of partitions started
failing. This involved the intermediate layer in the partition
hierarchy. So I explicitly limited the change to the leaf level
partitions to keep the code contained in one function.

I'm attaching the patch. Please take a look.

Thanks & Regards,
Nitin Motiani
Google

Attachments:

t259817_2
v1-0001-Fix-Memoize-cache-keys-for-PHV-lateral-refs-with-.patchapplication/x-patch; name=v1-0001-Fix-Memoize-cache-keys-for-PHV-lateral-refs-with-.patchDownload+155-4