PostgreSQL Source Code git master
Loading...
Searching...
No Matches
blscan.c File Reference
#include "postgres.h"
#include "access/relscan.h"
#include "bloom.h"
#include "executor/instrument_node.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/read_stream.h"
Include dependency graph for blscan.c:

Go to the source code of this file.

Functions

IndexScanDesc blbeginscan (Relation r, int nkeys, int norderbys)
 
void blrescan (IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
 
void blendscan (IndexScanDesc scan)
 
int64 blgetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 

Function Documentation

◆ blbeginscan()

IndexScanDesc blbeginscan ( Relation  r,
int  nkeys,
int  norderbys 
)

Definition at line 27 of file blscan.c.

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}
void initBloomState(BloomState *state, Relation index)
Definition blutils.c:167
BloomScanOpaqueData * BloomScanOpaque
Definition bloom.h:172
#define palloc_object(type)
Definition fe_memutils.h:74
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition genam.c:80
static int fb(int x)
Relation indexRelation
Definition relscan.h:138

References fb(), IndexScanDescData::indexRelation, initBloomState(), IndexScanDescData::opaque, palloc_object, and RelationGetIndexScan().

Referenced by blhandler().

◆ blendscan()

void blendscan ( IndexScanDesc  scan)

Definition at line 64 of file blscan.c.

65{
67
68 if (so->sign)
69 pfree(so->sign);
70 so->sign = NULL;
71}
void pfree(void *pointer)
Definition mcxt.c:1616

References fb(), IndexScanDescData::opaque, and pfree().

Referenced by blhandler().

◆ blgetbitmap()

int64 blgetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 77 of file blscan.c.

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
#define BLOOM_HEAD_BLKNO
Definition bloom.h:79
int Buffer
Definition buf.h:23
#define InvalidBuffer
Definition buf.h:25
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5518
@ 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:233
PageData * Page
Definition bufpage.h:81
#define Assert(condition)
Definition c.h:915
int64_t int64
Definition c.h:585
#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
int i
Definition isn.c:77
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
uint16 OffsetNumber
Definition off.h:24
#define pgstat_count_index_scan(rel)
Definition pgstat.h:708
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
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids, bool recheck)
Definition tidbitmap.c:367

References Assert, BAS_BULKREAD, block_range_read_stream_cb(), BLOOM_HEAD_BLKNO, BloomPageGetMaxOffset, BloomPageGetTuple, BloomPageIsDeleted, BUFFER_LOCK_SHARE, BufferGetPage(), CHECK_FOR_INTERRUPTS, BlockRangeReadStreamPrivate::current_blocknum, fb(), FreeAccessStrategy(), GetAccessStrategy(), BloomTuple::heapPtr, i, IndexScanDescData::indexRelation, IndexScanDescData::instrument, InvalidBuffer, IndexScanDescData::keyData, BlockRangeReadStreamPrivate::last_exclusive, LockBuffer(), MAIN_FORKNUM, IndexScanInstrumentation::nsearches, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, PageIsNew(), palloc0_array, pfree(), pgstat_count_index_scan, read_stream_begin_relation(), read_stream_end(), READ_STREAM_FULL, read_stream_next_buffer(), READ_STREAM_USE_BATCHING, RelationGetNumberOfBlocks, BloomTuple::sign, signValue(), SK_ISNULL, tbm_add_tuples(), and UnlockReleaseBuffer().

Referenced by blhandler().

◆ blrescan()

void blrescan ( IndexScanDesc  scan,
ScanKey  scankey,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 47 of file blscan.c.

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}

References fb(), IndexScanDescData::keyData, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, and pfree().

Referenced by blhandler().