| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* blscan.c |
| 4 |
* Bloom index scan functions. |
| 5 |
* |
| 6 |
* Copyright (c) 2016-2025, 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" |
| 17 |
#include "miscadmin.h" |
| 18 |
#include "pgstat.h" |
| 19 |
#include "storage/bufmgr.h" |
| 20 |
|
| 21 |
/* |
| 22 |
* Begin scan of bloom index. |
| 23 |
*/ |
| 24 |
IndexScanDesc |
| 25 |
blbeginscan(Relation r, int nkeys, int norderbys) |
| 26 |
{ |
| 27 |
IndexScanDesc scan; |
| 28 |
BloomScanOpaque so; |
| 29 |
|
| 30 |
scan = RelationGetIndexScan(r, nkeys, norderbys); |
| 31 |
|
| 32 |
so = (BloomScanOpaque) palloc(sizeof(BloomScanOpaqueData)); |
| 33 |
initBloomState(&so->state, scan->indexRelation); |
| 34 |
so->sign = NULL; |
| 35 |
|
| 36 |
scan->opaque = so; |
| 37 |
|
| 38 |
return scan; |
| 39 |
} |
| 40 |
|
| 41 |
/* |
| 42 |
* Rescan a bloom index. |
| 43 |
*/ |
| 44 |
void |
| 45 |
blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, |
| 46 |
ScanKey orderbys, int norderbys) |
| 47 |
{ |
| 48 |
BloomScanOpaque so = (BloomScanOpaque) scan->opaque; |
| 49 |
|
| 50 |
if (so->sign) |
| 51 |
pfree(so->sign); |
| 52 |
so->sign = NULL; |
| 53 |
|
| 54 |
if (scankey && scan->numberOfKeys > 0) |
| 55 |
memcpy(scan->keyData, scankey, scan->numberOfKeys * sizeof(ScanKeyData)); |
| 56 |
} |
| 57 |
|
| 58 |
/* |
| 59 |
* End scan of bloom index. |
| 60 |
*/ |
| 61 |
void |
| 62 |
blendscan(IndexScanDesc scan) |
| 63 |
{ |
| 64 |
BloomScanOpaque so = (BloomScanOpaque) scan->opaque; |
| 65 |
|
| 66 |
if (so->sign) |
| 67 |
pfree(so->sign); |
| 68 |
so->sign = NULL; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* Insert all matching tuples into a bitmap. |
| 73 |
*/ |
| 74 |
int64 |
| 75 |
blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm) |
| 76 |
{ |
| 77 |
int64 ntids = 0; |
| 78 |
BlockNumber blkno = BLOOM_HEAD_BLKNO, |
| 79 |
npages; |
| 80 |
int i; |
| 81 |
BufferAccessStrategy bas; |
| 82 |
BloomScanOpaque so = (BloomScanOpaque) scan->opaque; |
| 83 |
|
| 84 |
if (so->sign == NULL) |
| 85 |
{ |
| 86 |
/* New search: have to calculate search signature */ |
| 87 |
ScanKey skey = scan->keyData; |
| 88 |
|
| 89 |
so->sign = palloc0(sizeof(BloomSignatureWord) * so->state.opts.bloomLength); |
| 90 |
|
| 91 |
for (i = 0; i < scan->numberOfKeys; i++) |
| 92 |
{ |
| 93 |
/* |
| 94 |
* Assume bloom-indexable operators to be strict, so nothing could |
| 95 |
* be found for NULL key. |
| 96 |
*/ |
| 97 |
if (skey->sk_flags & SK_ISNULL) |
| 98 |
{ |
| 99 |
pfree(so->sign); |
| 100 |
so->sign = NULL; |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
/* Add next value to the signature */ |
| 105 |
signValue(&so->state, so->sign, skey->sk_argument, |
| 106 |
skey->sk_attno - 1); |
| 107 |
|
| 108 |
skey++; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* We're going to read the whole index. This is why we use appropriate |
| 114 |
* buffer access strategy. |
| 115 |
*/ |
| 116 |
bas = GetAccessStrategy(BAS_BULKREAD); |
| 117 |
npages = RelationGetNumberOfBlocks(scan->indexRelation); |
| 118 |
pgstat_count_index_scan(scan->indexRelation); |
| 119 |
if (scan->instrument) |
| 120 |
scan->instrument->nsearches++; |
| 121 |
|
| 122 |
for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++) |
| 123 |
{ |
| 124 |
Buffer buffer; |
| 125 |
Page page; |
| 126 |
|
| 127 |
buffer = ReadBufferExtended(scan->indexRelation, MAIN_FORKNUM, |
| 128 |
blkno, RBM_NORMAL, bas); |
| 129 |
|
| 130 |
LockBuffer(buffer, BUFFER_LOCK_SHARE); |
| 131 |
page = BufferGetPage(buffer); |
| 132 |
|
| 133 |
if (!PageIsNew(page) && !BloomPageIsDeleted(page)) |
| 134 |
{ |
| 135 |
OffsetNumber offset, |
| 136 |
maxOffset = BloomPageGetMaxOffset(page); |
| 137 |
|
| 138 |
for (offset = 1; offset <= maxOffset; offset++) |
| 139 |
{ |
| 140 |
BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset); |
| 141 |
bool res = true; |
| 142 |
|
| 143 |
/* Check index signature with scan signature */ |
| 144 |
for (i = 0; i < so->state.opts.bloomLength; i++) |
| 145 |
{ |
| 146 |
if ((itup->sign[i] & so->sign[i]) != so->sign[i]) |
| 147 |
{ |
| 148 |
res = false; |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/* Add matching tuples to bitmap */ |
| 154 |
if (res) |
| 155 |
{ |
| 156 |
tbm_add_tuples(tbm, &itup->heapPtr, 1, true); |
| 157 |
ntids++; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
UnlockReleaseBuffer(buffer); |
| 163 |
CHECK_FOR_INTERRUPTS(); |
| 164 |
} |
| 165 |
FreeAccessStrategy(bas); |
| 166 |
|
| 167 |
return ntids; |
| 168 |
} |