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 1jnUvZ-0001CF-Fn for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jun 2020 22:28:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jnUvY-0003tb-Bf for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jun 2020 22:28:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jnUsj-0000un-AL for pgsql-hackers@lists.postgresql.org; Mon, 22 Jun 2020 22:25:37 +0000 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jnUsg-0000kX-B7 for pgsql-hackers@lists.postgresql.org; Mon, 22 Jun 2020 22:25:36 +0000 Received: by mail-ej1-x644.google.com with SMTP id y10so5220140eje.1 for ; Mon, 22 Jun 2020 15:25:34 -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; bh=C/VKEpOWHVaEZvUfTCFbtCATZeMZUwtMvr0zHtUomA4=; b=AI2/WC5J7c8wVwkCSnTaRlfnVG9mcL2sCrTNRa9b+V8J57KCDbC6Lu+gLP5YC8iHRN vy//Jx3aAiJjiHYv/LQY2tLkSMugc4fEE6zTj31nlEk0k9pan6M1dV070gt7iUEAdsj1 EFocwK/W3uG0p6efNtDniY0j9pavM/feBWz60jIgT0LheTpEYrWlKCamSXDpJVs2bIOb 9YZD8PbpKSXl3Kyfu6apAKb9Qzr7dmdUs3D04VTEsyzroL6Kb9FgEvOuV5BEhfRxJn5r +XZ2uDfjtDTRETHosMd6ys8r+TL5ANtxo1TJkPT4gsNHQGhtjLmC4eX+c4JdJum25Dc7 8y6w== 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=C/VKEpOWHVaEZvUfTCFbtCATZeMZUwtMvr0zHtUomA4=; b=lXMuJMoz/O7OXX6uXSNA/y0vsIovWBaPqkvay/JezxH05mkxSPHWN0iEkj19vA189G za5/Uh5e6yRqi3T+ZzuTc6NaVjS82T3MOHnpHcdULh4ZAMt6BQWP8IOdwZVbeuKSJWLc mj0wI/ie2+bHzMjb/mZNcyqhgZwt3kKinpEy5l/M+qpWb5kF3cpS9LxLLAtqDYc7oj9k Tkb3o3vm+VQWegNtcJ0GWiiNxkudRzpvez8u9B7cWqcWkOXuBioFvDvyemG4cm7fXQG6 Gp++EEPOwgVB6mktmaHRAeGcYWP/1Qq2x3EGmsKAHYk4pwCeAK4yAR8geeN3GIOFFXBf 1g6Q== X-Gm-Message-State: AOAM533hXhiWtLaBxGVBN3zs89Hj5Cm82iZKZNYcRBFsoq4Bflz4wmFo 2j5+ai9n1sbBxBdReu9yi3AmoQ== X-Google-Smtp-Source: ABdhPJxP98E3Y4irh54XegkqFSkR8+eOod0UBlhNLQdd/Asih7H59/UvJh9VXjQerA1K0dghN8BFFA== X-Received: by 2002:a17:907:1190:: with SMTP id uz16mr3949375ejb.385.1592864733640; Mon, 22 Jun 2020 15:25:33 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id g14sm6960528ejx.77.2020.06.22.15.25.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jun 2020 15:25:32 -0700 (PDT) Date: Tue, 23 Jun 2020 00:25:30 +0200 From: Tomas Vondra To: Robert Haas Cc: Dmitry Dolgov <9erthalion6@gmail.com>, Teodor Sigaev , Gavin Flower , Andres Freund , Michael Paquier , PostgreSQL Developers Subject: Re: POC: GROUP BY optimization Message-ID: <20200622222530.qq22kdkukz7nnqmd@development> References: <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; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Jun 22, 2020 at 11:50:49AM -0400, Robert Haas wrote: >On Sat, May 16, 2020 at 10:56 AM Tomas Vondra > wrote: >> The local effects are trivial - it's for example reordering the >> pathkeys to make the explicit sort as cheap as possible. This thread >> already discussed a number of things to base this on - ndistinct for >> columns, cost of comparison function, ... In any case, this is >> something we can decide locally, when building the grouping paths. >> >> The global effects are much harder to tackle, because the decision >> can't be made locally when building the grouping paths. It requires >> changes both below and above the point where we build grouping paths. >> >> An example of a decision we need to make before we even get to >> building a grouping path is which index paths to build. Currently we >> only build index paths with "useful" pathkeys, and without tweaking >> that we'll never even see the index in add_paths_to_grouping_rel(). >> >> 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). >> >> 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 >> ... > > I agree with all of this and I think it's really good analysis. Part > of the reason why the planner isn't that sophisticated in this area is > that, for a long time, we didn't use paths at this level, and so it > was much harder to write any kind of viable patch to consider > alternatives. With Tom's planner path-ification word there should be > a lot more potential for optimization here, but, as you say, we need > to do that by leveraging the existing costing machinery, not just via > simple heuristics. Agreed. > It also strikes me that one of the problems in this area is that the > statistics we currently gather don't seem to be entirely useful or > reliable for aggregate planning. I don't think that's an immediate issue. I don't think anyone started implementing these optimizations and decided not to do that because of lack of statistics. The reason why we don't have those optimizations is more that we decided not to even consider those optimizations, possibly because of planner limitations. Of course, the optimizations may require additional stats, but that's only step #2. > I wonder if there are extensions to the extended statistics mechanism, > or even just more things we should gather during a routine ANALYZE, > that would enable us to estimate things better here. The most obvious > thing is that n_distinct is often wildly inaccurate, but it's deeper > than that. For instance, we need some way to estimate how many groups > you're going to get when you filter on a and then group by b that > doesn't assume uniform distribution. And we need also need something > that doesn't just output a number of groups, but gives you some inkling > of the sizes of those groups: 1 giant group and a bunch of little ones > isn't the same as a bunch of equally sized groups. I don't know that > these are things we really have much chance of figuring out with the > currently-available information, though. > Not sure. I think the extended stats we have now (ndistinct coeffs, multi-column MCV lists) should be good basis for these decisions, even with skewed data. Of course, we may need to add something else, but I'm not sure what would that be. The main limitation of extended stats is it's at the per-table level. Once you do a join, it's all over - we don't have that capability :-( > Sorry if this is hijacking the thread a bit; I don't mean to > discourage work on this specific patch. I'm just wondering if we need > to think a little bigger to see our way to a good solution. > I think it's fine continuing on this optimization. Either we'll be able to get it done with existing stats, or we'll find what other stats we need to sufficiently good heuristics. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services