reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

Started by Tomas Vondra15 days ago15 messageshackers
Jump to latest
#1Tomas Vondra
tomas@vondra.me

Hi,

While working on [1]/messages/by-id/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me, an AI review pointed out a separate long-standing
bug in reconsider_full_join_clause(). Consider a query like this:

SELECT *
FROM t1 FULL JOIN t2 ON (t1.id = t2.id)
WHERE COALESCE(t1.id, t2.id) = 12345

The function correctly replaces the COALESCE() clause with two clauses
pushed down to the join inputs, and removes the join clause, as if

SELECT *
FROM (SELECT * FROM t1 WHERE t1.id = 12345)
FULL JOIN (SELECT * FROM t2 WHERE t2.id = 12345)

So far all good - such transformation is correct (this example does not
even need fixes from [1]/messages/by-id/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me). But the coalesce arguments may not be plain
Vars. Consider this instead:

SELECT *
FROM t1 FULL JOIN t2 ON (t1.id = COALESCE(t2.id, 12345))
WHERE COALESCE(t1.id, COALESCE(t2.id, 12345)) = 12345

Well, reconsider_full_join_clause() still pushed down the clauses to the
join inputs etc. as if

SELECT *
FROM (SELECT * FROM t1 WHERE t1.id = 12345)
FULL JOIN (SELECT * FROM t2 WHERE COALESCE(t2.id,12345) = 12345)

But this is wrong, because the COALESCE() gets rid of NULLs. If t2.id is
NULL ("id" does not need to be a PK), the row incorrectly matches. A
trivial example:

CREATE TABLE t1 (id INT);
CREATE TABLE t2 (id INT);

INSERT INTO t1 VALUES (NULL);
INSERT INTO t2 VALUES (NULL);

SELECT t1.id as x, t2.id as y
FROM t1 FULL JOIN t2 ON (t1.id = COALESCE(t2.id, 12345))
WHERE COALESCE(t1.id, COALESCE(t2.id, 12345)) = 12345;

x | y
---+---
|
(1 row)

-- OFFSET 0 to block the pushdown, result should not change
SELECT * FROM (
SELECT t1.id as x, t2.id as y
FROM t1 FULL JOIN t2 ON (t1.id = COALESCE(t2.id, 12345)) OFFSET 0
) foo WHERE COALESCE(foo.x, COALESCE(foo.y, 12345)) = 12345;

x | y
---+---
|
|
(2 rows)

COALESCE is not the only way to get this issue, CASE can have the same
effect (example in the patch). There might be more ways to hit it ...

This requires the t1.id = COALESCE(t2.id, 12345) join clause, it works
OK without that. IIUC this is because it establishes a matching EC,
which is necessary for the misbehavior.

This query pattern (with COALESCE in the join clause) is pretty odd, but
what do I know? People (and tools) sometimes write pretty crazy stuff.

AFAICS to fix this, reconsider_full_join_clause() needs to ask if both
sides of the clause (or operands for COALESCE) preserve NULLs. The patch
adds that check before the loop, same place as [1]/messages/by-id/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me. The principle is the
same - unsafe expression -> return false -> keep the original clause.

The trouble is, I don't think we have a way to check this property yet.
There are places doing something similar, but nothing that'd check if an
expression is null-preserving (recursively).

After a fair amount of trial and error I came up with a new helper
is_full_join_expr_null_preserving doing this. It pretty much descends
into CoalesceExpr, and checks if the arguments are NULL-preserving.

Still, once we get a non-CoalesceExpr expression, we need to know if
it's null-preserving. The conservative way to do that is

return !contain_nonstrict_functions(node) &&
contain_vars_of_level(node, 0);

Which means it may contain only strict functions (NULL -> NULL) and some
vars. This rules out constants, functions that discard NULLs, etc. It
also rejects strict-behaving functions that are not labeled as such, but
I don't know how to do better.

There's also a special case for PlaceHolderVar, to preserve the
optimization for expressions calculated below the join (as the result
will be nulled no matter the expression).

Maybe we could improve this further, but this query pattern is pretty
strange, so it should be enough to not return incorrect results. I don't
think it's worth more effort.

The patch is pretty small. I've spent a bit of time with sqlfiddle to
check we get the same behavior as other databases (MSSQL and Oracle).
And that we produce incorrect results without the fix.

My questions are pretty much:

1) Is is_full_join_expr_null_preserving() necessary? Or is there some
existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

2) Is is_full_join_expr_null_preserving() correct?

regards

[1]: /messages/by-id/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me

--
Tomas Vondra

Attachments:

t258337_1
v1-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchtext/x-patch; charset=UTF-8; name=v1-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchDownload+168-1
#2Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Tomas Vondra (#1)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

Hi,

On Mon, 9 Nov 2026 at 02:37, Tomas Vondra <tomas@vondra.me> wrote:

Hi,

While working on [1], an AI review pointed out a separate long-standing
bug in reconsider_full_join_clause(). Consider a query like this:

SELECT *
FROM t1 FULL JOIN t2 ON (t1.id = t2.id)
WHERE COALESCE(t1.id, t2.id) = 12345

The function correctly replaces the COALESCE() clause with two clauses
pushed down to the join inputs, and removes the join clause, as if

SELECT *
FROM (SELECT * FROM t1 WHERE t1.id = 12345)
FULL JOIN (SELECT * FROM t2 WHERE t2.id = 12345)

So far all good - such transformation is correct (this example does not
even need fixes from [1]). But the coalesce arguments may not be plain
Vars. Consider this instead:

SELECT *
FROM t1 FULL JOIN t2 ON (t1.id = COALESCE(t2.id, 12345))
WHERE COALESCE(t1.id, COALESCE(t2.id, 12345)) = 12345

Well, reconsider_full_join_clause() still pushed down the clauses to the
join inputs etc. as if

SELECT *
FROM (SELECT * FROM t1 WHERE t1.id = 12345)
FULL JOIN (SELECT * FROM t2 WHERE COALESCE(t2.id,12345) = 12345)

But this is wrong, because the COALESCE() gets rid of NULLs. If t2.id is
NULL ("id" does not need to be a PK), the row incorrectly matches. A
trivial example:

CREATE TABLE t1 (id INT);
CREATE TABLE t2 (id INT);

INSERT INTO t1 VALUES (NULL);
INSERT INTO t2 VALUES (NULL);

SELECT t1.id as x, t2.id as y
FROM t1 FULL JOIN t2 ON (t1.id = COALESCE(t2.id, 12345))
WHERE COALESCE(t1.id, COALESCE(t2.id, 12345)) = 12345;

x | y
---+---
|
(1 row)

-- OFFSET 0 to block the pushdown, result should not change
SELECT * FROM (
SELECT t1.id as x, t2.id as y
FROM t1 FULL JOIN t2 ON (t1.id = COALESCE(t2.id, 12345)) OFFSET 0
) foo WHERE COALESCE(foo.x, COALESCE(foo.y, 12345)) = 12345;

x | y
---+---
|
|
(2 rows)

COALESCE is not the only way to get this issue, CASE can have the same
effect (example in the patch). There might be more ways to hit it ...

This requires the t1.id = COALESCE(t2.id, 12345) join clause, it works
OK without that. IIUC this is because it establishes a matching EC,
which is necessary for the misbehavior.

Thanks for the detailed explanation. This is almost the first time I
didn't have to go and search for the whole issue to understand what the
case is about.

This query pattern (with COALESCE in the join clause) is pretty odd, but
what do I know? People (and tools) sometimes write pretty crazy stuff.

AFAICS to fix this, reconsider_full_join_clause() needs to ask if both
sides of the clause (or operands for COALESCE) preserve NULLs. The patch
adds that check before the loop, same place as [1]. The principle is the
same - unsafe expression -> return false -> keep the original clause.

The trouble is, I don't think we have a way to check this property yet.
There are places doing something similar, but nothing that'd check if an
expression is null-preserving (recursively).

After a fair amount of trial and error I came up with a new helper
is_full_join_expr_null_preserving doing this. It pretty much descends
into CoalesceExpr, and checks if the arguments are NULL-preserving.

Still, once we get a non-CoalesceExpr expression, we need to know if
it's null-preserving. The conservative way to do that is

return !contain_nonstrict_functions(node) &&
contain_vars_of_level(node, 0);

Which means it may contain only strict functions (NULL -> NULL) and some
vars. This rules out constants, functions that discard NULLs, etc. It
also rejects strict-behaving functions that are not labeled as such, but
I don't know how to do better.

There's also a special case for PlaceHolderVar, to preserve the
optimization for expressions calculated below the join (as the result
will be nulled no matter the expression).

Maybe we could improve this further, but this query pattern is pretty
strange, so it should be enough to not return incorrect results. I don't
think it's worth more effort.

+1, Ig being conservative is fine here. If there's a report for some real
query that needs more, we can teach it then.

The patch is pretty small. I've spent a bit of time with sqlfiddle to
check we get the same behavior as other databases (MSSQL and Oracle).
And that we produce incorrect results without the fix.

My questions are pretty much:

1) Is is_full_join_expr_null_preserving() necessary? Or is there some
existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

I don't think we have one. I looked at find_nonnullable_rels() first,
but FALSE is good enough for that one, and here we need an actual NULL.
So, yeah the function looks necessary to me.

2) Is is_full_join_expr_null_preserving() correct?

Looks right to me. If all the COALESCE args go NULL, so does the
COALESCE. The PHV case is fine too, since it's the value computed below
the join that gets nulled. IIUC that part relies on the side check from
[1]: /messages/by-id/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me

Regards,
Ayush

PS. I did trip over the JsonExpr strictness bug while testing (a NULL
PASSING arg can still give you a non-NULL result), but that's already
being fixed in [2]/messages/by-id/CAD=-kXbsgddZeUJKDi5=-rE4pHdvNFYWk0FcE85MCzN4gzo0dQ@mail.gmail.com, so nothing to do here.

[1]: /messages/by-id/92808d26-1b4a-48fe-82f0-597da3ff471f@vondra.me
[2]: /messages/by-id/CAD=-kXbsgddZeUJKDi5=-rE4pHdvNFYWk0FcE85MCzN4gzo0dQ@mail.gmail.com
/messages/by-id/CAD=-kXbsgddZeUJKDi5=-rE4pHdvNFYWk0FcE85MCzN4gzo0dQ@mail.gmail.com

#3Tomas Vondra
tomas@vondra.me
In reply to: Ayush Tiwari (#2)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/9/26 06:47, Ayush Tiwari wrote:

Hi,

On Mon, 9 Nov 2026 at 02:37, Tomas Vondra <tomas@vondra.me
<mailto:tomas@vondra.me>> wrote:

Hi,

While working on [1], an AI review pointed out a separate long-standing
bug in reconsider_full_join_clause(). Consider a query like this:

    SELECT *
    FROM t1 FULL JOIN t2 ON (t1.id <http://t1.id> = t2.id <http://t2.id>)
    WHERE COALESCE(t1.id <http://t1.id>, t2.id <http://t2.id>) = 12345

The function correctly replaces the COALESCE() clause with two clauses
pushed down to the join inputs, and removes the join clause, as if

    SELECT *
    FROM (SELECT * FROM t1 WHERE t1.id <http://t1.id> = 12345)
    FULL JOIN (SELECT * FROM t2 WHERE t2.id <http://t2.id> = 12345)

So far all good - such transformation is correct (this example does not
even need fixes from [1]). But the coalesce arguments may not be plain
Vars. Consider this instead:

    SELECT *
    FROM t1 FULL JOIN t2 ON (t1.id <http://t1.id> = COALESCE(t2.id

<http://t2.id>, 12345))

    WHERE COALESCE(t1.id <http://t1.id>, COALESCE(t2.id <http://

t2.id>, 12345)) = 12345

Well, reconsider_full_join_clause() still pushed down the clauses to the
join inputs etc. as if

    SELECT *
    FROM (SELECT * FROM t1 WHERE t1.id <http://t1.id> = 12345)
    FULL JOIN (SELECT * FROM t2 WHERE COALESCE(t2.id <http://

t2.id>,12345) = 12345)

But this is wrong, because the COALESCE() gets rid of NULLs. If t2.id

<http://t2.id> is

NULL ("id" does not need to be a PK), the row incorrectly matches. A
trivial example:

    CREATE TABLE t1 (id INT);
    CREATE TABLE t2 (id INT);

    INSERT INTO t1 VALUES (NULL);
    INSERT INTO t2 VALUES (NULL);

    SELECT t1.id <http://t1.id> as x, t2.id <http://t2.id> as y
      FROM t1 FULL JOIN t2 ON (t1.id <http://t1.id> = COALESCE(t2.id

<http://t2.id>, 12345))

     WHERE COALESCE(t1.id <http://t1.id>, COALESCE(t2.id <http://

t2.id>, 12345)) = 12345;

     x | y
    ---+---
       |
    (1 row)

    -- OFFSET 0 to block the pushdown, result should not change
    SELECT * FROM (
     SELECT t1.id <http://t1.id> as x, t2.id <http://t2.id> as y
       FROM t1 FULL JOIN t2 ON (t1.id <http://t1.id> = COALESCE(t2.id

<http://t2.id>, 12345)) OFFSET 0

    ) foo WHERE COALESCE(foo.x, COALESCE(foo.y, 12345)) = 12345;

     x | y
    ---+---
       |
       |
    (2 rows)

COALESCE is not the only way to get this issue, CASE can have the same
effect (example in the patch). There might be more ways to hit it ...

This requires the t1.id <http://t1.id> = COALESCE(t2.id <http://

t2.id>, 12345) join clause, it works

OK without that. IIUC this is because it establishes a matching EC,
which is necessary for the misbehavior.

Thanks for the detailed explanation.  This is almost the first time I
didn't have to go and search for the whole issue to understand what the
case is about.

TBH this is a very new topic for me too, so I mostly wrote this
explanation for myself in the first place, to clarify my understanding
of how it works. And indeed I found a bunch of gaps in my understanding.
But it also means the explanation may be wrong / missing pieces, so I'd
encourage you to go through the code and verify I got it right - it'd be
a very useful review.

This query pattern (with COALESCE in the join clause) is pretty odd, but
what do I know? People (and tools) sometimes write pretty crazy stuff.

AFAICS to fix this, reconsider_full_join_clause() needs to ask if both
sides of the clause (or operands for COALESCE) preserve NULLs. The patch
adds that check before the loop, same place as [1]. The principle is the
same - unsafe expression -> return false -> keep the original clause.

The trouble is, I don't think we have a way to check this property yet.
There are places doing something similar, but nothing that'd check if an
expression is null-preserving (recursively).

After a fair amount of trial and error I came up with a new helper
is_full_join_expr_null_preserving doing this. It pretty much descends
into CoalesceExpr, and checks if the arguments are NULL-preserving.

Still, once we get a non-CoalesceExpr expression, we need to know if
it's null-preserving. The conservative way to do that is

    return !contain_nonstrict_functions(node) &&
        contain_vars_of_level(node, 0);

Which means it may contain only strict functions (NULL -> NULL) and some
vars. This rules out constants, functions that discard NULLs, etc. It
also rejects strict-behaving functions that are not labeled as such, but
I don't know how to do better.

There's also a special case for PlaceHolderVar, to preserve the
optimization for expressions calculated below the join (as the result
will be nulled no matter the expression).

Maybe we could improve this further, but this query pattern is pretty
strange, so it should be enough to not return incorrect results. I don't
think it's worth more effort.

+1, Ig being conservative is fine here.  If there's a report for some real
query that needs more, we can teach it then.

The patch is pretty small. I've spent a bit of time with sqlfiddle to
check we get the same behavior as other databases (MSSQL and Oracle).
And that we produce incorrect results without the fix.

My questions are pretty much:

1) Is is_full_join_expr_null_preserving() necessary? Or is there some
existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

I don't think we have one.  I looked at find_nonnullable_rels() first,
but FALSE is good enough for that one, and here we need an actual NULL.
So, yeah the function looks necessary to me.

2) Is is_full_join_expr_null_preserving() correct?

Looks right to me.  If all the COALESCE args go NULL, so does the
COALESCE.  The PHV case is fine too, since it's the value computed below
the join that gets nulled.  IIUC that part relies on the side check from
[1] though, so I assume this will go on top of it?

OK

Regards,
Ayush

PS. I did trip over the JsonExpr strictness bug while testing (a NULL
PASSING arg can still give you a non-NULL result), but that's already
being fixed in [2], so nothing to do here.

Ah, interesting.

Thanks!

--
Tomas Vondra

#4Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#3)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

Hi,

Here's a rebased version of the patch(es). I've made no change to the
code itself, I've tried to improve the commit message to explain why the
expressions need to be null-preserving, handling of COALESCE clauses and
placeholders.

Initially I've planned to backpatch to 16, as I assumed this was likely
introduced by the JoinDomain improvements in that release. But it seems
it's actually much older, possibly even going all the way back to 8.1
(but I haven't tried to build that).

All I know is that the regression tests give incorrect results on 15
(without the code fix).

regards

--
Tomas Vondra

Attachments:

v2-15-0001-Fix-FULL-JOIN-constant-propagation-with-non-st.patchtext/x-patch; charset=UTF-8; name=v2-15-0001-Fix-FULL-JOIN-constant-propagation-with-non-st.patchDownload+169-1
v2-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchtext/x-patch; charset=UTF-8; name=v2-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchDownload+168-1
#5Kirill Reshke
reshkekirill@gmail.com
In reply to: Tomas Vondra (#4)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On Thu, 12 Nov 2026 at 02:48, Tomas Vondra <tomas@vondra.me> wrote:

Hi,

Here's a rebased version of the patch(es). I've made no change to the
code itself, I've tried to improve the commit message to explain why the
expressions need to be null-preserving, handling of COALESCE clauses and
placeholders.

Hi!

1) Is is_full_join_expr_null_preserving() necessary? Or is there some

existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

I think it is. There is find_nonnullable_rels which solves similar
task, but I don't see any additional smartness in it.
There is also is_standard_join_alias_expression function but it
doesn't check for functions strictness

2) Is is_full_join_expr_null_preserving() correct?

I think it is.

It does lose some optimizations for safe filter pushdows, because
this doesn't recurse
+ if (IsA(node, PlaceHolderVar))
+ return ((PlaceHolderVar *) node)->phlevelsup == 0;

With v2:

CREATE TEMP TABLE a(x int);
CREATE TEMP TABLE b(x int);

EXPLAIN (COSTS OFF)
SELECT *
FROM (SELECT coalesce(x, 0) AS k FROM a) s
FULL JOIN b ON s.k + 0 = b.x
WHERE coalesce(s.k + 0, b.x) = 0;
CREATE TABLE
CREATE TABLE
QUERY PLAN
---------------------------------------------------------
Merge Full Join
Merge Cond: (((COALESCE(a.x, 0) + 0)) = b.x)
Filter: (COALESCE(((COALESCE(a.x, 0)) + 0), b.x) = 0)
-> Sort
Sort Key: ((COALESCE(a.x, 0) + 0))
-> Seq Scan on a
-> Sort
Sort Key: b.x
-> Seq Scan on b
(9 rows)

before, there is two filters

QUERY PLAN
--------------------------------------------
Merge Full Join
-> Seq Scan on a
Filter: ((COALESCE(x, 0) + 0) = 0)
-> Materialize
-> Seq Scan on b
Filter: (x = 0)
(6 rows)

Is this regression important? or can we accept this for sake of simplicity?

--
Best regards,
Kirill Reshke

#6Tomas Vondra
tomas@vondra.me
In reply to: Kirill Reshke (#5)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/12/26 07:38, Kirill Reshke wrote:

On Thu, 12 Nov 2026 at 02:48, Tomas Vondra <tomas@vondra.me> wrote:

Hi,

Here's a rebased version of the patch(es). I've made no change to the
code itself, I've tried to improve the commit message to explain why the
expressions need to be null-preserving, handling of COALESCE clauses and
placeholders.

Hi!

1) Is is_full_join_expr_null_preserving() necessary? Or is there some

existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

I think it is. There is find_nonnullable_rels which solves similar
task, but I don't see any additional smartness in it.
There is also is_standard_join_alias_expression function but it
doesn't check for functions strictness

2) Is is_full_join_expr_null_preserving() correct?

I think it is.

It does lose some optimizations for safe filter pushdows, because
this doesn't recurse
+ if (IsA(node, PlaceHolderVar))
+ return ((PlaceHolderVar *) node)->phlevelsup == 0;

Why would that matter? That returns 'true' for all PlaceHolderVars,
treating them as null-preserving, i.e. allowing pushdown. Why would it
block the optimization in the example you posted?

AFAICS it's treating the expression as unsafe because of the "+"
operator, because the associated function is not labeled as strict.

test=# select oid, proname, proisstrict from pg_proc where oid = 177;
oid | proname | proisstrict
-----+---------+-------------
177 | int4pl | t
(1 row)

I don't think that's really the fault of this patch, and I don't see a
way around it - if there are non-strict functions, that makes the whole
expression unsafe.

I don't recall why int4pl is not labeled as strict, maybe it should be?
I haven't thought about that very much, there probably are multiple
threads in the archives discussing that. But that's not something we can
fix without a catversion bump, so it wouldn't help backbranches.

Thinking about is_full_join_expr_null_preserving() a bit more, I wonder
it needs to "recurse better" by using an expression walker. Right now
the OpExpr is treated as an opaque expression, we never even get to the
PlaceHolderVar. I'm not sure if that's a problem, though.

regards

--
Tomas Vondra

#7Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#6)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/12/26 10:22, Tomas Vondra wrote:

On 11/12/26 07:38, Kirill Reshke wrote:

On Thu, 12 Nov 2026 at 02:48, Tomas Vondra <tomas@vondra.me> wrote:

Hi,

Here's a rebased version of the patch(es). I've made no change to the
code itself, I've tried to improve the commit message to explain why the
expressions need to be null-preserving, handling of COALESCE clauses and
placeholders.

Hi!

1) Is is_full_join_expr_null_preserving() necessary? Or is there some

existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

I think it is. There is find_nonnullable_rels which solves similar
task, but I don't see any additional smartness in it.
There is also is_standard_join_alias_expression function but it
doesn't check for functions strictness

2) Is is_full_join_expr_null_preserving() correct?

I think it is.

It does lose some optimizations for safe filter pushdows, because
this doesn't recurse
+ if (IsA(node, PlaceHolderVar))
+ return ((PlaceHolderVar *) node)->phlevelsup == 0;

Why would that matter? That returns 'true' for all PlaceHolderVars,
treating them as null-preserving, i.e. allowing pushdown. Why would it
block the optimization in the example you posted?

Correction. It does return true for placeholders with (phlevelsup == 0),
but that is the case of the expression at hand:

{OPEXPR :opno 551 :opfuncid 177 :opresulttype 23 :opretset false
:opcollid 0 :inputcollid 0 :args ({PLACEHOLDERVAR :phexpr {COALESCEEXPR
:coalescetype 23 :coalescecollid 0 :args ({VAR :varno 4 :varattno 1
:vartype 23 :vartypmod -1 :varcollid 0 :varnullingrels (b) :varlevelsup
0 :varreturningtype 0 :varnosyn 4 :varattnosyn 1 :location -1} {CONST
:consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 :constbyval
true :constisnull false :location -1 :constvalue 4 [ 0 0 0 0 0 0 0 0 ]})
:location -1} :phrels (b 4) :phnullingrels (b) :phid 1 :phlevelsup 0}
{CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4
:constbyval true :constisnull false :location -1 :constvalue 4 [ 0 0 0 0
0 0 0 0 ]}) :location -1}

because that has ":phlevelsup 0".

--
Tomas Vondra

#8Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#6)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/12/26 10:22, Tomas Vondra wrote:

On 11/12/26 07:38, Kirill Reshke wrote:

On Thu, 12 Nov 2026 at 02:48, Tomas Vondra <tomas@vondra.me> wrote:

Hi,

Here's a rebased version of the patch(es). I've made no change to the
code itself, I've tried to improve the commit message to explain why the
expressions need to be null-preserving, handling of COALESCE clauses and
placeholders.

Hi!

1) Is is_full_join_expr_null_preserving() necessary? Or is there some

existing piece of code checking the expression preserves NULLs? Possibly
with some additional smartness I haven't thought of?

I think it is. There is find_nonnullable_rels which solves similar
task, but I don't see any additional smartness in it.
There is also is_standard_join_alias_expression function but it
doesn't check for functions strictness

2) Is is_full_join_expr_null_preserving() correct?

I think it is.

It does lose some optimizations for safe filter pushdows, because
this doesn't recurse
+ if (IsA(node, PlaceHolderVar))
+ return ((PlaceHolderVar *) node)->phlevelsup == 0;

Why would that matter? That returns 'true' for all PlaceHolderVars,
treating them as null-preserving, i.e. allowing pushdown. Why would it
block the optimization in the example you posted?

AFAICS it's treating the expression as unsafe because of the "+"
operator, because the associated function is not labeled as strict.

test=# select oid, proname, proisstrict from pg_proc where oid = 177;
oid | proname | proisstrict
-----+---------+-------------
177 | int4pl | t
(1 row)

I don't think that's really the fault of this patch, and I don't see a
way around it - if there are non-strict functions, that makes the whole
expression unsafe.

Ah, damn it. The int4pl *is* strict of course (as obvious from the
output I myself posted). ENOCAFFEINE.

It does indeed descend into the contains_nonstrict_functions(), which
descends into the PlaceHolderVar, and finds the COALESCE(a.x, 0) wrapped
in it, which is not strict.

I don't recall why int4pl is not labeled as strict, maybe it should be?
I haven't thought about that very much, there probably are multiple
threads in the archives discussing that. But that's not something we can
fix without a catversion bump, so it wouldn't help backbranches.

Thinking about is_full_join_expr_null_preserving() a bit more, I wonder
it needs to "recurse better" by using an expression walker. Right now
the OpExpr is treated as an opaque expression, we never even get to the
PlaceHolderVar. I'm not sure if that's a problem, though.

I suppose an expression walker that does not descend into PlaceHolderVar
nodes (with phlevelsup = 0) would make this work correctly.

regards

--
Tomas Vondra

#9Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#8)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/12/26 11:28, Tomas Vondra wrote:

...

I suppose an expression walker that does not descend into PlaceHolderVar
nodes (with phlevelsup = 0) would make this work correctly.

Here's v3 doing it that way.

--
Tomas Vondra

Attachments:

v3-15-0001-Fix-FULL-JOIN-constant-propagation-with-non-st.patchtext/x-patch; charset=UTF-8; name=v3-15-0001-Fix-FULL-JOIN-constant-propagation-with-non-st.patchDownload+272-1
v3-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchtext/x-patch; charset=UTF-8; name=v3-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchDownload+270-1
#10Kirill Reshke
reshkekirill@gmail.com
In reply to: Tomas Vondra (#9)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On Thu, 12 Nov 2026 at 17:57, Tomas Vondra <tomas@vondra.me> wrote:

On 11/12/26 11:28, Tomas Vondra wrote:

...

I suppose an expression walker that does not descend into PlaceHolderVar
nodes (with phlevelsup = 0) would make this work correctly.

Here's v3 doing it that way.

--
Tomas Vondra

v3 looks totally fine for me, perf regressions from my report
eliminated - good.
There are maybe some more cases to recurce, but we shouldn't defer
this bugs fix commit because of this. LGTM

--
Best regards,
Kirill Reshke

#11Tomas Vondra
tomas@vondra.me
In reply to: Kirill Reshke (#10)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/12/26 16:18, Kirill Reshke wrote:

On Thu, 12 Nov 2026 at 17:57, Tomas Vondra <tomas@vondra.me> wrote:

On 11/12/26 11:28, Tomas Vondra wrote:

...

I suppose an expression walker that does not descend into PlaceHolderVar
nodes (with phlevelsup = 0) would make this work correctly.

Here's v3 doing it that way.

--
Tomas Vondra

v3 looks totally fine for me, perf regressions from my report
eliminated - good.
There are maybe some more cases to recurce, but we shouldn't defer
this bugs fix commit because of this. LGTM

True. I picked only a subset of nodes for v3, but I suspect it might be
expanded to the same set of nodes as contain_nonstrict_functions_walker
and check_functions_in_node. I don't know if all of them can appear in
the kind of quals we're processing.

This kind of clauses is not very common, so I was conservative. But it's
not great to cause a regression either ... I have to think about it.

regards

--
Tomas Vondra

#12Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#11)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/12/26 16:40, Tomas Vondra wrote:

On 11/12/26 16:18, Kirill Reshke wrote:

On Thu, 12 Nov 2026 at 17:57, Tomas Vondra <tomas@vondra.me> wrote:

On 11/12/26 11:28, Tomas Vondra wrote:

...

I suppose an expression walker that does not descend into PlaceHolderVar
nodes (with phlevelsup = 0) would make this work correctly.

Here's v3 doing it that way.

--
Tomas Vondra

v3 looks totally fine for me, perf regressions from my report
eliminated - good.
There are maybe some more cases to recurce, but we shouldn't defer
this bugs fix commit because of this. LGTM

True. I picked only a subset of nodes for v3, but I suspect it might be
expanded to the same set of nodes as contain_nonstrict_functions_walker
and check_functions_in_node. I don't know if all of them can appear in
the kind of quals we're processing.

This kind of clauses is not very common, so I was conservative. But it's
not great to cause a regression either ... I have to think about it.

I kept thinking about this, so I gave the expression walker idea a go,
and I like it. The master/15 patches are attached.

The new walker mostly mimics contain_nonstrict_functions_walker() with
some small tweaks for the interesting nodes (as in the previous patch).
I ended up moving it to clauses.c, for two reasons. It makes it easier
to keep the two walkers in sync when they're right next to each other.
It also means we don't need to export e.g. is_strict_saop (which is now
static in clauses.c).

I like this version more than v3. The patch is only a little bit larger,
and I trust this "copy of existing walker" more than I trust my earlier
limited implementation.

I'm now fairly happy with the patch. I'm considering whether to push it
now before the 19 freeze ...

regards

--
Tomas Vondra

Attachments:

v4-15-0001-Fix-FULL-JOIN-constant-propagation-with-non-st.patchtext/x-patch; charset=UTF-8; name=v4-15-0001-Fix-FULL-JOIN-constant-propagation-with-non-st.patchDownload+401-1
v4-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchtext/x-patch; charset=UTF-8; name=v4-master-0001-Fix-FULL-JOIN-constant-propagation-with-no.patchDownload+400-1
#13Kirill Reshke
reshkekirill@gmail.com
In reply to: Tomas Vondra (#12)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On Fri, 13 Nov 2026 at 17:11, Tomas Vondra <tomas@vondra.me> wrote:

I'm now fairly happy with the patch. I'm considering whether to push it
now before the 19 freeze ...

go

--
Best regards,
Kirill Reshke

#14Tomas Vondra
tomas@vondra.me
In reply to: Kirill Reshke (#13)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

On 11/13/26 18:19, Kirill Reshke wrote:

On Fri, 13 Nov 2026 at 17:11, Tomas Vondra <tomas@vondra.me> wrote:

I'm now fairly happy with the patch. I'm considering whether to push it
now before the 19 freeze ...

go

No.

I needed a bit more time to be sure it's correct, and I didn't want to
be the person who derails PG19 release by committing an incorrect fix on
Friday 13th.

After looking more I think the fix is correct, but I while reviewing the
new walker I found a separate bug in the existing one, due to not
checking JsonExpr / CoerceToDomain properly. See [1]/messages/by-id/b368a3ca-b196-4c28-8050-50320e9c955d@vondra.me.

That's mostly orthogonal, but the new walker will need to handle the two
nodes in a similar way.

regards

[1]: /messages/by-id/b368a3ca-b196-4c28-8050-50320e9c955d@vondra.me

--
Tomas Vondra

#15Tomas Vondra
tomas@vondra.me
In reply to: Tomas Vondra (#14)
Re: reconsider_full_join_clause fails to consider if COALESCE() arguments are NULL-preserving

Hi,

I've pushed the v4 fix with some minor adjustments.

The fix does not include the JsonExpr/CoerceToDomain fixes, so these
cases are not handled correctly. I consider that to be a separate
preexisting issue, present in contain_nonstrict_functions_walker(), and
there's a separate thread about that. I could have fixed that in the new
walker in "my" way, but I think it's better to agree on a common fix and
apply it to both places. I don't see a reason to not fix this FULL JOIN
issue independently of that.

regards

--
Tomas Vondra