Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jmerT-0005wi-Bz for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Jun 2020 14:52:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jmerS-00072d-AO for pgsql-hackers@arkaria.postgresql.org; Sat, 20 Jun 2020 14:52:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1jmerA-0000c7-5S for pgsql-hackers@lists.postgresql.org; Sat, 20 Jun 2020 14:52:32 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1jmeTG-0007zu-9j for pgsql-hackers@lists.postgresql.org; Sat, 20 Jun 2020 14:27:51 +0000 Received: by mail-ed1-x533.google.com with SMTP id p18so10041171eds.7 for ; Sat, 20 Jun 2020 07:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=IgLtPdWtyslhDohLZ82neoOiGILpOR2pjE8LloAznPY=; b=ljjuPjEzYsINtMk40tF2FiF+qpyvS3YCXtPAmnhUoARhW2Q1MFMdLQ2kSJ1rq7Ewwn BhNFg7huzBISMDQPaZjAY2uRwYwJXq4pJhpOdJpRDX2RKQe08xj2/0BhqiR8JY30U6Jb RFvECju+g/90EEYP5xCnLCB+RN3RY91tlmcYHg1UiUXQOKMMeZ+FoGphj0or1IThYOBD wc+qmdbpNWK7oDi+ti7ambaH8+TApM2ls1sBTRPTWW9pgYu1WQO5pv0TKa1wHEiBbgxb h6X5IZ3Bory5e00yaJarfW02/fgJ/QgN2XXIQH9cF7syt8yVOguAvQLZMvlbCkVF7qVu 2n8w== 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; bh=IgLtPdWtyslhDohLZ82neoOiGILpOR2pjE8LloAznPY=; b=j/+gowHroWqR1k3MWEgj/k8SDK2xZ4rzD8aZpOWW91bbkRaI7jpBq5PyrQbcWFCvEY MQ6Z+aIa8oSqfFFr+Ecjhe1fmYpFdd8LCzRrevE1x59yK7A45rZ2x1ry/Z6KzDrfo02O eM8UMq1Tsmp50HR55BIYaGOsBs44Ar4f6wDbEy83Aph2K1sS/oBIa0zShQefUJFeyohJ vacEtHc6OphjYacT6eln/tg1xzEbwdaoAoFJLMHYp0t8dWJ8x9ywf5VbAUQMXA20KtFe IMrJ4Pfqe/hEgup38QIQcuvYeVpS1uYFTl7o+JsMngkBZjaDCiEvtTuz/HpGe3D6GKLh F0nA== X-Gm-Message-State: AOAM5318O2orPfAZeELfCa/p1GTPstn3LGZmJkxKh7P0DUI9Dv62NfGw /yk/L/yXsVG2rvOyNATJfHI= X-Google-Smtp-Source: ABdhPJzLZBTc8mkoAf8+o/IjlRSPfn8rVewVZtl1NwbAqEp50FD6QFFyRNLRABK/fvo164yaAA2kIg== X-Received: by 2002:a05:6402:228d:: with SMTP id cw13mr8723695edb.150.1592663268933; Sat, 20 Jun 2020 07:27:48 -0700 (PDT) Received: from localhost (dslb-188-102-096-018.188.102.pools.vodafone-ip.de. [188.102.96.18]) by smtp.gmail.com with ESMTPSA id v2sm7578913eju.49.2020.06.20.07.27.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Jun 2020 07:27:48 -0700 (PDT) Date: Sat, 20 Jun 2020 16:30:10 +0200 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Tomas Vondra Cc: Teodor Sigaev , Gavin Flower , Andres Freund , Michael Paquier , PostgreSQL Developers Subject: Re: POC: GROUP BY optimization Message-ID: <20200620143010.z3af7wa4v22h4lu4@localhost> References: <20190131112407.zpbysdvghjyh4z47@alap3.anarazel.de> <20190409152100.5q25whnxs27zws5m@development> <20190503215510.bcr5ycszntqg65tw@development> <20190524225725.embuha33qvc5avz3@development> <20200514235220.xewrrwjvatxzn3g6@development> <20200516122431.b7wtpm7dspsaxfro@localhost> <20200516145609.vm7nrqy7frj4ha6r@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200516145609.vm7nrqy7frj4ha6r@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Sat, May 16, 2020 at 04:56:09PM +0200, Tomas Vondra wrote: > > So I don't think there will be a single "interesting" grouping pathkeys > (i.e. root->group_pathkeys), but a collection of pathkeys. And we'll > need to build grouping paths for all of those, and leave the planner to > eventually pick the one giving us the cheapest plan ... > > A "brute-force" approach would be to generate all possible orderings of > group_pathkeys, but that's probably not going to fly - it might easily > cause an explosion in number of paths we track, etc. So we'll need to > pick/prioritize orderings that are more likely to give us efficient > plans, and ORDER BY seems like a good example because it means we won't > need an explicit sort. Yes, I see. I've already rebased the original version of patch and now working on your suggestions. In the meantime one question: > But there are also decisions that can be made only after we build the > grouping paths. For example, we may have both GROUP BY and ORDER BY, and > there is no "always correct" way to combine those. In some cases it may > be correct to use the same pathkeys, in other cases it's better to use > different ones (which will require an extra Sort, with additional cost). Do I understand correctly, your idea is that in some cases it's cheaper to use different order for GROUP BY than in ORDER BY even with an extra sorting? In the current patch implementation there is an assumption that says it's always better to match the order of pathkeys for both GROUP BY/ORDER BY (which means that the only degree of freedom there is to reorder the tail, which in turn makes both "unsorted input" and "partially sorted input" cases from your original email essentially the same). Can you show such an example when this assumption is invalid? > I've only quickly skimmed the old thread, but IIRC there were two main > challenges in getting the optimization right: > > 1) deciding which orderings are interesting / worth additional work > > I think we need to consider these orderings, in addition to the one > specified in GROUP BY: Yes, looks like the current patch implementation together with preprocess_groupclause already implements this, although maybe not that flexible. > 2) costing the alternative orderings > > I think we've already discussed various ways to leverage as much > available info as possible (extended stats, MCVs, ...) but I think the > patch only does some of it. Right, there were couple of ideas what to do in case of a few groups which are too big they can invalidate current assumptions about costs. E.g. do not apply reordering if we detected such situation, or use "conservative" approach and take the biggest group for estimations.