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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: 1548140642(at)qq(dot)com
Subject: BUG #19945: Partitionwise join omits lateral dependency from Memoize cache key
Date: 2026-11-25 16:27:47
Message-ID: 19945-90b350dba6050df2@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19945
Logged by: Milo
Email address: 1548140642(at)qq(dot)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.

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-11-25 17:18:42 BUG #19946: Caught foreign-key violations retain RI fast-path memory until transaction end
Previous Message Jesper St John 2026-11-25 14:33:48 Re: Timing issue in pg_basebackup