From 15504167f9fb64a027b1cb5af2be1f68eebfe0d5 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Tue, 17 Nov 2026 11:24:19 +0900 Subject: [PATCH v5 3/3] Skip DISTINCT steps over grouped or windowed input A DISTINCT step is skipped when its input is already distinct, but so far only if that input is the scan/join relation. If the query also has grouping, aggregation or window functions, the input is an upper relation, about which we know nothing. We need no UniqueKeys for upper relations to do better. Grouping without grouping sets emits one row for each group, so its output is distinct if all the grouping columns are among the DISTINCT columns, as in SELECT DISTINCT x, count(*) FROM a GROUP BY x; The comparison is made on the EquivalenceClasses of the grouping and DISTINCT pathkeys, so redundant grouping columns are not required. Failing that, the output is still distinct if the scan/join relation is distinct for the grouping columns that are among the DISTINCT columns, since those then determine the group. With aggregation or HAVING but no GROUP BY, at most one row is emitted, and the DISTINCT step can be skipped as well. Window functions neither add nor remove rows. So without grouping, their output is distinct if the scan/join relation is distinct for the DISTINCT columns, which is the check we already had. --- src/backend/optimizer/README | 9 +++ src/backend/optimizer/plan/planner.c | 67 ++++++++++++++-- src/test/regress/expected/aggregates.out | 25 +++--- src/test/regress/expected/uniquekeys.out | 99 ++++++++++++++++++++++++ src/test/regress/sql/uniquekeys.sql | 31 ++++++++ 5 files changed, 208 insertions(+), 23 deletions(-) diff --git a/src/backend/optimizer/README b/src/backend/optimizer/README index d2c8a17974c..a07aebf2262 100644 --- a/src/backend/optimizer/README +++ b/src/backend/optimizer/README @@ -1099,6 +1099,15 @@ with a unique index on the nullable column b.u, every row of b that the join emits has a non-NULL b.u, so the index is as good as one on a NOT NULL column. +A DISTINCT step can also follow grouping or window functions, for which we +keep no UniqueKeys. Grouping without grouping sets emits one row for each +group, so its output is distinct if all the grouping columns are among the +DISTINCT columns, or if the relation below is distinct for those that are, +since they then determine the group. It emits at most one row if there is +no GROUP BY. Window functions neither add nor remove rows, so their output +is distinct if the relation below them is distinct for the DISTINCT columns +(see is_redundant_distinct). + Order of processing for EquivalenceClasses and PathKeys ------------------------------------------------------- diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 68c3a29c524..d249b5d6c5e 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -185,7 +185,7 @@ static RelOptInfo *create_grouping_paths(PlannerInfo *root, grouping_sets_data *gd); static bool is_degenerate_grouping(PlannerInfo *root); static bool is_redundant_grouping(PlannerInfo *root, RelOptInfo *input_rel); -static bool is_redundant_distinct(PlannerInfo *root, RelOptInfo *input_rel); +static bool is_redundant_distinct(PlannerInfo *root, RelOptInfo *scanjoin_rel); static void create_redundant_step_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *output_rel, @@ -227,6 +227,7 @@ static void create_one_window_path(PlannerInfo *root, List *activeWindows); static RelOptInfo *create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, + RelOptInfo *scanjoin_rel, PathTarget *target); static void create_partial_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, @@ -1853,6 +1854,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction, bool scanjoin_target_parallel_safe; bool scanjoin_target_same_exprs; bool have_grouping; + RelOptInfo *scanjoin_rel; WindowFuncLists *wflists = NULL; List *activeWindows = NIL; grouping_sets_data *gset_data = NULL; @@ -1967,6 +1969,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction, * of the query's sort clause, distinct clause, etc. */ current_rel = query_planner(root, standard_qp_callback, &qp_extra); + scanjoin_rel = current_rel; /* * Convert the query's result tlist into PathTarget format. @@ -2151,6 +2154,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction, { current_rel = create_distinct_paths(root, current_rel, + scanjoin_rel, sort_input_target); } } /* end of if (setOperations) */ @@ -4305,10 +4309,11 @@ is_redundant_grouping(PlannerInfo *root, RelOptInfo *input_rel) * is_redundant_distinct * * A redundant DISTINCT is one whose input rows are certain to be distinct - * already. + * already. The input rows are those of the scan/join relation, possibly + * after grouping and window functions. */ static bool -is_redundant_distinct(PlannerInfo *root, RelOptInfo *input_rel) +is_redundant_distinct(PlannerInfo *root, RelOptInfo *scanjoin_rel) { Query *parse = root->parse; @@ -4319,8 +4324,55 @@ is_redundant_distinct(PlannerInfo *root, RelOptInfo *input_rel) if (parse->hasTargetSRFs) return false; - /* As above, it's fine if distinct_pathkeys is empty */ - return rel_is_distinct_for_pathkeys(root, input_rel, + if (parse->groupClause || parse->groupingSets || + parse->hasAggs || root->hasHavingQual) + { + List *common_pathkeys = NIL; + + if (parse->groupingSets) + return false; + + /* Without GROUP BY, at most one row comes out of the grouping step */ + if (parse->groupClause == NIL) + return true; + + /* group_pathkeys does not represent an unsortable GROUP BY */ + if (!grouping_is_sortable(root->processed_groupClause)) + return false; + + /* + * There is one row for each group, so the rows are distinct if all + * the grouping columns are among the DISTINCT columns. Failing that, + * they are distinct if the scan/join relation is distinct for the + * grouping columns that are, since those then determine the group. + */ + for (int i = 0; i < root->num_groupby_pathkeys; i++) + { + PathKey *gpathkey = list_nth_node(PathKey, + root->group_pathkeys, i); + + foreach_node(PathKey, dpathkey, root->distinct_pathkeys) + { + if (dpathkey->pk_eclass == gpathkey->pk_eclass) + { + common_pathkeys = lappend(common_pathkeys, gpathkey); + break; + } + } + } + + if (list_length(common_pathkeys) == root->num_groupby_pathkeys) + return true; + + return rel_is_distinct_for_pathkeys(root, scanjoin_rel, + common_pathkeys); + } + + /* + * Window functions don't change the rows, so check the scan/join rel. As + * above, it's fine if distinct_pathkeys is empty. + */ + return rel_is_distinct_for_pathkeys(root, scanjoin_rel, root->distinct_pathkeys); } @@ -5169,6 +5221,7 @@ create_one_window_path(PlannerInfo *root, * Build a new upperrel containing Paths for SELECT DISTINCT evaluation. * * input_rel: contains the source-data Paths + * scanjoin_rel: the scan/join relation of the query * target: the pathtarget for the result Paths to compute * * Note: input paths should already compute the desired pathtarget, since @@ -5176,7 +5229,7 @@ create_one_window_path(PlannerInfo *root, */ static RelOptInfo * create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, - PathTarget *target) + RelOptInfo *scanjoin_rel, PathTarget *target) { RelOptInfo *distinct_rel; @@ -5200,7 +5253,7 @@ create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, distinct_rel->useridiscurrent = input_rel->useridiscurrent; distinct_rel->fdwroutine = input_rel->fdwroutine; - if (is_redundant_distinct(root, input_rel)) + if (is_redundant_distinct(root, scanjoin_rel)) { /* the input rows are distinct already, so just pass them on */ create_redundant_step_paths(root, input_rel, distinct_rel, target); diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index b9ffdf854b1..71c744675f9 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -1339,15 +1339,13 @@ explain (costs off) select distinct max(unique2) from tenk1; QUERY PLAN --------------------------------------------------------------------- - HashAggregate - Group Key: (InitPlan minmax_1).col1 + Result + Replaces: MinMaxAggregate InitPlan minmax_1 -> Limit -> Index Only Scan Backward using tenk1_unique2 on tenk1 Index Cond: (unique2 IS NOT NULL) - -> Result - Replaces: MinMaxAggregate -(8 rows) +(6 rows) select distinct max(unique2) from tenk1; max @@ -1530,7 +1528,8 @@ explain (costs off) select distinct min(f1), max(f1) from minmaxtest; QUERY PLAN --------------------------------------------------------------------------------------------- - Unique + Result + Replaces: MinMaxAggregate InitPlan minmax_1 -> Limit -> Merge Append @@ -1553,11 +1552,7 @@ explain (costs off) -> Index Only Scan using minmaxtest2i on minmaxtest2 minmaxtest_8 Index Cond: (f1 IS NOT NULL) -> Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest_9 - -> Sort - Sort Key: ((InitPlan minmax_1).col1), ((InitPlan minmax_2).col1) - -> Result - Replaces: MinMaxAggregate -(27 rows) +(24 rows) select distinct min(f1), max(f1) from minmaxtest; min | max @@ -1580,15 +1575,13 @@ explain (costs off) --------------------------------------------------------------------- Seq Scan on int4_tbl t0 SubPlan expr_1 - -> HashAggregate - Group Key: (InitPlan minmax_1).col1 + -> Result + Replaces: MinMaxAggregate InitPlan minmax_1 -> Limit -> Seq Scan on int4_tbl t1 Filter: ((f1 IS NOT NULL) AND (f1 = t0.f1)) - -> Result - Replaces: MinMaxAggregate -(10 rows) +(8 rows) select f1, (select distinct min(t1.f1) from int4_tbl t1 where t1.f1 = t0.f1) from int4_tbl t0; diff --git a/src/test/regress/expected/uniquekeys.out b/src/test/regress/expected/uniquekeys.out index 317f54a1f75..b102bfeee0f 100644 --- a/src/test/regress/expected/uniquekeys.out +++ b/src/test/regress/expected/uniquekeys.out @@ -332,4 +332,103 @@ select distinct id, generate_series(1, 2) from uk_a; -> Seq Scan on uk_a (4 rows) +-- DISTINCT over grouped input needs all the grouping columns +explain (costs off) +select distinct x, count(*) from uk_a group by x; + QUERY PLAN +------------------------ + HashAggregate + Group Key: x + -> Seq Scan on uk_a +(3 rows) + +explain (costs off) +select distinct x, count(*) from uk_a group by x, y; + QUERY PLAN +------------------------------ + HashAggregate + Group Key: x, count(*) + -> HashAggregate + Group Key: x, y + -> Seq Scan on uk_a +(5 rows) + +-- or grouping columns that the scan/join relation is distinct for +explain (costs off) +select distinct a.id, count(*) from uk_a a join uk_b b on a.x = b.id +group by a.id, b.y; + QUERY PLAN +-------------------------------------- + HashAggregate + Group Key: a.id, b.y + -> Hash Join + Hash Cond: (a.x = b.id) + -> Seq Scan on uk_a a + -> Hash + -> Seq Scan on uk_b b +(7 rows) + +explain (costs off) +select distinct b.id, count(*) from uk_a a join uk_b b on a.x = b.id +group by a.id, b.id; + QUERY PLAN +-------------------------------------------- + HashAggregate + Group Key: b.id, count(*) + -> HashAggregate + Group Key: a.id, b.id + -> Hash Join + Hash Cond: (a.x = b.id) + -> Seq Scan on uk_a a + -> Hash + -> Seq Scan on uk_b b +(9 rows) + +-- unless there are grouping sets +explain (costs off) +select distinct x, count(*) from uk_a group by grouping sets ((x), ()); + QUERY PLAN +------------------------------ + HashAggregate + Group Key: x, count(*) + -> MixedAggregate + Hash Key: x + Group Key: () + -> Seq Scan on uk_a +(6 rows) + +-- aggregation without GROUP BY returns one row +explain (costs off) +select distinct count(*) from uk_a; + QUERY PLAN +------------------------ + Aggregate + -> Seq Scan on uk_a +(2 rows) + +-- window functions do not change the rows of their input +explain (costs off) +select distinct id, sum(x) over (partition by y) from uk_a; + QUERY PLAN +---------------------------------- + WindowAgg + Window: w1 AS (PARTITION BY y) + -> Sort + Sort Key: y + -> Seq Scan on uk_a +(5 rows) + +explain (costs off) +select distinct x, sum(x) over (partition by y) from uk_a; + QUERY PLAN +---------------------------------------- + HashAggregate + Group Key: x, sum(x) OVER w1 + -> WindowAgg + Window: w1 AS (PARTITION BY y) + -> Sort + Sort Key: y + -> Seq Scan on uk_a +(7 rows) + drop table uk_a, uk_b, uk_c, uk_d, uk_n; diff --git a/src/test/regress/sql/uniquekeys.sql b/src/test/regress/sql/uniquekeys.sql index 5c0c04b16fb..7883ae76166 100644 --- a/src/test/regress/sql/uniquekeys.sql +++ b/src/test/regress/sql/uniquekeys.sql @@ -116,4 +116,35 @@ select distinct on (id) id, x from uk_a; explain (costs off) select distinct id, generate_series(1, 2) from uk_a; +-- DISTINCT over grouped input needs all the grouping columns +explain (costs off) +select distinct x, count(*) from uk_a group by x; + +explain (costs off) +select distinct x, count(*) from uk_a group by x, y; + +-- or grouping columns that the scan/join relation is distinct for +explain (costs off) +select distinct a.id, count(*) from uk_a a join uk_b b on a.x = b.id +group by a.id, b.y; + +explain (costs off) +select distinct b.id, count(*) from uk_a a join uk_b b on a.x = b.id +group by a.id, b.id; + +-- unless there are grouping sets +explain (costs off) +select distinct x, count(*) from uk_a group by grouping sets ((x), ()); + +-- aggregation without GROUP BY returns one row +explain (costs off) +select distinct count(*) from uk_a; + +-- window functions do not change the rows of their input +explain (costs off) +select distinct id, sum(x) over (partition by y) from uk_a; + +explain (costs off) +select distinct x, sum(x) over (partition by y) from uk_a; + drop table uk_a, uk_b, uk_c, uk_d, uk_n; -- 2.37.1 (Apple Git-137.1)