pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix FULL JOIN constant propagation with non-strict operands
6+ messages / 1 participants
[nested] [flat]

* pgsql: Fix FULL JOIN constant propagation with non-strict operands
@ 2026-11-19 14:34  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Tomas Vondra @ 2026-11-19 14:34 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix FULL JOIN constant propagation with non-strict operands
@ 2026-11-19 14:35  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Tomas Vondra @ 2026-11-19 14:35 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/699b879a8e350817b6f53013ed72fcd100997432

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix FULL JOIN constant propagation with non-strict operands
@ 2026-11-19 14:35  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Tomas Vondra @ 2026-11-19 14:35 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_18_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix FULL JOIN constant propagation with non-strict operands
@ 2026-11-19 14:35  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Tomas Vondra @ 2026-11-19 14:35 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5a82b620c0f9f5c6598a990ea9fb221398107a72

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix FULL JOIN constant propagation with non-strict operands
@ 2026-11-19 14:35  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Tomas Vondra @ 2026-11-19 14:35 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_16_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix FULL JOIN constant propagation with non-strict operands
@ 2026-11-19 14:35  Tomas Vondra <tomas.vondra@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Tomas Vondra @ 2026-11-19 14:35 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
REL_15_STABLE

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

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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-11-19 14:35 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 14:34 pgsql: Fix FULL JOIN constant propagation with non-strict operands Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-19 14:35 pgsql: Fix FULL JOIN constant propagation with non-strict operands Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-19 14:35 pgsql: Fix FULL JOIN constant propagation with non-strict operands Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-19 14:35 pgsql: Fix FULL JOIN constant propagation with non-strict operands Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-19 14:35 pgsql: Fix FULL JOIN constant propagation with non-strict operands Tomas Vondra <tomas.vondra@postgresql.org>
2026-11-19 14:35 pgsql: Fix FULL JOIN constant propagation with non-strict operands Tomas Vondra <tomas.vondra@postgresql.org>

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