38 #define CLUSTER_SORT 3
146 Oid *sortOperators,
Oid *sortCollations,
147 bool *nullsFirstFlags,
163 "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
187 for (
i = 0;
i < nkeys;
i++)
240 "begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c",
268 if (
arg->indexInfo->ii_IndexAttrNumbers[0] == 0)
273 arg->tupDesc = tupDesc;
277 if (
arg->indexInfo->ii_Expressions != NULL)
331 bool uniqueNullsNotDistinct,
350 "begin index sort: unique = %c, workMem = %d, randomAccess = %c",
351 enforceUnique ?
't' :
'f',
372 arg->index.heapRel = heapRel;
373 arg->index.indexRel = indexRel;
374 arg->enforceUnique = enforceUnique;
375 arg->uniqueNullsNotDistinct = uniqueNullsNotDistinct;
434 "begin index sort: high_mask = 0x%x, low_mask = 0x%x, "
435 "max_buckets = 0x%x, workMem = %d, randomAccess = %c",
453 arg->index.heapRel = heapRel;
454 arg->index.indexRel = indexRel;
456 arg->high_mask = high_mask;
457 arg->low_mask = low_mask;
458 arg->max_buckets = max_buckets;
485 "begin index sort: workMem = %d, randomAccess = %c",
499 arg->index.heapRel = heapRel;
500 arg->index.indexRel = indexRel;
501 arg->enforceUnique =
false;
502 arg->uniqueNullsNotDistinct =
false;
532 bool nullsFirstFlag,
int workMem,
549 "begin datum sort: workMem = %d, randomAccess = %c",
570 arg->datumType = datumType;
574 arg->datumTypeLen = typlen;
627 stup.tuple = (
void *) tuple;
658 stup.
tuple = (
void *) tup;
667 arg->indexInfo->ii_IndexAttrNumbers[0],
697 tuple->
t_tid = *
self;
741 stup.datum1 = !isNull ?
val : (
Datum) 0;
742 stup.isnull1 = isNull;
747 stup.isnull1 =
false;
898 *abbrev = stup.datum1;
900 if (stup.isnull1 || !base->
tuples)
903 *isNull = stup.isnull1;
930 for (
i = 0;
i < count;
i++)
955 b->datum1,
b->isnull1,
1001 for (nkey = 1; nkey < base->
nKeys; nkey++, sortKey++)
1005 datum1 =
heap_getattr(<up, attno, tupDesc, &isnull1);
1006 datum2 =
heap_getattr(&rtup, attno, tupDesc, &isnull2);
1029 unsigned int tuplen = tupbodylen +
sizeof(int);
1041 unsigned int tupbodylen =
len -
sizeof(int);
1049 tuple->
t_len = tuplen;
1053 stup->
tuple = (
void *) tuple;
1075 for (
i = 0;
i < count;
i++)
1081 arg->indexInfo->ii_IndexAttrNumbers[0],
1099 b->datum1,
b->isnull1,
1127 tupDesc =
arg->tupDesc;
1132 if (sortKey->abbrev_converter)
1134 AttrNumber leading =
arg->indexInfo->ii_IndexAttrNumbers[0];
1136 datum1 =
heap_getattr(ltup, leading, tupDesc, &isnull1);
1137 datum2 =
heap_getattr(rtup, leading, tupDesc, &isnull2);
1155 if (
arg->indexInfo->ii_Expressions == NULL)
1159 for (; nkey < base->
nKeys; nkey++, sortKey++)
1161 AttrNumber attno =
arg->indexInfo->ii_IndexAttrNumbers[nkey];
1194 l_index_values, l_index_isnull);
1198 r_index_values, r_index_isnull);
1200 for (; nkey < base->
nKeys; nkey++, sortKey++)
1203 l_index_isnull[nkey],
1204 r_index_values[nkey],
1205 r_index_isnull[nkey],
1242 tuple->
t_len = t_len;
1250 stup->
tuple = (
void *) tuple;
1254 arg->indexInfo->ii_IndexAttrNumbers[0],
1266 if (
arg->estate != NULL)
1290 for (
i = 0;
i < count;
i++)
1317 b->datum1,
b->isnull1,
1337 bool equal_hasnull =
false;
1347 keysz = base->
nKeys;
1350 if (sortKey->abbrev_converter)
1364 equal_hasnull =
true;
1367 for (nkey = 2; nkey <= keysz; nkey++, sortKey++)
1380 equal_hasnull =
true;
1393 if (
arg->enforceUnique && !(!
arg->uniqueNullsNotDistinct && equal_hasnull))
1405 Assert(tuple1 != tuple2);
1412 (
errcode(ERRCODE_UNIQUE_VIOLATION),
1413 errmsg(
"could not create unique index \"%s\"",
1415 key_desc ?
errdetail(
"Key %s is duplicated.", key_desc) :
1432 return (blk1 < blk2) ? -1 : 1;
1439 return (pos1 < pos2) ? -1 : 1;
1468 arg->max_buckets,
arg->high_mask,
1472 arg->max_buckets,
arg->high_mask,
1474 if (bucket1 > bucket2)
1476 else if (bucket1 < bucket2)
1488 else if (hash1 < hash2)
1504 return (blk1 < blk2) ? -1 : 1;
1511 return (pos1 < pos2) ? -1 : 1;
1538 unsigned int tuplen;
1553 unsigned int tuplen =
len -
sizeof(
unsigned int);
1559 stup->
tuple = (
void *) tuple;
1576 for (
i = 0;
i < count;
i++)
1587 b->datum1,
b->isnull1,
1616 unsigned int tuplen;
1617 unsigned int writtenlen;
1627 tuplen =
sizeof(
Datum);
1631 waddr = stup->
tuple;
1636 writtenlen = tuplen +
sizeof(
unsigned int);
1649 unsigned int tuplen =
len -
sizeof(
unsigned int);
1672 stup->
tuple = raddr;
static Datum values[MAXATTR]
Datum datumCopy(Datum value, bool typByVal, int typLen)
Size datumGetSize(Datum value, bool typByVal, int typLen)
elog(ERROR, "%s: %s", p2, msg)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
const TupleTableSlotOps TTSOpsHeapTuple
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
EState * CreateExecutorState(void)
void FreeExecutorState(EState *estate)
#define ResetPerTupleExprContext(estate)
#define GetPerTupleExprContext(estate)
char * BuildIndexValueDescription(Relation indexRelation, Datum *values, bool *isnull)
static int compare(const void *arg1, const void *arg2)
Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket, uint32 highmask, uint32 lowmask)
HeapTuple heap_copytuple(HeapTuple tuple)
MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup)
HeapTupleData * HeapTuple
MinimalTupleData * MinimalTuple
HeapTupleHeaderData * HeapTupleHeader
#define MINIMAL_TUPLE_OFFSET
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
#define MINIMAL_TUPLE_DATA_OFFSET
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
IndexInfo * BuildIndexInfo(Relation index)
IndexTuple index_form_tuple_context(TupleDesc tupleDescriptor, Datum *values, bool *isnull, MemoryContext context)
void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor, Datum *values, bool *isnull)
if(TABLE==NULL||TABLE_index==NULL)
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
IndexTupleData * IndexTuple
#define IndexTupleSize(itup)
static Datum index_getattr(IndexTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Assert(fmt[strlen(fmt) - 1] !='\n')
void LogicalTapeWrite(LogicalTape *lt, const void *ptr, size_t size)
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext CurrentMemoryContext
#define SK_BT_NULLS_FIRST
BTScanInsert _bt_mkscankey(Relation rel, IndexTuple itup)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static uint32 DatumGetUInt32(Datum X)
static Datum PointerGetDatum(const void *X)
static Pointer DatumGetPointer(Datum X)
#define RelationGetDescr(relation)
#define RelationGetNumberOfAttributes(relation)
#define RelationGetRelationName(relation)
#define IndexRelationGetNumberOfKeyAttributes(relation)
int errtableconstraint(Relation rel, const char *conname)
void PrepareSortSupportFromGistIndexRel(Relation indexRel, SortSupport ssup)
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
void PrepareSortSupportFromIndexRel(Relation indexRel, int16 strategy, SortSupport ssup)
static int ApplySortAbbrevFullComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
struct SortSupportData * SortSupport
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
#define BTGreaterStrategyNumber
#define BTLessStrategyNumber
ScanKeyData scankeys[INDEX_MAX_KEYS]
TupleTableSlot * ecxt_scantuple
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
bool uniqueNullsNotDistinct
MemoryContext maincontext
void(* writetup)(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
void(* removeabbrev)(Tuplesortstate *state, SortTuple *stups, int count)
void(* freestate)(Tuplesortstate *state)
MemoryContext tuplecontext
MemoryContext sortcontext
void(* readtup)(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
SortTupleComparator comparetup
SortTupleComparator comparetup_tiebreak
struct TupleDescData * TupleDesc
Tuplesortstate * tuplesort_begin_common(int workMem, SortCoordinate coordinate, int sortopt)
void tuplesort_puttuple_common(Tuplesortstate *state, SortTuple *tuple, bool useAbbrev)
bool tuplesort_gettuple_common(Tuplesortstate *state, bool forward, SortTuple *stup)
void * tuplesort_readtup_alloc(Tuplesortstate *state, Size tuplen)
#define PARALLEL_SORT(coordinate)
#define TUPLESORT_RANDOMACCESS
#define LogicalTapeReadExact(tape, ptr, len)
#define TuplesortstateGetPublic(state)
IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward)
HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward)
static void removeabbrev_datum(Tuplesortstate *state, SortTuple *stups, int count)
static int comparetup_index_btree_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
void tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
Tuplesortstate * tuplesort_begin_index_hash(Relation heapRel, Relation indexRel, uint32 high_mask, uint32 low_mask, uint32 max_buckets, int workMem, SortCoordinate coordinate, int sortopt)
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 int comparetup_cluster_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
static int comparetup_datum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Tuplesortstate * tuplesort_begin_index_gist(Relation heapRel, Relation indexRel, int workMem, SortCoordinate coordinate, int sortopt)
Tuplesortstate * tuplesort_begin_index_btree(Relation heapRel, Relation indexRel, bool enforceUnique, bool uniqueNullsNotDistinct, int workMem, SortCoordinate coordinate, int sortopt)
static void removeabbrev_index(Tuplesortstate *state, SortTuple *stups, int count)
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 readtup_heap(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int len)
static void readtup_cluster(Tuplesortstate *state, SortTuple *stup, LogicalTape *tape, unsigned int tuplen)
static void writetup_datum(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
Tuplesortstate * tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, bool nullsFirstFlag, int workMem, SortCoordinate coordinate, int sortopt)
static int comparetup_heap(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
static int comparetup_index_hash(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
Tuplesortstate * tuplesort_begin_cluster(TupleDesc tupDesc, Relation indexRel, int workMem, SortCoordinate coordinate, int sortopt)
static void writetup_heap(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
static void writetup_index(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
static int comparetup_heap_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static void freestate_cluster(Tuplesortstate *state)
static void removeabbrev_heap(Tuplesortstate *state, SortTuple *stups, int count)
void tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
static int comparetup_cluster(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
static int comparetup_index_hash_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state)
bool tuplesort_getdatum(Tuplesortstate *state, bool forward, bool copy, Datum *val, bool *isNull, Datum *abbrev)
static void writetup_cluster(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup)
void tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel, ItemPointer self, Datum *values, bool *isnull)
static void removeabbrev_cluster(Tuplesortstate *state, SortTuple *stups, int count)
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, SortCoordinate coordinate, int sortopt)
static MinimalTuple ExecCopySlotMinimalTuple(TupleTableSlot *slot)
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)