]> granicus.if.org Git - postgresql/blob - src/include/utils/tuplesort.h
Rewrite of planner statistics-gathering code. ANALYZE is now available as
[postgresql] / src / include / utils / tuplesort.h
1 /*-------------------------------------------------------------------------
2  *
3  * tuplesort.h
4  *        Generalized tuple sorting routines.
5  *
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
11  * algorithm.
12  *
13  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  * $Id: tuplesort.h,v 1.7 2001/05/07 00:43:26 tgl Exp $
17  *
18  *-------------------------------------------------------------------------
19  */
20 #ifndef TUPLESORT_H
21 #define TUPLESORT_H
22
23 #include "access/htup.h"
24 #include "access/itup.h"
25
26 /* Tuplesortstate is an opaque type whose details are not known outside tuplesort.c. */
27
28 typedef struct Tuplesortstate Tuplesortstate;
29
30 /*
31  * We provide two different interfaces to what is essentially the same
32  * code: one for sorting HeapTuples and one for sorting IndexTuples.
33  * They differ primarily in the way that the sort key information is
34  * supplied.
35  * Yet a third slightly different interface supports sorting bare Datums.
36  */
37
38 extern Tuplesortstate *tuplesort_begin_heap(TupleDesc tupDesc,
39                                           int nkeys,
40                                           Oid *sortOperators, AttrNumber *attNums,
41                                           bool randomAccess);
42 extern Tuplesortstate *tuplesort_begin_index(Relation indexRel,
43                                           bool enforceUnique,
44                                           bool randomAccess);
45 extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
46                                           Oid sortOperator,
47                                           bool randomAccess);
48
49 extern void tuplesort_puttuple(Tuplesortstate *state, void *tuple);
50
51 extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
52                                    bool isNull);
53
54 extern void tuplesort_performsort(Tuplesortstate *state);
55
56 extern void *tuplesort_gettuple(Tuplesortstate *state, bool forward,
57                                    bool *should_free);
58
59 #define tuplesort_getheaptuple(state, forward, should_free) \
60         ((HeapTuple) tuplesort_gettuple(state, forward, should_free))
61 #define tuplesort_getindextuple(state, forward, should_free) \
62         ((IndexTuple) tuplesort_gettuple(state, forward, should_free))
63
64 extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
65                                    Datum *val, bool *isNull);
66
67 extern void tuplesort_end(Tuplesortstate *state);
68
69 /*
70  * These routines may only be called if randomAccess was specified 'true'.
71  * Likewise, backwards scan in gettuple/getdatum is only allowed if
72  * randomAccess was specified.
73  */
74
75 extern void tuplesort_rescan(Tuplesortstate *state);
76 extern void tuplesort_markpos(Tuplesortstate *state);
77 extern void tuplesort_restorepos(Tuplesortstate *state);
78
79 /*
80  * This routine selects an appropriate sorting function to implement
81  * a sort operator as efficiently as possible.
82  */
83 typedef enum
84 {
85         SORTFUNC_LT,                            /* raw "<" operator */
86         SORTFUNC_CMP,                           /* -1 / 0 / 1 three-way comparator */
87         SORTFUNC_REVCMP                         /* 1 / 0 / -1 (reversed) 3-way comparator */
88 } SortFunctionKind;
89
90 extern void SelectSortFunction(Oid sortOperator,
91                                                            RegProcedure *sortFunction,
92                                                            SortFunctionKind *kind);
93
94 #endif   /* TUPLESORT_H */