Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4e1ff2a

Browse files
committed
Trim ORDER BY/DISTINCT aggregate pathkeys in gather_grouping_paths
Similar to d8a2953, 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 1349d27 was added
1 parent 52898c6 commit 4e1ff2a

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7349,13 +7349,24 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
73497349
{
73507350
ListCell *lc;
73517351
Path *cheapest_partial_path;
7352+
List *groupby_pathkeys;
7353+
7354+
/*
7355+
* This occurs after any partial aggregation has taken place, so trim off
7356+
* any pathkeys added for ORDER BY / DISTINCT aggregates.
7357+
*/
7358+
if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys)
7359+
groupby_pathkeys = list_copy_head(root->group_pathkeys,
7360+
root->num_groupby_pathkeys);
7361+
else
7362+
groupby_pathkeys = root->group_pathkeys;
73527363

73537364
/* Try Gather for unordered paths and Gather Merge for ordered ones. */
73547365
generate_useful_gather_paths(root, rel, true);
73557366

73567367
/* Try cheapest partial path + explicit Sort + Gather Merge. */
73577368
cheapest_partial_path = linitial(rel->partial_pathlist);
7358-
if (!pathkeys_contained_in(root->group_pathkeys,
7369+
if (!pathkeys_contained_in(groupby_pathkeys,
73597370
cheapest_partial_path->pathkeys))
73607371
{
73617372
Path *path;
@@ -7364,14 +7375,14 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
73647375
total_groups =
73657376
cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
73667377
path = (Path *) create_sort_path(root, rel, cheapest_partial_path,
7367-
root->group_pathkeys,
7378+
groupby_pathkeys,
73687379
-1.0);
73697380
path = (Path *)
73707381
create_gather_merge_path(root,
73717382
rel,
73727383
path,
73737384
rel->reltarget,
7374-
root->group_pathkeys,
7385+
groupby_pathkeys,
73757386
NULL,
73767387
&total_groups);
73777388

@@ -7382,10 +7393,10 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
73827393
* Consider incremental sort on all partial paths, if enabled.
73837394
*
73847395
* We can also skip the entire loop when we only have a single-item
7385-
* group_pathkeys because then we can't possibly have a presorted prefix
7396+
* groupby_pathkeys because then we can't possibly have a presorted prefix
73867397
* of the list without having the list be fully sorted.
73877398
*/
7388-
if (!enable_incremental_sort || list_length(root->group_pathkeys) == 1)
7399+
if (!enable_incremental_sort || list_length(groupby_pathkeys) == 1)
73897400
return;
73907401

73917402
/* also consider incremental sort on partial paths, if enabled */
@@ -7396,7 +7407,7 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
73967407
int presorted_keys;
73977408
double total_groups;
73987409

7399-
is_sorted = pathkeys_count_contained_in(root->group_pathkeys,
7410+
is_sorted = pathkeys_count_contained_in(groupby_pathkeys,
74007411
path->pathkeys,
74017412
&presorted_keys);
74027413

@@ -7409,7 +7420,7 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
74097420
path = (Path *) create_incremental_sort_path(root,
74107421
rel,
74117422
path,
7412-
root->group_pathkeys,
7423+
groupby_pathkeys,
74137424
presorted_keys,
74147425
-1.0);
74157426

@@ -7418,7 +7429,7 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
74187429
rel,
74197430
path,
74207431
rel->reltarget,
7421-
root->group_pathkeys,
7432+
groupby_pathkeys,
74227433
NULL,
74237434
&total_groups);
74247435

0 commit comments

Comments
 (0)