Header And Logo

PostgreSQL
| The world's most advanced open source database.

tuplesort.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * tuplesort.h
00004  *    Generalized tuple sorting routines.
00005  *
00006  * This module handles sorting of heap tuples, index tuples, or single
00007  * Datums (and could easily support other kinds of sortable objects,
00008  * if necessary).  It works efficiently for both small and large amounts
00009  * of data.  Small amounts are sorted in-memory using qsort().  Large
00010  * amounts are sorted using temporary files and a standard external sort
00011  * algorithm.
00012  *
00013  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00014  * Portions Copyright (c) 1994, Regents of the University of California
00015  *
00016  * src/include/utils/tuplesort.h
00017  *
00018  *-------------------------------------------------------------------------
00019  */
00020 #ifndef TUPLESORT_H
00021 #define TUPLESORT_H
00022 
00023 #include "access/itup.h"
00024 #include "executor/tuptable.h"
00025 #include "fmgr.h"
00026 #include "utils/relcache.h"
00027 
00028 
00029 /* Tuplesortstate is an opaque type whose details are not known outside
00030  * tuplesort.c.
00031  */
00032 typedef struct Tuplesortstate Tuplesortstate;
00033 
00034 /*
00035  * We provide multiple interfaces to what is essentially the same code,
00036  * since different callers have different data to be sorted and want to
00037  * specify the sort key information differently.  There are two APIs for
00038  * sorting HeapTuples and two more for sorting IndexTuples.  Yet another
00039  * API supports sorting bare Datums.
00040  *
00041  * The "heap" API actually stores/sorts MinimalTuples, which means it doesn't
00042  * preserve the system columns (tuple identity and transaction visibility
00043  * info).  The sort keys are specified by column numbers within the tuples
00044  * and sort operator OIDs.  We save some cycles by passing and returning the
00045  * tuples in TupleTableSlots, rather than forming actual HeapTuples (which'd
00046  * have to be converted to MinimalTuples).  This API works well for sorts
00047  * executed as parts of plan trees.
00048  *
00049  * The "cluster" API stores/sorts full HeapTuples including all visibility
00050  * info. The sort keys are specified by reference to a btree index that is
00051  * defined on the relation to be sorted.  Note that putheaptuple/getheaptuple
00052  * go with this API, not the "begin_heap" one!
00053  *
00054  * The "index_btree" API stores/sorts IndexTuples (preserving all their
00055  * header fields).  The sort keys are specified by a btree index definition.
00056  *
00057  * The "index_hash" API is similar to index_btree, but the tuples are
00058  * actually sorted by their hash codes not the raw data.
00059  */
00060 
00061 extern Tuplesortstate *tuplesort_begin_heap(TupleDesc tupDesc,
00062                      int nkeys, AttrNumber *attNums,
00063                      Oid *sortOperators, Oid *sortCollations,
00064                      bool *nullsFirstFlags,
00065                      int workMem, bool randomAccess);
00066 extern Tuplesortstate *tuplesort_begin_cluster(TupleDesc tupDesc,
00067                         Relation indexRel,
00068                         int workMem, bool randomAccess);
00069 extern Tuplesortstate *tuplesort_begin_index_btree(Relation heapRel,
00070                             Relation indexRel,
00071                             bool enforceUnique,
00072                             int workMem, bool randomAccess);
00073 extern Tuplesortstate *tuplesort_begin_index_hash(Relation heapRel,
00074                            Relation indexRel,
00075                            uint32 hash_mask,
00076                            int workMem, bool randomAccess);
00077 extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
00078                       Oid sortOperator, Oid sortCollation,
00079                       bool nullsFirstFlag,
00080                       int workMem, bool randomAccess);
00081 
00082 extern void tuplesort_set_bound(Tuplesortstate *state, int64 bound);
00083 
00084 extern void tuplesort_puttupleslot(Tuplesortstate *state,
00085                        TupleTableSlot *slot);
00086 extern void tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup);
00087 extern void tuplesort_putindextuple(Tuplesortstate *state, IndexTuple tuple);
00088 extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
00089                    bool isNull);
00090 
00091 extern void tuplesort_performsort(Tuplesortstate *state);
00092 
00093 extern bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
00094                        TupleTableSlot *slot);
00095 extern HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward,
00096                        bool *should_free);
00097 extern IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward,
00098                         bool *should_free);
00099 extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
00100                    Datum *val, bool *isNull);
00101 
00102 extern void tuplesort_end(Tuplesortstate *state);
00103 
00104 extern void tuplesort_get_stats(Tuplesortstate *state,
00105                     const char **sortMethod,
00106                     const char **spaceType,
00107                     long *spaceUsed);
00108 
00109 extern int  tuplesort_merge_order(long allowedMem);
00110 
00111 /*
00112  * These routines may only be called if randomAccess was specified 'true'.
00113  * Likewise, backwards scan in gettuple/getdatum is only allowed if
00114  * randomAccess was specified.
00115  */
00116 
00117 extern void tuplesort_rescan(Tuplesortstate *state);
00118 extern void tuplesort_markpos(Tuplesortstate *state);
00119 extern void tuplesort_restorepos(Tuplesortstate *state);
00120 
00121 #endif   /* TUPLESORT_H */