| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* tuplesortvariants.c |
| 4 |
* Implementation of tuple sorting variants. |
| 5 |
* |
| 6 |
* This module handles the sorting of heap tuples, index tuples, or single |
| 7 |
* Datums. The implementation is based on the generalized tuple sorting |
| 8 |
* facility given in tuplesort.c. Support other kinds of sortable objects |
| 9 |
* could be easily added here, another module, or even an extension. |
| 10 |
* |
| 11 |
* |
| 12 |
* Copyright (c) 2022-2025, PostgreSQL Global Development Group |
| 13 |
* |
| 14 |
* IDENTIFICATION |
| 15 |
* src/backend/utils/sort/tuplesortvariants.c |
| 16 |
* |
| 17 |
*------------------------------------------------------------------------- |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "postgres.h" |
| 21 |
|
| 22 |
#include "access/brin_tuple.h" |
| 23 |
#include "access/gin.h" |
| 24 |
#include "access/gin_tuple.h" |
| 25 |
#include "access/hash.h" |
| 26 |
#include "access/htup_details.h" |
| 27 |
#include "access/nbtree.h" |
| 28 |
#include "catalog/index.h" |
| 29 |
#include "catalog/pg_collation.h" |
| 30 |
#include "executor/executor.h" |
| 31 |
#include "pg_trace.h" |
| 32 |
#include "utils/builtins.h" |
| 33 |
#include "utils/datum.h" |
| 34 |
#include "utils/guc.h" |
| 35 |
#include "utils/lsyscache.h" |
| 36 |
#include "utils/tuplesort.h" |
| 37 |
|
| 38 |
|
| 39 |
/* sort-type codes for sort__start probes */ |
| 40 |
#define HEAP_SORT 0 |
| 41 |
#define INDEX_SORT 1 |
| 42 |
#define DATUM_SORT 2 |
| 43 |
#define CLUSTER_SORT 3 |
| 44 |
|
| 45 |
static void removeabbrev_heap(Tuplesortstate *state, SortTuple *stups, |
| 46 |
int count); |
| 47 |
static void removeabbrev_cluster(Tuplesortstate *state, SortTuple *stups, |
| 48 |
int count); |
| 49 |
static void removeabbrev_index(Tuplesortstate *state, SortTuple *stups, |
| 50 |
int count); |
| 51 |
static void removeabbrev_index_brin(Tuplesortstate *state, SortTuple *stups, |
| 52 |
int count); |
| 53 |
static void removeabbrev_index_gin(Tuplesortstate *state, SortTuple *stups, |
| 54 |
int count); |
| 55 |
static void removeabbrev_datum(Tuplesortstate *state, SortTuple *stups, |
| 56 |
int count); |
| 57 |
static int comparetup_heap(const SortTuple *a, const SortTuple *b, |
| 58 |
Tuplesortstate *state); |
| 59 |
static int comparetup_heap_tiebreak(const SortTuple *a, const SortTuple *b, |
| 60 |
Tuplesortstate *state); |
| 61 |
static void writetup_heap(Tuplesortstate *state, LogicalTape *tape, |
| 62 |
SortTuple *stup); |
| 63 |
static void readtup_heap(Tuplesortstate *state, SortTuple *stup, |
| 64 |
LogicalTape *tape, unsigned int len); |
| 65 |
static int comparetup_cluster(const SortTuple *a, const SortTuple *b, |
| 66 |
Tuplesortstate *state); |
| 67 |
static int comparetup_cluster_tiebreak(const SortTuple *a, const SortTuple *b, |
| 68 |
Tuplesortstate *state); |
| 69 |
static void writetup_cluster(Tuplesortstate *state, LogicalTape *tape, |
| 70 |
SortTuple *stup); |
| 71 |
static void readtup_cluster(Tuplesortstate *state, SortTuple *stup, |
| 72 |
LogicalTape *tape, unsigned int tuplen); |
| 73 |
static int comparetup_index_btree(const SortTuple *a, const SortTuple *b, |
| 74 |
Tuplesortstate *state); |
| 75 |
static int comparetup_index_btree_tiebreak(const SortTuple *a, const SortTuple *b, |
| 76 |
Tuplesortstate *state); |
| 77 |
static int comparetup_index_hash(const SortTuple *a, const SortTuple *b, |
| 78 |
Tuplesortstate *state); |
| 79 |
static int comparetup_index_hash_tiebreak(const SortTuple *a, const SortTuple *b, |
| 80 |
Tuplesortstate *state); |
| 81 |
static int comparetup_index_brin(const SortTuple *a, const SortTuple *b, |
| 82 |
Tuplesortstate *state); |
| 83 |
static int comparetup_index_gin(const SortTuple *a, const SortTuple *b, |
| 84 |
Tuplesortstate *state); |
| 85 |
static void writetup_index(Tuplesortstate *state, LogicalTape *tape, |
| 86 |
SortTuple *stup); |
| 87 |
static void readtup_index(Tuplesortstate *state, SortTuple *stup, |
| 88 |
LogicalTape *tape, unsigned int len); |
| 89 |
static void writetup_index_brin(Tuplesortstate *state, LogicalTape *tape, |
| 90 |
SortTuple *stup); |
| 91 |
static void readtup_index_brin(Tuplesortstate *state, SortTuple *stup, |
| 92 |
LogicalTape *tape, unsigned int len); |
| 93 |
static void writetup_index_gin(Tuplesortstate *state, LogicalTape *tape, |
| 94 |
SortTuple *stup); |
| 95 |
static void readtup_index_gin(Tuplesortstate *state, SortTuple *stup, |
| 96 |
LogicalTape *tape, unsigned int len); |
| 97 |
static int comparetup_datum(const SortTuple *a, const SortTuple *b, |
| 98 |
Tuplesortstate *state); |
| 99 |
static int comparetup_datum_tiebreak(const SortTuple *a, const SortTuple *b, |
| 100 |
Tuplesortstate *state); |
| 101 |
static void writetup_datum(Tuplesortstate *state, LogicalTape *tape, |
| 102 |
SortTuple *stup); |
| 103 |
static void readtup_datum(Tuplesortstate *state, SortTuple *stup, |
| 104 |
LogicalTape *tape, unsigned int len); |
| 105 |
static void freestate_cluster(Tuplesortstate *state); |
| 106 |
|
| 107 |
/* |
| 108 |
* Data structure pointed by "TuplesortPublic.arg" for the CLUSTER case. Set by |
| 109 |
* the tuplesort_begin_cluster. |
| 110 |
*/ |
| 111 |
typedef struct |
| 112 |
{ |
| 113 |
TupleDesc tupDesc; |
| 114 |
|
| 115 |
IndexInfo *indexInfo; /* info about index being used for reference */ |
| 116 |
EState *estate; /* for evaluating index expressions */ |
| 117 |
} TuplesortClusterArg; |
| 118 |
|
| 119 |
/* |
| 120 |
* Data structure pointed by "TuplesortPublic.arg" for the IndexTuple case. |
| 121 |
* Set by tuplesort_begin_index_xxx and used only by the IndexTuple routines. |
| 122 |
*/ |
| 123 |
typedef struct |
| 124 |
{ |
| 125 |
Relation heapRel; /* table the index is being built on */ |
| 126 |
Relation indexRel; /* index being built */ |
| 127 |
} TuplesortIndexArg; |
| 128 |
|
| 129 |
/* |
| 130 |
* Data structure pointed by "TuplesortPublic.arg" for the index_btree subcase. |
| 131 |
*/ |
| 132 |
typedef struct |
| 133 |
{ |
| 134 |
TuplesortIndexArg index; |
| 135 |
|
| 136 |
bool enforceUnique; /* complain if we find duplicate tuples */ |
| 137 |
bool uniqueNullsNotDistinct; /* unique constraint null treatment */ |
| 138 |
} TuplesortIndexBTreeArg; |
| 139 |
|
| 140 |
/* |
| 141 |
* Data structure pointed by "TuplesortPublic.arg" for the index_hash subcase. |
| 142 |
*/ |
| 143 |
typedef struct |
| 144 |
{ |
| 145 |
TuplesortIndexArg index; |
| 146 |
|
| 147 |
uint32 high_mask; /* masks for sortable part of hash code */ |
| 148 |
uint32 low_mask; |
| 149 |
uint32 max_buckets; |
| 150 |
} TuplesortIndexHashArg; |
| 151 |
|
| 152 |
/* |
| 153 |
* Data structure pointed by "TuplesortPublic.arg" for the Datum case. |
| 154 |
* Set by tuplesort_begin_datum and used only by the DatumTuple routines. |
| 155 |
*/ |
| 156 |
typedef struct |
| 157 |
{ |
| 158 |
/* the datatype oid of Datum's to be sorted */ |
| 159 |
Oid datumType; |
| 160 |
/* we need typelen in order to know how to copy the Datums. */ |
| 161 |
int datumTypeLen; |
| 162 |
} TuplesortDatumArg; |
| 163 |
|
| 164 |
/* |
| 165 |
* Computing BrinTuple size with only the tuple is difficult, so we want to track |
| 166 |
* the length referenced by the SortTuple. That's what BrinSortTuple is meant |
| 167 |
* to do - it's essentially a BrinTuple prefixed by its length. |
| 168 |
*/ |
| 169 |
typedef struct BrinSortTuple |
| 170 |
{ |
| 171 |
Size tuplen; |
| 172 |
BrinTuple tuple; |
| 173 |
} BrinSortTuple; |
| 174 |
|
| 175 |
/* Size of the BrinSortTuple, given length of the BrinTuple. */ |
| 176 |
#define BRINSORTTUPLE_SIZE(len) (offsetof(BrinSortTuple, tuple) + (len)) |
| 177 |
|
| 178 |
|
| 179 |
Tuplesortstate * |
| 180 |
tuplesort_begin_heap(TupleDesc tupDesc, |
| 181 |
int nkeys, AttrNumber *attNums, |
| 182 |
Oid *sortOperators, Oid *sortCollations, |
| 183 |
bool *nullsFirstFlags, |
| 184 |
int workMem, SortCoordinate coordinate, int sortopt) |
| 185 |
{ |
| 186 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 187 |
sortopt); |
| 188 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 189 |
MemoryContext oldcontext; |
| 190 |
int i; |
| 191 |
|
| 192 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 193 |
|
| 194 |
Assert(nkeys > 0); |
| 195 |
|
| 196 |
if (trace_sort) |
| 197 |
elog(LOG, |
| 198 |
"begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c", |
| 199 |
nkeys, workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 200 |
|
| 201 |
base->nKeys = nkeys; |
| 202 |
|
| 203 |
TRACE_POSTGRESQL_SORT_START(HEAP_SORT, |
| 204 |
false, /* no unique check */ |
| 205 |
nkeys, |
| 206 |
workMem, |
| 207 |
sortopt & TUPLESORT_RANDOMACCESS, |
| 208 |
PARALLEL_SORT(coordinate)); |
| 209 |
|
| 210 |
base->removeabbrev = removeabbrev_heap; |
| 211 |
base->comparetup = comparetup_heap; |
| 212 |
base->comparetup_tiebreak = comparetup_heap_tiebreak; |
| 213 |
base->writetup = writetup_heap; |
| 214 |
base->readtup = readtup_heap; |
| 215 |
base->haveDatum1 = true; |
| 216 |
base->arg = tupDesc; /* assume we need not copy tupDesc */ |
| 217 |
|
| 218 |
/* Prepare SortSupport data for each column */ |
| 219 |
base->sortKeys = (SortSupport) palloc0(nkeys * sizeof(SortSupportData)); |
| 220 |
|
| 221 |
for (i = 0; i < nkeys; i++) |
| 222 |
{ |
| 223 |
SortSupport sortKey = base->sortKeys + i; |
| 224 |
|
| 225 |
Assert(attNums[i] != 0); |
| 226 |
Assert(sortOperators[i] != 0); |
| 227 |
|
| 228 |
sortKey->ssup_cxt = CurrentMemoryContext; |
| 229 |
sortKey->ssup_collation = sortCollations[i]; |
| 230 |
sortKey->ssup_nulls_first = nullsFirstFlags[i]; |
| 231 |
sortKey->ssup_attno = attNums[i]; |
| 232 |
/* Convey if abbreviation optimization is applicable in principle */ |
| 233 |
sortKey->abbreviate = (i == 0 && base->haveDatum1); |
| 234 |
|
| 235 |
PrepareSortSupportFromOrderingOp(sortOperators[i], sortKey); |
| 236 |
} |
| 237 |
|
| 238 |
/* |
| 239 |
* The "onlyKey" optimization cannot be used with abbreviated keys, since |
| 240 |
* tie-breaker comparisons may be required. Typically, the optimization |
| 241 |
* is only of value to pass-by-value types anyway, whereas abbreviated |
| 242 |
* keys are typically only of value to pass-by-reference types. |
| 243 |
*/ |
| 244 |
if (nkeys == 1 && !base->sortKeys->abbrev_converter) |
| 245 |
base->onlyKey = base->sortKeys; |
| 246 |
|
| 247 |
MemoryContextSwitchTo(oldcontext); |
| 248 |
|
| 249 |
return state; |
| 250 |
} |
| 251 |
|
| 252 |
Tuplesortstate * |
| 253 |
tuplesort_begin_cluster(TupleDesc tupDesc, |
| 254 |
Relation indexRel, |
| 255 |
int workMem, |
| 256 |
SortCoordinate coordinate, int sortopt) |
| 257 |
{ |
| 258 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 259 |
sortopt); |
| 260 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 261 |
BTScanInsert indexScanKey; |
| 262 |
MemoryContext oldcontext; |
| 263 |
TuplesortClusterArg *arg; |
| 264 |
int i; |
| 265 |
|
| 266 |
Assert(indexRel->rd_rel->relam == BTREE_AM_OID); |
| 267 |
|
| 268 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 269 |
arg = (TuplesortClusterArg *) palloc0(sizeof(TuplesortClusterArg)); |
| 270 |
|
| 271 |
if (trace_sort) |
| 272 |
elog(LOG, |
| 273 |
"begin tuple sort: nkeys = %d, workMem = %d, randomAccess = %c", |
| 274 |
RelationGetNumberOfAttributes(indexRel), |
| 275 |
workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 276 |
|
| 277 |
base->nKeys = IndexRelationGetNumberOfKeyAttributes(indexRel); |
| 278 |
|
| 279 |
TRACE_POSTGRESQL_SORT_START(CLUSTER_SORT, |
| 280 |
false, /* no unique check */ |
| 281 |
base->nKeys, |
| 282 |
workMem, |
| 283 |
sortopt & TUPLESORT_RANDOMACCESS, |
| 284 |
PARALLEL_SORT(coordinate)); |
| 285 |
|
| 286 |
base->removeabbrev = removeabbrev_cluster; |
| 287 |
base->comparetup = comparetup_cluster; |
| 288 |
base->comparetup_tiebreak = comparetup_cluster_tiebreak; |
| 289 |
base->writetup = writetup_cluster; |
| 290 |
base->readtup = readtup_cluster; |
| 291 |
base->freestate = freestate_cluster; |
| 292 |
base->arg = arg; |
| 293 |
|
| 294 |
arg->indexInfo = BuildIndexInfo(indexRel); |
| 295 |
|
| 296 |
/* |
| 297 |
* If we don't have a simple leading attribute, we don't currently |
| 298 |
* initialize datum1, so disable optimizations that require it. |
| 299 |
*/ |
| 300 |
if (arg->indexInfo->ii_IndexAttrNumbers[0] == 0) |
| 301 |
base->haveDatum1 = false; |
| 302 |
else |
| 303 |
base->haveDatum1 = true; |
| 304 |
|
| 305 |
arg->tupDesc = tupDesc; /* assume we need not copy tupDesc */ |
| 306 |
|
| 307 |
indexScanKey = _bt_mkscankey(indexRel, NULL); |
| 308 |
|
| 309 |
if (arg->indexInfo->ii_Expressions != NULL) |
| 310 |
{ |
| 311 |
TupleTableSlot *slot; |
| 312 |
ExprContext *econtext; |
| 313 |
|
| 314 |
/* |
| 315 |
* We will need to use FormIndexDatum to evaluate the index |
| 316 |
* expressions. To do that, we need an EState, as well as a |
| 317 |
* TupleTableSlot to put the table tuples into. The econtext's |
| 318 |
* scantuple has to point to that slot, too. |
| 319 |
*/ |
| 320 |
arg->estate = CreateExecutorState(); |
| 321 |
slot = MakeSingleTupleTableSlot(tupDesc, &TTSOpsHeapTuple); |
| 322 |
econtext = GetPerTupleExprContext(arg->estate); |
| 323 |
econtext->ecxt_scantuple = slot; |
| 324 |
} |
| 325 |
|
| 326 |
/* Prepare SortSupport data for each column */ |
| 327 |
base->sortKeys = (SortSupport) palloc0(base->nKeys * |
| 328 |
sizeof(SortSupportData)); |
| 329 |
|
| 330 |
for (i = 0; i < base->nKeys; i++) |
| 331 |
{ |
| 332 |
SortSupport sortKey = base->sortKeys + i; |
| 333 |
ScanKey scanKey = indexScanKey->scankeys + i; |
| 334 |
bool reverse; |
| 335 |
|
| 336 |
sortKey->ssup_cxt = CurrentMemoryContext; |
| 337 |
sortKey->ssup_collation = scanKey->sk_collation; |
| 338 |
sortKey->ssup_nulls_first = |
| 339 |
(scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0; |
| 340 |
sortKey->ssup_attno = scanKey->sk_attno; |
| 341 |
/* Convey if abbreviation optimization is applicable in principle */ |
| 342 |
sortKey->abbreviate = (i == 0 && base->haveDatum1); |
| 343 |
|
| 344 |
Assert(sortKey->ssup_attno != 0); |
| 345 |
|
| 346 |
reverse = (scanKey->sk_flags & SK_BT_DESC) != 0; |
| 347 |
|
| 348 |
PrepareSortSupportFromIndexRel(indexRel, reverse, sortKey); |
| 349 |
} |
| 350 |
|
| 351 |
pfree(indexScanKey); |
| 352 |
|
| 353 |
MemoryContextSwitchTo(oldcontext); |
| 354 |
|
| 355 |
return state; |
| 356 |
} |
| 357 |
|
| 358 |
Tuplesortstate * |
| 359 |
tuplesort_begin_index_btree(Relation heapRel, |
| 360 |
Relation indexRel, |
| 361 |
bool enforceUnique, |
| 362 |
bool uniqueNullsNotDistinct, |
| 363 |
int workMem, |
| 364 |
SortCoordinate coordinate, |
| 365 |
int sortopt) |
| 366 |
{ |
| 367 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 368 |
sortopt); |
| 369 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 370 |
BTScanInsert indexScanKey; |
| 371 |
TuplesortIndexBTreeArg *arg; |
| 372 |
MemoryContext oldcontext; |
| 373 |
int i; |
| 374 |
|
| 375 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 376 |
arg = (TuplesortIndexBTreeArg *) palloc(sizeof(TuplesortIndexBTreeArg)); |
| 377 |
|
| 378 |
if (trace_sort) |
| 379 |
elog(LOG, |
| 380 |
"begin index sort: unique = %c, workMem = %d, randomAccess = %c", |
| 381 |
enforceUnique ? 't' : 'f', |
| 382 |
workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 383 |
|
| 384 |
base->nKeys = IndexRelationGetNumberOfKeyAttributes(indexRel); |
| 385 |
|
| 386 |
TRACE_POSTGRESQL_SORT_START(INDEX_SORT, |
| 387 |
enforceUnique, |
| 388 |
base->nKeys, |
| 389 |
workMem, |
| 390 |
sortopt & TUPLESORT_RANDOMACCESS, |
| 391 |
PARALLEL_SORT(coordinate)); |
| 392 |
|
| 393 |
base->removeabbrev = removeabbrev_index; |
| 394 |
base->comparetup = comparetup_index_btree; |
| 395 |
base->comparetup_tiebreak = comparetup_index_btree_tiebreak; |
| 396 |
base->writetup = writetup_index; |
| 397 |
base->readtup = readtup_index; |
| 398 |
base->haveDatum1 = true; |
| 399 |
base->arg = arg; |
| 400 |
|
| 401 |
arg->index.heapRel = heapRel; |
| 402 |
arg->index.indexRel = indexRel; |
| 403 |
arg->enforceUnique = enforceUnique; |
| 404 |
arg->uniqueNullsNotDistinct = uniqueNullsNotDistinct; |
| 405 |
|
| 406 |
indexScanKey = _bt_mkscankey(indexRel, NULL); |
| 407 |
|
| 408 |
/* Prepare SortSupport data for each column */ |
| 409 |
base->sortKeys = (SortSupport) palloc0(base->nKeys * |
| 410 |
sizeof(SortSupportData)); |
| 411 |
|
| 412 |
for (i = 0; i < base->nKeys; i++) |
| 413 |
{ |
| 414 |
SortSupport sortKey = base->sortKeys + i; |
| 415 |
ScanKey scanKey = indexScanKey->scankeys + i; |
| 416 |
bool reverse; |
| 417 |
|
| 418 |
sortKey->ssup_cxt = CurrentMemoryContext; |
| 419 |
sortKey->ssup_collation = scanKey->sk_collation; |
| 420 |
sortKey->ssup_nulls_first = |
| 421 |
(scanKey->sk_flags & SK_BT_NULLS_FIRST) != 0; |
| 422 |
sortKey->ssup_attno = scanKey->sk_attno; |
| 423 |
/* Convey if abbreviation optimization is applicable in principle */ |
| 424 |
sortKey->abbreviate = (i == 0 && base->haveDatum1); |
| 425 |
|
| 426 |
Assert(sortKey->ssup_attno != 0); |
| 427 |
|
| 428 |
reverse = (scanKey->sk_flags & SK_BT_DESC) != 0; |
| 429 |
|
| 430 |
PrepareSortSupportFromIndexRel(indexRel, reverse, sortKey); |
| 431 |
} |
| 432 |
|
| 433 |
pfree(indexScanKey); |
| 434 |
|
| 435 |
MemoryContextSwitchTo(oldcontext); |
| 436 |
|
| 437 |
return state; |
| 438 |
} |
| 439 |
|
| 440 |
Tuplesortstate * |
| 441 |
tuplesort_begin_index_hash(Relation heapRel, |
| 442 |
Relation indexRel, |
| 443 |
uint32 high_mask, |
| 444 |
uint32 low_mask, |
| 445 |
uint32 max_buckets, |
| 446 |
int workMem, |
| 447 |
SortCoordinate coordinate, |
| 448 |
int sortopt) |
| 449 |
{ |
| 450 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 451 |
sortopt); |
| 452 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 453 |
MemoryContext oldcontext; |
| 454 |
TuplesortIndexHashArg *arg; |
| 455 |
|
| 456 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 457 |
arg = (TuplesortIndexHashArg *) palloc(sizeof(TuplesortIndexHashArg)); |
| 458 |
|
| 459 |
if (trace_sort) |
| 460 |
elog(LOG, |
| 461 |
"begin index sort: high_mask = 0x%x, low_mask = 0x%x, " |
| 462 |
"max_buckets = 0x%x, workMem = %d, randomAccess = %c", |
| 463 |
high_mask, |
| 464 |
low_mask, |
| 465 |
max_buckets, |
| 466 |
workMem, |
| 467 |
sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 468 |
|
| 469 |
base->nKeys = 1; /* Only one sort column, the hash code */ |
| 470 |
|
| 471 |
base->removeabbrev = removeabbrev_index; |
| 472 |
base->comparetup = comparetup_index_hash; |
| 473 |
base->comparetup_tiebreak = comparetup_index_hash_tiebreak; |
| 474 |
base->writetup = writetup_index; |
| 475 |
base->readtup = readtup_index; |
| 476 |
base->haveDatum1 = true; |
| 477 |
base->arg = arg; |
| 478 |
|
| 479 |
arg->index.heapRel = heapRel; |
| 480 |
arg->index.indexRel = indexRel; |
| 481 |
|
| 482 |
arg->high_mask = high_mask; |
| 483 |
arg->low_mask = low_mask; |
| 484 |
arg->max_buckets = max_buckets; |
| 485 |
|
| 486 |
MemoryContextSwitchTo(oldcontext); |
| 487 |
|
| 488 |
return state; |
| 489 |
} |
| 490 |
|
| 491 |
Tuplesortstate * |
| 492 |
tuplesort_begin_index_gist(Relation heapRel, |
| 493 |
Relation indexRel, |
| 494 |
int workMem, |
| 495 |
SortCoordinate coordinate, |
| 496 |
int sortopt) |
| 497 |
{ |
| 498 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 499 |
sortopt); |
| 500 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 501 |
MemoryContext oldcontext; |
| 502 |
TuplesortIndexBTreeArg *arg; |
| 503 |
int i; |
| 504 |
|
| 505 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 506 |
arg = (TuplesortIndexBTreeArg *) palloc(sizeof(TuplesortIndexBTreeArg)); |
| 507 |
|
| 508 |
if (trace_sort) |
| 509 |
elog(LOG, |
| 510 |
"begin index sort: workMem = %d, randomAccess = %c", |
| 511 |
workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 512 |
|
| 513 |
base->nKeys = IndexRelationGetNumberOfKeyAttributes(indexRel); |
| 514 |
|
| 515 |
base->removeabbrev = removeabbrev_index; |
| 516 |
base->comparetup = comparetup_index_btree; |
| 517 |
base->comparetup_tiebreak = comparetup_index_btree_tiebreak; |
| 518 |
base->writetup = writetup_index; |
| 519 |
base->readtup = readtup_index; |
| 520 |
base->haveDatum1 = true; |
| 521 |
base->arg = arg; |
| 522 |
|
| 523 |
arg->index.heapRel = heapRel; |
| 524 |
arg->index.indexRel = indexRel; |
| 525 |
arg->enforceUnique = false; |
| 526 |
arg->uniqueNullsNotDistinct = false; |
| 527 |
|
| 528 |
/* Prepare SortSupport data for each column */ |
| 529 |
base->sortKeys = (SortSupport) palloc0(base->nKeys * |
| 530 |
sizeof(SortSupportData)); |
| 531 |
|
| 532 |
for (i = 0; i < base->nKeys; i++) |
| 533 |
{ |
| 534 |
SortSupport sortKey = base->sortKeys + i; |
| 535 |
|
| 536 |
sortKey->ssup_cxt = CurrentMemoryContext; |
| 537 |
sortKey->ssup_collation = indexRel->rd_indcollation[i]; |
| 538 |
sortKey->ssup_nulls_first = false; |
| 539 |
sortKey->ssup_attno = i + 1; |
| 540 |
/* Convey if abbreviation optimization is applicable in principle */ |
| 541 |
sortKey->abbreviate = (i == 0 && base->haveDatum1); |
| 542 |
|
| 543 |
Assert(sortKey->ssup_attno != 0); |
| 544 |
|
| 545 |
/* Look for a sort support function */ |
| 546 |
PrepareSortSupportFromGistIndexRel(indexRel, sortKey); |
| 547 |
} |
| 548 |
|
| 549 |
MemoryContextSwitchTo(oldcontext); |
| 550 |
|
| 551 |
return state; |
| 552 |
} |
| 553 |
|
| 554 |
Tuplesortstate * |
| 555 |
tuplesort_begin_index_brin(int workMem, |
| 556 |
SortCoordinate coordinate, |
| 557 |
int sortopt) |
| 558 |
{ |
| 559 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 560 |
sortopt); |
| 561 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 562 |
|
| 563 |
if (trace_sort) |
| 564 |
elog(LOG, |
| 565 |
"begin index sort: workMem = %d, randomAccess = %c", |
| 566 |
workMem, |
| 567 |
sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 568 |
|
| 569 |
base->nKeys = 1; /* Only one sort column, the block number */ |
| 570 |
|
| 571 |
base->removeabbrev = removeabbrev_index_brin; |
| 572 |
base->comparetup = comparetup_index_brin; |
| 573 |
base->writetup = writetup_index_brin; |
| 574 |
base->readtup = readtup_index_brin; |
| 575 |
base->haveDatum1 = true; |
| 576 |
base->arg = NULL; |
| 577 |
|
| 578 |
return state; |
| 579 |
} |
| 580 |
|
| 581 |
Tuplesortstate * |
| 582 |
tuplesort_begin_index_gin(Relation heapRel, |
| 583 |
Relation indexRel, |
| 584 |
int workMem, SortCoordinate coordinate, |
| 585 |
int sortopt) |
| 586 |
{ |
| 587 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 588 |
sortopt); |
| 589 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 590 |
MemoryContext oldcontext; |
| 591 |
int i; |
| 592 |
TupleDesc desc = RelationGetDescr(indexRel); |
| 593 |
|
| 594 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 595 |
|
| 596 |
#ifdef TRACE_SORT |
| 597 |
if (trace_sort) |
| 598 |
elog(LOG, |
| 599 |
"begin index sort: workMem = %d, randomAccess = %c", |
| 600 |
workMem, |
| 601 |
sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 602 |
#endif |
| 603 |
|
| 604 |
/* |
| 605 |
* Multi-column GIN indexes expand the row into a separate index entry for |
| 606 |
* attribute, and that's what we write into the tuplesort. But we still |
| 607 |
* need to initialize sortsupport for all the attributes. |
| 608 |
*/ |
| 609 |
base->nKeys = IndexRelationGetNumberOfKeyAttributes(indexRel); |
| 610 |
|
| 611 |
/* Prepare SortSupport data for each column */ |
| 612 |
base->sortKeys = (SortSupport) palloc0(base->nKeys * |
| 613 |
sizeof(SortSupportData)); |
| 614 |
|
| 615 |
for (i = 0; i < base->nKeys; i++) |
| 616 |
{ |
| 617 |
SortSupport sortKey = base->sortKeys + i; |
| 618 |
Form_pg_attribute att = TupleDescAttr(desc, i); |
| 619 |
Oid cmpFunc; |
| 620 |
|
| 621 |
sortKey->ssup_cxt = CurrentMemoryContext; |
| 622 |
sortKey->ssup_collation = indexRel->rd_indcollation[i]; |
| 623 |
sortKey->ssup_nulls_first = false; |
| 624 |
sortKey->ssup_attno = i + 1; |
| 625 |
sortKey->abbreviate = false; |
| 626 |
|
| 627 |
Assert(sortKey->ssup_attno != 0); |
| 628 |
|
| 629 |
if (!OidIsValid(sortKey->ssup_collation)) |
| 630 |
sortKey->ssup_collation = DEFAULT_COLLATION_OID; |
| 631 |
|
| 632 |
/* |
| 633 |
* If the compare proc isn't specified in the opclass definition, look |
| 634 |
* up the index key type's default btree comparator. |
| 635 |
*/ |
| 636 |
cmpFunc = index_getprocid(indexRel, i + 1, GIN_COMPARE_PROC); |
| 637 |
if (cmpFunc == InvalidOid) |
| 638 |
{ |
| 639 |
TypeCacheEntry *typentry; |
| 640 |
|
| 641 |
typentry = lookup_type_cache(att->atttypid, |
| 642 |
TYPECACHE_CMP_PROC_FINFO); |
| 643 |
if (!OidIsValid(typentry->cmp_proc_finfo.fn_oid)) |
| 644 |
ereport(ERROR, |
| 645 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 646 |
errmsg("could not identify a comparison function for type %s", |
| 647 |
format_type_be(att->atttypid)))); |
| 648 |
|
| 649 |
cmpFunc = typentry->cmp_proc_finfo.fn_oid; |
| 650 |
} |
| 651 |
|
| 652 |
PrepareSortSupportComparisonShim(cmpFunc, sortKey); |
| 653 |
} |
| 654 |
|
| 655 |
base->removeabbrev = removeabbrev_index_gin; |
| 656 |
base->comparetup = comparetup_index_gin; |
| 657 |
base->writetup = writetup_index_gin; |
| 658 |
base->readtup = readtup_index_gin; |
| 659 |
base->haveDatum1 = false; |
| 660 |
base->arg = NULL; |
| 661 |
|
| 662 |
MemoryContextSwitchTo(oldcontext); |
| 663 |
|
| 664 |
return state; |
| 665 |
} |
| 666 |
|
| 667 |
Tuplesortstate * |
| 668 |
tuplesort_begin_datum(Oid datumType, Oid sortOperator, Oid sortCollation, |
| 669 |
bool nullsFirstFlag, int workMem, |
| 670 |
SortCoordinate coordinate, int sortopt) |
| 671 |
{ |
| 672 |
Tuplesortstate *state = tuplesort_begin_common(workMem, coordinate, |
| 673 |
sortopt); |
| 674 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 675 |
TuplesortDatumArg *arg; |
| 676 |
MemoryContext oldcontext; |
| 677 |
int16 typlen; |
| 678 |
bool typbyval; |
| 679 |
|
| 680 |
oldcontext = MemoryContextSwitchTo(base->maincontext); |
| 681 |
arg = (TuplesortDatumArg *) palloc(sizeof(TuplesortDatumArg)); |
| 682 |
|
| 683 |
if (trace_sort) |
| 684 |
elog(LOG, |
| 685 |
"begin datum sort: workMem = %d, randomAccess = %c", |
| 686 |
workMem, sortopt & TUPLESORT_RANDOMACCESS ? 't' : 'f'); |
| 687 |
|
| 688 |
base->nKeys = 1; /* always a one-column sort */ |
| 689 |
|
| 690 |
TRACE_POSTGRESQL_SORT_START(DATUM_SORT, |
| 691 |
false, /* no unique check */ |
| 692 |
1, |
| 693 |
workMem, |
| 694 |
sortopt & TUPLESORT_RANDOMACCESS, |
| 695 |
PARALLEL_SORT(coordinate)); |
| 696 |
|
| 697 |
base->removeabbrev = removeabbrev_datum; |
| 698 |
base->comparetup = comparetup_datum; |
| 699 |
base->comparetup_tiebreak = comparetup_datum_tiebreak; |
| 700 |
base->writetup = writetup_datum; |
| 701 |
base->readtup = readtup_datum; |
| 702 |
base->haveDatum1 = true; |
| 703 |
base->arg = arg; |
| 704 |
|
| 705 |
arg->datumType = datumType; |
| 706 |
|
| 707 |
/* lookup necessary attributes of the datum type */ |
| 708 |
get_typlenbyval(datumType, &typlen, &typbyval); |
| 709 |
arg->datumTypeLen = typlen; |
| 710 |
base->tuples = !typbyval; |
| 711 |
|
| 712 |
/* Prepare SortSupport data */ |
| 713 |
base->sortKeys = (SortSupport) palloc0(sizeof(SortSupportData)); |
| 714 |
|
| 715 |
base->sortKeys->ssup_cxt = CurrentMemoryContext; |
| 716 |
base->sortKeys->ssup_collation = sortCollation; |
| 717 |
base->sortKeys->ssup_nulls_first = nullsFirstFlag; |
| 718 |
|
| 719 |
/* |
| 720 |
* Abbreviation is possible here only for by-reference types. In theory, |
| 721 |
* a pass-by-value datatype could have an abbreviated form that is cheaper |
| 722 |
* to compare. In a tuple sort, we could support that, because we can |
| 723 |
* always extract the original datum from the tuple as needed. Here, we |
| 724 |
* can't, because a datum sort only stores a single copy of the datum; the |
| 725 |
* "tuple" field of each SortTuple is NULL. |
| 726 |
*/ |
| 727 |
base->sortKeys->abbreviate = !typbyval; |
| 728 |
|
| 729 |
PrepareSortSupportFromOrderingOp(sortOperator, base->sortKeys); |
| 730 |
|
| 731 |
/* |
| 732 |
* The "onlyKey" optimization cannot be used with abbreviated keys, since |
| 733 |
* tie-breaker comparisons may be required. Typically, the optimization |
| 734 |
* is only of value to pass-by-value types anyway, whereas abbreviated |
| 735 |
* keys are typically only of value to pass-by-reference types. |
| 736 |
*/ |
| 737 |
if (!base->sortKeys->abbrev_converter) |
| 738 |
base->onlyKey = base->sortKeys; |
| 739 |
|
| 740 |
MemoryContextSwitchTo(oldcontext); |
| 741 |
|
| 742 |
return state; |
| 743 |
} |
| 744 |
|
| 745 |
/* |
| 746 |
* Accept one tuple while collecting input data for sort. |
| 747 |
* |
| 748 |
* Note that the input data is always copied; the caller need not save it. |
| 749 |
*/ |
| 750 |
void |
| 751 |
tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot) |
| 752 |
{ |
| 753 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 754 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext); |
| 755 |
TupleDesc tupDesc = (TupleDesc) base->arg; |
| 756 |
SortTuple stup; |
| 757 |
MinimalTuple tuple; |
| 758 |
HeapTupleData htup; |
| 759 |
Size tuplen; |
| 760 |
|
| 761 |
/* copy the tuple into sort storage */ |
| 762 |
tuple = ExecCopySlotMinimalTuple(slot); |
| 763 |
stup.tuple = tuple; |
| 764 |
/* set up first-column key value */ |
| 765 |
htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET; |
| 766 |
htup.t_data = (HeapTupleHeader) ((char *) tuple - MINIMAL_TUPLE_OFFSET); |
| 767 |
stup.datum1 = heap_getattr(&htup, |
| 768 |
base->sortKeys[0].ssup_attno, |
| 769 |
tupDesc, |
| 770 |
&stup.isnull1); |
| 771 |
|
| 772 |
/* GetMemoryChunkSpace is not supported for bump contexts */ |
| 773 |
if (TupleSortUseBumpTupleCxt(base->sortopt)) |
| 774 |
tuplen = MAXALIGN(tuple->t_len); |
| 775 |
else |
| 776 |
tuplen = GetMemoryChunkSpace(tuple); |
| 777 |
|
| 778 |
tuplesort_puttuple_common(state, &stup, |
| 779 |
base->sortKeys->abbrev_converter && |
| 780 |
!stup.isnull1, tuplen); |
| 781 |
|
| 782 |
MemoryContextSwitchTo(oldcontext); |
| 783 |
} |
| 784 |
|
| 785 |
/* |
| 786 |
* Accept one tuple while collecting input data for sort. |
| 787 |
* |
| 788 |
* Note that the input data is always copied; the caller need not save it. |
| 789 |
*/ |
| 790 |
void |
| 791 |
tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup) |
| 792 |
{ |
| 793 |
SortTuple stup; |
| 794 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 795 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext); |
| 796 |
TuplesortClusterArg *arg = (TuplesortClusterArg *) base->arg; |
| 797 |
Size tuplen; |
| 798 |
|
| 799 |
/* copy the tuple into sort storage */ |
| 800 |
tup = heap_copytuple(tup); |
| 801 |
stup.tuple = tup; |
| 802 |
|
| 803 |
/* |
| 804 |
* set up first-column key value, and potentially abbreviate, if it's a |
| 805 |
* simple column |
| 806 |
*/ |
| 807 |
if (base->haveDatum1) |
| 808 |
{ |
| 809 |
stup.datum1 = heap_getattr(tup, |
| 810 |
arg->indexInfo->ii_IndexAttrNumbers[0], |
| 811 |
arg->tupDesc, |
| 812 |
&stup.isnull1); |
| 813 |
} |
| 814 |
|
| 815 |
/* GetMemoryChunkSpace is not supported for bump contexts */ |
| 816 |
if (TupleSortUseBumpTupleCxt(base->sortopt)) |
| 817 |
tuplen = MAXALIGN(HEAPTUPLESIZE + tup->t_len); |
| 818 |
else |
| 819 |
tuplen = GetMemoryChunkSpace(tup); |
| 820 |
|
| 821 |
tuplesort_puttuple_common(state, &stup, |
| 822 |
base->haveDatum1 && |
| 823 |
base->sortKeys->abbrev_converter && |
| 824 |
!stup.isnull1, tuplen); |
| 825 |
|
| 826 |
MemoryContextSwitchTo(oldcontext); |
| 827 |
} |
| 828 |
|
| 829 |
/* |
| 830 |
* Collect one index tuple while collecting input data for sort, building |
| 831 |
* it from caller-supplied values. |
| 832 |
*/ |
| 833 |
void |
| 834 |
tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel, |
| 835 |
ItemPointer self, const Datum *values, |
| 836 |
const bool *isnull) |
| 837 |
{ |
| 838 |
SortTuple stup; |
| 839 |
IndexTuple tuple; |
| 840 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 841 |
TuplesortIndexArg *arg = (TuplesortIndexArg *) base->arg; |
| 842 |
Size tuplen; |
| 843 |
|
| 844 |
stup.tuple = index_form_tuple_context(RelationGetDescr(rel), values, |
| 845 |
isnull, base->tuplecontext); |
| 846 |
tuple = ((IndexTuple) stup.tuple); |
| 847 |
tuple->t_tid = *self; |
| 848 |
/* set up first-column key value */ |
| 849 |
stup.datum1 = index_getattr(tuple, |
| 850 |
1, |
| 851 |
RelationGetDescr(arg->indexRel), |
| 852 |
&stup.isnull1); |
| 853 |
|
| 854 |
/* GetMemoryChunkSpace is not supported for bump contexts */ |
| 855 |
if (TupleSortUseBumpTupleCxt(base->sortopt)) |
| 856 |
tuplen = MAXALIGN(tuple->t_info & INDEX_SIZE_MASK); |
| 857 |
else |
| 858 |
tuplen = GetMemoryChunkSpace(tuple); |
| 859 |
|
| 860 |
tuplesort_puttuple_common(state, &stup, |
| 861 |
base->sortKeys && |
| 862 |
base->sortKeys->abbrev_converter && |
| 863 |
!stup.isnull1, tuplen); |
| 864 |
} |
| 865 |
|
| 866 |
/* |
| 867 |
* Collect one BRIN tuple while collecting input data for sort. |
| 868 |
*/ |
| 869 |
void |
| 870 |
tuplesort_putbrintuple(Tuplesortstate *state, BrinTuple *tuple, Size size) |
| 871 |
{ |
| 872 |
SortTuple stup; |
| 873 |
BrinSortTuple *bstup; |
| 874 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 875 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext); |
| 876 |
Size tuplen; |
| 877 |
|
| 878 |
/* allocate space for the whole BRIN sort tuple */ |
| 879 |
bstup = palloc(BRINSORTTUPLE_SIZE(size)); |
| 880 |
|
| 881 |
bstup->tuplen = size; |
| 882 |
memcpy(&bstup->tuple, tuple, size); |
| 883 |
|
| 884 |
stup.tuple = bstup; |
| 885 |
stup.datum1 = tuple->bt_blkno; |
| 886 |
stup.isnull1 = false; |
| 887 |
|
| 888 |
/* GetMemoryChunkSpace is not supported for bump contexts */ |
| 889 |
if (TupleSortUseBumpTupleCxt(base->sortopt)) |
| 890 |
tuplen = MAXALIGN(BRINSORTTUPLE_SIZE(size)); |
| 891 |
else |
| 892 |
tuplen = GetMemoryChunkSpace(bstup); |
| 893 |
|
| 894 |
tuplesort_puttuple_common(state, &stup, |
| 895 |
base->sortKeys && |
| 896 |
base->sortKeys->abbrev_converter && |
| 897 |
!stup.isnull1, tuplen); |
| 898 |
|
| 899 |
MemoryContextSwitchTo(oldcontext); |
| 900 |
} |
| 901 |
|
| 902 |
void |
| 903 |
tuplesort_putgintuple(Tuplesortstate *state, GinTuple *tuple, Size size) |
| 904 |
{ |
| 905 |
SortTuple stup; |
| 906 |
GinTuple *ctup; |
| 907 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 908 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext); |
| 909 |
Size tuplen; |
| 910 |
|
| 911 |
/* copy the GinTuple into the right memory context */ |
| 912 |
ctup = palloc(size); |
| 913 |
memcpy(ctup, tuple, size); |
| 914 |
|
| 915 |
stup.tuple = ctup; |
| 916 |
stup.datum1 = (Datum) 0; |
| 917 |
stup.isnull1 = false; |
| 918 |
|
| 919 |
/* GetMemoryChunkSpace is not supported for bump contexts */ |
| 920 |
if (TupleSortUseBumpTupleCxt(base->sortopt)) |
| 921 |
tuplen = MAXALIGN(size); |
| 922 |
else |
| 923 |
tuplen = GetMemoryChunkSpace(ctup); |
| 924 |
|
| 925 |
tuplesort_puttuple_common(state, &stup, |
| 926 |
base->sortKeys && |
| 927 |
base->sortKeys->abbrev_converter && |
| 928 |
!stup.isnull1, tuplen); |
| 929 |
|
| 930 |
MemoryContextSwitchTo(oldcontext); |
| 931 |
} |
| 932 |
|
| 933 |
/* |
| 934 |
* Accept one Datum while collecting input data for sort. |
| 935 |
* |
| 936 |
* If the Datum is pass-by-ref type, the value will be copied. |
| 937 |
*/ |
| 938 |
void |
| 939 |
tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull) |
| 940 |
{ |
| 941 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 942 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext); |
| 943 |
TuplesortDatumArg *arg = (TuplesortDatumArg *) base->arg; |
| 944 |
SortTuple stup; |
| 945 |
|
| 946 |
/* |
| 947 |
* Pass-by-value types or null values are just stored directly in |
| 948 |
* stup.datum1 (and stup.tuple is not used and set to NULL). |
| 949 |
* |
| 950 |
* Non-null pass-by-reference values need to be copied into memory we |
| 951 |
* control, and possibly abbreviated. The copied value is pointed to by |
| 952 |
* stup.tuple and is treated as the canonical copy (e.g. to return via |
| 953 |
* tuplesort_getdatum or when writing to tape); stup.datum1 gets the |
| 954 |
* abbreviated value if abbreviation is happening, otherwise it's |
| 955 |
* identical to stup.tuple. |
| 956 |
*/ |
| 957 |
|
| 958 |
if (isNull || !base->tuples) |
| 959 |
{ |
| 960 |
/* |
| 961 |
* Set datum1 to zeroed representation for NULLs (to be consistent, |
| 962 |
* and to support cheap inequality tests for NULL abbreviated keys). |
| 963 |
*/ |
| 964 |
stup.datum1 = !isNull ? val : (Datum) 0; |
| 965 |
stup.isnull1 = isNull; |
| 966 |
stup.tuple = NULL; /* no separate storage */ |
| 967 |
} |
| 968 |
else |
| 969 |
{ |
| 970 |
stup.isnull1 = false; |
| 971 |
stup.datum1 = datumCopy(val, false, arg->datumTypeLen); |
| 972 |
stup.tuple = DatumGetPointer(stup.datum1); |
| 973 |
} |
| 974 |
|
| 975 |
tuplesort_puttuple_common(state, &stup, |
| 976 |
base->tuples && |
| 977 |
base->sortKeys->abbrev_converter && !isNull, 0); |
| 978 |
|
| 979 |
MemoryContextSwitchTo(oldcontext); |
| 980 |
} |
| 981 |
|
| 982 |
/* |
| 983 |
* Fetch the next tuple in either forward or back direction. |
| 984 |
* If successful, put tuple in slot and return true; else, clear the slot |
| 985 |
* and return false. |
| 986 |
* |
| 987 |
* Caller may optionally be passed back abbreviated value (on true return |
| 988 |
* value) when abbreviation was used, which can be used to cheaply avoid |
| 989 |
* equality checks that might otherwise be required. Caller can safely make a |
| 990 |
* determination of "non-equal tuple" based on simple binary inequality. A |
| 991 |
* NULL value in leading attribute will set abbreviated value to zeroed |
| 992 |
* representation, which caller may rely on in abbreviated inequality check. |
| 993 |
* |
| 994 |
* If copy is true, the slot receives a tuple that's been copied into the |
| 995 |
* caller's memory context, so that it will stay valid regardless of future |
| 996 |
* manipulations of the tuplesort's state (up to and including deleting the |
| 997 |
* tuplesort). If copy is false, the slot will just receive a pointer to a |
| 998 |
* tuple held within the tuplesort, which is more efficient, but only safe for |
| 999 |
* callers that are prepared to have any subsequent manipulation of the |
| 1000 |
* tuplesort's state invalidate slot contents. |
| 1001 |
*/ |
| 1002 |
bool |
| 1003 |
tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, |
| 1004 |
TupleTableSlot *slot, Datum *abbrev) |
| 1005 |
{ |
| 1006 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1007 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->sortcontext); |
| 1008 |
SortTuple stup; |
| 1009 |
|
| 1010 |
if (!tuplesort_gettuple_common(state, forward, &stup)) |
| 1011 |
stup.tuple = NULL; |
| 1012 |
|
| 1013 |
MemoryContextSwitchTo(oldcontext); |
| 1014 |
|
| 1015 |
if (stup.tuple) |
| 1016 |
{ |
| 1017 |
/* Record abbreviated key for caller */ |
| 1018 |
if (base->sortKeys->abbrev_converter && abbrev) |
| 1019 |
*abbrev = stup.datum1; |
| 1020 |
|
| 1021 |
if (copy) |
| 1022 |
stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple, 0); |
| 1023 |
|
| 1024 |
ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, copy); |
| 1025 |
return true; |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
ExecClearTuple(slot); |
| 1030 |
return false; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* |
| 1035 |
* Fetch the next tuple in either forward or back direction. |
| 1036 |
* Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory |
| 1037 |
* context, and must not be freed by caller. Caller may not rely on tuple |
| 1038 |
* remaining valid after any further manipulation of tuplesort. |
| 1039 |
*/ |
| 1040 |
HeapTuple |
| 1041 |
tuplesort_getheaptuple(Tuplesortstate *state, bool forward) |
| 1042 |
{ |
| 1043 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1044 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->sortcontext); |
| 1045 |
SortTuple stup; |
| 1046 |
|
| 1047 |
if (!tuplesort_gettuple_common(state, forward, &stup)) |
| 1048 |
stup.tuple = NULL; |
| 1049 |
|
| 1050 |
MemoryContextSwitchTo(oldcontext); |
| 1051 |
|
| 1052 |
return stup.tuple; |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* |
| 1056 |
* Fetch the next index tuple in either forward or back direction. |
| 1057 |
* Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory |
| 1058 |
* context, and must not be freed by caller. Caller may not rely on tuple |
| 1059 |
* remaining valid after any further manipulation of tuplesort. |
| 1060 |
*/ |
| 1061 |
IndexTuple |
| 1062 |
tuplesort_getindextuple(Tuplesortstate *state, bool forward) |
| 1063 |
{ |
| 1064 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1065 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->sortcontext); |
| 1066 |
SortTuple stup; |
| 1067 |
|
| 1068 |
if (!tuplesort_gettuple_common(state, forward, &stup)) |
| 1069 |
stup.tuple = NULL; |
| 1070 |
|
| 1071 |
MemoryContextSwitchTo(oldcontext); |
| 1072 |
|
| 1073 |
return (IndexTuple) stup.tuple; |
| 1074 |
} |
| 1075 |
|
| 1076 |
/* |
| 1077 |
* Fetch the next BRIN tuple in either forward or back direction. |
| 1078 |
* Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory |
| 1079 |
* context, and must not be freed by caller. Caller may not rely on tuple |
| 1080 |
* remaining valid after any further manipulation of tuplesort. |
| 1081 |
*/ |
| 1082 |
BrinTuple * |
| 1083 |
tuplesort_getbrintuple(Tuplesortstate *state, Size *len, bool forward) |
| 1084 |
{ |
| 1085 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1086 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->sortcontext); |
| 1087 |
SortTuple stup; |
| 1088 |
BrinSortTuple *btup; |
| 1089 |
|
| 1090 |
if (!tuplesort_gettuple_common(state, forward, &stup)) |
| 1091 |
stup.tuple = NULL; |
| 1092 |
|
| 1093 |
MemoryContextSwitchTo(oldcontext); |
| 1094 |
|
| 1095 |
if (!stup.tuple) |
| 1096 |
return NULL; |
| 1097 |
|
| 1098 |
btup = (BrinSortTuple *) stup.tuple; |
| 1099 |
|
| 1100 |
*len = btup->tuplen; |
| 1101 |
|
| 1102 |
return &btup->tuple; |
| 1103 |
} |
| 1104 |
|
| 1105 |
GinTuple * |
| 1106 |
tuplesort_getgintuple(Tuplesortstate *state, Size *len, bool forward) |
| 1107 |
{ |
| 1108 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1109 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->sortcontext); |
| 1110 |
SortTuple stup; |
| 1111 |
GinTuple *tup; |
| 1112 |
|
| 1113 |
if (!tuplesort_gettuple_common(state, forward, &stup)) |
| 1114 |
stup.tuple = NULL; |
| 1115 |
|
| 1116 |
MemoryContextSwitchTo(oldcontext); |
| 1117 |
|
| 1118 |
if (!stup.tuple) |
| 1119 |
return NULL; |
| 1120 |
|
| 1121 |
tup = (GinTuple *) stup.tuple; |
| 1122 |
|
| 1123 |
*len = tup->tuplen; |
| 1124 |
|
| 1125 |
return tup; |
| 1126 |
} |
| 1127 |
|
| 1128 |
/* |
| 1129 |
* Fetch the next Datum in either forward or back direction. |
| 1130 |
* Returns false if no more datums. |
| 1131 |
* |
| 1132 |
* If the Datum is pass-by-ref type, the returned value is freshly palloc'd |
| 1133 |
* in caller's context, and is now owned by the caller (this differs from |
| 1134 |
* similar routines for other types of tuplesorts). |
| 1135 |
* |
| 1136 |
* Caller may optionally be passed back abbreviated value (on true return |
| 1137 |
* value) when abbreviation was used, which can be used to cheaply avoid |
| 1138 |
* equality checks that might otherwise be required. Caller can safely make a |
| 1139 |
* determination of "non-equal tuple" based on simple binary inequality. A |
| 1140 |
* NULL value will have a zeroed abbreviated value representation, which caller |
| 1141 |
* may rely on in abbreviated inequality check. |
| 1142 |
* |
| 1143 |
* For byref Datums, if copy is true, *val is set to a copy of the Datum |
| 1144 |
* copied into the caller's memory context, so that it will stay valid |
| 1145 |
* regardless of future manipulations of the tuplesort's state (up to and |
| 1146 |
* including deleting the tuplesort). If copy is false, *val will just be |
| 1147 |
* set to a pointer to the Datum held within the tuplesort, which is more |
| 1148 |
* efficient, but only safe for callers that are prepared to have any |
| 1149 |
* subsequent manipulation of the tuplesort's state invalidate slot contents. |
| 1150 |
* For byval Datums, the value of the 'copy' parameter has no effect. |
| 1151 |
|
| 1152 |
*/ |
| 1153 |
bool |
| 1154 |
tuplesort_getdatum(Tuplesortstate *state, bool forward, bool copy, |
| 1155 |
Datum *val, bool *isNull, Datum *abbrev) |
| 1156 |
{ |
| 1157 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1158 |
MemoryContext oldcontext = MemoryContextSwitchTo(base->sortcontext); |
| 1159 |
TuplesortDatumArg *arg = (TuplesortDatumArg *) base->arg; |
| 1160 |
SortTuple stup; |
| 1161 |
|
| 1162 |
if (!tuplesort_gettuple_common(state, forward, &stup)) |
| 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 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
/* |
| 1196 |
* Routines specialized for HeapTuple (actually MinimalTuple) case |
| 1197 |
*/ |
| 1198 |
|
| 1199 |
static void |
| 1200 |
removeabbrev_heap(Tuplesortstate *state, SortTuple *stups, int count) |
| 1201 |
{ |
| 1202 |
int i; |
| 1203 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1204 |
|
| 1205 |
for (i = 0; i < count; i++) |
| 1206 |
{ |
| 1207 |
HeapTupleData htup; |
| 1208 |
|
| 1209 |
htup.t_len = ((MinimalTuple) stups[i].tuple)->t_len + |
| 1210 |
MINIMAL_TUPLE_OFFSET; |
| 1211 |
htup.t_data = (HeapTupleHeader) ((char *) stups[i].tuple - |
| 1212 |
MINIMAL_TUPLE_OFFSET); |
| 1213 |
stups[i].datum1 = heap_getattr(&htup, |
| 1214 |
base->sortKeys[0].ssup_attno, |
| 1215 |
(TupleDesc) base->arg, |
| 1216 |
&stups[i].isnull1); |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
static int |
| 1221 |
comparetup_heap(const SortTuple *a, const SortTuple *b, Tuplesortstate *state) |
| 1222 |
{ |
| 1223 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1224 |
SortSupport sortKey = base->sortKeys; |
| 1225 |
int32 compare; |
| 1226 |
|
| 1227 |
|
| 1228 |
/* Compare the leading sort key */ |
| 1229 |
compare = ApplySortComparator(a->datum1, a->isnull1, |
| 1230 |
b->datum1, b->isnull1, |
| 1231 |
sortKey); |
| 1232 |
if (compare != 0) |
| 1233 |
return compare; |
| 1234 |
|
| 1235 |
/* Compare additional sort keys */ |
| 1236 |
return comparetup_heap_tiebreak(a, b, state); |
| 1237 |
} |
| 1238 |
|
| 1239 |
static int |
| 1240 |
comparetup_heap_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state) |
| 1241 |
{ |
| 1242 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1243 |
SortSupport sortKey = base->sortKeys; |
| 1244 |
HeapTupleData ltup; |
| 1245 |
HeapTupleData rtup; |
| 1246 |
TupleDesc tupDesc; |
| 1247 |
int nkey; |
| 1248 |
int32 compare; |
| 1249 |
AttrNumber attno; |
| 1250 |
Datum datum1, |
| 1251 |
datum2; |
| 1252 |
bool isnull1, |
| 1253 |
isnull2; |
| 1254 |
|
| 1255 |
ltup.t_len = ((MinimalTuple) a->tuple)->t_len + MINIMAL_TUPLE_OFFSET; |
| 1256 |
ltup.t_data = (HeapTupleHeader) ((char *) a->tuple - MINIMAL_TUPLE_OFFSET); |
| 1257 |
rtup.t_len = ((MinimalTuple) b->tuple)->t_len + MINIMAL_TUPLE_OFFSET; |
| 1258 |
rtup.t_data = (HeapTupleHeader) ((char *) b->tuple - MINIMAL_TUPLE_OFFSET); |
| 1259 |
tupDesc = (TupleDesc) base->arg; |
| 1260 |
|
| 1261 |
if (sortKey->abbrev_converter) |
| 1262 |
{ |
| 1263 |
attno = sortKey->ssup_attno; |
| 1264 |
|
| 1265 |
datum1 = heap_getattr(<up, attno, tupDesc, &isnull1); |
| 1266 |
datum2 = heap_getattr(&rtup, attno, tupDesc, &isnull2); |
| 1267 |
|
| 1268 |
compare = ApplySortAbbrevFullComparator(datum1, isnull1, |
| 1269 |
datum2, isnull2, |
| 1270 |
sortKey); |
| 1271 |
if (compare != 0) |
| 1272 |
return compare; |
| 1273 |
} |
| 1274 |
|
| 1275 |
sortKey++; |
| 1276 |
for (nkey = 1; nkey < base->nKeys; nkey++, sortKey++) |
| 1277 |
{ |
| 1278 |
attno = sortKey->ssup_attno; |
| 1279 |
|
| 1280 |
datum1 = heap_getattr(<up, attno, tupDesc, &isnull1); |
| 1281 |
datum2 = heap_getattr(&rtup, attno, tupDesc, &isnull2); |
| 1282 |
|
| 1283 |
compare = ApplySortComparator(datum1, isnull1, |
| 1284 |
datum2, isnull2, |
| 1285 |
sortKey); |
| 1286 |
if (compare != 0) |
| 1287 |
return compare; |
| 1288 |
} |
| 1289 |
|
| 1290 |
return 0; |
| 1291 |
} |
| 1292 |
|
| 1293 |
static void |
| 1294 |
writetup_heap(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup) |
| 1295 |
{ |
| 1296 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1297 |
MinimalTuple tuple = (MinimalTuple) stup->tuple; |
| 1298 |
|
| 1299 |
/* the part of the MinimalTuple we'll write: */ |
| 1300 |
char *tupbody = (char *) tuple + MINIMAL_TUPLE_DATA_OFFSET; |
| 1301 |
unsigned int tupbodylen = tuple->t_len - MINIMAL_TUPLE_DATA_OFFSET; |
| 1302 |
|
| 1303 |
/* total on-disk footprint: */ |
| 1304 |
unsigned int tuplen = tupbodylen + sizeof(int); |
| 1305 |
|
| 1306 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1307 |
LogicalTapeWrite(tape, tupbody, tupbodylen); |
| 1308 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1309 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1310 |
} |
| 1311 |
|
| 1312 |
static void |
| 1313 |
readtup_heap(Tuplesortstate *state, SortTuple *stup, |
| 1314 |
LogicalTape *tape, unsigned int len) |
| 1315 |
{ |
| 1316 |
unsigned int tupbodylen = len - sizeof(int); |
| 1317 |
unsigned int tuplen = tupbodylen + MINIMAL_TUPLE_DATA_OFFSET; |
| 1318 |
MinimalTuple tuple = (MinimalTuple) tuplesort_readtup_alloc(state, tuplen); |
| 1319 |
char *tupbody = (char *) tuple + MINIMAL_TUPLE_DATA_OFFSET; |
| 1320 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1321 |
HeapTupleData htup; |
| 1322 |
|
| 1323 |
/* read in the tuple proper */ |
| 1324 |
tuple->t_len = tuplen; |
| 1325 |
LogicalTapeReadExact(tape, tupbody, tupbodylen); |
| 1326 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1327 |
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen)); |
| 1328 |
stup->tuple = tuple; |
| 1329 |
/* set up first-column key value */ |
| 1330 |
htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET; |
| 1331 |
htup.t_data = (HeapTupleHeader) ((char *) tuple - MINIMAL_TUPLE_OFFSET); |
| 1332 |
stup->datum1 = heap_getattr(&htup, |
| 1333 |
base->sortKeys[0].ssup_attno, |
| 1334 |
(TupleDesc) base->arg, |
| 1335 |
&stup->isnull1); |
| 1336 |
} |
| 1337 |
|
| 1338 |
/* |
| 1339 |
* Routines specialized for the CLUSTER case (HeapTuple data, with |
| 1340 |
* comparisons per a btree index definition) |
| 1341 |
*/ |
| 1342 |
|
| 1343 |
static void |
| 1344 |
removeabbrev_cluster(Tuplesortstate *state, SortTuple *stups, int count) |
| 1345 |
{ |
| 1346 |
int i; |
| 1347 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1348 |
TuplesortClusterArg *arg = (TuplesortClusterArg *) base->arg; |
| 1349 |
|
| 1350 |
for (i = 0; i < count; i++) |
| 1351 |
{ |
| 1352 |
HeapTuple tup; |
| 1353 |
|
| 1354 |
tup = (HeapTuple) stups[i].tuple; |
| 1355 |
stups[i].datum1 = heap_getattr(tup, |
| 1356 |
arg->indexInfo->ii_IndexAttrNumbers[0], |
| 1357 |
arg->tupDesc, |
| 1358 |
&stups[i].isnull1); |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
static int |
| 1363 |
comparetup_cluster(const SortTuple *a, const SortTuple *b, |
| 1364 |
Tuplesortstate *state) |
| 1365 |
{ |
| 1366 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1367 |
SortSupport sortKey = base->sortKeys; |
| 1368 |
int32 compare; |
| 1369 |
|
| 1370 |
/* Compare the leading sort key, if it's simple */ |
| 1371 |
if (base->haveDatum1) |
| 1372 |
{ |
| 1373 |
compare = ApplySortComparator(a->datum1, a->isnull1, |
| 1374 |
b->datum1, b->isnull1, |
| 1375 |
sortKey); |
| 1376 |
if (compare != 0) |
| 1377 |
return compare; |
| 1378 |
} |
| 1379 |
|
| 1380 |
return comparetup_cluster_tiebreak(a, b, state); |
| 1381 |
} |
| 1382 |
|
| 1383 |
static int |
| 1384 |
comparetup_cluster_tiebreak(const SortTuple *a, const SortTuple *b, |
| 1385 |
Tuplesortstate *state) |
| 1386 |
{ |
| 1387 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1388 |
TuplesortClusterArg *arg = (TuplesortClusterArg *) base->arg; |
| 1389 |
SortSupport sortKey = base->sortKeys; |
| 1390 |
HeapTuple ltup; |
| 1391 |
HeapTuple rtup; |
| 1392 |
TupleDesc tupDesc; |
| 1393 |
int nkey; |
| 1394 |
int32 compare = 0; |
| 1395 |
Datum datum1, |
| 1396 |
datum2; |
| 1397 |
bool isnull1, |
| 1398 |
isnull2; |
| 1399 |
|
| 1400 |
ltup = (HeapTuple) a->tuple; |
| 1401 |
rtup = (HeapTuple) b->tuple; |
| 1402 |
tupDesc = arg->tupDesc; |
| 1403 |
|
| 1404 |
/* Compare the leading sort key, if it's simple */ |
| 1405 |
if (base->haveDatum1) |
| 1406 |
{ |
| 1407 |
if (sortKey->abbrev_converter) |
| 1408 |
{ |
| 1409 |
AttrNumber leading = arg->indexInfo->ii_IndexAttrNumbers[0]; |
| 1410 |
|
| 1411 |
datum1 = heap_getattr(ltup, leading, tupDesc, &isnull1); |
| 1412 |
datum2 = heap_getattr(rtup, leading, tupDesc, &isnull2); |
| 1413 |
|
| 1414 |
compare = ApplySortAbbrevFullComparator(datum1, isnull1, |
| 1415 |
datum2, isnull2, |
| 1416 |
sortKey); |
| 1417 |
} |
| 1418 |
if (compare != 0 || base->nKeys == 1) |
| 1419 |
return compare; |
| 1420 |
/* Compare additional columns the hard way */ |
| 1421 |
sortKey++; |
| 1422 |
nkey = 1; |
| 1423 |
} |
| 1424 |
else |
| 1425 |
{ |
| 1426 |
/* Must compare all keys the hard way */ |
| 1427 |
nkey = 0; |
| 1428 |
} |
| 1429 |
|
| 1430 |
if (arg->indexInfo->ii_Expressions == NULL) |
| 1431 |
{ |
| 1432 |
/* If not expression index, just compare the proper heap attrs */ |
| 1433 |
|
| 1434 |
for (; nkey < base->nKeys; nkey++, sortKey++) |
| 1435 |
{ |
| 1436 |
AttrNumber attno = arg->indexInfo->ii_IndexAttrNumbers[nkey]; |
| 1437 |
|
| 1438 |
datum1 = heap_getattr(ltup, attno, tupDesc, &isnull1); |
| 1439 |
datum2 = heap_getattr(rtup, attno, tupDesc, &isnull2); |
| 1440 |
|
| 1441 |
compare = ApplySortComparator(datum1, isnull1, |
| 1442 |
datum2, isnull2, |
| 1443 |
sortKey); |
| 1444 |
if (compare != 0) |
| 1445 |
return compare; |
| 1446 |
} |
| 1447 |
} |
| 1448 |
else |
| 1449 |
{ |
| 1450 |
/* |
| 1451 |
* In the expression index case, compute the whole index tuple and |
| 1452 |
* then compare values. It would perhaps be faster to compute only as |
| 1453 |
* many columns as we need to compare, but that would require |
| 1454 |
* duplicating all the logic in FormIndexDatum. |
| 1455 |
*/ |
| 1456 |
Datum l_index_values[INDEX_MAX_KEYS]; |
| 1457 |
bool l_index_isnull[INDEX_MAX_KEYS]; |
| 1458 |
Datum r_index_values[INDEX_MAX_KEYS]; |
| 1459 |
bool r_index_isnull[INDEX_MAX_KEYS]; |
| 1460 |
TupleTableSlot *ecxt_scantuple; |
| 1461 |
|
| 1462 |
/* Reset context each time to prevent memory leakage */ |
| 1463 |
ResetPerTupleExprContext(arg->estate); |
| 1464 |
|
| 1465 |
ecxt_scantuple = GetPerTupleExprContext(arg->estate)->ecxt_scantuple; |
| 1466 |
|
| 1467 |
ExecStoreHeapTuple(ltup, ecxt_scantuple, false); |
| 1468 |
FormIndexDatum(arg->indexInfo, ecxt_scantuple, arg->estate, |
| 1469 |
l_index_values, l_index_isnull); |
| 1470 |
|
| 1471 |
ExecStoreHeapTuple(rtup, ecxt_scantuple, false); |
| 1472 |
FormIndexDatum(arg->indexInfo, ecxt_scantuple, arg->estate, |
| 1473 |
r_index_values, r_index_isnull); |
| 1474 |
|
| 1475 |
for (; nkey < base->nKeys; nkey++, sortKey++) |
| 1476 |
{ |
| 1477 |
compare = ApplySortComparator(l_index_values[nkey], |
| 1478 |
l_index_isnull[nkey], |
| 1479 |
r_index_values[nkey], |
| 1480 |
r_index_isnull[nkey], |
| 1481 |
sortKey); |
| 1482 |
if (compare != 0) |
| 1483 |
return compare; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
return 0; |
| 1488 |
} |
| 1489 |
|
| 1490 |
static void |
| 1491 |
writetup_cluster(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup) |
| 1492 |
{ |
| 1493 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1494 |
HeapTuple tuple = (HeapTuple) stup->tuple; |
| 1495 |
unsigned int tuplen = tuple->t_len + sizeof(ItemPointerData) + sizeof(int); |
| 1496 |
|
| 1497 |
/* We need to store t_self, but not other fields of HeapTupleData */ |
| 1498 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1499 |
LogicalTapeWrite(tape, &tuple->t_self, sizeof(ItemPointerData)); |
| 1500 |
LogicalTapeWrite(tape, tuple->t_data, tuple->t_len); |
| 1501 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1502 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1503 |
} |
| 1504 |
|
| 1505 |
static void |
| 1506 |
readtup_cluster(Tuplesortstate *state, SortTuple *stup, |
| 1507 |
LogicalTape *tape, unsigned int tuplen) |
| 1508 |
{ |
| 1509 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1510 |
TuplesortClusterArg *arg = (TuplesortClusterArg *) base->arg; |
| 1511 |
unsigned int t_len = tuplen - sizeof(ItemPointerData) - sizeof(int); |
| 1512 |
HeapTuple tuple = (HeapTuple) tuplesort_readtup_alloc(state, |
| 1513 |
t_len + HEAPTUPLESIZE); |
| 1514 |
|
| 1515 |
/* Reconstruct the HeapTupleData header */ |
| 1516 |
tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); |
| 1517 |
tuple->t_len = t_len; |
| 1518 |
LogicalTapeReadExact(tape, &tuple->t_self, sizeof(ItemPointerData)); |
| 1519 |
/* We don't currently bother to reconstruct t_tableOid */ |
| 1520 |
tuple->t_tableOid = InvalidOid; |
| 1521 |
/* Read in the tuple body */ |
| 1522 |
LogicalTapeReadExact(tape, tuple->t_data, tuple->t_len); |
| 1523 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1524 |
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen)); |
| 1525 |
stup->tuple = tuple; |
| 1526 |
/* set up first-column key value, if it's a simple column */ |
| 1527 |
if (base->haveDatum1) |
| 1528 |
stup->datum1 = heap_getattr(tuple, |
| 1529 |
arg->indexInfo->ii_IndexAttrNumbers[0], |
| 1530 |
arg->tupDesc, |
| 1531 |
&stup->isnull1); |
| 1532 |
} |
| 1533 |
|
| 1534 |
static void |
| 1535 |
freestate_cluster(Tuplesortstate *state) |
| 1536 |
{ |
| 1537 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1538 |
TuplesortClusterArg *arg = (TuplesortClusterArg *) base->arg; |
| 1539 |
|
| 1540 |
/* Free any execution state created for CLUSTER case */ |
| 1541 |
if (arg->estate != NULL) |
| 1542 |
{ |
| 1543 |
ExprContext *econtext = GetPerTupleExprContext(arg->estate); |
| 1544 |
|
| 1545 |
ExecDropSingleTupleTableSlot(econtext->ecxt_scantuple); |
| 1546 |
FreeExecutorState(arg->estate); |
| 1547 |
} |
| 1548 |
} |
| 1549 |
|
| 1550 |
/* |
| 1551 |
* Routines specialized for IndexTuple case |
| 1552 |
* |
| 1553 |
* The btree and hash cases require separate comparison functions, but the |
| 1554 |
* IndexTuple representation is the same so the copy/write/read support |
| 1555 |
* functions can be shared. |
| 1556 |
*/ |
| 1557 |
|
| 1558 |
static void |
| 1559 |
removeabbrev_index(Tuplesortstate *state, SortTuple *stups, int count) |
| 1560 |
{ |
| 1561 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1562 |
TuplesortIndexArg *arg = (TuplesortIndexArg *) base->arg; |
| 1563 |
int i; |
| 1564 |
|
| 1565 |
for (i = 0; i < count; i++) |
| 1566 |
{ |
| 1567 |
IndexTuple tuple; |
| 1568 |
|
| 1569 |
tuple = stups[i].tuple; |
| 1570 |
stups[i].datum1 = index_getattr(tuple, |
| 1571 |
1, |
| 1572 |
RelationGetDescr(arg->indexRel), |
| 1573 |
&stups[i].isnull1); |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
static int |
| 1578 |
comparetup_index_btree(const SortTuple *a, const SortTuple *b, |
| 1579 |
Tuplesortstate *state) |
| 1580 |
{ |
| 1581 |
/* |
| 1582 |
* This is similar to comparetup_heap(), but expects index tuples. There |
| 1583 |
* is also special handling for enforcing uniqueness, and special |
| 1584 |
* treatment for equal keys at the end. |
| 1585 |
*/ |
| 1586 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1587 |
SortSupport sortKey = base->sortKeys; |
| 1588 |
int32 compare; |
| 1589 |
|
| 1590 |
/* Compare the leading sort key */ |
| 1591 |
compare = ApplySortComparator(a->datum1, a->isnull1, |
| 1592 |
b->datum1, b->isnull1, |
| 1593 |
sortKey); |
| 1594 |
if (compare != 0) |
| 1595 |
return compare; |
| 1596 |
|
| 1597 |
/* Compare additional sort keys */ |
| 1598 |
return comparetup_index_btree_tiebreak(a, b, state); |
| 1599 |
} |
| 1600 |
|
| 1601 |
static int |
| 1602 |
comparetup_index_btree_tiebreak(const SortTuple *a, const SortTuple *b, |
| 1603 |
Tuplesortstate *state) |
| 1604 |
{ |
| 1605 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1606 |
TuplesortIndexBTreeArg *arg = (TuplesortIndexBTreeArg *) base->arg; |
| 1607 |
SortSupport sortKey = base->sortKeys; |
| 1608 |
IndexTuple tuple1; |
| 1609 |
IndexTuple tuple2; |
| 1610 |
int keysz; |
| 1611 |
TupleDesc tupDes; |
| 1612 |
bool equal_hasnull = false; |
| 1613 |
int nkey; |
| 1614 |
int32 compare; |
| 1615 |
Datum datum1, |
| 1616 |
datum2; |
| 1617 |
bool isnull1, |
| 1618 |
isnull2; |
| 1619 |
|
| 1620 |
tuple1 = (IndexTuple) a->tuple; |
| 1621 |
tuple2 = (IndexTuple) b->tuple; |
| 1622 |
keysz = base->nKeys; |
| 1623 |
tupDes = RelationGetDescr(arg->index.indexRel); |
| 1624 |
|
| 1625 |
if (sortKey->abbrev_converter) |
| 1626 |
{ |
| 1627 |
datum1 = index_getattr(tuple1, 1, tupDes, &isnull1); |
| 1628 |
datum2 = index_getattr(tuple2, 1, tupDes, &isnull2); |
| 1629 |
|
| 1630 |
compare = ApplySortAbbrevFullComparator(datum1, isnull1, |
| 1631 |
datum2, isnull2, |
| 1632 |
sortKey); |
| 1633 |
if (compare != 0) |
| 1634 |
return compare; |
| 1635 |
} |
| 1636 |
|
| 1637 |
/* they are equal, so we only need to examine one null flag */ |
| 1638 |
if (a->isnull1) |
| 1639 |
equal_hasnull = true; |
| 1640 |
|
| 1641 |
sortKey++; |
| 1642 |
for (nkey = 2; nkey <= keysz; nkey++, sortKey++) |
| 1643 |
{ |
| 1644 |
datum1 = index_getattr(tuple1, nkey, tupDes, &isnull1); |
| 1645 |
datum2 = index_getattr(tuple2, nkey, tupDes, &isnull2); |
| 1646 |
|
| 1647 |
compare = ApplySortComparator(datum1, isnull1, |
| 1648 |
datum2, isnull2, |
| 1649 |
sortKey); |
| 1650 |
if (compare != 0) |
| 1651 |
return compare; /* done when we find unequal attributes */ |
| 1652 |
|
| 1653 |
/* they are equal, so we only need to examine one null flag */ |
| 1654 |
if (isnull1) |
| 1655 |
equal_hasnull = true; |
| 1656 |
} |
| 1657 |
|
| 1658 |
/* |
| 1659 |
* If btree has asked us to enforce uniqueness, complain if two equal |
| 1660 |
* tuples are detected (unless there was at least one NULL field and NULLS |
| 1661 |
* NOT DISTINCT was not set). |
| 1662 |
* |
| 1663 |
* It is sufficient to make the test here, because if two tuples are equal |
| 1664 |
* they *must* get compared at some stage of the sort --- otherwise the |
| 1665 |
* sort algorithm wouldn't have checked whether one must appear before the |
| 1666 |
* other. |
| 1667 |
*/ |
| 1668 |
if (arg->enforceUnique && !(!arg->uniqueNullsNotDistinct && equal_hasnull)) |
| 1669 |
{ |
| 1670 |
Datum values[INDEX_MAX_KEYS]; |
| 1671 |
bool isnull[INDEX_MAX_KEYS]; |
| 1672 |
char *key_desc; |
| 1673 |
|
| 1674 |
/* |
| 1675 |
* Some rather brain-dead implementations of qsort (such as the one in |
| 1676 |
* QNX 4) will sometimes call the comparison routine to compare a |
| 1677 |
* value to itself, but we always use our own implementation, which |
| 1678 |
* does not. |
| 1679 |
*/ |
| 1680 |
Assert(tuple1 != tuple2); |
| 1681 |
|
| 1682 |
index_deform_tuple(tuple1, tupDes, values, isnull); |
| 1683 |
|
| 1684 |
key_desc = BuildIndexValueDescription(arg->index.indexRel, values, isnull); |
| 1685 |
|
| 1686 |
ereport(ERROR, |
| 1687 |
(errcode(ERRCODE_UNIQUE_VIOLATION), |
| 1688 |
errmsg("could not create unique index \"%s\"", |
| 1689 |
RelationGetRelationName(arg->index.indexRel)), |
| 1690 |
key_desc ? errdetail("Key %s is duplicated.", key_desc) : |
| 1691 |
errdetail("Duplicate keys exist."), |
| 1692 |
errtableconstraint(arg->index.heapRel, |
| 1693 |
RelationGetRelationName(arg->index.indexRel)))); |
| 1694 |
} |
| 1695 |
|
| 1696 |
/* |
| 1697 |
* If key values are equal, we sort on ItemPointer. This is required for |
| 1698 |
* btree indexes, since heap TID is treated as an implicit last key |
| 1699 |
* attribute in order to ensure that all keys in the index are physically |
| 1700 |
* unique. |
| 1701 |
*/ |
| 1702 |
{ |
| 1703 |
BlockNumber blk1 = ItemPointerGetBlockNumber(&tuple1->t_tid); |
| 1704 |
BlockNumber blk2 = ItemPointerGetBlockNumber(&tuple2->t_tid); |
| 1705 |
|
| 1706 |
if (blk1 != blk2) |
| 1707 |
return (blk1 < blk2) ? -1 : 1; |
| 1708 |
} |
| 1709 |
{ |
| 1710 |
OffsetNumber pos1 = ItemPointerGetOffsetNumber(&tuple1->t_tid); |
| 1711 |
OffsetNumber pos2 = ItemPointerGetOffsetNumber(&tuple2->t_tid); |
| 1712 |
|
| 1713 |
if (pos1 != pos2) |
| 1714 |
return (pos1 < pos2) ? -1 : 1; |
| 1715 |
} |
| 1716 |
|
| 1717 |
/* ItemPointer values should never be equal */ |
| 1718 |
Assert(false); |
| 1719 |
|
| 1720 |
return 0; |
| 1721 |
} |
| 1722 |
|
| 1723 |
static int |
| 1724 |
comparetup_index_hash(const SortTuple *a, const SortTuple *b, |
| 1725 |
Tuplesortstate *state) |
| 1726 |
{ |
| 1727 |
Bucket bucket1; |
| 1728 |
Bucket bucket2; |
| 1729 |
uint32 hash1; |
| 1730 |
uint32 hash2; |
| 1731 |
IndexTuple tuple1; |
| 1732 |
IndexTuple tuple2; |
| 1733 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1734 |
TuplesortIndexHashArg *arg = (TuplesortIndexHashArg *) base->arg; |
| 1735 |
|
| 1736 |
/* |
| 1737 |
* Fetch hash keys and mask off bits we don't want to sort by, so that the |
| 1738 |
* initial sort is just on the bucket number. We know that the first |
| 1739 |
* column of the index tuple is the hash key. |
| 1740 |
*/ |
| 1741 |
Assert(!a->isnull1); |
| 1742 |
bucket1 = _hash_hashkey2bucket(DatumGetUInt32(a->datum1), |
| 1743 |
arg->max_buckets, arg->high_mask, |
| 1744 |
arg->low_mask); |
| 1745 |
Assert(!b->isnull1); |
| 1746 |
bucket2 = _hash_hashkey2bucket(DatumGetUInt32(b->datum1), |
| 1747 |
arg->max_buckets, arg->high_mask, |
| 1748 |
arg->low_mask); |
| 1749 |
if (bucket1 > bucket2) |
| 1750 |
return 1; |
| 1751 |
else if (bucket1 < bucket2) |
| 1752 |
return -1; |
| 1753 |
|
| 1754 |
/* |
| 1755 |
* If bucket values are equal, sort by hash values. This allows us to |
| 1756 |
* insert directly onto bucket/overflow pages, where the index tuples are |
| 1757 |
* stored in hash order to allow fast binary search within each page. |
| 1758 |
*/ |
| 1759 |
hash1 = DatumGetUInt32(a->datum1); |
| 1760 |
hash2 = DatumGetUInt32(b->datum1); |
| 1761 |
if (hash1 > hash2) |
| 1762 |
return 1; |
| 1763 |
else if (hash1 < hash2) |
| 1764 |
return -1; |
| 1765 |
|
| 1766 |
/* |
| 1767 |
* If hash values are equal, we sort on ItemPointer. This does not affect |
| 1768 |
* validity of the finished index, but it may be useful to have index |
| 1769 |
* scans in physical order. |
| 1770 |
*/ |
| 1771 |
tuple1 = (IndexTuple) a->tuple; |
| 1772 |
tuple2 = (IndexTuple) b->tuple; |
| 1773 |
|
| 1774 |
{ |
| 1775 |
BlockNumber blk1 = ItemPointerGetBlockNumber(&tuple1->t_tid); |
| 1776 |
BlockNumber blk2 = ItemPointerGetBlockNumber(&tuple2->t_tid); |
| 1777 |
|
| 1778 |
if (blk1 != blk2) |
| 1779 |
return (blk1 < blk2) ? -1 : 1; |
| 1780 |
} |
| 1781 |
{ |
| 1782 |
OffsetNumber pos1 = ItemPointerGetOffsetNumber(&tuple1->t_tid); |
| 1783 |
OffsetNumber pos2 = ItemPointerGetOffsetNumber(&tuple2->t_tid); |
| 1784 |
|
| 1785 |
if (pos1 != pos2) |
| 1786 |
return (pos1 < pos2) ? -1 : 1; |
| 1787 |
} |
| 1788 |
|
| 1789 |
/* ItemPointer values should never be equal */ |
| 1790 |
Assert(false); |
| 1791 |
|
| 1792 |
return 0; |
| 1793 |
} |
| 1794 |
|
| 1795 |
/* |
| 1796 |
* Sorting for hash indexes only uses one sort key, so this shouldn't ever be |
| 1797 |
* called. It's only here for consistency. |
| 1798 |
*/ |
| 1799 |
static int |
| 1800 |
comparetup_index_hash_tiebreak(const SortTuple *a, const SortTuple *b, |
| 1801 |
Tuplesortstate *state) |
| 1802 |
{ |
| 1803 |
Assert(false); |
| 1804 |
|
| 1805 |
return 0; |
| 1806 |
} |
| 1807 |
|
| 1808 |
static void |
| 1809 |
writetup_index(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup) |
| 1810 |
{ |
| 1811 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1812 |
IndexTuple tuple = (IndexTuple) stup->tuple; |
| 1813 |
unsigned int tuplen; |
| 1814 |
|
| 1815 |
tuplen = IndexTupleSize(tuple) + sizeof(tuplen); |
| 1816 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1817 |
LogicalTapeWrite(tape, tuple, IndexTupleSize(tuple)); |
| 1818 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1819 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1820 |
} |
| 1821 |
|
| 1822 |
static void |
| 1823 |
readtup_index(Tuplesortstate *state, SortTuple *stup, |
| 1824 |
LogicalTape *tape, unsigned int len) |
| 1825 |
{ |
| 1826 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1827 |
TuplesortIndexArg *arg = (TuplesortIndexArg *) base->arg; |
| 1828 |
unsigned int tuplen = len - sizeof(unsigned int); |
| 1829 |
IndexTuple tuple = (IndexTuple) tuplesort_readtup_alloc(state, tuplen); |
| 1830 |
|
| 1831 |
LogicalTapeReadExact(tape, tuple, tuplen); |
| 1832 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1833 |
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen)); |
| 1834 |
stup->tuple = tuple; |
| 1835 |
/* set up first-column key value */ |
| 1836 |
stup->datum1 = index_getattr(tuple, |
| 1837 |
1, |
| 1838 |
RelationGetDescr(arg->indexRel), |
| 1839 |
&stup->isnull1); |
| 1840 |
} |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Routines specialized for BrinTuple case |
| 1844 |
*/ |
| 1845 |
|
| 1846 |
static void |
| 1847 |
removeabbrev_index_brin(Tuplesortstate *state, SortTuple *stups, int count) |
| 1848 |
{ |
| 1849 |
int i; |
| 1850 |
|
| 1851 |
for (i = 0; i < count; i++) |
| 1852 |
{ |
| 1853 |
BrinSortTuple *tuple; |
| 1854 |
|
| 1855 |
tuple = stups[i].tuple; |
| 1856 |
stups[i].datum1 = tuple->tuple.bt_blkno; |
| 1857 |
} |
| 1858 |
} |
| 1859 |
|
| 1860 |
static int |
| 1861 |
comparetup_index_brin(const SortTuple *a, const SortTuple *b, |
| 1862 |
Tuplesortstate *state) |
| 1863 |
{ |
| 1864 |
Assert(TuplesortstateGetPublic(state)->haveDatum1); |
| 1865 |
|
| 1866 |
if (DatumGetUInt32(a->datum1) > DatumGetUInt32(b->datum1)) |
| 1867 |
return 1; |
| 1868 |
|
| 1869 |
if (DatumGetUInt32(a->datum1) < DatumGetUInt32(b->datum1)) |
| 1870 |
return -1; |
| 1871 |
|
| 1872 |
/* silence compilers */ |
| 1873 |
return 0; |
| 1874 |
} |
| 1875 |
|
| 1876 |
static void |
| 1877 |
writetup_index_brin(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup) |
| 1878 |
{ |
| 1879 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1880 |
BrinSortTuple *tuple = (BrinSortTuple *) stup->tuple; |
| 1881 |
unsigned int tuplen = tuple->tuplen; |
| 1882 |
|
| 1883 |
tuplen = tuplen + sizeof(tuplen); |
| 1884 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1885 |
LogicalTapeWrite(tape, &tuple->tuple, tuple->tuplen); |
| 1886 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1887 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1888 |
} |
| 1889 |
|
| 1890 |
static void |
| 1891 |
readtup_index_brin(Tuplesortstate *state, SortTuple *stup, |
| 1892 |
LogicalTape *tape, unsigned int len) |
| 1893 |
{ |
| 1894 |
BrinSortTuple *tuple; |
| 1895 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1896 |
unsigned int tuplen = len - sizeof(unsigned int); |
| 1897 |
|
| 1898 |
/* |
| 1899 |
* Allocate space for the BRIN sort tuple, which is BrinTuple with an |
| 1900 |
* extra length field. |
| 1901 |
*/ |
| 1902 |
tuple = (BrinSortTuple *) tuplesort_readtup_alloc(state, |
| 1903 |
BRINSORTTUPLE_SIZE(tuplen)); |
| 1904 |
|
| 1905 |
tuple->tuplen = tuplen; |
| 1906 |
|
| 1907 |
LogicalTapeReadExact(tape, &tuple->tuple, tuplen); |
| 1908 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1909 |
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen)); |
| 1910 |
stup->tuple = tuple; |
| 1911 |
|
| 1912 |
/* set up first-column key value, which is block number */ |
| 1913 |
stup->datum1 = tuple->tuple.bt_blkno; |
| 1914 |
} |
| 1915 |
|
| 1916 |
/* |
| 1917 |
* Routines specialized for GIN case |
| 1918 |
*/ |
| 1919 |
|
| 1920 |
static void |
| 1921 |
removeabbrev_index_gin(Tuplesortstate *state, SortTuple *stups, int count) |
| 1922 |
{ |
| 1923 |
Assert(false); |
| 1924 |
elog(ERROR, "removeabbrev_index_gin not implemented"); |
| 1925 |
} |
| 1926 |
|
| 1927 |
static int |
| 1928 |
comparetup_index_gin(const SortTuple *a, const SortTuple *b, |
| 1929 |
Tuplesortstate *state) |
| 1930 |
{ |
| 1931 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1932 |
|
| 1933 |
Assert(!TuplesortstateGetPublic(state)->haveDatum1); |
| 1934 |
|
| 1935 |
return _gin_compare_tuples((GinTuple *) a->tuple, |
| 1936 |
(GinTuple *) b->tuple, |
| 1937 |
base->sortKeys); |
| 1938 |
} |
| 1939 |
|
| 1940 |
static void |
| 1941 |
writetup_index_gin(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup) |
| 1942 |
{ |
| 1943 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1944 |
GinTuple *tuple = (GinTuple *) stup->tuple; |
| 1945 |
unsigned int tuplen = tuple->tuplen; |
| 1946 |
|
| 1947 |
tuplen = tuplen + sizeof(tuplen); |
| 1948 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1949 |
LogicalTapeWrite(tape, tuple, tuple->tuplen); |
| 1950 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1951 |
LogicalTapeWrite(tape, &tuplen, sizeof(tuplen)); |
| 1952 |
} |
| 1953 |
|
| 1954 |
static void |
| 1955 |
readtup_index_gin(Tuplesortstate *state, SortTuple *stup, |
| 1956 |
LogicalTape *tape, unsigned int len) |
| 1957 |
{ |
| 1958 |
GinTuple *tuple; |
| 1959 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1960 |
unsigned int tuplen = len - sizeof(unsigned int); |
| 1961 |
|
| 1962 |
/* |
| 1963 |
* Allocate space for the GIN sort tuple, which already has the proper |
| 1964 |
* length included in the header. |
| 1965 |
*/ |
| 1966 |
tuple = (GinTuple *) tuplesort_readtup_alloc(state, tuplen); |
| 1967 |
|
| 1968 |
tuple->tuplen = tuplen; |
| 1969 |
|
| 1970 |
LogicalTapeReadExact(tape, tuple, tuplen); |
| 1971 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 1972 |
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen)); |
| 1973 |
stup->tuple = (void *) tuple; |
| 1974 |
|
| 1975 |
/* no abbreviations (FIXME maybe use attrnum for this?) */ |
| 1976 |
stup->datum1 = (Datum) 0; |
| 1977 |
} |
| 1978 |
|
| 1979 |
/* |
| 1980 |
* Routines specialized for DatumTuple case |
| 1981 |
*/ |
| 1982 |
|
| 1983 |
static void |
| 1984 |
removeabbrev_datum(Tuplesortstate *state, SortTuple *stups, int count) |
| 1985 |
{ |
| 1986 |
int i; |
| 1987 |
|
| 1988 |
for (i = 0; i < count; i++) |
| 1989 |
stups[i].datum1 = PointerGetDatum(stups[i].tuple); |
| 1990 |
} |
| 1991 |
|
| 1992 |
static int |
| 1993 |
comparetup_datum(const SortTuple *a, const SortTuple *b, Tuplesortstate *state) |
| 1994 |
{ |
| 1995 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 1996 |
int compare; |
| 1997 |
|
| 1998 |
compare = ApplySortComparator(a->datum1, a->isnull1, |
| 1999 |
b->datum1, b->isnull1, |
| 2000 |
base->sortKeys); |
| 2001 |
if (compare != 0) |
| 2002 |
return compare; |
| 2003 |
|
| 2004 |
return comparetup_datum_tiebreak(a, b, state); |
| 2005 |
} |
| 2006 |
|
| 2007 |
static int |
| 2008 |
comparetup_datum_tiebreak(const SortTuple *a, const SortTuple *b, Tuplesortstate *state) |
| 2009 |
{ |
| 2010 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 2011 |
int32 compare = 0; |
| 2012 |
|
| 2013 |
/* if we have abbreviations, then "tuple" has the original value */ |
| 2014 |
if (base->sortKeys->abbrev_converter) |
| 2015 |
compare = ApplySortAbbrevFullComparator(PointerGetDatum(a->tuple), a->isnull1, |
| 2016 |
PointerGetDatum(b->tuple), b->isnull1, |
| 2017 |
base->sortKeys); |
| 2018 |
|
| 2019 |
return compare; |
| 2020 |
} |
| 2021 |
|
| 2022 |
static void |
| 2023 |
writetup_datum(Tuplesortstate *state, LogicalTape *tape, SortTuple *stup) |
| 2024 |
{ |
| 2025 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 2026 |
TuplesortDatumArg *arg = (TuplesortDatumArg *) base->arg; |
| 2027 |
void *waddr; |
| 2028 |
unsigned int tuplen; |
| 2029 |
unsigned int writtenlen; |
| 2030 |
|
| 2031 |
if (stup->isnull1) |
| 2032 |
{ |
| 2033 |
waddr = NULL; |
| 2034 |
tuplen = 0; |
| 2035 |
} |
| 2036 |
else if (!base->tuples) |
| 2037 |
{ |
| 2038 |
waddr = &stup->datum1; |
| 2039 |
tuplen = sizeof(Datum); |
| 2040 |
} |
| 2041 |
else |
| 2042 |
{ |
| 2043 |
waddr = stup->tuple; |
| 2044 |
tuplen = datumGetSize(PointerGetDatum(stup->tuple), false, arg->datumTypeLen); |
| 2045 |
Assert(tuplen != 0); |
| 2046 |
} |
| 2047 |
|
| 2048 |
writtenlen = tuplen + sizeof(unsigned int); |
| 2049 |
|
| 2050 |
LogicalTapeWrite(tape, &writtenlen, sizeof(writtenlen)); |
| 2051 |
LogicalTapeWrite(tape, waddr, tuplen); |
| 2052 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 2053 |
LogicalTapeWrite(tape, &writtenlen, sizeof(writtenlen)); |
| 2054 |
} |
| 2055 |
|
| 2056 |
static void |
| 2057 |
readtup_datum(Tuplesortstate *state, SortTuple *stup, |
| 2058 |
LogicalTape *tape, unsigned int len) |
| 2059 |
{ |
| 2060 |
TuplesortPublic *base = TuplesortstateGetPublic(state); |
| 2061 |
unsigned int tuplen = len - sizeof(unsigned int); |
| 2062 |
|
| 2063 |
if (tuplen == 0) |
| 2064 |
{ |
| 2065 |
/* it's NULL */ |
| 2066 |
stup->datum1 = (Datum) 0; |
| 2067 |
stup->isnull1 = true; |
| 2068 |
stup->tuple = NULL; |
| 2069 |
} |
| 2070 |
else if (!base->tuples) |
| 2071 |
{ |
| 2072 |
Assert(tuplen == sizeof(Datum)); |
| 2073 |
LogicalTapeReadExact(tape, &stup->datum1, tuplen); |
| 2074 |
stup->isnull1 = false; |
| 2075 |
stup->tuple = NULL; |
| 2076 |
} |
| 2077 |
else |
| 2078 |
{ |
| 2079 |
void *raddr = tuplesort_readtup_alloc(state, tuplen); |
| 2080 |
|
| 2081 |
LogicalTapeReadExact(tape, raddr, tuplen); |
| 2082 |
stup->datum1 = PointerGetDatum(raddr); |
| 2083 |
stup->isnull1 = false; |
| 2084 |
stup->tuple = raddr; |
| 2085 |
} |
| 2086 |
|
| 2087 |
if (base->sortopt & TUPLESORT_RANDOMACCESS) /* need trailing length word? */ |
| 2088 |
LogicalTapeReadExact(tape, &tuplen, sizeof(tuplen)); |
| 2089 |
} |