pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix constant propagation through FULL JOIN clauses
Date: Wed, 11 Nov 2026 15:35:49 +0000
Message-ID: <E1xRAMX-00000000KTs-2m3r@gemulon.postgresql.org> (raw)

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@vondra.me>
Discussion: https://postgr.es/m/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/d35a59f1b9bcb239bdd6f1378fb996dac251ef7f

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(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1xRAMX-00000000KTs-2m3r@gemulon.postgresql.org>
Permalink:  ../E1xRAMX-00000000KTs-2m3r@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xRAMX-00000000KTs-2m3r@gemulon.postgresql.org

 · 

reply

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: tomas.vondra@postgresql.org
  Subject: Re: pgsql: Fix constant propagation through FULL JOIN clauses
  In-Reply-To: <E1xRAMX-00000000KTs-2m3r@gemulon.postgresql.org>
  References: <E1xRAMX-00000000KTs-2m3r@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