From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <david.rowley@2ndquadrant.com>
Cc: Jeff Janes <jeff.janes@gmail.com>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Order of columns in GROUP BY is significant to the planner.
Date: Thu, 21 Dec 2017 09:37:36 -0500
Message-ID: <12086.1513867056@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKJS1f-ic8+DVSt1GJCG1njkbS5ui4d4-tmmkhSt0Aq2q+B_uQ@mail.gmail.com>
References: <CAMkU=1w9scCPMicmhzrAkUUFVnmJ5JKok5K2KD_qNvXYyEQZ+A@mail.gmail.com>
<CAKJS1f-ic8+DVSt1GJCG1njkbS5ui4d4-tmmkhSt0Aq2q+B_uQ@mail.gmail.com>
David Rowley <david.rowley@2ndquadrant.com> writes:
> On 21 December 2017 at 19:16, Jeff Janes <jeff.janes@gmail.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, david.rowley@2ndquadrant.com, jeff.janes@gmail.com
Subject: Re: Order of columns in GROUP BY is significant to the planner.
In-Reply-To: <12086.1513867056@sss.pgh.pa.us>
References: <CAMkU=1w9scCPMicmhzrAkUUFVnmJ5JKok5K2KD_qNvXYyEQZ+A@mail.gmail.com> <CAKJS1f-ic8+DVSt1GJCG1njkbS5ui4d4-tmmkhSt0Aq2q+B_uQ@mail.gmail.com> <12086.1513867056@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox