1/*-------------------------------------------------------------------------
4 * Header for bloom index.
6 * Copyright (c) 2016-2025, PostgreSQL Global Development Group
9 * contrib/bloom/bloom.h
11 *-------------------------------------------------------------------------
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 */
38 * BloomPageOpaqueData and to place
39 * bloom_page_id exactly at the end of page */
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.
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 */
110 * FreeBlockNumberArray - array of block numbers sized so that metadata fill
111 * all space in metapage.
117/* Metadata of bloom index */
127/* Magic number to distinguish bloom pages from others */
128 #define BLOOM_MAGICK_NUMBER (0xDBAC0DED)
130/* Number of blocks numbers fit in BloomMetaPageData */
131 #define BloomMetaBlockN (sizeof(FreeBlockNumberArray) / sizeof(BlockNumber))
133 #define BloomPageGetMeta(page) ((BloomMetaPageData *) PageGetContents(page))
143 * sizeOfBloomTuple is index-specific, and it depends on reloptions, so
149 #define BloomPageGetFreeSpace(state, page) \
150 (BLCKSZ - MAXALIGN(SizeOfPageHeaderData) \
151 - BloomPageGetMaxOffset(page) * (state)->sizeOfBloomTuple \
152 - MAXALIGN(sizeof(BloomPageOpaqueData)))
155 * Tuples are very different from all other relations
163 #define BLOOMTUPLEHDRSZ offsetof(BloomTuple, sign)
165/* Opaque data structure for bloom index scan */
187/* index access method interface functions */
196 ScanKey orderbys,
int norderbys);
203 void *callback_state);
208 double loop_count,
Cost *indexStartupCost,
210 double *indexCorrelation,
double *indexPages);
static bool validate(Port *port, const char *auth)
IndexBuildResult * blbuild(Relation heap, Relation index, struct IndexInfo *indexInfo)
bool blvalidate(Oid opclassoid)
void BloomInitPage(Page page, uint16 flags)
int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys)
BloomTuple * BloomFormTuple(BloomState *state, ItemPointer iptr, Datum *values, bool *isnull)
bool BloomPageAddItem(BloomState *state, Page page, BloomTuple *tuple)
void blcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
void blbuildempty(Relation index)
BloomPageOpaqueData * BloomPageOpaque
BlockNumber FreeBlockNumberArray[MAXALIGN_DOWN(BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(BloomPageOpaqueData)) - MAXALIGN(sizeof(uint16) *2+sizeof(uint32)+sizeof(BloomOptions)))/sizeof(BlockNumber)]
struct BloomMetaPageData BloomMetaPageData
struct BloomOptions BloomOptions
Buffer BloomNewBuffer(Relation index)
struct BloomPageOpaqueData BloomPageOpaqueData
IndexBulkDeleteResult * blbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
void BloomFillMetapage(Relation index, Page metaPage)
bool blinsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, struct IndexInfo *indexInfo)
void BloomInitMetapage(Relation index, ForkNumber forknum)
struct BloomState BloomState
bytea * bloptions(Datum reloptions, bool validate)
IndexBulkDeleteResult * blvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
struct BloomTuple BloomTuple
uint16 BloomSignatureWord
void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
void initBloomState(BloomState *state, Relation index)
BloomScanOpaqueData * BloomScanOpaque
struct BloomScanOpaqueData BloomScanOpaqueData
void blendscan(IndexScanDesc scan)
void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
static Datum values[MAXATTR]
#define SizeOfPageHeaderData
#define MAXALIGN_DOWN(LEN)
#define FLEXIBLE_ARRAY_MEMBER
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
FreeBlockNumberArray notFullPage
int bitSize[INDEX_MAX_KEYS]
BloomSignatureWord * sign
Oid collations[INDEX_MAX_KEYS]
FmgrInfo hashFn[INDEX_MAX_KEYS]
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)