Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eS1yh-0006r9-6L for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Dec 2017 14:37:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eS1yf-0003sP-A4 for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Dec 2017 14:37:41 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eS1yf-0003sF-1T for pgsql-bugs@lists.postgresql.org; Thu, 21 Dec 2017 14:37:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eS1yc-0008BY-MT for pgsql-bugs@postgresql.org; Thu, 21 Dec 2017 14:37:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id vBLEbap9012087; Thu, 21 Dec 2017 09:37:36 -0500 From: Tom Lane To: David Rowley cc: Jeff Janes , Pg Bugs Subject: Re: Order of columns in GROUP BY is significant to the planner. In-reply-to: References: Comments: In-reply-to David Rowley message dated "Thu, 21 Dec 2017 23:13:24 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <12085.1513867056.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 21 Dec 2017 09:37:36 -0500 Message-ID: <12086.1513867056@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Rowley writes: > On 21 December 2017 at 19:16, Jeff Janes 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 tha= t 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