PostgreSQL Source Code  git master
brin_bloom.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/brin.h"
#include "access/brin_internal.h"
#include "access/brin_page.h"
#include "access/brin_tuple.h"
#include "access/hash.h"
#include "access/htup_details.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
#include "catalog/pg_amop.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include <math.h>
Include dependency graph for brin_bloom.c:

Go to the source code of this file.

Data Structures

struct  BloomOptions
 
struct  BloomFilter
 
struct  BloomOpaque
 

Macros

#define BloomEqualStrategyNumber   1
 
#define BLOOM_MAX_PROCNUMS   1 /* maximum support procs we need */
 
#define PROCNUM_HASH   11 /* required */
 
#define PROCNUM_BASE   11
 
#define BLOOM_MIN_NDISTINCT_PER_RANGE   16
 
#define BLOOM_DEFAULT_NDISTINCT_PER_RANGE   -0.1 /* 10% of values */
 
#define BLOOM_MIN_FALSE_POSITIVE_RATE   0.0001 /* 0.01% fp rate */
 
#define BLOOM_MAX_FALSE_POSITIVE_RATE   0.25 /* 25% fp rate */
 
#define BLOOM_DEFAULT_FALSE_POSITIVE_RATE   0.01 /* 1% fp rate */
 
#define BloomGetNDistinctPerRange(opts)
 
#define BloomGetFalsePositiveRate(opts)
 
#define BloomMaxFilterSize
 
#define BLOOM_SEED_1   0x71d924af
 
#define BLOOM_SEED_2   0xba48b314
 

Typedefs

typedef struct BloomOptions BloomOptions
 
typedef struct BloomFilter BloomFilter
 
typedef struct BloomOpaque BloomOpaque
 

Functions

static BloomFilterbloom_init (int ndistinct, double false_positive_rate)
 
static BloomFilterbloom_add_value (BloomFilter *filter, uint32 value, bool *updated)
 
static bool bloom_contains_value (BloomFilter *filter, uint32 value)
 
static FmgrInfobloom_get_procinfo (BrinDesc *bdesc, uint16 attno, uint16 procnum)
 
Datum brin_bloom_opcinfo (PG_FUNCTION_ARGS)
 
static int brin_bloom_get_ndistinct (BrinDesc *bdesc, BloomOptions *opts)
 
Datum brin_bloom_add_value (PG_FUNCTION_ARGS)
 
Datum brin_bloom_consistent (PG_FUNCTION_ARGS)
 
Datum brin_bloom_union (PG_FUNCTION_ARGS)
 
Datum brin_bloom_options (PG_FUNCTION_ARGS)
 
Datum brin_bloom_summary_in (PG_FUNCTION_ARGS)
 
Datum brin_bloom_summary_out (PG_FUNCTION_ARGS)
 
Datum brin_bloom_summary_recv (PG_FUNCTION_ARGS)
 
Datum brin_bloom_summary_send (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ BLOOM_DEFAULT_FALSE_POSITIVE_RATE

#define BLOOM_DEFAULT_FALSE_POSITIVE_RATE   0.01 /* 1% fp rate */

Definition at line 197 of file brin_bloom.c.

◆ BLOOM_DEFAULT_NDISTINCT_PER_RANGE

#define BLOOM_DEFAULT_NDISTINCT_PER_RANGE   -0.1 /* 10% of values */

Definition at line 179 of file brin_bloom.c.

◆ BLOOM_MAX_FALSE_POSITIVE_RATE

#define BLOOM_MAX_FALSE_POSITIVE_RATE   0.25 /* 25% fp rate */

Definition at line 196 of file brin_bloom.c.

◆ BLOOM_MAX_PROCNUMS

#define BLOOM_MAX_PROCNUMS   1 /* maximum support procs we need */

Definition at line 145 of file brin_bloom.c.

◆ BLOOM_MIN_FALSE_POSITIVE_RATE

#define BLOOM_MIN_FALSE_POSITIVE_RATE   0.0001 /* 0.01% fp rate */

Definition at line 195 of file brin_bloom.c.

◆ BLOOM_MIN_NDISTINCT_PER_RANGE

#define BLOOM_MIN_NDISTINCT_PER_RANGE   16

Definition at line 172 of file brin_bloom.c.

◆ BLOOM_SEED_1

#define BLOOM_SEED_1   0x71d924af

Definition at line 225 of file brin_bloom.c.

◆ BLOOM_SEED_2

#define BLOOM_SEED_2   0xba48b314

Definition at line 226 of file brin_bloom.c.

◆ BloomEqualStrategyNumber

#define BloomEqualStrategyNumber   1

Definition at line 136 of file brin_bloom.c.

◆ BloomGetFalsePositiveRate

#define BloomGetFalsePositiveRate (   opts)
Value:
((opts) && (((BloomOptions *) (opts))->falsePositiveRate != 0.0) ? \
(((BloomOptions *) (opts))->falsePositiveRate) : \
BLOOM_DEFAULT_FALSE_POSITIVE_RATE)
static AmcheckOptions opts
Definition: pg_amcheck.c:110

Definition at line 204 of file brin_bloom.c.

◆ BloomGetNDistinctPerRange

#define BloomGetNDistinctPerRange (   opts)
Value:
((opts) && (((BloomOptions *) (opts))->nDistinctPerRange != 0) ? \
(((BloomOptions *) (opts))->nDistinctPerRange) : \
#define BLOOM_DEFAULT_NDISTINCT_PER_RANGE
Definition: brin_bloom.c:179

Definition at line 199 of file brin_bloom.c.

◆ BloomMaxFilterSize

#define BloomMaxFilterSize
Value:
MAXALIGN_DOWN(BLCKSZ - \
sizeof(ItemIdData)) + \
#define SizeOfBrinTuple
Definition: brin_tuple.h:79
#define SizeOfPageHeaderData
Definition: bufpage.h:213
#define MAXALIGN_DOWN(LEN)
Definition: c.h:807
#define MAXALIGN(LEN)
Definition: c.h:795

Definition at line 214 of file brin_bloom.c.

◆ PROCNUM_BASE

#define PROCNUM_BASE   11

Definition at line 152 of file brin_bloom.c.

◆ PROCNUM_HASH

#define PROCNUM_HASH   11 /* required */

Definition at line 146 of file brin_bloom.c.

Typedef Documentation

◆ BloomFilter

typedef struct BloomFilter BloomFilter

◆ BloomOpaque

typedef struct BloomOpaque BloomOpaque

◆ BloomOptions

typedef struct BloomOptions BloomOptions

Function Documentation

◆ bloom_add_value()

static BloomFilter* bloom_add_value ( BloomFilter filter,
uint32  value,
bool updated 
)
static

Definition at line 343 of file brin_bloom.c.

344 {
345  int i;
346  uint64 h1,
347  h2;
348 
349  /* compute the hashes, used for the bloom filter */
352 
353  /* compute the requested number of hashes */
354  for (i = 0; i < filter->nhashes; i++)
355  {
356  /* h1 + h2 + f(i) */
357  uint32 h = (h1 + i * h2) % filter->nbits;
358  uint32 byte = (h / 8);
359  uint32 bit = (h % 8);
360 
361  /* if the bit is not set, set it and remember we did that */
362  if (!(filter->data[byte] & (0x01 << bit)))
363  {
364  filter->data[byte] |= (0x01 << bit);
365  filter->nbits_set++;
366  if (updated)
367  *updated = true;
368  }
369  }
370 
371  return filter;
372 }
#define BLOOM_SEED_1
Definition: brin_bloom.c:225
#define BLOOM_SEED_2
Definition: brin_bloom.c:226
unsigned int uint32
Definition: c.h:490
uint64 hash_bytes_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.c:631
static struct @143 value
int i
Definition: isn.c:73
uint8 nhashes
Definition: brin_bloom.c:254
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_bloom.c:259
uint32 nbits_set
Definition: brin_bloom.c:256
uint32 nbits
Definition: brin_bloom.c:255
Datum bit(PG_FUNCTION_ARGS)
Definition: varbit.c:391

References bit(), BLOOM_SEED_1, BLOOM_SEED_2, BloomFilter::data, hash_bytes_uint32_extended(), i, BloomFilter::nbits, BloomFilter::nbits_set, BloomFilter::nhashes, and value.

Referenced by brin_bloom_add_value().

◆ bloom_contains_value()

static bool bloom_contains_value ( BloomFilter filter,
uint32  value 
)
static

Definition at line 380 of file brin_bloom.c.

381 {
382  int i;
383  uint64 h1,
384  h2;
385 
386  /* calculate the two hashes */
389 
390  /* compute the requested number of hashes */
391  for (i = 0; i < filter->nhashes; i++)
392  {
393  /* h1 + h2 + f(i) */
394  uint32 h = (h1 + i * h2) % filter->nbits;
395  uint32 byte = (h / 8);
396  uint32 bit = (h % 8);
397 
398  /* if the bit is not set, the value is not there */
399  if (!(filter->data[byte] & (0x01 << bit)))
400  return false;
401  }
402 
403  /* all hashes found in bloom filter */
404  return true;
405 }

References bit(), BLOOM_SEED_1, BLOOM_SEED_2, BloomFilter::data, hash_bytes_uint32_extended(), i, BloomFilter::nbits, BloomFilter::nhashes, and value.

Referenced by brin_bloom_consistent().

◆ bloom_get_procinfo()

static FmgrInfo * bloom_get_procinfo ( BrinDesc bdesc,
uint16  attno,
uint16  procnum 
)
static

Definition at line 674 of file brin_bloom.c.

675 {
676  BloomOpaque *opaque;
677  uint16 basenum = procnum - PROCNUM_BASE;
678 
679  /*
680  * We cache these in the opaque struct, to avoid repetitive syscache
681  * lookups.
682  */
683  opaque = (BloomOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
684 
685  /*
686  * If we already searched for this proc and didn't find it, don't bother
687  * searching again.
688  */
689  if (opaque->extra_proc_missing[basenum])
690  return NULL;
691 
692  if (opaque->extra_procinfos[basenum].fn_oid == InvalidOid)
693  {
694  if (RegProcedureIsValid(index_getprocid(bdesc->bd_index, attno,
695  procnum)))
696  {
697  fmgr_info_copy(&opaque->extra_procinfos[basenum],
698  index_getprocinfo(bdesc->bd_index, attno, procnum),
699  bdesc->bd_context);
700  }
701  else
702  {
703  opaque->extra_proc_missing[basenum] = true;
704  return NULL;
705  }
706  }
707 
708  return &opaque->extra_procinfos[basenum];
709 }
#define PROCNUM_BASE
Definition: brin_bloom.c:152
unsigned short uint16
Definition: c.h:489
#define RegProcedureIsValid(p)
Definition: c.h:761
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:595
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:811
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:777
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define InvalidOid
Definition: postgres_ext.h:36
FmgrInfo extra_procinfos[BLOOM_MAX_PROCNUMS]
Definition: brin_bloom.c:414
bool extra_proc_missing[BLOOM_MAX_PROCNUMS]
Definition: brin_bloom.c:415
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
Relation bd_index
Definition: brin_internal.h:50
MemoryContext bd_context
Definition: brin_internal.h:47
void * oi_opaque
Definition: brin_internal.h:34
Oid fn_oid
Definition: fmgr.h:59

References BrinDesc::bd_context, BrinDesc::bd_index, BrinDesc::bd_info, BloomOpaque::extra_proc_missing, BloomOpaque::extra_procinfos, fmgr_info_copy(), FmgrInfo::fn_oid, if(), index_getprocid(), index_getprocinfo(), InvalidOid, BrinOpcInfo::oi_opaque, PROCNUM_BASE, and RegProcedureIsValid.

Referenced by brin_bloom_add_value(), and brin_bloom_consistent().

◆ bloom_init()

static BloomFilter* bloom_init ( int  ndistinct,
double  false_positive_rate 
)
static

Definition at line 271 of file brin_bloom.c.

272 {
273  Size len;
274  BloomFilter *filter;
275 
276  int nbits; /* size of filter / number of bits */
277  int nbytes; /* size of filter / number of bytes */
278 
279  double k; /* number of hash functions */
280 
281  Assert(ndistinct > 0);
282  Assert((false_positive_rate >= BLOOM_MIN_FALSE_POSITIVE_RATE) &&
283  (false_positive_rate < BLOOM_MAX_FALSE_POSITIVE_RATE));
284 
285  /* sizing bloom filter: -(n * ln(p)) / (ln(2))^2 */
286  nbits = ceil(-(ndistinct * log(false_positive_rate)) / pow(log(2.0), 2));
287 
288  /* round m to whole bytes */
289  nbytes = ((nbits + 7) / 8);
290  nbits = nbytes * 8;
291 
292  /*
293  * Reject filters that are obviously too large to store on a page.
294  *
295  * Initially the bloom filter is just zeroes and so very compressible, but
296  * as we add values it gets more and more random, and so less and less
297  * compressible. So initially everything fits on the page, but we might
298  * get surprising failures later - we want to prevent that, so we reject
299  * bloom filter that are obviously too large.
300  *
301  * XXX It's not uncommon to oversize the bloom filter a bit, to defend
302  * against unexpected data anomalies (parts of table with more distinct
303  * values per range etc.). But we still need to make sure even the
304  * oversized filter fits on page, if such need arises.
305  *
306  * XXX This check is not perfect, because the index may have multiple
307  * filters that are small individually, but too large when combined.
308  */
309  if (nbytes > BloomMaxFilterSize)
310  elog(ERROR, "the bloom filter is too large (%d > %zu)", nbytes,
312 
313  /*
314  * round(log(2.0) * m / ndistinct), but assume round() may not be
315  * available on Windows
316  */
317  k = log(2.0) * nbits / ndistinct;
318  k = (k - floor(k) >= 0.5) ? ceil(k) : floor(k);
319 
320  /*
321  * We allocate the whole filter. Most of it is going to be 0 bits, so the
322  * varlena is easy to compress.
323  */
324  len = offsetof(BloomFilter, data) + nbytes;
325 
326  filter = (BloomFilter *) palloc0(len);
327 
328  filter->flags = 0;
329  filter->nhashes = (int) k;
330  filter->nbits = nbits;
331 
332  SET_VARSIZE(filter, len);
333 
334  return filter;
335 }
#define BLOOM_MAX_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:196
#define BloomMaxFilterSize
Definition: brin_bloom.c:214
#define BLOOM_MIN_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:195
size_t Size
Definition: c.h:589
#define ERROR
Definition: elog.h:39
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1241
const void size_t len
const void * data
uint16 flags
Definition: brin_bloom.c:251
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References Assert(), BLOOM_MAX_FALSE_POSITIVE_RATE, BLOOM_MIN_FALSE_POSITIVE_RATE, BloomMaxFilterSize, data, elog(), ERROR, BloomFilter::flags, len, BloomFilter::nbits, BloomFilter::nhashes, palloc0(), and SET_VARSIZE.

Referenced by brin_bloom_add_value().

◆ brin_bloom_add_value()

Datum brin_bloom_add_value ( PG_FUNCTION_ARGS  )

Definition at line 513 of file brin_bloom.c.

514 {
515  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
516  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
518  bool isnull PG_USED_FOR_ASSERTS_ONLY = PG_GETARG_DATUM(3);
520  Oid colloid = PG_GET_COLLATION();
521  FmgrInfo *hashFn;
522  uint32 hashValue;
523  bool updated = false;
524  AttrNumber attno;
525  BloomFilter *filter;
526 
527  Assert(!isnull);
528 
529  attno = column->bv_attno;
530 
531  /*
532  * If this is the first non-null value, we need to initialize the bloom
533  * filter. Otherwise just extract the existing bloom filter from
534  * BrinValues.
535  */
536  if (column->bv_allnulls)
537  {
538  filter = bloom_init(brin_bloom_get_ndistinct(bdesc, opts),
540  column->bv_values[0] = PointerGetDatum(filter);
541  column->bv_allnulls = false;
542  updated = true;
543  }
544  else
545  filter = (BloomFilter *) PG_DETOAST_DATUM(column->bv_values[0]);
546 
547  /*
548  * Compute the hash of the new value, using the supplied hash function,
549  * and then add the hash value to the bloom filter.
550  */
551  hashFn = bloom_get_procinfo(bdesc, attno, PROCNUM_HASH);
552 
553  hashValue = DatumGetUInt32(FunctionCall1Coll(hashFn, colloid, newval));
554 
555  filter = bloom_add_value(filter, hashValue, &updated);
556 
557  column->bv_values[0] = PointerGetDatum(filter);
558 
559  PG_RETURN_BOOL(updated);
560 }
int16 AttrNumber
Definition: attnum.h:21
static int brin_bloom_get_ndistinct(BrinDesc *bdesc, BloomOptions *opts)
Definition: brin_bloom.c:470
#define PROCNUM_HASH
Definition: brin_bloom.c:146
static BloomFilter * bloom_init(int ndistinct, double false_positive_rate)
Definition: brin_bloom.c:271
#define BloomGetFalsePositiveRate(opts)
Definition: brin_bloom.c:204
static BloomFilter * bloom_add_value(BloomFilter *filter, uint32 value, bool *updated)
Definition: brin_bloom.c:343
static FmgrInfo * bloom_get_procinfo(BrinDesc *bdesc, uint16 attno, uint16 procnum)
Definition: brin_bloom.c:674
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1116
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GET_OPCLASS_OPTIONS()
Definition: fmgr.h:342
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define newval
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:222
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
Datum * bv_values
Definition: brin_tuple.h:34
AttrNumber bv_attno
Definition: brin_tuple.h:31
bool bv_allnulls
Definition: brin_tuple.h:33
Definition: fmgr.h:57

References Assert(), bloom_add_value(), bloom_get_procinfo(), bloom_init(), BloomGetFalsePositiveRate, brin_bloom_get_ndistinct(), BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_values, DatumGetUInt32(), FunctionCall1Coll(), newval, opts, PG_DETOAST_DATUM, PG_GET_COLLATION, PG_GET_OPCLASS_OPTIONS, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_BOOL, PG_USED_FOR_ASSERTS_ONLY, PointerGetDatum(), and PROCNUM_HASH.

◆ brin_bloom_consistent()

Datum brin_bloom_consistent ( PG_FUNCTION_ARGS  )

Definition at line 568 of file brin_bloom.c.

569 {
570  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
571  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
572  ScanKey *keys = (ScanKey *) PG_GETARG_POINTER(2);
573  int nkeys = PG_GETARG_INT32(3);
574  Oid colloid = PG_GET_COLLATION();
575  AttrNumber attno;
576  Datum value;
577  Datum matches;
578  FmgrInfo *finfo;
579  uint32 hashValue;
580  BloomFilter *filter;
581  int keyno;
582 
583  filter = (BloomFilter *) PG_DETOAST_DATUM(column->bv_values[0]);
584 
585  Assert(filter);
586 
587  matches = true;
588 
589  for (keyno = 0; keyno < nkeys; keyno++)
590  {
591  ScanKey key = keys[keyno];
592 
593  /* NULL keys are handled and filtered-out in bringetbitmap */
594  Assert(!(key->sk_flags & SK_ISNULL));
595 
596  attno = key->sk_attno;
597  value = key->sk_argument;
598 
599  switch (key->sk_strategy)
600  {
602 
603  /*
604  * In the equality case (WHERE col = someval), we want to
605  * return the current page range if the minimum value in the
606  * range <= scan key, and the maximum value >= scan key.
607  */
608  finfo = bloom_get_procinfo(bdesc, attno, PROCNUM_HASH);
609 
610  hashValue = DatumGetUInt32(FunctionCall1Coll(finfo, colloid, value));
611  matches &= bloom_contains_value(filter, hashValue);
612 
613  break;
614  default:
615  /* shouldn't happen */
616  elog(ERROR, "invalid strategy number %d", key->sk_strategy);
617  matches = 0;
618  break;
619  }
620 
621  if (!matches)
622  break;
623  }
624 
625  PG_RETURN_DATUM(matches);
626 }
static bool bloom_contains_value(BloomFilter *filter, uint32 value)
Definition: brin_bloom.c:380
#define BloomEqualStrategyNumber
Definition: brin_bloom.c:136
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define SK_ISNULL
Definition: skey.h:115

References Assert(), bloom_contains_value(), bloom_get_procinfo(), BloomEqualStrategyNumber, BrinValues::bv_values, DatumGetUInt32(), elog(), ERROR, FunctionCall1Coll(), sort-test::key, PG_DETOAST_DATUM, PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_POINTER, PG_RETURN_DATUM, PROCNUM_HASH, SK_ISNULL, and value.

◆ brin_bloom_get_ndistinct()

static int brin_bloom_get_ndistinct ( BrinDesc bdesc,
BloomOptions opts 
)
static

Definition at line 470 of file brin_bloom.c.

471 {
472  double ndistinct;
473  double maxtuples;
474  BlockNumber pagesPerRange;
475 
476  pagesPerRange = BrinGetPagesPerRange(bdesc->bd_index);
477  ndistinct = BloomGetNDistinctPerRange(opts);
478 
479  Assert(BlockNumberIsValid(pagesPerRange));
480 
481  maxtuples = MaxHeapTuplesPerPage * pagesPerRange;
482 
483  /*
484  * Similarly to n_distinct, negative values are relative - in this case to
485  * maximum number of tuples in the page range (maxtuples).
486  */
487  if (ndistinct < 0)
488  ndistinct = (-ndistinct) * maxtuples;
489 
490  /*
491  * Positive values are to be used directly, but we still apply a couple of
492  * safeties to avoid using unreasonably small bloom filters.
493  */
494  ndistinct = Max(ndistinct, BLOOM_MIN_NDISTINCT_PER_RANGE);
495 
496  /*
497  * And don't use more than the maximum possible number of tuples, in the
498  * range, which would be entirely wasteful.
499  */
500  ndistinct = Min(ndistinct, maxtuples);
501 
502  return (int) ndistinct;
503 }
uint32 BlockNumber
Definition: block.h:31
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define BrinGetPagesPerRange(relation)
Definition: brin.h:39
#define BloomGetNDistinctPerRange(opts)
Definition: brin_bloom.c:199
#define BLOOM_MIN_NDISTINCT_PER_RANGE
Definition: brin_bloom.c:172
#define Min(x, y)
Definition: c.h:988
#define Max(x, y)
Definition: c.h:982
#define MaxHeapTuplesPerPage
Definition: htup_details.h:572

References Assert(), BrinDesc::bd_index, BlockNumberIsValid(), BLOOM_MIN_NDISTINCT_PER_RANGE, BloomGetNDistinctPerRange, BrinGetPagesPerRange, Max, MaxHeapTuplesPerPage, Min, and opts.

Referenced by brin_bloom_add_value().

◆ brin_bloom_opcinfo()

Datum brin_bloom_opcinfo ( PG_FUNCTION_ARGS  )

Definition at line 423 of file brin_bloom.c.

424 {
425  BrinOpcInfo *result;
426 
427  /*
428  * opaque->strategy_procinfos is initialized lazily; here it is set to
429  * all-uninitialized by palloc0 which sets fn_oid to InvalidOid.
430  *
431  * bloom indexes only store the filter as a single BYTEA column
432  */
433 
434  result = palloc0(MAXALIGN(SizeofBrinOpcInfo(1)) +
435  sizeof(BloomOpaque));
436  result->oi_nstored = 1;
437  result->oi_regular_nulls = true;
438  result->oi_opaque = (BloomOpaque *)
439  MAXALIGN((char *) result + SizeofBrinOpcInfo(1));
440  result->oi_typcache[0] = lookup_type_cache(PG_BRIN_BLOOM_SUMMARYOID, 0);
441 
442  PG_RETURN_POINTER(result);
443 }
#define SizeofBrinOpcInfo(ncols)
Definition: brin_internal.h:41
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
TypeCacheEntry * oi_typcache[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:37
uint16 oi_nstored
Definition: brin_internal.h:28
bool oi_regular_nulls
Definition: brin_internal.h:31
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339

References lookup_type_cache(), MAXALIGN, BrinOpcInfo::oi_nstored, BrinOpcInfo::oi_opaque, BrinOpcInfo::oi_regular_nulls, BrinOpcInfo::oi_typcache, palloc0(), PG_RETURN_POINTER, and SizeofBrinOpcInfo.

◆ brin_bloom_options()

Datum brin_bloom_options ( PG_FUNCTION_ARGS  )

Definition at line 712 of file brin_bloom.c.

713 {
715 
716  init_local_reloptions(relopts, sizeof(BloomOptions));
717 
718  add_local_real_reloption(relopts, "n_distinct_per_range",
719  "number of distinct items expected in a BRIN page range",
721  -1.0, INT_MAX, offsetof(BloomOptions, nDistinctPerRange));
722 
723  add_local_real_reloption(relopts, "false_positive_rate",
724  "desired false-positive rate for the bloom filters",
728  offsetof(BloomOptions, falsePositiveRate));
729 
730  PG_RETURN_VOID();
731 }
#define BLOOM_DEFAULT_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:197
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition: reloptions.c:736
void add_local_real_reloption(local_relopts *relopts, const char *name, const char *desc, double default_val, double min_val, double max_val, int offset)
Definition: reloptions.c:974

References add_local_real_reloption(), BLOOM_DEFAULT_FALSE_POSITIVE_RATE, BLOOM_DEFAULT_NDISTINCT_PER_RANGE, BLOOM_MAX_FALSE_POSITIVE_RATE, BLOOM_MIN_FALSE_POSITIVE_RATE, init_local_reloptions(), PG_GETARG_POINTER, and PG_RETURN_VOID.

◆ brin_bloom_summary_in()

Datum brin_bloom_summary_in ( PG_FUNCTION_ARGS  )

Definition at line 742 of file brin_bloom.c.

743 {
744  /*
745  * brin_bloom_summary stores the data in binary form and parsing text
746  * input is not needed, so disallow this.
747  */
748  ereport(ERROR,
749  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
750  errmsg("cannot accept a value of type %s", "pg_brin_bloom_summary")));
751 
752  PG_RETURN_VOID(); /* keep compiler quiet */
753 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_VOID.

◆ brin_bloom_summary_out()

Datum brin_bloom_summary_out ( PG_FUNCTION_ARGS  )

Definition at line 764 of file brin_bloom.c.

765 {
766  BloomFilter *filter;
768 
769  /* detoast the data to get value with a full 4B header */
771 
773  appendStringInfoChar(&str, '{');
774 
775  appendStringInfo(&str, "mode: hashed nhashes: %u nbits: %u nbits_set: %u",
776  filter->nhashes, filter->nbits, filter->nbits_set);
777 
778  appendStringInfoChar(&str, '}');
779 
780  PG_RETURN_CSTRING(str.data);
781 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_DETOAST_DATUM_PACKED(datum)
Definition: fmgr.h:248
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References appendStringInfo(), appendStringInfoChar(), initStringInfo(), BloomFilter::nbits, BloomFilter::nbits_set, BloomFilter::nhashes, PG_DETOAST_DATUM_PACKED, PG_GETARG_DATUM, PG_RETURN_CSTRING, and generate_unaccent_rules::str.

◆ brin_bloom_summary_recv()

Datum brin_bloom_summary_recv ( PG_FUNCTION_ARGS  )

Definition at line 788 of file brin_bloom.c.

789 {
790  ereport(ERROR,
791  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
792  errmsg("cannot accept a value of type %s", "pg_brin_bloom_summary")));
793 
794  PG_RETURN_VOID(); /* keep compiler quiet */
795 }

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_VOID.

◆ brin_bloom_summary_send()

Datum brin_bloom_summary_send ( PG_FUNCTION_ARGS  )

Definition at line 805 of file brin_bloom.c.

806 {
807  return byteasend(fcinfo);
808 }
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:488

References byteasend().

◆ brin_bloom_union()

Datum brin_bloom_union ( PG_FUNCTION_ARGS  )

Definition at line 637 of file brin_bloom.c.

638 {
639  int i;
640  int nbytes;
641  BrinValues *col_a = (BrinValues *) PG_GETARG_POINTER(1);
642  BrinValues *col_b = (BrinValues *) PG_GETARG_POINTER(2);
643  BloomFilter *filter_a;
644  BloomFilter *filter_b;
645 
646  Assert(col_a->bv_attno == col_b->bv_attno);
647  Assert(!col_a->bv_allnulls && !col_b->bv_allnulls);
648 
649  filter_a = (BloomFilter *) PG_DETOAST_DATUM(col_a->bv_values[0]);
650  filter_b = (BloomFilter *) PG_DETOAST_DATUM(col_b->bv_values[0]);
651 
652  /* make sure the filters use the same parameters */
653  Assert(filter_a && filter_b);
654  Assert(filter_a->nbits == filter_b->nbits);
655  Assert(filter_a->nhashes == filter_b->nhashes);
656  Assert((filter_a->nbits > 0) && (filter_a->nbits % 8 == 0));
657 
658  nbytes = (filter_a->nbits) / 8;
659 
660  /* simply OR the bitmaps */
661  for (i = 0; i < nbytes; i++)
662  filter_a->data[i] |= filter_b->data[i];
663 
664  PG_RETURN_VOID();
665 }

References Assert(), BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_values, BloomFilter::data, i, BloomFilter::nbits, BloomFilter::nhashes, PG_DETOAST_DATUM, PG_GETARG_POINTER, and PG_RETURN_VOID.