tuplesort.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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-2016, PostgreSQL Global Development Group
  14. * Portions Copyright (c) 1994, Regents of the University of California
  15. *
  16. * src/include/utils/tuplesort.h
  17. *
  18. *-------------------------------------------------------------------------
  19. */
  20. #ifndef TUPLESORT_H
  21. #define TUPLESORT_H
  22. #include "access/itup.h"
  23. #include "executor/tuptable.h"
  24. #include "fmgr.h"
  25. #include "utils/relcache.h"
  26. /* Tuplesortstate is an opaque type whose details are not known outside
  27. * tuplesort.c.
  28. */
  29. typedef struct Tuplesortstate Tuplesortstate;
  30. /*
  31. * We provide multiple interfaces to what is essentially the same code,
  32. * since different callers have different data to be sorted and want to
  33. * specify the sort key information differently. There are two APIs for
  34. * sorting HeapTuples and two more for sorting IndexTuples. Yet another
  35. * API supports sorting bare Datums.
  36. *
  37. * The "heap" API actually stores/sorts MinimalTuples, which means it doesn't
  38. * preserve the system columns (tuple identity and transaction visibility
  39. * info). The sort keys are specified by column numbers within the tuples
  40. * and sort operator OIDs. We save some cycles by passing and returning the
  41. * tuples in TupleTableSlots, rather than forming actual HeapTuples (which'd
  42. * have to be converted to MinimalTuples). This API works well for sorts
  43. * executed as parts of plan trees.
  44. *
  45. * The "cluster" API stores/sorts full HeapTuples including all visibility
  46. * info. The sort keys are specified by reference to a btree index that is
  47. * defined on the relation to be sorted. Note that putheaptuple/getheaptuple
  48. * go with this API, not the "begin_heap" one!
  49. *
  50. * The "index_btree" API stores/sorts IndexTuples (preserving all their
  51. * header fields). The sort keys are specified by a btree index definition.
  52. *
  53. * The "index_hash" API is similar to index_btree, but the tuples are
  54. * actually sorted by their hash codes not the raw data.
  55. */
  56. extern Tuplesortstate *tuplesort_begin_heap(TupleDesc tupDesc,
  57. int nkeys, AttrNumber *attNums,
  58. Oid *sortOperators, Oid *sortCollations,
  59. bool *nullsFirstFlags,
  60. int workMem, bool randomAccess);
  61. extern Tuplesortstate *tuplesort_begin_cluster(TupleDesc tupDesc,
  62. Relation indexRel,
  63. int workMem, bool randomAccess);
  64. extern Tuplesortstate *tuplesort_begin_index_btree(Relation heapRel,
  65. Relation indexRel,
  66. bool enforceUnique,
  67. int workMem, bool randomAccess);
  68. extern Tuplesortstate *tuplesort_begin_index_hash(Relation heapRel,
  69. Relation indexRel,
  70. uint32 hash_mask,
  71. int workMem, bool randomAccess);
  72. extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
  73. Oid sortOperator, Oid sortCollation,
  74. bool nullsFirstFlag,
  75. int workMem, bool randomAccess);
  76. extern void tuplesort_set_bound(Tuplesortstate *state, int64 bound);
  77. extern void tuplesort_puttupleslot(Tuplesortstate *state,
  78. TupleTableSlot *slot);
  79. extern void tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup);
  80. extern void tuplesort_putindextuplevalues(Tuplesortstate *state,
  81. Relation rel, ItemPointer self,
  82. Datum *values, bool *isnull);
  83. extern void tuplesort_putdatum(Tuplesortstate *state, Datum val,
  84. bool isNull);
  85. extern void tuplesort_performsort(Tuplesortstate *state);
  86. extern bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
  87. TupleTableSlot *slot, Datum *abbrev);
  88. extern HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward,
  89. bool *should_free);
  90. extern IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward,
  91. bool *should_free);
  92. extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
  93. Datum *val, bool *isNull, Datum *abbrev);
  94. extern bool tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples,
  95. bool forward);
  96. extern void tuplesort_end(Tuplesortstate *state);
  97. extern void tuplesort_get_stats(Tuplesortstate *state,
  98. const char **sortMethod,
  99. const char **spaceType,
  100. long *spaceUsed);
  101. extern int tuplesort_merge_order(int64 allowedMem);
  102. /*
  103. * These routines may only be called if randomAccess was specified 'true'.
  104. * Likewise, backwards scan in gettuple/getdatum is only allowed if
  105. * randomAccess was specified.
  106. */
  107. extern void tuplesort_rescan(Tuplesortstate *state);
  108. extern void tuplesort_markpos(Tuplesortstate *state);
  109. extern void tuplesort_restorepos(Tuplesortstate *state);
  110. #endif /* TUPLESORT_H */