Re: Assert failure in try_nestloop_path()
Re: Assert failure in try_nestloop_path()
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Richard Guo writes: > I looked into it. What happens here is that outer-join identity 3 > permits the join to t3 to commute with the a/b left join, so we > generate multiple clones of "b.q1 = t3.cnt", differing in the > nullingrels of b.q1. Only one of them should be applied in any given > plan. However, when movable join clauses are pushed down into a > parameterized path, we fail to choose among the clones. Yup, clearly an oversight. Your fix LGTM. I'm not sure that we should push it into stable branches: it might destabilize queries that people are happy with, and we don't have field complaints about it. But v19 should still be fair game at this point. regards, tom lane
Re: Assert failure in try_nestloop_path()
On Mon, Sep 7, 2026 at 12:03 PM Richard Guo wrote:
> Thanks for reviewing! I agree that back-patching into stable
> branches isn't worth the risk of plan changes, given the lack of field
> complaints. I've pushed this to master and v19.
I was curious whether there are other cases where we can end up with
duplicate qual clauses, so I added the attached Assert to verify that
the clauses to be enforced at a join or at a parameterized path's scan
contain no duplicate rinfo_serial, and the regression tests
immediately crashed :-O
One regression query that trips the Assert is:
explain (costs off)
select * from onek t1
left join onek t2 on t1.unique1 = t2.unique1
left join onek t3 on t2.unique1 = t3.unique1
left join onek t4 on t3.unique1 = t4.unique1 and t2.unique2 = t4.unique2;
It crashes in get_baserel_parampathinfo for base rel t4, whose
joininfo contains four clauses:
[0] serial=3 clause_relids={4 5 6} required={2 4 5 6} incompatible={3 7}
[1] serial=4 clause_relids={2 6} required={2 4 5 6} incompatible={3 7}
[2] serial=3 clause_relids={4 5 6} required={2 3 4 5 6} incompatible={7}
[3] serial=4 clause_relids={2 3 6} required={2 3 4 5 6} incompatible={7}
[0] and [2] are two clone variants of "t3.unique1 = t4.unique1".
Since the commuting outer join (relid 3) nulls no Var referenced by
this clause, the two variants are textually identical, differing only
in required_relids and incompatible_relids.
When t4 is probed with required_outer = {4, 5}, joinrelids is {4 5 6}.
Both variants are movable into the scan, and neither one's
incompatible_relids overlaps joinrelids, so both end up in
ppi_clauses.
It seems to me that something is wrong somewhere.
After a closer look, I don't think anything is wrong in
deconstruct_distribute_oj_quals. Both variants are needed for clause
selection at joins, where subbuild_joinrel_restrictlist checks
required_relids and incompatible_relids against the input relids. In
the normal join order, [0] is rejected because relid 3 appears in its
incompatible_relids, and [2] is the one applied; in the commuted
order, where t3/t4 join is performed below t1/t2 join, [2] is rejected
because relid 3 in its required_relids is not available, and [0] is
the one applied.
But a parameterized path cannot tell them apart. Since outer join 3
nulls no Var referenced by this clause, the parameterization looks
exactly the same whether that join is computed below the scan (the
normal join order, where [2] is the right variant) or above it (the
commuted order, where [0] is), so the same ParamPathInfo serves both
orders. For the same reason, either variant is correct in any join
order, so I think we should just enforce one of them and ignore the
rest? Thought?
- Richard
Re: Assert failure in try_nestloop_path()
On Tue, Sep 8, 2026 at 12:13 PM Richard Guo wrote: > Hmm, I don't think bizarre join orderings are required. The duplicate > arises whenever the qual pushed down into the parameterized path is > not affected by the commuting outer join, which seems quite a common > case. Here is an example where the duplicate shows up in the selected > plan, with the joins performed just in syntactic order: ... and here is a patch to fix it. Now we have three patches fixing three duplicate-clause issues. - Richard
Re: Assert failure in try_nestloop_path()
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Richard Guo writes: > ... and here is a patch to fix it. I think the find_clause_by_serial tests are just clutter, really. Also, why is the last code hunk in get_joinrel_parampathinfo commented out? regards, tom lane
Re: Assert failure in try_nestloop_path()
Richard Guo writes: > Yes. I've merged the clone-dedup fix and your > drop-redundant-equivalence-clauses fix into a single patch (0001 > attached), removed the find_clause_by_serial cross-checks, and > un-commented the assertion in get_joinrel_parampathinfo, which now > holds with your fix in place. The one nit I can find to pick with this is that I don't really like the description of the clauses of concern as "textually identical". That makes it sound like surface syntax details (like number of spaces or presence of an unnecessary table qualifier) matter. Perhaps say "parse-tree identical", or "equal()"? That seems a bit jargony but I have no better ideas. > I've kept the create_join_clause change as a separate patch (0002), > since it's a distinct issue. There the trouble is that two > RestrictInfos for the same condition end up with different > rinfo_serials, rather than that we enforce multiple clones of the > same clause. I wonder if we are going to push the rinfo_serial mechanism too far. But that's just a vague feeling of discomfort, I can't say there is anything wrong with fixing it this way. > I plan to push both to HEAD and v19 but no further if there are no > objections. I wonder if we should leave the Assert bits out of v19. The intent to have hard prevention of duplicate clauses is a brand new aspiration, and I have pretty much no faith that no such cases remain. I don't really want to commit to fixing all such cases in v19. regards, tom lane
Re: Assert failure in try_nestloop_path()
От:
Alexander Lakhin <exclusion@gmail.com>
Дата:
Hello Richard and Tom,
10.09.2026 04:36, Richard Guo wrote:
10.09.2026 04:36, Richard Guo wrote:
On Wed, Sep 9, 2026 at 11:35 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:I wonder if we should leave the Assert bits out of v19. The intent to have hard prevention of duplicate clauses is a brand new aspiration, and I have pretty much no faith that no such cases remain. I don't really want to commit to fixing all such cases in v19.Fair point. I kept the assertions on HEAD only, with a note in the commit message. I've pushed both to master and v19.
I've discovered that:
echo "geqo_threshold = 2" >/tmp/temp.config
TEMP_CONFIG=/tmp/temp.config TESTS="partition_join" make -s check-tests
triggers one of the assertions:
TRAP: failed Assert("no_duplicate_clause_serials(*restrict_clauses)"), File: "relnode.c", Line: 2079, PID: 634394
Could you have a look, please?
Best regards,
Alexander
Re: Assert failure in try_nestloop_path()
От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Sun, Sep 27, 2026 at 1:00 AM Alexander Lakhin wrote:
> I've discovered that:
> echo "geqo_threshold = 2" >/tmp/temp.config
> TEMP_CONFIG=/tmp/temp.config TESTS="partition_join" make -s check-tests
>
> triggers one of the assertions:
> TRAP: failed Assert("no_duplicate_clause_serials(*restrict_clauses)"), File: "relnode.c", Line: 2079, PID: 634394
Thanks for the report. I think the culprit is that during GEQO join
planning, add_child_join_rel_equivalences() will re-add the same child
EC members in each GEQO round, since it has no concept that the
members it wants might be there already. As a result, in the reported
case, the same EC member PHV(COALESCE(t3_p1.c, t4.c)) appears three
times in the same EC. So, in get_joinrel_parampathinfo(), the
generate_join_implied_equalities() call generate two identical
RestrictInfos for "PHV(...) = PHV(...)".
I think we need to do something to avoid generating duplicate EC
members in add_child_join_rel_equivalences().
- Richard
Re: Assert failure in try_nestloop_path()
On Tue, Sep 29, 2026 at 4:20 PM Richard Guo wrote:
> I think we need to do something to avoid generating duplicate EC
> members in add_child_join_rel_equivalences().
After looking into this more, I don't think that is enough. The
problem is not specific to GEQO.
For instance, the plan of an existing query in partition_join.sql
already enforces the same condition twice:
-> Nested Loop
Join Filter: (((COALESCE(t3_3.c, t4_3.c)))::text = (t1_3.c)::text)
-> Index Scan using prt3_p3_a_idx on prt3_p3 t3_3
Index Cond: ((a = t4_3.a) AND (a IS NOT NULL))
-> Index Only Scan using prt4_p3_c_a_idx on prt4_p3 t1_3
Index Cond: ((c = ((COALESCE(t3_3.c, t4_3.c)))::text)
AND (a = t3_3.a))
And the same assertion can be hit without GEQO:
set enable_partitionwise_join to on;
explain (costs off)
select t1.a, t1.c, t3.c, t5.f1 from prt4 t1 join prt3 t2 on t1.a = t2.a
join prt1 t3 on t2.a = t3.a, int4_tbl t5
where t1.c = coalesce(t3.c, t5.f1::text);
Here the child joins {t1, t3}, {t2, t3} and {t1, t2, t3} each add
their own child version of the same EC member, and they get equated
to each other when we build the clauses for the three-way child join
parameterized by t5.
What these cases have in common is that the EC member references a rel
outside the child join. add_child_join_rel_equivalences() builds a
child version of any multi-relation member that overlaps the join's
parent relids. But child-join members are there for matching pathkeys
of the child join, and a member that references an outside rel cannot
be computed at the join, so it is of no use for that. Its only effect
is on the clauses we generate for parameterized child-join paths,
which is where the duplicates come from.
So I think the fix is to not build such members in the first place, by
considering only members that can be computed at the parent join.
This is what add_child_rel_equivalences() has been doing for base rels
since 2489d76c4.
Attached is a patch for that. The existing query above loses its
redundant join filter.
Thoughts?
- Richard