Re: Eager aggregation, take 3
Robert Haas <robertmhaas@gmail.com>
Sorry for the slow response. On Fri, Jun 13, 2025 at 3:42 AM Richard Guo <guofenglinux@gmail.com> wrote: > The transformation of eager aggregation is: > > GROUP BY G, AGG(A) on (R1 JOIN R2 ON J) > = > GROUP BY G, AGG(agg_A) on ((GROUP BY G1, AGG(A) AS agg_A on R1) > JOIN R2 ON J) > > This equivalence holds under the following conditions: > > 1) AGG is decomposable, meaning that it can be computed in two stages: > a partial aggregation followed by a final aggregation; > 2) The set G1 used in the pre-aggregation of R1 includes: > * all columns from R1 that are part of the grouping keys G, and > * all columns from R1 that appear in the join condition J. > 3) The grouping operator for any column in G1 must be compatible with > the operator used for that column in the join condition J. This proof seems to ignore join-order constraints. I'm not sure to what degree that influences the ultimate outcome here, but given A LEFT JOIN (B INNER JOIN C), we cannot simply decide that A and C comprise R1 and B comprises R2, because it is not actually possible to do the A-C join first and treat the result as a relation to be joined to B. That said, I do very much like the explicit enumeration of criteria that must be met for the optimization to be valid. That makes it a lot easier to evaluate whether the theory of the patch is correct. > To address these concerns, I'm thinking that maybe we can adopt a > strategy where partial aggregation is only pushed to the lowest > possible level in the join tree that is deemed useful. In other > words, if we can build a grouped path like "AGG(B) JOIN A" -- and > AGG(B) yields a significant reduction in row count -- we skip > exploring alternatives like "AGG(A JOIN B)". I really like this idea. I believe we need some heuristic here and this seems like a reasonable one. I think there could be a better one, potentially. For instance, it would be reasonable (in my opinion) to do some kind of evaluation of AGG(A JOIN B) vs. AGG(B) JOIN A that does not involve performing full path generation for both cases; e.g. one could try to decide considering only row counts, for instance. However, I'm not saying that would work better than your proposal here, or that it should be a requirement for this to be committed; it's just an idea. IMHO, the requirement to have something committable is that there is SOME heuristic limiting the search space and at the same time the patch can still be demonstrated to give SOME benefit. I think what you propose here meets those criteria. I also like the fact that it's simple and easy to understand. If it does go wrong, it will not be too difficult for someone to understand why it has gone wrong, which is very desirable. > I think this heuristic serves as a good starting point, and we can > look into extending it with more advanced strategies as the feature > evolves. So IOW, +1 to what you say here. -- Robert Haas EDB: http://www.enterprisedb.com
Commits
-
Fix eager aggregation for semi/antijoin inner rels
- ffeda04259bb 19 (unreleased) landed
-
Cover additional errors and corner conditions in repack.c
- 2670cc298f42 19 (unreleased) cited
-
Fix volatile function evaluation in eager aggregation
- 3a08a2a8b4fd 19 (unreleased) landed
-
Fix collation handling for grouping keys in eager aggregation
- bd94845e8c90 19 (unreleased) landed
-
Rename apply_at to apply_agg_at for clarity
- 1206df04c200 19 (unreleased) landed
-
Fix comment in eager_aggregate.sql
- 36fd8bde1b77 19 (unreleased) landed
-
Remove unnecessary include of "utils/fmgroids.h"
- f997d777adf7 19 (unreleased) landed
-
Implement Eager Aggregation
- 8e11859102f9 19 (unreleased) landed
-
Allow negative aggtransspace to indicate unbounded state size
- 185e30426334 19 (unreleased) landed
-
Add macros for looping through a List without a ListCell.
- 14dd0f27d7cd 17.0 cited
-
Account for the effect of lossy pages when costing bitmap scans.
- 5edc63bda68a 11.0 cited
-
Fix a thinko in join_is_legal: when we decide we can implement a semijoin
- a43b190e3c71 9.0.0 cited