Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VBok0-0006Vr-9a for pgsql-performance@arkaria.postgresql.org; Tue, 20 Aug 2013 16:25:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VBojz-0001LJ-OK for pgsql-performance@arkaria.postgresql.org; Tue, 20 Aug 2013 16:25:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VBojy-0001Jf-FL for pgsql-performance@postgresql.org; Tue, 20 Aug 2013 16:25:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VBojs-0001ug-0I for pgsql-performance@postgresql.org; Tue, 20 Aug 2013 16:25:06 +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 r7KGOujG007060; Tue, 20 Aug 2013 12:24:56 -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: <520E7F33.7010305@fuzzy.cz> References: <3a56c4d2c0b2daa11a625c2983823901.squirrel@sq.gransy.com> <11586.1376505314@sss.pgh.pa.us> <520E7F33.7010305@fuzzy.cz> Comments: In-reply-to Tomas Vondra message dated "Fri, 16 Aug 2013 21:36:19 +0200" Date: Tue, 20 Aug 2013 12:24:56 -0400 Message-ID: <7059.1377015896@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 managed to get the data to a different machine, and I've spent some > time on debugging it. Great, thanks for looking into it! > It seems that the difference is in evaluating hashentrysize > [ choose_hashed_distinct omits hash_agg_entry_size() ] > but the hashentrysize size is 24 (choose_hashed_distinct) or 56 > (choose_hashed_grouping). This causes that while _distinct evaluates the > condition as false, and _grouping as true (and thus returns false). Hah. > Now, the difference between 24 and 56 is caused by hash_agg_entry_size. > It's called with numAggs=0 but returns 32. I'm wondering if it should > return 0 in such cases, i.e. something like this: No, I don't think so. I'm pretty sure the reason choose_hashed_distinct is like that is that I subconsciously assumed hash_agg_entry_size would produce zero for numAggs = 0; but in fact it does not and should not, because there's still some overhead for the per-group hash entry whether or not there's any aggregates. So the right fix is that choose_hashed_distinct should add hash_agg_entry_size(0) onto its hashentrysize estimate. A separate issue is that the use of numAggs-1 in hash_agg_entry_size's calculations seems a bit risky if numAggs can be zero - I'm not sure we can rely on compilers to get that right. I'm inclined to replace that with use of offsetof. Likewise in build_hash_table. > I've tested that after this both queries use HashAggregate (which is the > right choice), but I haven't done any extensive checking so maybe I'm > missing something. It might be the preferable choice in this example, but you're looking at an edge case. If you want the thing to be using a hash aggregate for this size of problem, you should increase work_mem. 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