From 103e8e9fd2c7ab5a006d6a2ccd9eb4e0274a5c15 Mon Sep 17 00:00:00 2001 From: William Bernbaum Date: Wed, 26 Aug 2026 08:53:01 -0700 Subject: [PATCH v1 05/16] Take a pushed-down deduplication as the DISTINCT result Additionally, clear the 'Partial' label when it no longer applies to a grouping. --- src/backend/optimizer/plan/planner.c | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index a605387dcaa..750b430f495 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -4031,6 +4031,31 @@ get_number_of_groups(PlannerInfo *root, return dNumGroups; } +/* + * complete_dedup_path + * Clear the partial mark on a pushed-down deduplication. + * + * A deduplication is pushed down marked partial, which EXPLAIN would misreport + * once the path is taken as the finished grouping. Only the label changes. + */ +static Path * +complete_dedup_path(Path *path) +{ + AggPath *aggpath; + + if (!IsA(path, AggPath)) + return path; + + if (((AggPath *) path)->aggsplit != AGGSPLIT_INITIAL_SERIAL) + return path; + + aggpath = makeNode(AggPath); + memcpy(aggpath, path, sizeof(AggPath)); + aggpath->aggsplit = AGGSPLIT_SIMPLE; + + return (Path *) aggpath; +} + /* * create_grouping_paths * @@ -5083,6 +5108,29 @@ create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, /* now build distinct paths based on input_rel's partial_pathlist */ create_partial_distinct_paths(root, input_rel, distinct_rel, target); + /* + * A pushed-down deduplication already groups by the DISTINCT expressions, + * so its rows are the result. Neither Sort nor Unique projects, so add a + * projection onto the target. + */ + if (root->agg_clause_list == NIL && + input_rel->grouped_rel != NULL && + !IS_DUMMY_REL(input_rel->grouped_rel) && + input_rel->grouped_rel->pathlist != NIL) + { + ListCell *lc; + + foreach(lc, input_rel->grouped_rel->pathlist) + { + Path *path = (Path *) lfirst(lc); + + add_path(distinct_rel, + (Path *) create_projection_path(root, distinct_rel, + complete_dedup_path(path), + target)); + } + } + /* Give a helpful error if we failed to create any paths */ if (distinct_rel->pathlist == NIL) ereport(ERROR,