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 void bloom_filter_size (int ndistinct, double false_positive_rate, int *nbytesp, int *nbitsp, int *nhashesp)
 
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:111

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:80
#define SizeOfPageHeaderData
Definition: bufpage.h:213
#define MAXALIGN_DOWN(LEN)
Definition: c.h:812
#define MAXALIGN(LEN)
Definition: c.h:800

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 373 of file brin_bloom.c.

374 {
375  int i;
376  uint64 h1,
377  h2;
378 
379  /* compute the hashes, used for the bloom filter */
382 
383  /* compute the requested number of hashes */
384  for (i = 0; i < filter->nhashes; i++)
385  {
386  /* h1 + h2 + f(i) */
387  uint32 h = (h1 + i * h2) % filter->nbits;
388  uint32 byte = (h / 8);
389  uint32 bit = (h % 8);
390 
391  /* if the bit is not set, set it and remember we did that */
392  if (!(filter->data[byte] & (0x01 << bit)))
393  {
394  filter->data[byte] |= (0x01 << bit);
395  filter->nbits_set++;
396  if (updated)
397  *updated = true;
398  }
399  }
400 
401  return filter;
402 }
#define BLOOM_SEED_1
Definition: brin_bloom.c:225
#define BLOOM_SEED_2
Definition: brin_bloom.c:226
unsigned int uint32
Definition: c.h:495
uint64 hash_bytes_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.c:631
static struct @148 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 410 of file brin_bloom.c.

411 {
412  int i;
413  uint64 h1,
414  h2;
415 
416  /* calculate the two hashes */
419 
420  /* compute the requested number of hashes */
421  for (i = 0; i < filter->nhashes; i++)
422  {
423  /* h1 + h2 + f(i) */
424  uint32 h = (h1 + i * h2) % filter->nbits;
425  uint32 byte = (h / 8);
426  uint32 bit = (h % 8);
427 
428  /* if the bit is not set, the value is not there */
429  if (!(filter->data[byte] & (0x01 << bit)))
430  return false;
431  }
432 
433  /* all hashes found in bloom filter */
434  return true;
435 }

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_filter_size()

static void bloom_filter_size ( int  ndistinct,
double  false_positive_rate,
int *  nbytesp,
int *  nbitsp,
int *  nhashesp 
)
static

Definition at line 274 of file brin_bloom.c.

276 {
277  double k;
278  int nbits,
279  nbytes;
280 
281  /* sizing bloom filter: -(n * ln(p)) / (ln(2))^2 */
282  nbits = ceil(-(ndistinct * log(false_positive_rate)) / pow(log(2.0), 2));
283 
284  /* round m to whole bytes */
285  nbytes = ((nbits + 7) / 8);
286  nbits = nbytes * 8;
287 
288  /*
289  * round(log(2.0) * m / ndistinct), but assume round() may not be
290  * available on Windows
291  */
292  k = log(2.0) * nbits / ndistinct;
293  k = (k - floor(k) >= 0.5) ? ceil(k) : floor(k);
294 
295  if (nbytesp)
296  *nbytesp = nbytes;
297 
298  if (nbitsp)
299  *nbitsp = nbits;
300 
301  if (nhashesp)
302  *nhashesp = (int) k;
303 }

Referenced by bloom_init().

◆ bloom_get_procinfo()

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

Definition at line 707 of file brin_bloom.c.

708 {
709  BloomOpaque *opaque;
710  uint16 basenum = procnum - PROCNUM_BASE;
711 
712  /*
713  * We cache these in the opaque struct, to avoid repetitive syscache
714  * lookups.
715  */
716  opaque = (BloomOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
717 
718  /*
719  * If we already searched for this proc and didn't find it, don't bother
720  * searching again.
721  */
722  if (opaque->extra_proc_missing[basenum])
723  return NULL;
724 
725  if (opaque->extra_procinfos[basenum].fn_oid == InvalidOid)
726  {
727  if (RegProcedureIsValid(index_getprocid(bdesc->bd_index, attno,
728  procnum)))
729  {
730  fmgr_info_copy(&opaque->extra_procinfos[basenum],
731  index_getprocinfo(bdesc->bd_index, attno, procnum),
732  bdesc->bd_context);
733  }
734  else
735  {
736  opaque->extra_proc_missing[basenum] = true;
737  return NULL;
738  }
739  }
740 
741  return &opaque->extra_procinfos[basenum];
742 }
#define PROCNUM_BASE
Definition: brin_bloom.c:152
unsigned short uint16
Definition: c.h:494
#define RegProcedureIsValid(p)
Definition: c.h:766
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:863
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:829
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:444
bool extra_proc_missing[BLOOM_MAX_PROCNUMS]
Definition: brin_bloom.c:445
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 313 of file brin_bloom.c.

314 {
315  Size len;
316  BloomFilter *filter;
317 
318  int nbits; /* size of filter / number of bits */
319  int nbytes; /* size of filter / number of bytes */
320  int nhashes; /* number of hash functions */
321 
322  Assert(ndistinct > 0);
323  Assert(false_positive_rate > 0 && false_positive_rate < 1);
324 
325  /* calculate bloom filter size / parameters */
326  bloom_filter_size(ndistinct, false_positive_rate,
327  &nbytes, &nbits, &nhashes);
328 
329  /*
330  * Reject filters that are obviously too large to store on a page.
331  *
332  * Initially the bloom filter is just zeroes and so very compressible, but
333  * as we add values it gets more and more random, and so less and less
334  * compressible. So initially everything fits on the page, but we might
335  * get surprising failures later - we want to prevent that, so we reject
336  * bloom filter that are obviously too large.
337  *
338  * XXX It's not uncommon to oversize the bloom filter a bit, to defend
339  * against unexpected data anomalies (parts of table with more distinct
340  * values per range etc.). But we still need to make sure even the
341  * oversized filter fits on page, if such need arises.
342  *
343  * XXX This check is not perfect, because the index may have multiple
344  * filters that are small individually, but too large when combined.
345  */
346  if (nbytes > BloomMaxFilterSize)
347  elog(ERROR, "the bloom filter is too large (%d > %zu)", nbytes,
349 
350  /*
351  * We allocate the whole filter. Most of it is going to be 0 bits, so the
352  * varlena is easy to compress.
353  */
354  len = offsetof(BloomFilter, data) + nbytes;
355 
356  filter = (BloomFilter *) palloc0(len);
357 
358  filter->flags = 0;
359  filter->nhashes = nhashes;
360  filter->nbits = nbits;
361 
362  SET_VARSIZE(filter, len);
363 
364  return filter;
365 }
static void bloom_filter_size(int ndistinct, double false_positive_rate, int *nbytesp, int *nbitsp, int *nhashesp)
Definition: brin_bloom.c:274
#define BloomMaxFilterSize
Definition: brin_bloom.c:214
size_t Size
Definition: c.h:594
#define ERROR
Definition: elog.h:39
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1232
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_filter_size(), 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 543 of file brin_bloom.c.

544 {
545  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
546  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
548  bool isnull PG_USED_FOR_ASSERTS_ONLY = PG_GETARG_DATUM(3);
550  Oid colloid = PG_GET_COLLATION();
551  FmgrInfo *hashFn;
552  uint32 hashValue;
553  bool updated = false;
554  AttrNumber attno;
555  BloomFilter *filter;
556 
557  Assert(!isnull);
558 
559  attno = column->bv_attno;
560 
561  /*
562  * If this is the first non-null value, we need to initialize the bloom
563  * filter. Otherwise just extract the existing bloom filter from
564  * BrinValues.
565  */
566  if (column->bv_allnulls)
567  {
568  filter = bloom_init(brin_bloom_get_ndistinct(bdesc, opts),
570  column->bv_values[0] = PointerGetDatum(filter);
571  column->bv_allnulls = false;
572  updated = true;
573  }
574  else
575  filter = (BloomFilter *) PG_DETOAST_DATUM(column->bv_values[0]);
576 
577  /*
578  * Compute the hash of the new value, using the supplied hash function,
579  * and then add the hash value to the bloom filter.
580  */
581  hashFn = bloom_get_procinfo(bdesc, attno, PROCNUM_HASH);
582 
583  hashValue = DatumGetUInt32(FunctionCall1Coll(hashFn, colloid, newval));
584 
585  filter = bloom_add_value(filter, hashValue, &updated);
586 
587  column->bv_values[0] = PointerGetDatum(filter);
588 
589  PG_RETURN_BOOL(updated);
590 }
int16 AttrNumber
Definition: attnum.h:21
static int brin_bloom_get_ndistinct(BrinDesc *bdesc, BloomOptions *opts)
Definition: brin_bloom.c:500
#define PROCNUM_HASH
Definition: brin_bloom.c:146
static BloomFilter * bloom_init(int ndistinct, double false_positive_rate)
Definition: brin_bloom.c:313
#define BloomGetFalsePositiveRate(opts)
Definition: brin_bloom.c:204
static BloomFilter * bloom_add_value(BloomFilter *filter, uint32 value, bool *updated)
Definition: brin_bloom.c:373
static FmgrInfo * bloom_get_procinfo(BrinDesc *bdesc, uint16 attno, uint16 procnum)
Definition: brin_bloom.c:707
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:171
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1129
#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 598 of file brin_bloom.c.

599 {
600  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
601  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
602  ScanKey *keys = (ScanKey *) PG_GETARG_POINTER(2);
603  int nkeys = PG_GETARG_INT32(3);
604  Oid colloid = PG_GET_COLLATION();
605  AttrNumber attno;
606  Datum value;
607  bool matches;
608  FmgrInfo *finfo;
609  uint32 hashValue;
610  BloomFilter *filter;
611  int keyno;
612 
613  filter = (BloomFilter *) PG_DETOAST_DATUM(column->bv_values[0]);
614 
615  Assert(filter);
616 
617  /*
618  * Assume all scan keys match. We'll be searching for a scan key
619  * eliminating the page range (we can stop on the first such key).
620  */
621  matches = true;
622 
623  for (keyno = 0; keyno < nkeys; keyno++)
624  {
625  ScanKey key = keys[keyno];
626 
627  /* NULL keys are handled and filtered-out in bringetbitmap */
628  Assert(!(key->sk_flags & SK_ISNULL));
629 
630  attno = key->sk_attno;
631  value = key->sk_argument;
632 
633  switch (key->sk_strategy)
634  {
636 
637  /*
638  * We want to return the current page range if the bloom
639  * filter seems to contain the value.
640  */
641  finfo = bloom_get_procinfo(bdesc, attno, PROCNUM_HASH);
642 
643  hashValue = DatumGetUInt32(FunctionCall1Coll(finfo, colloid, value));
644  matches &= bloom_contains_value(filter, hashValue);
645 
646  break;
647  default:
648  /* shouldn't happen */
649  elog(ERROR, "invalid strategy number %d", key->sk_strategy);
650  matches = false;
651  break;
652  }
653 
654  if (!matches)
655  break;
656  }
657 
658  PG_RETURN_BOOL(matches);
659 }
static bool bloom_contains_value(BloomFilter *filter, uint32 value)
Definition: brin_bloom.c:410
#define BloomEqualStrategyNumber
Definition: brin_bloom.c:136
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#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_BOOL, PROCNUM_HASH, SK_ISNULL, and value.

◆ brin_bloom_get_ndistinct()

static int brin_bloom_get_ndistinct ( BrinDesc bdesc,
BloomOptions opts 
)
static

Definition at line 500 of file brin_bloom.c.

501 {
502  double ndistinct;
503  double maxtuples;
504  BlockNumber pagesPerRange;
505 
506  pagesPerRange = BrinGetPagesPerRange(bdesc->bd_index);
507  ndistinct = BloomGetNDistinctPerRange(opts);
508 
509  Assert(BlockNumberIsValid(pagesPerRange));
510 
511  maxtuples = MaxHeapTuplesPerPage * pagesPerRange;
512 
513  /*
514  * Similarly to n_distinct, negative values are relative - in this case to
515  * maximum number of tuples in the page range (maxtuples).
516  */
517  if (ndistinct < 0)
518  ndistinct = (-ndistinct) * maxtuples;
519 
520  /*
521  * Positive values are to be used directly, but we still apply a couple of
522  * safeties to avoid using unreasonably small bloom filters.
523  */
524  ndistinct = Max(ndistinct, BLOOM_MIN_NDISTINCT_PER_RANGE);
525 
526  /*
527  * And don't use more than the maximum possible number of tuples, in the
528  * range, which would be entirely wasteful.
529  */
530  ndistinct = Min(ndistinct, maxtuples);
531 
532  return (int) ndistinct;
533 }
uint32 BlockNumber
Definition: block.h:31
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define BrinGetPagesPerRange(relation)
Definition: brin.h:40
#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:993
#define Max(x, y)
Definition: c.h:987
#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 453 of file brin_bloom.c.

454 {
455  BrinOpcInfo *result;
456 
457  /*
458  * opaque->strategy_procinfos is initialized lazily; here it is set to
459  * all-uninitialized by palloc0 which sets fn_oid to InvalidOid.
460  *
461  * bloom indexes only store the filter as a single BYTEA column
462  */
463 
464  result = palloc0(MAXALIGN(SizeofBrinOpcInfo(1)) +
465  sizeof(BloomOpaque));
466  result->oi_nstored = 1;
467  result->oi_regular_nulls = true;
468  result->oi_opaque = (BloomOpaque *)
469  MAXALIGN((char *) result + SizeofBrinOpcInfo(1));
470  result->oi_typcache[0] = lookup_type_cache(PG_BRIN_BLOOM_SUMMARYOID, 0);
471 
472  PG_RETURN_POINTER(result);
473 }
#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:345

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 745 of file brin_bloom.c.

746 {
748 
749  init_local_reloptions(relopts, sizeof(BloomOptions));
750 
751  add_local_real_reloption(relopts, "n_distinct_per_range",
752  "number of distinct items expected in a BRIN page range",
754  -1.0, INT_MAX, offsetof(BloomOptions, nDistinctPerRange));
755 
756  add_local_real_reloption(relopts, "false_positive_rate",
757  "desired false-positive rate for the bloom filters",
761  offsetof(BloomOptions, falsePositiveRate));
762 
763  PG_RETURN_VOID();
764 }
#define BLOOM_MAX_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:196
#define BLOOM_DEFAULT_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:197
#define BLOOM_MIN_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:195
#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 775 of file brin_bloom.c.

776 {
777  /*
778  * brin_bloom_summary stores the data in binary form and parsing text
779  * input is not needed, so disallow this.
780  */
781  ereport(ERROR,
782  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
783  errmsg("cannot accept a value of type %s", "pg_brin_bloom_summary")));
784 
785  PG_RETURN_VOID(); /* keep compiler quiet */
786 }
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#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 797 of file brin_bloom.c.

798 {
799  BloomFilter *filter;
801 
802  /* detoast the data to get value with a full 4B header */
804 
806  appendStringInfoChar(&str, '{');
807 
808  appendStringInfo(&str, "mode: hashed nhashes: %u nbits: %u nbits_set: %u",
809  filter->nhashes, filter->nbits, filter->nbits_set);
810 
811  appendStringInfoChar(&str, '}');
812 
813  PG_RETURN_CSTRING(str.data);
814 }
#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:97
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
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 821 of file brin_bloom.c.

822 {
823  ereport(ERROR,
824  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
825  errmsg("cannot accept a value of type %s", "pg_brin_bloom_summary")));
826 
827  PG_RETURN_VOID(); /* keep compiler quiet */
828 }

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

◆ brin_bloom_summary_send()

Datum brin_bloom_summary_send ( PG_FUNCTION_ARGS  )

Definition at line 838 of file brin_bloom.c.

839 {
840  return byteasend(fcinfo);
841 }
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:490

References byteasend().

◆ brin_bloom_union()

Datum brin_bloom_union ( PG_FUNCTION_ARGS  )

Definition at line 670 of file brin_bloom.c.

671 {
672  int i;
673  int nbytes;
674  BrinValues *col_a = (BrinValues *) PG_GETARG_POINTER(1);
675  BrinValues *col_b = (BrinValues *) PG_GETARG_POINTER(2);
676  BloomFilter *filter_a;
677  BloomFilter *filter_b;
678 
679  Assert(col_a->bv_attno == col_b->bv_attno);
680  Assert(!col_a->bv_allnulls && !col_b->bv_allnulls);
681 
682  filter_a = (BloomFilter *) PG_DETOAST_DATUM(col_a->bv_values[0]);
683  filter_b = (BloomFilter *) PG_DETOAST_DATUM(col_b->bv_values[0]);
684 
685  /* make sure the filters use the same parameters */
686  Assert(filter_a && filter_b);
687  Assert(filter_a->nbits == filter_b->nbits);
688  Assert(filter_a->nhashes == filter_b->nhashes);
689  Assert((filter_a->nbits > 0) && (filter_a->nbits % 8 == 0));
690 
691  nbytes = (filter_a->nbits) / 8;
692 
693  /* simply OR the bitmaps */
694  for (i = 0; i < nbytes; i++)
695  filter_a->data[i] |= filter_b->data[i];
696 
697  PG_RETURN_VOID();
698 }

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.