Re: POC: GROUP BY optimization
Andrei Lepikhov <a.lepikhov@postgrespro.ru>
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
- 0001-Explore-alternative-orderings-of-group-by-pathkeys-d-20231228.patch (text/plain) patch 0001
On 27/12/2023 12:07, Tom Lane wrote: > Andrei Lepikhov <a.lepikhov@postgrespro.ru> writes: >> To be clear. In [1], I mentioned we can perform micro-benchmarks and >> structure costs of operators. At least for fixed-length operators, it is >> relatively easy. > > I repeat what I said: this is a fool's errand. You will not get > trustworthy results even for the cases you measured, let alone > all the rest. I'd go as far as to say I would not believe your > microbenchmarks, because they would only apply for one platform, > compiler, backend build, phase of the moon, etc. Thanks for the explanation. I removed all cost-related codes. It still needs to be finished; I will smooth the code further and rewrite regression tests - many of them without cost-dependent reorderings look silly. Also, remember Alexander's remarks, which must be implemented, too. But already here, it works well. Look: Preliminaries: CREATE TABLE t(x int, y int, z text, w int); INSERT INTO t SELECT gs%100,gs%100, 'abc' || gs%10, gs FROM generate_series(1,10000) AS gs; CREATE INDEX abc ON t(x,y); ANALYZE t; SET enable_hashagg = 'off'; This patch eliminates unneeded Sort operation: explain SELECT x,y FROM t GROUP BY (x,y); explain SELECT x,y FROM t GROUP BY (y,x); Engages incremental sort: explain SELECT x,y FROM t GROUP BY (x,y,z,w); explain SELECT x,y FROM t GROUP BY (z,y,w,x); explain SELECT x,y FROM t GROUP BY (w,z,x,y); explain SELECT x,y FROM t GROUP BY (w,x,z,y); Works with subqueries: explain SELECT x,y FROM (SELECT * FROM t ORDER BY x,y,w,z) AS q1 GROUP BY (w,x,z,y); explain SELECT x,y FROM (SELECT * FROM t ORDER BY x,y,w,z LIMIT 100) AS q1 GROUP BY (w,x,z,y); But arrangement with an ORDER BY clause doesn't work: DROP INDEX abc; explain SELECT x,w,z FROM t GROUP BY (w,x,z) ORDER BY (x,z,w); I think the reason is that the sort_pathkeys and group_pathkeys are physically different structures, and we can't just compare pointers here. -- regards, Andrei Lepikhov Postgres Professional