PostgreSQL Source Code  git master
blutils.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/generic_xlog.h"
#include "catalog/index.h"
#include "storage/lmgr.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/indexfsm.h"
#include "utils/memutils.h"
#include "access/reloptions.h"
#include "storage/freespace.h"
#include "bloom.h"
Include dependency graph for blutils.c:

Go to the source code of this file.

Macros

#define GETWORD(x, i)   ( *( (BloomSignatureWord *)(x) + ( (i) / SIGNWORDBITS ) ) )
 
#define CLRBIT(x, i)   GETWORD(x,i) &= ~( 0x01 << ( (i) % SIGNWORDBITS ) )
 
#define SETBIT(x, i)   GETWORD(x,i) |= ( 0x01 << ( (i) % SIGNWORDBITS ) )
 
#define GETBIT(x, i)   ( (GETWORD(x,i) >> ( (i) % SIGNWORDBITS )) & 0x01 )
 

Functions

 PG_FUNCTION_INFO_V1 (blhandler)
 
static int32 myRand (void)
 
static void mySrand (uint32 seed)
 
void _PG_init (void)
 
static BloomOptionsmakeDefaultBloomOptions (void)
 
Datum blhandler (PG_FUNCTION_ARGS)
 
void initBloomState (BloomState *state, Relation index)
 
void signValue (BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
 
BloomTupleBloomFormTuple (BloomState *state, ItemPointer iptr, Datum *values, bool *isnull)
 
bool BloomPageAddItem (BloomState *state, Page page, BloomTuple *tuple)
 
Buffer BloomNewBuffer (Relation index)
 
void BloomInitPage (Page page, uint16 flags)
 
void BloomFillMetapage (Relation index, Page metaPage)
 
void BloomInitMetapage (Relation index)
 
byteabloptions (Datum reloptions, bool validate)
 

Variables

static relopt_kind bl_relopt_kind
 
static relopt_parse_elt bl_relopt_tab [INDEX_MAX_KEYS+1]
 
static int32 next
 

Macro Definition Documentation

◆ CLRBIT

#define CLRBIT (   x,
  i 
)    GETWORD(x,i) &= ~( 0x01 << ( (i) % SIGNWORDBITS ) )

Definition at line 32 of file blutils.c.

Referenced by _hash_freeovflpage(), and hash_xlog_squeeze_page().

◆ GETBIT

#define GETBIT (   x,
  i 
)    ( (GETWORD(x,i) >> ( (i) % SIGNWORDBITS )) & 0x01 )

◆ GETWORD

#define GETWORD (   x,
  i 
)    ( *( (BloomSignatureWord *)(x) + ( (i) / SIGNWORDBITS ) ) )

Definition at line 31 of file blutils.c.

◆ SETBIT

#define SETBIT (   x,
  i 
)    GETWORD(x,i) |= ( 0x01 << ( (i) % SIGNWORDBITS ) )

Definition at line 33 of file blutils.c.

Referenced by _hash_addovflpage(), hash_xlog_add_ovfl_page(), makesign(), and signValue().

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 53 of file blutils.c.

References AccessExclusiveLock, add_int_reloption(), add_reloption_kind(), bl_relopt_kind, buf, DEFAULT_BLOOM_BITS, DEFAULT_BLOOM_LENGTH, i, INDEX_MAX_KEYS, MAX_BLOOM_BITS, MAX_BLOOM_LENGTH, MemoryContextStrdup(), relopt_parse_elt::offset, offsetof, relopt_parse_elt::optname, relopt_parse_elt::opttype, RELOPT_TYPE_INT, snprintf, and TopMemoryContext.

54 {
55  int i;
56  char buf[16];
57 
59 
60  /* Option for length of signature */
62  "Length of signature in bits",
65  bl_relopt_tab[0].optname = "length";
67  bl_relopt_tab[0].offset = offsetof(BloomOptions, bloomLength);
68 
69  /* Number of bits for each possible index column: col1, col2, ... */
70  for (i = 0; i < INDEX_MAX_KEYS; i++)
71  {
72  snprintf(buf, sizeof(buf), "col%d", i + 1);
74  "Number of bits generated for each index column",
78  buf);
80  bl_relopt_tab[i + 1].offset = offsetof(BloomOptions, bitSize[0]) + sizeof(int) * i;
81  }
82 }
relopt_type opttype
Definition: reloptions.h:147
#define MAX_BLOOM_LENGTH
Definition: bloom.h:91
#define DEFAULT_BLOOM_BITS
Definition: bloom.h:96
void add_int_reloption(bits32 kinds, const char *name, const char *desc, int default_val, int min_val, int max_val, LOCKMODE lockmode)
Definition: reloptions.c:730
#define DEFAULT_BLOOM_LENGTH
Definition: bloom.h:90
static relopt_kind bl_relopt_kind
Definition: blutils.c:39
static relopt_parse_elt bl_relopt_tab[INDEX_MAX_KEYS+1]
Definition: blutils.c:42
static char * buf
Definition: pg_test_fsync.c:68
MemoryContext TopMemoryContext
Definition: mcxt.c:44
const char * optname
Definition: reloptions.h:146
#define INDEX_MAX_KEYS
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:609
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define MAX_BLOOM_BITS
Definition: bloom.h:97
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
int i
#define snprintf
Definition: port.h:192
#define offsetof(type, field)
Definition: c.h:655

◆ blhandler()

Datum blhandler ( PG_FUNCTION_ARGS  )

Definition at line 107 of file blutils.c.

References IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambuildphasename, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcanbackward, IndexAmRoutine::amcaninclude, IndexAmRoutine::amcanmulticol, IndexAmRoutine::amcanorder, IndexAmRoutine::amcanorderbyop, IndexAmRoutine::amcanparallel, IndexAmRoutine::amcanreturn, IndexAmRoutine::amcanunique, IndexAmRoutine::amclusterable, IndexAmRoutine::amcostestimate, IndexAmRoutine::amendscan, IndexAmRoutine::amestimateparallelscan, IndexAmRoutine::amgetbitmap, IndexAmRoutine::amgettuple, IndexAmRoutine::aminitparallelscan, IndexAmRoutine::aminsert, IndexAmRoutine::amkeytype, IndexAmRoutine::ammarkpos, IndexAmRoutine::amoptionalkey, IndexAmRoutine::amoptions, IndexAmRoutine::amparallelrescan, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, blbeginscan(), blbuild(), blbuildempty(), blbulkdelete(), blcostestimate(), blendscan(), blgetbitmap(), blinsert(), BLOOM_NPROC, BLOOM_NSTRATEGIES, bloptions(), blrescan(), blvacuumcleanup(), blvalidate(), InvalidOid, makeNode, and PG_RETURN_POINTER.

108 {
109  IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
110 
111  amroutine->amstrategies = BLOOM_NSTRATEGIES;
112  amroutine->amsupport = BLOOM_NPROC;
113  amroutine->amcanorder = false;
114  amroutine->amcanorderbyop = false;
115  amroutine->amcanbackward = false;
116  amroutine->amcanunique = false;
117  amroutine->amcanmulticol = true;
118  amroutine->amoptionalkey = true;
119  amroutine->amsearcharray = false;
120  amroutine->amsearchnulls = false;
121  amroutine->amstorage = false;
122  amroutine->amclusterable = false;
123  amroutine->ampredlocks = false;
124  amroutine->amcanparallel = false;
125  amroutine->amcaninclude = false;
126  amroutine->amkeytype = InvalidOid;
127 
128  amroutine->ambuild = blbuild;
129  amroutine->ambuildempty = blbuildempty;
130  amroutine->aminsert = blinsert;
131  amroutine->ambulkdelete = blbulkdelete;
132  amroutine->amvacuumcleanup = blvacuumcleanup;
133  amroutine->amcanreturn = NULL;
134  amroutine->amcostestimate = blcostestimate;
135  amroutine->amoptions = bloptions;
136  amroutine->amproperty = NULL;
137  amroutine->ambuildphasename = NULL;
138  amroutine->amvalidate = blvalidate;
139  amroutine->ambeginscan = blbeginscan;
140  amroutine->amrescan = blrescan;
141  amroutine->amgettuple = NULL;
142  amroutine->amgetbitmap = blgetbitmap;
143  amroutine->amendscan = blendscan;
144  amroutine->ammarkpos = NULL;
145  amroutine->amrestrpos = NULL;
146  amroutine->amestimateparallelscan = NULL;
147  amroutine->aminitparallelscan = NULL;
148  amroutine->amparallelrescan = NULL;
149 
150  PG_RETURN_POINTER(amroutine);
151 }
ambeginscan_function ambeginscan
Definition: amapi.h:221
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
ambulkdelete_function ambulkdelete
Definition: amapi.h:213
bool amcanmulticol
Definition: amapi.h:183
uint16 amsupport
Definition: amapi.h:173
IndexBuildResult * blbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: blinsert.c:123
amgettuple_function amgettuple
Definition: amapi.h:223
bool amcanorderbyop
Definition: amapi.h:177
amproperty_function amproperty
Definition: amapi.h:218
amparallelrescan_function amparallelrescan
Definition: amapi.h:232
#define BLOOM_NPROC
Definition: bloom.h:25
bool amstorage
Definition: amapi.h:191
bool ampredlocks
Definition: amapi.h:195
aminsert_function aminsert
Definition: amapi.h:212
Oid amkeytype
Definition: amapi.h:201
void blcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: blcost.c:24
bool amoptionalkey
Definition: amapi.h:185
amvalidate_function amvalidate
Definition: amapi.h:220
IndexBulkDeleteResult * blvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: blvacuum.c:169
bool blvalidate(Oid opclassoid)
Definition: blvalidate.c:33
void blendscan(IndexScanDesc scan)
Definition: blscan.c:69
amgetbitmap_function amgetbitmap
Definition: amapi.h:224
ambuild_function ambuild
Definition: amapi.h:210
amoptions_function amoptions
Definition: amapi.h:217
bool amcaninclude
Definition: amapi.h:199
amcostestimate_function amcostestimate
Definition: amapi.h:216
bool amcanunique
Definition: amapi.h:181
IndexBulkDeleteResult * blbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: blvacuum.c:34
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:214
amendscan_function amendscan
Definition: amapi.h:225
bool amcanbackward
Definition: amapi.h:179
int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: blscan.c:82
amrescan_function amrescan
Definition: amapi.h:222
bool amcanparallel
Definition: amapi.h:197
#define BLOOM_NSTRATEGIES
Definition: bloom.h:29
bool amsearchnulls
Definition: amapi.h:189
void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: blscan.c:49
bool amclusterable
Definition: amapi.h:193
bool amsearcharray
Definition: amapi.h:187
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:573
bool blinsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: blinsert.c:199
ammarkpos_function ammarkpos
Definition: amapi.h:226
bool amcanorder
Definition: amapi.h:175
ambuildphasename_function ambuildphasename
Definition: amapi.h:219
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:230
void blbuildempty(Relation index)
Definition: blinsert.c:166
uint16 amstrategies
Definition: amapi.h:171
IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys)
Definition: blscan.c:29
ambuildempty_function ambuildempty
Definition: amapi.h:211
bytea * bloptions(Datum reloptions, bool validate)
Definition: blutils.c:478
amcanreturn_function amcanreturn
Definition: amapi.h:215
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:231
amrestrpos_function amrestrpos
Definition: amapi.h:227

◆ BloomFillMetapage()

void BloomFillMetapage ( Relation  index,
Page  metaPage 
)

Definition at line 419 of file blutils.c.

References Assert, BLOOM_MAGICK_NUMBER, BLOOM_META, BloomInitPage(), BloomPageGetMeta, BloomMetaPageData::magickNumber, makeDefaultBloomOptions(), BloomMetaPageData::opts, and RelationData::rd_options.

Referenced by blbuildempty(), and BloomInitMetapage().

420 {
421  BloomOptions *opts;
422  BloomMetaPageData *metadata;
423 
424  /*
425  * Choose the index's options. If reloptions have been assigned, use
426  * those, otherwise create default options.
427  */
428  opts = (BloomOptions *) index->rd_options;
429  if (!opts)
430  opts = makeDefaultBloomOptions();
431 
432  /*
433  * Initialize contents of meta page, including a copy of the options,
434  * which are now frozen for the life of the index.
435  */
436  BloomInitPage(metaPage, BLOOM_META);
437  metadata = BloomPageGetMeta(metaPage);
438  memset(metadata, 0, sizeof(BloomMetaPageData));
439  metadata->magickNumber = BLOOM_MAGICK_NUMBER;
440  metadata->opts = *opts;
441  ((PageHeader) metaPage)->pd_lower += sizeof(BloomMetaPageData);
442 
443  /* If this fails, probably FreeBlockNumberArray size calc is wrong: */
444  Assert(((PageHeader) metaPage)->pd_lower <= ((PageHeader) metaPage)->pd_upper);
445 }
static BloomOptions * makeDefaultBloomOptions(void)
Definition: blutils.c:88
#define BloomPageGetMeta(page)
Definition: bloom.h:135
BloomOptions opts
Definition: bloom.h:125
PageHeaderData * PageHeader
Definition: bufpage.h:166
#define Assert(condition)
Definition: c.h:732
uint32 magickNumber
Definition: bloom.h:122
void BloomInitPage(Page page, uint16 flags)
Definition: blutils.c:403
#define BLOOM_META
Definition: bloom.h:45
struct BloomMetaPageData BloomMetaPageData
#define BLOOM_MAGICK_NUMBER
Definition: bloom.h:130
bytea * rd_options
Definition: rel.h:126

◆ BloomFormTuple()

BloomTuple* BloomFormTuple ( BloomState state,
ItemPointer  iptr,
Datum values,
bool isnull 
)

Definition at line 289 of file blutils.c.

References BloomTuple::heapPtr, i, BloomState::nColumns, palloc0(), BloomTuple::sign, signValue(), and BloomState::sizeOfBloomTuple.

Referenced by blinsert(), and bloomBuildCallback().

290 {
291  int i;
292  BloomTuple *res = (BloomTuple *) palloc0(state->sizeOfBloomTuple);
293 
294  res->heapPtr = *iptr;
295 
296  /* Blooming each column */
297  for (i = 0; i < state->nColumns; i++)
298  {
299  /* skip nulls */
300  if (isnull[i])
301  continue;
302 
303  signValue(state, res->sign, values[i], i);
304  }
305 
306  return res;
307 }
void * palloc0(Size size)
Definition: mcxt.c:980
void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
Definition: blutils.c:256
int32 nColumns
Definition: bloom.h:142
BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]
Definition: bloom.h:162
static Datum values[MAXATTR]
Definition: bootstrap.c:167
ItemPointerData heapPtr
Definition: bloom.h:161
int i
Size sizeOfBloomTuple
Definition: bloom.h:148

◆ BloomInitMetapage()

void BloomInitMetapage ( Relation  index)

Definition at line 451 of file blutils.c.

References Assert, BLOOM_METAPAGE_BLKNO, BloomFillMetapage(), BloomNewBuffer(), BufferGetBlockNumber(), GENERIC_XLOG_FULL_IMAGE, GenericXLogFinish(), GenericXLogRegisterBuffer(), GenericXLogStart(), and UnlockReleaseBuffer().

Referenced by blbuild().

452 {
453  Buffer metaBuffer;
454  Page metaPage;
456 
457  /*
458  * Make a new page; since it is first page it should be associated with
459  * block number 0 (BLOOM_METAPAGE_BLKNO).
460  */
461  metaBuffer = BloomNewBuffer(index);
463 
464  /* Initialize contents of meta page */
465  state = GenericXLogStart(index);
466  metaPage = GenericXLogRegisterBuffer(state, metaBuffer,
468  BloomFillMetapage(index, metaPage);
469  GenericXLogFinish(state);
470 
471  UnlockReleaseBuffer(metaBuffer);
472 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:77
Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer, int flags)
Definition: generic_xlog.c:295
Buffer BloomNewBuffer(Relation index)
Definition: blutils.c:349
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
void BloomFillMetapage(Relation index, Page metaPage)
Definition: blutils.c:419
#define Assert(condition)
Definition: c.h:732
Definition: regguts.h:298
XLogRecPtr GenericXLogFinish(GenericXLogState *state)
Definition: generic_xlog.c:333
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
int Buffer
Definition: buf.h:23
#define GENERIC_XLOG_FULL_IMAGE
Definition: generic_xlog.h:26
GenericXLogState * GenericXLogStart(Relation relation)
Definition: generic_xlog.c:267
Pointer Page
Definition: bufpage.h:78

◆ BloomInitPage()

void BloomInitPage ( Page  page,
uint16  flags 
)

Definition at line 403 of file blutils.c.

References BloomPageOpaqueData::bloom_page_id, BLOOM_PAGE_ID, BloomPageGetOpaque, BloomPageOpaqueData::flags, and PageInit().

Referenced by blinsert(), BloomFillMetapage(), and initCachedPage().

404 {
405  BloomPageOpaque opaque;
406 
407  PageInit(page, BLCKSZ, sizeof(BloomPageOpaqueData));
408 
409  opaque = BloomPageGetOpaque(page);
410  memset(opaque, 0, sizeof(BloomPageOpaqueData));
411  opaque->flags = flags;
412  opaque->bloom_page_id = BLOOM_PAGE_ID;
413 }
#define BLOOM_PAGE_ID
Definition: bloom.h:56
uint16 bloom_page_id
Definition: bloom.h:39
uint16 flags
Definition: bloom.h:35
#define BloomPageGetOpaque(page)
Definition: bloom.h:59
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42

◆ BloomNewBuffer()

Buffer BloomNewBuffer ( Relation  index)

Definition at line 349 of file blutils.c.

References BloomPageIsDeleted, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage, ConditionalLockBuffer(), ExclusiveLock, GetFreeIndexPage(), InvalidBlockNumber, LockBuffer(), LockRelationForExtension(), P_NEW, PageIsNew, ReadBuffer(), RELATION_IS_LOCAL, ReleaseBuffer(), and UnlockRelationForExtension().

Referenced by blinsert(), BloomInitMetapage(), and flushCachedPage().

350 {
351  Buffer buffer;
352  bool needLock;
353 
354  /* First, try to get a page from FSM */
355  for (;;)
356  {
357  BlockNumber blkno = GetFreeIndexPage(index);
358 
359  if (blkno == InvalidBlockNumber)
360  break;
361 
362  buffer = ReadBuffer(index, blkno);
363 
364  /*
365  * We have to guard against the possibility that someone else already
366  * recycled this page; the buffer may be locked if so.
367  */
368  if (ConditionalLockBuffer(buffer))
369  {
370  Page page = BufferGetPage(buffer);
371 
372  if (PageIsNew(page))
373  return buffer; /* OK to use, if never initialized */
374 
375  if (BloomPageIsDeleted(page))
376  return buffer; /* OK to use */
377 
379  }
380 
381  /* Can't use it, so release buffer and try again */
382  ReleaseBuffer(buffer);
383  }
384 
385  /* Must extend the file */
386  needLock = !RELATION_IS_LOCAL(index);
387  if (needLock)
389 
390  buffer = ReadBuffer(index, P_NEW);
392 
393  if (needLock)
395 
396  return buffer;
397 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define BloomPageIsDeleted(page)
Definition: bloom.h:63
#define ExclusiveLock
Definition: lockdefs.h:44
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:539
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
#define P_NEW
Definition: bufmgr.h:81
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:88
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:3628
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:402
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:452
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
#define InvalidBlockNumber
Definition: block.h:33
#define PageIsNew(page)
Definition: bufpage.h:229
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:78

◆ BloomPageAddItem()

bool BloomPageAddItem ( BloomState state,
Page  page,
BloomTuple tuple 
)

Definition at line 314 of file blutils.c.

References Assert, BloomPageGetFreeSpace, BloomPageGetOpaque, BloomPageGetTuple, BloomPageIsDeleted, BloomPageOpaqueData::maxoff, PageIsNew, and BloomState::sizeOfBloomTuple.

Referenced by blinsert(), and bloomBuildCallback().

315 {
316  BloomTuple *itup;
317  BloomPageOpaque opaque;
318  Pointer ptr;
319 
320  /* We shouldn't be pointed to an invalid page */
321  Assert(!PageIsNew(page) && !BloomPageIsDeleted(page));
322 
323  /* Does new tuple fit on the page? */
324  if (BloomPageGetFreeSpace(state, page) < state->sizeOfBloomTuple)
325  return false;
326 
327  /* Copy new tuple to the end of page */
328  opaque = BloomPageGetOpaque(page);
329  itup = BloomPageGetTuple(state, page, opaque->maxoff + 1);
330  memcpy((Pointer) itup, (Pointer) tuple, state->sizeOfBloomTuple);
331 
332  /* Adjust maxoff and pd_lower */
333  opaque->maxoff++;
334  ptr = (Pointer) BloomPageGetTuple(state, page, opaque->maxoff + 1);
335  ((PageHeader) page)->pd_lower = ptr - page;
336 
337  /* Assert we didn't overrun available space */
338  Assert(((PageHeader) page)->pd_lower <= ((PageHeader) page)->pd_upper);
339 
340  return true;
341 }
#define BloomPageIsDeleted(page)
Definition: bloom.h:63
OffsetNumber maxoff
Definition: bloom.h:34
char * Pointer
Definition: c.h:335
#define BloomPageGetOpaque(page)
Definition: bloom.h:59
#define BloomPageGetFreeSpace(state, page)
Definition: bloom.h:151
#define BloomPageGetTuple(state, page, offset)
Definition: bloom.h:70
PageHeaderData * PageHeader
Definition: bufpage.h:166
#define Assert(condition)
Definition: c.h:732
#define PageIsNew(page)
Definition: bufpage.h:229
Size sizeOfBloomTuple
Definition: bloom.h:148

◆ bloptions()

bytea* bloptions ( Datum  reloptions,
bool  validate 
)

Definition at line 478 of file blutils.c.

References allocateReloptStruct(), bl_relopt_kind, fillRelOptions(), lengthof, options, parseRelOptions(), and SIGNWORDBITS.

Referenced by blhandler().

479 {
481  int numoptions;
482  BloomOptions *rdopts;
483 
484  /* Parse the user-given reloptions */
485  options = parseRelOptions(reloptions, validate, bl_relopt_kind, &numoptions);
486  rdopts = allocateReloptStruct(sizeof(BloomOptions), options, numoptions);
487  fillRelOptions((void *) rdopts, sizeof(BloomOptions), options, numoptions,
488  validate, bl_relopt_tab, lengthof(bl_relopt_tab));
489 
490  /* Convert signature length from # of bits to # to words, rounding up */
491  rdopts->bloomLength = (rdopts->bloomLength + SIGNWORDBITS - 1) / SIGNWORDBITS;
492 
493  return (bytea *) rdopts;
494 }
#define lengthof(array)
Definition: c.h:662
static relopt_kind bl_relopt_kind
Definition: blutils.c:39
static relopt_parse_elt bl_relopt_tab[INDEX_MAX_KEYS+1]
Definition: blutils.c:42
#define SIGNWORDBITS
Definition: bloom.h:85
void * allocateReloptStruct(Size base, relopt_value *options, int numoptions)
Definition: reloptions.c:1369
static char ** options
void fillRelOptions(void *rdopts, Size basesize, relopt_value *options, int numoptions, bool validate, const relopt_parse_elt *elems, int numelems)
Definition: reloptions.c:1393
Definition: c.h:549
relopt_value * parseRelOptions(Datum options, bool validate, relopt_kind kind, int *numrelopts)
Definition: reloptions.c:1144

◆ initBloomState()

void initBloomState ( BloomState state,
Relation  index 
)

Definition at line 157 of file blutils.c.

References BLOOM_HASH_PROC, BLOOM_MAGICK_NUMBER, BLOOM_METAPAGE_BLKNO, BloomOptions::bloomLength, BloomPageGetMeta, BloomPageIsMeta, BLOOMTUPLEHDRSZ, BUFFER_LOCK_SHARE, BufferGetPage, BloomState::collations, CurrentMemoryContext, elog, ERROR, fmgr_info_copy(), BloomState::hashFn, i, index_getprocinfo(), LockBuffer(), BloomMetaPageData::magickNumber, MemoryContextAlloc(), TupleDescData::natts, BloomState::nColumns, BloomMetaPageData::opts, BloomState::opts, RelationData::rd_amcache, RelationData::rd_att, RelationData::rd_indcollation, RelationData::rd_indexcxt, ReadBuffer(), BloomState::sizeOfBloomTuple, and UnlockReleaseBuffer().

Referenced by blbeginscan(), blbuild(), blbulkdelete(), and blinsert().

158 {
159  int i;
160 
161  state->nColumns = index->rd_att->natts;
162 
163  /* Initialize hash function for each attribute */
164  for (i = 0; i < index->rd_att->natts; i++)
165  {
166  fmgr_info_copy(&(state->hashFn[i]),
167  index_getprocinfo(index, i + 1, BLOOM_HASH_PROC),
169  state->collations[i] = index->rd_indcollation[i];
170  }
171 
172  /* Initialize amcache if needed with options from metapage */
173  if (!index->rd_amcache)
174  {
175  Buffer buffer;
176  Page page;
177  BloomMetaPageData *meta;
178  BloomOptions *opts;
179 
180  opts = MemoryContextAlloc(index->rd_indexcxt, sizeof(BloomOptions));
181 
182  buffer = ReadBuffer(index, BLOOM_METAPAGE_BLKNO);
183  LockBuffer(buffer, BUFFER_LOCK_SHARE);
184 
185  page = BufferGetPage(buffer);
186 
187  if (!BloomPageIsMeta(page))
188  elog(ERROR, "Relation is not a bloom index");
189  meta = BloomPageGetMeta(BufferGetPage(buffer));
190 
191  if (meta->magickNumber != BLOOM_MAGICK_NUMBER)
192  elog(ERROR, "Relation is not a bloom index");
193 
194  *opts = meta->opts;
195 
196  UnlockReleaseBuffer(buffer);
197 
198  index->rd_amcache = (void *) opts;
199  }
200 
201  memcpy(&state->opts, index->rd_amcache, sizeof(state->opts));
203  sizeof(BloomSignatureWord) * state->opts.bloomLength;
204 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:77
BloomOptions opts
Definition: bloom.h:141
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:794
#define BloomPageIsMeta(page)
Definition: bloom.h:61
FmgrInfo hashFn[INDEX_MAX_KEYS]
Definition: bloom.h:139
Oid collations[INDEX_MAX_KEYS]
Definition: bloom.h:140
uint16 BloomSignatureWord
Definition: bloom.h:83
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
Oid * rd_indcollation
Definition: rel.h:168
#define ERROR
Definition: elog.h:43
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:610
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define BloomPageGetMeta(page)
Definition: bloom.h:135
#define BLOOMTUPLEHDRSZ
Definition: bloom.h:165
BloomOptions opts
Definition: bloom.h:125
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
int32 nColumns
Definition: bloom.h:142
TupleDesc rd_att
Definition: rel.h:84
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:596
int bloomLength
Definition: bloom.h:103
uint32 magickNumber
Definition: bloom.h:122
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
#define elog(elevel,...)
Definition: elog.h:226
MemoryContext rd_indexcxt
Definition: rel.h:155
int i
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
void * rd_amcache
Definition: rel.h:179
#define BLOOM_MAGICK_NUMBER
Definition: bloom.h:130
Size sizeOfBloomTuple
Definition: bloom.h:148
int Buffer
Definition: buf.h:23
#define BLOOM_HASH_PROC
Definition: bloom.h:24
Pointer Page
Definition: bufpage.h:78

◆ makeDefaultBloomOptions()

static BloomOptions* makeDefaultBloomOptions ( void  )
static

Definition at line 88 of file blutils.c.

References BloomOptions::bitSize, BloomOptions::bloomLength, DEFAULT_BLOOM_BITS, DEFAULT_BLOOM_LENGTH, i, INDEX_MAX_KEYS, palloc0(), SET_VARSIZE, and SIGNWORDBITS.

Referenced by BloomFillMetapage().

89 {
90  BloomOptions *opts;
91  int i;
92 
93  opts = (BloomOptions *) palloc0(sizeof(BloomOptions));
94  /* Convert DEFAULT_BLOOM_LENGTH from # of bits to # of words */
96  for (i = 0; i < INDEX_MAX_KEYS; i++)
97  opts->bitSize[i] = DEFAULT_BLOOM_BITS;
98  SET_VARSIZE(opts, sizeof(BloomOptions));
99  return opts;
100 }
#define DEFAULT_BLOOM_BITS
Definition: bloom.h:96
int bitSize[INDEX_MAX_KEYS]
Definition: bloom.h:104
#define DEFAULT_BLOOM_LENGTH
Definition: bloom.h:90
#define SIGNWORDBITS
Definition: bloom.h:85
void * palloc0(Size size)
Definition: mcxt.c:980
#define INDEX_MAX_KEYS
int bloomLength
Definition: bloom.h:103
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ myRand()

static int32 myRand ( void  )
static

Definition at line 218 of file blutils.c.

References next.

Referenced by signValue().

219 {
220  /*----------
221  * Compute x = (7^5 * x) mod (2^31 - 1)
222  * without overflowing 31 bits:
223  * (2^31 - 1) = 127773 * (7^5) + 2836
224  * From "Random number generators: good ones are hard to find",
225  * Park and Miller, Communications of the ACM, vol. 31, no. 10,
226  * October 1988, p. 1195.
227  *----------
228  */
229  int32 hi,
230  lo,
231  x;
232 
233  /* Must be in [1, 0x7ffffffe] range at this point. */
234  hi = next / 127773;
235  lo = next % 127773;
236  x = 16807 * lo - 2836 * hi;
237  if (x < 0)
238  x += 0x7fffffff;
239  next = x;
240  /* Transform to [0, 0x7ffffffd] range. */
241  return (x - 1);
242 }
static int32 next
Definition: blutils.c:215
signed int int32
Definition: c.h:346

◆ mySrand()

static void mySrand ( uint32  seed)
static

Definition at line 245 of file blutils.c.

References next.

Referenced by signValue().

246 {
247  next = seed;
248  /* Transform to [1, 0x7ffffffe] range. */
249  next = (next % 0x7ffffffe) + 1;
250 }
static int32 next
Definition: blutils.c:215

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( blhandler  )

◆ signValue()

void signValue ( BloomState state,
BloomSignatureWord sign,
Datum  value,
int  attno 
)

Definition at line 256 of file blutils.c.

References BloomOptions::bitSize, BloomOptions::bloomLength, BloomState::collations, DatumGetInt32, FunctionCall1Coll(), BloomState::hashFn, myRand(), mySrand(), BloomState::opts, SETBIT, and SIGNWORDBITS.

Referenced by blgetbitmap(), and BloomFormTuple().

257 {
258  uint32 hashVal;
259  int nBit,
260  j;
261 
262  /*
263  * init generator with "column's" number to get "hashed" seed for new
264  * value. We don't want to map the same numbers from different columns
265  * into the same bits!
266  */
267  mySrand(attno);
268 
269  /*
270  * Init hash sequence to map our value into bits. the same values in
271  * different columns will be mapped into different bits because of step
272  * above
273  */
274  hashVal = DatumGetInt32(FunctionCall1Coll(&state->hashFn[attno], state->collations[attno], value));
275  mySrand(hashVal ^ myRand());
276 
277  for (j = 0; j < state->opts.bitSize[attno]; j++)
278  {
279  /* prevent multiple evaluation in SETBIT macro */
280  nBit = myRand() % (state->opts.bloomLength * SIGNWORDBITS);
281  SETBIT(sign, nBit);
282  }
283 }
#define SETBIT(x, i)
Definition: blutils.c:33
BloomOptions opts
Definition: bloom.h:141
#define DatumGetInt32(X)
Definition: postgres.h:472
FmgrInfo hashFn[INDEX_MAX_KEYS]
Definition: bloom.h:139
static struct @145 value
int bitSize[INDEX_MAX_KEYS]
Definition: bloom.h:104
Oid collations[INDEX_MAX_KEYS]
Definition: bloom.h:140
#define SIGNWORDBITS
Definition: bloom.h:85
char sign
Definition: informix.c:688
unsigned int uint32
Definition: c.h:358
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1130
static int32 myRand(void)
Definition: blutils.c:218
static void mySrand(uint32 seed)
Definition: blutils.c:245
int bloomLength
Definition: bloom.h:103

Variable Documentation

◆ bl_relopt_kind

relopt_kind bl_relopt_kind
static

Definition at line 39 of file blutils.c.

Referenced by _PG_init(), and bloptions().

◆ bl_relopt_tab

relopt_parse_elt bl_relopt_tab[INDEX_MAX_KEYS+1]
static

Definition at line 42 of file blutils.c.

◆ next