You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bloom.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*-------------------------------------------------------------------------
  2. *
  3. * bloom.h
  4. * Header for bloom index.
  5. *
  6. * Copyright (c) 2016-2019, PostgreSQL Global Development Group
  7. *
  8. * IDENTIFICATION
  9. * contrib/bloom/bloom.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef _BLOOM_H_
  14. #define _BLOOM_H_
  15. #include "access/amapi.h"
  16. #include "access/generic_xlog.h"
  17. #include "access/itup.h"
  18. #include "access/xlog.h"
  19. #include "nodes/relation.h"
  20. #include "fmgr.h"
  21. /* Support procedures numbers */
  22. #define BLOOM_HASH_PROC 1
  23. #define BLOOM_NPROC 1
  24. /* Scan strategies */
  25. #define BLOOM_EQUAL_STRATEGY 1
  26. #define BLOOM_NSTRATEGIES 1
  27. /* Opaque for bloom pages */
  28. typedef struct BloomPageOpaqueData
  29. {
  30. OffsetNumber maxoff; /* number of index tuples on page */
  31. uint16 flags; /* see bit definitions below */
  32. uint16 unused; /* placeholder to force maxaligning of size of
  33. * BloomPageOpaqueData and to place
  34. * bloom_page_id exactly at the end of page */
  35. uint16 bloom_page_id; /* for identification of BLOOM indexes */
  36. } BloomPageOpaqueData;
  37. typedef BloomPageOpaqueData *BloomPageOpaque;
  38. /* Bloom page flags */
  39. #define BLOOM_META (1<<0)
  40. #define BLOOM_DELETED (2<<0)
  41. /*
  42. * The page ID is for the convenience of pg_filedump and similar utilities,
  43. * which otherwise would have a hard time telling pages of different index
  44. * types apart. It should be the last 2 bytes on the page. This is more or
  45. * less "free" due to alignment considerations.
  46. *
  47. * See comments above GinPageOpaqueData.
  48. */
  49. #define BLOOM_PAGE_ID 0xFF83
  50. /* Macros for accessing bloom page structures */
  51. #define BloomPageGetOpaque(page) ((BloomPageOpaque) PageGetSpecialPointer(page))
  52. #define BloomPageGetMaxOffset(page) (BloomPageGetOpaque(page)->maxoff)
  53. #define BloomPageIsMeta(page) \
  54. ((BloomPageGetOpaque(page)->flags & BLOOM_META) != 0)
  55. #define BloomPageIsDeleted(page) \
  56. ((BloomPageGetOpaque(page)->flags & BLOOM_DELETED) != 0)
  57. #define BloomPageSetDeleted(page) \
  58. (BloomPageGetOpaque(page)->flags |= BLOOM_DELETED)
  59. #define BloomPageSetNonDeleted(page) \
  60. (BloomPageGetOpaque(page)->flags &= ~BLOOM_DELETED)
  61. #define BloomPageGetData(page) ((BloomTuple *)PageGetContents(page))
  62. #define BloomPageGetTuple(state, page, offset) \
  63. ((BloomTuple *)(PageGetContents(page) \
  64. + (state)->sizeOfBloomTuple * ((offset) - 1)))
  65. #define BloomPageGetNextTuple(state, tuple) \
  66. ((BloomTuple *)((Pointer)(tuple) + (state)->sizeOfBloomTuple))
  67. /* Preserved page numbers */
  68. #define BLOOM_METAPAGE_BLKNO (0)
  69. #define BLOOM_HEAD_BLKNO (1) /* first data page */
  70. /*
  71. * We store Bloom signatures as arrays of uint16 words.
  72. */
  73. typedef uint16 BloomSignatureWord;
  74. #define SIGNWORDBITS ((int) (BITS_PER_BYTE * sizeof(BloomSignatureWord)))
  75. /*
  76. * Default and maximum Bloom signature length in bits.
  77. */
  78. #define DEFAULT_BLOOM_LENGTH (5 * SIGNWORDBITS)
  79. #define MAX_BLOOM_LENGTH (256 * SIGNWORDBITS)
  80. /*
  81. * Default and maximum signature bits generated per index key.
  82. */
  83. #define DEFAULT_BLOOM_BITS 2
  84. #define MAX_BLOOM_BITS (MAX_BLOOM_LENGTH - 1)
  85. /* Bloom index options */
  86. typedef struct BloomOptions
  87. {
  88. int32 vl_len_; /* varlena header (do not touch directly!) */
  89. int bloomLength; /* length of signature in words (not bits!) */
  90. int bitSize[INDEX_MAX_KEYS]; /* # of bits generated for each
  91. * index key */
  92. } BloomOptions;
  93. /*
  94. * FreeBlockNumberArray - array of block numbers sized so that metadata fill
  95. * all space in metapage.
  96. */
  97. typedef BlockNumber FreeBlockNumberArray[
  98. MAXALIGN_DOWN(
  99. BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(BloomPageOpaqueData))
  100. - MAXALIGN(sizeof(uint16) * 2 + sizeof(uint32) + sizeof(BloomOptions))
  101. ) / sizeof(BlockNumber)
  102. ];
  103. /* Metadata of bloom index */
  104. typedef struct BloomMetaPageData
  105. {
  106. uint32 magickNumber;
  107. uint16 nStart;
  108. uint16 nEnd;
  109. BloomOptions opts;
  110. FreeBlockNumberArray notFullPage;
  111. } BloomMetaPageData;
  112. /* Magic number to distinguish bloom pages among anothers */
  113. #define BLOOM_MAGICK_NUMBER (0xDBAC0DED)
  114. /* Number of blocks numbers fit in BloomMetaPageData */
  115. #define BloomMetaBlockN (sizeof(FreeBlockNumberArray) / sizeof(BlockNumber))
  116. #define BloomPageGetMeta(page) ((BloomMetaPageData *) PageGetContents(page))
  117. typedef struct BloomState
  118. {
  119. FmgrInfo hashFn[INDEX_MAX_KEYS];
  120. BloomOptions opts; /* copy of options on index's metapage */
  121. int32 nColumns;
  122. /*
  123. * sizeOfBloomTuple is index-specific, and it depends on reloptions, so
  124. * precompute it
  125. */
  126. Size sizeOfBloomTuple;
  127. } BloomState;
  128. #define BloomPageGetFreeSpace(state, page) \
  129. (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
  130. - BloomPageGetMaxOffset(page) * (state)->sizeOfBloomTuple \
  131. - MAXALIGN(sizeof(BloomPageOpaqueData)))
  132. /*
  133. * Tuples are very different from all other relations
  134. */
  135. typedef struct BloomTuple
  136. {
  137. ItemPointerData heapPtr;
  138. BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER];
  139. } BloomTuple;
  140. #define BLOOMTUPLEHDRSZ offsetof(BloomTuple, sign)
  141. /* Opaque data structure for bloom index scan */
  142. typedef struct BloomScanOpaqueData
  143. {
  144. BloomSignatureWord *sign; /* Scan signature */
  145. BloomState state;
  146. } BloomScanOpaqueData;
  147. typedef BloomScanOpaqueData *BloomScanOpaque;
  148. /* blutils.c */
  149. extern void _PG_init(void);
  150. extern void initBloomState(BloomState *state, Relation index);
  151. extern void BloomFillMetapage(Relation index, Page metaPage);
  152. extern void BloomInitMetapage(Relation index);
  153. extern void BloomInitPage(Page page, uint16 flags);
  154. extern Buffer BloomNewBuffer(Relation index);
  155. extern void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno);
  156. extern BloomTuple *BloomFormTuple(BloomState *state, ItemPointer iptr, Datum *values, bool *isnull);
  157. extern bool BloomPageAddItem(BloomState *state, Page page, BloomTuple *tuple);
  158. /* blvalidate.c */
  159. extern bool blvalidate(Oid opclassoid);
  160. /* index access method interface functions */
  161. extern bool blinsert(Relation index, Datum *values, bool *isnull,
  162. ItemPointer ht_ctid, Relation heapRel,
  163. IndexUniqueCheck checkUnique,
  164. struct IndexInfo *indexInfo);
  165. extern IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys);
  166. extern int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm);
  167. extern void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
  168. ScanKey orderbys, int norderbys);
  169. extern void blendscan(IndexScanDesc scan);
  170. extern IndexBuildResult *blbuild(Relation heap, Relation index,
  171. struct IndexInfo *indexInfo);
  172. extern void blbuildempty(Relation index);
  173. extern IndexBulkDeleteResult *blbulkdelete(IndexVacuumInfo *info,
  174. IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback,
  175. void *callback_state);
  176. extern IndexBulkDeleteResult *blvacuumcleanup(IndexVacuumInfo *info,
  177. IndexBulkDeleteResult *stats);
  178. extern bytea *bloptions(Datum reloptions, bool validate);
  179. extern void blcostestimate(PlannerInfo *root, IndexPath *path,
  180. double loop_count, Cost *indexStartupCost,
  181. Cost *indexTotalCost, Selectivity *indexSelectivity,
  182. double *indexCorrelation, double *indexPages);
  183. #endif