]> granicus.if.org Git - postgresql/blob - src/include/utils/tuplesort.h
Ye-old pgindent run. Same 4-space tabs.
[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-2000, PostgreSQL, Inc
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  * $Id: tuplesort.h,v 1.4 2000/04/12 17:16:56 momjian Exp $
17  *
18  *-------------------------------------------------------------------------
19  */
20 #ifndef TUPLESORT_H
21 #define TUPLESORT_H
22
23 #include "access/htup.h"
24 #include "access/itup.h"
25 #include "access/skey.h"
26 #include "access/tupdesc.h"
27 #include "utils/rel.h"
28
29 /* Tuplesortstate is an opaque type whose details are not known outside tuplesort.c. */
30
31 typedef struct Tuplesortstate Tuplesortstate;
32
33 /*
34  * We provide two different interfaces to what is essentially the same
35  * code: one for sorting HeapTuples and one for sorting IndexTuples.
36  * They differ primarily in the way that the sort key information is
37  * supplied.
38  * Yet a third slightly different interface supports sorting bare Datums.
39  */
40
41 extern Tuplesortstate *tuplesort_begin_heap(TupleDesc tupDesc,
42                                          int nkeys, ScanKey keys,
43                                          bool randomAccess);
44 extern Tuplesortstate *tuplesort_begin_index(Relation indexRel,
45                                           bool enforceUnique,
46                                           bool randomAccess);
47 extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
48                                           Oid sortOperator,
49                                           bool randomAccess);
50
51 extern void tuplesort_puttuple(Tuplesortstate *state, void *tuple);
52
53 extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
54                                    bool isNull);
55
56 extern void tuplesort_performsort(Tuplesortstate *state);
57
58 extern void *tuplesort_gettuple(Tuplesortstate *state, bool forward,
59                                    bool *should_free);
60
61 #define tuplesort_getheaptuple(state, forward, should_free) \
62         ((HeapTuple) tuplesort_gettuple(state, forward, should_free))
63 #define tuplesort_getindextuple(state, forward, should_free) \
64         ((IndexTuple) tuplesort_gettuple(state, forward, should_free))
65
66 extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
67                                    Datum *val, bool *isNull);
68
69 extern void tuplesort_end(Tuplesortstate *state);
70
71 /*
72  * These routines may only be called if randomAccess was specified 'true'.
73  * Likewise, backwards scan in gettuple/getdatum is only allowed if
74  * randomAccess was specified.
75  */
76
77 extern void tuplesort_rescan(Tuplesortstate *state);
78 extern void tuplesort_markpos(Tuplesortstate *state);
79 extern void tuplesort_restorepos(Tuplesortstate *state);
80
81 #endif   /* TUPLESORT_H */