Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9fup-0002bA-5c for pgsql-performance@arkaria.postgresql.org; Wed, 14 Aug 2013 18:35:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V9fuo-0008PU-M9 for pgsql-performance@arkaria.postgresql.org; Wed, 14 Aug 2013 18:35:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9fun-0008PO-UV for pgsql-performance@postgresql.org; Wed, 14 Aug 2013 18:35:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V9fuk-0001HE-GO for pgsql-performance@postgresql.org; Wed, 14 Aug 2013 18:35:25 +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 r7EIZEEd011587; Wed, 14 Aug 2013 14:35:14 -0400 From: Tom Lane To: "Tomas Vondra" cc: pgsql-performance@postgresql.org Subject: Re: queries with DISTINCT / GROUP BY giving different plans In-reply-to: <3a56c4d2c0b2daa11a625c2983823901.squirrel@sq.gransy.com> References: <3a56c4d2c0b2daa11a625c2983823901.squirrel@sq.gransy.com> Comments: In-reply-to "Tomas Vondra" message dated "Wed, 14 Aug 2013 17:33:53 +0200" Date: Wed, 14 Aug 2013 14:35:14 -0400 Message-ID: <11586.1376505314@sss.pgh.pa.us> X-Pg-Spam-Score: -4.7 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org "Tomas Vondra" writes: > I've run into a strange plan difference on 9.1.9 - the first query does > "DISTINCT" by doing a GROUP BY on the columns (both INT). ... > Now, this takes ~45 seconds to execute, but after rewriting the query to > use the regular DISTINCT it suddenly switches to HashAggregate with ~1/3 > the cost (although it produces the same output, AFAIK), and it executes in > ~15 seconds. [ scratches head... ] I guess you're running into some corner case where choose_hashed_grouping and choose_hashed_distinct make different choices. It's going to be tough to debug without a test case though. I couldn't reproduce the behavior in a few tries here. > BTW I can't test this on 9.2 or 9.3 easily, as this is our production > environment and I can't just export the data. I've tried to simulate this > but so far no luck. I suppose they won't yet you step through those two functions with a debugger either ... regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance