Re: POC: GROUP BY optimization
Robert Haas <robertmhaas@gmail.com>
From: Robert Haas <robertmhaas@gmail.com>
To: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: Dmitry Dolgov <9erthalion6@gmail.com>, Teodor Sigaev <teodor@sigaev.ru>, Gavin Flower <GavinFlower@archidevsys.co.nz>,
Andres Freund <andres@anarazel.de>, Michael Paquier <michael@paquier.xyz>, PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Date: 2020-06-22T15:50:49Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Restore preprocess_groupclause()
- 505c008ca37c 17.0 landed
-
Rename PathKeyInfo to GroupByOrdering
- 0c1af2c35c7b 17.0 landed
-
Add invariants check to get_useful_group_keys_orderings()
- 91143c03d4ca 17.0 landed
-
Fix asymmetry in setting EquivalenceClass.ec_sortref
- 199012a3d844 17.0 landed
-
Multiple revisions to the GROUP BY reordering tests
- 874d817baa16 17.0 landed
-
Get rid of pg_class usage in SJE regression tests
- e1b7fde418f2 17.0 landed
-
Rename index "abc" in aggregates.sql
- b91f91870828 17.0 landed
-
Explore alternative orderings of group-by pathkeys during optimization.
- 0452b461bc40 17.0 landed
-
Generalize the common code of adding sort before processing of grouping
- 7ab80ac1caf9 17.0 landed
-
Fix out-dated comment in preprocess_groupclause()
- f6c70b81802a 15.0 landed
- 78a9af1a2764 16.0 landed
-
Force parallelism in partition_aggregate
- 2fe6b2a806f2 16.0 landed
- 01474f56981a 15.0 landed
-
Optimize order of GROUP BY keys
- db0d67db2401 15.0 landed
On Sat, May 16, 2020 at 10:56 AM Tomas Vondra <tomas.vondra@2ndquadrant.com> 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. 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 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. 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. -- Robert Haas EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company