PostgreSQL Source Code  git master
blscan.c File Reference
#include "postgres.h"
#include "access/relscan.h"
#include "bloom.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/memutils.h"
#include "utils/rel.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 28 of file blscan.c.

References IndexScanDescData::indexRelation, initBloomState(), IndexScanDescData::opaque, palloc(), RelationGetIndexScan(), BloomScanOpaqueData::sign, and BloomScanOpaqueData::state.

Referenced by blhandler().

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 }
void initBloomState(BloomState *state, Relation index)
Definition: blutils.c:161
Relation indexRelation
Definition: relscan.h:115
BloomState state
Definition: bloom.h:172
void * palloc(Size size)
Definition: mcxt.c:950
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:175
BloomSignatureWord * sign
Definition: bloom.h:171
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:81

◆ blendscan()

void blendscan ( IndexScanDesc  scan)

Definition at line 68 of file blscan.c.

References IndexScanDescData::opaque, pfree(), and BloomScanOpaqueData::sign.

Referenced by blhandler().

69 {
71 
72  if (so->sign)
73  pfree(so->sign);
74  so->sign = NULL;
75 }
void pfree(void *pointer)
Definition: mcxt.c:1057
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:175
BloomSignatureWord * sign
Definition: bloom.h:171

◆ blgetbitmap()

int64 blgetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 81 of file blscan.c.

References BAS_BULKREAD, BLOOM_HEAD_BLKNO, BloomOptions::bloomLength, BloomPageGetMaxOffset, BloomPageGetTuple, BloomPageIsDeleted, BUFFER_LOCK_SHARE, BufferGetPage, CHECK_FOR_INTERRUPTS, FreeAccessStrategy(), GetAccessStrategy(), BloomTuple::heapPtr, i, IndexScanDescData::indexRelation, IndexScanDescData::keyData, LockBuffer(), MAIN_FORKNUM, IndexScanDescData::numberOfKeys, IndexScanDescData::opaque, BloomState::opts, PageIsNew, palloc0(), pfree(), RBM_NORMAL, ReadBufferExtended(), RelationGetNumberOfBlocks, BloomTuple::sign, BloomScanOpaqueData::sign, signValue(), ScanKeyData::sk_argument, ScanKeyData::sk_attno, ScanKeyData::sk_flags, SK_ISNULL, BloomScanOpaqueData::state, tbm_add_tuples(), TestForOldSnapshot(), UnlockReleaseBuffer(), and IndexScanDescData::xs_snapshot.

Referenced by blhandler().

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  TestForOldSnapshot(scan->xs_snapshot, scan->indexRelation, page);
136 
137  if (!PageIsNew(page) && !BloomPageIsDeleted(page))
138  {
139  OffsetNumber offset,
140  maxOffset = BloomPageGetMaxOffset(page);
141 
142  for (offset = 1; offset <= maxOffset; offset++)
143  {
144  BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset);
145  bool res = true;
146 
147  /* Check index signature with scan signature */
148  for (i = 0; i < so->state.opts.bloomLength; i++)
149  {
150  if ((itup->sign[i] & so->sign[i]) != so->sign[i])
151  {
152  res = false;
153  break;
154  }
155  }
156 
157  /* Add matching tuples to bitmap */
158  if (res)
159  {
160  tbm_add_tuples(tbm, &itup->heapPtr, 1, true);
161  ntids++;
162  }
163  }
164  }
165 
166  UnlockReleaseBuffer(buffer);
168  }
169  FreeAccessStrategy(bas);
170 
171  return ntids;
172 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
#define BloomPageIsDeleted(page)
Definition: bloom.h:64
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:277
BloomOptions opts
Definition: bloom.h:142
void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
Definition: blutils.c:260
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:376
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:653
struct SnapshotData * xs_snapshot
Definition: relscan.h:116
uint32 BlockNumber
Definition: block.h:31
#define BLOOM_HEAD_BLKNO
Definition: bloom.h:79
Relation indexRelation
Definition: relscan.h:115
uint16 OffsetNumber
Definition: off.h:24
uint16 BloomSignatureWord
Definition: bloom.h:84
void pfree(void *pointer)
Definition: mcxt.c:1057
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
#define BloomPageGetTuple(state, page, offset)
Definition: bloom.h:71
#define SK_ISNULL
Definition: skey.h:115
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void * palloc0(Size size)
Definition: mcxt.c:981
BloomState state
Definition: bloom.h:172
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:597
int sk_flags
Definition: skey.h:66
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]
Definition: bloom.h:163
int bloomLength
Definition: bloom.h:104
struct ScanKeyData * keyData
Definition: relscan.h:119
ItemPointerData heapPtr
Definition: bloom.h:162
#define PageIsNew(page)
Definition: bufpage.h:229
#define BloomPageGetMaxOffset(page)
Definition: bloom.h:61
int i
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:175
BloomSignatureWord * sign
Definition: bloom.h:171
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
int Buffer
Definition: buf.h:23
Datum sk_argument
Definition: skey.h:72
AttrNumber sk_attno
Definition: skey.h:67
Pointer Page
Definition: bufpage.h:78

◆ blrescan()

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

Definition at line 48 of file blscan.c.

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

Referenced by blhandler().

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 }
void pfree(void *pointer)
Definition: mcxt.c:1057
struct ScanKeyData * keyData
Definition: relscan.h:119
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:175
BloomSignatureWord * sign
Definition: bloom.h:171