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 FULL JOIN constant propagation with non-strict operands
Date: Thu, 19 Nov 2026 14:34:53 +0000
Message-ID: <E1xU3Dw-00000000OYn-179B@gemulon.postgresql.org> (raw)

Fix FULL JOIN constant propagation with non-strict operands

Deriving input restrictions from COALESCE(left, right) = constant can
discard unmatched rows when either operand produces a non-NULL value
for a null-extended input.

Require both operands to be provably null-preserving before deriving
either equality, so that the value remains NULL for null-extended input.
An expression is null-preserving if it contains only strict functions,
and at least one level-zero variable (Var or PlaceHolderVar).

The check is implemented as an expression walker, closely mirrorring
contain_nonstrict_functions_walker(), with different handling for Var
and PlaceHolderVar at level zero, and CoalesceExpr at any depth. The
walker does not descend into level-zero PlaceHolderVars, as those are
evaluated below the join and so go to NULL for null-extended rows.

CoalesceExpr is treated as null-preserving if all its arguments are
null-preserving. So for example (when K is a constant)

    COALESCE(left.a, COALESCE(right.a, right.b)) = K

is null-preserving, while

    COALESCE(left.a, COALESCE(right.a, K)) = K

is not, because the nested COALESCE discards NULL values.

PlaceHolderVars at level zero are automatically null-preserving.  Those
represent expressions calculated below the join, so the result will be
nulled no matter the expression.

Add regression coverage for unsafe operands and safe nested FULL JOIN
USING cases.

Backpatch to all supported branches.  The issue was likely introduced in
8.1, by commit cc5e80b8d1c4.

Author: Tomas Vondra <tomas@vondra.me>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Discussion: https://postgr.es/m/0074fbf7-a92c-417a-937a-6b1fc27aed82@vondra.me
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/72b480e55d72cae35e798500167d05ad5bb58176

Modified Files
--------------
src/backend/optimizer/path/equivclass.c |   9 ++
src/backend/optimizer/util/clauses.c    | 197 ++++++++++++++++++++++++++++++++
src/include/optimizer/clauses.h         |   1 +
src/test/regress/expected/join.out      | 135 ++++++++++++++++++++++
src/test/regress/sql/join.sql           |  58 ++++++++++
5 files changed, 400 insertions(+)



view thread (6+ messages)  latest in thread

Message-ID: <E1xU3Dw-00000000OYn-179B@gemulon.postgresql.org>
Permalink:  ../E1xU3Dw-00000000OYn-179B@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xU3Dw-00000000OYn-179B@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 FULL JOIN constant propagation with non-strict operands
  In-Reply-To: <E1xU3Dw-00000000OYn-179B@gemulon.postgresql.org>
  References: <E1xU3Dw-00000000OYn-179B@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