gist.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*-------------------------------------------------------------------------
  2. *
  3. * gist.h
  4. * The public API for GiST indexes. This API is exposed to
  5. * individuals implementing GiST indexes, so backward-incompatible
  6. * changes should be made with care.
  7. *
  8. *
  9. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  10. * Portions Copyright (c) 1994, Regents of the University of California
  11. *
  12. * src/include/access/gist.h
  13. *
  14. *-------------------------------------------------------------------------
  15. */
  16. #ifndef GIST_H
  17. #define GIST_H
  18. #include "access/xlog.h"
  19. #include "access/xlogdefs.h"
  20. #include "storage/block.h"
  21. #include "storage/bufpage.h"
  22. #include "utils/relcache.h"
  23. /*
  24. * amproc indexes for GiST indexes.
  25. */
  26. #define GIST_CONSISTENT_PROC 1
  27. #define GIST_UNION_PROC 2
  28. #define GIST_COMPRESS_PROC 3
  29. #define GIST_DECOMPRESS_PROC 4
  30. #define GIST_PENALTY_PROC 5
  31. #define GIST_PICKSPLIT_PROC 6
  32. #define GIST_EQUAL_PROC 7
  33. #define GIST_DISTANCE_PROC 8
  34. #define GIST_FETCH_PROC 9
  35. #define GISTNProcs 9
  36. /*
  37. * Page opaque data in a GiST index page.
  38. */
  39. #define F_LEAF (1 << 0) /* leaf page */
  40. #define F_DELETED (1 << 1) /* the page has been deleted */
  41. #define F_TUPLES_DELETED (1 << 2) /* some tuples on the page were
  42. * deleted */
  43. #define F_FOLLOW_RIGHT (1 << 3) /* page to the right has no downlink */
  44. #define F_HAS_GARBAGE (1 << 4) /* some tuples on the page are dead,
  45. * but not deleted yet */
  46. typedef XLogRecPtr GistNSN;
  47. /*
  48. * For on-disk compatibility with pre-9.3 servers, NSN is stored as two
  49. * 32-bit fields on disk, same as LSNs.
  50. */
  51. typedef PageXLogRecPtr PageGistNSN;
  52. typedef struct GISTPageOpaqueData
  53. {
  54. PageGistNSN nsn; /* this value must change on page split */
  55. BlockNumber rightlink; /* next page if any */
  56. uint16 flags; /* see bit definitions above */
  57. uint16 gist_page_id; /* for identification of GiST indexes */
  58. } GISTPageOpaqueData;
  59. typedef GISTPageOpaqueData *GISTPageOpaque;
  60. /*
  61. * The page ID is for the convenience of pg_filedump and similar utilities,
  62. * which otherwise would have a hard time telling pages of different index
  63. * types apart. It should be the last 2 bytes on the page. This is more or
  64. * less "free" due to alignment considerations.
  65. */
  66. #define GIST_PAGE_ID 0xFF81
  67. /*
  68. * This is the Split Vector to be returned by the PickSplit method.
  69. * PickSplit should fill the indexes of tuples to go to the left side into
  70. * spl_left[], and those to go to the right into spl_right[] (note the method
  71. * is responsible for palloc'ing both of these arrays!). The tuple counts
  72. * go into spl_nleft/spl_nright, and spl_ldatum/spl_rdatum must be set to
  73. * the union keys for each side.
  74. *
  75. * If spl_ldatum_exists and spl_rdatum_exists are true, then we are performing
  76. * a "secondary split" using a non-first index column. In this case some
  77. * decisions have already been made about a page split, and the set of tuples
  78. * being passed to PickSplit is just the tuples about which we are undecided.
  79. * spl_ldatum/spl_rdatum then contain the union keys for the tuples already
  80. * chosen to go left or right. Ideally the PickSplit method should take those
  81. * keys into account while deciding what to do with the remaining tuples, ie
  82. * it should try to "build out" from those unions so as to minimally expand
  83. * them. If it does so, it should union the given tuples' keys into the
  84. * existing spl_ldatum/spl_rdatum values rather than just setting those values
  85. * from scratch, and then set spl_ldatum_exists/spl_rdatum_exists to false to
  86. * show it has done this.
  87. *
  88. * If the PickSplit method fails to clear spl_ldatum_exists/spl_rdatum_exists,
  89. * the core GiST code will make its own decision about how to merge the
  90. * secondary-split results with the previously-chosen tuples, and will then
  91. * recompute the union keys from scratch. This is a workable though often not
  92. * optimal approach.
  93. */
  94. typedef struct GIST_SPLITVEC
  95. {
  96. OffsetNumber *spl_left; /* array of entries that go left */
  97. int spl_nleft; /* size of this array */
  98. Datum spl_ldatum; /* Union of keys in spl_left */
  99. bool spl_ldatum_exists; /* true, if spl_ldatum already exists. */
  100. OffsetNumber *spl_right; /* array of entries that go right */
  101. int spl_nright; /* size of the array */
  102. Datum spl_rdatum; /* Union of keys in spl_right */
  103. bool spl_rdatum_exists; /* true, if spl_rdatum already exists. */
  104. } GIST_SPLITVEC;
  105. /*
  106. * An entry on a GiST node. Contains the key, as well as its own
  107. * location (rel,page,offset) which can supply the matching pointer.
  108. * leafkey is a flag to tell us if the entry is in a leaf node.
  109. */
  110. typedef struct GISTENTRY
  111. {
  112. Datum key;
  113. Relation rel;
  114. Page page;
  115. OffsetNumber offset;
  116. bool leafkey;
  117. } GISTENTRY;
  118. #define GistPageGetOpaque(page) ( (GISTPageOpaque) PageGetSpecialPointer(page) )
  119. #define GistPageIsLeaf(page) ( GistPageGetOpaque(page)->flags & F_LEAF)
  120. #define GIST_LEAF(entry) (GistPageIsLeaf((entry)->page))
  121. #define GistPageIsDeleted(page) ( GistPageGetOpaque(page)->flags & F_DELETED)
  122. #define GistPageSetDeleted(page) ( GistPageGetOpaque(page)->flags |= F_DELETED)
  123. #define GistPageSetNonDeleted(page) ( GistPageGetOpaque(page)->flags &= ~F_DELETED)
  124. #define GistTuplesDeleted(page) ( GistPageGetOpaque(page)->flags & F_TUPLES_DELETED)
  125. #define GistMarkTuplesDeleted(page) ( GistPageGetOpaque(page)->flags |= F_TUPLES_DELETED)
  126. #define GistClearTuplesDeleted(page) ( GistPageGetOpaque(page)->flags &= ~F_TUPLES_DELETED)
  127. #define GistPageHasGarbage(page) ( GistPageGetOpaque(page)->flags & F_HAS_GARBAGE)
  128. #define GistMarkPageHasGarbage(page) ( GistPageGetOpaque(page)->flags |= F_HAS_GARBAGE)
  129. #define GistClearPageHasGarbage(page) ( GistPageGetOpaque(page)->flags &= ~F_HAS_GARBAGE)
  130. #define GistFollowRight(page) ( GistPageGetOpaque(page)->flags & F_FOLLOW_RIGHT)
  131. #define GistMarkFollowRight(page) ( GistPageGetOpaque(page)->flags |= F_FOLLOW_RIGHT)
  132. #define GistClearFollowRight(page) ( GistPageGetOpaque(page)->flags &= ~F_FOLLOW_RIGHT)
  133. #define GistPageGetNSN(page) ( PageXLogRecPtrGet(GistPageGetOpaque(page)->nsn))
  134. #define GistPageSetNSN(page, val) ( PageXLogRecPtrSet(GistPageGetOpaque(page)->nsn, val))
  135. /*
  136. * Vector of GISTENTRY structs; user-defined methods union and picksplit
  137. * take it as one of their arguments
  138. */
  139. typedef struct
  140. {
  141. int32 n; /* number of elements */
  142. GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER];
  143. } GistEntryVector;
  144. #define GEVHDRSZ (offsetof(GistEntryVector, vector))
  145. /*
  146. * macro to initialize a GISTENTRY
  147. */
  148. #define gistentryinit(e, k, r, pg, o, l) \
  149. do { (e).key = (k); (e).rel = (r); (e).page = (pg); \
  150. (e).offset = (o); (e).leafkey = (l); } while (0)
  151. #endif /* GIST_H */