Re: POC: GROUP BY optimization
Tomas Vondra <tomas.vondra@enterprisedb.com>
Commits
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
Attachments
- plans-14.log (text/x-log)
- plans-15.log (text/x-log)
On 8/2/22 13:14, David Rowley wrote: > On Tue, 2 Aug 2022 at 22:21, Michael Paquier <michael@paquier.xyz> wrote: >> >> On Fri, Jul 15, 2022 at 09:46:51PM +0200, Tomas Vondra wrote: >>> I agree this is a mistake in db0d67db2 that makes the test useless. >> >> Tomas, please note that this is an open item assigned to you. Are you >> planning to do something with these regression tests by beta3? > > There's still something to do there for PG15, but 1349d2790 (just gone > in to master) made those two tests run as a parallel query again. > Hi, I've been looking at this, and as I speculated before it's due to the sort costing changing a little bit. On PG14, the costing of the plans looks like this: Gather (cost=1869.39..2823.15 rows=8 width=52) and with disabled parallelism, it's like this: Append (cost=998.04..3000.64 rows=10 width=52) so there's a (fairly small) diffrence in favor of the parallel plan. But on PG15 it's the other way around - the selected non-parallel one is costed like this: Append (cost=779.41..2490.61 rows=10 width=52) and by setting parallel_setup_cost=0 you get this: Gather (cost=700.34..1531.76 rows=8 width=52) So with the setup cost included it's ~2531, and it loses to the simple plan. This happens because the patch changed sort costing - the same sort on PG14 and PG15 looks like this: PG14: -> Sort (cost=998.04..1028.04 rows=12000 width=13) PG15: -> Sort (cost=779.41..809.41 rows=12000 width=13) As mentioned, the commit tweaked sort costing - before it was pretty much just comparison_cost * tuples * LOG2(tuples) but the patch needs to cost different pathkey orderings, and consider that maybe we don't need to compare all the keys (which the original costing kind assumes). That's the whole point of this optimization. The costing (compute_cpu_sort_cost) considers a couple other things, like cost of the comparator function for the data type, width of the values, groupings determined by preceding keys, and so on. It might seem strange that a query with a single pathkey changes, but that single pathkey is costed the same way, of course. In principle we might have a special costing for this case, but I'd bet that would result in pretty surprising inconsistencies when adding a sort key (going from 1 to 2 keys). So I don't think the current costing is wrong, but it certainly is more complex. But the test does not test what it intended - I have two ideas how to make it work: 1) increase the number of rows in the table 2) increase cpu_operator_cost (for that one test?) 3) tweak the costing somehow, to increase the cost a bit Both (1) and (2) work - I've tried doubling the number of rows or setting the operator cost to 0.005, and that does the trick, but maybe a smaller change would be enough. I don't like (3) very much - changing the costing just to get the same test behavior as on older release does not seem very principled. Yes, maybe it should be tweaked, but not because of a regression test. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company