PostgreSQL Source Code git master
brin_bloom.c File Reference
#include "postgres.h"
#include <limits.h>
#include <math.h>
#include "access/brin.h"
#include "access/brin_internal.h"
#include "access/brin_page.h"
#include "access/brin_tuple.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/reloptions.h"
#include "catalog/pg_am.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "port/pg_bitutils.h"
#include "utils/fmgrprotos.h"
#include "utils/rel.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 195 of file brin_bloom.c.

◆ BLOOM_DEFAULT_NDISTINCT_PER_RANGE

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

Definition at line 177 of file brin_bloom.c.

◆ BLOOM_MAX_FALSE_POSITIVE_RATE

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

Definition at line 194 of file brin_bloom.c.

◆ BLOOM_MAX_PROCNUMS

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

Definition at line 143 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 193 of file brin_bloom.c.

◆ BLOOM_MIN_NDISTINCT_PER_RANGE

#define BLOOM_MIN_NDISTINCT_PER_RANGE   16

Definition at line 170 of file brin_bloom.c.

◆ BLOOM_SEED_1

#define BLOOM_SEED_1   0x71d924af

Definition at line 223 of file brin_bloom.c.

◆ BLOOM_SEED_2

#define BLOOM_SEED_2   0xba48b314

Definition at line 224 of file brin_bloom.c.

◆ BloomEqualStrategyNumber

#define BloomEqualStrategyNumber   1

Definition at line 134 of file brin_bloom.c.

◆ BloomGetFalsePositiveRate

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

Definition at line 202 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:177

Definition at line 197 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:216
#define MAXALIGN_DOWN(LEN)
Definition: c.h:825
#define MAXALIGN(LEN)
Definition: c.h:813

Definition at line 212 of file brin_bloom.c.

◆ PROCNUM_BASE

#define PROCNUM_BASE   11

Definition at line 150 of file brin_bloom.c.

◆ PROCNUM_HASH

#define PROCNUM_HASH   11 /* required */

Definition at line 144 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 371 of file brin_bloom.c.

372{
373 int i;
374 uint64 h1,
375 h2;
376
377 /* compute the hashes, used for the bloom filter */
380
381 /* compute the requested number of hashes */
382 for (i = 0; i < filter->nhashes; i++)
383 {
384 /* h1 + h2 + f(i) */
385 uint32 h = (h1 + i * h2) % filter->nbits;
386 uint32 byte = (h / 8);
387 uint32 bit = (h % 8);
388
389 /* if the bit is not set, set it and remember we did that */
390 if (!(filter->data[byte] & (0x01 << bit)))
391 {
392 filter->data[byte] |= (0x01 << bit);
393 filter->nbits_set++;
394 if (updated)
395 *updated = true;
396 }
397 }
398
399 return filter;
400}
#define BLOOM_SEED_1
Definition: brin_bloom.c:223
#define BLOOM_SEED_2
Definition: brin_bloom.c:224
uint64_t uint64
Definition: c.h:542
uint32_t uint32
Definition: c.h:541
uint64 hash_bytes_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.c:631
static struct @171 value
int i
Definition: isn.c:77
uint8 nhashes
Definition: brin_bloom.c:252
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_bloom.c:257
uint32 nbits_set
Definition: brin_bloom.c:254
uint32 nbits
Definition: brin_bloom.c:253
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 408 of file brin_bloom.c.

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

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

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

Referenced by bloom_init().

◆ bloom_get_procinfo()

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

Definition at line 718 of file brin_bloom.c.

719{
720 BloomOpaque *opaque;
721 uint16 basenum = procnum - PROCNUM_BASE;
722
723 /*
724 * We cache these in the opaque struct, to avoid repetitive syscache
725 * lookups.
726 */
727 opaque = (BloomOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
728
729 if (opaque->extra_procinfos[basenum].fn_oid == InvalidOid)
730 {
732 procnum)))
733 fmgr_info_copy(&opaque->extra_procinfos[basenum],
734 index_getprocinfo(bdesc->bd_index, attno, procnum),
735 bdesc->bd_context);
736 else
738 errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
739 errmsg_internal("invalid opclass definition"),
740 errdetail_internal("The operator class is missing support function %d for column %d.",
741 procnum, attno));
742 }
743
744 return &opaque->extra_procinfos[basenum];
745}
#define PROCNUM_BASE
Definition: brin_bloom.c:150
#define RegProcedureIsValid(p)
Definition: c.h:779
uint16_t uint16
Definition: c.h:540
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1170
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1243
int errcode(int sqlerrcode)
Definition: elog.c:863
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:581
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:917
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:883
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
#define InvalidOid
Definition: postgres_ext.h:37
FmgrInfo extra_procinfos[BLOOM_MAX_PROCNUMS]
Definition: brin_bloom.c:442
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, ereport, errcode(), errdetail_internal(), errmsg_internal(), ERROR, 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 311 of file brin_bloom.c.

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

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

541{
542 BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
543 BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
547 Oid colloid = PG_GET_COLLATION();
548 FmgrInfo *hashFn;
549 uint32 hashValue;
550 bool updated = false;
551 AttrNumber attno;
552 BloomFilter *filter;
553
554 Assert(!isnull);
555
556 attno = column->bv_attno;
557
558 /*
559 * If this is the first non-null value, we need to initialize the bloom
560 * filter. Otherwise just extract the existing bloom filter from
561 * BrinValues.
562 */
563 if (column->bv_allnulls)
564 {
567 column->bv_values[0] = PointerGetDatum(filter);
568 column->bv_allnulls = false;
569 updated = true;
570 }
571 else
572 filter = (BloomFilter *) PG_DETOAST_DATUM(column->bv_values[0]);
573
574 /*
575 * Compute the hash of the new value, using the supplied hash function,
576 * and then add the hash value to the bloom filter.
577 */
578 hashFn = bloom_get_procinfo(bdesc, attno, PROCNUM_HASH);
579
580 hashValue = DatumGetUInt32(FunctionCall1Coll(hashFn, colloid, newval));
581
582 filter = bloom_add_value(filter, hashValue, &updated);
583
584 column->bv_values[0] = PointerGetDatum(filter);
585
586 PG_RETURN_BOOL(updated);
587}
int16 AttrNumber
Definition: attnum.h:21
static int brin_bloom_get_ndistinct(BrinDesc *bdesc, BloomOptions *opts)
Definition: brin_bloom.c:497
#define PROCNUM_HASH
Definition: brin_bloom.c:144
static BloomFilter * bloom_init(int ndistinct, double false_positive_rate)
Definition: brin_bloom.c:311
#define BloomGetFalsePositiveRate(opts)
Definition: brin_bloom.c:202
static FmgrInfo * bloom_get_procinfo(BrinDesc *bdesc, uint16 attno, uint16 procnum)
Definition: brin_bloom.c:718
static BloomFilter * bloom_add_value(BloomFilter *filter, uint32 value, bool *updated)
Definition: brin_bloom.c:371
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:228
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1130
#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_GETARG_BOOL(n)
Definition: fmgr.h:274
#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:232
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
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_BOOL, 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 595 of file brin_bloom.c.

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

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

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

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

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

749{
751
752 init_local_reloptions(relopts, sizeof(BloomOptions));
753
754 add_local_real_reloption(relopts, "n_distinct_per_range",
755 "number of distinct items expected in a BRIN page range",
757 -1.0, INT_MAX, offsetof(BloomOptions, nDistinctPerRange));
758
759 add_local_real_reloption(relopts, "false_positive_rate",
760 "desired false-positive rate for the bloom filters",
764 offsetof(BloomOptions, falsePositiveRate));
765
767}
#define BLOOM_MAX_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:194
#define BLOOM_MIN_FALSE_POSITIVE_RATE
Definition: brin_bloom.c:193
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition: reloptions.c:754
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:992

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

779{
780 /*
781 * brin_bloom_summary stores the data in binary form and parsing text
782 * input is not needed, so disallow this.
783 */
785 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
786 errmsg("cannot accept a value of type %s", "pg_brin_bloom_summary")));
787
788 PG_RETURN_VOID(); /* keep compiler quiet */
789}
int errmsg(const char *fmt,...)
Definition: elog.c:1080

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

◆ brin_bloom_summary_out()

Datum brin_bloom_summary_out ( PG_FUNCTION_ARGS  )

Definition at line 800 of file brin_bloom.c.

801{
802 BloomFilter *filter;
804
805 /* detoast the data to get value with a full 4B header */
807
810
811 appendStringInfo(&str, "mode: hashed nhashes: %u nbits: %u nbits_set: %u",
812 filter->nhashes, filter->nbits, filter->nbits_set);
813
815
817}
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
const char * str
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97

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

◆ brin_bloom_summary_recv()

Datum brin_bloom_summary_recv ( PG_FUNCTION_ARGS  )

Definition at line 824 of file brin_bloom.c.

825{
827 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
828 errmsg("cannot accept a value of type %s", "pg_brin_bloom_summary")));
829
830 PG_RETURN_VOID(); /* keep compiler quiet */
831}

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

◆ brin_bloom_summary_send()

Datum brin_bloom_summary_send ( PG_FUNCTION_ARGS  )

Definition at line 841 of file brin_bloom.c.

842{
843 return byteasend(fcinfo);
844}
Datum byteasend(PG_FUNCTION_ARGS)
Definition: bytea.c:363

References byteasend().

◆ brin_bloom_union()

Datum brin_bloom_union ( PG_FUNCTION_ARGS  )

Definition at line 667 of file brin_bloom.c.

668{
669 int i;
670 int nbytes;
673 BloomFilter *filter_a;
674 BloomFilter *filter_b;
675
676 Assert(col_a->bv_attno == col_b->bv_attno);
677 Assert(!col_a->bv_allnulls && !col_b->bv_allnulls);
678
679 filter_a = (BloomFilter *) PG_DETOAST_DATUM(col_a->bv_values[0]);
680 filter_b = (BloomFilter *) PG_DETOAST_DATUM(col_b->bv_values[0]);
681
682 /* make sure the filters use the same parameters */
683 Assert(filter_a && filter_b);
684 Assert(filter_a->nbits == filter_b->nbits);
685 Assert(filter_a->nhashes == filter_b->nhashes);
686 Assert((filter_a->nbits > 0) && (filter_a->nbits % 8 == 0));
687
688 nbytes = (filter_a->nbits) / 8;
689
690 /* simply OR the bitmaps */
691 for (i = 0; i < nbytes; i++)
692 filter_a->data[i] |= filter_b->data[i];
693
694 /* update the number of bits set in the filter */
695 filter_a->nbits_set = pg_popcount((const char *) filter_a->data, nbytes);
696
697 /* if we decompressed filter_a, update the summary */
698 if (PointerGetDatum(filter_a) != col_a->bv_values[0])
699 {
700 pfree(DatumGetPointer(col_a->bv_values[0]));
701 col_a->bv_values[0] = PointerGetDatum(filter_a);
702 }
703
704 /* also free filter_b, if it was decompressed */
705 if (PointerGetDatum(filter_b) != col_b->bv_values[0])
706 pfree(filter_b);
707
709}
void pfree(void *pointer)
Definition: mcxt.c:1594
static uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.h:363
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322

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