1 /*-------------------------------------------------------------------------
4 * Generalized tuple sorting routines.
6 * This module handles sorting of heap tuples, index tuples, or single
7 * Datums (and could easily support other kinds of sortable objects,
8 * if necessary). It works efficiently for both small and large amounts
9 * of data. Small amounts are sorted in-memory using qsort(). Large
10 * amounts are sorted using temporary files and a standard external sort
13 * See Knuth, volume 3, for more than you want to know about the external
14 * sorting algorithm. We divide the input into sorted runs using replacement
15 * selection, in the form of a priority tree implemented as a heap
16 * (essentially his Algorithm 5.2.3H), then merge the runs using polyphase
17 * merge, Knuth's Algorithm 5.4.2D. The logical "tapes" used by Algorithm D
18 * are implemented by logtape.c, which avoids space wastage by recycling
19 * disk space as soon as each block is read from its "tape".
21 * We do not form the initial runs using Knuth's recommended replacement
22 * selection data structure (Algorithm 5.4.1R), because it uses a fixed
23 * number of records in memory at all times. Since we are dealing with
24 * tuples that may vary considerably in size, we want to be able to vary
25 * the number of records kept in memory to ensure full utilization of the
26 * allowed sort memory space. So, we keep the tuples in a variable-size
27 * heap, with the next record to go out at the top of the heap. Like
28 * Algorithm 5.4.1R, each record is stored with the run number that it
29 * must go into, and we use (run number, key) as the ordering key for the
30 * heap. When the run number at the top of the heap changes, we know that
31 * no more records of the prior run are left in the heap.
33 * The (approximate) amount of memory allowed for any one sort operation
34 * is given in kilobytes by the external variable SortMem. Initially,
35 * we absorb tuples and simply store them in an unsorted array as long as
36 * we haven't exceeded SortMem. If we reach the end of the input without
37 * exceeding SortMem, we sort the array using qsort() and subsequently return
38 * tuples just by scanning the tuple array sequentially. If we do exceed
39 * SortMem, we construct a heap using Algorithm H and begin to emit tuples
40 * into sorted runs in temporary tapes, emitting just enough tuples at each
41 * step to get back within the SortMem limit. Whenever the run number at
42 * the top of the heap changes, we begin a new run with a new output tape
43 * (selected per Algorithm D). After the end of the input is reached,
44 * we dump out remaining tuples in memory into a final run (or two),
45 * then merge the runs using Algorithm D.
47 * When merging runs, we use a heap containing just the frontmost tuple from
48 * each source run; we repeatedly output the smallest tuple and insert the
49 * next tuple from its source tape (if any). When the heap empties, the merge
50 * is complete. The basic merge algorithm thus needs very little memory ---
51 * only M tuples for an M-way merge, and M is at most six in the present code.
52 * However, we can still make good use of our full SortMem allocation by
53 * pre-reading additional tuples from each source tape. Without prereading,
54 * our access pattern to the temporary file would be very erratic; on average
55 * we'd read one block from each of M source tapes during the same time that
56 * we're writing M blocks to the output tape, so there is no sequentiality of
57 * access at all, defeating the read-ahead methods used by most Unix kernels.
58 * Worse, the output tape gets written into a very random sequence of blocks
59 * of the temp file, ensuring that things will be even worse when it comes
60 * time to read that tape. A straightforward merge pass thus ends up doing a
61 * lot of waiting for disk seeks. We can improve matters by prereading from
62 * each source tape sequentially, loading about SortMem/M bytes from each tape
63 * in turn. Then we run the merge algorithm, writing but not reading until
64 * one of the preloaded tuple series runs out. Then we switch back to preread
65 * mode, fill memory again, and repeat. This approach helps to localize both
66 * read and write accesses.
68 * When the caller requests random access to the sort result, we form
69 * the final sorted run on a logical tape which is then "frozen", so
70 * that we can access it randomly. When the caller does not need random
71 * access, we return from tuplesort_performsort() as soon as we are down
72 * to one run per logical tape. The final merge is then performed
73 * on-the-fly as the caller repeatedly calls tuplesort_gettuple; this
74 * saves one cycle of writing all the data out to disk and reading it in.
77 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
78 * Portions Copyright (c) 1994, Regents of the University of California
81 * $Header: /cvsroot/pgsql/src/backend/utils/sort/tuplesort.c,v 1.21 2001/11/11 22:00:25 tgl Exp $
83 *-------------------------------------------------------------------------
88 #include "access/heapam.h"
89 #include "access/nbtree.h"
90 #include "catalog/catname.h"
91 #include "catalog/pg_amop.h"
92 #include "catalog/pg_amproc.h"
93 #include "catalog/pg_operator.h"
94 #include "miscadmin.h"
95 #include "utils/fmgroids.h"
96 #include "utils/logtape.h"
97 #include "utils/lsyscache.h"
98 #include "utils/syscache.h"
99 #include "utils/tuplesort.h"
102 * Possible states of a Tuplesort object. These denote the states that
103 * persist between calls of Tuplesort routines.
107 TSS_INITIAL, /* Loading tuples; still within memory
109 TSS_BUILDRUNS, /* Loading tuples; writing to tape */
110 TSS_SORTEDINMEM, /* Sort completed entirely in memory */
111 TSS_SORTEDONTAPE, /* Sort completed, final run is on tape */
112 TSS_FINALMERGE /* Performing final merge on-the-fly */
116 * We use a seven-tape polyphase merge, which is the "sweet spot" on the
117 * tapes-to-passes curve according to Knuth's figure 70 (section 5.4.2).
119 #define MAXTAPES 7 /* Knuth's T */
120 #define TAPERANGE (MAXTAPES-1) /* Knuth's P */
123 * Private state of a Tuplesort operation.
125 struct Tuplesortstate
127 TupSortStatus status; /* enumerated value as shown above */
128 bool randomAccess; /* did caller request random access? */
129 long availMem; /* remaining memory available, in bytes */
130 LogicalTapeSet *tapeset; /* logtape.c object for tapes in a temp
134 * These function pointers decouple the routines that must know what
135 * kind of tuple we are sorting from the routines that don't need to
136 * know it. They are set up by the tuplesort_begin_xxx routines.
138 * Function to compare two tuples; result is per qsort() convention, ie:
140 * <0, 0, >0 according as a<b, a=b, a>b.
142 int (*comparetup) (Tuplesortstate *state, const void *a, const void *b);
145 * Function to copy a supplied input tuple into palloc'd space. (NB:
146 * we assume that a single pfree() is enough to release the tuple
147 * later, so the representation must be "flat" in one palloc chunk.)
148 * state->availMem must be decreased by the amount of space used.
150 void *(*copytup) (Tuplesortstate *state, void *tup);
153 * Function to write a stored tuple onto tape. The representation of
154 * the tuple on tape need not be the same as it is in memory;
155 * requirements on the tape representation are given below. After
156 * writing the tuple, pfree() it, and increase state->availMem by the
157 * amount of memory space thereby released.
159 void (*writetup) (Tuplesortstate *state, int tapenum, void *tup);
162 * Function to read a stored tuple from tape back into memory. 'len'
163 * is the already-read length of the stored tuple. Create and return
164 * a palloc'd copy, and decrease state->availMem by the amount of
165 * memory space consumed.
167 void *(*readtup) (Tuplesortstate *state, int tapenum, unsigned int len);
170 * Obtain memory space occupied by a stored tuple. (This routine is
171 * only needed in the FINALMERGE case, since copytup, writetup, and
172 * readtup are expected to adjust availMem appropriately.)
174 unsigned int (*tuplesize) (Tuplesortstate *state, void *tup);
177 * This array holds pointers to tuples in sort memory. If we are in
178 * state INITIAL, the tuples are in no particular order; if we are in
179 * state SORTEDINMEM, the tuples are in final sorted order; in states
180 * BUILDRUNS and FINALMERGE, the tuples are organized in "heap" order
181 * per Algorithm H. (Note that memtupcount only counts the tuples
182 * that are part of the heap --- during merge passes, memtuples[]
183 * entries beyond TAPERANGE are never in the heap and are used to hold
184 * pre-read tuples.) In state SORTEDONTAPE, the array is not used.
186 void **memtuples; /* array of pointers to palloc'd tuples */
187 int memtupcount; /* number of tuples currently present */
188 int memtupsize; /* allocated length of memtuples array */
191 * While building initial runs, this array holds the run number for
192 * each tuple in memtuples[]. During merge passes, we re-use it to
193 * hold the input tape number that each tuple in the heap was read
194 * from, or to hold the index of the next tuple pre-read from the same
195 * tape in the case of pre-read entries. This array is never
196 * allocated unless we need to use tapes. Whenever it is allocated,
197 * it has the same length as memtuples[].
199 int *memtupindex; /* index value associated with
203 * While building initial runs, this is the current output run number
204 * (starting at 0). Afterwards, it is the number of initial runs we
210 * These variables are only used during merge passes. mergeactive[i]
211 * is true if we are reading an input run from (actual) tape number i
212 * and have not yet exhausted that run. mergenext[i] is the memtuples
213 * index of the next pre-read tuple (next to be loaded into the heap)
214 * for tape i, or 0 if we are out of pre-read tuples. mergelast[i]
215 * similarly points to the last pre-read tuple from each tape.
216 * mergeavailmem[i] is the amount of unused space allocated for tape
217 * i. mergefreelist and mergefirstfree keep track of unused locations
218 * in the memtuples[] array. memtupindex[] links together pre-read
219 * tuples for each tape as well as recycled locations in
220 * mergefreelist. It is OK to use 0 as a null link in these lists,
221 * because memtuples[0] is part of the merge heap and is never a
224 bool mergeactive[MAXTAPES]; /* Active input run source? */
225 int mergenext[MAXTAPES]; /* first preread tuple for each
227 int mergelast[MAXTAPES]; /* last preread tuple for each
229 long mergeavailmem[MAXTAPES]; /* availMem for prereading
231 long spacePerTape; /* actual per-tape target usage */
232 int mergefreelist; /* head of freelist of recycled slots */
233 int mergefirstfree; /* first slot never used in this merge */
236 * Variables for Algorithm D. Note that destTape is a "logical" tape
237 * number, ie, an index into the tp_xxx[] arrays. Be careful to keep
238 * "logical" and "actual" tape numbers straight!
240 int Level; /* Knuth's l */
241 int destTape; /* current output tape (Knuth's j, less 1) */
242 int tp_fib[MAXTAPES]; /* Target Fibonacci run counts
244 int tp_runs[MAXTAPES]; /* # of real runs on each tape */
245 int tp_dummy[MAXTAPES]; /* # of dummy runs for each tape
247 int tp_tapenum[MAXTAPES]; /* Actual tape numbers (TAPE[]) */
250 * These variables are used after completion of sorting to keep track
251 * of the next tuple to return. (In the tape case, the tape's current
252 * read position is also critical state.)
254 int result_tape; /* actual tape number of finished output */
255 int current; /* array index (only used if SORTEDINMEM) */
256 bool eof_reached; /* reached EOF (needed for cursors) */
258 /* markpos_xxx holds marked position for mark and restore */
259 long markpos_block; /* tape block# (only used if SORTEDONTAPE) */
260 int markpos_offset; /* saved "current", or offset in tape
262 bool markpos_eof; /* saved "eof_reached" */
265 * These variables are specific to the HeapTuple case; they are set by
266 * tuplesort_begin_heap and used only by the HeapTuple routines.
271 SortFunctionKind *sortFnKinds;
274 * These variables are specific to the IndexTuple case; they are set
275 * by tuplesort_begin_index and used only by the IndexTuple routines.
278 ScanKey indexScanKey;
279 bool enforceUnique; /* complain if we find duplicate tuples */
282 * These variables are specific to the Datum case; they are set by
283 * tuplesort_begin_datum and used only by the DatumTuple routines.
287 FmgrInfo sortOpFn; /* cached lookup data for sortOperator */
288 SortFunctionKind sortFnKind;
289 /* we need typelen and byval in order to know how to copy the Datums. */
294 #define COMPARETUP(state,a,b) ((*(state)->comparetup) (state, a, b))
295 #define COPYTUP(state,tup) ((*(state)->copytup) (state, tup))
296 #define WRITETUP(state,tape,tup) ((*(state)->writetup) (state, tape, tup))
297 #define READTUP(state,tape,len) ((*(state)->readtup) (state, tape, len))
298 #define TUPLESIZE(state,tup) ((*(state)->tuplesize) (state, tup))
299 #define LACKMEM(state) ((state)->availMem < 0)
300 #define USEMEM(state,amt) ((state)->availMem -= (amt))
301 #define FREEMEM(state,amt) ((state)->availMem += (amt))
303 /*--------------------
305 * NOTES about on-tape representation of tuples:
307 * We require the first "unsigned int" of a stored tuple to be the total size
308 * on-tape of the tuple, including itself (so it is never zero; an all-zero
309 * unsigned int is used to delimit runs). The remainder of the stored tuple
310 * may or may not match the in-memory representation of the tuple ---
311 * any conversion needed is the job of the writetup and readtup routines.
313 * If state->randomAccess is true, then the stored representation of the
314 * tuple must be followed by another "unsigned int" that is a copy of the
315 * length --- so the total tape space used is actually sizeof(unsigned int)
316 * more than the stored length value. This allows read-backwards. When
317 * randomAccess is not true, the write/read routines may omit the extra
320 * writetup is expected to write both length words as well as the tuple
321 * data. When readtup is called, the tape is positioned just after the
322 * front length word; readtup must read the tuple data and advance past
323 * the back length word (if present).
325 * The write/read routines can make use of the tuple description data
326 * stored in the Tuplesortstate record, if needed. They are also expected
327 * to adjust state->availMem by the amount of memory space (not tape space!)
328 * released or consumed. There is no error return from either writetup
329 * or readtup; they should elog() on failure.
332 * NOTES about memory consumption calculations:
334 * We count space requested for tuples against the SortMem limit.
335 * Fixed-size space (primarily the LogicalTapeSet I/O buffers) is not
336 * counted, nor do we count the variable-size memtuples and memtupindex
337 * arrays. (Even though those could grow pretty large, they should be
338 * small compared to the tuples proper, so this is not unreasonable.)
340 * The major deficiency in this approach is that it ignores palloc overhead.
341 * The memory space actually allocated for a palloc chunk is always more
342 * than the request size, and could be considerably more (as much as 2X
343 * larger, in the current aset.c implementation). So the space used could
344 * be considerably more than SortMem says.
346 * One way to fix this is to add a memory management function that, given
347 * a pointer to a palloc'd chunk, returns the actual space consumed by the
348 * chunk. This would be very easy in the current aset.c module, but I'm
349 * hesitant to do it because it might be unpleasant to support in future
350 * implementations of memory management. (For example, a direct
351 * implementation of palloc as malloc could not support such a function
354 * A cruder answer is just to apply a fudge factor, say by initializing
355 * availMem to only three-quarters of what SortMem indicates. This is
356 * probably the right answer if anyone complains that SortMem is not being
357 * obeyed very faithfully.
359 *--------------------
363 * For sorting single Datums, we build "pseudo tuples" that just carry
364 * the datum's value and null flag. For pass-by-reference data types,
365 * the actual data value appears after the DatumTupleHeader (MAXALIGNed,
366 * of course), and the value field in the header is just a pointer to it.
376 static Tuplesortstate *tuplesort_begin_common(bool randomAccess);
377 static void puttuple_common(Tuplesortstate *state, void *tuple);
378 static void inittapes(Tuplesortstate *state);
379 static void selectnewtape(Tuplesortstate *state);
380 static void mergeruns(Tuplesortstate *state);
381 static void mergeonerun(Tuplesortstate *state);
382 static void beginmerge(Tuplesortstate *state);
383 static void mergepreread(Tuplesortstate *state);
384 static void dumptuples(Tuplesortstate *state, bool alltuples);
385 static void tuplesort_heap_insert(Tuplesortstate *state, void *tuple,
386 int tupleindex, bool checkIndex);
387 static void tuplesort_heap_siftup(Tuplesortstate *state, bool checkIndex);
388 static unsigned int getlen(Tuplesortstate *state, int tapenum, bool eofOK);
389 static void markrunend(Tuplesortstate *state, int tapenum);
390 static int qsort_comparetup(const void *a, const void *b);
391 static int comparetup_heap(Tuplesortstate *state,
392 const void *a, const void *b);
393 static void *copytup_heap(Tuplesortstate *state, void *tup);
394 static void writetup_heap(Tuplesortstate *state, int tapenum, void *tup);
395 static void *readtup_heap(Tuplesortstate *state, int tapenum,
397 static unsigned int tuplesize_heap(Tuplesortstate *state, void *tup);
398 static int comparetup_index(Tuplesortstate *state,
399 const void *a, const void *b);
400 static void *copytup_index(Tuplesortstate *state, void *tup);
401 static void writetup_index(Tuplesortstate *state, int tapenum, void *tup);
402 static void *readtup_index(Tuplesortstate *state, int tapenum,
404 static unsigned int tuplesize_index(Tuplesortstate *state, void *tup);
405 static int comparetup_datum(Tuplesortstate *state,
406 const void *a, const void *b);
407 static void *copytup_datum(Tuplesortstate *state, void *tup);
408 static void writetup_datum(Tuplesortstate *state, int tapenum, void *tup);
409 static void *readtup_datum(Tuplesortstate *state, int tapenum,
411 static unsigned int tuplesize_datum(Tuplesortstate *state, void *tup);
414 * Since qsort(3) will not pass any context info to qsort_comparetup(),
415 * we have to use this ugly static variable. It is set to point to the
416 * active Tuplesortstate object just before calling qsort. It should
417 * not be used directly by anything except qsort_comparetup().
419 static Tuplesortstate *qsort_tuplesortstate;
423 * tuplesort_begin_xxx
425 * Initialize for a tuple sort operation.
427 * After calling tuplesort_begin, the caller should call tuplesort_puttuple
428 * zero or more times, then call tuplesort_performsort when all the tuples
429 * have been supplied. After performsort, retrieve the tuples in sorted
430 * order by calling tuplesort_gettuple until it returns NULL. (If random
431 * access was requested, rescan, markpos, and restorepos can also be called.)
432 * For Datum sorts, putdatum/getdatum are used instead of puttuple/gettuple.
433 * Call tuplesort_end to terminate the operation and release memory/disk space.
436 static Tuplesortstate *
437 tuplesort_begin_common(bool randomAccess)
439 Tuplesortstate *state;
441 state = (Tuplesortstate *) palloc(sizeof(Tuplesortstate));
443 MemSet((char *) state, 0, sizeof(Tuplesortstate));
445 state->status = TSS_INITIAL;
446 state->randomAccess = randomAccess;
447 state->availMem = SortMem * 1024L;
448 state->tapeset = NULL;
450 state->memtupcount = 0;
451 state->memtupsize = 1024; /* initial guess */
452 state->memtuples = (void **) palloc(state->memtupsize * sizeof(void *));
454 state->memtupindex = NULL; /* until and unless needed */
456 state->currentRun = 0;
458 /* Algorithm D variables will be initialized by inittapes, if needed */
460 state->result_tape = -1; /* flag that result tape has not been
467 tuplesort_begin_heap(TupleDesc tupDesc,
469 Oid *sortOperators, AttrNumber *attNums,
472 Tuplesortstate *state = tuplesort_begin_common(randomAccess);
475 AssertArg(nkeys > 0);
477 state->comparetup = comparetup_heap;
478 state->copytup = copytup_heap;
479 state->writetup = writetup_heap;
480 state->readtup = readtup_heap;
481 state->tuplesize = tuplesize_heap;
483 state->tupDesc = tupDesc;
484 state->nKeys = nkeys;
485 state->scanKeys = (ScanKey) palloc(nkeys * sizeof(ScanKeyData));
486 MemSet(state->scanKeys, 0, nkeys * sizeof(ScanKeyData));
487 state->sortFnKinds = (SortFunctionKind *)
488 palloc(nkeys * sizeof(SortFunctionKind));
489 MemSet(state->sortFnKinds, 0, nkeys * sizeof(SortFunctionKind));
491 for (i = 0; i < nkeys; i++)
493 RegProcedure sortFunction;
495 AssertArg(sortOperators[i] != 0);
496 AssertArg(attNums[i] != 0);
498 /* select a function that implements the sort operator */
499 SelectSortFunction(sortOperators[i], &sortFunction,
500 &state->sortFnKinds[i]);
502 ScanKeyEntryInitialize(&state->scanKeys[i],
513 tuplesort_begin_index(Relation indexRel,
517 Tuplesortstate *state = tuplesort_begin_common(randomAccess);
519 state->comparetup = comparetup_index;
520 state->copytup = copytup_index;
521 state->writetup = writetup_index;
522 state->readtup = readtup_index;
523 state->tuplesize = tuplesize_index;
525 state->indexRel = indexRel;
526 /* see comments below about btree dependence of this code... */
527 state->indexScanKey = _bt_mkscankey_nodata(indexRel);
528 state->enforceUnique = enforceUnique;
534 tuplesort_begin_datum(Oid datumType,
538 Tuplesortstate *state = tuplesort_begin_common(randomAccess);
539 RegProcedure sortFunction;
543 state->comparetup = comparetup_datum;
544 state->copytup = copytup_datum;
545 state->writetup = writetup_datum;
546 state->readtup = readtup_datum;
547 state->tuplesize = tuplesize_datum;
549 state->datumType = datumType;
550 state->sortOperator = sortOperator;
552 /* select a function that implements the sort operator */
553 SelectSortFunction(sortOperator, &sortFunction, &state->sortFnKind);
554 /* and look up the function */
555 fmgr_info(sortFunction, &state->sortOpFn);
557 /* lookup necessary attributes of the datum type */
558 get_typlenbyval(datumType, &typlen, &typbyval);
559 state->datumTypeLen = typlen;
560 state->datumTypeByVal = typbyval;
568 * Release resources and clean up.
571 tuplesort_end(Tuplesortstate *state)
576 LogicalTapeSetClose(state->tapeset);
577 if (state->memtuples)
579 for (i = 0; i < state->memtupcount; i++)
580 pfree(state->memtuples[i]);
581 pfree(state->memtuples);
583 if (state->memtupindex)
584 pfree(state->memtupindex);
587 * this stuff might better belong in a variant-specific shutdown
591 pfree(state->scanKeys);
592 if (state->sortFnKinds)
593 pfree(state->sortFnKinds);
599 * Accept one tuple while collecting input data for sort.
601 * Note that the input tuple is always copied; the caller need not save it.
604 tuplesort_puttuple(Tuplesortstate *state, void *tuple)
607 * Copy the given tuple into memory we control, and decrease availMem.
608 * Then call the code shared with the Datum case.
610 tuple = COPYTUP(state, tuple);
612 puttuple_common(state, tuple);
616 * Accept one Datum while collecting input data for sort.
618 * If the Datum is pass-by-ref type, the value will be copied.
621 tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
626 * Build pseudo-tuple carrying the datum, and decrease availMem.
628 if (isNull || state->datumTypeByVal)
630 USEMEM(state, sizeof(DatumTuple));
631 tuple = (DatumTuple *) palloc(sizeof(DatumTuple));
633 tuple->isNull = isNull;
637 int datalen = state->datumTypeLen;
641 if (datalen == -1) /* variable length type? */
642 datalen = VARSIZE((struct varlena *) DatumGetPointer(val));
643 tuplelen = datalen + MAXALIGN(sizeof(DatumTuple));
644 USEMEM(state, tuplelen);
645 newVal = (char *) palloc(tuplelen);
646 tuple = (DatumTuple *) newVal;
647 newVal += MAXALIGN(sizeof(DatumTuple));
648 memcpy(newVal, DatumGetPointer(val), datalen);
649 tuple->val = PointerGetDatum(newVal);
650 tuple->isNull = false;
653 puttuple_common(state, (void *) tuple);
657 * Shared code for tuple and datum cases.
660 puttuple_common(Tuplesortstate *state, void *tuple)
662 switch (state->status)
667 * Save the copied tuple into the unsorted array.
669 if (state->memtupcount >= state->memtupsize)
671 /* Grow the unsorted array as needed. */
672 state->memtupsize *= 2;
673 state->memtuples = (void **)
674 repalloc(state->memtuples,
675 state->memtupsize * sizeof(void *));
677 state->memtuples[state->memtupcount++] = tuple;
680 * Done if we still fit in available memory.
686 * Nope; time to switch to tape-based operation.
691 * Dump tuples until we are back under the limit.
693 dumptuples(state, false);
698 * Insert the copied tuple into the heap, with run number
699 * currentRun if it can go into the current run, else run
700 * number currentRun+1. The tuple can go into the current run
701 * if it is >= the first not-yet-output tuple. (Actually, it
702 * could go into the current run if it is >= the most recently
703 * output tuple ... but that would require keeping around the
704 * tuple we last output, and it's simplest to let writetup
705 * free each tuple as soon as it's written.)
707 * Note there will always be at least one tuple in the heap at
708 * this point; see dumptuples.
710 Assert(state->memtupcount > 0);
711 if (COMPARETUP(state, tuple, state->memtuples[0]) >= 0)
712 tuplesort_heap_insert(state, tuple, state->currentRun, true);
714 tuplesort_heap_insert(state, tuple, state->currentRun + 1, true);
717 * If we are over the memory limit, dump tuples till we're
720 dumptuples(state, false);
723 elog(ERROR, "tuplesort_puttuple: invalid state");
729 * All tuples have been provided; finish the sort.
732 tuplesort_performsort(Tuplesortstate *state)
734 switch (state->status)
739 * We were able to accumulate all the tuples within the
740 * allowed amount of memory. Just qsort 'em and we're done.
742 if (state->memtupcount > 1)
744 qsort_tuplesortstate = state;
745 qsort((void *) state->memtuples, state->memtupcount,
746 sizeof(void *), qsort_comparetup);
749 state->eof_reached = false;
750 state->markpos_offset = 0;
751 state->markpos_eof = false;
752 state->status = TSS_SORTEDINMEM;
757 * Finish tape-based sort. First, flush all tuples remaining
758 * in memory out to tape; then merge until we have a single
759 * remaining run (or, if !randomAccess, one run per tape).
760 * Note that mergeruns sets the correct state->status.
762 dumptuples(state, true);
764 state->eof_reached = false;
765 state->markpos_block = 0L;
766 state->markpos_offset = 0;
767 state->markpos_eof = false;
770 elog(ERROR, "tuplesort_performsort: invalid state");
776 * Fetch the next tuple in either forward or back direction.
777 * Returns NULL if no more tuples. If should_free is set, the
778 * caller must pfree the returned tuple when done with it.
781 tuplesort_gettuple(Tuplesortstate *state, bool forward,
787 switch (state->status)
789 case TSS_SORTEDINMEM:
790 Assert(forward || state->randomAccess);
791 *should_free = false;
794 if (state->current < state->memtupcount)
795 return state->memtuples[state->current++];
796 state->eof_reached = true;
801 if (state->current <= 0)
805 * if all tuples are fetched already then we return last
806 * tuple, else - tuple before last returned.
808 if (state->eof_reached)
809 state->eof_reached = false;
812 state->current--; /* last returned tuple */
813 if (state->current <= 0)
816 return state->memtuples[state->current - 1];
820 case TSS_SORTEDONTAPE:
821 Assert(forward || state->randomAccess);
825 if (state->eof_reached)
827 if ((tuplen = getlen(state, state->result_tape, true)) != 0)
829 tup = READTUP(state, state->result_tape, tuplen);
834 state->eof_reached = true;
842 * if all tuples are fetched already then we return last tuple,
843 * else - tuple before last returned.
845 if (state->eof_reached)
848 * Seek position is pointing just past the zero tuplen at
849 * the end of file; back up to fetch last tuple's ending
850 * length word. If seek fails we must have a completely
853 if (!LogicalTapeBackspace(state->tapeset,
855 2 * sizeof(unsigned int)))
857 state->eof_reached = false;
862 * Back up and fetch previously-returned tuple's ending
863 * length word. If seek fails, assume we are at start of
866 if (!LogicalTapeBackspace(state->tapeset,
868 sizeof(unsigned int)))
870 tuplen = getlen(state, state->result_tape, false);
873 * Back up to get ending length word of tuple before it.
875 if (!LogicalTapeBackspace(state->tapeset,
877 tuplen + 2 * sizeof(unsigned int)))
880 * If that fails, presumably the prev tuple is the
881 * first in the file. Back up so that it becomes next
882 * to read in forward direction (not obviously right,
883 * but that is what in-memory case does).
885 if (!LogicalTapeBackspace(state->tapeset,
887 tuplen + sizeof(unsigned int)))
888 elog(ERROR, "tuplesort_gettuple: bogus tuple len in backward scan");
893 tuplen = getlen(state, state->result_tape, false);
896 * Now we have the length of the prior tuple, back up and read
897 * it. Note: READTUP expects we are positioned after the
898 * initial length word of the tuple, so back up to that point.
900 if (!LogicalTapeBackspace(state->tapeset,
903 elog(ERROR, "tuplesort_gettuple: bogus tuple len in backward scan");
904 tup = READTUP(state, state->result_tape, tuplen);
912 * This code should match the inner loop of mergeonerun().
914 if (state->memtupcount > 0)
916 int srcTape = state->memtupindex[0];
921 tup = state->memtuples[0];
922 /* returned tuple is no longer counted in our memory space */
923 tuplen = TUPLESIZE(state, tup);
924 state->availMem += tuplen;
925 state->mergeavailmem[srcTape] += tuplen;
926 tuplesort_heap_siftup(state, false);
927 if ((tupIndex = state->mergenext[srcTape]) == 0)
930 * out of preloaded data on this tape, try to read
936 * if still no data, we've reached end of run on this
939 if ((tupIndex = state->mergenext[srcTape]) == 0)
942 /* pull next preread tuple from list, insert in heap */
943 newtup = state->memtuples[tupIndex];
944 state->mergenext[srcTape] = state->memtupindex[tupIndex];
945 if (state->mergenext[srcTape] == 0)
946 state->mergelast[srcTape] = 0;
947 state->memtupindex[tupIndex] = state->mergefreelist;
948 state->mergefreelist = tupIndex;
949 tuplesort_heap_insert(state, newtup, srcTape, false);
955 elog(ERROR, "tuplesort_gettuple: invalid state");
956 return NULL; /* keep compiler quiet */
961 * Fetch the next Datum in either forward or back direction.
962 * Returns FALSE if no more datums.
964 * If the Datum is pass-by-ref type, the returned value is freshly palloc'd
965 * and is now owned by the caller.
968 tuplesort_getdatum(Tuplesortstate *state, bool forward,
969 Datum *val, bool *isNull)
974 tuple = (DatumTuple *) tuplesort_gettuple(state, forward, &should_free);
979 if (tuple->isNull || state->datumTypeByVal)
982 *isNull = tuple->isNull;
986 int datalen = state->datumTypeLen;
989 if (datalen == -1) /* variable length type? */
990 datalen = VARSIZE((struct varlena *) DatumGetPointer(tuple->val));
991 newVal = (char *) palloc(datalen);
992 memcpy(newVal, DatumGetPointer(tuple->val), datalen);
993 *val = PointerGetDatum(newVal);
1005 * inittapes - initialize for tape sorting.
1007 * This is called only if we have found we don't have room to sort in memory.
1010 inittapes(Tuplesortstate *state)
1015 state->tapeset = LogicalTapeSetCreate(MAXTAPES);
1018 * Allocate the memtupindex array, same size as memtuples.
1020 state->memtupindex = (int *) palloc(state->memtupsize * sizeof(int));
1023 * Convert the unsorted contents of memtuples[] into a heap. Each
1024 * tuple is marked as belonging to run number zero.
1026 * NOTE: we pass false for checkIndex since there's no point in comparing
1027 * indexes in this step, even though we do intend the indexes to be
1028 * part of the sort key...
1030 ntuples = state->memtupcount;
1031 state->memtupcount = 0; /* make the heap empty */
1032 for (j = 0; j < ntuples; j++)
1033 tuplesort_heap_insert(state, state->memtuples[j], 0, false);
1034 Assert(state->memtupcount == ntuples);
1036 state->currentRun = 0;
1039 * Initialize variables of Algorithm D (step D1).
1041 for (j = 0; j < MAXTAPES; j++)
1043 state->tp_fib[j] = 1;
1044 state->tp_runs[j] = 0;
1045 state->tp_dummy[j] = 1;
1046 state->tp_tapenum[j] = j;
1048 state->tp_fib[TAPERANGE] = 0;
1049 state->tp_dummy[TAPERANGE] = 0;
1052 state->destTape = 0;
1054 state->status = TSS_BUILDRUNS;
1058 * selectnewtape -- select new tape for new initial run.
1060 * This is called after finishing a run when we know another run
1061 * must be started. This implements steps D3, D4 of Algorithm D.
1064 selectnewtape(Tuplesortstate *state)
1069 /* Step D3: advance j (destTape) */
1070 if (state->tp_dummy[state->destTape] < state->tp_dummy[state->destTape + 1])
1075 if (state->tp_dummy[state->destTape] != 0)
1077 state->destTape = 0;
1081 /* Step D4: increase level */
1083 a = state->tp_fib[0];
1084 for (j = 0; j < TAPERANGE; j++)
1086 state->tp_dummy[j] = a + state->tp_fib[j + 1] - state->tp_fib[j];
1087 state->tp_fib[j] = a + state->tp_fib[j + 1];
1089 state->destTape = 0;
1093 * mergeruns -- merge all the completed initial runs.
1095 * This implements steps D5, D6 of Algorithm D. All input data has
1096 * already been written to initial runs on tape (see dumptuples).
1099 mergeruns(Tuplesortstate *state)
1106 Assert(state->status == TSS_BUILDRUNS);
1107 Assert(state->memtupcount == 0);
1110 * If we produced only one initial run (quite likely if the total data
1111 * volume is between 1X and 2X SortMem), we can just use that tape as
1112 * the finished output, rather than doing a useless merge.
1114 if (state->currentRun == 1)
1116 state->result_tape = state->tp_tapenum[state->destTape];
1117 /* must freeze and rewind the finished output tape */
1118 LogicalTapeFreeze(state->tapeset, state->result_tape);
1119 state->status = TSS_SORTEDONTAPE;
1123 /* End of step D2: rewind all output tapes to prepare for merging */
1124 for (tapenum = 0; tapenum < TAPERANGE; tapenum++)
1125 LogicalTapeRewind(state->tapeset, tapenum, false);
1129 /* Step D5: merge runs onto tape[T] until tape[P] is empty */
1130 while (state->tp_runs[TAPERANGE - 1] || state->tp_dummy[TAPERANGE - 1])
1132 bool allDummy = true;
1133 bool allOneRun = true;
1135 for (tapenum = 0; tapenum < TAPERANGE; tapenum++)
1137 if (state->tp_dummy[tapenum] == 0)
1139 if (state->tp_runs[tapenum] + state->tp_dummy[tapenum] != 1)
1144 * If we don't have to produce a materialized sorted tape,
1145 * quit as soon as we're down to one real/dummy run per tape.
1147 if (!state->randomAccess && allOneRun)
1150 /* Initialize for the final merge pass */
1152 state->status = TSS_FINALMERGE;
1157 state->tp_dummy[TAPERANGE]++;
1158 for (tapenum = 0; tapenum < TAPERANGE; tapenum++)
1159 state->tp_dummy[tapenum]--;
1164 /* Step D6: decrease level */
1165 if (--state->Level == 0)
1167 /* rewind output tape T to use as new input */
1168 LogicalTapeRewind(state->tapeset, state->tp_tapenum[TAPERANGE],
1170 /* rewind used-up input tape P, and prepare it for write pass */
1171 LogicalTapeRewind(state->tapeset, state->tp_tapenum[TAPERANGE - 1],
1173 state->tp_runs[TAPERANGE - 1] = 0;
1176 * reassign tape units per step D6; note we no longer care about
1179 svTape = state->tp_tapenum[TAPERANGE];
1180 svDummy = state->tp_dummy[TAPERANGE];
1181 svRuns = state->tp_runs[TAPERANGE];
1182 for (tapenum = TAPERANGE; tapenum > 0; tapenum--)
1184 state->tp_tapenum[tapenum] = state->tp_tapenum[tapenum - 1];
1185 state->tp_dummy[tapenum] = state->tp_dummy[tapenum - 1];
1186 state->tp_runs[tapenum] = state->tp_runs[tapenum - 1];
1188 state->tp_tapenum[0] = svTape;
1189 state->tp_dummy[0] = svDummy;
1190 state->tp_runs[0] = svRuns;
1194 * Done. Knuth says that the result is on TAPE[1], but since we
1195 * exited the loop without performing the last iteration of step D6,
1196 * we have not rearranged the tape unit assignment, and therefore the
1197 * result is on TAPE[T]. We need to do it this way so that we can
1198 * freeze the final output tape while rewinding it. The last
1199 * iteration of step D6 would be a waste of cycles anyway...
1201 state->result_tape = state->tp_tapenum[TAPERANGE];
1202 LogicalTapeFreeze(state->tapeset, state->result_tape);
1203 state->status = TSS_SORTEDONTAPE;
1207 * Merge one run from each input tape, except ones with dummy runs.
1209 * This is the inner loop of Algorithm D step D5. We know that the
1210 * output tape is TAPE[T].
1213 mergeonerun(Tuplesortstate *state)
1215 int destTape = state->tp_tapenum[TAPERANGE];
1223 * Start the merge by loading one tuple from each active source tape
1224 * into the heap. We can also decrease the input run/dummy run
1230 * Execute merge by repeatedly extracting lowest tuple in heap,
1231 * writing it out, and replacing it with next tuple from same tape (if
1232 * there is another one).
1234 while (state->memtupcount > 0)
1236 /* write the tuple to destTape */
1237 priorAvail = state->availMem;
1238 srcTape = state->memtupindex[0];
1239 WRITETUP(state, destTape, state->memtuples[0]);
1240 /* writetup adjusted total free space, now fix per-tape space */
1241 spaceFreed = state->availMem - priorAvail;
1242 state->mergeavailmem[srcTape] += spaceFreed;
1243 /* compact the heap */
1244 tuplesort_heap_siftup(state, false);
1245 if ((tupIndex = state->mergenext[srcTape]) == 0)
1247 /* out of preloaded data on this tape, try to read more */
1248 mergepreread(state);
1249 /* if still no data, we've reached end of run on this tape */
1250 if ((tupIndex = state->mergenext[srcTape]) == 0)
1253 /* pull next preread tuple from list, insert in heap */
1254 tup = state->memtuples[tupIndex];
1255 state->mergenext[srcTape] = state->memtupindex[tupIndex];
1256 if (state->mergenext[srcTape] == 0)
1257 state->mergelast[srcTape] = 0;
1258 state->memtupindex[tupIndex] = state->mergefreelist;
1259 state->mergefreelist = tupIndex;
1260 tuplesort_heap_insert(state, tup, srcTape, false);
1264 * When the heap empties, we're done. Write an end-of-run marker on
1265 * the output tape, and increment its count of real runs.
1267 markrunend(state, destTape);
1268 state->tp_runs[TAPERANGE]++;
1272 * beginmerge - initialize for a merge pass
1274 * We decrease the counts of real and dummy runs for each tape, and mark
1275 * which tapes contain active input runs in mergeactive[]. Then, load
1276 * as many tuples as we can from each active input tape, and finally
1277 * fill the merge heap with the first tuple from each active tape.
1280 beginmerge(Tuplesortstate *state)
1286 /* Heap should be empty here */
1287 Assert(state->memtupcount == 0);
1289 /* Clear merge-pass state variables */
1290 memset(state->mergeactive, 0, sizeof(state->mergeactive));
1291 memset(state->mergenext, 0, sizeof(state->mergenext));
1292 memset(state->mergelast, 0, sizeof(state->mergelast));
1293 memset(state->mergeavailmem, 0, sizeof(state->mergeavailmem));
1294 state->mergefreelist = 0; /* nothing in the freelist */
1295 state->mergefirstfree = MAXTAPES; /* first slot available for
1298 /* Adjust run counts and mark the active tapes */
1300 for (tapenum = 0; tapenum < TAPERANGE; tapenum++)
1302 if (state->tp_dummy[tapenum] > 0)
1303 state->tp_dummy[tapenum]--;
1306 Assert(state->tp_runs[tapenum] > 0);
1307 state->tp_runs[tapenum]--;
1308 srcTape = state->tp_tapenum[tapenum];
1309 state->mergeactive[srcTape] = true;
1315 * Initialize space allocation to let each active input tape have an
1316 * equal share of preread space.
1318 Assert(activeTapes > 0);
1319 state->spacePerTape = state->availMem / activeTapes;
1320 for (srcTape = 0; srcTape < MAXTAPES; srcTape++)
1322 if (state->mergeactive[srcTape])
1323 state->mergeavailmem[srcTape] = state->spacePerTape;
1327 * Preread as many tuples as possible (and at least one) from each
1330 mergepreread(state);
1332 /* Load the merge heap with the first tuple from each input tape */
1333 for (srcTape = 0; srcTape < MAXTAPES; srcTape++)
1335 int tupIndex = state->mergenext[srcTape];
1340 tup = state->memtuples[tupIndex];
1341 state->mergenext[srcTape] = state->memtupindex[tupIndex];
1342 if (state->mergenext[srcTape] == 0)
1343 state->mergelast[srcTape] = 0;
1344 state->memtupindex[tupIndex] = state->mergefreelist;
1345 state->mergefreelist = tupIndex;
1346 tuplesort_heap_insert(state, tup, srcTape, false);
1352 * mergepreread - load tuples from merge input tapes
1354 * This routine exists to improve sequentiality of reads during a merge pass,
1355 * as explained in the header comments of this file. Load tuples from each
1356 * active source tape until the tape's run is exhausted or it has used up
1357 * its fair share of available memory. In any case, we guarantee that there
1358 * is at one preread tuple available from each unexhausted input tape.
1361 mergepreread(Tuplesortstate *state)
1364 unsigned int tuplen;
1370 for (srcTape = 0; srcTape < MAXTAPES; srcTape++)
1372 if (!state->mergeactive[srcTape])
1376 * Skip reading from any tape that still has at least half of its
1377 * target memory filled with tuples (threshold fraction may need
1378 * adjustment?). This avoids reading just a few tuples when the
1379 * incoming runs are not being consumed evenly.
1381 if (state->mergenext[srcTape] != 0 &&
1382 state->mergeavailmem[srcTape] <= state->spacePerTape / 2)
1386 * Read tuples from this tape until it has used up its free
1387 * memory, but ensure that we have at least one.
1389 priorAvail = state->availMem;
1390 state->availMem = state->mergeavailmem[srcTape];
1391 while (!LACKMEM(state) || state->mergenext[srcTape] == 0)
1393 /* read next tuple, if any */
1394 if ((tuplen = getlen(state, srcTape, true)) == 0)
1396 state->mergeactive[srcTape] = false;
1399 tup = READTUP(state, srcTape, tuplen);
1400 /* find or make a free slot in memtuples[] for it */
1401 tupIndex = state->mergefreelist;
1403 state->mergefreelist = state->memtupindex[tupIndex];
1406 tupIndex = state->mergefirstfree++;
1407 /* Might need to enlarge arrays! */
1408 if (tupIndex >= state->memtupsize)
1410 state->memtupsize *= 2;
1411 state->memtuples = (void **)
1412 repalloc(state->memtuples,
1413 state->memtupsize * sizeof(void *));
1414 state->memtupindex = (int *)
1415 repalloc(state->memtupindex,
1416 state->memtupsize * sizeof(int));
1419 /* store tuple, append to list for its tape */
1420 state->memtuples[tupIndex] = tup;
1421 state->memtupindex[tupIndex] = 0;
1422 if (state->mergelast[srcTape])
1423 state->memtupindex[state->mergelast[srcTape]] = tupIndex;
1425 state->mergenext[srcTape] = tupIndex;
1426 state->mergelast[srcTape] = tupIndex;
1428 /* update per-tape and global availmem counts */
1429 spaceUsed = state->mergeavailmem[srcTape] - state->availMem;
1430 state->mergeavailmem[srcTape] = state->availMem;
1431 state->availMem = priorAvail - spaceUsed;
1436 * dumptuples - remove tuples from heap and write to tape
1438 * This is used during initial-run building, but not during merging.
1440 * When alltuples = false, dump only enough tuples to get under the
1441 * availMem limit (and leave at least one tuple in the heap in any case,
1442 * since puttuple assumes it always has a tuple to compare to).
1444 * When alltuples = true, dump everything currently in memory.
1445 * (This case is only used at end of input data.)
1447 * If we empty the heap, close out the current run and return (this should
1448 * only happen at end of input data). If we see that the tuple run number
1449 * at the top of the heap has changed, start a new run.
1452 dumptuples(Tuplesortstate *state, bool alltuples)
1455 (LACKMEM(state) && state->memtupcount > 1))
1458 * Dump the heap's frontmost entry, and sift up to remove it from
1461 Assert(state->memtupcount > 0);
1462 WRITETUP(state, state->tp_tapenum[state->destTape],
1463 state->memtuples[0]);
1464 tuplesort_heap_siftup(state, true);
1467 * If the heap is empty *or* top run number has changed, we've
1468 * finished the current run.
1470 if (state->memtupcount == 0 ||
1471 state->currentRun != state->memtupindex[0])
1473 markrunend(state, state->tp_tapenum[state->destTape]);
1474 state->currentRun++;
1475 state->tp_runs[state->destTape]++;
1476 state->tp_dummy[state->destTape]--; /* per Alg D step D2 */
1479 * Done if heap is empty, else prepare for new run.
1481 if (state->memtupcount == 0)
1483 Assert(state->currentRun == state->memtupindex[0]);
1484 selectnewtape(state);
1490 * tuplesort_rescan - rewind and replay the scan
1493 tuplesort_rescan(Tuplesortstate *state)
1495 Assert(state->randomAccess);
1497 switch (state->status)
1499 case TSS_SORTEDINMEM:
1501 state->eof_reached = false;
1502 state->markpos_offset = 0;
1503 state->markpos_eof = false;
1505 case TSS_SORTEDONTAPE:
1506 LogicalTapeRewind(state->tapeset,
1509 state->eof_reached = false;
1510 state->markpos_block = 0L;
1511 state->markpos_offset = 0;
1512 state->markpos_eof = false;
1515 elog(ERROR, "tuplesort_rescan: invalid state");
1521 * tuplesort_markpos - saves current position in the merged sort file
1524 tuplesort_markpos(Tuplesortstate *state)
1526 Assert(state->randomAccess);
1528 switch (state->status)
1530 case TSS_SORTEDINMEM:
1531 state->markpos_offset = state->current;
1532 state->markpos_eof = state->eof_reached;
1534 case TSS_SORTEDONTAPE:
1535 LogicalTapeTell(state->tapeset,
1537 &state->markpos_block,
1538 &state->markpos_offset);
1539 state->markpos_eof = state->eof_reached;
1542 elog(ERROR, "tuplesort_markpos: invalid state");
1548 * tuplesort_restorepos - restores current position in merged sort file to
1549 * last saved position
1552 tuplesort_restorepos(Tuplesortstate *state)
1554 Assert(state->randomAccess);
1556 switch (state->status)
1558 case TSS_SORTEDINMEM:
1559 state->current = state->markpos_offset;
1560 state->eof_reached = state->markpos_eof;
1562 case TSS_SORTEDONTAPE:
1563 if (!LogicalTapeSeek(state->tapeset,
1565 state->markpos_block,
1566 state->markpos_offset))
1567 elog(ERROR, "tuplesort_restorepos failed");
1568 state->eof_reached = state->markpos_eof;
1571 elog(ERROR, "tuplesort_restorepos: invalid state");
1578 * Heap manipulation routines, per Knuth's Algorithm 5.2.3H.
1580 * The heap lives in state->memtuples[], with parallel data storage
1581 * for indexes in state->memtupindex[]. If checkIndex is true, use
1582 * the tuple index as the front of the sort key; otherwise, no.
1585 #define HEAPCOMPARE(tup1,index1,tup2,index2) \
1586 (checkIndex && (index1 != index2) ? index1 - index2 : \
1587 COMPARETUP(state, tup1, tup2))
1590 * Insert a new tuple into an empty or existing heap, maintaining the
1594 tuplesort_heap_insert(Tuplesortstate *state, void *tuple,
1595 int tupleindex, bool checkIndex)
1602 * Make sure memtuples[] can handle another entry.
1604 if (state->memtupcount >= state->memtupsize)
1606 state->memtupsize *= 2;
1607 state->memtuples = (void **)
1608 repalloc(state->memtuples,
1609 state->memtupsize * sizeof(void *));
1610 state->memtupindex = (int *)
1611 repalloc(state->memtupindex,
1612 state->memtupsize * sizeof(int));
1614 memtuples = state->memtuples;
1615 memtupindex = state->memtupindex;
1618 * Sift-up the new entry, per Knuth 5.2.3 exercise 16. Note that Knuth
1619 * is using 1-based array indexes, not 0-based.
1621 j = state->memtupcount++;
1624 int i = (j - 1) >> 1;
1626 if (HEAPCOMPARE(tuple, tupleindex,
1627 memtuples[i], memtupindex[i]) >= 0)
1629 memtuples[j] = memtuples[i];
1630 memtupindex[j] = memtupindex[i];
1633 memtuples[j] = tuple;
1634 memtupindex[j] = tupleindex;
1638 * The tuple at state->memtuples[0] has been removed from the heap.
1639 * Decrement memtupcount, and sift up to maintain the heap invariant.
1642 tuplesort_heap_siftup(Tuplesortstate *state, bool checkIndex)
1644 void **memtuples = state->memtuples;
1645 int *memtupindex = state->memtupindex;
1651 if (--state->memtupcount <= 0)
1653 n = state->memtupcount;
1654 tuple = memtuples[n]; /* tuple that must be reinserted */
1655 tupindex = memtupindex[n];
1656 i = 0; /* i is where the "hole" is */
1664 HEAPCOMPARE(memtuples[j], memtupindex[j],
1665 memtuples[j + 1], memtupindex[j + 1]) > 0)
1667 if (HEAPCOMPARE(tuple, tupindex,
1668 memtuples[j], memtupindex[j]) <= 0)
1670 memtuples[i] = memtuples[j];
1671 memtupindex[i] = memtupindex[j];
1674 memtuples[i] = tuple;
1675 memtupindex[i] = tupindex;
1680 * Tape interface routines
1684 getlen(Tuplesortstate *state, int tapenum, bool eofOK)
1688 if (LogicalTapeRead(state->tapeset, tapenum, (void *) &len,
1689 sizeof(len)) != sizeof(len))
1690 elog(ERROR, "tuplesort: unexpected end of tape");
1691 if (len == 0 && !eofOK)
1692 elog(ERROR, "tuplesort: unexpected end of data");
1697 markrunend(Tuplesortstate *state, int tapenum)
1699 unsigned int len = 0;
1701 LogicalTapeWrite(state->tapeset, tapenum, (void *) &len, sizeof(len));
1710 qsort_comparetup(const void *a, const void *b)
1712 /* The passed pointers are pointers to void * ... */
1714 return COMPARETUP(qsort_tuplesortstate, *(void **) a, *(void **) b);
1719 * Routines specialized for HeapTuple case
1723 comparetup_heap(Tuplesortstate *state, const void *a, const void *b)
1725 HeapTuple ltup = (HeapTuple) a;
1726 HeapTuple rtup = (HeapTuple) b;
1727 TupleDesc tupDesc = state->tupDesc;
1730 for (nkey = 0; nkey < state->nKeys; nkey++)
1732 ScanKey scanKey = state->scanKeys + nkey;
1733 AttrNumber attno = scanKey->sk_attno;
1740 datum1 = heap_getattr(ltup, attno, tupDesc, &isnull1);
1741 datum2 = heap_getattr(rtup, attno, tupDesc, &isnull2);
1743 compare = ApplySortFunction(&scanKey->sk_func,
1744 state->sortFnKinds[nkey],
1745 datum1, isnull1, datum2, isnull2);
1748 /* dead code? SK_COMMUTE can't actually be set here, can it? */
1749 if (scanKey->sk_flags & SK_COMMUTE)
1759 copytup_heap(Tuplesortstate *state, void *tup)
1761 HeapTuple tuple = (HeapTuple) tup;
1763 USEMEM(state, HEAPTUPLESIZE + tuple->t_len);
1764 return (void *) heap_copytuple(tuple);
1768 * We don't bother to write the HeapTupleData part of the tuple.
1772 writetup_heap(Tuplesortstate *state, int tapenum, void *tup)
1774 HeapTuple tuple = (HeapTuple) tup;
1775 unsigned int tuplen;
1777 tuplen = tuple->t_len + sizeof(tuplen);
1778 LogicalTapeWrite(state->tapeset, tapenum,
1779 (void *) &tuplen, sizeof(tuplen));
1780 LogicalTapeWrite(state->tapeset, tapenum,
1781 (void *) tuple->t_data, tuple->t_len);
1782 if (state->randomAccess) /* need trailing length word? */
1783 LogicalTapeWrite(state->tapeset, tapenum,
1784 (void *) &tuplen, sizeof(tuplen));
1786 FREEMEM(state, HEAPTUPLESIZE + tuple->t_len);
1787 heap_freetuple(tuple);
1791 readtup_heap(Tuplesortstate *state, int tapenum, unsigned int len)
1793 unsigned int tuplen = len - sizeof(unsigned int) + HEAPTUPLESIZE;
1794 HeapTuple tuple = (HeapTuple) palloc(tuplen);
1796 USEMEM(state, tuplen);
1797 /* reconstruct the HeapTupleData portion */
1798 tuple->t_len = len - sizeof(unsigned int);
1799 ItemPointerSetInvalid(&(tuple->t_self));
1800 tuple->t_datamcxt = CurrentMemoryContext;
1801 tuple->t_data = (HeapTupleHeader) (((char *) tuple) + HEAPTUPLESIZE);
1802 /* read in the tuple proper */
1803 if (LogicalTapeRead(state->tapeset, tapenum, (void *) tuple->t_data,
1804 tuple->t_len) != tuple->t_len)
1805 elog(ERROR, "tuplesort: unexpected end of data");
1806 if (state->randomAccess) /* need trailing length word? */
1807 if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen,
1808 sizeof(tuplen)) != sizeof(tuplen))
1809 elog(ERROR, "tuplesort: unexpected end of data");
1810 return (void *) tuple;
1814 tuplesize_heap(Tuplesortstate *state, void *tup)
1816 HeapTuple tuple = (HeapTuple) tup;
1818 return HEAPTUPLESIZE + tuple->t_len;
1823 * Routines specialized for IndexTuple case
1825 * NOTE: actually, these are specialized for the btree case; it's not
1826 * clear whether you could use them for a non-btree index. Possibly
1827 * you'd need to make another set of routines if you needed to sort
1828 * according to another kind of index.
1832 comparetup_index(Tuplesortstate *state, const void *a, const void *b)
1835 * This is almost the same as _bt_tuplecompare(), but we need to keep
1836 * track of whether any null fields are present.
1838 IndexTuple tuple1 = (IndexTuple) a;
1839 IndexTuple tuple2 = (IndexTuple) b;
1840 Relation rel = state->indexRel;
1841 int keysz = RelationGetNumberOfAttributes(rel);
1842 ScanKey scankey = state->indexScanKey;
1845 bool equal_hasnull = false;
1847 tupDes = RelationGetDescr(rel);
1849 for (i = 1; i <= keysz; i++)
1851 ScanKey entry = &scankey[i - 1];
1858 datum1 = index_getattr(tuple1, i, tupDes, &isnull1);
1859 datum2 = index_getattr(tuple2, i, tupDes, &isnull2);
1861 /* see comments about NULLs handling in btbuild */
1863 /* the comparison function is always of CMP type */
1864 compare = ApplySortFunction(&entry->sk_func, SORTFUNC_CMP,
1865 datum1, isnull1, datum2, isnull2);
1868 return (int) compare; /* done when we find unequal
1871 /* they are equal, so we only need to examine one null flag */
1873 equal_hasnull = true;
1877 * If btree has asked us to enforce uniqueness, complain if two equal
1878 * tuples are detected (unless there was at least one NULL field).
1880 * It is sufficient to make the test here, because if two tuples are
1881 * equal they *must* get compared at some stage of the sort ---
1882 * otherwise the sort algorithm wouldn't have checked whether one must
1883 * appear before the other.
1885 * Some rather brain-dead implementations of qsort will sometimes
1886 * call the comparison routine to compare a value to itself. (At this
1887 * writing only QNX 4 is known to do such silly things.) Don't raise
1888 * a bogus error in that case.
1890 if (state->enforceUnique && !equal_hasnull && tuple1 != tuple2)
1891 elog(ERROR, "Cannot create unique index. Table contains non-unique values");
1897 copytup_index(Tuplesortstate *state, void *tup)
1899 IndexTuple tuple = (IndexTuple) tup;
1900 unsigned int tuplen = IndexTupleSize(tuple);
1901 IndexTuple newtuple;
1903 USEMEM(state, tuplen);
1904 newtuple = (IndexTuple) palloc(tuplen);
1905 memcpy(newtuple, tuple, tuplen);
1907 return (void *) newtuple;
1911 writetup_index(Tuplesortstate *state, int tapenum, void *tup)
1913 IndexTuple tuple = (IndexTuple) tup;
1914 unsigned int tuplen;
1916 tuplen = IndexTupleSize(tuple) + sizeof(tuplen);
1917 LogicalTapeWrite(state->tapeset, tapenum,
1918 (void *) &tuplen, sizeof(tuplen));
1919 LogicalTapeWrite(state->tapeset, tapenum,
1920 (void *) tuple, IndexTupleSize(tuple));
1921 if (state->randomAccess) /* need trailing length word? */
1922 LogicalTapeWrite(state->tapeset, tapenum,
1923 (void *) &tuplen, sizeof(tuplen));
1925 FREEMEM(state, IndexTupleSize(tuple));
1930 readtup_index(Tuplesortstate *state, int tapenum, unsigned int len)
1932 unsigned int tuplen = len - sizeof(unsigned int);
1933 IndexTuple tuple = (IndexTuple) palloc(tuplen);
1935 USEMEM(state, tuplen);
1936 if (LogicalTapeRead(state->tapeset, tapenum, (void *) tuple,
1938 elog(ERROR, "tuplesort: unexpected end of data");
1939 if (state->randomAccess) /* need trailing length word? */
1940 if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen,
1941 sizeof(tuplen)) != sizeof(tuplen))
1942 elog(ERROR, "tuplesort: unexpected end of data");
1943 return (void *) tuple;
1947 tuplesize_index(Tuplesortstate *state, void *tup)
1949 IndexTuple tuple = (IndexTuple) tup;
1950 unsigned int tuplen = IndexTupleSize(tuple);
1957 * Routines specialized for DatumTuple case
1961 comparetup_datum(Tuplesortstate *state, const void *a, const void *b)
1963 DatumTuple *ltup = (DatumTuple *) a;
1964 DatumTuple *rtup = (DatumTuple *) b;
1966 return ApplySortFunction(&state->sortOpFn, state->sortFnKind,
1967 ltup->val, ltup->isNull,
1968 rtup->val, rtup->isNull);
1972 copytup_datum(Tuplesortstate *state, void *tup)
1974 /* Not currently needed */
1975 elog(ERROR, "copytup_datum() should not be called");
1980 writetup_datum(Tuplesortstate *state, int tapenum, void *tup)
1982 DatumTuple *tuple = (DatumTuple *) tup;
1983 unsigned int tuplen = tuplesize_datum(state, tup);
1984 unsigned int writtenlen = tuplen + sizeof(unsigned int);
1986 LogicalTapeWrite(state->tapeset, tapenum,
1987 (void *) &writtenlen, sizeof(writtenlen));
1988 LogicalTapeWrite(state->tapeset, tapenum,
1989 (void *) tuple, tuplen);
1990 if (state->randomAccess) /* need trailing length word? */
1991 LogicalTapeWrite(state->tapeset, tapenum,
1992 (void *) &writtenlen, sizeof(writtenlen));
1994 FREEMEM(state, tuplen);
1999 readtup_datum(Tuplesortstate *state, int tapenum, unsigned int len)
2001 unsigned int tuplen = len - sizeof(unsigned int);
2002 DatumTuple *tuple = (DatumTuple *) palloc(tuplen);
2004 USEMEM(state, tuplen);
2005 if (LogicalTapeRead(state->tapeset, tapenum, (void *) tuple,
2007 elog(ERROR, "tuplesort: unexpected end of data");
2008 if (state->randomAccess) /* need trailing length word? */
2009 if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen,
2010 sizeof(tuplen)) != sizeof(tuplen))
2011 elog(ERROR, "tuplesort: unexpected end of data");
2013 if (!tuple->isNull && !state->datumTypeByVal)
2014 tuple->val = PointerGetDatum(((char *) tuple) +
2015 MAXALIGN(sizeof(DatumTuple)));
2016 return (void *) tuple;
2020 tuplesize_datum(Tuplesortstate *state, void *tup)
2022 DatumTuple *tuple = (DatumTuple *) tup;
2024 if (tuple->isNull || state->datumTypeByVal)
2025 return (unsigned int) sizeof(DatumTuple);
2028 int datalen = state->datumTypeLen;
2031 if (datalen == -1) /* variable length type? */
2032 datalen = VARSIZE((struct varlena *) DatumGetPointer(tuple->val));
2033 tuplelen = datalen + MAXALIGN(sizeof(DatumTuple));
2034 return (unsigned int) tuplelen;
2040 * This routine selects an appropriate sorting function to implement
2041 * a sort operator as efficiently as possible. The straightforward
2042 * method is to use the operator's implementation proc --- ie, "<"
2043 * comparison. However, that way often requires two calls of the function
2044 * per comparison. If we can find a btree three-way comparator function
2045 * associated with the operator, we can use it to do the comparisons
2046 * more efficiently. We also support the possibility that the operator
2047 * is ">" (descending sort), in which case we have to reverse the output
2048 * of the btree comparator.
2050 * Possibly this should live somewhere else (backend/catalog/, maybe?).
2053 SelectSortFunction(Oid sortOperator,
2054 RegProcedure *sortFunction,
2055 SortFunctionKind *kind)
2059 ScanKeyData skey[1];
2061 Form_pg_operator optup;
2062 Oid opclass = InvalidOid;
2065 * Scan pg_amop to see if the target operator is registered as the "<"
2066 * or ">" operator of any btree opclass. It's possible that it might
2067 * be registered both ways (eg, if someone were to build a "reverse
2068 * sort" opclass for some reason); prefer the "<" case if so. If the
2069 * operator is registered the same way in multiple opclasses, assume
2070 * we can use the associated comparator function from any one.
2072 ScanKeyEntryInitialize(&skey[0], 0x0,
2073 Anum_pg_amop_amopopr,
2075 ObjectIdGetDatum(sortOperator));
2077 relation = heap_openr(AccessMethodOperatorRelationName, AccessShareLock);
2078 scan = heap_beginscan(relation, false, SnapshotNow, 1, skey);
2080 while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
2082 Form_pg_amop aform = (Form_pg_amop) GETSTRUCT(tuple);
2084 if (!opclass_is_btree(aform->amopclaid))
2086 if (aform->amopstrategy == BTLessStrategyNumber)
2088 opclass = aform->amopclaid;
2089 *kind = SORTFUNC_CMP;
2090 break; /* done looking */
2092 else if (aform->amopstrategy == BTGreaterStrategyNumber)
2094 opclass = aform->amopclaid;
2095 *kind = SORTFUNC_REVCMP;
2096 /* keep scanning in hopes of finding a BTLess entry */
2101 heap_close(relation, AccessShareLock);
2103 if (OidIsValid(opclass))
2105 /* Found a suitable opclass, get its comparator support function */
2106 tuple = SearchSysCache(AMPROCNUM,
2107 ObjectIdGetDatum(opclass),
2108 Int16GetDatum(BTORDER_PROC),
2110 if (HeapTupleIsValid(tuple))
2112 Form_pg_amproc aform = (Form_pg_amproc) GETSTRUCT(tuple);
2114 *sortFunction = aform->amproc;
2115 ReleaseSysCache(tuple);
2116 Assert(RegProcedureIsValid(*sortFunction));
2122 * Can't find a comparator, so use the operator as-is. Decide whether
2123 * it is forward or reverse sort by looking at its name (grotty, but
2124 * this only matters for deciding which end NULLs should get sorted
2127 tuple = SearchSysCache(OPEROID,
2128 ObjectIdGetDatum(sortOperator),
2130 if (!HeapTupleIsValid(tuple))
2131 elog(ERROR, "SelectSortFunction: cache lookup failed for operator %u",
2133 optup = (Form_pg_operator) GETSTRUCT(tuple);
2134 if (strcmp(NameStr(optup->oprname), ">") == 0)
2135 *kind = SORTFUNC_REVLT;
2137 *kind = SORTFUNC_LT;
2138 *sortFunction = optup->oprcode;
2139 ReleaseSysCache(tuple);
2141 Assert(RegProcedureIsValid(*sortFunction));
2145 * Apply a sort function (by now converted to fmgr lookup form)
2146 * and return a 3-way comparison result. This takes care of handling
2147 * NULLs and sort ordering direction properly.
2150 ApplySortFunction(FmgrInfo *sortFunction, SortFunctionKind kind,
2151 Datum datum1, bool isNull1,
2152 Datum datum2, bool isNull2)
2161 return 1; /* NULL sorts after non-NULL */
2165 if (DatumGetBool(FunctionCall2(sortFunction, datum1, datum2)))
2166 return -1; /* a < b */
2167 if (DatumGetBool(FunctionCall2(sortFunction, datum2, datum1)))
2168 return 1; /* a > b */
2171 case SORTFUNC_REVLT:
2172 /* We reverse the ordering of NULLs, but not the operator */
2177 return -1; /* NULL sorts before non-NULL */
2181 if (DatumGetBool(FunctionCall2(sortFunction, datum1, datum2)))
2182 return -1; /* a < b */
2183 if (DatumGetBool(FunctionCall2(sortFunction, datum2, datum1)))
2184 return 1; /* a > b */
2192 return 1; /* NULL sorts after non-NULL */
2196 return DatumGetInt32(FunctionCall2(sortFunction,
2199 case SORTFUNC_REVCMP:
2204 return -1; /* NULL sorts before non-NULL */
2208 return -DatumGetInt32(FunctionCall2(sortFunction,
2212 elog(ERROR, "Invalid SortFunctionKind %d", (int) kind);
2213 return 0; /* can't get here, but keep compiler quiet */