pgsql: Fix more duplicate qual clauses in parameterized paths

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix more duplicate qual clauses in parameterized paths
Date: 2026-09-10 01:04:21
Message-ID: E1x4TDA-00000004Csp-2gIP@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix more duplicate qual clauses in parameterized paths

Commit f836b688f ensured that at most one clone of a join clause is
chosen for a parameterized path, so long as the clone variants are
parse-tree distinct. Two ways remained to end up enforcing the same
condition more than once, which wastes execution effort and applies
the clause's selectivity multiple times, underestimating the row
count.

If a commuting outer join nulls no Var referenced by a particular
qual, the clone variants of that qual are parse-tree identical, and
the incompatible_relids test cannot tell them apart. To fix, enforce
just the first surviving variant, identified by rinfo_serial.

The other case is in get_joinrel_parampathinfo, where the
clause-recovery pass for dropped EquivalenceClasses queries the EC
machinery with a context that overlaps the preceding loop's, so the
derived-clause cache can hand back a clause that that loop already
accepted. To fix, skip any clause that is already present in the
collected list.

Also add assertions verifying that the clauses to be enforced at a
join or within a parameterized path contain no duplicate rinfo_serial,
to catch any remaining or future violations. These assertions are
added in master only, since hard prevention of duplicate clauses is a
new goal and other unfixed cases may remain.

Back-patch to v19, as with commit f836b688f.

Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/CAMbWs4_Ezb1Lnj7BqcTQSysC6B5UFZxfhib8xr6rqpu5uqr8dA@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8510bcc981bd4bba06fd2c038dab7cc15887b51e

Modified Files
--------------
src/backend/optimizer/util/relnode.c | 146 +++++++++++++++++++++++++----------
src/test/regress/expected/join.out | 55 +++++++++++++
src/test/regress/sql/join.sql | 22 ++++++
3 files changed, 183 insertions(+), 40 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-09-10 02:04:08 pgsql: test_extensible: Improve handling of incorrect inputs
Previous Message Fujii Masao 2026-09-10 00:48:10 pgsql: postgres_fdw: Fix crash when estimating joins with functions