From b7642798432d971c3e5f224a6ac5673c32c888d3 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Thu, 19 Nov 2026 14:59:20 +0900 Subject: [PATCH v6 7/8] Detect unique inner sides of joins for partially aggregated relations Eager aggregation partially aggregates a relation before it is joined to the rest of the query. The grouped relation then emits one row for each group, so no two of its rows have the same values in all its grouping expressions. But the check whether the inner side of a join is unique made no use of that. This commit teaches rel_is_distinct_for that a grouped relation is distinct for join clauses that cover all its grouping expressions. No UniqueKeys are needed for that. This holds only for a grouped relation whose paths apply the partial aggregation on top of the ungrouped relation. The paths of a grouped join relation can instead join a grouped relation to an ungrouped one, and then a group is emitted once for each matching row of the latter. A grouped join relation can still be proven distinct in the way that any join relation is, by asking about the ungrouped join relation. Each row of the grouped relation stands for a separate set of rows of the ungrouped one, and the join clauses can only refer to grouping expressions, which have the same values throughout a set. Two more things need care. First, the partial paths of a grouped relation emit each group once per worker. A Parallel Hash join builds its hash table from the rows of all workers, so it must not treat a grouped inner relation as unique. Second, a grouped relation is created as a copy of the ungrouped one and so shared its lists of the relations it has been proven unique for. What is proven for the grouped relation need not hold for the ungrouped one, so the copy now starts with empty lists. --- .../postgres_fdw/expected/postgres_fdw.out | 3 +- src/backend/optimizer/README | 12 +- src/backend/optimizer/path/joinpath.c | 12 +- src/backend/optimizer/path/uniquekeys.c | 104 +++++++++++++++++- src/backend/optimizer/plan/analyzejoins.c | 8 ++ src/backend/optimizer/util/relnode.c | 4 + src/include/optimizer/paths.h | 3 + src/test/regress/expected/eager_aggregate.out | 77 ++++++++++--- src/test/regress/expected/join.out | 8 +- src/test/regress/expected/uniquekeys.out | 58 ++++++++++ src/test/regress/sql/uniquekeys.sql | 22 ++++ 11 files changed, 280 insertions(+), 31 deletions(-) diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index dc8922885ea..241bed17194 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -4462,6 +4462,7 @@ select count(*), x.b from ft1, (select c2 a, sum(c1) b from ft1 group by c2) x w Sort Key: (sum(ft1_1.c1)) -> Hash Join Output: (sum(ft1_1.c1)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (ft1_1.c2 = ft1.c2) -> Foreign Scan Output: ft1_1.c2, (sum(ft1_1.c1)) @@ -4475,7 +4476,7 @@ select count(*), x.b from ft1, (select c2 a, sum(c1) b from ft1 group by c2) x w -> Foreign Scan on public.ft1 Output: ft1.c2 Remote SQL: SELECT c2 FROM "S 1"."T 1" -(24 rows) +(25 rows) select count(*), x.b from ft1, (select c2 a, sum(c1) b from ft1 group by c2) x where ft1.c2 = x.a group by x.b order by 1, 2; count | b diff --git a/src/backend/optimizer/README b/src/backend/optimizer/README index b8144bbba44..ac3b1743aec 100644 --- a/src/backend/optimizer/README +++ b/src/backend/optimizer/README @@ -1048,8 +1048,8 @@ so that a query that never needs them does not pay for them. UniqueKeys are used to detect unique inner sides of joins when the inner side is itself a join, and to skip DISTINCT and GROUP BY steps whose input is already distinct. The following describes each of these in turn, and -then how outer joins, semijoins and appendrel children fit in. For now -only plain tables get UniqueKeys. +then how outer joins, semijoins and other kinds of relations fit in. For +now only plain tables get UniqueKeys. We do not keep UniqueKeys for join relations. A join relation would typically have many of them, most of which would never be used. And since @@ -1192,6 +1192,14 @@ child members. So a child join is distinct if its parent is, and we just ask about the parent. That misses, for child joins, what could be proven from indexes that only individual partitions have. +A relation that is partially aggregated by eager aggregation has no +UniqueKeys either, but it emits one row for each group, so it is distinct +for join clauses that cover all its grouping expressions. That applies +only where the aggregation is done on top of the ungrouped relation. A +grouped join relation is otherwise distinct if the ungrouped one is. None +of this holds across workers, so a Parallel Hash join must not treat a +grouped inner relation as unique. + Order of processing for EquivalenceClasses and PathKeys ------------------------------------------------------- diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c index beb2856dd21..4d7dbc4f257 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -2507,12 +2507,22 @@ hash_inner_and_outer(PlannerInfo *root, if (innerrel->partial_pathlist != NIL && enable_parallel_hash) { + JoinPathExtraData parallel_extra = *extra; + + /* + * A grouped relation's partial paths emit each group once per + * worker, so it is not unique across workers. + */ + if (IS_GROUPED_REL(innerrel)) + parallel_extra.inner_unique = false; + cheapest_partial_inner = (Path *) linitial(innerrel->partial_pathlist); try_partial_hashjoin_path(root, joinrel, cheapest_partial_outer, cheapest_partial_inner, - hashclauses, jointype, extra, + hashclauses, jointype, + ¶llel_extra, true /* parallel_hash */ ); } diff --git a/src/backend/optimizer/path/uniquekeys.c b/src/backend/optimizer/path/uniquekeys.c index bfdb42747c0..08ab6d7e7de 100644 --- a/src/backend/optimizer/path/uniquekeys.c +++ b/src/backend/optimizer/path/uniquekeys.c @@ -7,8 +7,9 @@ * relation agree on. Plain relations get them from their unique indexes. * Join relations have no UniqueKeys of their own: whether a join relation is * distinct is worked out when needed, from the UniqueKeys of the base - * relations it consists of. Appendrel children have none either; see - * joinrel_is_distinct_for(). + * relations it consists of. Appendrel children and relations that are + * partially aggregated by eager aggregation have none either; see + * joinrel_is_distinct_for() and grouped_rel_is_distinct_for() for those. * * There are two kinds of question. Mergejoinable join clauses are assumed * to be strict, so when matching them we need not care about rows that have @@ -40,6 +41,7 @@ #include "optimizer/optimizer.h" #include "optimizer/pathnode.h" #include "optimizer/paths.h" +#include "optimizer/tlist.h" #include "rewrite/rewriteManip.h" #include "utils/lsyscache.h" @@ -93,10 +95,6 @@ joinrel_supports_distinctness(PlannerInfo *root, RelOptInfo *joinrel) if (IS_OTHER_REL(joinrel)) joinrel = joinrel->top_parent; - /* A grouped join rel's rows are not combinations of base rel rows */ - if (IS_GROUPED_REL(joinrel)) - return false; - build_base_rel_uniquekeys(root); /* If every base rel in the join has a UniqueKey, we can go ahead */ @@ -122,6 +120,11 @@ joinrel_supports_distinctness(PlannerInfo *root, RelOptInfo *joinrel) * its outer_is_left flag, and an expression not involving this relation on * the other. Return true if no two rows emitted by the join relation could * possibly join to the same row of the other relation. + * + * For a grouped join relation we answer for the ungrouped one. That is safe: + * each grouped row stands for a separate set of ungrouped rows, and the + * clauses refer only to grouping expressions, which are the same throughout + * a set. It does not hold for partial paths taken across workers. */ bool joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, @@ -195,6 +198,95 @@ rel_is_distinct_for_pathkeys(PlannerInfo *root, RelOptInfo *rel, return rel_has_uniquekey_in(root, rel, known_ecs, NULL, true); } +/* + * grouped_rel_supports_distinctness + * Could the grouped relation possibly be proven distinct? + * + * This is a pre-checking function for grouped_rel_is_distinct_for(). + */ +bool +grouped_rel_supports_distinctness(RelOptInfo *rel) +{ + Assert(IS_GROUPED_REL(rel)); + + /* + * The partial aggregation must be on top of the ungrouped relation. A + * grouped relation joined to an ungrouped one can emit a group + * repeatedly. + */ + return bms_equal(rel->agg_info->apply_agg_at, rel->relids); +} + +/* + * grouped_rel_is_distinct_for + * Does the grouped relation return only distinct rows according to + * clause_list? + * + * clause_list is as for joinrel_is_distinct_for(). + * + * Partial aggregation emits one row for each group, so the relation is + * distinct if the clauses cover all its grouping expressions. This does not + * hold for partial paths taken across workers. + */ +bool +grouped_rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, + List *clause_list) +{ + RelAggInfo *agg_info = rel->agg_info; + RelOptInfo *top_grouped_rel; + List *tlist; + List *pathkeys; + + if (!grouped_rel_supports_distinctness(rel)) + return false; + + /* + * Find the grouping expressions' ECs through pathkeys. For a child + * relation use its parent's expressions, as generate_grouped_paths() + * does. + */ + if (!grouping_is_sortable(agg_info->group_clauses)) + return false; + + top_grouped_rel = IS_OTHER_REL(rel) ? rel->top_parent->grouped_rel : rel; + if (top_grouped_rel == NULL) + return false; + + /* This leaves out expressions that are known equal to constants */ + tlist = make_tlist_from_pathtarget(top_grouped_rel->agg_info->target); + pathkeys = make_pathkeys_for_sortclauses(root, agg_info->group_clauses, + tlist); + + foreach_node(PathKey, pathkey, pathkeys) + { + bool found = false; + + foreach_node(RestrictInfo, rinfo, clause_list) + { + EquivalenceClass *ec; + + /* caller identified the side belonging to this relation for us */ + ec = rinfo->outer_is_left ? rinfo->right_ec : rinfo->left_ec; + if (ec == NULL) + continue; + + /* the EC might be non-canonical, so chase up to the top */ + while (ec->ec_merged) + ec = ec->ec_merged; + + if (ec == pathkey->pk_eclass && !ec->ec_broken) + { + found = true; + break; + } + } + if (!found) + return false; + } + + return true; +} + /* * joinrel_is_distinct_for_ecs * Does the join relation return only distinct rows according to the diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c index 70e9416ef8b..a24f88f3b0d 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -602,6 +602,9 @@ reduce_semijoin_in_jointree(Node *jtnode, Relids syn_righthand) static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel) { + /* A grouped relation can be distinct on its grouping expressions */ + if (IS_GROUPED_REL(rel) && grouped_rel_supports_distinctness(rel)) + return true; /* For a join relation, the proof has to come from UniqueKeys */ if (IS_JOIN_REL(rel)) return joinrel_supports_distinctness(root, rel); @@ -666,6 +669,11 @@ static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, List **extra_clauses) { + /* A grouped relation can be distinct on its grouping expressions */ + if (IS_GROUPED_REL(rel) && + grouped_rel_is_distinct_for(root, rel, clause_list)) + return true; + /* * For a join relation, see if UniqueKeys can prove it. We can't supply * extra_clauses that way, but only self-join checks ask for those, and diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index 04da325bf48..c2862360e04 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -546,6 +546,10 @@ build_grouped_rel(PlannerInfo *root, RelOptInfo *rel) */ grouped_rel->rows = 0; + /* what is proven for the grouped rel need not hold for the ungrouped */ + grouped_rel->unique_for_rels = NIL; + grouped_rel->non_unique_for_rels = NIL; + return grouped_rel; } diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h index a62c17bf538..53dfd8c9b18 100644 --- a/src/include/optimizer/paths.h +++ b/src/include/optimizer/paths.h @@ -221,6 +221,9 @@ extern bool joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, List *clause_list); extern bool rel_is_distinct_for_pathkeys(PlannerInfo *root, RelOptInfo *rel, List *pathkeys); +extern bool grouped_rel_supports_distinctness(RelOptInfo *rel); +extern bool grouped_rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, + List *clause_list); /* * pathkeys.c diff --git a/src/test/regress/expected/eager_aggregate.out b/src/test/regress/expected/eager_aggregate.out index bdda7da68f0..def4eeb0a52 100644 --- a/src/test/regress/expected/eager_aggregate.out +++ b/src/test/regress/expected/eager_aggregate.out @@ -31,6 +31,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t1.a -> Hash Join Output: t1.a, (PARTIAL avg(t2.c)) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -41,7 +42,7 @@ GROUP BY t1.a ORDER BY t1.a; Group Key: t2.b -> Seq Scan on public.eager_agg_t2 t2 Output: t2.a, t2.b, t2.c -(18 rows) +(19 rows) SELECT t1.a, avg(t2.c) FROM eager_agg_t1 t1 @@ -77,6 +78,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t1.a -> Hash Join Output: t1.a, (PARTIAL avg(t2.c)) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -90,7 +92,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t2.b -> Seq Scan on public.eager_agg_t2 t2 Output: t2.c, t2.b -(21 rows) +(22 rows) SELECT t1.a, avg(t2.c) FROM eager_agg_t1 t1 @@ -131,6 +133,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t1.a -> Hash Join Output: t1.a, (PARTIAL avg((t2.c + t3.c))) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -148,7 +151,7 @@ GROUP BY t1.a ORDER BY t1.a; Output: t2.c, t2.b, t2.a -> Seq Scan on public.eager_agg_t2 t2 Output: t2.c, t2.b, t2.a -(25 rows) +(26 rows) SELECT t1.a, avg(t2.c + t3.c) FROM eager_agg_t1 t1 @@ -186,6 +189,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t1.a -> Hash Join Output: t1.a, (PARTIAL avg((t2.c + t3.c))) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -206,7 +210,7 @@ GROUP BY t1.a ORDER BY t1.a; Output: t2.c, t2.b, t2.a -> Seq Scan on public.eager_agg_t2 t2 Output: t2.c, t2.b, t2.a -(28 rows) +(29 rows) SELECT t1.a, avg(t2.c + t3.c) FROM eager_agg_t1 t1 @@ -246,6 +250,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t1.a -> Hash Right Join Output: t1.a, (PARTIAL avg(t2.c)) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -256,7 +261,7 @@ GROUP BY t1.a ORDER BY t1.a; Group Key: t2.b -> Seq Scan on public.eager_agg_t2 t2 Output: t2.a, t2.b, t2.c -(18 rows) +(19 rows) SELECT t1.a, avg(t2.c) FROM eager_agg_t1 t1 @@ -397,6 +402,7 @@ GROUP BY t1.a ORDER BY t1.a; Sort Key: t1.a -> Hash Join Output: t1.a, (PARTIAL avg(t2.c)) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -407,7 +413,7 @@ GROUP BY t1.a ORDER BY t1.a; Group Key: t2.b -> Seq Scan on public.eager_agg_t2 t2 Output: t2.a, t2.b, t2.c -(18 rows) +(19 rows) SELECT t1.a, avg(t2.c) FROM eager_agg_t1 t1 @@ -547,6 +553,7 @@ GROUP BY t2.b ORDER BY t2.b; Sort Key: t2.b -> Hash Right Semi Join Output: t2.b, (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1.b = t2.b) -> Seq Scan on public.eager_agg_t1 t1 Output: t1.a, t1.b, t1.c @@ -557,7 +564,7 @@ GROUP BY t2.b ORDER BY t2.b; Group Key: t2.b -> Seq Scan on public.eager_agg_t2 t2 Output: t2.a, t2.b, t2.c -(18 rows) +(19 rows) SELECT t2.b, count(*) FROM eager_agg_t2 t2 @@ -599,6 +606,7 @@ GROUP BY s1.a; Sort Key: s1.a -> Nested Loop Output: s1.a, (PARTIAL sum(s2.c)) + Inner Unique: true Join Filter: (s1.a = s2.b) -> Seq Scan on public.eager_agg_s1 s1 Output: s1.a @@ -607,7 +615,7 @@ GROUP BY s1.a; Group Key: s2.b -> Seq Scan on public.eager_agg_s2 s2 Output: s2.b, s2.c -(16 rows) +(17 rows) SELECT s1.a, sum(s2.c) FROM eager_agg_s1 s1 @@ -660,6 +668,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1.x -> Hash Join Output: t1.x, (PARTIAL sum(t1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2.y = t1.x) -> Seq Scan on public.eager_agg_tab2_p1 t2 Output: t2.y @@ -675,6 +684,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_1.x -> Hash Join Output: t1_1.x, (PARTIAL sum(t1_1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2_1.y = t1_1.x) -> Seq Scan on public.eager_agg_tab2_p2 t2_1 Output: t2_1.y @@ -690,6 +700,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Hash Join Output: t1_2.x, (PARTIAL sum(t1_2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2_2.y = t1_2.x) -> Seq Scan on public.eager_agg_tab2_p3 t2_2 Output: t2_2.y @@ -700,7 +711,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Seq Scan on public.eager_agg_tab1_p3 t1_2 Output: t1_2.x, t1_2.y -(49 rows) +(52 rows) SELECT t1.x, sum(t1.y), count(*) FROM eager_agg_tab1 t1 @@ -742,6 +753,7 @@ GROUP BY t2.y ORDER BY t2.y; Group Key: t2.y -> Hash Join Output: t2.y, (PARTIAL sum(t1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2.y = t1.x) -> Seq Scan on public.eager_agg_tab2_p1 t2 Output: t2.y @@ -757,6 +769,7 @@ GROUP BY t2.y ORDER BY t2.y; Group Key: t2_1.y -> Hash Join Output: t2_1.y, (PARTIAL sum(t1_1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2_1.y = t1_1.x) -> Seq Scan on public.eager_agg_tab2_p2 t2_1 Output: t2_1.y @@ -772,6 +785,7 @@ GROUP BY t2.y ORDER BY t2.y; Group Key: t2_2.y -> Hash Join Output: t2_2.y, (PARTIAL sum(t1_2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2_2.y = t1_2.x) -> Seq Scan on public.eager_agg_tab2_p3 t2_2 Output: t2_2.y @@ -782,7 +796,7 @@ GROUP BY t2.y ORDER BY t2.y; Group Key: t1_2.x -> Seq Scan on public.eager_agg_tab1_p3 t1_2 Output: t1_2.y, t1_2.x -(49 rows) +(52 rows) SELECT t2.y, sum(t1.y), count(*) FROM eager_agg_tab1 t1 @@ -826,6 +840,7 @@ GROUP BY t2.x HAVING avg(t1.x) > 5 ORDER BY t2.x; -> Append -> Hash Join Output: t2.x, (PARTIAL sum(t1.x)), (PARTIAL count(*)), (PARTIAL avg(t1.x)) + Inner Unique: true Hash Cond: (t2.y = t1.x) -> Seq Scan on public.eager_agg_tab2_p1 t2 Output: t2.x, t2.y @@ -838,6 +853,7 @@ GROUP BY t2.x HAVING avg(t1.x) > 5 ORDER BY t2.x; Output: t1.x -> Hash Join Output: t2_1.x, (PARTIAL sum(t1_1.x)), (PARTIAL count(*)), (PARTIAL avg(t1_1.x)) + Inner Unique: true Hash Cond: (t2_1.y = t1_1.x) -> Seq Scan on public.eager_agg_tab2_p2 t2_1 Output: t2_1.x, t2_1.y @@ -850,6 +866,7 @@ GROUP BY t2.x HAVING avg(t1.x) > 5 ORDER BY t2.x; Output: t1_1.x -> Hash Join Output: t2_2.x, (PARTIAL sum(t1_2.x)), (PARTIAL count(*)), (PARTIAL avg(t1_2.x)) + Inner Unique: true Hash Cond: (t2_2.y = t1_2.x) -> Seq Scan on public.eager_agg_tab2_p3 t2_2 Output: t2_2.x, t2_2.y @@ -860,7 +877,7 @@ GROUP BY t2.x HAVING avg(t1.x) > 5 ORDER BY t2.x; Group Key: t1_2.x -> Seq Scan on public.eager_agg_tab1_p3 t1_2 Output: t1_2.x -(44 rows) +(47 rows) SELECT t2.x, sum(t1.x), count(*) FROM eager_agg_tab1 t1 @@ -899,6 +916,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1.x -> Hash Join Output: t1.x, (PARTIAL sum((t2.y + t3.y))) + Inner Unique: true Hash Cond: (t1.x = t2.x) -> Seq Scan on public.eager_agg_tab1_p1 t1 Output: t1.x @@ -921,6 +939,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_1.x -> Hash Join Output: t1_1.x, (PARTIAL sum((t2_1.y + t3_1.y))) + Inner Unique: true Hash Cond: (t1_1.x = t2_1.x) -> Seq Scan on public.eager_agg_tab1_p2 t1_1 Output: t1_1.x @@ -943,6 +962,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Hash Join Output: t1_2.x, (PARTIAL sum((t2_2.y + t3_2.y))) + Inner Unique: true Hash Cond: (t1_2.x = t2_2.x) -> Seq Scan on public.eager_agg_tab1_p3 t1_2 Output: t1_2.x @@ -960,7 +980,7 @@ GROUP BY t1.x ORDER BY t1.x; Output: t3_2.y, t3_2.x -> Seq Scan on public.eager_agg_tab1_p3 t3_2 Output: t3_2.y, t3_2.x -(70 rows) +(73 rows) SELECT t1.x, sum(t2.y + t3.y) FROM eager_agg_tab1 t1 @@ -1356,6 +1376,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1.x -> Hash Join Output: t1.x, (PARTIAL sum(t1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2.y = t1.x) -> Seq Scan on public.eager_agg_tab2_p1 t2 Output: t2.y @@ -1371,6 +1392,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_1.x -> Hash Join Output: t1_1.x, (PARTIAL sum(t1_1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2_1.y = t1_1.x) -> Seq Scan on public.eager_agg_tab2_p2 t2_1 Output: t2_1.y @@ -1386,6 +1408,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Hash Join Output: t1_2.x, (PARTIAL sum(t1_2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t2_2.y = t1_2.x) -> Seq Scan on public.eager_agg_tab2_p3 t2_2 Output: t2_2.y @@ -1396,7 +1419,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Seq Scan on public.eager_agg_tab1_p3 t1_2 Output: t1_2.x, t1_2.y -(49 rows) +(52 rows) SELECT t1.x, sum(t1.y), count(*) FROM eager_agg_tab1 t1 @@ -1456,6 +1479,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1.x -> Hash Join Output: t1.x, (PARTIAL sum(t2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1.x = t2.x) -> Seq Scan on public.eager_agg_tab_ml_p1 t1 Output: t1.x @@ -1471,6 +1495,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_1.x -> Hash Join Output: t1_1.x, (PARTIAL sum(t2_1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_1.x = t2_1.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s1 t1_1 Output: t1_1.x @@ -1486,6 +1511,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Hash Join Output: t1_2.x, (PARTIAL sum(t2_2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_2.x = t2_2.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s2 t1_2 Output: t1_2.x @@ -1501,6 +1527,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_3.x -> Hash Join Output: t1_3.x, (PARTIAL sum(t2_3.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_3.x = t2_3.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s1 t1_3 Output: t1_3.x @@ -1516,6 +1543,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_4.x -> Hash Join Output: t1_4.x, (PARTIAL sum(t2_4.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_4.x = t2_4.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t1_4 Output: t1_4.x @@ -1526,7 +1554,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t2_4.x -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t2_4 Output: t2_4.y, t2_4.x -(79 rows) +(84 rows) SELECT t1.x, sum(t2.y), count(*) FROM eager_agg_tab_ml t1 @@ -1584,6 +1612,7 @@ GROUP BY t1.y ORDER BY t1.y; -> Append -> Hash Join Output: t1.y, (PARTIAL sum(t2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1.x = t2.x) -> Seq Scan on public.eager_agg_tab_ml_p1 t1 Output: t1.y, t1.x @@ -1596,6 +1625,7 @@ GROUP BY t1.y ORDER BY t1.y; Output: t2.y, t2.x -> Hash Join Output: t1_1.y, (PARTIAL sum(t2_1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_1.x = t2_1.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s1 t1_1 Output: t1_1.y, t1_1.x @@ -1608,6 +1638,7 @@ GROUP BY t1.y ORDER BY t1.y; Output: t2_1.y, t2_1.x -> Hash Join Output: t1_2.y, (PARTIAL sum(t2_2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_2.x = t2_2.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s2 t1_2 Output: t1_2.y, t1_2.x @@ -1620,6 +1651,7 @@ GROUP BY t1.y ORDER BY t1.y; Output: t2_2.y, t2_2.x -> Hash Join Output: t1_3.y, (PARTIAL sum(t2_3.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_3.x = t2_3.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s1 t1_3 Output: t1_3.y, t1_3.x @@ -1632,6 +1664,7 @@ GROUP BY t1.y ORDER BY t1.y; Output: t2_3.y, t2_3.x -> Hash Join Output: t1_4.y, (PARTIAL sum(t2_4.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_4.x = t2_4.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t1_4 Output: t1_4.y, t1_4.x @@ -1642,7 +1675,7 @@ GROUP BY t1.y ORDER BY t1.y; Group Key: t2_4.x -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t2_4 Output: t2_4.y, t2_4.x -(67 rows) +(72 rows) SELECT t1.y, sum(t2.y), count(*) FROM eager_agg_tab_ml t1 @@ -1701,6 +1734,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1.x -> Hash Join Output: t1.x, (PARTIAL sum((t2.y + t3.y))), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1.x = t2.x) -> Seq Scan on public.eager_agg_tab_ml_p1 t1 Output: t1.x @@ -1723,6 +1757,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_1.x -> Hash Join Output: t1_1.x, (PARTIAL sum((t2_1.y + t3_1.y))), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_1.x = t2_1.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s1 t1_1 Output: t1_1.x @@ -1745,6 +1780,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Hash Join Output: t1_2.x, (PARTIAL sum((t2_2.y + t3_2.y))), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_2.x = t2_2.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s2 t1_2 Output: t1_2.x @@ -1767,6 +1803,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_3.x -> Hash Join Output: t1_3.x, (PARTIAL sum((t2_3.y + t3_3.y))), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_3.x = t2_3.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s1 t1_3 Output: t1_3.x @@ -1789,6 +1826,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_4.x -> Hash Join Output: t1_4.x, (PARTIAL sum((t2_4.y + t3_4.y))), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_4.x = t2_4.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t1_4 Output: t1_4.x @@ -1806,7 +1844,7 @@ GROUP BY t1.x ORDER BY t1.x; Output: t3_4.y, t3_4.x -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t3_4 Output: t3_4.y, t3_4.x -(114 rows) +(119 rows) SELECT t1.x, sum(t2.y + t3.y), count(*) FROM eager_agg_tab_ml t1 @@ -2018,6 +2056,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1.x -> Hash Join Output: t1.x, (PARTIAL sum(t2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1.x = t2.x) -> Seq Scan on public.eager_agg_tab_ml_p1 t1 Output: t1.x @@ -2033,6 +2072,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_1.x -> Hash Join Output: t1_1.x, (PARTIAL sum(t2_1.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_1.x = t2_1.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s1 t1_1 Output: t1_1.x @@ -2048,6 +2088,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_2.x -> Hash Join Output: t1_2.x, (PARTIAL sum(t2_2.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_2.x = t2_2.x) -> Seq Scan on public.eager_agg_tab_ml_p2_s2 t1_2 Output: t1_2.x @@ -2063,6 +2104,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_3.x -> Hash Join Output: t1_3.x, (PARTIAL sum(t2_3.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_3.x = t2_3.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s1 t1_3 Output: t1_3.x @@ -2078,6 +2120,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t1_4.x -> Hash Join Output: t1_4.x, (PARTIAL sum(t2_4.y)), (PARTIAL count(*)) + Inner Unique: true Hash Cond: (t1_4.x = t2_4.x) -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t1_4 Output: t1_4.x @@ -2088,7 +2131,7 @@ GROUP BY t1.x ORDER BY t1.x; Group Key: t2_4.x -> Seq Scan on public.eager_agg_tab_ml_p3_s2 t2_4 Output: t2_4.y, t2_4.x -(79 rows) +(84 rows) SELECT t1.x, sum(t2.y), count(*) FROM eager_agg_tab_ml t1 diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index d4f59175f52..9d88bfe4ef8 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -2848,13 +2848,13 @@ order by x.thousand desc, x.twothousand; Sort Key: x.thousand DESC, x.twothousand Presorted Key: x.thousand -> Merge Join - Merge Cond: (y.thousand = x.thousand) - -> Partial GroupAggregate - Group Key: y.thousand - -> Index Only Scan Backward using tenk1_thous_tenthous on tenk1 y + Merge Cond: (x.thousand = y.thousand) -> Sort Sort Key: x.thousand DESC -> Seq Scan on tenk1 x + -> Partial GroupAggregate + Group Key: y.thousand + -> Index Only Scan Backward using tenk1_thous_tenthous on tenk1 y (13 rows) reset enable_hashagg; diff --git a/src/test/regress/expected/uniquekeys.out b/src/test/regress/expected/uniquekeys.out index e52e23954e5..83be435e0db 100644 --- a/src/test/regress/expected/uniquekeys.out +++ b/src/test/regress/expected/uniquekeys.out @@ -1008,4 +1008,62 @@ reset enable_partitionwise_join; reset max_parallel_workers_per_gather; reset enable_hashjoin; reset enable_mergejoin; +-- +-- Test relations that are partially aggregated by eager aggregation. +-- +create table uk_ea1 (id int primary key, x int); +create table uk_ea2 (id int, y int, z int); +insert into uk_ea1 select i, i % 10 from generate_series(1, 1000) i; +insert into uk_ea2 select i % 100, i, i % 3 from generate_series(1, 1000) i; +analyze uk_ea1, uk_ea2; +-- uk_ea2 is grouped by the join column +explain (verbose, costs off) +select a.x, sum(b.y) from uk_ea1 a join uk_ea2 b on a.id = b.id +group by a.x; + QUERY PLAN +------------------------------------------------------------- + Finalize HashAggregate + Output: a.x, sum(b.y) + Group Key: a.x + -> Merge Join + Output: a.x, (PARTIAL sum(b.y)) + Inner Unique: true + Merge Cond: (a.id = b.id) + -> Index Scan using uk_ea1_pkey on public.uk_ea1 a + Output: a.id, a.x + -> Sort + Output: b.id, (PARTIAL sum(b.y)) + Sort Key: b.id + -> Partial HashAggregate + Output: b.id, PARTIAL sum(b.y) + Group Key: b.id + -> Seq Scan on public.uk_ea2 b + Output: b.id, b.y, b.z +(17 rows) + +-- uk_ea2 is grouped by more than the join column +explain (verbose, costs off) +select a.x, b.z, sum(b.y) from uk_ea1 a join uk_ea2 b on a.id = b.id +group by a.x, b.z; + QUERY PLAN +------------------------------------------------------------- + Finalize HashAggregate + Output: a.x, b.z, sum(b.y) + Group Key: a.x, b.z + -> Merge Join + Output: a.x, b.z, (PARTIAL sum(b.y)) + Merge Cond: (a.id = b.id) + -> Index Scan using uk_ea1_pkey on public.uk_ea1 a + Output: a.id, a.x + -> Sort + Output: b.z, b.id, (PARTIAL sum(b.y)) + Sort Key: b.id + -> Partial HashAggregate + Output: b.z, b.id, PARTIAL sum(b.y) + Group Key: b.z, b.id + -> Seq Scan on public.uk_ea2 b + Output: b.id, b.y, b.z +(16 rows) + +drop table uk_ea1, uk_ea2; drop table uk_a, uk_b, uk_c, uk_d, uk_n, uk_p, uk_q, uk_r; diff --git a/src/test/regress/sql/uniquekeys.sql b/src/test/regress/sql/uniquekeys.sql index 24764f565c3..6c7ea109006 100644 --- a/src/test/regress/sql/uniquekeys.sql +++ b/src/test/regress/sql/uniquekeys.sql @@ -299,4 +299,26 @@ reset max_parallel_workers_per_gather; reset enable_hashjoin; reset enable_mergejoin; +-- +-- Test relations that are partially aggregated by eager aggregation. +-- + +create table uk_ea1 (id int primary key, x int); +create table uk_ea2 (id int, y int, z int); +insert into uk_ea1 select i, i % 10 from generate_series(1, 1000) i; +insert into uk_ea2 select i % 100, i, i % 3 from generate_series(1, 1000) i; +analyze uk_ea1, uk_ea2; + +-- uk_ea2 is grouped by the join column +explain (verbose, costs off) +select a.x, sum(b.y) from uk_ea1 a join uk_ea2 b on a.id = b.id +group by a.x; + +-- uk_ea2 is grouped by more than the join column +explain (verbose, costs off) +select a.x, b.z, sum(b.y) from uk_ea1 a join uk_ea2 b on a.id = b.id +group by a.x, b.z; + +drop table uk_ea1, uk_ea2; + drop table uk_a, uk_b, uk_c, uk_d, uk_n, uk_p, uk_q, uk_r; -- 2.53.0