PostgreSQL Source Code  git master
blutils.c File Reference
#include "postgres.h"
#include "access/amapi.h"
#include "access/generic_xlog.h"
#include "access/reloptions.h"
#include "bloom.h"
#include "catalog/index.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
#include "utils/memutils.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, ForkNumber forknum)
 
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 31 of file blutils.c.

◆ GETBIT

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

Definition at line 33 of file blutils.c.

◆ GETWORD

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

Definition at line 30 of file blutils.c.

◆ SETBIT

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

Definition at line 32 of file blutils.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 52 of file blutils.c.

53 {
54  int i;
55  char buf[16];
56 
58 
59  /* Option for length of signature */
61  "Length of signature in bits",
64  bl_relopt_tab[0].optname = "length";
66  bl_relopt_tab[0].offset = offsetof(BloomOptions, bloomLength);
67 
68  /* Number of bits for each possible index column: col1, col2, ... */
69  for (i = 0; i < INDEX_MAX_KEYS; i++)
70  {
71  snprintf(buf, sizeof(buf), "col%d", i + 1);
73  "Number of bits generated for each index column",
77  buf);
79  bl_relopt_tab[i + 1].offset = offsetof(BloomOptions, bitSize[0]) + sizeof(int) * i;
80  }
81 }
#define DEFAULT_BLOOM_BITS
Definition: bloom.h:97
#define DEFAULT_BLOOM_LENGTH
Definition: bloom.h:91
#define MAX_BLOOM_BITS
Definition: bloom.h:98
#define MAX_BLOOM_LENGTH
Definition: bloom.h:92
static relopt_kind bl_relopt_kind
Definition: blutils.c:38
static relopt_parse_elt bl_relopt_tab[INDEX_MAX_KEYS+1]
Definition: blutils.c:41
int i
Definition: isn.c:73
#define AccessExclusiveLock
Definition: lockdefs.h:43
MemoryContext TopMemoryContext
Definition: mcxt.c:141
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1631
#define INDEX_MAX_KEYS
static char * buf
Definition: pg_test_fsync.c:73
#define snprintf
Definition: port.h:238
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:903
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:685
@ RELOPT_TYPE_INT
Definition: reloptions.h:32
const char * optname
Definition: reloptions.h:152
relopt_type opttype
Definition: reloptions.h:153

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

◆ blhandler()

Datum blhandler ( PG_FUNCTION_ARGS  )

Definition at line 106 of file blutils.c.

107 {
108  IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
109 
110  amroutine->amstrategies = BLOOM_NSTRATEGIES;
111  amroutine->amsupport = BLOOM_NPROC;
112  amroutine->amoptsprocnum = BLOOM_OPTIONS_PROC;
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->amusemaintenanceworkmem = false;
127  amroutine->amparallelvacuumoptions =
129  amroutine->amkeytype = InvalidOid;
130 
131  amroutine->ambuild = blbuild;
132  amroutine->ambuildempty = blbuildempty;
133  amroutine->aminsert = blinsert;
134  amroutine->aminsertcleanup = NULL;
135  amroutine->ambulkdelete = blbulkdelete;
136  amroutine->amvacuumcleanup = blvacuumcleanup;
137  amroutine->amcanreturn = NULL;
138  amroutine->amcostestimate = blcostestimate;
139  amroutine->amoptions = bloptions;
140  amroutine->amproperty = NULL;
141  amroutine->ambuildphasename = NULL;
142  amroutine->amvalidate = blvalidate;
143  amroutine->amadjustmembers = NULL;
144  amroutine->ambeginscan = blbeginscan;
145  amroutine->amrescan = blrescan;
146  amroutine->amgettuple = NULL;
147  amroutine->amgetbitmap = blgetbitmap;
148  amroutine->amendscan = blendscan;
149  amroutine->ammarkpos = NULL;
150  amroutine->amrestrpos = NULL;
151  amroutine->amestimateparallelscan = NULL;
152  amroutine->aminitparallelscan = NULL;
153  amroutine->amparallelrescan = NULL;
154 
155  PG_RETURN_POINTER(amroutine);
156 }
void blcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: blcost.c:23
void blbuildempty(Relation index)
Definition: blinsert.c:164
IndexBuildResult * blbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: blinsert.c:121
bool blinsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: blinsert.c:174
bool blvalidate(Oid opclassoid)
Definition: blvalidate.c:32
int64 blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: blscan.c:81
IndexScanDesc blbeginscan(Relation r, int nkeys, int norderbys)
Definition: blscan.c:28
#define BLOOM_NPROC
Definition: bloom.h:26
#define BLOOM_NSTRATEGIES
Definition: bloom.h:30
IndexBulkDeleteResult * blbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: blvacuum.c:34
#define BLOOM_OPTIONS_PROC
Definition: bloom.h:25
IndexBulkDeleteResult * blvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: blvacuum.c:169
void blendscan(IndexScanDesc scan)
Definition: blscan.c:68
void blrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: blscan.c:48
bytea * bloptions(Datum reloptions, bool validate)
Definition: blutils.c:476
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define makeNode(_type_)
Definition: nodes.h:176
#define InvalidOid
Definition: postgres_ext.h:36
ambuildphasename_function ambuildphasename
Definition: amapi.h:274
ambuildempty_function ambuildempty
Definition: amapi.h:265
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:269
bool amclusterable
Definition: amapi.h:241
amoptions_function amoptions
Definition: amapi.h:272
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:286
amrestrpos_function amrestrpos
Definition: amapi.h:283
aminsert_function aminsert
Definition: amapi.h:266
amendscan_function amendscan
Definition: amapi.h:281
uint16 amoptsprocnum
Definition: amapi.h:221
amparallelrescan_function amparallelrescan
Definition: amapi.h:288
Oid amkeytype
Definition: amapi.h:255
bool ampredlocks
Definition: amapi.h:243
uint16 amsupport
Definition: amapi.h:219
amcostestimate_function amcostestimate
Definition: amapi.h:271
bool amcanorderbyop
Definition: amapi.h:225
amadjustmembers_function amadjustmembers
Definition: amapi.h:276
ambuild_function ambuild
Definition: amapi.h:264
bool amstorage
Definition: amapi.h:239
uint16 amstrategies
Definition: amapi.h:217
bool amoptionalkey
Definition: amapi.h:233
amgettuple_function amgettuple
Definition: amapi.h:279
amcanreturn_function amcanreturn
Definition: amapi.h:270
bool amcanunique
Definition: amapi.h:229
amgetbitmap_function amgetbitmap
Definition: amapi.h:280
amproperty_function amproperty
Definition: amapi.h:273
ambulkdelete_function ambulkdelete
Definition: amapi.h:268
bool amsearcharray
Definition: amapi.h:235
amvalidate_function amvalidate
Definition: amapi.h:275
ammarkpos_function ammarkpos
Definition: amapi.h:282
bool amcanmulticol
Definition: amapi.h:231
bool amusemaintenanceworkmem
Definition: amapi.h:249
ambeginscan_function ambeginscan
Definition: amapi.h:277
bool amcanparallel
Definition: amapi.h:245
amrescan_function amrescan
Definition: amapi.h:278
bool amcanorder
Definition: amapi.h:223
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:287
uint8 amparallelvacuumoptions
Definition: amapi.h:253
aminsertcleanup_function aminsertcleanup
Definition: amapi.h:267
bool amcanbackward
Definition: amapi.h:227
bool amcaninclude
Definition: amapi.h:247
bool amsearchnulls
Definition: amapi.h:237
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:62
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:47

References IndexAmRoutine::amadjustmembers, 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::aminsertcleanup, IndexAmRoutine::amkeytype, IndexAmRoutine::ammarkpos, IndexAmRoutine::amoptionalkey, IndexAmRoutine::amoptions, IndexAmRoutine::amoptsprocnum, IndexAmRoutine::amparallelrescan, IndexAmRoutine::amparallelvacuumoptions, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amusemaintenanceworkmem, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, blbeginscan(), blbuild(), blbuildempty(), blbulkdelete(), blcostestimate(), blendscan(), blgetbitmap(), blinsert(), BLOOM_NPROC, BLOOM_NSTRATEGIES, BLOOM_OPTIONS_PROC, bloptions(), blrescan(), blvacuumcleanup(), blvalidate(), InvalidOid, makeNode, PG_RETURN_POINTER, VACUUM_OPTION_PARALLEL_BULKDEL, and VACUUM_OPTION_PARALLEL_CLEANUP.

◆ BloomFillMetapage()

void BloomFillMetapage ( Relation  index,
Page  metaPage 
)

Definition at line 415 of file blutils.c.

416 {
418  BloomMetaPageData *metadata;
419 
420  /*
421  * Choose the index's options. If reloptions have been assigned, use
422  * those, otherwise create default options.
423  */
424  opts = (BloomOptions *) index->rd_options;
425  if (!opts)
427 
428  /*
429  * Initialize contents of meta page, including a copy of the options,
430  * which are now frozen for the life of the index.
431  */
432  BloomInitPage(metaPage, BLOOM_META);
433  metadata = BloomPageGetMeta(metaPage);
434  memset(metadata, 0, sizeof(BloomMetaPageData));
435  metadata->magickNumber = BLOOM_MAGICK_NUMBER;
436  metadata->opts = *opts;
437  ((PageHeader) metaPage)->pd_lower += sizeof(BloomMetaPageData);
438 
439  /* If this fails, probably FreeBlockNumberArray size calc is wrong: */
440  Assert(((PageHeader) metaPage)->pd_lower <= ((PageHeader) metaPage)->pd_upper);
441 }
#define BloomPageGetMeta(page)
Definition: bloom.h:136
struct BloomMetaPageData BloomMetaPageData
#define BLOOM_MAGICK_NUMBER
Definition: bloom.h:131
#define BLOOM_META
Definition: bloom.h:46
void BloomInitPage(Page page, uint16 flags)
Definition: blutils.c:400
static BloomOptions * makeDefaultBloomOptions(void)
Definition: blutils.c:87
PageHeaderData * PageHeader
Definition: bufpage.h:170
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
static AmcheckOptions opts
Definition: pg_amcheck.c:111
BloomOptions opts
Definition: bloom.h:126
uint32 magickNumber
Definition: bloom.h:123
Definition: type.h:95

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

Referenced by BloomInitMetapage().

◆ BloomFormTuple()

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

Definition at line 294 of file blutils.c.

295 {
296  int i;
297  BloomTuple *res = (BloomTuple *) palloc0(state->sizeOfBloomTuple);
298 
299  res->heapPtr = *iptr;
300 
301  /* Blooming each column */
302  for (i = 0; i < state->nColumns; i++)
303  {
304  /* skip nulls */
305  if (isnull[i])
306  continue;
307 
308  signValue(state, res->sign, values[i], i);
309  }
310 
311  return res;
312 }
void signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int attno)
Definition: blutils.c:261
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void * palloc0(Size size)
Definition: mcxt.c:1257
Definition: regguts.h:323

References i, palloc0(), res, signValue(), and values.

Referenced by blinsert(), and bloomBuildCallback().

◆ BloomInitMetapage()

void BloomInitMetapage ( Relation  index,
ForkNumber  forknum 
)

Definition at line 447 of file blutils.c.

448 {
449  Buffer metaBuffer;
450  Page metaPage;
452 
453  /*
454  * Make a new page; since it is first page it should be associated with
455  * block number 0 (BLOOM_METAPAGE_BLKNO). No need to hold the extension
456  * lock because there cannot be concurrent inserters yet.
457  */
458  metaBuffer = ReadBufferExtended(index, forknum, P_NEW, RBM_NORMAL, NULL);
459  LockBuffer(metaBuffer, BUFFER_LOCK_EXCLUSIVE);
461 
462  /* Initialize contents of meta page */
464  metaPage = GenericXLogRegisterBuffer(state, metaBuffer,
466  BloomFillMetapage(index, metaPage);
468 
469  UnlockReleaseBuffer(metaBuffer);
470 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:78
void BloomFillMetapage(Relation index, Page metaPage)
Definition: blutils.c:415
int Buffer
Definition: buf.h:23
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3386
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4590
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4808
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:782
#define P_NEW
Definition: bufmgr.h:152
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:159
@ RBM_NORMAL
Definition: bufmgr.h:44
Pointer Page
Definition: bufpage.h:78
Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer, int flags)
Definition: generic_xlog.c:300
GenericXLogState * GenericXLogStart(Relation relation)
Definition: generic_xlog.c:270
XLogRecPtr GenericXLogFinish(GenericXLogState *state)
Definition: generic_xlog.c:338
#define GENERIC_XLOG_FULL_IMAGE
Definition: generic_xlog.h:26

References Assert(), BLOOM_METAPAGE_BLKNO, BloomFillMetapage(), BUFFER_LOCK_EXCLUSIVE, BufferGetBlockNumber(), GENERIC_XLOG_FULL_IMAGE, GenericXLogFinish(), GenericXLogRegisterBuffer(), GenericXLogStart(), LockBuffer(), P_NEW, RBM_NORMAL, ReadBufferExtended(), and UnlockReleaseBuffer().

Referenced by blbuild(), and blbuildempty().

◆ BloomInitPage()

void BloomInitPage ( Page  page,
uint16  flags 
)

Definition at line 400 of file blutils.c.

401 {
402  BloomPageOpaque opaque;
403 
404  PageInit(page, BLCKSZ, sizeof(BloomPageOpaqueData));
405 
406  opaque = BloomPageGetOpaque(page);
407  opaque->flags = flags;
408  opaque->bloom_page_id = BLOOM_PAGE_ID;
409 }
#define BloomPageGetOpaque(page)
Definition: bloom.h:60
#define BLOOM_PAGE_ID
Definition: bloom.h:57
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42
uint16 flags
Definition: bloom.h:36
uint16 bloom_page_id
Definition: bloom.h:40

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

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

◆ BloomNewBuffer()

Buffer BloomNewBuffer ( Relation  index)

Definition at line 354 of file blutils.c.

355 {
356  Buffer buffer;
357 
358  /* First, try to get a page from FSM */
359  for (;;)
360  {
362 
363  if (blkno == InvalidBlockNumber)
364  break;
365 
366  buffer = ReadBuffer(index, blkno);
367 
368  /*
369  * We have to guard against the possibility that someone else already
370  * recycled this page; the buffer may be locked if so.
371  */
372  if (ConditionalLockBuffer(buffer))
373  {
374  Page page = BufferGetPage(buffer);
375 
376  if (PageIsNew(page))
377  return buffer; /* OK to use, if never initialized */
378 
379  if (BloomPageIsDeleted(page))
380  return buffer; /* OK to use */
381 
383  }
384 
385  /* Can't use it, so release buffer and try again */
386  ReleaseBuffer(buffer);
387  }
388 
389  /* Must extend the file */
390  buffer = ExtendBufferedRel(BMR_REL(index), MAIN_FORKNUM, NULL,
391  EB_LOCK_FIRST);
392 
393  return buffer;
394 }
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#define BloomPageIsDeleted(page)
Definition: bloom.h:64
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition: bufmgr.c:839
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:4834
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4573
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:735
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:157
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:350
@ EB_LOCK_FIRST
Definition: bufmgr.h:85
#define BMR_REL(p_rel)
Definition: bufmgr.h:106
static bool PageIsNew(Page page)
Definition: bufpage.h:230
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
@ MAIN_FORKNUM
Definition: relpath.h:50

References BloomPageIsDeleted, BMR_REL, BUFFER_LOCK_UNLOCK, BufferGetPage(), ConditionalLockBuffer(), EB_LOCK_FIRST, ExtendBufferedRel(), GetFreeIndexPage(), InvalidBlockNumber, LockBuffer(), MAIN_FORKNUM, PageIsNew(), ReadBuffer(), and ReleaseBuffer().

Referenced by blinsert(), and flushCachedPage().

◆ BloomPageAddItem()

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

Definition at line 319 of file blutils.c.

320 {
321  BloomTuple *itup;
322  BloomPageOpaque opaque;
323  Pointer ptr;
324 
325  /* We shouldn't be pointed to an invalid page */
326  Assert(!PageIsNew(page) && !BloomPageIsDeleted(page));
327 
328  /* Does new tuple fit on the page? */
329  if (BloomPageGetFreeSpace(state, page) < state->sizeOfBloomTuple)
330  return false;
331 
332  /* Copy new tuple to the end of page */
333  opaque = BloomPageGetOpaque(page);
334  itup = BloomPageGetTuple(state, page, opaque->maxoff + 1);
335  memcpy((Pointer) itup, (Pointer) tuple, state->sizeOfBloomTuple);
336 
337  /* Adjust maxoff and pd_lower */
338  opaque->maxoff++;
339  ptr = (Pointer) BloomPageGetTuple(state, page, opaque->maxoff + 1);
340  ((PageHeader) page)->pd_lower = ptr - page;
341 
342  /* Assert we didn't overrun available space */
343  Assert(((PageHeader) page)->pd_lower <= ((PageHeader) page)->pd_upper);
344 
345  return true;
346 }
#define BloomPageGetFreeSpace(state, page)
Definition: bloom.h:152
#define BloomPageGetTuple(state, page, offset)
Definition: bloom.h:71
char * Pointer
Definition: c.h:472
OffsetNumber maxoff
Definition: bloom.h:35

References Assert(), BloomPageGetFreeSpace, BloomPageGetOpaque, BloomPageGetTuple, BloomPageIsDeleted, BloomPageOpaqueData::maxoff, and PageIsNew().

Referenced by blinsert(), and bloomBuildCallback().

◆ bloptions()

bytea* bloptions ( Datum  reloptions,
bool  validate 
)

Definition at line 476 of file blutils.c.

477 {
478  BloomOptions *rdopts;
479 
480  /* Parse the user-given reloptions */
481  rdopts = (BloomOptions *) build_reloptions(reloptions, validate,
483  sizeof(BloomOptions),
486 
487  /* Convert signature length from # of bits to # to words, rounding up */
488  if (rdopts)
489  rdopts->bloomLength = (rdopts->bloomLength + SIGNWORDBITS - 1) / SIGNWORDBITS;
490 
491  return (bytea *) rdopts;
492 }
#define SIGNWORDBITS
Definition: bloom.h:86
#define lengthof(array)
Definition: c.h:777
void * build_reloptions(Datum reloptions, bool validate, relopt_kind kind, Size relopt_struct_size, const relopt_parse_elt *relopt_elems, int num_relopt_elems)
Definition: reloptions.c:1910
int bloomLength
Definition: bloom.h:104
Definition: c.h:676

References bl_relopt_kind, bl_relopt_tab, BloomOptions::bloomLength, build_reloptions(), lengthof, and SIGNWORDBITS.

Referenced by blhandler().

◆ initBloomState()

void initBloomState ( BloomState state,
Relation  index 
)

Definition at line 162 of file blutils.c.

163 {
164  int i;
165 
166  state->nColumns = index->rd_att->natts;
167 
168  /* Initialize hash function for each attribute */
169  for (i = 0; i < index->rd_att->natts; i++)
170  {
171  fmgr_info_copy(&(state->hashFn[i]),
174  state->collations[i] = index->rd_indcollation[i];
175  }
176 
177  /* Initialize amcache if needed with options from metapage */
178  if (!index->rd_amcache)
179  {
180  Buffer buffer;
181  Page page;
182  BloomMetaPageData *meta;
184 
185  opts = MemoryContextAlloc(index->rd_indexcxt, sizeof(BloomOptions));
186 
188  LockBuffer(buffer, BUFFER_LOCK_SHARE);
189 
190  page = BufferGetPage(buffer);
191 
192  if (!BloomPageIsMeta(page))
193  elog(ERROR, "Relation is not a bloom index");
194  meta = BloomPageGetMeta(BufferGetPage(buffer));
195 
196  if (meta->magickNumber != BLOOM_MAGICK_NUMBER)
197  elog(ERROR, "Relation is not a bloom index");
198 
199  *opts = meta->opts;
200 
201  UnlockReleaseBuffer(buffer);
202 
203  index->rd_amcache = (void *) opts;
204  }
205 
206  memcpy(&state->opts, index->rd_amcache, sizeof(state->opts));
207  state->sizeOfBloomTuple = BLOOMTUPLEHDRSZ +
208  sizeof(BloomSignatureWord) * state->opts.bloomLength;
209 }
#define BLOOMTUPLEHDRSZ
Definition: bloom.h:166
#define BLOOM_HASH_PROC
Definition: bloom.h:24
uint16 BloomSignatureWord
Definition: bloom.h:84
#define BloomPageIsMeta(page)
Definition: bloom.h:62
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:158
#define ERROR
Definition: elog.h:39
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:580
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:826
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021

References BLOOM_HASH_PROC, BLOOM_MAGICK_NUMBER, BLOOM_METAPAGE_BLKNO, BloomPageGetMeta, BloomPageIsMeta, BLOOMTUPLEHDRSZ, BUFFER_LOCK_SHARE, BufferGetPage(), CurrentMemoryContext, elog(), ERROR, fmgr_info_copy(), i, index_getprocinfo(), LockBuffer(), BloomMetaPageData::magickNumber, MemoryContextAlloc(), BloomMetaPageData::opts, opts, ReadBuffer(), and UnlockReleaseBuffer().

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

◆ makeDefaultBloomOptions()

static BloomOptions* makeDefaultBloomOptions ( void  )
static

Definition at line 87 of file blutils.c.

88 {
90  int i;
91 
92  opts = (BloomOptions *) palloc0(sizeof(BloomOptions));
93  /* Convert DEFAULT_BLOOM_LENGTH from # of bits to # of words */
94  opts->bloomLength = (DEFAULT_BLOOM_LENGTH + SIGNWORDBITS - 1) / SIGNWORDBITS;
95  for (i = 0; i < INDEX_MAX_KEYS; i++)
96  opts->bitSize[i] = DEFAULT_BLOOM_BITS;
97  SET_VARSIZE(opts, sizeof(BloomOptions));
98  return opts;
99 }
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References DEFAULT_BLOOM_BITS, DEFAULT_BLOOM_LENGTH, i, INDEX_MAX_KEYS, opts, palloc0(), SET_VARSIZE, and SIGNWORDBITS.

Referenced by BloomFillMetapage().

◆ myRand()

static int32 myRand ( void  )
static

Definition at line 223 of file blutils.c.

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

References next, and x.

Referenced by signValue().

◆ mySrand()

static void mySrand ( uint32  seed)
static

Definition at line 250 of file blutils.c.

251 {
252  next = seed;
253  /* Transform to [1, 0x7ffffffe] range. */
254  next = (next % 0x7ffffffe) + 1;
255 }

References next.

Referenced by signValue().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( blhandler  )

◆ signValue()

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

Definition at line 261 of file blutils.c.

262 {
263  uint32 hashVal;
264  int nBit,
265  j;
266 
267  /*
268  * init generator with "column's" number to get "hashed" seed for new
269  * value. We don't want to map the same numbers from different columns
270  * into the same bits!
271  */
272  mySrand(attno);
273 
274  /*
275  * Init hash sequence to map our value into bits. the same values in
276  * different columns will be mapped into different bits because of step
277  * above
278  */
279  hashVal = DatumGetInt32(FunctionCall1Coll(&state->hashFn[attno], state->collations[attno], value));
280  mySrand(hashVal ^ myRand());
281 
282  for (j = 0; j < state->opts.bitSize[attno]; j++)
283  {
284  /* prevent multiple evaluation in SETBIT macro */
285  nBit = myRand() % (state->opts.bloomLength * SIGNWORDBITS);
286  SETBIT(sign, nBit);
287  }
288 }
static int32 myRand(void)
Definition: blutils.c:223
#define SETBIT(x, i)
Definition: blutils.c:32
static void mySrand(uint32 seed)
Definition: blutils.c:250
unsigned int uint32
Definition: c.h:495
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1112
static struct @148 value
char sign
Definition: informix.c:668
int j
Definition: isn.c:74
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202

References DatumGetInt32(), FunctionCall1Coll(), j, myRand(), mySrand(), SETBIT, sign, SIGNWORDBITS, and value.

Referenced by blgetbitmap(), and BloomFormTuple().

Variable Documentation

◆ bl_relopt_kind

relopt_kind bl_relopt_kind
static

Definition at line 38 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 41 of file blutils.c.

Referenced by _PG_init(), and bloptions().

◆ next

int32 next
static

Definition at line 220 of file blutils.c.

Referenced by addItemsToLeaf(), AllocSetDelete(), AllocSetReset(), appendBoolResult(), AtEOSubXact_SPI(), AtSubAbort_Snapshot(), bbsink_gzip_new(), bbsink_lz4_new(), bbsink_progress_new(), bbsink_server_new(), bbsink_throttle_new(), bbsink_zstd_new(), bbstreamer_gzip_decompressor_new(), bbstreamer_lz4_compressor_new(), bbstreamer_lz4_decompressor_new(), bbstreamer_recovery_injector_new(), bbstreamer_tar_archiver_new(), bbstreamer_tar_parser_new(), bbstreamer_tar_terminator_new(), bbstreamer_zstd_compressor_new(), bbstreamer_zstd_decompressor_new(), CallSubXactCallbacks(), CallXactCallbacks(), deallocate_one(), delete_item_from_bucket(), delete_key_from_bucket(), descriptor_deallocate_all(), do_setval(), doLog(), dsa_trim(), ecpg_register_prepared_stmt(), ecpg_sqlda_align_add_size(), ECPGconnect(), encrypt_init(), encrypt_process(), entry_purge_tuples(), ExecHashTableDetachBatch(), ExecParallelHashNextTuple(), executeKeyValueMethod(), executeNextItem(), executeNumericItemMethod(), extract_jsp_path_expr_nodes(), find_struct(), find_struct_member(), find_variable(), flattenJsonPathParseItem(), FreePageManagerGetInternal(), FreePagePopSpanLeader(), get_nextfield(), get_source_line(), get_typedef(), getNextFlagFromString(), GinDataLeafPageGetItems(), heap_lock_updated_tuple_rec(), infer_arbiter_indexes(), intset_iterate_next(), IssuePendingWritebacks(), LagTrackerRead(), lrq_alloc(), main(), make_new_segment(), merge_children(), myRand(), mySrand(), nextval_internal(), pg_event_trigger_dropped_objects(), pgstat_flush_pending_entries(), pkt_stream_flush(), pkt_stream_process(), plpgsql_destroy_econtext(), plpgsql_subxact_cb(), PostPrepare_smgr(), pqTraceOutputNchar(), prepare_common(), pushf_create(), ReadMultiXactIdRange(), rebin_segment(), RelationPreserveStorage(), ReorderBufferIterTXNNext(), replace_auto_config_value(), ResourceOwnerReleaseInternal(), setCompoundAffixFlagValue(), setval3_oid(), setval_oid(), simple_oid_list_destroy(), simple_string_list_destroy(), smgrDoPendingDeletes(), SPI_freetuptable(), StandbyReleaseXidEntryLocks(), unicode_normalize(), unlink_segment(), and unlink_span().