Fix relids of EC-derived join clauses for lateral UNION ALL members When a LATERAL UNION ALL subquery is flattened into an appendrel, a child EquivalenceMember's expression can contain lateral references to other relations. create_join_clause() builds a clause from such a member with make_restrictinfo(), which computes clause_relids from the expression, and so includes the relids of the lateral references along with those of any outer joins that null them. Commit 03107b4ed made sure that clause_relids include the child's relids, but it only ever added relids.
This is a problem when a lateral reference is nullable. The child is parameterized by its lateral_relids, which do not include the outer joins that null the lateral references, so the clause fails the join_clause_is_movable_into() assertion that get_baserel_parampathinfo() applies to EC-derived clauses. To fix, set a child clause's clause_relids from its members' em_relids rather than from their expressions. This matches how adjust_appendrel_attrs() computes clause_relids for a child's other join clauses. The lateral references need not be included, since every path for the child is parameterized by at least its lateral_relids. Also update num_base_rels to match, which commit 03107b4ed neglected to do. Back-patch to v16, where outer-join relids were added to clause_relids. Before that, the relids of a lateral reference are always part of the child's parameterization, so the problem does not arise. Author: Richard Guo <[email protected]> Reviewed-by: wenhui qiu <[email protected]> Discussion: https://postgr.es/m/CAMbWs4-cRxpQNyQJW=PkDagSDGQ=3rp2t_fsayroksdstjk...@mail.gmail.com Backpatch-through: 16 Branch ------ REL_17_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/b83b7ddf7a64dce41624b5c0dabefd74dfe10ec0 Modified Files -------------- src/backend/optimizer/path/equivclass.c | 32 +++++++----- src/test/regress/expected/join.out | 92 +++++++++++++++++++++++++++++++++ src/test/regress/sql/join.sql | 26 ++++++++++ 3 files changed, 138 insertions(+), 12 deletions(-)
