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