Skip to content

Commit 8eea52e

Browse files
committed
Merge branch 'partial-sort' of https://github.com/postgrespro/postgres_cluster into partial-sort
2 parents 663c650 + 039d674 commit 8eea52e

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/backend/optimizer/path/pathkeys.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ pathkeys_contained_in(List *keys1, List *keys2)
330330
return false;
331331
}
332332

333+
int
334+
pathkeys_get_prefix(List *keys1, List *keys2)
335+
{
336+
ListCell *key1,
337+
*key2;
338+
339+
int prefix = 0;
340+
341+
forboth(key1, keys1, key2, keys2)
342+
{
343+
PathKey *pathkey1 = (PathKey *) lfirst(key1);
344+
PathKey *pathkey2 = (PathKey *) lfirst(key2);
345+
346+
if (pathkey1 != pathkey2)
347+
break;
348+
prefix += 1;
349+
}
350+
return prefix;
351+
}
352+
333353
/*
334354
* get_cheapest_path_for_pathkeys
335355
* Find the cheapest path (according to the specified criterion) that

src/backend/optimizer/plan/createplan.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4049,6 +4049,17 @@ make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
40494049
0.0,
40504050
work_mem,
40514051
limit_tuples);
4052+
if (lefttree->type == T_IndexOnlyScan && root->simple_rel_array_size == 2)
4053+
{
4054+
RelOptInfo* relinfo = root->simple_rel_array[1];
4055+
IndexOptInfo* indexinfo = linitial(root->simple_rel_array[1]->indexlist);
4056+
IndexOnlyScan* indexscan = (IndexOnlyScan*)lefttree;
4057+
List *index_pathkeys = build_index_pathkeys(root, indexinfo, indexscan->indexorderdir);
4058+
int prefix_len = pathkeys_get_prefix(root->query_pathkeys, index_pathkeys);
4059+
sort_path.total_cost -= sort_path.startup_cost;
4060+
sort_path.startup_cost /= (prefix_len+1);
4061+
sort_path.total_cost += sort_path.startup_cost;
4062+
}
40524063
plan->startup_cost = sort_path.startup_cost;
40534064
plan->total_cost = sort_path.total_cost;
40544065
plan->targetlist = lefttree->targetlist;

src/include/optimizer/paths.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ typedef enum
167167

168168
extern PathKeysComparison compare_pathkeys(List *keys1, List *keys2);
169169
extern bool pathkeys_contained_in(List *keys1, List *keys2);
170+
extern int pathkeys_get_prefix(List *keys1, List *keys2);
170171
extern Path *get_cheapest_path_for_pathkeys(List *paths, List *pathkeys,
171172
Relids required_outer,
172173
CostSelector cost_criterion);

0 commit comments

Comments
 (0)