Fix eclass_useful_for_merging to give valid results for appendrel children.
authorTom Lane
Fri, 7 Aug 2015 00:14:37 +0000 (20:14 -0400)
committerTom Lane
Fri, 7 Aug 2015 00:14:37 +0000 (20:14 -0400)
Formerly, this function would always return "true" for an appendrel child
relation, because it would think that the appendrel parent was a potential
join target for the child.  In principle that should only lead to some
inefficiency in planning, but fuzz testing by Andreas Seltenreich disclosed
that it could lead to "could not find pathkey item to sort" planner errors
in odd corner cases.  Specifically, we would think that all columns of a
child table's multicolumn index were interesting pathkeys, causing us to
generate a MergeAppend path that sorts by all the columns.  However, if any
of those columns weren't actually used above the level of the appendrel,
they would not get added to that rel's targetlist, which would result in
being unable to resolve the MergeAppend's sort keys against its targetlist
during createplan.c.

Backpatch to 9.3.  In older versions, columns of an appendrel get added
to its targetlist even if they're not mentioned above the scan level,
so that the failure doesn't occur.  It might be worth back-patching this
fix to older versions anyway, but I'll refrain for the moment.

src/backend/optimizer/path/equivclass.c
src/backend/optimizer/path/pathkeys.c
src/include/optimizer/paths.h
src/test/regress/expected/inherit.out
src/test/regress/sql/inherit.sql

index dc2f94436803fbf081f2837d29610851e00c209f..7ab23413c3a4287ac54f723500b5c8719382578a 100644 (file)
@@ -2286,9 +2286,11 @@ has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
  * from actually being generated.
  */
 bool
-eclass_useful_for_merging(EquivalenceClass *eclass,
+eclass_useful_for_merging(PlannerInfo *root,
+                         EquivalenceClass *eclass,
                          RelOptInfo *rel)
 {
+   Relids      relids;
    ListCell   *lc;
 
    Assert(!eclass->ec_merged);
@@ -2306,8 +2308,14 @@ eclass_useful_for_merging(EquivalenceClass *eclass,
     * possibly-overoptimistic heuristic.
     */
 
+   /* If specified rel is a child, we must consider the topmost parent rel */
+   if (rel->reloptkind == RELOPT_OTHER_MEMBER_REL)
+       relids = find_childrel_top_parent(root, rel)->relids;
+   else
+       relids = rel->relids;
+
    /* If rel already includes all members of eclass, no point in searching */
-   if (bms_is_subset(eclass->ec_relids, rel->relids))
+   if (bms_is_subset(eclass->ec_relids, relids))
        return false;
 
    /* To join, we need a member not in the given rel */
@@ -2318,7 +2326,7 @@ eclass_useful_for_merging(EquivalenceClass *eclass,
        if (cur_em->em_is_child)
            continue;           /* ignore children here */
 
-       if (!bms_overlap(cur_em->em_relids, rel->relids))
+       if (!bms_overlap(cur_em->em_relids, relids))
            return true;
    }
 
index 897be6cb985f8859c5b7f4f2297849de716da19b..a20fec3147c3df11f22d58ab791a16e0633a8b00 100644 (file)
@@ -1322,7 +1322,7 @@ pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys)
         * surely possible to generate a mergejoin clause using them.
         */
        if (rel->has_eclass_joins &&
-           eclass_useful_for_merging(pathkey->pk_eclass, rel))
+           eclass_useful_for_merging(root, pathkey->pk_eclass, rel))
            matched = true;
        else
        {
index 96ffdb195fe62e03e0f5a33ce854855392d5886b..bcc59a820ff3e3023926c443070c4b1205eb1e52 100644 (file)
@@ -139,7 +139,8 @@ extern bool have_relevant_eclass_joinclause(PlannerInfo *root,
                                RelOptInfo *rel1, RelOptInfo *rel2);
 extern bool has_relevant_eclass_joinclause(PlannerInfo *root,
                               RelOptInfo *rel1);
-extern bool eclass_useful_for_merging(EquivalenceClass *eclass,
+extern bool eclass_useful_for_merging(PlannerInfo *root,
+                         EquivalenceClass *eclass,
                          RelOptInfo *rel);
 extern bool is_redundant_derived_clause(RestrictInfo *rinfo, List *clauselist);
 
index c84c435a8ac46564183944e6d0e5500abed1460e..67fb580e304e164566c0057c60691a5a1de0e8af 100644 (file)
@@ -1320,6 +1320,40 @@ DETAIL:  drop cascades to table matest1
 drop cascades to table matest2
 drop cascades to table matest3
 --
+-- Check that use of an index with an extraneous column doesn't produce
+-- a plan with extraneous sorting
+--
+create table matest0 (a int, b int, c int, d int);
+create table matest1 () inherits(matest0);
+create index matest0i on matest0 (b, c);
+create index matest1i on matest1 (b, c);
+set enable_nestloop = off;  -- we want a plan with two MergeAppends
+explain (costs off)
+select t1.* from matest0 t1, matest0 t2
+where t1.b = t2.b and t2.c = t2.d
+order by t1.b limit 10;
+                            QUERY PLAN                             
+-------------------------------------------------------------------
+ Limit
+   ->  Merge Join
+         Merge Cond: (t1.b = t2.b)
+         ->  Merge Append
+               Sort Key: t1.b
+               ->  Index Scan using matest0i on matest0 t1
+               ->  Index Scan using matest1i on matest1 t1_1
+         ->  Materialize
+               ->  Merge Append
+                     Sort Key: t2.b
+                     ->  Index Scan using matest0i on matest0 t2
+                           Filter: (c = d)
+                     ->  Index Scan using matest1i on matest1 t2_1
+                           Filter: (c = d)
+(14 rows)
+
+reset enable_nestloop;
+drop table matest0 cascade;
+NOTICE:  drop cascades to table matest1
+--
 -- Test merge-append for UNION ALL append relations
 --
 set enable_seqscan = off;
index 09bb7507ad9eafe7a1c03db05686f2f9dad30d18..793c216376858a69b3a0c3cfb7f662f0347a8b3c 100644 (file)
@@ -402,6 +402,27 @@ reset enable_seqscan;
 
 drop table matest0 cascade;
 
+--
+-- Check that use of an index with an extraneous column doesn't produce
+-- a plan with extraneous sorting
+--
+
+create table matest0 (a int, b int, c int, d int);
+create table matest1 () inherits(matest0);
+create index matest0i on matest0 (b, c);
+create index matest1i on matest1 (b, c);
+
+set enable_nestloop = off;  -- we want a plan with two MergeAppends
+
+explain (costs off)
+select t1.* from matest0 t1, matest0 t2
+where t1.b = t2.b and t2.c = t2.d
+order by t1.b limit 10;
+
+reset enable_nestloop;
+
+drop table matest0 cascade;
+
 --
 -- Test merge-append for UNION ALL append relations
 --