Skip to content

Commit 91800af

Browse files
committed
Ensure generated join clauses for child rels have correct relids.
When building a join clause derived from an EquivalenceClass, if the clause is to be used with an appendrel child relation then make sure its clause_relids include the relids of that child relation. Normally this would be true already because the EquivalenceMember would be a Var of that relation. However, if the appendrel represents a flattened UNION ALL construct then some child EquivalenceMembers could be constants with no relids. The resulting under-marked clause is problematic because it could mislead join_clause_is_movable_into about where the clause should be evaluated. We do not have an example showing incorrect plan generation, but there are existing cases in the regression tests that will fail the Asserts this patch adds to get_baserel_parampathinfo. A similarly wrong conclusion about a clause being considered by get_joinrel_parampathinfo would lead to wrong placement of the clause. (This also squares with the way that clause_relids is calculated for non-equijoin clauses in adjust_appendrel_attrs.) The other reason for wanting these new Asserts is that the previous blithe assumption that the results of generate_join_implied_equalities "necessarily satisfy join_clause_is_movable_into" turns out to be wrong pre-v16. If it's still wrong it'd be good to find out. Per bug #18429 from Benoît Ryder. The bug as filed was fixed by commit 2489d76, but these changes correlate with the fix we will need to apply in pre-v16 branches. Discussion: https://postgr.es/m/18429-8982d4a348cc86c6@postgresql.org
1 parent 7c93f31 commit 91800af

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/backend/optimizer/path/equivclass.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,21 @@ create_join_clause(PlannerInfo *root,
18851885
rightem->em_relids),
18861886
ec->ec_min_security);
18871887

1888+
/*
1889+
* If either EM is a child, force the clause's clause_relids to include
1890+
* the relid(s) of the child rel. In normal cases it would already, but
1891+
* not if we are considering appendrel child relations with pseudoconstant
1892+
* translated variables (i.e., UNION ALL sub-selects with constant output
1893+
* items). We must do this so that join_clause_is_movable_into() will
1894+
* think that the clause should be evaluated at the correct place.
1895+
*/
1896+
if (leftem->em_is_child)
1897+
rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
1898+
leftem->em_relids);
1899+
if (rightem->em_is_child)
1900+
rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
1901+
rightem->em_relids);
1902+
18881903
/* If it's a child clause, copy the parent's rinfo_serial */
18891904
if (parent_rinfo)
18901905
rinfo->rinfo_serial = parent_rinfo->rinfo_serial;

src/backend/optimizer/util/relnode.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,7 @@ get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel,
15351535
ParamPathInfo *ppi;
15361536
Relids joinrelids;
15371537
List *pclauses;
1538+
List *eqclauses;
15381539
Bitmapset *pserials;
15391540
double rows;
15401541
ListCell *lc;
@@ -1570,14 +1571,25 @@ get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel,
15701571

15711572
/*
15721573
* Add in joinclauses generated by EquivalenceClasses, too. (These
1573-
* necessarily satisfy join_clause_is_movable_into.)
1574+
* necessarily satisfy join_clause_is_movable_into; but in assert-enabled
1575+
* builds, let's verify that.)
15741576
*/
1575-
pclauses = list_concat(pclauses,
1576-
generate_join_implied_equalities(root,
1577-
joinrelids,
1578-
required_outer,
1579-
baserel,
1580-
NULL));
1577+
eqclauses = generate_join_implied_equalities(root,
1578+
joinrelids,
1579+
required_outer,
1580+
baserel,
1581+
NULL);
1582+
#ifdef USE_ASSERT_CHECKING
1583+
foreach(lc, eqclauses)
1584+
{
1585+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1586+
1587+
Assert(join_clause_is_movable_into(rinfo,
1588+
baserel->relids,
1589+
joinrelids));
1590+
}
1591+
#endif
1592+
pclauses = list_concat(pclauses, eqclauses);
15811593

15821594
/* Compute set of serial numbers of the enforced clauses */
15831595
pserials = NULL;

0 commit comments

Comments
 (0)