Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 707867e

Browse files
committed
When we have successfully optimized a MIN or MAX aggregate into an indexscan,
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.
1 parent 1c0e618 commit 707867e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.226 2008/01/01 19:45:50 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.226.2.1 2008/03/27 19:06:23 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -943,6 +943,17 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
943943
* right tlist, and it has no sort order.
944944
*/
945945
current_pathkeys = NIL;
946+
/*
947+
* In fact, since we don't optimize grouped aggregates, it
948+
* needs no sort order --- there must be exactly one output row,
949+
* and so any ORDER BY or DISTINCT attached to the query is
950+
* useless and can be dropped. Aside from saving useless cycles,
951+
* this protects us against problems with matching the hacked-up
952+
* tlist entries to sort clauses.
953+
*/
954+
Assert(!parse->groupClause);
955+
parse->sortClause = NULL;
956+
parse->distinctClause = NULL;
946957
}
947958
else
948959
{

0 commit comments

Comments
 (0)