1 /*-------------------------------------------------------------------------
4 * Header for bloom index.
6 * Copyright (c) 2016-2020, PostgreSQL Global Development Group
9 * contrib/bloom/bloom.h
11 *-------------------------------------------------------------------------
16 #include "access/amapi.h"
17 #include "access/generic_xlog.h"
18 #include "access/itup.h"
19 #include "access/xlog.h"
21 #include "nodes/pathnodes.h"
23 /* Support procedures numbers */
24 #define BLOOM_HASH_PROC 1
25 #define BLOOM_OPTIONS_PROC 2
29 #define BLOOM_EQUAL_STRATEGY 1
30 #define BLOOM_NSTRATEGIES 1
32 /* Opaque for bloom pages */
33 typedef struct BloomPageOpaqueData
35 OffsetNumber maxoff
; /* number of index tuples on page */
36 uint16 flags
; /* see bit definitions below */
37 uint16 unused
; /* placeholder to force maxaligning of size of
38 * BloomPageOpaqueData and to place
39 * bloom_page_id exactly at the end of page */
40 uint16 bloom_page_id
; /* for identification of BLOOM indexes */
41 } BloomPageOpaqueData
;
43 typedef BloomPageOpaqueData
*BloomPageOpaque
;
45 /* Bloom page flags */
46 #define BLOOM_META (1<<0)
47 #define BLOOM_DELETED (2<<0)
50 * The page ID is for the convenience of pg_filedump and similar utilities,
51 * which otherwise would have a hard time telling pages of different index
52 * types apart. It should be the last 2 bytes on the page. This is more or
53 * less "free" due to alignment considerations.
55 * See comments above GinPageOpaqueData.
57 #define BLOOM_PAGE_ID 0xFF83
59 /* Macros for accessing bloom page structures */
60 #define BloomPageGetOpaque(page) ((BloomPageOpaque) PageGetSpecialPointer(page))
61 #define BloomPageGetMaxOffset(page) (BloomPageGetOpaque(page)->maxoff)
62 #define BloomPageIsMeta(page) \
63 ((BloomPageGetOpaque(page)->flags & BLOOM_META) != 0)
64 #define BloomPageIsDeleted(page) \
65 ((BloomPageGetOpaque(page)->flags & BLOOM_DELETED) != 0)
66 #define BloomPageSetDeleted(page) \
67 (BloomPageGetOpaque(page)->flags |= BLOOM_DELETED)
68 #define BloomPageSetNonDeleted(page) \
69 (BloomPageGetOpaque(page)->flags &= ~BLOOM_DELETED)
70 #define BloomPageGetData(page) ((BloomTuple *)PageGetContents(page))
71 #define BloomPageGetTuple(state, page, offset) \
72 ((BloomTuple *)(PageGetContents(page) \
73 + (state)->sizeOfBloomTuple * ((offset) - 1)))
74 #define BloomPageGetNextTuple(state, tuple) \
75 ((BloomTuple *)((Pointer)(tuple) + (state)->sizeOfBloomTuple))
77 /* Preserved page numbers */
78 #define BLOOM_METAPAGE_BLKNO (0)
79 #define BLOOM_HEAD_BLKNO (1) /* first data page */
82 * We store Bloom signatures as arrays of uint16 words.
84 typedef uint16 BloomSignatureWord
;
86 #define SIGNWORDBITS ((int) (BITS_PER_BYTE * sizeof(BloomSignatureWord)))
89 * Default and maximum Bloom signature length in bits.
91 #define DEFAULT_BLOOM_LENGTH (5 * SIGNWORDBITS)
92 #define MAX_BLOOM_LENGTH (256 * SIGNWORDBITS)
95 * Default and maximum signature bits generated per index key.
97 #define DEFAULT_BLOOM_BITS 2
98 #define MAX_BLOOM_BITS (MAX_BLOOM_LENGTH - 1)
100 /* Bloom index options */
101 typedef struct BloomOptions
103 int32 vl_len_
; /* varlena header (do not touch directly!) */
104 int bloomLength
; /* length of signature in words (not bits!) */
105 int bitSize
[INDEX_MAX_KEYS
]; /* # of bits generated for each
110 * FreeBlockNumberArray - array of block numbers sized so that metadata fill
111 * all space in metapage.
113 typedef BlockNumber FreeBlockNumberArray
[
115 BLCKSZ
- SizeOfPageHeaderData
- MAXALIGN(sizeof(BloomPageOpaqueData
))
116 - MAXALIGN(sizeof(uint16
) * 2 + sizeof(uint32
) + sizeof(BloomOptions
))
117 ) / sizeof(BlockNumber
)
120 /* Metadata of bloom index */
121 typedef struct BloomMetaPageData
127 FreeBlockNumberArray notFullPage
;
130 /* Magic number to distinguish bloom pages among anothers */
131 #define BLOOM_MAGICK_NUMBER (0xDBAC0DED)
133 /* Number of blocks numbers fit in BloomMetaPageData */
134 #define BloomMetaBlockN (sizeof(FreeBlockNumberArray) / sizeof(BlockNumber))
136 #define BloomPageGetMeta(page) ((BloomMetaPageData *) PageGetContents(page))
138 typedef struct BloomState
140 FmgrInfo hashFn
[INDEX_MAX_KEYS
];
141 Oid collations
[INDEX_MAX_KEYS
];
142 BloomOptions opts
; /* copy of options on index's metapage */
146 * sizeOfBloomTuple is index-specific, and it depends on reloptions, so
149 Size sizeOfBloomTuple
;
152 #define BloomPageGetFreeSpace(state, page) \
153 (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
154 - BloomPageGetMaxOffset(page) * (state)->sizeOfBloomTuple \
155 - MAXALIGN(sizeof(BloomPageOpaqueData)))
158 * Tuples are very different from all other relations
160 typedef struct BloomTuple
162 ItemPointerData heapPtr
;
163 BloomSignatureWord sign
[FLEXIBLE_ARRAY_MEMBER
];
166 #define BLOOMTUPLEHDRSZ offsetof(BloomTuple, sign)
168 /* Opaque data structure for bloom index scan */
169 typedef struct BloomScanOpaqueData
171 BloomSignatureWord
*sign
; /* Scan signature */
173 } BloomScanOpaqueData
;
175 typedef BloomScanOpaqueData
*BloomScanOpaque
;
178 extern void _PG_init(void);
179 extern void initBloomState(BloomState
*state
, Relation index
);
180 extern void BloomFillMetapage(Relation index
, Page metaPage
);
181 extern void BloomInitMetapage(Relation index
);
182 extern void BloomInitPage(Page page
, uint16 flags
);
183 extern Buffer
BloomNewBuffer(Relation index
);
184 extern void signValue(BloomState
*state
, BloomSignatureWord
*sign
, Datum value
, int attno
);
185 extern BloomTuple
*BloomFormTuple(BloomState
*state
, ItemPointer iptr
, Datum
*values
, bool *isnull
);
186 extern bool BloomPageAddItem(BloomState
*state
, Page page
, BloomTuple
*tuple
);
189 extern bool blvalidate(Oid opclassoid
);
191 /* index access method interface functions */
192 extern bool blinsert(Relation index
, Datum
*values
, bool *isnull
,
193 ItemPointer ht_ctid
, Relation heapRel
,
194 IndexUniqueCheck checkUnique
,
195 struct IndexInfo
*indexInfo
);
196 extern IndexScanDesc
blbeginscan(Relation r
, int nkeys
, int norderbys
);
197 extern int64
blgetbitmap(IndexScanDesc scan
, TIDBitmap
*tbm
);
198 extern void blrescan(IndexScanDesc scan
, ScanKey scankey
, int nscankeys
,
199 ScanKey orderbys
, int norderbys
);
200 extern void blendscan(IndexScanDesc scan
);
201 extern IndexBuildResult
*blbuild(Relation heap
, Relation index
,
202 struct IndexInfo
*indexInfo
);
203 extern void blbuildempty(Relation index
);
204 extern IndexBulkDeleteResult
*blbulkdelete(IndexVacuumInfo
*info
,
205 IndexBulkDeleteResult
*stats
, IndexBulkDeleteCallback callback
,
206 void *callback_state
);
207 extern IndexBulkDeleteResult
*blvacuumcleanup(IndexVacuumInfo
*info
,
208 IndexBulkDeleteResult
*stats
);
209 extern bytea
*bloptions(Datum reloptions
, bool validate
);
210 extern void blcostestimate(PlannerInfo
*root
, IndexPath
*path
,
211 double loop_count
, Cost
*indexStartupCost
,
212 Cost
*indexTotalCost
, Selectivity
*indexSelectivity
,
213 double *indexCorrelation
, double *indexPages
);