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

Commit ff72280

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 39627b1 commit ff72280

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/backend/optimizer/plan/planner.c

+12-1
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.227 2008/03/18 22:04:14 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.228 2008/03/27 19:06:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -950,6 +950,17 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
950950
* right tlist, and it has no sort order.
951951
*/
952952
current_pathkeys = NIL;
953+
/*
954+
* In fact, since we don't optimize grouped aggregates, it
955+
* needs no sort order --- there must be exactly one output row,
956+
* and so any ORDER BY or DISTINCT attached to the query is
957+
* useless and can be dropped. Aside from saving useless cycles,
958+
* this protects us against problems with matching the hacked-up
959+
* tlist entries to sort clauses.
960+
*/
961+
Assert(!parse->groupClause);
962+
parse->sortClause = NULL;
963+
parse->distinctClause = NULL;
953964
}
954965
else
955966
{

0 commit comments

Comments
 (0)