Skip to content

Commit 77bae39

Browse files
committed
Adjust tuplesort API to have bitwise option flags
This replaces the bool flag for randomAccess. An upcoming patch requires adding another option, so instead of breaking the API for that, then breaking it again one day if we add more options, let's just break it once. Any boolean options we add in the future will just make use of an unused bit in the flags. Any extensions making use of tuplesorts will need to update their code to pass TUPLESORT_RANDOMACCESS instead of true for randomAccess. TUPLESORT_NONE can be used for a set of empty options. Author: David Rowley Reviewed-by: Justin Pryzby Discussion: https://postgr.es/m/CAApHDvoH4ASzsAOyHcxkuY01Qf%2B%2B8JJ0paw%2B03dk%2BW25tQEcNQ%40mail.gmail.com
1 parent 1b0d9aa commit 77bae39

File tree

11 files changed

+99
-73
lines changed

11 files changed

+99
-73
lines changed

src/backend/access/gist/gistbuild.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
271271
index,
272272
maintenance_work_mem,
273273
NULL,
274-
false);
274+
TUPLESORT_NONE);
275275

276276
/* Scan the table, adding all tuples to the tuplesort */
277277
reltuples = table_index_build_scan(heap, index, indexInfo, true, true,

src/backend/access/hash/hashsort.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
8686
hspool->max_buckets,
8787
maintenance_work_mem,
8888
NULL,
89-
false);
89+
TUPLESORT_NONE);
9090

9191
return hspool;
9292
}

src/backend/access/heap/heapam_handler.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap,
726726
if (use_sort)
727727
tuplesort = tuplesort_begin_cluster(oldTupDesc, OldIndex,
728728
maintenance_work_mem,
729-
NULL, false);
729+
NULL, TUPLESORT_NONE);
730730
else
731731
tuplesort = NULL;
732732

src/backend/access/nbtree/nbtsort.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ _bt_spools_heapscan(Relation heap, Relation index, BTBuildState *buildstate,
436436
tuplesort_begin_index_btree(heap, index, buildstate->isunique,
437437
buildstate->nulls_not_distinct,
438438
maintenance_work_mem, coordinate,
439-
false);
439+
TUPLESORT_NONE);
440440

441441
/*
442442
* If building a unique index, put dead tuples in a second spool to keep
@@ -475,7 +475,7 @@ _bt_spools_heapscan(Relation heap, Relation index, BTBuildState *buildstate,
475475
*/
476476
buildstate->spool2->sortstate =
477477
tuplesort_begin_index_btree(heap, index, false, false, work_mem,
478-
coordinate2, false);
478+
coordinate2, TUPLESORT_NONE);
479479
}
480480

481481
/* Fill spool using either serial or parallel heap scan */
@@ -1939,7 +1939,7 @@ _bt_parallel_scan_and_sort(BTSpool *btspool, BTSpool *btspool2,
19391939
btspool->isunique,
19401940
btspool->nulls_not_distinct,
19411941
sortmem, coordinate,
1942-
false);
1942+
TUPLESORT_NONE);
19431943

19441944
/*
19451945
* Just as with serial case, there may be a second spool. If so, a

src/backend/catalog/index.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3364,7 +3364,7 @@ validate_index(Oid heapId, Oid indexId, Snapshot snapshot)
33643364
state.tuplesort = tuplesort_begin_datum(INT8OID, Int8LessOperator,
33653365
InvalidOid, false,
33663366
maintenance_work_mem,
3367-
NULL, false);
3367+
NULL, TUPLESORT_NONE);
33683368
state.htups = state.itups = state.tups_inserted = 0;
33693369

33703370
/* ambulkdelete updates progress metrics */

src/backend/executor/nodeAgg.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ initialize_phase(AggState *aggstate, int newphase)
530530
sortnode->collations,
531531
sortnode->nullsFirst,
532532
work_mem,
533-
NULL, false);
533+
NULL, TUPLESORT_NONE);
534534
}
535535

536536
aggstate->current_phase = newphase;
@@ -607,7 +607,7 @@ initialize_aggregate(AggState *aggstate, AggStatePerTrans pertrans,
607607
pertrans->sortOperators[0],
608608
pertrans->sortCollations[0],
609609
pertrans->sortNullsFirst[0],
610-
work_mem, NULL, false);
610+
work_mem, NULL, TUPLESORT_NONE);
611611
}
612612
else
613613
pertrans->sortstates[aggstate->current_set] =
@@ -617,7 +617,7 @@ initialize_aggregate(AggState *aggstate, AggStatePerTrans pertrans,
617617
pertrans->sortOperators,
618618
pertrans->sortCollations,
619619
pertrans->sortNullsFirst,
620-
work_mem, NULL, false);
620+
work_mem, NULL, TUPLESORT_NONE);
621621
}
622622

623623
/*

src/backend/executor/nodeIncrementalSort.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ switchToPresortedPrefixMode(PlanState *pstate)
315315
&(plannode->sort.nullsFirst[nPresortedCols]),
316316
work_mem,
317317
NULL,
318-
false);
318+
TUPLESORT_NONE);
319319
node->prefixsort_state = prefixsort_state;
320320
}
321321
else
@@ -616,7 +616,7 @@ ExecIncrementalSort(PlanState *pstate)
616616
plannode->sort.nullsFirst,
617617
work_mem,
618618
NULL,
619-
false);
619+
TUPLESORT_NONE);
620620
node->fullsort_state = fullsort_state;
621621
}
622622
else

src/backend/executor/nodeSort.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ ExecSort(PlanState *pstate)
7777
Sort *plannode = (Sort *) node->ss.ps.plan;
7878
PlanState *outerNode;
7979
TupleDesc tupDesc;
80+
int tuplesortopts = TUPLESORT_NONE;
8081

8182
SO1_printf("ExecSort: %s\n",
8283
"sorting subplan");
@@ -96,14 +97,17 @@ ExecSort(PlanState *pstate)
9697
outerNode = outerPlanState(node);
9798
tupDesc = ExecGetResultType(outerNode);
9899

100+
if (node->randomAccess)
101+
tuplesortopts |= TUPLESORT_RANDOMACCESS;
102+
99103
if (node->datumSort)
100104
tuplesortstate = tuplesort_begin_datum(TupleDescAttr(tupDesc, 0)->atttypid,
101105
plannode->sortOperators[0],
102106
plannode->collations[0],
103107
plannode->nullsFirst[0],
104108
work_mem,
105109
NULL,
106-
node->randomAccess);
110+
tuplesortopts);
107111
else
108112
tuplesortstate = tuplesort_begin_heap(tupDesc,
109113
plannode->numCols,
@@ -113,7 +117,7 @@ ExecSort(PlanState *pstate)
113117
plannode->nullsFirst,
114118
work_mem,
115119
NULL,
116-
node->randomAccess);
120+
tuplesortopts);
117121
if (node->bounded)
118122
tuplesort_set_bound(tuplesortstate, node->bound);
119123
node->tuplesortstate = (void *) tuplesortstate;

src/backend/utils/adt/orderedsetaggs.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
118118
OSAPerQueryState *qstate;
119119
MemoryContext gcontext;
120120
MemoryContext oldcontext;
121+
int tuplesortopt;
121122

122123
/*
123124
* Check we're called as aggregate (and not a window function), and get
@@ -283,6 +284,11 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
283284
osastate->qstate = qstate;
284285
osastate->gcontext = gcontext;
285286

287+
tuplesortopt = TUPLESORT_NONE;
288+
289+
if (qstate->rescan_needed)
290+
tuplesortopt |= TUPLESORT_RANDOMACCESS;
291+
286292
/*
287293
* Initialize tuplesort object.
288294
*/
@@ -295,15 +301,15 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples)
295301
qstate->sortNullsFirsts,
296302
work_mem,
297303
NULL,
298-
qstate->rescan_needed);
304+
tuplesortopt);
299305
else
300306
osastate->sortstate = tuplesort_begin_datum(qstate->sortColType,
301307
qstate->sortOperator,
302308
qstate->sortCollation,
303309
qstate->sortNullsFirst,
304310
work_mem,
305311
NULL,
306-
qstate->rescan_needed);
312+
tuplesortopt);
307313

308314
osastate->number_of_rows = 0;
309315
osastate->sort_done = false;

0 commit comments

Comments
 (0)