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)
 
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.

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 30 of file blutils.c.

◆ SETBIT

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

Definition at line 32 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 52 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.

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 }
relopt_type opttype
Definition: reloptions.h:153
#define MAX_BLOOM_LENGTH
Definition: bloom.h:92
#define DEFAULT_BLOOM_BITS
Definition: bloom.h:97
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:877
#define DEFAULT_BLOOM_LENGTH
Definition: bloom.h:91
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
static char * buf
Definition: pg_test_fsync.c:67
MemoryContext TopMemoryContext
Definition: mcxt.c:44
const char * optname
Definition: reloptions.h:152
#define INDEX_MAX_KEYS
relopt_kind add_reloption_kind(void)
Definition: reloptions.c:659
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define MAX_BLOOM_BITS
Definition: bloom.h:98
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
int i
#define snprintf
Definition: port.h:193
#define offsetof(type, field)
Definition: c.h:668

◆ blhandler()

Datum blhandler ( PG_FUNCTION_ARGS  )

Definition at line 106 of file blutils.c.

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::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.

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

◆ BloomFillMetapage()

void BloomFillMetapage ( Relation  index,
Page  metaPage 
)

Definition at line 423 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().

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

◆ BloomFormTuple()

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

Definition at line 293 of file blutils.c.

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

Referenced by blinsert(), and bloomBuildCallback().

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

◆ BloomInitMetapage()

void BloomInitMetapage ( Relation  index)

Definition at line 455 of file blutils.c.

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

Referenced by blbuild().

456 {
457  Buffer metaBuffer;
458  Page metaPage;
460 
461  /*
462  * Make a new page; since it is first page it should be associated with
463  * block number 0 (BLOOM_METAPAGE_BLKNO).
464  */
465  metaBuffer = BloomNewBuffer(index);
467 
468  /* Initialize contents of meta page */
469  state = GenericXLogStart(index);
470  metaPage = GenericXLogRegisterBuffer(state, metaBuffer,
472  BloomFillMetapage(index, metaPage);
473  GenericXLogFinish(state);
474 
475  UnlockReleaseBuffer(metaBuffer);
476 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:78
Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer, int flags)
Definition: generic_xlog.c:295
Buffer BloomNewBuffer(Relation index)
Definition: blutils.c:353
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
void BloomFillMetapage(Relation index, Page metaPage)
Definition: blutils.c:423
#define Assert(condition)
Definition: c.h:745
Definition: regguts.h:298
XLogRecPtr GenericXLogFinish(GenericXLogState *state)
Definition: generic_xlog.c:333
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2668
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 407 of file blutils.c.

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

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

408 {
409  BloomPageOpaque opaque;
410 
411  PageInit(page, BLCKSZ, sizeof(BloomPageOpaqueData));
412 
413  opaque = BloomPageGetOpaque(page);
414  memset(opaque, 0, sizeof(BloomPageOpaqueData));
415  opaque->flags = flags;
416  opaque->bloom_page_id = BLOOM_PAGE_ID;
417 }
#define BLOOM_PAGE_ID
Definition: bloom.h:57
uint16 bloom_page_id
Definition: bloom.h:40
uint16 flags
Definition: bloom.h:36
#define BloomPageGetOpaque(page)
Definition: bloom.h:60
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42

◆ BloomNewBuffer()

Buffer BloomNewBuffer ( Relation  index)

Definition at line 353 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().

354 {
355  Buffer buffer;
356  bool needLock;
357 
358  /* First, try to get a page from FSM */
359  for (;;)
360  {
361  BlockNumber blkno = GetFreeIndexPage(index);
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  needLock = !RELATION_IS_LOCAL(index);
391  if (needLock)
393 
394  buffer = ReadBuffer(index, P_NEW);
396 
397  if (needLock)
399 
400  return buffer;
401 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BloomPageIsDeleted(page)
Definition: bloom.h:64
#define ExclusiveLock
Definition: lockdefs.h:44
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:583
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3518
#define P_NEW
Definition: bufmgr.h:91
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:3783
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:3757
BlockNumber GetFreeIndexPage(Relation rel)
Definition: indexfsm.c:38
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
#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 318 of file blutils.c.

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

Referenced by blinsert(), and bloomBuildCallback().

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

◆ bloptions()

bytea* bloptions ( Datum  reloptions,
bool  validate 
)

Definition at line 482 of file blutils.c.

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

Referenced by blhandler().

483 {
484  BloomOptions *rdopts;
485 
486  /* Parse the user-given reloptions */
487  rdopts = (BloomOptions *) build_reloptions(reloptions, validate,
489  sizeof(BloomOptions),
492 
493  /* Convert signature length from # of bits to # to words, rounding up */
494  if (rdopts)
495  rdopts->bloomLength = (rdopts->bloomLength + SIGNWORDBITS - 1) / SIGNWORDBITS;
496 
497  return (bytea *) rdopts;
498 }
#define lengthof(array)
Definition: c.h:675
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:1887
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
#define SIGNWORDBITS
Definition: bloom.h:86
int bloomLength
Definition: bloom.h:104
Definition: c.h:562

◆ initBloomState()

void initBloomState ( BloomState state,
Relation  index 
)

Definition at line 161 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().

162 {
163  int i;
164 
165  state->nColumns = index->rd_att->natts;
166 
167  /* Initialize hash function for each attribute */
168  for (i = 0; i < index->rd_att->natts; i++)
169  {
170  fmgr_info_copy(&(state->hashFn[i]),
171  index_getprocinfo(index, i + 1, BLOOM_HASH_PROC),
173  state->collations[i] = index->rd_indcollation[i];
174  }
175 
176  /* Initialize amcache if needed with options from metapage */
177  if (!index->rd_amcache)
178  {
179  Buffer buffer;
180  Page page;
181  BloomMetaPageData *meta;
182  BloomOptions *opts;
183 
184  opts = MemoryContextAlloc(index->rd_indexcxt, sizeof(BloomOptions));
185 
186  buffer = ReadBuffer(index, BLOOM_METAPAGE_BLKNO);
187  LockBuffer(buffer, BUFFER_LOCK_SHARE);
188 
189  page = BufferGetPage(buffer);
190 
191  if (!BloomPageIsMeta(page))
192  elog(ERROR, "Relation is not a bloom index");
193  meta = BloomPageGetMeta(BufferGetPage(buffer));
194 
195  if (meta->magickNumber != BLOOM_MAGICK_NUMBER)
196  elog(ERROR, "Relation is not a bloom index");
197 
198  *opts = meta->opts;
199 
200  UnlockReleaseBuffer(buffer);
201 
202  index->rd_amcache = (void *) opts;
203  }
204 
205  memcpy(&state->opts, index->rd_amcache, sizeof(state->opts));
207  sizeof(BloomSignatureWord) * state->opts.bloomLength;
208 }
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:78
BloomOptions opts
Definition: bloom.h:142
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:801
#define BloomPageIsMeta(page)
Definition: bloom.h:62
FmgrInfo hashFn[INDEX_MAX_KEYS]
Definition: bloom.h:140
Oid collations[INDEX_MAX_KEYS]
Definition: bloom.h:141
uint16 BloomSignatureWord
Definition: bloom.h:84
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3541
Oid * rd_indcollation
Definition: rel.h:199
#define ERROR
Definition: elog.h:43
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:612
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define BloomPageGetMeta(page)
Definition: bloom.h:136
#define BLOOMTUPLEHDRSZ
Definition: bloom.h:166
BloomOptions opts
Definition: bloom.h:126
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3757
int32 nColumns
Definition: bloom.h:143
TupleDesc rd_att
Definition: rel.h:110
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:607
int bloomLength
Definition: bloom.h:104
uint32 magickNumber
Definition: bloom.h:123
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
#define elog(elevel,...)
Definition: elog.h:214
MemoryContext rd_indexcxt
Definition: rel.h:186
int i
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
void * rd_amcache
Definition: rel.h:211
#define BLOOM_MAGICK_NUMBER
Definition: bloom.h:131
Size sizeOfBloomTuple
Definition: bloom.h:149
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 87 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().

88 {
89  BloomOptions *opts;
90  int i;
91 
92  opts = (BloomOptions *) palloc0(sizeof(BloomOptions));
93  /* Convert DEFAULT_BLOOM_LENGTH from # of bits to # of words */
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 DEFAULT_BLOOM_BITS
Definition: bloom.h:97
int bitSize[INDEX_MAX_KEYS]
Definition: bloom.h:105
#define DEFAULT_BLOOM_LENGTH
Definition: bloom.h:91
#define SIGNWORDBITS
Definition: bloom.h:86
void * palloc0(Size size)
Definition: mcxt.c:980
#define INDEX_MAX_KEYS
int bloomLength
Definition: bloom.h:104
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ myRand()

static int32 myRand ( void  )
static

Definition at line 222 of file blutils.c.

References next.

Referenced by signValue().

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

◆ mySrand()

static void mySrand ( uint32  seed)
static

Definition at line 249 of file blutils.c.

References next.

Referenced by signValue().

250 {
251  next = seed;
252  /* Transform to [1, 0x7ffffffe] range. */
253  next = (next % 0x7ffffffe) + 1;
254 }
static int32 next
Definition: blutils.c:219

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( blhandler  )

◆ signValue()

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

Definition at line 260 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().

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

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 bloptions().

◆ next