From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> |
Cc: | Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, Pg Bugs <pgsql-bugs(at)postgresql(dot)org> |
Subject: | Re: Order of columns in GROUP BY is significant to the planner. |
Date: | 2017-12-21 14:37:36 |
Message-ID: | 12086.1513867056@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-bugs |
David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> writes:
> On 21 December 2017 at 19:16, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:
>> The first one inserts a sort node on col1,col2 before doing the Group
>> Aggregate. The second one uses the ordering of the tuples derived from the
>> index scan to do the Group Aggregate directly. Isn't it surprising that the
>> order of the columns in the GROUP BY has to be same as the order in the
>> index definition in order to make maximal use of the index? Is that a bug?
> Not a bug,
No, although you could argue that it's a hangover from back before we
had hash-based grouping. If you look into the parser's processing of
GROUP BY clauses you'll find out that the semantics of GROUP BY are
more tightly tied to ORDER BY than you might have expected --
specifically, the operator class involved, and therefore potentially
the definition of equality, can be absorbed from ORDER BY if the column
lists match. So historically the planner treated GROUP BY + ORDER BY
as largely one thing, and there was no value in considering grouping
with a different column ordering.
> just the number of combinations to try could end up growing
> very large
Yeah, I'm pretty doubtful that the potential improvement would be
worth the extra planner cycles in most cases. Maybe if there are
just two or three GROUP BY columns, it'd be OK to consider all the
combinations, but it could get out of hand very quickly.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | PG Bug reporting form | 2017-12-21 15:06:56 | BUG #14989: Postgis 2.4.2 for PostgreSQL 10 fails to install |
Previous Message | Amit Kapila | 2017-12-21 14:28:08 | Re: vacuum vs heap_update_tuple() and multixactids |