pgsql: Fix constant propagation through FULL JOIN clauses

From: Tomas Vondra <tomas(dot)vondra(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix constant propagation through FULL JOIN clauses
Date: 2026-11-11 15:35:41
Message-ID: E1xRAMP-00000000KTE-2yI5@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix constant propagation through FULL JOIN clauses

Given a FULL JOIN clause "LEFTVAR = RIGHTVAR" and an EquivalenceClass
containing the expression COALESCE(LEFTVAR, RIGHTVAR) and a constant,
reconsider_full_join_clause() derives "LEFTVAR = CONSTANT" and "RIGHTVAR
= CONSTANT" and discards the join clause and the COALESCE condition.

It however assumed LEFTVAR comes from the join's lefthand input and
RIGHTVAR from its righthand input, which holds for FULL JOIN USING but
not necessarily for a user-written ON clause.

With the operands reversed, the derived equalities got the wrong
JoinDomains. That could cause an assertion failure or, in non-assert
builds, incorrect results, due to a derived qual being applied to the
wrong side of the join. With both operands from the same input, or one
referencing both inputs, nothing can be deduced, yet bogus equalities
were derived anyway. Both could produce wrong results.

Fix by determining which input each operand comes from, and giving up
unless they come from different inputs. A FULL JOIN whose only condition
is a clause of the latter kind now fails with "FULL JOIN is only
supported with merge-joinable or hash-joinable join conditions", as it
would without the COALESCE condition.

The JoinDomain problem was introduced in v16. The other one (bogus
equalities derived from a qual with both operands from the same input,
or one referencing both inputs) exists in all supported branches, but
before the introduction of outer-join-aware Vars (v16) only an operand
referencing both inputs can trigger it.

Backpatch to all supported versions. The 15 branch needs only the part
addressing the second issue.

Author: Tomas Vondra <tomas(at)vondra(dot)me>
Discussion: https://postgr.es/m/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me
Backpatch-through: 15

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/50cbffb7917a47c5f8f0df3b5994888a36c08ffb

Modified Files
--------------
src/backend/optimizer/path/equivclass.c | 36 ++++--
src/backend/optimizer/plan/initsplan.c | 7 +-
src/test/regress/expected/join.out | 211 ++++++++++++++++++++++++++++++++
src/test/regress/sql/join.sql | 83 +++++++++++++
4 files changed, 326 insertions(+), 11 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tomas Vondra 2026-11-11 15:35:49 pgsql: Fix constant propagation through FULL JOIN clauses
Previous Message Tomas Vondra 2026-11-11 15:35:34 pgsql: Fix constant propagation through FULL JOIN clauses