]> granicus.if.org Git - postgresql/commitdiff
Back-patch "Fix EquivalenceClass processing for nested append relations".
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Jun 2014 17:41:06 +0000 (10:41 -0700)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 26 Jun 2014 17:42:03 +0000 (10:42 -0700)
When we committed a87c729153e372f3731689a7be007bc2b53f1410, we somehow
failed to notice that it didn't merely improve plan quality for expression
indexes; there were very closely related cases that failed outright with
"could not find pathkey item to sort".  The failing cases seem to be those
where the planner was already capable of selecting a MergeAppend plan,
and there was inheritance involved: the lack of appropriate eclass child
members would prevent prepare_sort_from_pathkeys() from succeeding on the
MergeAppend's child plan nodes for inheritance child tables.

Accordingly, back-patch into 9.1 through 9.3, along with an extra
regression test case covering the problem.

Per trouble report from Michael Glaesemann.

src/backend/optimizer/path/allpaths.c
src/backend/optimizer/path/equivclass.c
src/backend/optimizer/plan/createplan.c
src/test/regress/expected/union.out
src/test/regress/sql/union.sql

index 56bbaece3db96f50741f99fa90a01fa84716d80a..5c6470af72a08fbc2d37dee7a69e8aa212577b4f 100644 (file)
@@ -1011,10 +1011,15 @@ get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
  * accumulate_append_subpath
  *             Add a subpath to the list being built for an Append or MergeAppend
  *
- * It's possible that the child is itself an Append path, in which case
- * we can "cut out the middleman" and just add its child paths to our
- * own list.  (We don't try to do this earlier because we need to
- * apply both levels of transformation to the quals.)
+ * It's possible that the child is itself an Append or MergeAppend path, in
+ * which case we can "cut out the middleman" and just add its child paths to
+ * our own list.  (We don't try to do this earlier because we need to apply
+ * both levels of transformation to the quals.)
+ *
+ * Note that if we omit a child MergeAppend in this way, we are effectively
+ * omitting a sort step, which seems fine: if the parent is to be an Append,
+ * its result would be unsorted anyway, while if the parent is to be a
+ * MergeAppend, there's no point in a separate sort on a child.
  */
 static List *
 accumulate_append_subpath(List *subpaths, Path *path)
@@ -1026,6 +1031,13 @@ accumulate_append_subpath(List *subpaths, Path *path)
                /* list_copy is important here to avoid sharing list substructure */
                return list_concat(subpaths, list_copy(apath->subpaths));
        }
+       else if (IsA(path, MergeAppendPath))
+       {
+               MergeAppendPath *mpath = (MergeAppendPath *) path;
+
+               /* list_copy is important here to avoid sharing list substructure */
+               return list_concat(subpaths, list_copy(mpath->subpaths));
+       }
        else
                return lappend(subpaths, path);
 }
index 19c2edfddd8b00a5afca2fa3e5e231d178e1d0db..e242db9dbb055f77a2f2591d7a386635757c9fa1 100644 (file)
@@ -1937,16 +1937,20 @@ add_child_rel_equivalences(PlannerInfo *root,
                if (cur_ec->ec_has_volatile)
                        continue;
 
-               /* No point in searching if parent rel not mentioned in eclass */
-               if (!bms_is_subset(parent_rel->relids, cur_ec->ec_relids))
+               /*
+                * No point in searching if parent rel not mentioned in eclass; but
+                * we can't tell that for sure if parent rel is itself a child.
+                */
+               if (parent_rel->reloptkind == RELOPT_BASEREL &&
+                       !bms_is_subset(parent_rel->relids, cur_ec->ec_relids))
                        continue;
 
                foreach(lc2, cur_ec->ec_members)
                {
                        EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc2);
 
-                       if (cur_em->em_is_const || cur_em->em_is_child)
-                               continue;               /* ignore consts and children here */
+                       if (cur_em->em_is_const)
+                               continue;               /* ignore consts here */
 
                        /* Does it reference parent_rel? */
                        if (bms_overlap(cur_em->em_relids, parent_rel->relids))
index c5df505d49196c164f4de5117f8c81405a709f67..91be71351bce39e5a05746c3b2d1f3ff04ec636e 100644 (file)
@@ -731,7 +731,7 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
 
        /* Compute sort column info, and adjust MergeAppend's tlist as needed */
        (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
-                                                                         NULL,
+                                                                         best_path->path.parent->relids,
                                                                          NULL,
                                                                          true,
                                                                          &node->numCols,
index b7c4c7b312123a498d0d3baac44f8879180c03a1..beefea5cf4e7c05dd9a969cd765aab89cb799c08 100644 (file)
@@ -500,9 +500,39 @@ explain (costs off)
                Index Cond: (ab = 'ab'::text)
 (6 rows)
 
+--
+-- Test that ORDER BY for UNION ALL can be pushed down to inheritance
+-- children.
+--
 reset enable_seqscan;
 reset enable_indexscan;
 reset enable_bitmapscan;
+set enable_indexonlyscan = off;
+create table events (event_id int primary key);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "events_pkey" for table "events"
+create table other_events (event_id int primary key);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "other_events_pkey" for table "other_events"
+create table events_child () inherits (events);
+explain (costs off)
+select event_id
+ from (select event_id from events
+       union all
+       select event_id from other_events) ss
+ order by event_id;
+                           QUERY PLAN                           
+----------------------------------------------------------------
+ Result
+   ->  Merge Append
+         Sort Key: public.events.event_id
+         ->  Index Scan using events_pkey on events
+         ->  Sort
+               Sort Key: public.events.event_id
+               ->  Seq Scan on events_child events
+         ->  Index Scan using other_events_pkey on other_events
+(8 rows)
+
+drop table events_child, events, other_events;
+reset enable_indexonlyscan;
 -- Test constraint exclusion of UNION ALL subqueries
 explain (costs off)
  SELECT * FROM
index 6194ce475110944abb69951b70c66ef368a8834a..7e895183e7d4ec5b6137b85dc9a73cf56cc58532 100644 (file)
@@ -196,9 +196,30 @@ explain (costs off)
   SELECT * FROM t2) t
  WHERE ab = 'ab';
 
+--
+-- Test that ORDER BY for UNION ALL can be pushed down to inheritance
+-- children.
+--
+
 reset enable_seqscan;
 reset enable_indexscan;
 reset enable_bitmapscan;
+set enable_indexonlyscan = off;
+
+create table events (event_id int primary key);
+create table other_events (event_id int primary key);
+create table events_child () inherits (events);
+
+explain (costs off)
+select event_id
+ from (select event_id from events
+       union all
+       select event_id from other_events) ss
+ order by event_id;
+
+drop table events_child, events, other_events;
+
+reset enable_indexonlyscan;
 
 -- Test constraint exclusion of UNION ALL subqueries
 explain (costs off)