PostgreSQL Source Code  git master
blscan.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * blscan.c
4  * Bloom index scan functions.
5  *
6  * Copyright (c) 2016-2024, 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 #include "storage/lmgr.h"
21 #include "utils/memutils.h"
22 #include "utils/rel.h"
23 
24 /*
25  * Begin scan of bloom index.
26  */
28 blbeginscan(Relation r, int nkeys, int norderbys)
29 {
30  IndexScanDesc scan;
31  BloomScanOpaque so;
32 
33  scan = RelationGetIndexScan(r, nkeys, norderbys);
34 
36  initBloomState(&so->state, scan->indexRelation);
37  so->sign = NULL;
38 
39  scan->opaque = so;
40 
41  return scan;
42 }
43 
44 /*
45  * Rescan a bloom index.
46  */
47 void
48 blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
49  ScanKey orderbys, int norderbys)
50 {
52 
53  if (so->sign)
54  pfree(so->sign);
55  so->sign = NULL;
56 
57  if (scankey && scan->numberOfKeys > 0)
58  {
59  memmove(scan->keyData, scankey,
60  scan->numberOfKeys * sizeof(ScanKeyData));
61  }
62 }
63 
64 /*
65  * End scan of bloom index.
66  */
67 void
69 {
71 
72  if (so->sign)
73  pfree(so->sign);
74  so->sign = NULL;
75 }
76 
77 /*
78  * Insert all matching tuples into a bitmap.
79  */
80 int64
82 {
83  int64 ntids = 0;
85  npages;
86  int i;
89 
90  if (so->sign == NULL)
91  {
92  /* New search: have to calculate search signature */
93  ScanKey skey = scan->keyData;
94 
95  so->sign = palloc0(sizeof(BloomSignatureWord) * so->state.opts.bloomLength);
96 
97  for (i = 0; i < scan->numberOfKeys; i++)
98  {
99  /*
100  * Assume bloom-indexable operators to be strict, so nothing could
101  * be found for NULL key.
102  */
103  if (skey->sk_flags & SK_ISNULL)
104  {
105  pfree(so->sign);
106  so->sign = NULL;
107  return 0;
108  }
109 
110  /* Add next value to the signature */
111  signValue(&so->state, so->sign, skey->sk_argument,
112  skey->sk_attno - 1);
113 
114  skey++;
115  }
116  }
117 
118  /*
119  * We're going to read the whole index. This is why we use appropriate
120  * buffer access strategy.
121  */
124 
125  for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
126  {
127  Buffer buffer;
128  Page page;
129 
131  blkno, RBM_NORMAL, bas);
132 
133  LockBuffer(buffer, BUFFER_LOCK_SHARE);
134  page = BufferGetPage(buffer);
135 
136  if (!PageIsNew(page) && !BloomPageIsDeleted(page))
137  {
138  OffsetNumber offset,
139  maxOffset = BloomPageGetMaxOffset(page);
140 
141  for (offset = 1; offset <= maxOffset; offset++)
142  {
143  BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset);
144  bool res = true;
145 
146  /* Check index signature with scan signature */
147  for (i = 0; i < so->state.opts.bloomLength; i++)
148  {
149  if ((itup->sign[i] & so->sign[i]) != so->sign[i])
150  {
151  res = false;
152  break;
153  }
154  }
155 
156  /* Add matching tuples to bitmap */
157  if (res)
158  {
159  tbm_add_tuples(tbm, &itup->heapPtr, 1, true);
160  ntids++;
161  }
162  }
163  }
164 
165  UnlockReleaseBuffer(buffer);
167  }
168  FreeAccessStrategy(bas);
169 
170  return ntids;
171 }
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:262
#define BloomPageIsDeleted(page)
Definition: bloom.h:64
void initBloomState(BloomState *state, Relation index)
Definition: blutils.c:163
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:175
#define BLOOM_HEAD_BLKNO
Definition: bloom.h:79
int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: blscan.c:81
IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys)
Definition: blscan.c:28
void blendscan(IndexScanDesc scan)
Definition: blscan.c:68
void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: blscan.c:48
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4577
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4795
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:781
@ BAS_BULKREAD
Definition: bufmgr.h:35
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:158
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:229
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
@ RBM_NORMAL
Definition: bufmgr.h:44
Pointer Page
Definition: bufpage.h:78
static bool PageIsNew(Page page)
Definition: bufpage.h:230
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:639
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void pfree(void *pointer)
Definition: mcxt.c:1508
void * palloc0(Size size)
Definition: mcxt.c:1334
void * palloc(Size size)
Definition: mcxt.c:1304
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
uint16 OffsetNumber
Definition: off.h:24
@ MAIN_FORKNUM
Definition: relpath.h:50
#define SK_ISNULL
Definition: skey.h:115
int bloomLength
Definition: bloom.h:104
BloomSignatureWord * sign
Definition: bloom.h:171
BloomState state
Definition: bloom.h:172
BloomOptions opts
Definition: bloom.h:142
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]
Definition: bloom.h:163
ItemPointerData heapPtr
Definition: bloom.h:162
struct ScanKeyData * keyData
Definition: relscan.h:122
Relation indexRelation
Definition: relscan.h:118
int sk_flags
Definition: skey.h:66
Datum sk_argument
Definition: skey.h:72
AttrNumber sk_attno
Definition: skey.h:67
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:377