From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Don't replace clone clauses with constant TRUE
Date: Thu, 29 Oct 2026 02:30:05 +0000
Message-ID: <E1xMFu1-00000001CJg-0Reb@gemulon.postgresql.org> (raw)
Don't replace clone clauses with constant TRUE
When reconsider_outer_join_clauses() finds an outer-join clause
OUTERVAR = INNERVAR whose OUTERVAR is known to equal a constant, it
pushes INNERVAR = CONSTANT into the inner side and replaces the clause
with constant TRUE. The replacement assumes that OUTERVAR cannot have
gone to NULL where the clause is evaluated.
That assumption fails for a clone made for outer join identity 3.
When an outer join is pushed into the RHS of a lower outer join, its
relid is not added to the joinrel until the commuted pair is complete,
so a version of a clause that lacks that join's nulling bit can be
applied above it, after it has null-extended OUTERVAR. This is fine
for the original clause, because it is strict, which is what allowed
the commutation in the first place; but it is not fine for constant
TRUE. As a result, inner rows could be joined to null-extended outer
rows, producing wrong query results.
To fix, throw back a clone as-is rather than the TRUE replacement, as
v15 and earlier did. The derived INNERVAR = CONSTANT clause remains
valid and is still pushed down. This can leave a redundant join
filter in some plans, as seen in an existing regression test.
To keep the thrown-back clause from affecting selectivity estimates,
including for parameterized scans, where the cached selectivity is not
used, restore the redundancy marker removed by b448f1c8d: a norm_selec
greater than 1 marks a redundant clause, for which
clause_selectivity_ext() always returns 1.0.
Back-patch to v16, where clone clauses were introduced.
Bug: #19765
Reported-by: Viktor Leis <leis@in.tum.de>
Diagnosed-by: Tender Wang <tndrwang@gmail.com>
Author: Richard Guo <guofenglinux@gmail.com>
Co-authored-by: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Discussion: https://postgr.es/m/19765-5778b18918040583@postgresql.org
Backpatch-through: 16
Branch
------
REL_19_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/4567d170f87bedc577ffe8e934f44db00dbdae7f
Modified Files
--------------
src/backend/optimizer/path/clausesel.c | 6 +++
src/backend/optimizer/path/equivclass.c | 24 ++++++++++++
src/include/nodes/pathnodes.h | 5 ++-
src/test/regress/expected/join.out | 68 ++++++++++++++++++++++++++++++++-
src/test/regress/sql/join.sql | 24 ++++++++++++
5 files changed, 125 insertions(+), 2 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: rguo@postgresql.org
Subject: Re: pgsql: Don't replace clone clauses with constant TRUE
In-Reply-To: <E1xMFu1-00000001CJg-0Reb@gemulon.postgresql.org>
References: <E1xMFu1-00000001CJg-0Reb@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox