PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tuplesort.h File Reference
#include "access/itup.h"
#include "executor/tuptable.h"
#include "fmgr.h"
#include "utils/relcache.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  TuplesortInstrumentation
 

Typedefs

typedef struct Tuplesortstate Tuplesortstate
 
typedef struct
TuplesortInstrumentation 
TuplesortInstrumentation
 

Enumerations

enum  TuplesortMethod {
  SORT_TYPE_STILL_IN_PROGRESS = 0, SORT_TYPE_TOP_N_HEAPSORT, SORT_TYPE_QUICKSORT, SORT_TYPE_EXTERNAL_SORT,
  SORT_TYPE_EXTERNAL_MERGE
}
 
enum  TuplesortSpaceType { SORT_SPACE_TYPE_DISK, SORT_SPACE_TYPE_MEMORY }
 

Functions

Tuplesortstatetuplesort_begin_heap (TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, bool randomAccess)
 
Tuplesortstatetuplesort_begin_cluster (TupleDesc tupDesc, Relation indexRel, int workMem, bool randomAccess)
 
Tuplesortstatetuplesort_begin_index_btree (Relation heapRel, Relation indexRel, bool enforceUnique, int workMem, bool randomAccess)
 
Tuplesortstatetuplesort_begin_index_hash (Relation heapRel, Relation indexRel, uint32 high_mask, uint32 low_mask, uint32 max_buckets, int workMem, bool randomAccess)
 
Tuplesortstatetuplesort_begin_datum (Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, bool randomAccess)
 
void tuplesort_set_bound (Tuplesortstate *state, int64 bound)
 
void tuplesort_puttupleslot (Tuplesortstate *state, TupleTableSlot *slot)
 
void tuplesort_putheaptuple (Tuplesortstate *state, HeapTuple tup)
 
void tuplesort_putindextuplevalues (Tuplesortstate *state, Relation rel, ItemPointer self, Datum *values, bool *isnull)
 
void tuplesort_putdatum (Tuplesortstate *state, Datum val, bool isNull)
 
void tuplesort_performsort (Tuplesortstate *state)
 
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)
 
bool tuplesort_getdatum (Tuplesortstate *state, bool forward, Datum *val, bool *isNull, Datum *abbrev)
 
bool tuplesort_skiptuples (Tuplesortstate *state, int64 ntuples, bool forward)
 
void tuplesort_end (Tuplesortstate *state)
 
void tuplesort_get_stats (Tuplesortstate *state, TuplesortInstrumentation *stats)
 
const char * tuplesort_method_name (TuplesortMethod m)
 
const char * tuplesort_space_type_name (TuplesortSpaceType t)
 
int tuplesort_merge_order (int64 allowedMem)
 
void tuplesort_rescan (Tuplesortstate *state)
 
void tuplesort_markpos (Tuplesortstate *state)
 
void tuplesort_restorepos (Tuplesortstate *state)
 

Typedef Documentation

Definition at line 32 of file tuplesort.h.

Enumeration Type Documentation

Enumerator
SORT_TYPE_STILL_IN_PROGRESS 
SORT_TYPE_TOP_N_HEAPSORT 
SORT_TYPE_QUICKSORT 
SORT_TYPE_EXTERNAL_SORT 
SORT_TYPE_EXTERNAL_MERGE 

Definition at line 39 of file tuplesort.h.

Enumerator
SORT_SPACE_TYPE_DISK 
SORT_SPACE_TYPE_MEMORY 

Definition at line 48 of file tuplesort.h.

Function Documentation

Tuplesortstate* tuplesort_begin_cluster ( TupleDesc  tupDesc,
Relation  indexRel,
int  workMem,
bool  randomAccess 
)

Definition at line 765 of file tuplesort.c.

References _bt_freeskey(), _bt_mkscankey_nodata(), SortSupportData::abbreviate, Tuplesortstate::abbrevNext, Assert, AssertState, BTGreaterStrategyNumber, BTLessStrategyNumber, BTREE_AM_OID, BuildIndexInfo(), CLUSTER_SORT, Tuplesortstate::comparetup, comparetup_cluster(), Tuplesortstate::copytup, copytup_cluster(), CreateExecutorState(), CurrentMemoryContext, ExprContext::ecxt_scantuple, elog, Tuplesortstate::estate, GetPerTupleExprContext, i, IndexInfo::ii_Expressions, Tuplesortstate::indexInfo, LOG, MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), Tuplesortstate::nKeys, palloc0(), PrepareSortSupportFromIndexRel(), RelationData::rd_rel, Tuplesortstate::readtup, readtup_cluster(), RelationGetNumberOfAttributes, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortSupportData::ssup_attno, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, trace_sort, Tuplesortstate::tupDesc, tuplesort_begin_common(), Tuplesortstate::writetup, and writetup_cluster().

Referenced by copy_heap_data().

768 {
769  Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
770  ScanKey indexScanKey;
771  MemoryContext oldcontext;
772  int i;
773 
774  Assert(indexRel->rd_rel->relam == BTREE_AM_OID);
775 
776  oldcontext = MemoryContextSwitchTo(state->sortcontext);
777 
778 #ifdef TRACE_SORT
779  if (trace_sort)
780  elog(LOG,
781  "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
783  workMem, randomAccess ? 't' : 'f');
784 #endif
785 
786  state->nKeys = RelationGetNumberOfAttributes(indexRel);
787 
788  TRACE_POSTGRESQL_SORT_START(CLUSTER_SORT,
789  false, /* no unique check */
790  state->nKeys,
791  workMem,
792  randomAccess);
793 
795  state->copytup = copytup_cluster;
796  state->writetup = writetup_cluster;
797  state->readtup = readtup_cluster;
798  state->abbrevNext = 10;
799 
800  state->indexInfo = BuildIndexInfo(indexRel);
801 
802  state->tupDesc = tupDesc; /* assume we need not copy tupDesc */
803 
804  indexScanKey = _bt_mkscankey_nodata(indexRel);
805 
806  if (state->indexInfo->ii_Expressions != NULL)
807  {
808  TupleTableSlot *slot;
809  ExprContext *econtext;
810 
811  /*
812  * We will need to use FormIndexDatum to evaluate the index
813  * expressions. To do that, we need an EState, as well as a
814  * TupleTableSlot to put the table tuples into. The econtext's
815  * scantuple has to point to that slot, too.
816  */
817  state->estate = CreateExecutorState();
818  slot = MakeSingleTupleTableSlot(tupDesc);
819  econtext = GetPerTupleExprContext(state->estate);
820  econtext->ecxt_scantuple = slot;
821  }
822 
823  /* Prepare SortSupport data for each column */
824  state->sortKeys = (SortSupport) palloc0(state->nKeys *
825  sizeof(SortSupportData));
826 
827  for (i = 0; i < state->nKeys; i++)
828  {
829  SortSupport sortKey = state->sortKeys + i;
830  ScanKey scanKey = indexScanKey + i;
831  int16 strategy;
832 
833  sortKey->ssup_cxt = CurrentMemoryContext;
834  sortKey->ssup_collation = scanKey->sk_collation;
835  sortKey->ssup_nulls_first =
836  (scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0;
837  sortKey->ssup_attno = scanKey->sk_attno;
838  /* Convey if abbreviation optimization is applicable in principle */
839  sortKey->abbreviate = (i == 0);
840 
841  AssertState(sortKey->ssup_attno != 0);
842 
843  strategy = (scanKey->sk_flags & SK_BT_DESC) != 0 ?
845 
846  PrepareSortSupportFromIndexRel(indexRel, strategy, sortKey);
847  }
848 
849  _bt_freeskey(indexScanKey);
850 
851  MemoryContextSwitchTo(oldcontext);
852 
853  return state;
854 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
signed short int16
Definition: c.h:245
bool ssup_nulls_first
Definition: sortsupport.h:75
ScanKey _bt_mkscankey_nodata(Relation rel)
Definition: nbtutils.c:115
static void writetup_cluster(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:3700
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define AssertState(condition)
Definition: c.h:684
int64 abbrevNext
Definition: tuplesort.c:397
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:422
EState * estate
Definition: tuplesort.c:405
SortSupport sortKeys
Definition: tuplesort.c:383
void _bt_freeskey(ScanKey skey)
Definition: nbtutils.c:155
#define BTREE_AM_OID
Definition: pg_am.h:70
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SortTupleComparator comparetup
Definition: tuplesort.c:247
#define CLUSTER_SORT
Definition: tuplesort.c:114
static int comparetup_cluster(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Definition: tuplesort.c:3516
IndexInfo * BuildIndexInfo(Relation index)
Definition: index.c:1642
void(* writetup)(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:265
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:114
static void copytup_cluster(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:3627
bool trace_sort
Definition: tuplesort.c:118
#define GetPerTupleExprContext(estate)
Definition: executor.h:477
static void readtup_cluster(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:3724
MemoryContext sortcontext
Definition: tuplesort.c:234
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void(* readtup)(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:273
IndexInfo * indexInfo
Definition: tuplesort.c:404
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:199
void PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy, SortSupport ssup)
Definition: sortsupport.c:160
EState * CreateExecutorState(void)
Definition: execUtils.c:80
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:428
void(* copytup)(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:255
void * palloc0(Size size)
Definition: mcxt.c:877
AttrNumber ssup_attno
Definition: sortsupport.h:81
int sk_flags
Definition: skey.h:66
List * ii_Expressions
Definition: execnodes.h:136
#define Assert(condition)
Definition: c.h:681
#define SK_BT_DESC
Definition: nbtree.h:427
Definition: regguts.h:298
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:197
Oid sk_collation
Definition: skey.h:70
int i
#define elog
Definition: elog.h:219
static Tuplesortstate * tuplesort_begin_common(int workMem, bool randomAccess)
Definition: tuplesort.c:607
#define BTLessStrategyNumber
Definition: stratnum.h:29
AttrNumber sk_attno
Definition: skey.h:67
TupleDesc tupDesc
Definition: tuplesort.c:382
Tuplesortstate* tuplesort_begin_datum ( Oid  datumType,
Oid  sortOperator,
Oid  sortCollation,
bool  nullsFirstFlag,
int  workMem,
bool  randomAccess 
)

Definition at line 975 of file tuplesort.c.

References SortSupportData::abbrev_converter, SortSupportData::abbreviate, Tuplesortstate::abbrevNext, Tuplesortstate::comparetup, comparetup_datum(), Tuplesortstate::copytup, copytup_datum(), CurrentMemoryContext, DATUM_SORT, Tuplesortstate::datumType, Tuplesortstate::datumTypeLen, elog, get_typlenbyval(), LOG, MemoryContextSwitchTo(), Tuplesortstate::nKeys, Tuplesortstate::onlyKey, palloc0(), PrepareSortSupportFromOrderingOp(), Tuplesortstate::readtup, readtup_datum(), Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, trace_sort, Tuplesortstate::tuples, tuplesort_begin_common(), Tuplesortstate::writetup, and writetup_datum().

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

978 {
979  Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
980  MemoryContext oldcontext;
981  int16 typlen;
982  bool typbyval;
983 
984  oldcontext = MemoryContextSwitchTo(state->sortcontext);
985 
986 #ifdef TRACE_SORT
987  if (trace_sort)
988  elog(LOG,
989  "begin datum sort: workMem = %d, randomAccess = %c",
990  workMem, randomAccess ? 't' : 'f');
991 #endif
992 
993  state->nKeys = 1; /* always a one-column sort */
994 
995  TRACE_POSTGRESQL_SORT_START(DATUM_SORT,
996  false, /* no unique check */
997  1,
998  workMem,
999  randomAccess);
1000 
1001  state->comparetup = comparetup_datum;
1002  state->copytup = copytup_datum;
1003  state->writetup = writetup_datum;
1004  state->readtup = readtup_datum;
1005  state->abbrevNext = 10;
1006 
1007  state->datumType = datumType;
1008 
1009  /* lookup necessary attributes of the datum type */
1010  get_typlenbyval(datumType, &typlen, &typbyval);
1011  state->datumTypeLen = typlen;
1012  state->tuples = !typbyval;
1013 
1014  /* Prepare SortSupport data */
1015  state->sortKeys = (SortSupport) palloc0(sizeof(SortSupportData));
1016 
1018  state->sortKeys->ssup_collation = sortCollation;
1019  state->sortKeys->ssup_nulls_first = nullsFirstFlag;
1020 
1021  /*
1022  * Abbreviation is possible here only for by-reference types. In theory,
1023  * a pass-by-value datatype could have an abbreviated form that is cheaper
1024  * to compare. In a tuple sort, we could support that, because we can
1025  * always extract the original datum from the tuple is needed. Here, we
1026  * can't, because a datum sort only stores a single copy of the datum; the
1027  * "tuple" field of each sortTuple is NULL.
1028  */
1029  state->sortKeys->abbreviate = !typbyval;
1030 
1031  PrepareSortSupportFromOrderingOp(sortOperator, state->sortKeys);
1032 
1033  /*
1034  * The "onlyKey" optimization cannot be used with abbreviated keys, since
1035  * tie-breaker comparisons may be required. Typically, the optimization
1036  * is only of value to pass-by-value types anyway, whereas abbreviated
1037  * keys are typically only of value to pass-by-reference types.
1038  */
1039  if (!state->sortKeys->abbrev_converter)
1040  state->onlyKey = state->sortKeys;
1041 
1042  MemoryContextSwitchTo(oldcontext);
1043 
1044  return state;
1045 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
signed short int16
Definition: c.h:245
bool ssup_nulls_first
Definition: sortsupport.h:75
int64 abbrevNext
Definition: tuplesort.c:397
SortSupport sortKeys
Definition: tuplesort.c:383
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:133
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SortTupleComparator comparetup
Definition: tuplesort.c:247
void(* writetup)(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:265
#define LOG
Definition: elog.h:26
bool trace_sort
Definition: tuplesort.c:118
#define DATUM_SORT
Definition: tuplesort.c:113
MemoryContext sortcontext
Definition: tuplesort.c:234
MemoryContext ssup_cxt
Definition: sortsupport.h:66
static int comparetup_datum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Definition: tuplesort.c:4055
static void copytup_datum(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:4076
static void readtup_datum(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:4124
void(* readtup)(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:273
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static void writetup_datum(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:4083
void(* copytup)(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:255
void * palloc0(Size size)
Definition: mcxt.c:877
Definition: regguts.h:298
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2001
SortSupport onlyKey
Definition: tuplesort.c:389
#define elog
Definition: elog.h:219
static Tuplesortstate * tuplesort_begin_common(int workMem, bool randomAccess)
Definition: tuplesort.c:607
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
Tuplesortstate* tuplesort_begin_heap ( TupleDesc  tupDesc,
int  nkeys,
AttrNumber attNums,
Oid sortOperators,
Oid sortCollations,
bool nullsFirstFlags,
int  workMem,
bool  randomAccess 
)

Definition at line 693 of file tuplesort.c.

References SortSupportData::abbrev_converter, SortSupportData::abbreviate, Tuplesortstate::abbrevNext, AssertArg, Tuplesortstate::comparetup, comparetup_heap(), Tuplesortstate::copytup, copytup_heap(), CurrentMemoryContext, elog, HEAP_SORT, i, LOG, MemoryContextSwitchTo(), Tuplesortstate::nKeys, Tuplesortstate::onlyKey, palloc0(), PrepareSortSupportFromOrderingOp(), Tuplesortstate::readtup, readtup_heap(), Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortSupportData::ssup_attno, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, trace_sort, Tuplesortstate::tupDesc, tuplesort_begin_common(), Tuplesortstate::writetup, and writetup_heap().

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

698 {
699  Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
700  MemoryContext oldcontext;
701  int i;
702 
703  oldcontext = MemoryContextSwitchTo(state->sortcontext);
704 
705  AssertArg(nkeys > 0);
706 
707 #ifdef TRACE_SORT
708  if (trace_sort)
709  elog(LOG,
710  "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
711  nkeys, workMem, randomAccess ? 't' : 'f');
712 #endif
713 
714  state->nKeys = nkeys;
715 
716  TRACE_POSTGRESQL_SORT_START(HEAP_SORT,
717  false, /* no unique check */
718  nkeys,
719  workMem,
720  randomAccess);
721 
722  state->comparetup = comparetup_heap;
723  state->copytup = copytup_heap;
724  state->writetup = writetup_heap;
725  state->readtup = readtup_heap;
726 
727  state->tupDesc = tupDesc; /* assume we need not copy tupDesc */
728  state->abbrevNext = 10;
729 
730  /* Prepare SortSupport data for each column */
731  state->sortKeys = (SortSupport) palloc0(nkeys * sizeof(SortSupportData));
732 
733  for (i = 0; i < nkeys; i++)
734  {
735  SortSupport sortKey = state->sortKeys + i;
736 
737  AssertArg(attNums[i] != 0);
738  AssertArg(sortOperators[i] != 0);
739 
740  sortKey->ssup_cxt = CurrentMemoryContext;
741  sortKey->ssup_collation = sortCollations[i];
742  sortKey->ssup_nulls_first = nullsFirstFlags[i];
743  sortKey->ssup_attno = attNums[i];
744  /* Convey if abbreviation optimization is applicable in principle */
745  sortKey->abbreviate = (i == 0);
746 
747  PrepareSortSupportFromOrderingOp(sortOperators[i], sortKey);
748  }
749 
750  /*
751  * The "onlyKey" optimization cannot be used with abbreviated keys, since
752  * tie-breaker comparisons may be required. Typically, the optimization
753  * is only of value to pass-by-value types anyway, whereas abbreviated
754  * keys are typically only of value to pass-by-reference types.
755  */
756  if (nkeys == 1 && !state->sortKeys->abbrev_converter)
757  state->onlyKey = state->sortKeys;
758 
759  MemoryContextSwitchTo(oldcontext);
760 
761  return state;
762 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
bool ssup_nulls_first
Definition: sortsupport.h:75
static int comparetup_heap(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Definition: tuplesort.c:3317
int64 abbrevNext
Definition: tuplesort.c:397
SortSupport sortKeys
Definition: tuplesort.c:383
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:133
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SortTupleComparator comparetup
Definition: tuplesort.c:247
void(* writetup)(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:265
#define LOG
Definition: elog.h:26
#define HEAP_SORT
Definition: tuplesort.c:111
bool trace_sort
Definition: tuplesort.c:118
MemoryContext sortcontext
Definition: tuplesort.c:234
static void writetup_heap(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:3457
MemoryContext ssup_cxt
Definition: sortsupport.h:66
void(* readtup)(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:273
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define AssertArg(condition)
Definition: c.h:683
static void copytup_heap(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:3379
void(* copytup)(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:255
void * palloc0(Size size)
Definition: mcxt.c:877
AttrNumber ssup_attno
Definition: sortsupport.h:81
Definition: regguts.h:298
static void readtup_heap(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:3484
int i
SortSupport onlyKey
Definition: tuplesort.c:389
#define elog
Definition: elog.h:219
static Tuplesortstate * tuplesort_begin_common(int workMem, bool randomAccess)
Definition: tuplesort.c:607
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
TupleDesc tupDesc
Definition: tuplesort.c:382
Tuplesortstate* tuplesort_begin_index_btree ( Relation  heapRel,
Relation  indexRel,
bool  enforceUnique,
int  workMem,
bool  randomAccess 
)

Definition at line 857 of file tuplesort.c.

References _bt_freeskey(), _bt_mkscankey_nodata(), SortSupportData::abbreviate, Tuplesortstate::abbrevNext, AssertState, BTGreaterStrategyNumber, BTLessStrategyNumber, Tuplesortstate::comparetup, comparetup_index_btree(), Tuplesortstate::copytup, copytup_index(), CurrentMemoryContext, elog, Tuplesortstate::enforceUnique, Tuplesortstate::heapRel, i, INDEX_SORT, Tuplesortstate::indexRel, LOG, MemoryContextSwitchTo(), Tuplesortstate::nKeys, palloc0(), PrepareSortSupportFromIndexRel(), Tuplesortstate::readtup, readtup_index(), RelationGetNumberOfAttributes, ScanKeyData::sk_attno, SK_BT_DESC, SK_BT_NULLS_FIRST, ScanKeyData::sk_collation, ScanKeyData::sk_flags, Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortSupportData::ssup_attno, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, trace_sort, tuplesort_begin_common(), Tuplesortstate::writetup, and writetup_index().

Referenced by _bt_spoolinit().

861 {
862  Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
863  ScanKey indexScanKey;
864  MemoryContext oldcontext;
865  int i;
866 
867  oldcontext = MemoryContextSwitchTo(state->sortcontext);
868 
869 #ifdef TRACE_SORT
870  if (trace_sort)
871  elog(LOG,
872  "begin index sort: unique = %c, workMem = %d, randomAccess = %c",
873  enforceUnique ? 't' : 'f',
874  workMem, randomAccess ? 't' : 'f');
875 #endif
876 
877  state->nKeys = RelationGetNumberOfAttributes(indexRel);
878 
879  TRACE_POSTGRESQL_SORT_START(INDEX_SORT,
880  enforceUnique,
881  state->nKeys,
882  workMem,
883  randomAccess);
884 
886  state->copytup = copytup_index;
887  state->writetup = writetup_index;
888  state->readtup = readtup_index;
889  state->abbrevNext = 10;
890 
891  state->heapRel = heapRel;
892  state->indexRel = indexRel;
893  state->enforceUnique = enforceUnique;
894 
895  indexScanKey = _bt_mkscankey_nodata(indexRel);
896  state->nKeys = RelationGetNumberOfAttributes(indexRel);
897 
898  /* Prepare SortSupport data for each column */
899  state->sortKeys = (SortSupport) palloc0(state->nKeys *
900  sizeof(SortSupportData));
901 
902  for (i = 0; i < state->nKeys; i++)
903  {
904  SortSupport sortKey = state->sortKeys + i;
905  ScanKey scanKey = indexScanKey + i;
906  int16 strategy;
907 
908  sortKey->ssup_cxt = CurrentMemoryContext;
909  sortKey->ssup_collation = scanKey->sk_collation;
910  sortKey->ssup_nulls_first =
911  (scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0;
912  sortKey->ssup_attno = scanKey->sk_attno;
913  /* Convey if abbreviation optimization is applicable in principle */
914  sortKey->abbreviate = (i == 0);
915 
916  AssertState(sortKey->ssup_attno != 0);
917 
918  strategy = (scanKey->sk_flags & SK_BT_DESC) != 0 ?
920 
921  PrepareSortSupportFromIndexRel(indexRel, strategy, sortKey);
922  }
923 
924  _bt_freeskey(indexScanKey);
925 
926  MemoryContextSwitchTo(oldcontext);
927 
928  return state;
929 }
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
signed short int16
Definition: c.h:245
bool ssup_nulls_first
Definition: sortsupport.h:75
ScanKey _bt_mkscankey_nodata(Relation rel)
Definition: nbtutils.c:115
Relation heapRel
Definition: tuplesort.c:411
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define AssertState(condition)
Definition: c.h:684
int64 abbrevNext
Definition: tuplesort.c:397
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:422
static void copytup_index(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:3943
SortSupport sortKeys
Definition: tuplesort.c:383
void _bt_freeskey(ScanKey skey)
Definition: nbtutils.c:155
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SortTupleComparator comparetup
Definition: tuplesort.c:247
#define INDEX_SORT
Definition: tuplesort.c:112
void(* writetup)(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:265
#define LOG
Definition: elog.h:26
static int comparetup_index_btree(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Definition: tuplesort.c:3762
bool trace_sort
Definition: tuplesort.c:118
MemoryContext sortcontext
Definition: tuplesort.c:234
static void writetup_index(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:4009
MemoryContext ssup_cxt
Definition: sortsupport.h:66
static void readtup_index(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:4031
void(* readtup)(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:273
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy, SortSupport ssup)
Definition: sortsupport.c:160
#define SK_BT_NULLS_FIRST
Definition: nbtree.h:428
void(* copytup)(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:255
void * palloc0(Size size)
Definition: mcxt.c:877
Relation indexRel
Definition: tuplesort.c:412
AttrNumber ssup_attno
Definition: sortsupport.h:81
int sk_flags
Definition: skey.h:66
#define SK_BT_DESC
Definition: nbtree.h:427
Definition: regguts.h:298
bool enforceUnique
Definition: tuplesort.c:415
Oid sk_collation
Definition: skey.h:70
int i
#define elog
Definition: elog.h:219
static Tuplesortstate * tuplesort_begin_common(int workMem, bool randomAccess)
Definition: tuplesort.c:607
#define BTLessStrategyNumber
Definition: stratnum.h:29
AttrNumber sk_attno
Definition: skey.h:67
Tuplesortstate* tuplesort_begin_index_hash ( Relation  heapRel,
Relation  indexRel,
uint32  high_mask,
uint32  low_mask,
uint32  max_buckets,
int  workMem,
bool  randomAccess 
)

Definition at line 932 of file tuplesort.c.

References Tuplesortstate::comparetup, comparetup_index_hash(), Tuplesortstate::copytup, copytup_index(), elog, Tuplesortstate::heapRel, Tuplesortstate::high_mask, Tuplesortstate::indexRel, LOG, Tuplesortstate::low_mask, Tuplesortstate::max_buckets, MemoryContextSwitchTo(), Tuplesortstate::nKeys, Tuplesortstate::readtup, readtup_index(), Tuplesortstate::sortcontext, trace_sort, tuplesort_begin_common(), Tuplesortstate::writetup, and writetup_index().

Referenced by _h_spoolinit().

938 {
939  Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
940  MemoryContext oldcontext;
941 
942  oldcontext = MemoryContextSwitchTo(state->sortcontext);
943 
944 #ifdef TRACE_SORT
945  if (trace_sort)
946  elog(LOG,
947  "begin index sort: high_mask = 0x%x, low_mask = 0x%x, "
948  "max_buckets = 0x%x, workMem = %d, randomAccess = %c",
949  high_mask,
950  low_mask,
951  max_buckets,
952  workMem, randomAccess ? 't' : 'f');
953 #endif
954 
955  state->nKeys = 1; /* Only one sort column, the hash code */
956 
958  state->copytup = copytup_index;
959  state->writetup = writetup_index;
960  state->readtup = readtup_index;
961 
962  state->heapRel = heapRel;
963  state->indexRel = indexRel;
964 
965  state->high_mask = high_mask;
966  state->low_mask = low_mask;
967  state->max_buckets = max_buckets;
968 
969  MemoryContextSwitchTo(oldcontext);
970 
971  return state;
972 }
static int comparetup_index_hash(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Definition: tuplesort.c:3891
Relation heapRel
Definition: tuplesort.c:411
static void copytup_index(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:3943
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SortTupleComparator comparetup
Definition: tuplesort.c:247
void(* writetup)(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:265
#define LOG
Definition: elog.h:26
bool trace_sort
Definition: tuplesort.c:118
MemoryContext sortcontext
Definition: tuplesort.c:234
static void writetup_index(Tuplesortstate *state, int tapenum, SortTuple *stup)
Definition: tuplesort.c:4009
uint32 high_mask
Definition: tuplesort.c:418
static void readtup_index(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:4031
void(* readtup)(Tuplesortstate *state, SortTuple *stup, int tapenum, unsigned int len)
Definition: tuplesort.c:273
void(* copytup)(Tuplesortstate *state, SortTuple *stup, void *tup)
Definition: tuplesort.c:255
Relation indexRel
Definition: tuplesort.c:412
Definition: regguts.h:298
uint32 max_buckets
Definition: tuplesort.c:420
uint32 low_mask
Definition: tuplesort.c:419
#define elog
Definition: elog.h:219
static Tuplesortstate * tuplesort_begin_common(int workMem, bool randomAccess)
Definition: tuplesort.c:607
void tuplesort_end ( Tuplesortstate state)

Definition at line 1104 of file tuplesort.c.

References Tuplesortstate::allowedMem, Tuplesortstate::availMem, ExprContext::ecxt_scantuple, elog, Tuplesortstate::estate, ExecDropSingleTupleTableSlot(), FreeExecutorState(), GetPerTupleExprContext, LOG, LogicalTapeSetBlocks(), LogicalTapeSetClose(), MemoryContextDelete(), MemoryContextSwitchTo(), pg_rusage_show(), Tuplesortstate::ru_start, Tuplesortstate::sortcontext, Tuplesortstate::tapeset, and trace_sort.

Referenced by _bt_spooldestroy(), _h_spooldestroy(), copy_heap_data(), ExecEndAgg(), ExecEndSort(), ExecReScanAgg(), ExecReScanSort(), initialize_aggregate(), initialize_phase(), ordered_set_shutdown(), process_ordered_aggregate_multi(), process_ordered_aggregate_single(), and validate_index().

1105 {
1106  /* context swap probably not needed, but let's be safe */
1107  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
1108 
1109 #ifdef TRACE_SORT
1110  long spaceUsed;
1111 
1112  if (state->tapeset)
1113  spaceUsed = LogicalTapeSetBlocks(state->tapeset);
1114  else
1115  spaceUsed = (state->allowedMem - state->availMem + 1023) / 1024;
1116 #endif
1117 
1118  /*
1119  * Delete temporary "tape" files, if any.
1120  *
1121  * Note: want to include this in reported total cost of sort, hence need
1122  * for two #ifdef TRACE_SORT sections.
1123  */
1124  if (state->tapeset)
1125  LogicalTapeSetClose(state->tapeset);
1126 
1127 #ifdef TRACE_SORT
1128  if (trace_sort)
1129  {
1130  if (state->tapeset)
1131  elog(LOG, "external sort ended, %ld disk blocks used: %s",
1132  spaceUsed, pg_rusage_show(&state->ru_start));
1133  else
1134  elog(LOG, "internal sort ended, %ld KB used: %s",
1135  spaceUsed, pg_rusage_show(&state->ru_start));
1136  }
1137 
1138  TRACE_POSTGRESQL_SORT_DONE(state->tapeset != NULL, spaceUsed);
1139 #else
1140 
1141  /*
1142  * If you disabled TRACE_SORT, you can still probe sort__done, but you
1143  * ain't getting space-used stats.
1144  */
1145  TRACE_POSTGRESQL_SORT_DONE(state->tapeset != NULL, 0L);
1146 #endif
1147 
1148  /* Free any execution state created for CLUSTER case */
1149  if (state->estate != NULL)
1150  {
1151  ExprContext *econtext = GetPerTupleExprContext(state->estate);
1152 
1154  FreeExecutorState(state->estate);
1155  }
1156 
1157  MemoryContextSwitchTo(oldcontext);
1158 
1159  /*
1160  * Free the per-sort memory context, thereby releasing all working memory,
1161  * including the Tuplesortstate struct itself.
1162  */
1164 }
int64 availMem
Definition: tuplesort.c:230
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
PGRUsage ru_start
Definition: tuplesort.c:434
EState * estate
Definition: tuplesort.c:405
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define LOG
Definition: elog.h:26
bool trace_sort
Definition: tuplesort.c:118
void FreeExecutorState(EState *estate)
Definition: execUtils.c:183
#define GetPerTupleExprContext(estate)
Definition: executor.h:477
MemoryContext sortcontext
Definition: tuplesort.c:234
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:216
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
LogicalTapeSet * tapeset
Definition: tuplesort.c:236
int64 allowedMem
Definition: tuplesort.c:231
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:197
void LogicalTapeSetClose(LogicalTapeSet *lts)
Definition: logtape.c:427
#define elog
Definition: elog.h:219
long LogicalTapeSetBlocks(LogicalTapeSet *lts)
Definition: logtape.c:889
void tuplesort_get_stats ( Tuplesortstate state,
TuplesortInstrumentation stats 
)

Definition at line 2941 of file tuplesort.c.

References Tuplesortstate::allowedMem, Tuplesortstate::availMem, Tuplesortstate::boundUsed, LogicalTapeSetBlocks(), 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, Tuplesortstate::status, Tuplesortstate::tapeset, TSS_FINALMERGE, TSS_SORTEDINMEM, and TSS_SORTEDONTAPE.

Referenced by ExecSort(), and show_sort_info().

2943 {
2944  /*
2945  * Note: it might seem we should provide both memory and disk usage for a
2946  * disk-based sort. However, the current code doesn't track memory space
2947  * accurately once we have begun to return tuples to the caller (since we
2948  * don't account for pfree's the caller is expected to do), so we cannot
2949  * rely on availMem in a disk sort. This does not seem worth the overhead
2950  * to fix. Is it worth creating an API for the memory context code to
2951  * tell us how much is actually used in sortcontext?
2952  */
2953  if (state->tapeset)
2954  {
2956  stats->spaceUsed = LogicalTapeSetBlocks(state->tapeset) * (BLCKSZ / 1024);
2957  }
2958  else
2959  {
2961  stats->spaceUsed = (state->allowedMem - state->availMem + 1023) / 1024;
2962  }
2963 
2964  switch (state->status)
2965  {
2966  case TSS_SORTEDINMEM:
2967  if (state->boundUsed)
2969  else
2971  break;
2972  case TSS_SORTEDONTAPE:
2974  break;
2975  case TSS_FINALMERGE:
2977  break;
2978  default:
2980  break;
2981  }
2982 }
int64 availMem
Definition: tuplesort.c:230
TupSortStatus status
Definition: tuplesort.c:222
TuplesortMethod sortMethod
Definition: tuplesort.h:56
LogicalTapeSet * tapeset
Definition: tuplesort.c:236
int64 allowedMem
Definition: tuplesort.c:231
TuplesortSpaceType spaceType
Definition: tuplesort.h:57
long LogicalTapeSetBlocks(LogicalTapeSet *lts)
Definition: logtape.c:889
bool tuplesort_getdatum ( Tuplesortstate state,
bool  forward,
Datum val,
bool isNull,
Datum abbrev 
)

Definition at line 2082 of file tuplesort.c.

References SortSupportData::abbrev_converter, SortTuple::datum1, datumCopy(), Tuplesortstate::datumTypeLen, SortTuple::isnull1, MemoryContextSwitchTo(), PointerGetDatum, Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortTuple::tuple, Tuplesortstate::tuples, and tuplesort_gettuple_common().

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

2084 {
2085  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2086  SortTuple stup;
2087 
2088  if (!tuplesort_gettuple_common(state, forward, &stup))
2089  {
2090  MemoryContextSwitchTo(oldcontext);
2091  return false;
2092  }
2093 
2094  /* Record abbreviated key for caller */
2095  if (state->sortKeys->abbrev_converter && abbrev)
2096  *abbrev = stup.datum1;
2097 
2098  if (stup.isnull1 || !state->tuples)
2099  {
2100  *val = stup.datum1;
2101  *isNull = stup.isnull1;
2102  }
2103  else
2104  {
2105  /* use stup.tuple because stup.datum1 may be an abbreviation */
2106  *val = datumCopy(PointerGetDatum(stup.tuple), false, state->datumTypeLen);
2107  *isNull = false;
2108  }
2109 
2110  MemoryContextSwitchTo(oldcontext);
2111 
2112  return true;
2113 }
#define PointerGetDatum(X)
Definition: postgres.h:562
SortSupport sortKeys
Definition: tuplesort.c:383
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum datum1
Definition: tuplesort.c:160
bool isnull1
Definition: tuplesort.c:161
void * tuple
Definition: tuplesort.c:159
MemoryContext sortcontext
Definition: tuplesort.c:234
static bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
Definition: tuplesort.c:1742
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
long val
Definition: informix.c:689
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
HeapTuple tuplesort_getheaptuple ( Tuplesortstate state,
bool  forward 
)

Definition at line 2033 of file tuplesort.c.

References MemoryContextSwitchTo(), Tuplesortstate::sortcontext, SortTuple::tuple, and tuplesort_gettuple_common().

Referenced by copy_heap_data().

2034 {
2035  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2036  SortTuple stup;
2037 
2038  if (!tuplesort_gettuple_common(state, forward, &stup))
2039  stup.tuple = NULL;
2040 
2041  MemoryContextSwitchTo(oldcontext);
2042 
2043  return stup.tuple;
2044 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * tuple
Definition: tuplesort.c:159
MemoryContext sortcontext
Definition: tuplesort.c:234
static bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
Definition: tuplesort.c:1742
IndexTuple tuplesort_getindextuple ( Tuplesortstate state,
bool  forward 
)

Definition at line 2053 of file tuplesort.c.

References MemoryContextSwitchTo(), Tuplesortstate::sortcontext, SortTuple::tuple, and tuplesort_gettuple_common().

Referenced by _bt_load(), and _h_indexbuild().

2054 {
2055  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2056  SortTuple stup;
2057 
2058  if (!tuplesort_gettuple_common(state, forward, &stup))
2059  stup.tuple = NULL;
2060 
2061  MemoryContextSwitchTo(oldcontext);
2062 
2063  return (IndexTuple) stup.tuple;
2064 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * tuple
Definition: tuplesort.c:159
MemoryContext sortcontext
Definition: tuplesort.c:234
static bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
Definition: tuplesort.c:1742
bool tuplesort_gettupleslot ( Tuplesortstate state,
bool  forward,
bool  copy,
TupleTableSlot slot,
Datum abbrev 
)

Definition at line 1996 of file tuplesort.c.

References SortSupportData::abbrev_converter, SortTuple::datum1, ExecClearTuple(), ExecStoreMinimalTuple(), heap_copy_minimal_tuple(), MemoryContextSwitchTo(), Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortTuple::tuple, and tuplesort_gettuple_common().

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

1998 {
1999  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2000  SortTuple stup;
2001 
2002  if (!tuplesort_gettuple_common(state, forward, &stup))
2003  stup.tuple = NULL;
2004 
2005  MemoryContextSwitchTo(oldcontext);
2006 
2007  if (stup.tuple)
2008  {
2009  /* Record abbreviated key for caller */
2010  if (state->sortKeys->abbrev_converter && abbrev)
2011  *abbrev = stup.datum1;
2012 
2013  if (copy)
2015 
2016  ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, copy);
2017  return true;
2018  }
2019  else
2020  {
2021  ExecClearTuple(slot);
2022  return false;
2023  }
2024 }
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:384
SortSupport sortKeys
Definition: tuplesort.c:383
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum datum1
Definition: tuplesort.c:160
MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1480
void * tuple
Definition: tuplesort.c:159
MemoryContext sortcontext
Definition: tuplesort.c:234
static bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
Definition: tuplesort.c:1742
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
void tuplesort_markpos ( Tuplesortstate state)

Definition at line 2875 of file tuplesort.c.

References Assert, Tuplesortstate::current, elog, Tuplesortstate::eof_reached, ERROR, LogicalTapeTell(), Tuplesortstate::markpos_block, Tuplesortstate::markpos_eof, Tuplesortstate::markpos_offset, MemoryContextSwitchTo(), Tuplesortstate::randomAccess, Tuplesortstate::result_tape, Tuplesortstate::sortcontext, Tuplesortstate::status, Tuplesortstate::tapeset, TSS_SORTEDINMEM, and TSS_SORTEDONTAPE.

Referenced by ExecSortMarkPos().

2876 {
2877  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2878 
2879  Assert(state->randomAccess);
2880 
2881  switch (state->status)
2882  {
2883  case TSS_SORTEDINMEM:
2884  state->markpos_offset = state->current;
2885  state->markpos_eof = state->eof_reached;
2886  break;
2887  case TSS_SORTEDONTAPE:
2888  LogicalTapeTell(state->tapeset,
2889  state->result_tape,
2890  &state->markpos_block,
2891  &state->markpos_offset);
2892  state->markpos_eof = state->eof_reached;
2893  break;
2894  default:
2895  elog(ERROR, "invalid tuplesort state");
2896  break;
2897  }
2898 
2899  MemoryContextSwitchTo(oldcontext);
2900 }
TupSortStatus status
Definition: tuplesort.c:222
bool randomAccess
Definition: tuplesort.c:224
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int markpos_offset
Definition: tuplesort.c:374
#define ERROR
Definition: elog.h:43
MemoryContext sortcontext
Definition: tuplesort.c:234
LogicalTapeSet * tapeset
Definition: tuplesort.c:236
void LogicalTapeTell(LogicalTapeSet *lts, int tapenum, long *blocknum, int *offset)
Definition: logtape.c:870
#define Assert(condition)
Definition: c.h:681
long markpos_block
Definition: tuplesort.c:373
bool eof_reached
Definition: tuplesort.c:370
bool markpos_eof
Definition: tuplesort.c:375
#define elog
Definition: elog.h:219
int tuplesort_merge_order ( int64  allowedMem)

Definition at line 2188 of file tuplesort.c.

References Max, MAXORDER, MERGE_BUFFER_SIZE, Min, MINORDER, and TAPE_BUFFER_OVERHEAD.

Referenced by cost_sort(), and inittapes().

2189 {
2190  int mOrder;
2191 
2192  /*
2193  * We need one tape for each merge input, plus another one for the output,
2194  * and each of these tapes needs buffer space. In addition we want
2195  * MERGE_BUFFER_SIZE workspace per input tape (but the output tape doesn't
2196  * count).
2197  *
2198  * Note: you might be thinking we need to account for the memtuples[]
2199  * array in this calculation, but we effectively treat that as part of the
2200  * MERGE_BUFFER_SIZE workspace.
2201  */
2202  mOrder = (allowedMem - TAPE_BUFFER_OVERHEAD) /
2204 
2205  /*
2206  * Even in minimum memory, use at least a MINORDER merge. On the other
2207  * hand, even when we have lots of memory, do not use more than a MAXORDER
2208  * merge. Tapes are pretty cheap, but they're not entirely free. Each
2209  * additional tape reduces the amount of memory available to build runs,
2210  * which in turn can cause the same sort to need more runs, which makes
2211  * merging slower even if it can still be done in a single pass. Also,
2212  * high order merges are quite slow due to CPU cache effects; it can be
2213  * faster to pay the I/O cost of a polyphase merge than to perform a
2214  * single merge pass across many hundreds of tapes.
2215  */
2216  mOrder = Max(mOrder, MINORDER);
2217  mOrder = Min(mOrder, MAXORDER);
2218 
2219  return mOrder;
2220 }
#define Min(x, y)
Definition: c.h:812
#define TAPE_BUFFER_OVERHEAD
Definition: tuplesort.c:211
#define MERGE_BUFFER_SIZE
Definition: tuplesort.c:212
#define MINORDER
Definition: tuplesort.c:209
#define Max(x, y)
Definition: c.h:806
#define MAXORDER
Definition: tuplesort.c:210
const char* tuplesort_method_name ( TuplesortMethod  m)

Definition at line 2988 of file tuplesort.c.

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_sort_info().

2989 {
2990  switch (m)
2991  {
2993  return "still in progress";
2995  return "top-N heapsort";
2996  case SORT_TYPE_QUICKSORT:
2997  return "quicksort";
2999  return "external sort";
3001  return "external merge";
3002  }
3003 
3004  return "unknown";
3005 }
void tuplesort_performsort ( Tuplesortstate state)

Definition at line 1656 of file tuplesort.c.

References Tuplesortstate::activeTapes, Tuplesortstate::current, dumptuples(), elog, Tuplesortstate::eof_reached, ERROR, LOG, Tuplesortstate::markpos_block, Tuplesortstate::markpos_eof, Tuplesortstate::markpos_offset, MemoryContextSwitchTo(), mergeruns(), pg_rusage_show(), Tuplesortstate::ru_start, sort_bounded_heap(), Tuplesortstate::sortcontext, Tuplesortstate::status, trace_sort, TSS_BOUNDED, TSS_BUILDRUNS, TSS_FINALMERGE, TSS_INITIAL, TSS_SORTEDINMEM, and tuplesort_sort_memtuples().

Referenced by _bt_leafbuild(), _h_indexbuild(), copy_heap_data(), ExecSort(), 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(), and validate_index().

1657 {
1658  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
1659 
1660 #ifdef TRACE_SORT
1661  if (trace_sort)
1662  elog(LOG, "performsort starting: %s",
1663  pg_rusage_show(&state->ru_start));
1664 #endif
1665 
1666  switch (state->status)
1667  {
1668  case TSS_INITIAL:
1669 
1670  /*
1671  * We were able to accumulate all the tuples within the allowed
1672  * amount of memory. Just qsort 'em and we're done.
1673  */
1674  tuplesort_sort_memtuples(state);
1675  state->current = 0;
1676  state->eof_reached = false;
1677  state->markpos_offset = 0;
1678  state->markpos_eof = false;
1679  state->status = TSS_SORTEDINMEM;
1680  break;
1681 
1682  case TSS_BOUNDED:
1683 
1684  /*
1685  * We were able to accumulate all the tuples required for output
1686  * in memory, using a heap to eliminate excess tuples. Now we
1687  * have to transform the heap to a properly-sorted array.
1688  */
1689  sort_bounded_heap(state);
1690  state->current = 0;
1691  state->eof_reached = false;
1692  state->markpos_offset = 0;
1693  state->markpos_eof = false;
1694  state->status = TSS_SORTEDINMEM;
1695  break;
1696 
1697  case TSS_BUILDRUNS:
1698 
1699  /*
1700  * Finish tape-based sort. First, flush all tuples remaining in
1701  * memory out to tape; then merge until we have a single remaining
1702  * run (or, if !randomAccess, one run per tape). Note that
1703  * mergeruns sets the correct state->status.
1704  */
1705  dumptuples(state, true);
1706  mergeruns(state);
1707  state->eof_reached = false;
1708  state->markpos_block = 0L;
1709  state->markpos_offset = 0;
1710  state->markpos_eof = false;
1711  break;
1712 
1713  default:
1714  elog(ERROR, "invalid tuplesort state");
1715  break;
1716  }
1717 
1718 #ifdef TRACE_SORT
1719  if (trace_sort)
1720  {
1721  if (state->status == TSS_FINALMERGE)
1722  elog(LOG, "performsort done (except %d-way final merge): %s",
1723  state->activeTapes,
1724  pg_rusage_show(&state->ru_start));
1725  else
1726  elog(LOG, "performsort done: %s",
1727  pg_rusage_show(&state->ru_start));
1728  }
1729 #endif
1730 
1731  MemoryContextSwitchTo(oldcontext);
1732 }
static void dumptuples(Tuplesortstate *state, bool alltuples)
Definition: tuplesort.c:2739
TupSortStatus status
Definition: tuplesort.c:222
PGRUsage ru_start
Definition: tuplesort.c:434
static void sort_bounded_heap(Tuplesortstate *state)
Definition: tuplesort.c:3080
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define LOG
Definition: elog.h:26
int markpos_offset
Definition: tuplesort.c:374
bool trace_sort
Definition: tuplesort.c:118
static void mergeruns(Tuplesortstate *state)
Definition: tuplesort.c:2373
#define ERROR
Definition: elog.h:43
MemoryContext sortcontext
Definition: tuplesort.c:234
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
long markpos_block
Definition: tuplesort.c:373
bool eof_reached
Definition: tuplesort.c:370
static void tuplesort_sort_memtuples(Tuplesortstate *state)
Definition: tuplesort.c:3119
bool markpos_eof
Definition: tuplesort.c:375
#define elog
Definition: elog.h:219
void tuplesort_putdatum ( Tuplesortstate state,
Datum  val,
bool  isNull 
)

Definition at line 1422 of file tuplesort.c.

References SortSupportData::abbrev_converter, consider_abort_common(), SortTuple::datum1, datumCopy(), DatumGetPointer, Tuplesortstate::datumTypeLen, GetMemoryChunkSpace(), i, SortTuple::isnull1, MemoryContextSwitchTo(), Tuplesortstate::memtupcount, Tuplesortstate::memtuples, PointerGetDatum, puttuple_common(), Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, SortTuple::tuple, Tuplesortstate::tuplecontext, Tuplesortstate::tuples, and USEMEM.

Referenced by advance_aggregates(), ordered_set_transition(), and validate_index_callback().

1423 {
1424  MemoryContext oldcontext = MemoryContextSwitchTo(state->tuplecontext);
1425  SortTuple stup;
1426 
1427  /*
1428  * Pass-by-value types or null values are just stored directly in
1429  * stup.datum1 (and stup.tuple is not used and set to NULL).
1430  *
1431  * Non-null pass-by-reference values need to be copied into memory we
1432  * control, and possibly abbreviated. The copied value is pointed to by
1433  * stup.tuple and is treated as the canonical copy (e.g. to return via
1434  * tuplesort_getdatum or when writing to tape); stup.datum1 gets the
1435  * abbreviated value if abbreviation is happening, otherwise it's
1436  * identical to stup.tuple.
1437  */
1438 
1439  if (isNull || !state->tuples)
1440  {
1441  /*
1442  * Set datum1 to zeroed representation for NULLs (to be consistent,
1443  * and to support cheap inequality tests for NULL abbreviated keys).
1444  */
1445  stup.datum1 = !isNull ? val : (Datum) 0;
1446  stup.isnull1 = isNull;
1447  stup.tuple = NULL; /* no separate storage */
1449  }
1450  else
1451  {
1452  Datum original = datumCopy(val, false, state->datumTypeLen);
1453 
1454  stup.isnull1 = false;
1455  stup.tuple = DatumGetPointer(original);
1456  USEMEM(state, GetMemoryChunkSpace(stup.tuple));
1458 
1459  if (!state->sortKeys->abbrev_converter)
1460  {
1461  stup.datum1 = original;
1462  }
1463  else if (!consider_abort_common(state))
1464  {
1465  /* Store abbreviated key representation */
1466  stup.datum1 = state->sortKeys->abbrev_converter(original,
1467  state->sortKeys);
1468  }
1469  else
1470  {
1471  /* Abort abbreviation */
1472  int i;
1473 
1474  stup.datum1 = original;
1475 
1476  /*
1477  * Set state to be consistent with never trying abbreviation.
1478  *
1479  * Alter datum1 representation in already-copied tuples, so as to
1480  * ensure a consistent representation (current tuple was just
1481  * handled). It does not matter if some dumped tuples are already
1482  * sorted on tape, since serialized tuples lack abbreviated keys
1483  * (TSS_BUILDRUNS state prevents control reaching here in any
1484  * case).
1485  */
1486  for (i = 0; i < state->memtupcount; i++)
1487  {
1488  SortTuple *mtup = &state->memtuples[i];
1489 
1490  mtup->datum1 = PointerGetDatum(mtup->tuple);
1491  }
1492  }
1493  }
1494 
1495  puttuple_common(state, &stup);
1496 
1497  MemoryContextSwitchTo(oldcontext);
1498 }
#define PointerGetDatum(X)
Definition: postgres.h:562
SortSupport sortKeys
Definition: tuplesort.c:383
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum datum1
Definition: tuplesort.c:160
Size GetMemoryChunkSpace(void *pointer)
Definition: mcxt.c:390
bool isnull1
Definition: tuplesort.c:161
void * tuple
Definition: tuplesort.c:159
static bool consider_abort_common(Tuplesortstate *state)
Definition: tuplesort.c:1612
MemoryContext sortcontext
Definition: tuplesort.c:234
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
uintptr_t Datum
Definition: postgres.h:372
static void puttuple_common(Tuplesortstate *state, SortTuple *tuple)
Definition: tuplesort.c:1504
#define DatumGetPointer(X)
Definition: postgres.h:555
MemoryContext tuplecontext
Definition: tuplesort.c:235
#define USEMEM(state, amt)
Definition: tuplesort.c:466
int i
long val
Definition: informix.c:689
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
SortTuple * memtuples
Definition: tuplesort.c:283
void tuplesort_putheaptuple ( Tuplesortstate state,
HeapTuple  tup 
)

Definition at line 1323 of file tuplesort.c.

References COPYTUP, MemoryContextSwitchTo(), puttuple_common(), and Tuplesortstate::sortcontext.

Referenced by copy_heap_data().

1324 {
1325  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
1326  SortTuple stup;
1327 
1328  /*
1329  * Copy the given tuple into memory we control, and decrease availMem.
1330  * Then call the common code.
1331  */
1332  COPYTUP(state, &stup, (void *) tup);
1333 
1334  puttuple_common(state, &stup);
1335 
1336  MemoryContextSwitchTo(oldcontext);
1337 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext sortcontext
Definition: tuplesort.c:234
#define COPYTUP(state, stup, tup)
Definition: tuplesort.c:462
static void puttuple_common(Tuplesortstate *state, SortTuple *tuple)
Definition: tuplesort.c:1504
void tuplesort_putindextuplevalues ( Tuplesortstate state,
Relation  rel,
ItemPointer  self,
Datum values,
bool isnull 
)

Definition at line 1344 of file tuplesort.c.

References SortSupportData::abbrev_converter, consider_abort_common(), SortTuple::datum1, GetMemoryChunkSpace(), i, index_form_tuple(), index_getattr, Tuplesortstate::indexRel, SortTuple::isnull1, MemoryContextSwitchTo(), Tuplesortstate::memtupcount, Tuplesortstate::memtuples, puttuple_common(), RelationGetDescr, Tuplesortstate::sortcontext, Tuplesortstate::sortKeys, IndexTupleData::t_tid, SortTuple::tuple, Tuplesortstate::tuplecontext, and USEMEM.

Referenced by _bt_spool(), and _h_spool().

1347 {
1348  MemoryContext oldcontext = MemoryContextSwitchTo(state->tuplecontext);
1349  SortTuple stup;
1350  Datum original;
1351  IndexTuple tuple;
1352 
1353  stup.tuple = index_form_tuple(RelationGetDescr(rel), values, isnull);
1354  tuple = ((IndexTuple) stup.tuple);
1355  tuple->t_tid = *self;
1356  USEMEM(state, GetMemoryChunkSpace(stup.tuple));
1357  /* set up first-column key value */
1358  original = index_getattr(tuple,
1359  1,
1360  RelationGetDescr(state->indexRel),
1361  &stup.isnull1);
1362 
1364 
1365  if (!state->sortKeys || !state->sortKeys->abbrev_converter || stup.isnull1)
1366  {
1367  /*
1368  * Store ordinary Datum representation, or NULL value. If there is a
1369  * converter it won't expect NULL values, and cost model is not
1370  * required to account for NULL, so in that case we avoid calling
1371  * converter and just set datum1 to zeroed representation (to be
1372  * consistent, and to support cheap inequality tests for NULL
1373  * abbreviated keys).
1374  */
1375  stup.datum1 = original;
1376  }
1377  else if (!consider_abort_common(state))
1378  {
1379  /* Store abbreviated key representation */
1380  stup.datum1 = state->sortKeys->abbrev_converter(original,
1381  state->sortKeys);
1382  }
1383  else
1384  {
1385  /* Abort abbreviation */
1386  int i;
1387 
1388  stup.datum1 = original;
1389 
1390  /*
1391  * Set state to be consistent with never trying abbreviation.
1392  *
1393  * Alter datum1 representation in already-copied tuples, so as to
1394  * ensure a consistent representation (current tuple was just
1395  * handled). It does not matter if some dumped tuples are already
1396  * sorted on tape, since serialized tuples lack abbreviated keys
1397  * (TSS_BUILDRUNS state prevents control reaching here in any case).
1398  */
1399  for (i = 0; i < state->memtupcount; i++)
1400  {
1401  SortTuple *mtup = &state->memtuples[i];
1402 
1403  tuple = mtup->tuple;
1404  mtup->datum1 = index_getattr(tuple,
1405  1,
1406  RelationGetDescr(state->indexRel),
1407  &mtup->isnull1);
1408  }
1409  }
1410 
1411  puttuple_common(state, &stup);
1412 
1413  MemoryContextSwitchTo(oldcontext);
1414 }
#define RelationGetDescr(relation)
Definition: rel.h:428
SortSupport sortKeys
Definition: tuplesort.c:383
ItemPointerData t_tid
Definition: itup.h:37
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum datum1
Definition: tuplesort.c:160
Size GetMemoryChunkSpace(void *pointer)
Definition: mcxt.c:390
bool isnull1
Definition: tuplesort.c:161
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:37
void * tuple
Definition: tuplesort.c:159
static bool consider_abort_common(Tuplesortstate *state)
Definition: tuplesort.c:1612
MemoryContext sortcontext
Definition: tuplesort.c:234
IndexTupleData * IndexTuple
Definition: itup.h:53
Relation indexRel
Definition: tuplesort.c:412
uintptr_t Datum
Definition: postgres.h:372
static void puttuple_common(Tuplesortstate *state, SortTuple *tuple)
Definition: tuplesort.c:1504
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
static Datum values[MAXATTR]
Definition: bootstrap.c:164
MemoryContext tuplecontext
Definition: tuplesort.c:235
#define USEMEM(state, amt)
Definition: tuplesort.c:466
int i
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
SortTuple * memtuples
Definition: tuplesort.c:283
void tuplesort_puttupleslot ( Tuplesortstate state,
TupleTableSlot slot 
)

Definition at line 1301 of file tuplesort.c.

References COPYTUP, MemoryContextSwitchTo(), puttuple_common(), and Tuplesortstate::sortcontext.

Referenced by advance_aggregates(), ExecSort(), fetch_input_tuple(), hypothetical_dense_rank_final(), hypothetical_rank_common(), and ordered_set_transition_multi().

1302 {
1303  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
1304  SortTuple stup;
1305 
1306  /*
1307  * Copy the given tuple into memory we control, and decrease availMem.
1308  * Then call the common code.
1309  */
1310  COPYTUP(state, &stup, (void *) slot);
1311 
1312  puttuple_common(state, &stup);
1313 
1314  MemoryContextSwitchTo(oldcontext);
1315 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext sortcontext
Definition: tuplesort.c:234
#define COPYTUP(state, stup, tup)
Definition: tuplesort.c:462
static void puttuple_common(Tuplesortstate *state, SortTuple *tuple)
Definition: tuplesort.c:1504
void tuplesort_rescan ( Tuplesortstate state)

Definition at line 2840 of file tuplesort.c.

References Assert, Tuplesortstate::current, elog, Tuplesortstate::eof_reached, ERROR, LogicalTapeRewindForRead(), Tuplesortstate::markpos_block, Tuplesortstate::markpos_eof, Tuplesortstate::markpos_offset, MemoryContextSwitchTo(), Tuplesortstate::randomAccess, Tuplesortstate::result_tape, Tuplesortstate::sortcontext, Tuplesortstate::status, Tuplesortstate::tapeset, TSS_SORTEDINMEM, and TSS_SORTEDONTAPE.

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

2841 {
2842  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2843 
2844  Assert(state->randomAccess);
2845 
2846  switch (state->status)
2847  {
2848  case TSS_SORTEDINMEM:
2849  state->current = 0;
2850  state->eof_reached = false;
2851  state->markpos_offset = 0;
2852  state->markpos_eof = false;
2853  break;
2854  case TSS_SORTEDONTAPE:
2856  state->result_tape,
2857  0);
2858  state->eof_reached = false;
2859  state->markpos_block = 0L;
2860  state->markpos_offset = 0;
2861  state->markpos_eof = false;
2862  break;
2863  default:
2864  elog(ERROR, "invalid tuplesort state");
2865  break;
2866  }
2867 
2868  MemoryContextSwitchTo(oldcontext);
2869 }
TupSortStatus status
Definition: tuplesort.c:222
bool randomAccess
Definition: tuplesort.c:224
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int markpos_offset
Definition: tuplesort.c:374
#define ERROR
Definition: elog.h:43
MemoryContext sortcontext
Definition: tuplesort.c:234
LogicalTapeSet * tapeset
Definition: tuplesort.c:236
#define Assert(condition)
Definition: c.h:681
long markpos_block
Definition: tuplesort.c:373
bool eof_reached
Definition: tuplesort.c:370
void LogicalTapeRewindForRead(LogicalTapeSet *lts, int tapenum, size_t buffer_size)
Definition: logtape.c:551
bool markpos_eof
Definition: tuplesort.c:375
#define elog
Definition: elog.h:219
void tuplesort_restorepos ( Tuplesortstate state)

Definition at line 2907 of file tuplesort.c.

References Assert, Tuplesortstate::current, elog, Tuplesortstate::eof_reached, ERROR, LogicalTapeSeek(), Tuplesortstate::markpos_block, Tuplesortstate::markpos_eof, Tuplesortstate::markpos_offset, MemoryContextSwitchTo(), Tuplesortstate::randomAccess, Tuplesortstate::result_tape, Tuplesortstate::sortcontext, Tuplesortstate::status, Tuplesortstate::tapeset, TSS_SORTEDINMEM, and TSS_SORTEDONTAPE.

Referenced by ExecSortRestrPos().

2908 {
2909  MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
2910 
2911  Assert(state->randomAccess);
2912 
2913  switch (state->status)
2914  {
2915  case TSS_SORTEDINMEM:
2916  state->current = state->markpos_offset;
2917  state->eof_reached = state->markpos_eof;
2918  break;
2919  case TSS_SORTEDONTAPE:
2920  LogicalTapeSeek(state->tapeset,
2921  state->result_tape,
2922  state->markpos_block,
2923  state->markpos_offset);
2924  state->eof_reached = state->markpos_eof;
2925  break;
2926  default:
2927  elog(ERROR, "invalid tuplesort state");
2928  break;
2929  }
2930 
2931  MemoryContextSwitchTo(oldcontext);
2932 }
TupSortStatus status
Definition: tuplesort.c:222
bool randomAccess
Definition: tuplesort.c:224
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int markpos_offset
Definition: tuplesort.c:374
#define ERROR
Definition: elog.h:43
MemoryContext sortcontext
Definition: tuplesort.c:234
LogicalTapeSet * tapeset
Definition: tuplesort.c:236
#define Assert(condition)
Definition: c.h:681
long markpos_block
Definition: tuplesort.c:373
bool eof_reached
Definition: tuplesort.c:370
bool markpos_eof
Definition: tuplesort.c:375
void LogicalTapeSeek(LogicalTapeSet *lts, int tapenum, long blocknum, int offset)
Definition: logtape.c:839
#define elog
Definition: elog.h:219
void tuplesort_set_bound ( Tuplesortstate state,
int64  bound 
)

Definition at line 1060 of file tuplesort.c.

References SortSupportData::abbrev_abort, SortSupportData::abbrev_converter, SortSupportData::abbrev_full_comparator, Assert, Tuplesortstate::bound, Tuplesortstate::bounded, SortSupportData::comparator, Tuplesortstate::memtupcount, Tuplesortstate::sortKeys, Tuplesortstate::status, and TSS_INITIAL.

Referenced by ExecSort().

1061 {
1062  /* Assert we're called before loading any tuples */
1063  Assert(state->status == TSS_INITIAL);
1064  Assert(state->memtupcount == 0);
1065  Assert(!state->bounded);
1066 
1067 #ifdef DEBUG_BOUNDED_SORT
1068  /* Honor GUC setting that disables the feature (for easy testing) */
1069  if (!optimize_bounded_sort)
1070  return;
1071 #endif
1072 
1073  /* We want to be able to compute bound * 2, so limit the setting */
1074  if (bound > (int64) (INT_MAX / 2))
1075  return;
1076 
1077  state->bounded = true;
1078  state->bound = (int) bound;
1079 
1080  /*
1081  * Bounded sorts are not an effective target for abbreviated key
1082  * optimization. Disable by setting state to be consistent with no
1083  * abbreviation support.
1084  */
1085  state->sortKeys->abbrev_converter = NULL;
1086  if (state->sortKeys->abbrev_full_comparator)
1088 
1089  /* Not strictly necessary, but be tidy */
1090  state->sortKeys->abbrev_abort = NULL;
1091  state->sortKeys->abbrev_full_comparator = NULL;
1092 }
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:107
TupSortStatus status
Definition: tuplesort.c:222
SortSupport sortKeys
Definition: tuplesort.c:383
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:192
#define Assert(condition)
Definition: c.h:681
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:183
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:173
bool tuplesort_skiptuples ( Tuplesortstate state,
int64  ntuples,
bool  forward 
)

Definition at line 2121 of file tuplesort.c.

References Assert, Tuplesortstate::bound, Tuplesortstate::bounded, CHECK_FOR_INTERRUPTS, Tuplesortstate::current, elog, Tuplesortstate::eof_reached, ERROR, MemoryContextSwitchTo(), Tuplesortstate::memtupcount, Tuplesortstate::sortcontext, Tuplesortstate::status, TSS_FINALMERGE, TSS_SORTEDINMEM, TSS_SORTEDONTAPE, and tuplesort_gettuple_common().

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

2122 {
2123  MemoryContext oldcontext;
2124 
2125  /*
2126  * We don't actually support backwards skip yet, because no callers need
2127  * it. The API is designed to allow for that later, though.
2128  */
2129  Assert(forward);
2130  Assert(ntuples >= 0);
2131 
2132  switch (state->status)
2133  {
2134  case TSS_SORTEDINMEM:
2135  if (state->memtupcount - state->current >= ntuples)
2136  {
2137  state->current += ntuples;
2138  return true;
2139  }
2140  state->current = state->memtupcount;
2141  state->eof_reached = true;
2142 
2143  /*
2144  * Complain if caller tries to retrieve more tuples than
2145  * originally asked for in a bounded sort. This is because
2146  * returning EOF here might be the wrong thing.
2147  */
2148  if (state->bounded && state->current >= state->bound)
2149  elog(ERROR, "retrieved too many tuples in a bounded sort");
2150 
2151  return false;
2152 
2153  case TSS_SORTEDONTAPE:
2154  case TSS_FINALMERGE:
2155 
2156  /*
2157  * We could probably optimize these cases better, but for now it's
2158  * not worth the trouble.
2159  */
2160  oldcontext = MemoryContextSwitchTo(state->sortcontext);
2161  while (ntuples-- > 0)
2162  {
2163  SortTuple stup;
2164 
2165  if (!tuplesort_gettuple_common(state, forward, &stup))
2166  {
2167  MemoryContextSwitchTo(oldcontext);
2168  return false;
2169  }
2171  }
2172  MemoryContextSwitchTo(oldcontext);
2173  return true;
2174 
2175  default:
2176  elog(ERROR, "invalid tuplesort state");
2177  return false; /* keep compiler quiet */
2178  }
2179 }
TupSortStatus status
Definition: tuplesort.c:222
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define ERROR
Definition: elog.h:43
MemoryContext sortcontext
Definition: tuplesort.c:234
static bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
Definition: tuplesort.c:1742
#define Assert(condition)
Definition: c.h:681
bool eof_reached
Definition: tuplesort.c:370
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
const char* tuplesort_space_type_name ( TuplesortSpaceType  t)

Definition at line 3011 of file tuplesort.c.

References Assert, SORT_SPACE_TYPE_DISK, and SORT_SPACE_TYPE_MEMORY.

Referenced by show_sort_info().

3012 {
3014  return t == SORT_SPACE_TYPE_DISK ? "Disk" : "Memory";
3015 }
#define Assert(condition)
Definition: c.h:681