Skip to content

Commit 477023e

Browse files
committed
Fix some more problems with nested append relations.
As of commit a87c729 (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 5b7b551, 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.)
1 parent d1844c2 commit 477023e

File tree

5 files changed

+619
-3
lines changed

5 files changed

+619
-3
lines changed

src/backend/optimizer/path/equivclass.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,12 @@ generate_base_implied_equalities_no_const(PlannerInfo *root,
902902
* of the EC back into the main restrictinfo datastructures. Multi-relation
903903
* clauses will be regurgitated later by generate_join_implied_equalities().
904904
* (We do it this way to maintain continuity with the case that ec_broken
905-
* becomes set only after we've gone up a join level or two.)
905+
* becomes set only after we've gone up a join level or two.) However, for
906+
* an EC that contains constants, we can adopt a simpler strategy and just
907+
* throw back all the source RestrictInfos immediately; that works because
908+
* we know that such an EC can't become broken later. (This rule justifies
909+
* ignoring ec_has_const ECs in generate_join_implied_equalities, even when
910+
* they are broken.)
906911
*/
907912
static void
908913
generate_base_implied_equalities_broken(PlannerInfo *root,
@@ -914,7 +919,8 @@ generate_base_implied_equalities_broken(PlannerInfo *root,
914919
{
915920
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
916921

917-
if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
922+
if (ec->ec_has_const ||
923+
bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
918924
distribute_restrictinfo_to_rels(root, restrictinfo);
919925
}
920926
}

0 commit comments

Comments
 (0)