| From: | PG Bug reporting form <noreply(at)postgresql(dot)org> |
|---|---|
| To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | 798604270(at)qq(dot)com |
| Subject: | BUG #19944: Memoize caches volatile LATERAL scans across RTE_FUNCTION and RTE_VALUES, returning wrong results |
| Date: | 2026-11-25 12:14:18 |
| Message-ID: | 19944-47f104b3e68ebb40@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: 19944
Logged by: Chi Zhang
Email address: 798604270(at)qq(dot)com
PostgreSQL version: 19.0
Operating system: Ubuntu 24.04
Description:
Hi,
I found that under the default enable_memoize = on, get_memoize_path()
places a Memoize node on top of volatile LATERAL inner scans for all four
non-table RTE kinds (RTE_FUNCTION, RTE_VALUES, RTE_SUBQUERY, and
RTE_TABLEFUNC/JSON_TABLE). Cache hits suppress VOLATILE function evaluations
(such as nextval() / random()) and return drastically wrong row counts and
values.
This is the test case:
```
CREATE TEMP TABLE t_005(id int, k int);
INSERT INTO t_005 SELECT g, g % 5 FROM generate_series(1, 200) g;
ANALYZE t_005;
-- 1. RTE_SUBQUERY: LATERAL subquery (with OFFSET 0 so it remains
RTE_SUBQUERY)
CREATE TEMP SEQUENCE s_005_fn_off;
CREATE TEMP SEQUENCE s_005_fn_on;
SET enable_memoize = off;
SELECT 'FN_OFF' AS tag, count(*) AS rows, count(DISTINCT v) AS
distinct_vals,
(SELECT last_value FROM s_005_fn_off) AS seq_calls
FROM t_005,
LATERAL (
SELECT t_005.k + nextval('s_005_fn_off') AS v
FROM (VALUES (1),(2),(3)) d(x)
WHERE d.x = 1
OFFSET 0
) l;
RESET enable_memoize;
SELECT 'FN_ON_DEFAULT' AS tag, count(*) AS rows, count(DISTINCT v) AS
distinct_vals,
(SELECT last_value FROM s_005_fn_on) AS seq_calls
FROM t_005,
LATERAL (
SELECT t_005.k + nextval('s_005_fn_on') AS v
FROM (VALUES (1),(2),(3)) d(x)
WHERE d.x = 1
OFFSET 0
) l;
-- 2. RTE_VALUES: multi-row LATERAL VALUES (stays ValuesScan)
CREATE TEMP SEQUENCE s_005_val_off;
CREATE TEMP SEQUENCE s_005_val_on;
SET enable_memoize = off;
SELECT 'VAL_OFF' AS tag, count(DISTINCT v) AS distinct_vals,
(SELECT last_value FROM s_005_val_off) AS seq_calls
FROM t_005, LATERAL (VALUES (t_005.k, nextval('s_005_val_off')), (t_005.k,
0)) AS l(k, v)
WHERE v > 0;
RESET enable_memoize;
SELECT 'VAL_ON_DEFAULT' AS tag, count(DISTINCT v) AS distinct_vals,
(SELECT last_value FROM s_005_val_on) AS seq_calls
FROM t_005, LATERAL (VALUES (t_005.k, nextval('s_005_val_on')), (t_005.k,
0)) AS l(k, v)
WHERE v > 0;
-- 3. RTE_FUNCTION: LATERAL generate_series(...) with volatile nextval()
CREATE TEMP SEQUENCE s_005_srf_off;
CREATE TEMP SEQUENCE s_005_srf_on;
SET enable_memoize = off;
SELECT 'SRF_OFF' AS tag, count(*) AS rows,
(SELECT last_value FROM s_005_srf_off) AS seq_calls
FROM t_005, LATERAL generate_series(1, CASE WHEN t_005.k >= 0 AND
nextval('s_005_srf_off') > 0 THEN 1 ELSE 0 END) gs;
RESET enable_memoize;
SELECT 'SRF_ON_DEFAULT' AS tag, count(*) AS rows,
(SELECT last_value FROM s_005_srf_on) AS seq_calls
FROM t_005, LATERAL generate_series(1, CASE WHEN t_005.k >= 0 AND
nextval('s_005_srf_on') > 0 THEN 1 ELSE 0 END) gs;
-- 4. RTE_TABLEFUNC (JSON_TABLE): LATERAL JSON_TABLE(...) with volatile
nextval()
CREATE TEMP SEQUENCE s_005_jt_off;
CREATE TEMP SEQUENCE s_005_jt_on;
SET enable_memoize = off;
SELECT 'JT_OFF' AS tag, count(DISTINCT jt.v) AS distinct_vals,
(SELECT last_value FROM s_005_jt_off) AS seq_calls
FROM t_005,
LATERAL JSON_TABLE(
jsonb_build_object('v', t_005.k + nextval('s_005_jt_off')), '$'
COLUMNS (v bigint PATH '$.v')
) jt;
RESET enable_memoize;
SELECT 'JT_ON_DEFAULT' AS tag, count(DISTINCT jt.v) AS distinct_vals,
(SELECT last_value FROM s_005_jt_on) AS seq_calls
FROM t_005,
LATERAL JSON_TABLE(
jsonb_build_object('v', t_005.k + nextval('s_005_jt_on')), '$'
COLUMNS (v bigint PATH '$.v')
) jt;
```
```
-- enable_memoize = off (expected):
mode | rows | seq_calls
------+------+-----------
OFF | 10 | 200
-- enable_memoize = on (default, actual):
mode | rows | seq_calls
------------+------+-----------
ON_DEFAULT | 200 | 5
```
All three remaining non-plain-table RTE kinds reproduce identically
(returning 5 distinct values and 5 sequence calls instead of 200):
• RTE_VALUES: FROM t, LATERAL (VALUES (t.k, nextval('s')), (t.k, 0)) AS
l(k, v) WHERE v > 0
• RTE_SUBQUERY: FROM t, LATERAL (SELECT t.k + nextval('s') AS v FROM
(VALUES (1),(2),(3)) d(x) WHERE d.x = 1 OFFSET 0) l
• RTE_TABLEFUNC (JSON_TABLE): FROM t, LATERAL
JSON_TABLE(jsonb_build_object('v', t.k + nextval('s')), '$' COLUMNS (v
bigint PATH '$.v')) jt
I think the root cause is that in src/backend/optimizer/path/joinpath.c,
get_memoize_path() (lines 967–998) attempts to reject volatile inner paths
by checking contain_volatile_functions() on:
1. innerrel->reltarget (line 972),
2. innerrel->baserestrictinfo (lines 975–981), and
3. inner_path->param_info->ppi_clauses (lines 989–998).
However, it never inspects the volatility of the underlying RangeTblEntry
source expressions (rte->functions for RTE_FUNCTION, rte->values_lists for
RTE_VALUES, rte->subquery for RTE_SUBQUERY, and rte->tablefunc for
RTE_TABLEFUNC), nor checks for volatile expressions inside inner base
relations when innerrel is a join relation. Because innerrel->reltarget for
these RTEs only contains simple output Vars referencing the scan outputs,
contain_volatile_functions() returns false and Memoize caches volatile scan
results.
I can reproduce this on PostgreSQL 20devel (master, commit b4cc5e924b7a)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Jesper St John | 2026-11-25 14:33:48 | Re: Timing issue in pg_basebackup |
| Previous Message | PG Bug reporting form | 2026-11-25 12:00:13 | BUG #19943: Wrong results under partition with CHECK/NOT NULL on overridden VIRTUAL generated columns |