PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
blscan.c File Reference
#include "postgres.h"
#include "access/relscan.h"
#include "pgstat.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "bloom.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

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

Definition at line 29 of file blscan.c.

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

Referenced by blhandler().

30 {
31  IndexScanDesc scan;
32  BloomScanOpaque so;
33 
34  scan = RelationGetIndexScan(r, nkeys, norderbys);
35 
37  initBloomState(&so->state, scan->indexRelation);
38  so->sign = NULL;
39 
40  scan->opaque = so;
41 
42  return scan;
43 }
void initBloomState(BloomState *state, Relation index)
Definition: blutils.c:153
Relation indexRelation
Definition: relscan.h:89
BloomState state
Definition: bloom.h:170
#define NULL
Definition: c.h:229
void * palloc(Size size)
Definition: mcxt.c:849
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:173
BloomSignatureWord * sign
Definition: bloom.h:169
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78
void blendscan ( IndexScanDesc  scan)

Definition at line 69 of file blscan.c.

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

Referenced by blhandler().

70 {
72 
73  if (so->sign)
74  pfree(so->sign);
75  so->sign = NULL;
76 }
void pfree(void *pointer)
Definition: mcxt.c:950
#define NULL
Definition: c.h:229
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:173
BloomSignatureWord * sign
Definition: bloom.h:169
int64 blgetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 82 of file blscan.c.

References BAS_BULKREAD, BLOOM_HEAD_BLKNO, BloomOptions::bloomLength, BloomPageGetMaxOffset, BloomPageGetTuple, BloomPageIsDeleted, buffer, BUFFER_LOCK_SHARE, BufferGetPage, CHECK_FOR_INTERRUPTS, FreeAccessStrategy(), GetAccessStrategy(), BloomTuple::heapPtr, i, IndexScanDescData::indexRelation, IndexScanDescData::keyData, LockBuffer(), MAIN_FORKNUM, NULL, 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().

83 {
84  int64 ntids = 0;
86  npages;
87  int i;
90 
91  if (so->sign == NULL)
92  {
93  /* New search: have to calculate search signature */
94  ScanKey skey = scan->keyData;
95 
96  so->sign = palloc0(sizeof(BloomSignatureWord) * so->state.opts.bloomLength);
97 
98  for (i = 0; i < scan->numberOfKeys; i++)
99  {
100  /*
101  * Assume bloom-indexable operators to be strict, so nothing could
102  * be found for NULL key.
103  */
104  if (skey->sk_flags & SK_ISNULL)
105  {
106  pfree(so->sign);
107  so->sign = NULL;
108  return 0;
109  }
110 
111  /* Add next value to the signature */
112  signValue(&so->state, so->sign, skey->sk_argument,
113  skey->sk_attno - 1);
114 
115  skey++;
116  }
117  }
118 
119  /*
120  * We're going to read the whole index. This is why we use appropriate
121  * buffer access strategy.
122  */
125 
126  for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
127  {
128  Buffer buffer;
129  Page page;
130 
132  blkno, RBM_NORMAL, bas);
133 
134  LockBuffer(buffer, BUFFER_LOCK_SHARE);
135  page = BufferGetPage(buffer);
136  TestForOldSnapshot(scan->xs_snapshot, scan->indexRelation, page);
137 
138  if (!PageIsNew(page) && !BloomPageIsDeleted(page))
139  {
140  OffsetNumber offset,
141  maxOffset = BloomPageGetMaxOffset(page);
142 
143  for (offset = 1; offset <= maxOffset; offset++)
144  {
145  BloomTuple *itup = BloomPageGetTuple(&so->state, page, offset);
146  bool res = true;
147 
148  /* Check index signature with scan signature */
149  for (i = 0; i < so->state.opts.bloomLength; i++)
150  {
151  if ((itup->sign[i] & so->sign[i]) != so->sign[i])
152  {
153  res = false;
154  break;
155  }
156  }
157 
158  /* Add matching tuples to bitmap */
159  if (res)
160  {
161  tbm_add_tuples(tbm, &itup->heapPtr, 1, true);
162  ntids++;
163  }
164  }
165  }
166 
167  UnlockReleaseBuffer(buffer);
169  }
170  FreeAccessStrategy(bas);
171 
172  return ntids;
173 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:525
#define BloomPageIsDeleted(page)
Definition: bloom.h:63
static void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
Definition: bufmgr.h:265
BloomOptions opts
Definition: bloom.h:140
void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
Definition: blutils.c:251
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:403
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
Snapshot xs_snapshot
Definition: relscan.h:90
uint32 BlockNumber
Definition: block.h:31
#define BLOOM_HEAD_BLKNO
Definition: bloom.h:78
Relation indexRelation
Definition: relscan.h:89
uint16 OffsetNumber
Definition: off.h:24
uint16 BloomSignatureWord
Definition: bloom.h:83
void pfree(void *pointer)
Definition: mcxt.c:950
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define BloomPageGetTuple(state, page, offset)
Definition: bloom.h:70
#define SK_ISNULL
Definition: skey.h:115
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void * palloc0(Size size)
Definition: mcxt.c:878
BloomState state
Definition: bloom.h:170
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:580
int sk_flags
Definition: skey.h:66
#define NULL
Definition: c.h:229
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
ScanKey keyData
Definition: relscan.h:93
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]
Definition: bloom.h:161
int bloomLength
Definition: bloom.h:103
ItemPointerData heapPtr
Definition: bloom.h:160
#define PageIsNew(page)
Definition: bufpage.h:226
#define BloomPageGetMaxOffset(page)
Definition: bloom.h:60
int i
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:173
BloomSignatureWord * sign
Definition: bloom.h:169
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
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:74
void blrescan ( IndexScanDesc  scan,
ScanKey  scankey,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 49 of file blscan.c.

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

Referenced by blhandler().

51 {
53 
54  if (so->sign)
55  pfree(so->sign);
56  so->sign = NULL;
57 
58  if (scankey && scan->numberOfKeys > 0)
59  {
60  memmove(scan->keyData, scankey,
61  scan->numberOfKeys * sizeof(ScanKeyData));
62  }
63 }
void pfree(void *pointer)
Definition: mcxt.c:950
#define memmove(d, s, c)
Definition: c.h:1058
#define NULL
Definition: c.h:229
ScanKey keyData
Definition: relscan.h:93
BloomScanOpaqueData * BloomScanOpaque
Definition: bloom.h:173
BloomSignatureWord * sign
Definition: bloom.h:169