Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbaKk-00085D-8J for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jun 2019 00:44:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hbaKj-0002tv-5U for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jun 2019 00:44:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbaKi-0002to-Po for pgsql-hackers@lists.postgresql.org; Fri, 14 Jun 2019 00:44:44 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbaKg-0002Gu-6y for pgsql-hackers@postgresql.org; Fri, 14 Jun 2019 00:44:43 +0000 Received: by mail-wm1-x341.google.com with SMTP id u8so553352wmm.1 for ; Thu, 13 Jun 2019 17:44:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=DL1mdD2fLV2+VqtrpuhFmNC6ODtEvZmk6pSslqMsoDY=; b=C6bZkAGL6KA7LEAwtwV8je2tBurNd+X4LQBdc4uFISWES015D/cPc8hgt9rG1o7erJ /4VVZ268JRVGbEqP3FgrkfoWXi7/KRwdTuestWaG1KrLHySf4k4Dl1ITFR70lRj+YqX+ BmRGVanPYO9HIMQiRNEVu/y6sHa4gAnyxbaGmhCpw9oe/pZDnpsk4KgmidnYxMCmpLpI c4poSnpzILfpRkc0q+CLUPFkSo2SZPOOM/pdIf1ITTh4OHeH9+515vo802Eey7aHnFx9 GU1ZcUM56Hw6Ty+LK0hECH+7o0LuffKQgW1y6Cgtxd+43Z8n3HLsH67eerSy6MwcL5iR bXJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=DL1mdD2fLV2+VqtrpuhFmNC6ODtEvZmk6pSslqMsoDY=; b=eGqhZ5UdFokDawzccTr+OZxPfBOgB6LidT03AD+YnTw0p6SGjhUW5YNN1LWKXgjoyc AdJNW1AmEWTKGQcKHmZx7S0p6RTbzlkKMNIOm1NsmJL0+SFKG7iYSd95GpyL8shBfUWc AFQQi9ArobtXJ7DhJpXWoCo3Iz4ROAue+C7d//R5l1qssjjLViuZPNWTCdGwaN1NuAPv 6Px+gJ2ez8xT/jDnL4rdgWzy84OcCWzkHDF1Q6WJuxMzGQgSd/NWNwE5EqJo/tYqwd4K CmMVQcFgnRKNTfo2LI9pziF9TWp23DncpdMZCXzYtlAetoTSiauPdzSBTH+RYJOflfIC 49qQ== X-Gm-Message-State: APjAAAVAFeOmixN+XaQGHaCivyjTdNBesWGCzH2uZa6tu2W2CbdWsaM4 rxHxfdBeXbN6qJ00NvpG/weTfA== X-Google-Smtp-Source: APXvYqwk9//U/fh5QD6Sccks+s/mOxzKOzBFJPTZZ7WKQPMFOBBdE7w0Cj+gnY5EJ3WFEFuiOl4pkw== X-Received: by 2002:a1c:65c3:: with SMTP id z186mr5900068wmb.116.1560473080753; Thu, 13 Jun 2019 17:44:40 -0700 (PDT) Received: from localhost (ip-86-49-243-43.net.upcbroadband.cz. [86.49.243.43]) by smtp.gmail.com with ESMTPSA id o11sm914571wmh.37.2019.06.13.17.44.39 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 13 Jun 2019 17:44:40 -0700 (PDT) Date: Fri, 14 Jun 2019 02:44:38 +0200 From: Tomas Vondra To: David Rowley Cc: Richard Guo , PostgreSQL-development Subject: Re: Parallel grouping sets Message-ID: <20190614004438.e2y7ubdnpobq5t37@development> References: <20190613234518.a3votby32snkwiju@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716-1444-295967 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jun 14, 2019 at 12:02:52PM +1200, David Rowley wrote: >On Fri, 14 Jun 2019 at 11:45, Tomas Vondra wrote: >> >> On Wed, Jun 12, 2019 at 10:58:44AM +0800, Richard Guo wrote: > >> ># explain (costs off, verbose) select c1, c2, avg(c3) from t2 group by >> >grouping sets((c1,c2), (c1)); >> > QUERY PLAN >> >-------------------------------------------------------------- >> > Finalize GroupAggregate >> > Output: c1, c2, avg(c3), (gset_id) >> > Group Key: t2.c1, t2.c2, (gset_id) >> > -> Gather Merge >> > Output: c1, c2, (gset_id), (PARTIAL avg(c3)) >> > Workers Planned: 2 >> > -> Sort >> > Output: c1, c2, (gset_id), (PARTIAL avg(c3)) >> > Sort Key: t2.c1, t2.c2, (gset_id) >> > -> Partial HashAggregate >> > Output: c1, c2, gset_id, PARTIAL avg(c3) >> > Hash Key: t2.c1, t2.c2 >> > Hash Key: t2.c1 >> > -> Parallel Seq Scan on public.t2 >> > Output: c1, c2, c3 >> >(15 rows) >> > >> >> OK, I'm not sure I understand the point of this - can you give an >> example which is supposed to benefit from this? Where does the speedup >> came from? > >I think this is a bad example since the first grouping set is a >superset of the 2nd. If those were independent and each grouping set >produced a reasonable number of groups then it may be better to do it >this way instead of grouping by all exprs in all grouping sets in the >first phase, as is done by #1. To do #2 would require that we tag >the aggregate state with the grouping set that belong to, which seem >to be what gset_id is in Richard's output. > Aha! So if we have grouping sets (a,b) and (c,d), then with the first approach we'd do partial aggregate on (a,b,c,d) - which may produce quite a few distinct groups, making it inefficient. But with the second approach, we'd do just (a,b) and (c,d) and mark the rows with gset_id. Neat! >In my example upthread the first phase of aggregation produced a group >per input row. Method #2 would work better for that case since it >would only produce 2000 groups instead of 1 million. > >Likely both methods would be good to consider, but since #1 seems much >easier than #2, then to me it seems to make sense to start there. > Yep. Thanks for the explanation. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services