Two bugs in reconsider_full_join_clause(), handling coalesce() = const clauses
Hi,
While working on some improvements to join order search, I cobbled
together a randomized join generator to shake out bugs in my patches.
But it pretty quickly tripped over two long-standing bugs in master.
This is the first one, with a proposed fix.
AFAIK the fix is correct (and not very complex). But it's an area of
code I don't know particularly well (or have dealt before). So it really
needs some review. CC Tom, who wrote some of this code.
The gist of the bug is relatively simple - consider this query
SELECT * FROM (t4 AS r2 JOIN t10 AS r3 ON (r2.id = r3.a1))
FULL JOIN t8 AS r4 ON (r4.t4_id = r2.id)
WHERE coalesce(r4.t4_id, r2.id) = 21501;
On a debug build trips this assert in process_implied_equality():
Assert(bms_is_subset(relids, qualscope));
AFAIK the reason is relatively simple - reconsider_full_join_clause is
not careful enough about which input the expressions come from. It seems
to assume that for clause (leftvar = rightvar) the leftvar comes from
left input, and rightvar comes from the right one. Which will be true
for many cases, but does not seem to be guaranteed. I may be missing
something important, of course. Maybe it's an assumption only clauses
with COALESCE break?
Anyway, reconsider_full_join_clause does it's business, creating the
clauses in both inputs, adjusting the EC etc. In assert build this
crashes in process_implied_equality(). In regular builds the query may
produce incorrect results - I think that can happen only with
variable-free clauses, but I'm not sure. (I'm attaching a reproducer for
this.)
But I think there's a second issue, when handling clauses that don't
relate an expression of one input to an expression of the other.
Consider this query (in repro-wrong-result-2.sql):
SELECT *
FROM (t4 r2 CROSS JOIN t10 r3)
FULL JOIN t8 r4 ON r2.id = r3.a1 AND r2.id = r4.t4_id
WHERE coalesce(r2.id, r3.a1) = 1;
AFAIK this pushes both the r2.id=1 and r3.a1=1 to the left side of the
join, which does not seem correct?
The releases before 16 (which invented the JoinDomain stuff) seem to
have only the second issue. And it does not crash on assert.
The commit messages are somewhat longer. It took me a while to
comprehend what's happening (and that it's not due to my patch), so
maybe it's too detailed.
regards
--
Tomas Vondra
Attachments:
v14-0001-Fix-constant-propagation-through-FULL-JOIN-claus.patchtext/x-patch; charset=UTF-8; name=v14-0001-Fix-constant-propagation-through-FULL-JOIN-claus.patchDownload+130-4
vmaster-0001-Fix-constant-propagation-through-FULL-JOIN-c.patchtext/x-patch; charset=UTF-8; name=vmaster-0001-Fix-constant-propagation-through-FULL-JOIN-c.patchDownload+177-12
Hi,
Here's a much more polished version of the patches, with per-branch
patches. I've significantly shortened the commit message, expanded the
tests a bit to test additional variations, etc.
The 16-master code is the same, there are just some minor plan changes
in tests. The 15 is more different, as it does not have the JoinDomain
stuff, so it needs to fix just one of the bugs.
After all this cleanup and more testing, I feel much more confident in
correctness of the patches. It's not a very complex fix, so I intend to
commit not to long after the minor releases are out.
I'd still welcome a review and feedback, of course. One thing I'm not
entirely sure about is the lookup of SpecialJoinInfo in the fix for 15
(in reconsider_full_join_clause).
I'm wondering about these things:
1) Is there a better way to find the right join for the clause than
walking root->join_info_list? I haven't found any. In 16 and later
reconsider_full_join_clause gets it from the outside through the
OuterJoinClauseInfo. I believe that's not possible in 15.
2) Let's say we need to do the lookup in root->join_info_list. Is the
condition used to "match" the join correct?
bms_equal(rinfo->outer_relids,
bms_union(cur_sjinfo->syn_lefthand,
cur_sjinfo->syn_righthand)))
Or could it be wrong in some corner cases? AFAIK it's correct, but maybe
there's a weird corner case that breaks it.
3) I'm not sure about the elog(ERROR) after the lookup:
if (sjinfo == NULL)
elog(ERROR, "failed to find SpecialJoinInfo for FULL JOIN clause");
Maybe it should be replaced with an assert - we're not really expecting
a failure here, right? We should always find the SpecialJoinInfo. But if
the condition in (2) may not find the right join, maybe it'd be better
to return false here, as if the condition can't be propagated. Otherwise
a production build would segfault on the next block that dereferences
the sjinfo pointer.
So I'm thinking about replacing it with something like
/* failed to find SpecialJoinInfo for FULL JOIN clause */
if (sjinfo == NULL)
{
Assert(false);
return false;
}
Suggestions welcome, ofc.
regards
--
Tomas Vondra
Attachments:
v2-master-0001-Fix-constant-propagation-through-FULL-JOIN.patchtext/x-patch; charset=UTF-8; name=v2-master-0001-Fix-constant-propagation-through-FULL-JOIN.patchDownload+326-12
v2-19-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchtext/x-patch; charset=UTF-8; name=v2-19-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchDownload+326-12
v2-18-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchtext/x-patch; charset=UTF-8; name=v2-18-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchDownload+326-12
v2-17-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchtext/x-patch; charset=UTF-8; name=v2-17-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchDownload+326-12
v2-15-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchtext/x-patch; charset=UTF-8; name=v2-15-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchDownload+142-4
v2-16-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchtext/x-patch; charset=UTF-8; name=v2-16-0001-Fix-constant-propagation-through-FULL-JOIN-cla.patchDownload+326-12
On 11/8/26 00:01, Tomas Vondra wrote:
...
I'm wondering about these things:
1) Is there a better way to find the right join for the clause than
walking root->join_info_list? I haven't found any. In 16 and later
reconsider_full_join_clause gets it from the outside through the
OuterJoinClauseInfo. I believe that's not possible in 15.2) Let's say we need to do the lookup in root->join_info_list. Is the
condition used to "match" the join correct?bms_equal(rinfo->outer_relids,
bms_union(cur_sjinfo->syn_lefthand,
cur_sjinfo->syn_righthand)))Or could it be wrong in some corner cases? AFAIK it's correct, but maybe
there's a weird corner case that breaks it.3) I'm not sure about the elog(ERROR) after the lookup:
if (sjinfo == NULL)
elog(ERROR, "failed to find SpecialJoinInfo for FULL JOIN clause");Maybe it should be replaced with an assert - we're not really expecting
a failure here, right? We should always find the SpecialJoinInfo. But if
the condition in (2) may not find the right join, maybe it'd be better
to return false here, as if the condition can't be propagated. Otherwise
a production build would segfault on the next block that dereferences
the sjinfo pointer.So I'm thinking about replacing it with something like
/* failed to find SpecialJoinInfo for FULL JOIN clause */
if (sjinfo == NULL)
{
Assert(false);
return false;
}Suggestions welcome, ofc.
I've now pushed and backpatched this. I've managed to convince myself
that the 15 fix is correct, and the (sjinfo == NULL) check is not
actually necessary. I've replaced it with a plain assert().
The argument is fairly simple - I considered explaining it in a comment,
but decided not to do that. So I'll at least post it here:
When deconstruct_recurse() builds the join tree, the JOIN_FULL does:
case JOIN_FULL:
leftjoinlist = deconstruct_recurse(root, j->larg,
true,
&leftids, &left_inners,
&child_postponed_quals);
rightjoinlist = deconstruct_recurse(root, j->rarg,
true,
&rightids, &right_inners,
&child_postponed_quals);
*qualscope = bms_union(leftids, rightids);
*inner_join_rels = bms_union(left_inners, right_inners);
/* each side is both outer and inner */
nonnullable_rels = *qualscope;
nullable_rels = *qualscope;
break;
and then
sjinfo = make_outerjoininfo(root,
leftids, rightids,
*inner_join_rels,
j->jointype,
my_quals);
Which then sets leftids/rightids as syn_lefthand/syn_righthand.
distribute_qual_to_rels then uses nonnullable_rels as outer_relids. And
nonnullable_rels was calculated as bms_union(leftids, rightids).
This exactly matches the bms_equal() condition in the lookup. Also,
there really ought to be the SJI in the list, because all of this runs
from reconsider_outer_join_clauses(), right after the join tree is built
by deconstruct_jointree().
regards
--
Tomas Vondra
On Wed, 11 Nov 2026 at 20:54, Tomas Vondra <tomas@vondra.me> wrote:
On 11/8/26 00:01, Tomas Vondra wrote:
...
I'm wondering about these things:
1) Is there a better way to find the right join for the clause than
walking root->join_info_list? I haven't found any. In 16 and later
reconsider_full_join_clause gets it from the outside through the
OuterJoinClauseInfo. I believe that's not possible in 15.2) Let's say we need to do the lookup in root->join_info_list. Is the
condition used to "match" the join correct?bms_equal(rinfo->outer_relids,
bms_union(cur_sjinfo->syn_lefthand,
cur_sjinfo->syn_righthand)))Or could it be wrong in some corner cases? AFAIK it's correct, but maybe
there's a weird corner case that breaks it.3) I'm not sure about the elog(ERROR) after the lookup:
if (sjinfo == NULL)
elog(ERROR, "failed to find SpecialJoinInfo for FULL JOIN clause");Maybe it should be replaced with an assert - we're not really expecting
a failure here, right? We should always find the SpecialJoinInfo. But if
the condition in (2) may not find the right join, maybe it'd be better
to return false here, as if the condition can't be propagated. Otherwise
a production build would segfault on the next block that dereferences
the sjinfo pointer.So I'm thinking about replacing it with something like
/* failed to find SpecialJoinInfo for FULL JOIN clause */
if (sjinfo == NULL)
{
Assert(false);
return false;
}Suggestions welcome, ofc.
I've now pushed and backpatched this. I've managed to convince myself
that the 15 fix is correct, and the (sjinfo == NULL) check is not
actually necessary. I've replaced it with a plain assert().The argument is fairly simple - I considered explaining it in a comment,
but decided not to do that. So I'll at least post it here:When deconstruct_recurse() builds the join tree, the JOIN_FULL does:
case JOIN_FULL:
leftjoinlist = deconstruct_recurse(root, j->larg,
true,
&leftids, &left_inners,
&child_postponed_quals);
rightjoinlist = deconstruct_recurse(root, j->rarg,
true,
&rightids, &right_inners,
&child_postponed_quals);
*qualscope = bms_union(leftids, rightids);
*inner_join_rels = bms_union(left_inners, right_inners);
/* each side is both outer and inner */
nonnullable_rels = *qualscope;
nullable_rels = *qualscope;
break;and then
sjinfo = make_outerjoininfo(root,
leftids, rightids,
*inner_join_rels,
j->jointype,
my_quals);Which then sets leftids/rightids as syn_lefthand/syn_righthand.
distribute_qual_to_rels then uses nonnullable_rels as outer_relids. And
nonnullable_rels was calculated as bms_union(leftids, rightids).This exactly matches the bms_equal() condition in the lookup. Also,
there really ought to be the SJI in the list, because all of this runs
from reconsider_outer_join_clauses(), right after the join tree is built
by deconstruct_jointree().regards
--
Tomas Vondra
Hi!
Cool findings.
There is another issue I found during review, but this issue is not
directly related (exist on master before and after
e50d958f092bc33fcb51089c68c72779bae2a6a3), and maybe should be fixed
as separate bugs.But I decided to report here
example setup & query
CREATE TABLE fj_left (x int);
CREATE TABLE fj_right (x int);
INSERT INTO fj_left VALUES (0);
INSERT INTO fj_right VALUES (1);
SELECT a.x AS ax, b.x AS bx
FROM fj_left a FULL JOIN fj_right b ON concat(a.x) = b.x::text
WHERE coalesce(concat(a.x), b.x::text) = '';
(zero rows)
The thing is reconsider_full_join_clause() assumes that a COALESCE
operand becomes NULL its input is. That does not hold for nonstrict
functions like concat(), num_nulls, format, etc.
If disable full join predicate consideration with OFFSET 0, query
behaves correctly.
reshke=# SELECT * FROM (
SELECT a.x AS ax, b.x AS bx
FROM fj_left a FULL JOIN fj_right b ON concat(a.x) = b.x::text OFFSET 0
) s WHERE coalesce(concat(ax), bx::text) = '';
ax | bx
----+----
| 1
(1 row)
another example:
SELECT a.x AS ax, b.x AS bx
FROM fj_left a FULL JOIN fj_right b ON quote_nullable(a.x) = b.x::text
WHERE coalesce(quote_nullable(a.x), b.x::text) = 'NULL';
(0 rows, 1 row with OFFSET 0 hack)
--
Best regards,
Kirill Reshke
On 11/11/26 19:49, Kirill Reshke wrote:
...
Hi!
Cool findings.There is another issue I found during review, but this issue is not
directly related (exist on master before and after
e50d958f092bc33fcb51089c68c72779bae2a6a3), and maybe should be fixed
as separate bugs.But I decided to report hereexample setup & query
CREATE TABLE fj_left (x int);
CREATE TABLE fj_right (x int);
INSERT INTO fj_left VALUES (0);
INSERT INTO fj_right VALUES (1);SELECT a.x AS ax, b.x AS bx
FROM fj_left a FULL JOIN fj_right b ON concat(a.x) = b.x::text
WHERE coalesce(concat(a.x), b.x::text) = '';(zero rows)
Isn't this the same issue as
/messages/by-id/0074fbf7-a92c-417a-937a-6b1fc27aed82@vondra.me
?
The fix I posted in that thread is a bit more elaborate, but also uses
contain_nonstrict_functions. Please take a look and check if my fix
addresses this issue, and whether it's correct.
regards
--
Tomas Vondra
On 11/11/26 20:11, Tomas Vondra wrote:
On 11/11/26 19:49, Kirill Reshke wrote:
...
Hi!
Cool findings.There is another issue I found during review, but this issue is not
directly related (exist on master before and after
e50d958f092bc33fcb51089c68c72779bae2a6a3), and maybe should be fixed
as separate bugs.But I decided to report hereexample setup & query
CREATE TABLE fj_left (x int);
CREATE TABLE fj_right (x int);
INSERT INTO fj_left VALUES (0);
INSERT INTO fj_right VALUES (1);SELECT a.x AS ax, b.x AS bx
FROM fj_left a FULL JOIN fj_right b ON concat(a.x) = b.x::text
WHERE coalesce(concat(a.x), b.x::text) = '';(zero rows)
Isn't this the same issue as
/messages/by-id/0074fbf7-a92c-417a-937a-6b1fc27aed82@vondra.me
?The fix I posted in that thread is a bit more elaborate, but also uses
contain_nonstrict_functions. Please take a look and check if my fix
addresses this issue, and whether it's correct.
FWIW with my patch applied your example query returns
ax | bx
----+----
| 1
(1 row)
which I think is the correct / expected result. So I guess it really is
the same issue.
regards
--
Tomas Vondra