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#include "storage/read_stream.h"
22
23/*
24 * Begin scan of bloom index.
25 */
27blbeginscan(Relation r, int nkeys, int norderbys)
28{
29 IndexScanDesc scan;
31
32 scan = RelationGetIndexScan(r, nkeys, norderbys);
33
35 initBloomState(&so->state, scan->indexRelation);
36 so->sign = NULL;
37
38 scan->opaque = so;
39
40 return scan;
41}
42
43/*
44 * Rescan a bloom index.
45 */
46void
48 ScanKey orderbys, int norderbys)
49{
51
52 if (so->sign)
53 pfree(so->sign);
54 so->sign = NULL;
55
56 if (scankey && scan->numberOfKeys > 0)
57 memcpy(scan->keyData, scankey, scan->numberOfKeys * sizeof(ScanKeyData));
58}
59
60/*
61 * End scan of bloom index.
62 */
63void
65{
67
68 if (so->sign)
69 pfree(so->sign);
70 so->sign = NULL;
71}
72
73/*
74 * Insert all matching tuples into a bitmap.
75 */
78{
79 int64 ntids = 0;
80 BlockNumber blkno,
81 npages;
82 int i;
86 ReadStream *stream;
87
88 if (so->sign == NULL)
89 {
90 /* New search: have to calculate search signature */
91 ScanKey skey = scan->keyData;
92
93 so->sign = palloc0_array(BloomSignatureWord, so->state.opts.bloomLength);
94
95 for (i = 0; i < scan->numberOfKeys; i++)
96 {
97 /*
98 * Assume bloom-indexable operators to be strict, so nothing could
99 * be found for NULL key.
100 */
101 if (skey->sk_flags & SK_ISNULL)
102 {
103 pfree(so->sign);
104 so->sign = NULL;
105 return 0;
106 }
107
108 /* Add next value to the signature */
109 signValue(&so->state, so->sign, skey->sk_argument,
110 skey->sk_attno - 1);
111
112 skey++;
113 }
114 }
115
116 /*
117 * We're going to read the whole index. This is why we use appropriate
118 * buffer access strategy.
119 */
123 if (scan->instrument)
124 scan->instrument->nsearches++;
125
126 /* Scan all blocks except the metapage using streaming reads */
128 p.last_exclusive = npages;
129
130 /*
131 * It is safe to use batchmode as block_range_read_stream_cb takes no
132 * locks.
133 */
136 bas,
137 scan->indexRelation,
140 &p,
141 0);
142
143 for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
144 {
145 Buffer buffer;
146 Page page;
147
148 buffer = read_stream_next_buffer(stream, NULL);
150 page = BufferGetPage(buffer);
151
152 if (!PageIsNew(page) && !BloomPageIsDeleted(page))
153 {
154 OffsetNumber offset,
156
157 for (offset = 1; offset <= maxOffset; offset++)
158 {
159 BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset);
160 bool res = true;
161
162 /* Check index signature with scan signature */
163 for (i = 0; i < so->state.opts.bloomLength; i++)
164 {
165 if ((itup->sign[i] & so->sign[i]) != so->sign[i])
166 {
167 res = false;
168 break;
169 }
170 }
171
172 /* Add matching tuples to bitmap */
173 if (res)
174 {
175 tbm_add_tuples(tbm, &itup->heapPtr, 1, true);
176 ntids++;
177 }
178 }
179 }
180
181 UnlockReleaseBuffer(buffer);
183 }
184
186 read_stream_end(stream);
188
189 return ntids;
190}
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:77
IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys)
Definition blscan.c:27
void blendscan(IndexScanDesc scan)
Definition blscan.c:64
void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition blscan.c:47
int Buffer
Definition buf.h:23
#define InvalidBuffer
Definition buf.h:25
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5522
@ BAS_BULKREAD
Definition bufmgr.h:37
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:470
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:210
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:332
static bool PageIsNew(const PageData *page)
Definition bufpage.h:259
PageData * Page
Definition bufpage.h:81
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
#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:708
static int fb(int x)
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
ReadStream * read_stream_begin_relation(int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
void read_stream_end(ReadStream *stream)
BlockNumber block_range_read_stream_cb(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
#define READ_STREAM_USE_BATCHING
Definition read_stream.h:64
#define READ_STREAM_FULL
Definition read_stream.h:43
@ 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