Re: BUG #19861: Self-join elimination drops a VOLATILE predicate and changes the query result

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>, Tender Wang <tndrwang(at)gmail(dot)com>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, idrissaajalloh(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19861: Self-join elimination drops a VOLATILE predicate and changes the query result
Date: 2026-12-06 02:30:36
Message-ID: 179652423665.2280139.8351676823303959628@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, 11 Nov 2026 at 16:03, Kirill Reshke <reshkekirill(at)gmail(dot)com>
wrote:
> I tested v2 on master, It fixes the report, however, there are
> remaining target-list cases where SJE changes the result.

Hi Kirill, Tender,

Adding Richard to Cc: 9774003d953 changed this code, and v2 no
longer applies on top of it.

I ran the fix through a differential test I had for
enable_self_join_elimination: generated queries, each run with the
GUC on and off in the same server; rows, errors, row locks and the
number of calls of counting volatile functions have to agree. All
on today's master (8d90516d76d, cassert), 190000 cases with a fixed
seed; the details are in the attached measurements.txt.

The bug is still there after 9774003d953: unpatched master differs
in 4978 cases, all with fewer volatile calls in the plan with the
GUC on, which is the dropped qual of the report.

Since that commit git am rejects v2 (patch -p1 takes it with fuzz).
The v3-0001 file is Tender's v2 rebased, with no functional changes:
same author, message and changed lines. With it no case differs,
and regress and isolation pass.

The cost: in the volatile class v3 removes the join only when the
volatile function is in the target list (975 of 10000 cases), and
master was right in 4047 of the 9025 joins v3 now keeps. In the RLS
class it keeps 2102 more (leak() in WHERE on one side), where master
was right too.

> So, ChangeVarNodes rewrites their correlated references before their
> targets are planned, checking only root->processed_tlist would miss
> the this case?

I don't think your two target-list cases are specific to SJE.
Against one relation, with no join, they give the same answers (the
attached tlist-control.sql.txt):

SELECT nextval('sje_seq') + a.id AS x,
nextval('sje_seq') + a.id AS y
FROM sje_target a ORDER BY x;

returns (1,1) with last_value = 1 on 15.19, 17.11 and master, and
the generate_series() pair returns the single row (1,1). Without
the ORDER BY it returns (1,2), as does the self-join form with the
GUC on. SJE only makes the two expressions identical; computing
identical target-list expressions once under a Sort or ProjectSet is
older than SJE, so I would not hold the fix for it. v3 does not
change either case.

I also tried a narrower fix: keep a duplicate qual when it contains
volatile functions (one condition at the list_member() test in
fixup_selfjoin_quals()). It drops no qual, but about 2300 cases
still differ in how many times or in which order the function runs,
so I think v3's approach is the right one. Not covered:
nondeterministic collations (no ICU build here) and REL_18_STABLE.

Regards,
Manu

Attachment Content-Type Size
v3-0001-Skip-SJE-if-jointree-containing-VOLATILE-function.patch text/x-patch 4.0 KB
measurements.txt text/plain 3.6 KB
tlist-control.sql.txt text/plain 1.0 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-12-06 03:03:16 Re: BUG #19861: Self-join elimination drops a VOLATILE predicate and changes the query result
Previous Message Abed Alawieh 2026-12-05 23:39:07 Re: BUG #19970: json #> finds a nested key under an earlier duplicate key