diff options
author | David Rowley <drowley@postgresql.org> | 2024-03-15 11:54:36 +1300 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2024-03-15 11:54:36 +1300 |
commit | 4169850f0b6fc98a5f28d2b0ca4c3a4c1ecf4553 (patch) | |
tree | 31f98e96b56888e8c09dfed9f139de94b4ef7add /src/backend/optimizer/plan/planner.c | |
parent | 4665cebc8a01dabd54b000bcc107a3468be3a81c (diff) | |
download | postgresql-4169850f0b6fc98a5f28d2b0ca4c3a4c1ecf4553.tar.gz postgresql-4169850f0b6fc98a5f28d2b0ca4c3a4c1ecf4553.zip |
Trim ORDER BY/DISTINCT aggregate pathkeys in gather_grouping_paths
Similar to d8a295389, trim off any PathKeys which are for ORDER BY /
DISTINCT aggregate functions from the PathKey List for the Gather Merge
paths created by gather_grouping_paths(). These additional PathKeys are
not valid to use after grouping has taken place as these PathKeys belong
to columns which are inputs to an aggregate function and, therefore are
unavailable after aggregation.
Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/cf63174c-8c89-3953-cb49-48f41f74941a@gmail.com
Backpatch-through: 16, where 1349d2790 was added
Diffstat (limited to 'src/backend/optimizer/plan/planner.c')
-rw-r--r-- | src/backend/optimizer/plan/planner.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 443ab08d75a..5564826cb4a 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -7320,6 +7320,17 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel) { ListCell *lc; Path *cheapest_partial_path; + List *groupby_pathkeys; + + /* + * This occurs after any partial aggregation has taken place, so trim off + * any pathkeys added for ORDER BY / DISTINCT aggregates. + */ + if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys) + groupby_pathkeys = list_copy_head(root->group_pathkeys, + root->num_groupby_pathkeys); + else + groupby_pathkeys = root->group_pathkeys; /* Try Gather for unordered paths and Gather Merge for ordered ones. */ generate_useful_gather_paths(root, rel, true); @@ -7334,7 +7345,7 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel) int presorted_keys; double total_groups; - is_sorted = pathkeys_count_contained_in(root->group_pathkeys, + is_sorted = pathkeys_count_contained_in(groupby_pathkeys, path->pathkeys, &presorted_keys); @@ -7360,13 +7371,13 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel) */ if (presorted_keys == 0 || !enable_incremental_sort) path = (Path *) create_sort_path(root, rel, path, - root->group_pathkeys, + groupby_pathkeys, -1.0); else path = (Path *) create_incremental_sort_path(root, rel, path, - root->group_pathkeys, + groupby_pathkeys, presorted_keys, -1.0); @@ -7375,7 +7386,7 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel) rel, path, rel->reltarget, - root->group_pathkeys, + groupby_pathkeys, NULL, &total_groups); |