PostgreSQL Source Code git master
Loading...
Searching...
No Matches
tuplesort.h File Reference
#include "access/itup.h"
#include "executor/instrument_node.h"
#include "executor/tuptable.h"
#include "storage/dsm.h"
#include "utils/logtape.h"
#include "utils/relcache.h"
#include "utils/sortsupport.h"
Include dependency graph for tuplesort.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SortCoordinateData
 
struct  SortTuple
 
struct  TuplesortPublic
 

Macros

#define TUPLESORT_NONE   0
 
#define TUPLESORT_RANDOMACCESS   (1 << 0)
 
#define TUPLESORT_ALLOWBOUNDED   (1 << 1)
 
#define TupleSortUseBumpTupleCxt(opt)   (((opt) & TUPLESORT_ALLOWBOUNDED) == 0)
 
#define PARALLEL_SORT(coordinate)
 
#define TuplesortstateGetPublic(state)   ((TuplesortPublic *) state)
 
#define LogicalTapeReadExact(tape, ptr, len)
 

Typedefs

typedef struct BrinTuple BrinTuple
 
typedef struct GinTuple GinTuple
 
typedef struct Tuplesortstate Tuplesortstate
 
typedef struct Sharedsort Sharedsort
 
typedef struct SortCoordinateData SortCoordinateData
 
typedef struct SortCoordinateDataSortCoordinate
 
typedef int(* SortTupleComparator) (const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
 

Functions

Tuplesortstatetuplesort_begin_common (int workMem, SortCoordinate coordinate, int sortopt)
 
void tuplesort_set_bound (Tuplesortstate *state, int64 bound)
 
bool tuplesort_used_bound (Tuplesortstate *state)
 
void tuplesort_puttuple_common (Tuplesortstate *state, SortTuple *tuple, bool useAbbrev, Size tuplen)
 
void tuplesort_performsort (Tuplesortstate *state)
 
bool tuplesort_gettuple_common (Tuplesortstate *state, bool forward, SortTuple *stup)
 
bool tuplesort_skiptuples (Tuplesortstate *state, int64 ntuples, bool forward)
 
void tuplesort_end (Tuplesortstate *state)
 
void tuplesort_reset (Tuplesortstate *state)
 
void tuplesort_get_stats (Tuplesortstate *state, TuplesortInstrumentation *stats)
 
const chartuplesort_method_name (TuplesortMethod m)
 
const chartuplesort_space_type_name (TuplesortSpaceType t)
 
int tuplesort_merge_order (int64 allowedMem)
 
Size tuplesort_estimate_shared (int nWorkers)
 
void tuplesort_initialize_shared (Sharedsort *shared, int nWorkers, dsm_segment *seg)
 
void tuplesort_attach_shared (Sharedsort *shared, dsm_segment *seg)
 
void tuplesort_rescan (Tuplesortstate *state)
 
void tuplesort_markpos (Tuplesortstate *state)
 
void tuplesort_restorepos (Tuplesortstate *state)
 
voidtuplesort_readtup_alloc (Tuplesortstate *state, Size tuplen)
 
Tuplesortstatetuplesort_begin_heap (TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_cluster (TupleDesc tupDesc, Relation indexRel, int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_index_btree (Relation heapRel, Relation indexRel, bool enforceUnique, bool uniqueNullsNotDistinct, int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_index_hash (Relation heapRel, Relation indexRel, uint32 high_mask, uint32 low_mask, uint32 max_buckets, int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_index_gist (Relation heapRel, Relation indexRel, int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_index_brin (int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_index_gin (Relation heapRel, Relation indexRel, int workMem, SortCoordinate coordinate, int sortopt)
 
Tuplesortstatetuplesort_begin_datum (Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, SortCoordinate coordinate, int sortopt)
 
void tuplesort_puttupleslot (Tuplesortstate *state, TupleTableSlot *slot)
 
void tuplesort_putheaptuple (Tuplesortstate *state, HeapTuple tup)
 
void tuplesort_putindextuplevalues (Tuplesortstate *state, Relation rel, const ItemPointerData *self, const Datum *values, const bool *isnull)
 
void tuplesort_putbrintuple (Tuplesortstate *state, BrinTuple *tuple, Size size)
 
void tuplesort_putgintuple (Tuplesortstate *state, GinTuple *tuple, Size size)
 
void tuplesort_putdatum (Tuplesortstate *state, Datum val, bool isNull)
 
bool tuplesort_gettupleslot (Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
 
HeapTuple tuplesort_getheaptuple (Tuplesortstate *state, bool forward)
 
IndexTuple tuplesort_getindextuple (Tuplesortstate *state, bool forward)
 
BrinTupletuplesort_getbrintuple (Tuplesortstate *state, Size *len, bool forward)
 
GinTupletuplesort_getgintuple (Tuplesortstate *state, Size *len, bool forward)
 
bool tuplesort_getdatum (Tuplesortstate *state, bool forward, bool copy, Datum *val, bool *isNull, Datum *abbrev)
 

Macro Definition Documentation

◆ LogicalTapeReadExact

#define LogicalTapeReadExact (   tape,
  ptr,
  len 
)
Value:
do { \
if (LogicalTapeRead(tape, ptr, len) != (size_t) (len)) \
elog(ERROR, "unexpected end of data"); \
} while(0)
#define ERROR
Definition elog.h:39
size_t LogicalTapeRead(LogicalTape *lt, void *ptr, size_t size)
Definition logtape.c:928
const void size_t len
static int fb(int x)

Definition at line 229 of file tuplesort.h.

230 { \
231 if (LogicalTapeRead(tape, ptr, len) != (size_t) (len)) \
232 elog(ERROR, "unexpected end of data"); \
233 } while(0)

◆ PARALLEL_SORT

#define PARALLEL_SORT (   coordinate)
Value:
(coordinate == NULL || \
(coordinate)->sharedsort == NULL ? 0 : \
(coordinate)->isWorker ? 1 : 2)

Definition at line 222 of file tuplesort.h.

223 : \
224 (coordinate)->isWorker ? 1 : 2)

◆ TUPLESORT_ALLOWBOUNDED

#define TUPLESORT_ALLOWBOUNDED   (1 << 1)

Definition at line 73 of file tuplesort.h.

◆ TUPLESORT_NONE

#define TUPLESORT_NONE   0

Definition at line 67 of file tuplesort.h.

◆ TUPLESORT_RANDOMACCESS

#define TUPLESORT_RANDOMACCESS   (1 << 0)

Definition at line 70 of file tuplesort.h.

◆ TuplesortstateGetPublic

#define TuplesortstateGetPublic (   state)    ((TuplesortPublic *) state)

Definition at line 226 of file tuplesort.h.

◆ TupleSortUseBumpTupleCxt

#define TupleSortUseBumpTupleCxt (   opt)    (((opt) & TUPLESORT_ALLOWBOUNDED) == 0)

Definition at line 82 of file tuplesort.h.

Typedef Documentation

◆ BrinTuple

Definition at line 33 of file tuplesort.h.

◆ GinTuple

Definition at line 34 of file tuplesort.h.

◆ Sharedsort

Definition at line 41 of file tuplesort.h.

◆ SortCoordinate

◆ SortCoordinateData

◆ SortTupleComparator

typedef int(* SortTupleComparator) (const SortTuple *a, const SortTuple *b, Tuplesortstate *state)

Definition at line 122 of file tuplesort.h.

◆ Tuplesortstate

Function Documentation

◆ tuplesort_attach_shared()

void tuplesort_attach_shared ( Sharedsort shared,
dsm_segment seg 
)
extern

Definition at line 2944 of file tuplesort.c.

2945{
2946 /* Attach to SharedFileSet */
2947 SharedFileSetAttach(&shared->fileset, seg);
2948}
void SharedFileSetAttach(SharedFileSet *fileset, dsm_segment *seg)
SharedFileSet fileset
Definition tuplesort.c:357

References Sharedsort::fileset, and SharedFileSetAttach().

Referenced by _brin_parallel_build_main(), _bt_parallel_build_main(), and _gin_parallel_build_main().

◆ tuplesort_begin_cluster()

Tuplesortstate * tuplesort_begin_cluster ( TupleDesc  tupDesc,
Relation  indexRel,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 254 of file tuplesortvariants.c.

258{
260 sortopt);
263 MemoryContext oldcontext;
265 int i;
266
267 Assert(indexRel->rd_rel->relam == BTREE_AM_OID);
268
269 oldcontext = MemoryContextSwitchTo(base->maincontext);
271
272 if (trace_sort)
273 elog(LOG,
274 "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
276 workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
277
279
281 false, /* no unique check */
282 base->nKeys,
283 workMem,
284 sortopt & TUPLESORT_RANDOMACCESS,
286
291 base->readtup = readtup_cluster;
293 base->arg = arg;
294
295 arg->indexInfo = BuildIndexInfo(indexRel);
296
297 /*
298 * If we don't have a simple leading attribute, we don't currently
299 * initialize datum1, so disable optimizations that require it.
300 */
301 if (arg->indexInfo->ii_IndexAttrNumbers[0] == 0)
302 base->haveDatum1 = false;
303 else
304 base->haveDatum1 = true;
305
306 arg->tupDesc = tupDesc; /* assume we need not copy tupDesc */
307
308 indexScanKey = _bt_mkscankey(indexRel, NULL);
309
310 if (arg->indexInfo->ii_Expressions != NULL)
311 {
312 TupleTableSlot *slot;
313 ExprContext *econtext;
314
315 /*
316 * We will need to use FormIndexDatum to evaluate the index
317 * expressions. To do that, we need an EState, as well as a
318 * TupleTableSlot to put the table tuples into. The econtext's
319 * scantuple has to point to that slot, too.
320 */
321 arg->estate = CreateExecutorState();
323 econtext = GetPerTupleExprContext(arg->estate);
324 econtext->ecxt_scantuple = slot;
325 }
326
327 /* Prepare SortSupport data for each column */
328 base->sortKeys = (SortSupport) palloc0(base->nKeys *
329 sizeof(SortSupportData));
330
331 for (i = 0; i < base->nKeys; i++)
332 {
333 SortSupport sortKey = base->sortKeys + i;
334 ScanKey scanKey = indexScanKey->scankeys + i;
335 bool reverse;
336
337 sortKey->ssup_cxt = CurrentMemoryContext;
338 sortKey->ssup_collation = scanKey->sk_collation;
339 sortKey->ssup_nulls_first =
340 (scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0;
341 sortKey->ssup_attno = scanKey->sk_attno;
342 /* Convey if abbreviation optimization is applicable in principle */
343 sortKey->abbreviate = (i == 0 && base->haveDatum1);
344
345 Assert(sortKey->ssup_attno != 0);
346
347 reverse = (scanKey->sk_flags & SK_BT_DESC) != 0;
348
349 PrepareSortSupportFromIndexRel(indexRel, reverse, sortKey);
350 }
351
353
354 MemoryContextSwitchTo(oldcontext);
355
356 return state;
357}
#define Assert(condition)
Definition c.h:873
#define LOG
Definition elog.h:31
#define elog(elevel,...)
Definition elog.h:226
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
const TupleTableSlotOps TTSOpsHeapTuple
Definition execTuples.c:85
EState * CreateExecutorState(void)
Definition execUtils.c:88
#define GetPerTupleExprContext(estate)
Definition executor.h:656
#define palloc0_object(type)
Definition fe_memutils.h:75
IndexInfo * BuildIndexInfo(Relation index)
Definition index.c:2426
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc0(Size size)
Definition mcxt.c:1417
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define SK_BT_NULLS_FIRST
Definition nbtree.h:1117
#define SK_BT_DESC
Definition nbtree.h:1116
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
Definition nbtutils.c:59
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
void * arg
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:520
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition rel.h:533
void PrepareSortSupportFromIndexRel(Relation indexRel, bool reverse, SortSupport ssup)
struct SortSupportData * SortSupport
Definition sortsupport.h:58
TupleTableSlot * ecxt_scantuple
Definition execnodes.h:275
Form_pg_class rd_rel
Definition rel.h:111
MemoryContext maincontext
Definition tuplesort.h:185
void(* writetup)(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
Definition tuplesort.h:161
void(* removeabbrev)(Tuplesortstate *state, SortTuple *stups, int count)
Definition tuplesort.h:154
void(* freestate)(Tuplesortstate *state)
Definition tuplesort.h:179
void(* readtup)(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
Definition tuplesort.h:170
SortTupleComparator comparetup
Definition tuplesort.h:141
SortSupport sortKeys
Definition tuplesort.h:202
SortTupleComparator comparetup_tiebreak
Definition tuplesort.h:148
Tuplesortstate * tuplesort_begin_common(int workMem, SortCoordinate coordinate, int sortopt)
Definition tuplesort.c:635
bool trace_sort
Definition tuplesort.c:122
#define PARALLEL_SORT(coordinate)
Definition tuplesort.h:222
#define TUPLESORT_RANDOMACCESS
Definition tuplesort.h:70
#define TuplesortstateGetPublic(state)
Definition tuplesort.h:226
static int comparetup_cluster_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void readtup_cluster(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int tuplen)
#define CLUSTER_SORT
static void freestate_cluster(Tuplesortstate *state)
static int comparetup_cluster(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void writetup_cluster(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
static void removeabbrev_cluster(Tuplesortstate *state, SortTuple *stups, int count)

References _bt_mkscankey(), arg, TuplesortPublic::arg, Assert, BuildIndexInfo(), CLUSTER_SORT, TuplesortPublic::comparetup, comparetup_cluster(), comparetup_cluster_tiebreak(), TuplesortPublic::comparetup_tiebreak, CreateExecutorState(), CurrentMemoryContext, ExprContext::ecxt_scantuple, elog, fb(), TuplesortPublic::freestate, freestate_cluster(), GetPerTupleExprContext, TuplesortPublic::haveDatum1, i, IndexRelationGetNumberOfKeyAttributes, LOG, TuplesortPublic::maincontext, MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), TuplesortPublic::nKeys, palloc0(), palloc0_object, PARALLEL_SORT, pfree(), PrepareSortSupportFromIndexRel(), RelationData::rd_rel, TuplesortPublic::readtup, readtup_cluster(), RelationGetNumberOfAttributes, TuplesortPublic::removeabbrev, removeabbrev_cluster(), SK_BT_DESC, SK_BT_NULLS_FIRST, TuplesortPublic::sortKeys, trace_sort, TTSOpsHeapTuple, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_cluster().

Referenced by heapam_relation_copy_for_cluster().

◆ tuplesort_begin_common()

Tuplesortstate * tuplesort_begin_common ( int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 635 of file tuplesort.c.

636{
638 MemoryContext maincontext;
639 MemoryContext sortcontext;
640 MemoryContext oldcontext;
641
642 /* See leader_takeover_tapes() remarks on random access support */
643 if (coordinate && (sortopt & TUPLESORT_RANDOMACCESS))
644 elog(ERROR, "random access disallowed under parallel sort");
645
646 /*
647 * Memory context surviving tuplesort_reset. This memory context holds
648 * data which is useful to keep while sorting multiple similar batches.
649 */
651 "TupleSort main",
653
654 /*
655 * Create a working memory context for one sort operation. The content of
656 * this context is deleted by tuplesort_reset.
657 */
658 sortcontext = AllocSetContextCreate(maincontext,
659 "TupleSort sort",
661
662 /*
663 * Additionally a working memory context for tuples is setup in
664 * tuplesort_begin_batch.
665 */
666
667 /*
668 * Make the Tuplesortstate within the per-sortstate context. This way, we
669 * don't need a separate pfree() operation for it at shutdown.
670 */
671 oldcontext = MemoryContextSwitchTo(maincontext);
672
674
675 if (trace_sort)
676 pg_rusage_init(&state->ru_start);
677
678 state->base.sortopt = sortopt;
679 state->base.tuples = true;
680 state->abbrevNext = 10;
681
682 /*
683 * workMem is forced to be at least 64KB, the current minimum valid value
684 * for the work_mem GUC. This is a defense against parallel sort callers
685 * that divide out memory among many workers in a way that leaves each
686 * with very little memory.
687 */
688 state->allowedMem = Max(workMem, 64) * (int64) 1024;
689 state->base.sortcontext = sortcontext;
690 state->base.maincontext = maincontext;
691
692 state->memtupsize = INITIAL_MEMTUPSIZE;
693 state->memtuples = NULL;
694
695 /*
696 * After all of the other non-parallel-related state, we setup all of the
697 * state needed for each batch.
698 */
700
701 /*
702 * Initialize parallel-related state based on coordination information
703 * from caller
704 */
705 if (!coordinate)
706 {
707 /* Serial sort */
708 state->shared = NULL;
709 state->worker = -1;
710 state->nParticipants = -1;
711 }
712 else if (coordinate->isWorker)
713 {
714 /* Parallel worker produces exactly one final run from all input */
715 state->shared = coordinate->sharedsort;
717 state->nParticipants = -1;
718 }
719 else
720 {
721 /* Parallel leader state only used for final merge */
722 state->shared = coordinate->sharedsort;
723 state->worker = -1;
724 state->nParticipants = coordinate->nParticipants;
725 Assert(state->nParticipants >= 1);
726 }
727
728 MemoryContextSwitchTo(oldcontext);
729
730 return state;
731}
#define Max(x, y)
Definition c.h:991
int64_t int64
Definition c.h:543
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
void pg_rusage_init(PGRUsage *ru0)
Definition pg_rusage.c:27
#define INITIAL_MEMTUPSIZE
Definition tuplesort.c:118
static int worker_get_identifier(Tuplesortstate *state)
Definition tuplesort.c:2964
static void tuplesort_begin_batch(Tuplesortstate *state)
Definition tuplesort.c:741

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, CurrentMemoryContext, elog, ERROR, fb(), INITIAL_MEMTUPSIZE, Max, MemoryContextSwitchTo(), palloc0_object, pg_rusage_init(), trace_sort, tuplesort_begin_batch(), TUPLESORT_RANDOMACCESS, and worker_get_identifier().

Referenced by tuplesort_begin_cluster(), tuplesort_begin_datum(), tuplesort_begin_heap(), tuplesort_begin_index_brin(), tuplesort_begin_index_btree(), tuplesort_begin_index_gin(), tuplesort_begin_index_gist(), and tuplesort_begin_index_hash().

◆ tuplesort_begin_datum()

Tuplesortstate * tuplesort_begin_datum ( Oid  datumType,
Oid  sortOperator,
Oid  sortCollation,
bool  nullsFirstFlag,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 669 of file tuplesortvariants.c.

672{
674 sortopt);
677 MemoryContext oldcontext;
678 int16 typlen;
679 bool typbyval;
680
681 oldcontext = MemoryContextSwitchTo(base->maincontext);
683
684 if (trace_sort)
685 elog(LOG,
686 "begin datum sort: workMem = %d, randomAccess = %c",
687 workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
688
689 base->nKeys = 1; /* always a one-column sort */
690
692 false, /* no unique check */
693 1,
694 workMem,
695 sortopt & TUPLESORT_RANDOMACCESS,
697
701 base->writetup = writetup_datum;
702 base->readtup = readtup_datum;
703 base->haveDatum1 = true;
704 base->arg = arg;
705
706 arg->datumType = datumType;
707
708 /* lookup necessary attributes of the datum type */
709 get_typlenbyval(datumType, &typlen, &typbyval);
710 arg->datumTypeLen = typlen;
711 base->tuples = !typbyval;
712
713 /* Prepare SortSupport data */
715
717 base->sortKeys->ssup_collation = sortCollation;
719
720 /*
721 * Abbreviation is possible here only for by-reference types. In theory,
722 * a pass-by-value datatype could have an abbreviated form that is cheaper
723 * to compare. In a tuple sort, we could support that, because we can
724 * always extract the original datum from the tuple as needed. Here, we
725 * can't, because a datum sort only stores a single copy of the datum; the
726 * "tuple" field of each SortTuple is NULL.
727 */
728 base->sortKeys->abbreviate = !typbyval;
729
730 PrepareSortSupportFromOrderingOp(sortOperator, base->sortKeys);
731
732 /*
733 * The "onlyKey" optimization cannot be used with abbreviated keys, since
734 * tie-breaker comparisons may be required. Typically, the optimization
735 * is only of value to pass-by-value types anyway, whereas abbreviated
736 * keys are typically only of value to pass-by-reference types.
737 */
738 if (!base->sortKeys->abbrev_converter)
739 base->onlyKey = base->sortKeys;
740
741 MemoryContextSwitchTo(oldcontext);
742
743 return state;
744}
int16_t int16
Definition c.h:541
#define palloc_object(type)
Definition fe_memutils.h:74
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition lsyscache.c:2401
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
MemoryContext ssup_cxt
Definition sortsupport.h:66
SortSupport onlyKey
Definition tuplesort.h:212
static void removeabbrev_datum(Tuplesortstate *state, SortTuple *stups, int count)
static int comparetup_datum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static int comparetup_datum_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void readtup_datum(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
static void writetup_datum(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
#define DATUM_SORT

References SortSupportData::abbrev_converter, SortSupportData::abbreviate, arg, TuplesortPublic::arg, TuplesortPublic::comparetup, comparetup_datum(), comparetup_datum_tiebreak(), TuplesortPublic::comparetup_tiebreak, CurrentMemoryContext, DATUM_SORT, elog, fb(), get_typlenbyval(), TuplesortPublic::haveDatum1, LOG, TuplesortPublic::maincontext, MemoryContextSwitchTo(), TuplesortPublic::nKeys, TuplesortPublic::onlyKey, palloc0_object, palloc_object, PARALLEL_SORT, PrepareSortSupportFromOrderingOp(), TuplesortPublic::readtup, readtup_datum(), TuplesortPublic::removeabbrev, removeabbrev_datum(), TuplesortPublic::sortKeys, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, trace_sort, TuplesortPublic::tuples, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_datum().

Referenced by array_sort_internal(), ExecSort(), initialize_aggregate(), ordered_set_startup(), and validate_index().

◆ tuplesort_begin_heap()

Tuplesortstate * tuplesort_begin_heap ( TupleDesc  tupDesc,
int  nkeys,
AttrNumber attNums,
Oid sortOperators,
Oid sortCollations,
bool nullsFirstFlags,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 181 of file tuplesortvariants.c.

186{
188 sortopt);
190 MemoryContext oldcontext;
191 int i;
192
193 oldcontext = MemoryContextSwitchTo(base->maincontext);
194
195 Assert(nkeys > 0);
196
197 if (trace_sort)
198 elog(LOG,
199 "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
200 nkeys, workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
201
202 base->nKeys = nkeys;
203
205 false, /* no unique check */
206 nkeys,
207 workMem,
208 sortopt & TUPLESORT_RANDOMACCESS,
210
214 base->writetup = writetup_heap;
215 base->readtup = readtup_heap;
216 base->haveDatum1 = true;
217 base->arg = tupDesc; /* assume we need not copy tupDesc */
218
219 /* Prepare SortSupport data for each column */
220 base->sortKeys = (SortSupport) palloc0(nkeys * sizeof(SortSupportData));
221
222 for (i = 0; i < nkeys; i++)
223 {
224 SortSupport sortKey = base->sortKeys + i;
225
226 Assert(attNums[i] != 0);
227 Assert(sortOperators[i] != 0);
228
229 sortKey->ssup_cxt = CurrentMemoryContext;
230 sortKey->ssup_collation = sortCollations[i];
231 sortKey->ssup_nulls_first = nullsFirstFlags[i];
232 sortKey->ssup_attno = attNums[i];
233 /* Convey if abbreviation optimization is applicable in principle */
234 sortKey->abbreviate = (i == 0 && base->haveDatum1);
235
237 }
238
239 /*
240 * The "onlyKey" optimization cannot be used with abbreviated keys, since
241 * tie-breaker comparisons may be required. Typically, the optimization
242 * is only of value to pass-by-value types anyway, whereas abbreviated
243 * keys are typically only of value to pass-by-reference types.
244 */
245 if (nkeys == 1 && !base->sortKeys->abbrev_converter)
246 base->onlyKey = base->sortKeys;
247
248 MemoryContextSwitchTo(oldcontext);
249
250 return state;
251}
static void readtup_heap(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
static int comparetup_heap(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void writetup_heap(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
static int comparetup_heap_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void removeabbrev_heap(Tuplesortstate *state, SortTuple *stups, int count)
#define HEAP_SORT

References SortSupportData::abbrev_converter, TuplesortPublic::arg, Assert, TuplesortPublic::comparetup, comparetup_heap(), comparetup_heap_tiebreak(), TuplesortPublic::comparetup_tiebreak, CurrentMemoryContext, elog, fb(), TuplesortPublic::haveDatum1, HEAP_SORT, i, LOG, TuplesortPublic::maincontext, MemoryContextSwitchTo(), TuplesortPublic::nKeys, TuplesortPublic::onlyKey, palloc0(), PARALLEL_SORT, PrepareSortSupportFromOrderingOp(), TuplesortPublic::readtup, readtup_heap(), TuplesortPublic::removeabbrev, removeabbrev_heap(), TuplesortPublic::sortKeys, trace_sort, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_heap().

Referenced by ExecIncrementalSort(), ExecSort(), initialize_aggregate(), initialize_phase(), ordered_set_startup(), and switchToPresortedPrefixMode().

◆ tuplesort_begin_index_brin()

Tuplesortstate * tuplesort_begin_index_brin ( int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 556 of file tuplesortvariants.c.

559{
561 sortopt);
563
564 if (trace_sort)
565 elog(LOG,
566 "begin index sort: workMem = %d, randomAccess = %c",
567 workMem,
568 sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
569
570 base->nKeys = 1; /* Only one sort column, the block number */
571
576 base->haveDatum1 = true;
577 base->arg = NULL;
578
579 return state;
580}
static void writetup_index_brin(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
static void removeabbrev_index_brin(Tuplesortstate *state, SortTuple *stups, int count)
static void readtup_index_brin(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
static int comparetup_index_brin(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)

References TuplesortPublic::arg, TuplesortPublic::comparetup, comparetup_index_brin(), elog, fb(), TuplesortPublic::haveDatum1, LOG, TuplesortPublic::nKeys, TuplesortPublic::readtup, readtup_index_brin(), TuplesortPublic::removeabbrev, removeabbrev_index_brin(), trace_sort, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_index_brin().

Referenced by _brin_parallel_scan_and_build(), and brinbuild().

◆ tuplesort_begin_index_btree()

Tuplesortstate * tuplesort_begin_index_btree ( Relation  heapRel,
Relation  indexRel,
bool  enforceUnique,
bool  uniqueNullsNotDistinct,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 360 of file tuplesortvariants.c.

367{
369 sortopt);
373 MemoryContext oldcontext;
374 int i;
375
376 oldcontext = MemoryContextSwitchTo(base->maincontext);
378
379 if (trace_sort)
380 elog(LOG,
381 "begin index sort: unique = %c, workMem = %d, randomAccess = %c",
382 enforceUnique ? 't' : 'f',
383 workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
384
386
388 enforceUnique,
389 base->nKeys,
390 workMem,
391 sortopt & TUPLESORT_RANDOMACCESS,
393
397 base->writetup = writetup_index;
398 base->readtup = readtup_index;
399 base->haveDatum1 = true;
400 base->arg = arg;
401
402 arg->index.heapRel = heapRel;
403 arg->index.indexRel = indexRel;
404 arg->enforceUnique = enforceUnique;
405 arg->uniqueNullsNotDistinct = uniqueNullsNotDistinct;
406
407 indexScanKey = _bt_mkscankey(indexRel, NULL);
408
409 /* Prepare SortSupport data for each column */
410 base->sortKeys = (SortSupport) palloc0(base->nKeys *
411 sizeof(SortSupportData));
412
413 for (i = 0; i < base->nKeys; i++)
414 {
415 SortSupport sortKey = base->sortKeys + i;
416 ScanKey scanKey = indexScanKey->scankeys + i;
417 bool reverse;
418
419 sortKey->ssup_cxt = CurrentMemoryContext;
420 sortKey->ssup_collation = scanKey->sk_collation;
421 sortKey->ssup_nulls_first =
422 (scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0;
423 sortKey->ssup_attno = scanKey->sk_attno;
424 /* Convey if abbreviation optimization is applicable in principle */
425 sortKey->abbreviate = (i == 0 && base->haveDatum1);
426
427 Assert(sortKey->ssup_attno != 0);
428
429 reverse = (scanKey->sk_flags & SK_BT_DESC) != 0;
430
431 PrepareSortSupportFromIndexRel(indexRel, reverse, sortKey);
432 }
433
435
436 MemoryContextSwitchTo(oldcontext);
437
438 return state;
439}
static int comparetup_index_btree_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static int comparetup_index_btree(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void readtup_index(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
static void removeabbrev_index(Tuplesortstate *state, SortTuple *stups, int count)
#define INDEX_SORT
static void writetup_index(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)

References _bt_mkscankey(), arg, TuplesortPublic::arg, Assert, TuplesortPublic::comparetup, comparetup_index_btree(), comparetup_index_btree_tiebreak(), TuplesortPublic::comparetup_tiebreak, CurrentMemoryContext, elog, fb(), TuplesortPublic::haveDatum1, i, INDEX_SORT, IndexRelationGetNumberOfKeyAttributes, LOG, TuplesortPublic::maincontext, MemoryContextSwitchTo(), TuplesortPublic::nKeys, palloc0(), palloc_object, PARALLEL_SORT, pfree(), PrepareSortSupportFromIndexRel(), TuplesortPublic::readtup, readtup_index(), TuplesortPublic::removeabbrev, removeabbrev_index(), SK_BT_DESC, SK_BT_NULLS_FIRST, TuplesortPublic::sortKeys, trace_sort, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_index().

Referenced by _bt_parallel_scan_and_sort(), and _bt_spools_heapscan().

◆ tuplesort_begin_index_gin()

Tuplesortstate * tuplesort_begin_index_gin ( Relation  heapRel,
Relation  indexRel,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 583 of file tuplesortvariants.c.

587{
589 sortopt);
591 MemoryContext oldcontext;
592 int i;
593 TupleDesc desc = RelationGetDescr(indexRel);
594
595 oldcontext = MemoryContextSwitchTo(base->maincontext);
596
597#ifdef TRACE_SORT
598 if (trace_sort)
599 elog(LOG,
600 "begin index sort: workMem = %d, randomAccess = %c",
601 workMem,
602 sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
603#endif
604
605 /*
606 * Multi-column GIN indexes expand the row into a separate index entry for
607 * attribute, and that's what we write into the tuplesort. But we still
608 * need to initialize sortsupport for all the attributes.
609 */
611
612 /* Prepare SortSupport data for each column */
613 base->sortKeys = (SortSupport) palloc0(base->nKeys *
614 sizeof(SortSupportData));
615
616 for (i = 0; i < base->nKeys; i++)
617 {
618 SortSupport sortKey = base->sortKeys + i;
620 Oid cmpFunc;
621
622 sortKey->ssup_cxt = CurrentMemoryContext;
623 sortKey->ssup_collation = indexRel->rd_indcollation[i];
624 sortKey->ssup_nulls_first = false;
625 sortKey->ssup_attno = i + 1;
626 sortKey->abbreviate = false;
627
628 Assert(sortKey->ssup_attno != 0);
629
630 if (!OidIsValid(sortKey->ssup_collation))
631 sortKey->ssup_collation = DEFAULT_COLLATION_OID;
632
633 /*
634 * If the compare proc isn't specified in the opclass definition, look
635 * up the index key type's default btree comparator.
636 */
637 cmpFunc = index_getprocid(indexRel, i + 1, GIN_COMPARE_PROC);
638 if (cmpFunc == InvalidOid)
639 {
640 TypeCacheEntry *typentry;
641
642 typentry = lookup_type_cache(att->atttypid,
644 if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid))
647 errmsg("could not identify a comparison function for type %s",
648 format_type_be(att->atttypid))));
649
650 cmpFunc = typentry->cmp_proc_finfo.fn_oid;
651 }
652
654 }
655
660 base->haveDatum1 = false;
661 base->arg = NULL;
662
663 MemoryContextSwitchTo(oldcontext);
664
665 return state;
666}
#define OidIsValid(objectId)
Definition c.h:788
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ereport(elevel,...)
Definition elog.h:150
char * format_type_be(Oid type_oid)
#define GIN_COMPARE_PROC
Definition gin.h:24
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition indexam.c:883
FormData_pg_attribute * Form_pg_attribute
#define InvalidOid
unsigned int Oid
#define RelationGetDescr(relation)
Definition rel.h:540
void PrepareSortSupportComparisonShim(Oid cmpFunc, SortSupport ssup)
Definition sortsupport.c:68
Oid fn_oid
Definition fmgr.h:59
Oid * rd_indcollation
Definition rel.h:217
FmgrInfo cmp_proc_finfo
Definition typcache.h:77
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:160
static void writetup_index_gin(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
static void readtup_index_gin(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
static void removeabbrev_index_gin(Tuplesortstate *state, SortTuple *stups, int count)
static int comparetup_index_gin(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition typcache.c:386
#define TYPECACHE_CMP_PROC_FINFO
Definition typcache.h:144

References TuplesortPublic::arg, Assert, TypeCacheEntry::cmp_proc_finfo, TuplesortPublic::comparetup, comparetup_index_gin(), CurrentMemoryContext, elog, ereport, errcode(), errmsg(), ERROR, fb(), FmgrInfo::fn_oid, format_type_be(), GIN_COMPARE_PROC, TuplesortPublic::haveDatum1, i, index_getprocid(), IndexRelationGetNumberOfKeyAttributes, InvalidOid, LOG, lookup_type_cache(), TuplesortPublic::maincontext, MemoryContextSwitchTo(), TuplesortPublic::nKeys, OidIsValid, palloc0(), PrepareSortSupportComparisonShim(), RelationData::rd_indcollation, TuplesortPublic::readtup, readtup_index_gin(), RelationGetDescr, TuplesortPublic::removeabbrev, removeabbrev_index_gin(), TuplesortPublic::sortKeys, trace_sort, TupleDescAttr(), tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TYPECACHE_CMP_PROC_FINFO, TuplesortPublic::writetup, and writetup_index_gin().

Referenced by _gin_parallel_scan_and_build(), and ginbuild().

◆ tuplesort_begin_index_gist()

Tuplesortstate * tuplesort_begin_index_gist ( Relation  heapRel,
Relation  indexRel,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 493 of file tuplesortvariants.c.

498{
500 sortopt);
502 MemoryContext oldcontext;
504 int i;
505
506 oldcontext = MemoryContextSwitchTo(base->maincontext);
508
509 if (trace_sort)
510 elog(LOG,
511 "begin index sort: workMem = %d, randomAccess = %c",
512 workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
513
515
519 base->writetup = writetup_index;
520 base->readtup = readtup_index;
521 base->haveDatum1 = true;
522 base->arg = arg;
523
524 arg->index.heapRel = heapRel;
525 arg->index.indexRel = indexRel;
526 arg->enforceUnique = false;
527 arg->uniqueNullsNotDistinct = false;
528
529 /* Prepare SortSupport data for each column */
530 base->sortKeys = (SortSupport) palloc0(base->nKeys *
531 sizeof(SortSupportData));
532
533 for (i = 0; i < base->nKeys; i++)
534 {
535 SortSupport sortKey = base->sortKeys + i;
536
538 sortKey->ssup_collation = indexRel->rd_indcollation[i];
539 sortKey->ssup_nulls_first = false;
540 sortKey->ssup_attno = i + 1;
541 /* Convey if abbreviation optimization is applicable in principle */
542 sortKey->abbreviate = (i == 0 && base->haveDatum1);
543
544 Assert(sortKey->ssup_attno != 0);
545
546 /* Look for a sort support function */
548 }
549
550 MemoryContextSwitchTo(oldcontext);
551
552 return state;
553}
void PrepareSortSupportFromGistIndexRel(Relation indexRel, SortSupport ssup)

References arg, TuplesortPublic::arg, Assert, TuplesortPublic::comparetup, comparetup_index_btree(), comparetup_index_btree_tiebreak(), TuplesortPublic::comparetup_tiebreak, CurrentMemoryContext, elog, fb(), TuplesortPublic::haveDatum1, i, IndexRelationGetNumberOfKeyAttributes, LOG, TuplesortPublic::maincontext, MemoryContextSwitchTo(), TuplesortPublic::nKeys, palloc0(), palloc_object, PrepareSortSupportFromGistIndexRel(), RelationData::rd_indcollation, TuplesortPublic::readtup, readtup_index(), TuplesortPublic::removeabbrev, removeabbrev_index(), TuplesortPublic::sortKeys, SortSupportData::ssup_cxt, trace_sort, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_index().

Referenced by gistbuild().

◆ tuplesort_begin_index_hash()

Tuplesortstate * tuplesort_begin_index_hash ( Relation  heapRel,
Relation  indexRel,
uint32  high_mask,
uint32  low_mask,
uint32  max_buckets,
int  workMem,
SortCoordinate  coordinate,
int  sortopt 
)
extern

Definition at line 442 of file tuplesortvariants.c.

450{
452 sortopt);
454 MemoryContext oldcontext;
456
457 oldcontext = MemoryContextSwitchTo(base->maincontext);
459
460 if (trace_sort)
461 elog(LOG,
462 "begin index sort: high_mask = 0x%x, low_mask = 0x%x, "
463 "max_buckets = 0x%x, workMem = %d, randomAccess = %c",
464 high_mask,
465 low_mask,
466 max_buckets,
467 workMem,
468 sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f');
469
470 base->nKeys = 1; /* Only one sort column, the hash code */
471
475 base->writetup = writetup_index;
476 base->readtup = readtup_index;
477 base->haveDatum1 = true;
478 base->arg = arg;
479
480 arg->index.heapRel = heapRel;
481 arg->index.indexRel = indexRel;
482
483 arg->high_mask = high_mask;
484 arg->low_mask = low_mask;
485 arg->max_buckets = max_buckets;
486
487 MemoryContextSwitchTo(oldcontext);
488
489 return state;
490}
static int comparetup_index_hash(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static int comparetup_index_hash_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)

References arg, TuplesortPublic::arg, TuplesortPublic::comparetup, comparetup_index_hash(), comparetup_index_hash_tiebreak(), TuplesortPublic::comparetup_tiebreak, elog, fb(), TuplesortPublic::haveDatum1, LOG, TuplesortPublic::maincontext, MemoryContextSwitchTo(), TuplesortPublic::nKeys, palloc_object, TuplesortPublic::readtup, readtup_index(), TuplesortPublic::removeabbrev, removeabbrev_index(), trace_sort, tuplesort_begin_common(), TUPLESORT_RANDOMACCESS, TuplesortstateGetPublic, TuplesortPublic::writetup, and writetup_index().

Referenced by _h_spoolinit().

◆ tuplesort_end()

◆ tuplesort_estimate_shared()

Size tuplesort_estimate_shared ( int  nWorkers)
extern

Definition at line 2900 of file tuplesort.c.

2901{
2903
2904 Assert(nWorkers > 0);
2905
2906 /* Make sure that BufFile shared state is MAXALIGN'd */
2909
2910 return tapesSize;
2911}
#define MAXALIGN(LEN)
Definition c.h:826
size_t Size
Definition c.h:619
Size add_size(Size s1, Size s2)
Definition shmem.c:495
Size mul_size(Size s1, Size s2)
Definition shmem.c:510

References add_size(), Assert, fb(), MAXALIGN, and mul_size().

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), and _gin_begin_parallel().

◆ tuplesort_get_stats()

void tuplesort_get_stats ( Tuplesortstate state,
TuplesortInstrumentation stats 
)
extern

Definition at line 2484 of file tuplesort.c.

2486{
2487 /*
2488 * Note: it might seem we should provide both memory and disk usage for a
2489 * disk-based sort. However, the current code doesn't track memory space
2490 * accurately once we have begun to return tuples to the caller (since we
2491 * don't account for pfree's the caller is expected to do), so we cannot
2492 * rely on availMem in a disk sort. This does not seem worth the overhead
2493 * to fix. Is it worth creating an API for the memory context code to
2494 * tell us how much is actually used in sortcontext?
2495 */
2497
2498 if (state->isMaxSpaceDisk)
2500 else
2502 stats->spaceUsed = (state->maxSpace + 1023) / 1024;
2503
2504 switch (state->maxSpaceStatus)
2505 {
2506 case TSS_SORTEDINMEM:
2507 if (state->boundUsed)
2509 else
2511 break;
2512 case TSS_SORTEDONTAPE:
2514 break;
2515 case TSS_FINALMERGE:
2517 break;
2518 default:
2520 break;
2521 }
2522}
@ SORT_SPACE_TYPE_DISK
@ SORT_SPACE_TYPE_MEMORY
@ SORT_TYPE_EXTERNAL_SORT
@ SORT_TYPE_TOP_N_HEAPSORT
@ SORT_TYPE_QUICKSORT
@ SORT_TYPE_STILL_IN_PROGRESS
@ SORT_TYPE_EXTERNAL_MERGE
TuplesortSpaceType spaceType
@ TSS_SORTEDONTAPE
Definition tuplesort.c:158
@ TSS_SORTEDINMEM
Definition tuplesort.c:157
@ TSS_FINALMERGE
Definition tuplesort.c:159
static void tuplesort_updatemax(Tuplesortstate *state)
Definition tuplesort.c:953

References SORT_SPACE_TYPE_DISK, SORT_SPACE_TYPE_MEMORY, SORT_TYPE_EXTERNAL_MERGE, SORT_TYPE_EXTERNAL_SORT, SORT_TYPE_QUICKSORT, SORT_TYPE_STILL_IN_PROGRESS, SORT_TYPE_TOP_N_HEAPSORT, TuplesortInstrumentation::sortMethod, TuplesortInstrumentation::spaceType, TuplesortInstrumentation::spaceUsed, TSS_FINALMERGE, TSS_SORTEDINMEM, TSS_SORTEDONTAPE, and tuplesort_updatemax().

Referenced by ExecSort(), instrumentSortedGroup(), and show_sort_info().

◆ tuplesort_getbrintuple()

BrinTuple * tuplesort_getbrintuple ( Tuplesortstate state,
Size len,
bool  forward 
)
extern

Definition at line 1084 of file tuplesortvariants.c.

1085{
1090
1092 stup.tuple = NULL;
1093
1094 MemoryContextSwitchTo(oldcontext);
1095
1096 if (!stup.tuple)
1097 return NULL;
1098
1099 btup = (BrinSortTuple *) stup.tuple;
1100
1101 *len = btup->tuplen;
1102
1103 return &btup->tuple;
1104}
MemoryContext sortcontext
Definition tuplesort.h:187
bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
Definition tuplesort.c:1455

References fb(), len, MemoryContextSwitchTo(), TuplesortPublic::sortcontext, BrinSortTuple::tuple, tuplesort_gettuple_common(), and TuplesortstateGetPublic.

Referenced by _brin_parallel_merge().

◆ tuplesort_getdatum()

bool tuplesort_getdatum ( Tuplesortstate state,
bool  forward,
bool  copy,
Datum val,
bool isNull,
Datum abbrev 
)
extern

Definition at line 1154 of file tuplesortvariants.c.

1156{
1161
1163 {
1164 MemoryContextSwitchTo(oldcontext);
1165 return false;
1166 }
1167
1168 /* Ensure we copy into caller's memory context */
1169 MemoryContextSwitchTo(oldcontext);
1170
1171 /* Record abbreviated key for caller */
1172 if (base->sortKeys->abbrev_converter && abbrev)
1173 *abbrev = stup.datum1;
1174
1175 if (stup.isnull1 || !base->tuples)
1176 {
1177 *val = stup.datum1;
1178 *isNull = stup.isnull1;
1179 }
1180 else
1181 {
1182 /* use stup.tuple because stup.datum1 may be an abbreviation */
1183 if (copy)
1184 *val = datumCopy(PointerGetDatum(stup.tuple), false,
1185 arg->datumTypeLen);
1186 else
1187 *val = PointerGetDatum(stup.tuple);
1188 *isNull = false;
1189 }
1190
1191 return true;
1192}
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition datum.c:132
long val
Definition informix.c:689
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352

References SortSupportData::abbrev_converter, arg, TuplesortPublic::arg, datumCopy(), fb(), MemoryContextSwitchTo(), PointerGetDatum(), TuplesortPublic::sortcontext, TuplesortPublic::sortKeys, TuplesortPublic::tuples, tuplesort_gettuple_common(), TuplesortstateGetPublic, and val.

Referenced by array_sort_internal(), ExecSort(), heapam_index_validate_scan(), mode_final(), percentile_cont_final_common(), percentile_cont_multi_final_common(), percentile_disc_final(), percentile_disc_multi_final(), and process_ordered_aggregate_single().

◆ tuplesort_getgintuple()

GinTuple * tuplesort_getgintuple ( Tuplesortstate state,
Size len,
bool  forward 
)
extern

Definition at line 1107 of file tuplesortvariants.c.

1108{
1112 GinTuple *tup;
1113
1115 stup.tuple = NULL;
1116
1117 MemoryContextSwitchTo(oldcontext);
1118
1119 if (!stup.tuple)
1120 return NULL;
1121
1122 tup = (GinTuple *) stup.tuple;
1123
1124 *len = tup->tuplen;
1125
1126 return tup;
1127}

References fb(), len, MemoryContextSwitchTo(), TuplesortPublic::sortcontext, tuplesort_gettuple_common(), and TuplesortstateGetPublic.

Referenced by _gin_parallel_merge(), and _gin_process_worker_data().

◆ tuplesort_getheaptuple()

HeapTuple tuplesort_getheaptuple ( Tuplesortstate state,
bool  forward 
)
extern

◆ tuplesort_getindextuple()

IndexTuple tuplesort_getindextuple ( Tuplesortstate state,
bool  forward 
)
extern

◆ tuplesort_gettuple_common()

bool tuplesort_gettuple_common ( Tuplesortstate state,
bool  forward,
SortTuple stup 
)
extern

Definition at line 1455 of file tuplesort.c.

1457{
1458 unsigned int tuplen;
1459 size_t nmoved;
1460
1461 Assert(!WORKER(state));
1462
1463 switch (state->status)
1464 {
1465 case TSS_SORTEDINMEM:
1466 Assert(forward || state->base.sortopt & TUPLESORT_RANDOMACCESS);
1467 Assert(!state->slabAllocatorUsed);
1468 if (forward)
1469 {
1470 if (state->current < state->memtupcount)
1471 {
1472 *stup = state->memtuples[state->current++];
1473 return true;
1474 }
1475 state->eof_reached = true;
1476
1477 /*
1478 * Complain if caller tries to retrieve more tuples than
1479 * originally asked for in a bounded sort. This is because
1480 * returning EOF here might be the wrong thing.
1481 */
1482 if (state->bounded && state->current >= state->bound)
1483 elog(ERROR, "retrieved too many tuples in a bounded sort");
1484
1485 return false;
1486 }
1487 else
1488 {
1489 if (state->current <= 0)
1490 return false;
1491
1492 /*
1493 * if all tuples are fetched already then we return last
1494 * tuple, else - tuple before last returned.
1495 */
1496 if (state->eof_reached)
1497 state->eof_reached = false;
1498 else
1499 {
1500 state->current--; /* last returned tuple */
1501 if (state->current <= 0)
1502 return false;
1503 }
1504 *stup = state->memtuples[state->current - 1];
1505 return true;
1506 }
1507 break;
1508
1509 case TSS_SORTEDONTAPE:
1510 Assert(forward || state->base.sortopt & TUPLESORT_RANDOMACCESS);
1511 Assert(state->slabAllocatorUsed);
1512
1513 /*
1514 * The slot that held the tuple that we returned in previous
1515 * gettuple call can now be reused.
1516 */
1517 if (state->lastReturnedTuple)
1518 {
1519 RELEASE_SLAB_SLOT(state, state->lastReturnedTuple);
1520 state->lastReturnedTuple = NULL;
1521 }
1522
1523 if (forward)
1524 {
1525 if (state->eof_reached)
1526 return false;
1527
1528 if ((tuplen = getlen(state->result_tape, true)) != 0)
1529 {
1530 READTUP(state, stup, state->result_tape, tuplen);
1531
1532 /*
1533 * Remember the tuple we return, so that we can recycle
1534 * its memory on next call. (This can be NULL, in the
1535 * !state->tuples case).
1536 */
1537 state->lastReturnedTuple = stup->tuple;
1538
1539 return true;
1540 }
1541 else
1542 {
1543 state->eof_reached = true;
1544 return false;
1545 }
1546 }
1547
1548 /*
1549 * Backward.
1550 *
1551 * if all tuples are fetched already then we return last tuple,
1552 * else - tuple before last returned.
1553 */
1554 if (state->eof_reached)
1555 {
1556 /*
1557 * Seek position is pointing just past the zero tuplen at the
1558 * end of file; back up to fetch last tuple's ending length
1559 * word. If seek fails we must have a completely empty file.
1560 */
1561 nmoved = LogicalTapeBackspace(state->result_tape,
1562 2 * sizeof(unsigned int));
1563 if (nmoved == 0)
1564 return false;
1565 else if (nmoved != 2 * sizeof(unsigned int))
1566 elog(ERROR, "unexpected tape position");
1567 state->eof_reached = false;
1568 }
1569 else
1570 {
1571 /*
1572 * Back up and fetch previously-returned tuple's ending length
1573 * word. If seek fails, assume we are at start of file.
1574 */
1575 nmoved = LogicalTapeBackspace(state->result_tape,
1576 sizeof(unsigned int));
1577 if (nmoved == 0)
1578 return false;
1579 else if (nmoved != sizeof(unsigned int))
1580 elog(ERROR, "unexpected tape position");
1581 tuplen = getlen(state->result_tape, false);
1582
1583 /*
1584 * Back up to get ending length word of tuple before it.
1585 */
1586 nmoved = LogicalTapeBackspace(state->result_tape,
1587 tuplen + 2 * sizeof(unsigned int));
1588 if (nmoved == tuplen + sizeof(unsigned int))
1589 {
1590 /*
1591 * We backed up over the previous tuple, but there was no
1592 * ending length word before it. That means that the prev
1593 * tuple is the first tuple in the file. It is now the
1594 * next to read in forward direction (not obviously right,
1595 * but that is what in-memory case does).
1596 */
1597 return false;
1598 }
1599 else if (nmoved != tuplen + 2 * sizeof(unsigned int))
1600 elog(ERROR, "bogus tuple length in backward scan");
1601 }
1602
1603 tuplen = getlen(state->result_tape, false);
1604
1605 /*
1606 * Now we have the length of the prior tuple, back up and read it.
1607 * Note: READTUP expects we are positioned after the initial
1608 * length word of the tuple, so back up to that point.
1609 */
1610 nmoved = LogicalTapeBackspace(state->result_tape,
1611 tuplen);
1612 if (nmoved != tuplen)
1613 elog(ERROR, "bogus tuple length in backward scan");
1614 READTUP(state, stup, state->result_tape, tuplen);
1615
1616 /*
1617 * Remember the tuple we return, so that we can recycle its memory
1618 * on next call. (This can be NULL, in the Datum case).
1619 */
1620 state->lastReturnedTuple = stup->tuple;
1621
1622 return true;
1623
1624 case TSS_FINALMERGE:
1625 Assert(forward);
1626 /* We are managing memory ourselves, with the slab allocator. */
1627 Assert(state->slabAllocatorUsed);
1628
1629 /*
1630 * The slab slot holding the tuple that we returned in previous
1631 * gettuple call can now be reused.
1632 */
1633 if (state->lastReturnedTuple)
1634 {
1635 RELEASE_SLAB_SLOT(state, state->lastReturnedTuple);
1636 state->lastReturnedTuple = NULL;
1637 }
1638
1639 /*
1640 * This code should match the inner loop of mergeonerun().
1641 */
1642 if (state->memtupcount > 0)
1643 {
1644 int srcTapeIndex = state->memtuples[0].srctape;
1645 LogicalTape *srcTape = state->inputTapes[srcTapeIndex];
1647
1648 *stup = state->memtuples[0];
1649
1650 /*
1651 * Remember the tuple we return, so that we can recycle its
1652 * memory on next call. (This can be NULL, in the Datum case).
1653 */
1654 state->lastReturnedTuple = stup->tuple;
1655
1656 /*
1657 * Pull next tuple from tape, and replace the returned tuple
1658 * at top of the heap with it.
1659 */
1661 {
1662 /*
1663 * If no more data, we've reached end of run on this tape.
1664 * Remove the top node from the heap.
1665 */
1667 state->nInputRuns--;
1668
1669 /*
1670 * Close the tape. It'd go away at the end of the sort
1671 * anyway, but better to release the memory early.
1672 */
1674 return true;
1675 }
1676 newtup.srctape = srcTapeIndex;
1678 return true;
1679 }
1680 return false;
1681
1682 default:
1683 elog(ERROR, "invalid tuplesort state");
1684 return false; /* keep compiler quiet */
1685 }
1686}
size_t LogicalTapeBackspace(LogicalTape *lt, size_t size)
Definition logtape.c:1062
void LogicalTapeClose(LogicalTape *lt)
Definition logtape.c:733
static void tuplesort_heap_delete_top(Tuplesortstate *state)
Definition tuplesort.c:2757
static unsigned int getlen(LogicalTape *tape, bool eofOK)
Definition tuplesort.c:2839
#define READTUP(state, stup, tape, len)
Definition tuplesort.c:395
#define WORKER(state)
Definition tuplesort.c:401
static bool mergereadnext(Tuplesortstate *state, LogicalTape *srcTape, SortTuple *stup)
Definition tuplesort.c:2273
#define RELEASE_SLAB_SLOT(state, tuple)
Definition tuplesort.c:380
static void tuplesort_heap_replace_top(Tuplesortstate *state, SortTuple *tuple)
Definition tuplesort.c:2781

References Assert, elog, ERROR, fb(), getlen(), LogicalTapeBackspace(), LogicalTapeClose(), mergereadnext(), READTUP, RELEASE_SLAB_SLOT, TSS_FINALMERGE, TSS_SORTEDINMEM, TSS_SORTEDONTAPE, tuplesort_heap_delete_top(), tuplesort_heap_replace_top(), TUPLESORT_RANDOMACCESS, and WORKER.

Referenced by tuplesort_getbrintuple(), tuplesort_getdatum(), tuplesort_getgintuple(), tuplesort_getheaptuple(), tuplesort_getindextuple(), tuplesort_gettupleslot(), and tuplesort_skiptuples().

◆ tuplesort_gettupleslot()

bool tuplesort_gettupleslot ( Tuplesortstate state,
bool  forward,
bool  copy,
TupleTableSlot slot,
Datum abbrev 
)
extern

Definition at line 1004 of file tuplesortvariants.c.

1006{
1010
1012 stup.tuple = NULL;
1013
1014 MemoryContextSwitchTo(oldcontext);
1015
1016 if (stup.tuple)
1017 {
1018 /* Record abbreviated key for caller */
1019 if (base->sortKeys->abbrev_converter && abbrev)
1020 *abbrev = stup.datum1;
1021
1022 if (copy)
1023 stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple, 0);
1024
1026 return true;
1027 }
1028 else
1029 {
1030 ExecClearTuple(slot);
1031 return false;
1032 }
1033}
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup, Size extra)
Definition heaptuple.c:1541
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:457

References SortSupportData::abbrev_converter, ExecClearTuple(), ExecStoreMinimalTuple(), fb(), heap_copy_minimal_tuple(), MemoryContextSwitchTo(), TuplesortPublic::sortcontext, TuplesortPublic::sortKeys, SortTuple::tuple, tuplesort_gettuple_common(), and TuplesortstateGetPublic.

Referenced by ExecIncrementalSort(), ExecSort(), fetch_input_tuple(), hypothetical_dense_rank_final(), hypothetical_rank_common(), process_ordered_aggregate_multi(), and switchToPresortedPrefixMode().

◆ tuplesort_initialize_shared()

void tuplesort_initialize_shared ( Sharedsort shared,
int  nWorkers,
dsm_segment seg 
)
extern

Definition at line 2921 of file tuplesort.c.

2922{
2923 int i;
2924
2925 Assert(nWorkers > 0);
2926
2927 SpinLockInit(&shared->mutex);
2928 shared->currentWorker = 0;
2929 shared->workersFinished = 0;
2930 SharedFileSetInit(&shared->fileset, seg);
2931 shared->nTapes = nWorkers;
2932 for (i = 0; i < nWorkers; i++)
2933 {
2934 shared->tapes[i].firstblocknumber = 0L;
2935 }
2936}
void SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg)
#define SpinLockInit(lock)
Definition spin.h:57
TapeShare tapes[FLEXIBLE_ARRAY_MEMBER]
Definition tuplesort.c:366
int workersFinished
Definition tuplesort.c:354
slock_t mutex
Definition tuplesort.c:343
int currentWorker
Definition tuplesort.c:353
int64 firstblocknumber
Definition logtape.h:54

References Assert, Sharedsort::currentWorker, fb(), Sharedsort::fileset, TapeShare::firstblocknumber, i, Sharedsort::mutex, Sharedsort::nTapes, SharedFileSetInit(), SpinLockInit, Sharedsort::tapes, and Sharedsort::workersFinished.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), and _gin_begin_parallel().

◆ tuplesort_markpos()

void tuplesort_markpos ( Tuplesortstate state)
extern

Definition at line 2420 of file tuplesort.c.

2421{
2422 MemoryContext oldcontext = MemoryContextSwitchTo(state->base.sortcontext);
2423
2424 Assert(state->base.sortopt & TUPLESORT_RANDOMACCESS);
2425
2426 switch (state->status)
2427 {
2428 case TSS_SORTEDINMEM:
2429 state->markpos_offset = state->current;
2430 state->markpos_eof = state->eof_reached;
2431 break;
2432 case TSS_SORTEDONTAPE:
2433 LogicalTapeTell(state->result_tape,
2434 &state->markpos_block,
2435 &state->markpos_offset);
2436 state->markpos_eof = state->eof_reached;
2437 break;
2438 default:
2439 elog(ERROR, "invalid tuplesort state");
2440 break;
2441 }
2442
2443 MemoryContextSwitchTo(oldcontext);
2444}
void LogicalTapeTell(LogicalTape *lt, int64 *blocknum, int *offset)
Definition logtape.c:1162

References Assert, elog, ERROR, LogicalTapeTell(), MemoryContextSwitchTo(), TSS_SORTEDINMEM, TSS_SORTEDONTAPE, and TUPLESORT_RANDOMACCESS.

Referenced by ExecSortMarkPos().

◆ tuplesort_merge_order()

int tuplesort_merge_order ( int64  allowedMem)
extern

Definition at line 1763 of file tuplesort.c.

1764{
1765 int mOrder;
1766
1767 /*----------
1768 * In the merge phase, we need buffer space for each input and output tape.
1769 * Each pass in the balanced merge algorithm reads from M input tapes, and
1770 * writes to N output tapes. Each tape consumes TAPE_BUFFER_OVERHEAD bytes
1771 * of memory. In addition to that, we want MERGE_BUFFER_SIZE workspace per
1772 * input tape.
1773 *
1774 * totalMem = M * (TAPE_BUFFER_OVERHEAD + MERGE_BUFFER_SIZE) +
1775 * N * TAPE_BUFFER_OVERHEAD
1776 *
1777 * Except for the last and next-to-last merge passes, where there can be
1778 * fewer tapes left to process, M = N. We choose M so that we have the
1779 * desired amount of memory available for the input buffers
1780 * (TAPE_BUFFER_OVERHEAD + MERGE_BUFFER_SIZE), given the total memory
1781 * available for the tape buffers (allowedMem).
1782 *
1783 * Note: you might be thinking we need to account for the memtuples[]
1784 * array in this calculation, but we effectively treat that as part of the
1785 * MERGE_BUFFER_SIZE workspace.
1786 *----------
1787 */
1788 mOrder = allowedMem /
1790
1791 /*
1792 * Even in minimum memory, use at least a MINORDER merge. On the other
1793 * hand, even when we have lots of memory, do not use more than a MAXORDER
1794 * merge. Tapes are pretty cheap, but they're not entirely free. Each
1795 * additional tape reduces the amount of memory available to build runs,
1796 * which in turn can cause the same sort to need more runs, which makes
1797 * merging slower even if it can still be done in a single pass. Also,
1798 * high order merges are quite slow due to CPU cache effects; it can be
1799 * faster to pay the I/O cost of a multi-pass merge than to perform a
1800 * single merge pass across many hundreds of tapes.
1801 */
1804
1805 return mOrder;
1806}
#define Min(x, y)
Definition c.h:997
#define TAPE_BUFFER_OVERHEAD
Definition tuplesort.c:176
#define MAXORDER
Definition tuplesort.c:175
#define MERGE_BUFFER_SIZE
Definition tuplesort.c:177
#define MINORDER
Definition tuplesort.c:174

References fb(), Max, MAXORDER, MERGE_BUFFER_SIZE, Min, MINORDER, and TAPE_BUFFER_OVERHEAD.

Referenced by cost_tuplesort(), and inittapes().

◆ tuplesort_method_name()

const char * tuplesort_method_name ( TuplesortMethod  m)
extern

Definition at line 2528 of file tuplesort.c.

2529{
2530 switch (m)
2531 {
2533 return "still in progress";
2535 return "top-N heapsort";
2537 return "quicksort";
2539 return "external sort";
2541 return "external merge";
2542 }
2543
2544 return "unknown";
2545}

References SORT_TYPE_EXTERNAL_MERGE, SORT_TYPE_EXTERNAL_SORT, SORT_TYPE_QUICKSORT, SORT_TYPE_STILL_IN_PROGRESS, and SORT_TYPE_TOP_N_HEAPSORT.

Referenced by show_incremental_sort_group_info(), and show_sort_info().

◆ tuplesort_performsort()

void tuplesort_performsort ( Tuplesortstate state)
extern

Definition at line 1348 of file tuplesort.c.

1349{
1350 MemoryContext oldcontext = MemoryContextSwitchTo(state->base.sortcontext);
1351
1352 if (trace_sort)
1353 elog(LOG, "performsort of worker %d starting: %s",
1354 state->worker, pg_rusage_show(&state->ru_start));
1355
1356 switch (state->status)
1357 {
1358 case TSS_INITIAL:
1359
1360 /*
1361 * We were able to accumulate all the tuples within the allowed
1362 * amount of memory, or leader to take over worker tapes
1363 */
1364 if (SERIAL(state))
1365 {
1366 /* Just qsort 'em and we're done */
1368 state->status = TSS_SORTEDINMEM;
1369 }
1370 else if (WORKER(state))
1371 {
1372 /*
1373 * Parallel workers must still dump out tuples to tape. No
1374 * merge is required to produce single output run, though.
1375 */
1376 inittapes(state, false);
1377 dumptuples(state, true);
1379 state->status = TSS_SORTEDONTAPE;
1380 }
1381 else
1382 {
1383 /*
1384 * Leader will take over worker tapes and merge worker runs.
1385 * Note that mergeruns sets the correct state->status.
1386 */
1389 }
1390 state->current = 0;
1391 state->eof_reached = false;
1392 state->markpos_block = 0L;
1393 state->markpos_offset = 0;
1394 state->markpos_eof = false;
1395 break;
1396
1397 case TSS_BOUNDED:
1398
1399 /*
1400 * We were able to accumulate all the tuples required for output
1401 * in memory, using a heap to eliminate excess tuples. Now we
1402 * have to transform the heap to a properly-sorted array. Note
1403 * that sort_bounded_heap sets the correct state->status.
1404 */
1406 state->current = 0;
1407 state->eof_reached = false;
1408 state->markpos_offset = 0;
1409 state->markpos_eof = false;
1410 break;
1411
1412 case TSS_BUILDRUNS:
1413
1414 /*
1415 * Finish tape-based sort. First, flush all tuples remaining in
1416 * memory out to tape; then merge until we have a single remaining
1417 * run (or, if !randomAccess and !WORKER(), one run per tape).
1418 * Note that mergeruns sets the correct state->status.
1419 */
1420 dumptuples(state, true);
1422 state->eof_reached = false;
1423 state->markpos_block = 0L;
1424 state->markpos_offset = 0;
1425 state->markpos_eof = false;
1426 break;
1427
1428 default:
1429 elog(ERROR, "invalid tuplesort state");
1430 break;
1431 }
1432
1433 if (trace_sort)
1434 {
1435 if (state->status == TSS_FINALMERGE)
1436 elog(LOG, "performsort of worker %d done (except %d-way final merge): %s",
1437 state->worker, state->nInputTapes,
1438 pg_rusage_show(&state->ru_start));
1439 else
1440 elog(LOG, "performsort of worker %d done: %s",
1441 state->worker, pg_rusage_show(&state->ru_start));
1442 }
1443
1444 MemoryContextSwitchTo(oldcontext);
1445}
const char * pg_rusage_show(const PGRUsage *ru0)
Definition pg_rusage.c:40
#define SERIAL(state)
Definition tuplesort.c:400
static void sort_bounded_heap(Tuplesortstate *state)
Definition tuplesort.c:2621
@ TSS_INITIAL
Definition tuplesort.c:154
@ TSS_BUILDRUNS
Definition tuplesort.c:156
@ TSS_BOUNDED
Definition tuplesort.c:155
static void leader_takeover_tapes(Tuplesortstate *state)
Definition tuplesort.c:3052
static void tuplesort_sort_memtuples(Tuplesortstate *state)
Definition tuplesort.c:2661
static void inittapes(Tuplesortstate *state, bool mergeruns)
Definition tuplesort.c:1850
static void worker_nomergeruns(Tuplesortstate *state)
Definition tuplesort.c:3030
static void mergeruns(Tuplesortstate *state)
Definition tuplesort.c:2002
static void dumptuples(Tuplesortstate *state, bool alltuples)
Definition tuplesort.c:2292

References dumptuples(), elog, ERROR, fb(), inittapes(), leader_takeover_tapes(), LOG, MemoryContextSwitchTo(), mergeruns(), pg_rusage_show(), SERIAL, sort_bounded_heap(), trace_sort, TSS_BOUNDED, TSS_BUILDRUNS, TSS_FINALMERGE, TSS_INITIAL, TSS_SORTEDINMEM, TSS_SORTEDONTAPE, tuplesort_sort_memtuples(), WORKER, and worker_nomergeruns().

Referenced by _brin_parallel_merge(), _brin_parallel_scan_and_build(), _bt_leafbuild(), _bt_parallel_scan_and_sort(), _gin_parallel_merge(), _gin_parallel_scan_and_build(), _gin_process_worker_data(), _h_indexbuild(), array_sort_internal(), ExecIncrementalSort(), ExecSort(), gistbuild(), heapam_relation_copy_for_cluster(), hypothetical_dense_rank_final(), hypothetical_rank_common(), initialize_phase(), mode_final(), percentile_cont_final_common(), percentile_cont_multi_final_common(), percentile_disc_final(), percentile_disc_multi_final(), process_ordered_aggregate_multi(), process_ordered_aggregate_single(), switchToPresortedPrefixMode(), and validate_index().

◆ tuplesort_putbrintuple()

void tuplesort_putbrintuple ( Tuplesortstate state,
BrinTuple tuple,
Size  size 
)
extern

Definition at line 871 of file tuplesortvariants.c.

872{
877 Size tuplen;
878
879 /* allocate space for the whole BRIN sort tuple */
881
882 bstup->tuplen = size;
883 memcpy(&bstup->tuple, tuple, size);
884
885 stup.tuple = bstup;
886 stup.datum1 = UInt32GetDatum(tuple->bt_blkno);
887 stup.isnull1 = false;
888
889 /* GetMemoryChunkSpace is not supported for bump contexts */
891 tuplen = MAXALIGN(BRINSORTTUPLE_SIZE(size));
892 else
893 tuplen = GetMemoryChunkSpace(bstup);
894
896 base->sortKeys &&
897 base->sortKeys->abbrev_converter &&
898 !stup.isnull1, tuplen);
899
900 MemoryContextSwitchTo(oldcontext);
901}
Size GetMemoryChunkSpace(void *pointer)
Definition mcxt.c:770
void * palloc(Size size)
Definition mcxt.c:1387
static Datum UInt32GetDatum(uint32 X)
Definition postgres.h:242
BlockNumber bt_blkno
Definition brin_tuple.h:66
MemoryContext tuplecontext
Definition tuplesort.h:188
void tuplesort_puttuple_common(Tuplesortstate *state, SortTuple *tuple, bool useAbbrev, Size tuplen)
Definition tuplesort.c:1154
#define TupleSortUseBumpTupleCxt(opt)
Definition tuplesort.h:82
#define BRINSORTTUPLE_SIZE(len)

References SortSupportData::abbrev_converter, BRINSORTTUPLE_SIZE, BrinTuple::bt_blkno, fb(), GetMemoryChunkSpace(), MAXALIGN, MemoryContextSwitchTo(), palloc(), TuplesortPublic::sortKeys, TuplesortPublic::sortopt, TuplesortPublic::tuplecontext, tuplesort_puttuple_common(), TuplesortstateGetPublic, TupleSortUseBumpTupleCxt, and UInt32GetDatum().

Referenced by form_and_spill_tuple().

◆ tuplesort_putdatum()

void tuplesort_putdatum ( Tuplesortstate state,
Datum  val,
bool  isNull 
)
extern

Definition at line 940 of file tuplesortvariants.c.

941{
946
947 /*
948 * Pass-by-value types or null values are just stored directly in
949 * stup.datum1 (and stup.tuple is not used and set to NULL).
950 *
951 * Non-null pass-by-reference values need to be copied into memory we
952 * control, and possibly abbreviated. The copied value is pointed to by
953 * stup.tuple and is treated as the canonical copy (e.g. to return via
954 * tuplesort_getdatum or when writing to tape); stup.datum1 gets the
955 * abbreviated value if abbreviation is happening, otherwise it's
956 * identical to stup.tuple.
957 */
958
959 if (isNull || !base->tuples)
960 {
961 /*
962 * Set datum1 to zeroed representation for NULLs (to be consistent,
963 * and to support cheap inequality tests for NULL abbreviated keys).
964 */
965 stup.datum1 = !isNull ? val : (Datum) 0;
966 stup.isnull1 = isNull;
967 stup.tuple = NULL; /* no separate storage */
968 }
969 else
970 {
971 stup.isnull1 = false;
972 stup.datum1 = datumCopy(val, false, arg->datumTypeLen);
973 stup.tuple = DatumGetPointer(stup.datum1);
974 }
975
977 base->tuples &&
978 base->sortKeys->abbrev_converter && !isNull, 0);
979
980 MemoryContextSwitchTo(oldcontext);
981}
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
Datum datum1
Definition tuplesort.h:117

References SortSupportData::abbrev_converter, arg, TuplesortPublic::arg, SortTuple::datum1, datumCopy(), DatumGetPointer(), fb(), MemoryContextSwitchTo(), TuplesortPublic::sortKeys, TuplesortPublic::tuplecontext, TuplesortPublic::tuples, tuplesort_puttuple_common(), TuplesortstateGetPublic, and val.

Referenced by array_sort_internal(), ExecEvalAggOrderedTransDatum(), ExecSort(), ordered_set_transition(), and validate_index_callback().

◆ tuplesort_putgintuple()

void tuplesort_putgintuple ( Tuplesortstate state,
GinTuple tuple,
Size  size 
)
extern

Definition at line 904 of file tuplesortvariants.c.

905{
907 GinTuple *ctup;
910 Size tuplen;
911
912 /* copy the GinTuple into the right memory context */
913 ctup = palloc(size);
914 memcpy(ctup, tuple, size);
915
916 stup.tuple = ctup;
917 stup.datum1 = (Datum) 0;
918 stup.isnull1 = false;
919
920 /* GetMemoryChunkSpace is not supported for bump contexts */
922 tuplen = MAXALIGN(size);
923 else
924 tuplen = GetMemoryChunkSpace(ctup);
925
927 base->sortKeys &&
928 base->sortKeys->abbrev_converter &&
929 !stup.isnull1, tuplen);
930
931 MemoryContextSwitchTo(oldcontext);
932}

References SortSupportData::abbrev_converter, fb(), GetMemoryChunkSpace(), MAXALIGN, MemoryContextSwitchTo(), palloc(), TuplesortPublic::sortKeys, TuplesortPublic::sortopt, TuplesortPublic::tuplecontext, tuplesort_puttuple_common(), TuplesortstateGetPublic, and TupleSortUseBumpTupleCxt.

Referenced by _gin_process_worker_data(), and ginFlushBuildState().

◆ tuplesort_putheaptuple()

void tuplesort_putheaptuple ( Tuplesortstate state,
HeapTuple  tup 
)
extern

Definition at line 792 of file tuplesortvariants.c.

793{
798 Size tuplen;
799
800 /* copy the tuple into sort storage */
802 stup.tuple = tup;
803
804 /*
805 * set up first-column key value, and potentially abbreviate, if it's a
806 * simple column
807 */
808 if (base->haveDatum1)
809 {
810 stup.datum1 = heap_getattr(tup,
811 arg->indexInfo->ii_IndexAttrNumbers[0],
812 arg->tupDesc,
813 &stup.isnull1);
814 }
815
816 /* GetMemoryChunkSpace is not supported for bump contexts */
817 if (TupleSortUseBumpTupleCxt(base->sortopt))
818 tuplen = MAXALIGN(HEAPTUPLESIZE + tup->t_len);
819 else
820 tuplen = GetMemoryChunkSpace(tup);
821
823 base->haveDatum1 &&
824 base->sortKeys->abbrev_converter &&
825 !stup.isnull1, tuplen);
826
827 MemoryContextSwitchTo(oldcontext);
828}
HeapTuple heap_copytuple(HeapTuple tuple)
Definition heaptuple.c:778
#define HEAPTUPLESIZE
Definition htup.h:73
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)

References SortSupportData::abbrev_converter, arg, TuplesortPublic::arg, fb(), GetMemoryChunkSpace(), TuplesortPublic::haveDatum1, heap_copytuple(), heap_getattr(), HEAPTUPLESIZE, MAXALIGN, MemoryContextSwitchTo(), TuplesortPublic::sortKeys, TuplesortPublic::sortopt, TuplesortPublic::tuplecontext, tuplesort_puttuple_common(), TuplesortstateGetPublic, and TupleSortUseBumpTupleCxt.

Referenced by heapam_relation_copy_for_cluster().

◆ tuplesort_putindextuplevalues()

void tuplesort_putindextuplevalues ( Tuplesortstate state,
Relation  rel,
const ItemPointerData self,
const Datum values,
const bool isnull 
)
extern

Definition at line 835 of file tuplesortvariants.c.

838{
840 IndexTuple tuple;
843 Size tuplen;
844
846 isnull, base->tuplecontext);
847 tuple = ((IndexTuple) stup.tuple);
848 tuple->t_tid = *self;
849 /* set up first-column key value */
850 stup.datum1 = index_getattr(tuple,
851 1,
852 RelationGetDescr(arg->indexRel),
853 &stup.isnull1);
854
855 /* GetMemoryChunkSpace is not supported for bump contexts */
856 if (TupleSortUseBumpTupleCxt(base->sortopt))
857 tuplen = MAXALIGN(tuple->t_info & INDEX_SIZE_MASK);
858 else
859 tuplen = GetMemoryChunkSpace(tuple);
860
862 base->sortKeys &&
863 base->sortKeys->abbrev_converter &&
864 !stup.isnull1, tuplen);
865}
static Datum values[MAXATTR]
Definition bootstrap.c:155
IndexTuple index_form_tuple_context(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull, MemoryContext context)
Definition indextuple.c:65
IndexTupleData * IndexTuple
Definition itup.h:53
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition itup.h:131
#define INDEX_SIZE_MASK
Definition itup.h:65

References SortSupportData::abbrev_converter, arg, TuplesortPublic::arg, fb(), GetMemoryChunkSpace(), index_form_tuple_context(), index_getattr(), INDEX_SIZE_MASK, MAXALIGN, RelationGetDescr, TuplesortPublic::sortKeys, TuplesortPublic::sortopt, IndexTupleData::t_info, IndexTupleData::t_tid, TuplesortPublic::tuplecontext, tuplesort_puttuple_common(), TuplesortstateGetPublic, TupleSortUseBumpTupleCxt, and values.

Referenced by _bt_spool(), _h_spool(), and gistSortedBuildCallback().

◆ tuplesort_puttuple_common()

void tuplesort_puttuple_common ( Tuplesortstate state,
SortTuple tuple,
bool  useAbbrev,
Size  tuplen 
)
extern

Definition at line 1154 of file tuplesort.c.

1156{
1157 MemoryContext oldcontext = MemoryContextSwitchTo(state->base.sortcontext);
1158
1159 Assert(!LEADER(state));
1160
1161 /* account for the memory used for this tuple */
1162 USEMEM(state, tuplen);
1163 state->tupleMem += tuplen;
1164
1165 if (!useAbbrev)
1166 {
1167 /*
1168 * Leave ordinary Datum representation, or NULL value. If there is a
1169 * converter it won't expect NULL values, and cost model is not
1170 * required to account for NULL, so in that case we avoid calling
1171 * converter and just set datum1 to zeroed representation (to be
1172 * consistent, and to support cheap inequality tests for NULL
1173 * abbreviated keys).
1174 */
1175 }
1176 else if (!consider_abort_common(state))
1177 {
1178 /* Store abbreviated key representation */
1179 tuple->datum1 = state->base.sortKeys->abbrev_converter(tuple->datum1,
1180 state->base.sortKeys);
1181 }
1182 else
1183 {
1184 /*
1185 * Set state to be consistent with never trying abbreviation.
1186 *
1187 * Alter datum1 representation in already-copied tuples, so as to
1188 * ensure a consistent representation (current tuple was just
1189 * handled). It does not matter if some dumped tuples are already
1190 * sorted on tape, since serialized tuples lack abbreviated keys
1191 * (TSS_BUILDRUNS state prevents control reaching here in any case).
1192 */
1193 REMOVEABBREV(state, state->memtuples, state->memtupcount);
1194 }
1195
1196 switch (state->status)
1197 {
1198 case TSS_INITIAL:
1199
1200 /*
1201 * Save the tuple into the unsorted array. First, grow the array
1202 * as needed. Note that we try to grow the array when there is
1203 * still one free slot remaining --- if we fail, there'll still be
1204 * room to store the incoming tuple, and then we'll switch to
1205 * tape-based operation.
1206 */
1207 if (state->memtupcount >= state->memtupsize - 1)
1208 {
1210 Assert(state->memtupcount < state->memtupsize);
1211 }
1212 state->memtuples[state->memtupcount++] = *tuple;
1213
1214 /*
1215 * Check if it's time to switch over to a bounded heapsort. We do
1216 * so if the input tuple count exceeds twice the desired tuple
1217 * count (this is a heuristic for where heapsort becomes cheaper
1218 * than a quicksort), or if we've just filled workMem and have
1219 * enough tuples to meet the bound.
1220 *
1221 * Note that once we enter TSS_BOUNDED state we will always try to
1222 * complete the sort that way. In the worst case, if later input
1223 * tuples are larger than earlier ones, this might cause us to
1224 * exceed workMem significantly.
1225 */
1226 if (state->bounded &&
1227 (state->memtupcount > state->bound * 2 ||
1228 (state->memtupcount > state->bound && LACKMEM(state))))
1229 {
1230 if (trace_sort)
1231 elog(LOG, "switching to bounded heapsort at %d tuples: %s",
1232 state->memtupcount,
1233 pg_rusage_show(&state->ru_start));
1235 MemoryContextSwitchTo(oldcontext);
1236 return;
1237 }
1238
1239 /*
1240 * Done if we still fit in available memory and have array slots.
1241 */
1242 if (state->memtupcount < state->memtupsize && !LACKMEM(state))
1243 {
1244 MemoryContextSwitchTo(oldcontext);
1245 return;
1246 }
1247
1248 /*
1249 * Nope; time to switch to tape-based operation.
1250 */
1251 inittapes(state, true);
1252
1253 /*
1254 * Dump all tuples.
1255 */
1256 dumptuples(state, false);
1257 break;
1258
1259 case TSS_BOUNDED:
1260
1261 /*
1262 * We don't want to grow the array here, so check whether the new
1263 * tuple can be discarded before putting it in. This should be a
1264 * good speed optimization, too, since when there are many more
1265 * input tuples than the bound, most input tuples can be discarded
1266 * with just this one comparison. Note that because we currently
1267 * have the sort direction reversed, we must check for <= not >=.
1268 */
1269 if (COMPARETUP(state, tuple, &state->memtuples[0]) <= 0)
1270 {
1271 /* new tuple <= top of the heap, so we can discard it */
1272 free_sort_tuple(state, tuple);
1274 }
1275 else
1276 {
1277 /* discard top of heap, replacing it with the new tuple */
1278 free_sort_tuple(state, &state->memtuples[0]);
1280 }
1281 break;
1282
1283 case TSS_BUILDRUNS:
1284
1285 /*
1286 * Save the tuple into the unsorted array (there must be space)
1287 */
1288 state->memtuples[state->memtupcount++] = *tuple;
1289
1290 /*
1291 * If we are over the memory limit, dump all tuples.
1292 */
1293 dumptuples(state, false);
1294 break;
1295
1296 default:
1297 elog(ERROR, "invalid tuplesort state");
1298 break;
1299 }
1300 MemoryContextSwitchTo(oldcontext);
1301}
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define COMPARETUP(state, a, b)
Definition tuplesort.c:393
static void free_sort_tuple(Tuplesortstate *state, SortTuple *stup)
Definition tuplesort.c:3111
#define REMOVEABBREV(state, stup, count)
Definition tuplesort.c:392
#define LACKMEM(state)
Definition tuplesort.c:397
#define USEMEM(state, amt)
Definition tuplesort.c:398
static bool grow_memtuples(Tuplesortstate *state)
Definition tuplesort.c:1037
static void make_bounded_heap(Tuplesortstate *state)
Definition tuplesort.c:2572
#define LEADER(state)
Definition tuplesort.c:402
static bool consider_abort_common(Tuplesortstate *state)
Definition tuplesort.c:1304

References Assert, CHECK_FOR_INTERRUPTS, COMPARETUP, consider_abort_common(), SortTuple::datum1, dumptuples(), elog, ERROR, fb(), free_sort_tuple(), grow_memtuples(), inittapes(), LACKMEM, LEADER, LOG, make_bounded_heap(), MemoryContextSwitchTo(), pg_rusage_show(), REMOVEABBREV, trace_sort, TSS_BOUNDED, TSS_BUILDRUNS, TSS_INITIAL, tuplesort_heap_replace_top(), and USEMEM.

Referenced by tuplesort_putbrintuple(), tuplesort_putdatum(), tuplesort_putgintuple(), tuplesort_putheaptuple(), tuplesort_putindextuplevalues(), and tuplesort_puttupleslot().

◆ tuplesort_puttupleslot()

void tuplesort_puttupleslot ( Tuplesortstate state,
TupleTableSlot slot 
)
extern

Definition at line 752 of file tuplesortvariants.c.

753{
756 TupleDesc tupDesc = (TupleDesc) base->arg;
758 MinimalTuple tuple;
759 HeapTupleData htup;
760 Size tuplen;
761
762 /* copy the tuple into sort storage */
763 tuple = ExecCopySlotMinimalTuple(slot);
764 stup.tuple = tuple;
765 /* set up first-column key value */
766 htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET;
767 htup.t_data = (HeapTupleHeader) ((char *) tuple - MINIMAL_TUPLE_OFFSET);
768 stup.datum1 = heap_getattr(&htup,
769 base->sortKeys[0].ssup_attno,
770 tupDesc,
771 &stup.isnull1);
772
773 /* GetMemoryChunkSpace is not supported for bump contexts */
774 if (TupleSortUseBumpTupleCxt(base->sortopt))
775 tuplen = MAXALIGN(tuple->t_len);
776 else
777 tuplen = GetMemoryChunkSpace(tuple);
778
780 base->sortKeys->abbrev_converter &&
781 !stup.isnull1, tuplen);
782
783 MemoryContextSwitchTo(oldcontext);
784}
HeapTupleHeaderData * HeapTupleHeader
Definition htup.h:23
#define MINIMAL_TUPLE_OFFSET
uint32 t_len
Definition htup.h:64
HeapTupleHeader t_data
Definition htup.h:68
struct TupleDescData * TupleDesc
Definition tupdesc.h:145
static MinimalTuple ExecCopySlotMinimalTuple(TupleTableSlot *slot)
Definition tuptable.h:495

References SortSupportData::abbrev_converter, TuplesortPublic::arg, ExecCopySlotMinimalTuple(), fb(), GetMemoryChunkSpace(), heap_getattr(), MAXALIGN, MemoryContextSwitchTo(), MINIMAL_TUPLE_OFFSET, TuplesortPublic::sortKeys, TuplesortPublic::sortopt, SortSupportData::ssup_attno, HeapTupleData::t_data, HeapTupleData::t_len, MinimalTupleData::t_len, TuplesortPublic::tuplecontext, tuplesort_puttuple_common(), TuplesortstateGetPublic, and TupleSortUseBumpTupleCxt.

Referenced by ExecEvalAggOrderedTransTuple(), ExecIncrementalSort(), ExecSort(), fetch_input_tuple(), hypothetical_dense_rank_final(), hypothetical_rank_common(), ordered_set_transition_multi(), and switchToPresortedPrefixMode().

◆ tuplesort_readtup_alloc()

void * tuplesort_readtup_alloc ( Tuplesortstate state,
Size  tuplen 
)
extern

Definition at line 2866 of file tuplesort.c.

2867{
2868 SlabSlot *buf;
2869
2870 /*
2871 * We pre-allocate enough slots in the slab arena that we should never run
2872 * out.
2873 */
2874 Assert(state->slabFreeHead);
2875
2876 if (tuplen > SLAB_SLOT_SIZE || !state->slabFreeHead)
2877 return MemoryContextAlloc(state->base.sortcontext, tuplen);
2878 else
2879 {
2880 buf = state->slabFreeHead;
2881 /* Reuse this slot */
2882 state->slabFreeHead = buf->nextfree;
2883
2884 return buf;
2885 }
2886}
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define SLAB_SLOT_SIZE
Definition tuplesort.c:140

References Assert, buf, MemoryContextAlloc(), and SLAB_SLOT_SIZE.

Referenced by readtup_cluster(), readtup_datum(), readtup_heap(), readtup_index(), readtup_index_brin(), and readtup_index_gin().

◆ tuplesort_rescan()

void tuplesort_rescan ( Tuplesortstate state)
extern

Definition at line 2387 of file tuplesort.c.

2388{
2389 MemoryContext oldcontext = MemoryContextSwitchTo(state->base.sortcontext);
2390
2391 Assert(state->base.sortopt & TUPLESORT_RANDOMACCESS);
2392
2393 switch (state->status)
2394 {
2395 case TSS_SORTEDINMEM:
2396 state->current = 0;
2397 state->eof_reached = false;
2398 state->markpos_offset = 0;
2399 state->markpos_eof = false;
2400 break;
2401 case TSS_SORTEDONTAPE:
2402 LogicalTapeRewindForRead(state->result_tape, 0);
2403 state->eof_reached = false;
2404 state->markpos_block = 0L;
2405 state->markpos_offset = 0;
2406 state->markpos_eof = false;
2407 break;
2408 default:
2409 elog(ERROR, "invalid tuplesort state");
2410 break;
2411 }
2412
2413 MemoryContextSwitchTo(oldcontext);
2414}
void LogicalTapeRewindForRead(LogicalTape *lt, size_t buffer_size)
Definition logtape.c:846

References Assert, elog, ERROR, fb(), LogicalTapeRewindForRead(), MemoryContextSwitchTo(), TSS_SORTEDINMEM, TSS_SORTEDONTAPE, and TUPLESORT_RANDOMACCESS.

Referenced by ExecReScanSort(), mode_final(), percentile_cont_final_common(), percentile_cont_multi_final_common(), percentile_disc_final(), and percentile_disc_multi_final().

◆ tuplesort_reset()

void tuplesort_reset ( Tuplesortstate state)
extern

Definition at line 1004 of file tuplesort.c.

1005{
1008
1009 /*
1010 * After we've freed up per-batch memory, re-setup all of the state common
1011 * to both the first batch and any subsequent batch.
1012 */
1014
1015 state->lastReturnedTuple = NULL;
1016 state->slabMemoryBegin = NULL;
1017 state->slabMemoryEnd = NULL;
1018 state->slabFreeHead = NULL;
1019}

References fb(), tuplesort_begin_batch(), tuplesort_free(), and tuplesort_updatemax().

Referenced by ExecIncrementalSort(), ExecReScanIncrementalSort(), and switchToPresortedPrefixMode().

◆ tuplesort_restorepos()

void tuplesort_restorepos ( Tuplesortstate state)
extern

Definition at line 2451 of file tuplesort.c.

2452{
2453 MemoryContext oldcontext = MemoryContextSwitchTo(state->base.sortcontext);
2454
2455 Assert(state->base.sortopt & TUPLESORT_RANDOMACCESS);
2456
2457 switch (state->status)
2458 {
2459 case TSS_SORTEDINMEM:
2460 state->current = state->markpos_offset;
2461 state->eof_reached = state->markpos_eof;
2462 break;
2463 case TSS_SORTEDONTAPE:
2464 LogicalTapeSeek(state->result_tape,
2465 state->markpos_block,
2466 state->markpos_offset);
2467 state->eof_reached = state->markpos_eof;
2468 break;
2469 default:
2470 elog(ERROR, "invalid tuplesort state");
2471 break;
2472 }
2473
2474 MemoryContextSwitchTo(oldcontext);
2475}
void LogicalTapeSeek(LogicalTape *lt, int64 blocknum, int offset)
Definition logtape.c:1133

References Assert, elog, ERROR, LogicalTapeSeek(), MemoryContextSwitchTo(), TSS_SORTEDINMEM, TSS_SORTEDONTAPE, and TUPLESORT_RANDOMACCESS.

Referenced by ExecSortRestrPos().

◆ tuplesort_set_bound()

void tuplesort_set_bound ( Tuplesortstate state,
int64  bound 
)
extern

Definition at line 823 of file tuplesort.c.

824{
825 /* Assert we're called before loading any tuples */
826 Assert(state->status == TSS_INITIAL && state->memtupcount == 0);
827 /* Assert we allow bounded sorts */
828 Assert(state->base.sortopt & TUPLESORT_ALLOWBOUNDED);
829 /* Can't set the bound twice, either */
830 Assert(!state->bounded);
831 /* Also, this shouldn't be called in a parallel worker */
833
834 /* Parallel leader allows but ignores hint */
835 if (LEADER(state))
836 return;
837
838#ifdef DEBUG_BOUNDED_SORT
839 /* Honor GUC setting that disables the feature (for easy testing) */
841 return;
842#endif
843
844 /* We want to be able to compute bound * 2, so limit the setting */
845 if (bound > (int64) (INT_MAX / 2))
846 return;
847
848 state->bounded = true;
849 state->bound = (int) bound;
850
851 /*
852 * Bounded sorts are not an effective target for abbreviated key
853 * optimization. Disable by setting state to be consistent with no
854 * abbreviation support.
855 */
856 state->base.sortKeys->abbrev_converter = NULL;
857 if (state->base.sortKeys->abbrev_full_comparator)
858 state->base.sortKeys->comparator = state->base.sortKeys->abbrev_full_comparator;
859
860 /* Not strictly necessary, but be tidy */
861 state->base.sortKeys->abbrev_abort = NULL;
862 state->base.sortKeys->abbrev_full_comparator = NULL;
863}
#define TUPLESORT_ALLOWBOUNDED
Definition tuplesort.h:73

References Assert, fb(), LEADER, TSS_INITIAL, TUPLESORT_ALLOWBOUNDED, and WORKER.

Referenced by ExecIncrementalSort(), ExecSort(), and switchToPresortedPrefixMode().

◆ tuplesort_skiptuples()

bool tuplesort_skiptuples ( Tuplesortstate state,
int64  ntuples,
bool  forward 
)
extern

Definition at line 1695 of file tuplesort.c.

1696{
1697 MemoryContext oldcontext;
1698
1699 /*
1700 * We don't actually support backwards skip yet, because no callers need
1701 * it. The API is designed to allow for that later, though.
1702 */
1703 Assert(forward);
1704 Assert(ntuples >= 0);
1705 Assert(!WORKER(state));
1706
1707 switch (state->status)
1708 {
1709 case TSS_SORTEDINMEM:
1710 if (state->memtupcount - state->current >= ntuples)
1711 {
1712 state->current += ntuples;
1713 return true;
1714 }
1715 state->current = state->memtupcount;
1716 state->eof_reached = true;
1717
1718 /*
1719 * Complain if caller tries to retrieve more tuples than
1720 * originally asked for in a bounded sort. This is because
1721 * returning EOF here might be the wrong thing.
1722 */
1723 if (state->bounded && state->current >= state->bound)
1724 elog(ERROR, "retrieved too many tuples in a bounded sort");
1725
1726 return false;
1727
1728 case TSS_SORTEDONTAPE:
1729 case TSS_FINALMERGE:
1730
1731 /*
1732 * We could probably optimize these cases better, but for now it's
1733 * not worth the trouble.
1734 */
1735 oldcontext = MemoryContextSwitchTo(state->base.sortcontext);
1736 while (ntuples-- > 0)
1737 {
1739
1741 {
1742 MemoryContextSwitchTo(oldcontext);
1743 return false;
1744 }
1746 }
1747 MemoryContextSwitchTo(oldcontext);
1748 return true;
1749
1750 default:
1751 elog(ERROR, "invalid tuplesort state");
1752 return false; /* keep compiler quiet */
1753 }
1754}

References Assert, CHECK_FOR_INTERRUPTS, elog, ERROR, fb(), MemoryContextSwitchTo(), TSS_FINALMERGE, TSS_SORTEDINMEM, TSS_SORTEDONTAPE, tuplesort_gettuple_common(), and WORKER.

Referenced by percentile_cont_final_common(), percentile_cont_multi_final_common(), percentile_disc_final(), and percentile_disc_multi_final().

◆ tuplesort_space_type_name()

const char * tuplesort_space_type_name ( TuplesortSpaceType  t)
extern

Definition at line 2551 of file tuplesort.c.

2552{
2554 return t == SORT_SPACE_TYPE_DISK ? "Disk" : "Memory";
2555}

References Assert, SORT_SPACE_TYPE_DISK, and SORT_SPACE_TYPE_MEMORY.

Referenced by show_incremental_sort_group_info(), and show_sort_info().

◆ tuplesort_used_bound()

bool tuplesort_used_bound ( Tuplesortstate state)
extern

Definition at line 871 of file tuplesort.c.

872{
873 return state->boundUsed;
874}

Referenced by ExecIncrementalSort().