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 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
16 * $PostgreSQL: pgsql/src/include/utils/tuplesort.h,v 1.15 2004/02/03 17:34:04 tgl Exp $
18 *-------------------------------------------------------------------------
23 #include "access/htup.h"
24 #include "access/itup.h"
27 /* Tuplesortstate is an opaque type whose details are not known outside tuplesort.c. */
29 typedef struct Tuplesortstate Tuplesortstate;
32 * We provide two different interfaces to what is essentially the same
33 * code: one for sorting HeapTuples and one for sorting IndexTuples.
34 * They differ primarily in the way that the sort key information is
36 * Yet a third slightly different interface supports sorting bare Datums.
39 extern Tuplesortstate *tuplesort_begin_heap(TupleDesc tupDesc,
41 Oid *sortOperators, AttrNumber *attNums,
42 int workMem, bool randomAccess);
43 extern Tuplesortstate *tuplesort_begin_index(Relation indexRel,
45 int workMem, bool randomAccess);
46 extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
48 int workMem, bool randomAccess);
50 extern void tuplesort_puttuple(Tuplesortstate *state, void *tuple);
52 extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
55 extern void tuplesort_performsort(Tuplesortstate *state);
57 extern void *tuplesort_gettuple(Tuplesortstate *state, bool forward,
60 #define tuplesort_getheaptuple(state, forward, should_free) \
61 ((HeapTuple) tuplesort_gettuple(state, forward, should_free))
62 #define tuplesort_getindextuple(state, forward, should_free) \
63 ((IndexTuple) tuplesort_gettuple(state, forward, should_free))
65 extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
66 Datum *val, bool *isNull);
68 extern void tuplesort_end(Tuplesortstate *state);
71 * These routines may only be called if randomAccess was specified 'true'.
72 * Likewise, backwards scan in gettuple/getdatum is only allowed if
73 * randomAccess was specified.
76 extern void tuplesort_rescan(Tuplesortstate *state);
77 extern void tuplesort_markpos(Tuplesortstate *state);
78 extern void tuplesort_restorepos(Tuplesortstate *state);
81 * This routine selects an appropriate sorting function to implement
82 * a sort operator as efficiently as possible.
86 SORTFUNC_LT, /* raw "<" operator */
87 SORTFUNC_REVLT, /* raw "<" operator, but reverse NULLs */
88 SORTFUNC_CMP, /* -1 / 0 / 1 three-way comparator */
89 SORTFUNC_REVCMP /* 1 / 0 / -1 (reversed) 3-way comparator */
92 extern void SelectSortFunction(Oid sortOperator,
93 RegProcedure *sortFunction,
94 SortFunctionKind *kind);
97 * Apply a sort function (by now converted to fmgr lookup form)
98 * and return a 3-way comparison result. This takes care of handling
99 * NULLs and sort ordering direction properly.
101 extern int32 ApplySortFunction(FmgrInfo *sortFunction, SortFunctionKind kind,
102 Datum datum1, bool isNull1,
103 Datum datum2, bool isNull2);
105 #endif /* TUPLESORT_H */