1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Fix some more problems with nested append relations.

As of commit a87c72915 (which later got backpatched as far as 9.1),
we're explicitly supporting the notion that append relations can be
nested; this can occur when UNION ALL constructs are nested, or when
a UNION ALL contains a table with inheritance children.

Bug #11457 from Nelson Page, as well as an earlier report from Elvis
Pranskevichus, showed that there were still nasty bugs associated with such
cases: in particular the EquivalenceClass mechanism could try to generate
"join" clauses connecting an appendrel child to some grandparent appendrel,
which would result in assertion failures or bogus plans.

Upon investigation I concluded that all current callers of
find_childrel_appendrelinfo() need to be fixed to explicitly consider
multiple levels of parent appendrels.  The most complex fix was in
processing of "broken" EquivalenceClasses, which are ECs for which we have
been unable to generate all the derived equality clauses we would like to
because of missing cross-type equality operators in the underlying btree
operator family.  That code path is more or less entirely untested by
the regression tests to date, because no standard opfamilies have such
holes in them.  So I wrote a new regression test script to try to exercise
it a bit, which turned out to be quite a worthwhile activity as it exposed
existing bugs in all supported branches.

The present patch is essentially the same as far back as 9.2, which is
where parameterized paths were introduced.  In 9.0 and 9.1, we only need
to back-patch a small fragment of commit 5b7b5518d, which fixes failure to
propagate out the original WHERE clauses when a broken EC contains constant
members.  (The regression test case results show that these older branches
are noticeably stupider than 9.2+ in terms of the quality of the plans
generated; but we don't really care about plan quality in such cases,
only that the plan not be outright wrong.  A more invasive fix in the
older branches would not be a good idea anyway from a plan-stability
standpoint.)
This commit is contained in:
Tom Lane
2014-10-01 19:30:24 -04:00
parent 0c013e08cf
commit 5a6c168c78
10 changed files with 717 additions and 27 deletions

View File

@ -48,7 +48,7 @@ static List *generate_join_implied_equalities_broken(PlannerInfo *root,
Relids nominal_join_relids,
Relids outer_relids,
Relids nominal_inner_relids,
AppendRelInfo *inner_appinfo);
RelOptInfo *inner_rel);
static Oid select_equality_operator(EquivalenceClass *ec,
Oid lefttype, Oid righttype);
static RestrictInfo *create_join_clause(PlannerInfo *root,
@ -1000,22 +1000,18 @@ generate_join_implied_equalities(PlannerInfo *root,
Relids inner_relids = inner_rel->relids;
Relids nominal_inner_relids;
Relids nominal_join_relids;
AppendRelInfo *inner_appinfo;
ListCell *lc;
/* If inner rel is a child, extra setup work is needed */
if (inner_rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
{
/* Lookup parent->child translation data */
inner_appinfo = find_childrel_appendrelinfo(root, inner_rel);
/* Construct relids for the parent rel */
nominal_inner_relids = bms_make_singleton(inner_appinfo->parent_relid);
/* Fetch relid set for the topmost parent rel */
nominal_inner_relids = find_childrel_top_parent(root, inner_rel)->relids;
/* ECs will be marked with the parent's relid, not the child's */
nominal_join_relids = bms_union(outer_relids, nominal_inner_relids);
}
else
{
inner_appinfo = NULL;
nominal_inner_relids = inner_relids;
nominal_join_relids = join_relids;
}
@ -1051,7 +1047,7 @@ generate_join_implied_equalities(PlannerInfo *root,
nominal_join_relids,
outer_relids,
nominal_inner_relids,
inner_appinfo);
inner_rel);
result = list_concat(result, sublist);
}
@ -1244,7 +1240,7 @@ generate_join_implied_equalities_broken(PlannerInfo *root,
Relids nominal_join_relids,
Relids outer_relids,
Relids nominal_inner_relids,
AppendRelInfo *inner_appinfo)
RelOptInfo *inner_rel)
{
List *result = NIL;
ListCell *lc;
@ -1266,10 +1262,16 @@ generate_join_implied_equalities_broken(PlannerInfo *root,
* RestrictInfos that are not listed in ec_derives, but there shouldn't be
* any duplication, and it's a sufficiently narrow corner case that we
* shouldn't sweat too much over it anyway.
*
* Since inner_rel might be an indirect descendant of the baserel
* mentioned in the ec_sources clauses, we have to be prepared to apply
* multiple levels of Var translation.
*/
if (inner_appinfo)
result = (List *) adjust_appendrel_attrs(root, (Node *) result,
inner_appinfo);
if (inner_rel->reloptkind == RELOPT_OTHER_MEMBER_REL &&
result != NIL)
result = (List *) adjust_appendrel_attrs_multilevel(root,
(Node *) result,
inner_rel);
return result;
}
@ -2071,14 +2073,14 @@ generate_implied_equalities_for_column(PlannerInfo *root,
{
List *result = NIL;
bool is_child_rel = (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
Index parent_relid;
Relids parent_relids;
ListCell *lc1;
/* If it's a child rel, we'll need to know what its parent is */
/* If it's a child rel, we'll need to know what its parent(s) are */
if (is_child_rel)
parent_relid = find_childrel_appendrelinfo(root, rel)->parent_relid;
parent_relids = find_childrel_parents(root, rel);
else
parent_relid = 0; /* not used, but keep compiler quiet */
parent_relids = NULL; /* not used, but keep compiler quiet */
foreach(lc1, root->eq_classes)
{
@ -2148,10 +2150,10 @@ generate_implied_equalities_for_column(PlannerInfo *root,
/*
* Also, if this is a child rel, avoid generating a useless join
* to its parent rel.
* to its parent rel(s).
*/
if (is_child_rel &&
bms_is_member(parent_relid, other_em->em_relids))
bms_overlap(parent_relids, other_em->em_relids))
continue;
eq_op = select_equality_operator(cur_ec,

View File

@ -2586,16 +2586,11 @@ check_partial_indexes(PlannerInfo *root, RelOptInfo *rel)
* Add on any equivalence-derivable join clauses. Computing the correct
* relid sets for generate_join_implied_equalities is slightly tricky
* because the rel could be a child rel rather than a true baserel, and in
* that case we must remove its parent's relid from all_baserels.
* that case we must remove its parents' relid(s) from all_baserels.
*/
if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
{
/* Lookup parent->child translation data */
AppendRelInfo *appinfo = find_childrel_appendrelinfo(root, rel);
otherrels = bms_difference(root->all_baserels,
bms_make_singleton(appinfo->parent_relid));
}
find_childrel_parents(root, rel));
else
otherrels = bms_difference(root->all_baserels, rel->relids);