When we have successfully optimized a MIN or MAX aggregate into an indexscan,
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 27 Mar 2008 19:06:23 +0000 (19:06 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 27 Mar 2008 19:06:23 +0000 (19:06 +0000)
the query result must be exactly one row (since we don't do this when there's
any GROUP BY).  Therefore any ORDER BY or DISTINCT attached to the query is
useless and can be dropped.  Aside from saving useless cycles, this protects
us against problems with matching the hacked-up tlist entries to sort clauses,
as seen in a bug report from Taiki Yamaguchi.  We might need to work harder
if we ever try to optimize grouped queries with this approach, but this
solution will do for now.

src/backend/optimizer/plan/planner.c

index 8a569d7cbd468259e6bc6da031eb04c3d82bb631..a57fb54cbf040a148a830a23e828eae89f41b10e 100644 (file)
@@ -950,6 +950,17 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
                         * right tlist, and it has no sort order.
                         */
                        current_pathkeys = NIL;
+                       /*
+                        * In fact, since we don't optimize grouped aggregates, it
+                        * needs no sort order --- there must be exactly one output row,
+                        * and so any ORDER BY or DISTINCT attached to the query is
+                        * useless and can be dropped.  Aside from saving useless cycles,
+                        * this protects us against problems with matching the hacked-up
+                        * tlist entries to sort clauses.
+                        */
+                       Assert(!parse->groupClause);
+                       parse->sortClause = NULL;
+                       parse->distinctClause = NULL;
                }
                else
                {