PostgreSQL Source Code git master
Loading...
Searching...
No Matches
blscan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * blscan.c
4 * Bloom index scan functions.
5 *
6 * Copyright (c) 2016-2026, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * contrib/bloom/blscan.c
10 *
11 *-------------------------------------------------------------------------
12 */
13#include "postgres.h"
14
15#include "access/relscan.h"
16#include "bloom.h"
18#include "miscadmin.h"
19#include "pgstat.h"
20#include "storage/bufmgr.h"
21
22/*
23 * Begin scan of bloom index.
24 */
26blbeginscan(Relation r, int nkeys, int norderbys)
27{
28 IndexScanDesc scan;
30
31 scan = RelationGetIndexScan(r, nkeys, norderbys);
32
34 initBloomState(&so->state, scan->indexRelation);
35 so->sign = NULL;
36
37 scan->opaque = so;
38
39 return scan;
40}
41
42/*
43 * Rescan a bloom index.
44 */
45void
47 ScanKey orderbys, int norderbys)
48{
50
51 if (so->sign)
52 pfree(so->sign);
53 so->sign = NULL;
54
55 if (scankey && scan->numberOfKeys > 0)
56 memcpy(scan->keyData, scankey, scan->numberOfKeys * sizeof(ScanKeyData));
57}
58
59/*
60 * End scan of bloom index.
61 */
62void
64{
66
67 if (so->sign)
68 pfree(so->sign);
69 so->sign = NULL;
70}
71
72/*
73 * Insert all matching tuples into a bitmap.
74 */
77{
78 int64 ntids = 0;
80 npages;
81 int i;
84
85 if (so->sign == NULL)
86 {
87 /* New search: have to calculate search signature */
88 ScanKey skey = scan->keyData;
89
90 so->sign = palloc0_array(BloomSignatureWord, so->state.opts.bloomLength);
91
92 for (i = 0; i < scan->numberOfKeys; i++)
93 {
94 /*
95 * Assume bloom-indexable operators to be strict, so nothing could
96 * be found for NULL key.
97 */
98 if (skey->sk_flags & SK_ISNULL)
99 {
100 pfree(so->sign);
101 so->sign = NULL;
102 return 0;
103 }
104
105 /* Add next value to the signature */
106 signValue(&so->state, so->sign, skey->sk_argument,
107 skey->sk_attno - 1);
108
109 skey++;
110 }
111 }
112
113 /*
114 * We're going to read the whole index. This is why we use appropriate
115 * buffer access strategy.
116 */
120 if (scan->instrument)
121 scan->instrument->nsearches++;
122
123 for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
124 {
125 Buffer buffer;
126 Page page;
127
129 blkno, RBM_NORMAL, bas);
130
132 page = BufferGetPage(buffer);
133
134 if (!PageIsNew(page) && !BloomPageIsDeleted(page))
135 {
136 OffsetNumber offset,
138
139 for (offset = 1; offset <= maxOffset; offset++)
140 {
141 BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset);
142 bool res = true;
143
144 /* Check index signature with scan signature */
145 for (i = 0; i < so->state.opts.bloomLength; i++)
146 {
147 if ((itup->sign[i] & so->sign[i]) != so->sign[i])
148 {
149 res = false;
150 break;
151 }
152 }
153
154 /* Add matching tuples to bitmap */
155 if (res)
156 {
157 tbm_add_tuples(tbm, &itup->heapPtr, 1, true);
158 ntids++;
159 }
160 }
161 }
162
163 UnlockReleaseBuffer(buffer);
165 }
167
168 return ntids;
169}
uint32 BlockNumber
Definition block.h:31
#define BloomPageGetMaxOffset(page)
Definition bloom.h:61
#define BloomPageGetTuple(state, page, offset)
Definition bloom.h:71
uint16 BloomSignatureWord
Definition bloom.h:84
void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
Definition blutils.c:266
#define BloomPageIsDeleted(page)
Definition bloom.h:64
void initBloomState(BloomState *state, Relation index)
Definition blutils.c:167
BloomScanOpaqueData * BloomScanOpaque
Definition bloom.h:172
#define BLOOM_HEAD_BLKNO
Definition bloom.h:79
int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition blscan.c:76
IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys)
Definition blscan.c:26
void blendscan(IndexScanDesc scan)
Definition blscan.c:63
void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition blscan.c:46
int Buffer
Definition buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5518
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition bufmgr.c:911
@ BAS_BULKREAD
Definition bufmgr.h:37
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:466
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:210
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:328
@ RBM_NORMAL
Definition bufmgr.h:46
static bool PageIsNew(const PageData *page)
Definition bufpage.h:233
PageData * Page
Definition bufpage.h:81
int64_t int64
Definition c.h:543
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc0_array(type, count)
Definition fe_memutils.h:77
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition freelist.c:461
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition freelist.c:643
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition genam.c:80
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
uint16 OffsetNumber
Definition off.h:24
#define pgstat_count_index_scan(rel)
Definition pgstat.h:705
static int fb(int x)
@ MAIN_FORKNUM
Definition relpath.h:58
#define SK_ISNULL
Definition skey.h:115
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]
Definition bloom.h:160
ItemPointerData heapPtr
Definition bloom.h:159
struct ScanKeyData * keyData
Definition relscan.h:142
struct IndexScanInstrumentation * instrument
Definition relscan.h:160
Relation indexRelation
Definition relscan.h:138
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids, bool recheck)
Definition tidbitmap.c:367