From: Tomas Vondra Date: Wed, 16 Dec 2020 23:57:11 +0100 Subject: [PATCH 6/8] add sort_mode opclass parameter --- src/backend/access/brin/brin_bloom.c | 34 ++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c index ffeb459d3e..ac5a5c249c 100644 --- a/src/backend/access/brin/brin_bloom.c +++ b/src/backend/access/brin/brin_bloom.c @@ -180,6 +180,7 @@ typedef struct BloomOptions int32 vl_len_; /* varlena header (do not touch directly!) */ double nDistinctPerRange; /* number of distinct values per range */ double falsePositiveRate; /* false positive for bloom filter */ + bool sortMode; /* start in sort mode */ } BloomOptions; /* @@ -216,6 +217,7 @@ typedef struct BloomOptions #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 BLOOM_DEFAULT_SORT_MODE true /* start in sort */ #define BloomGetNDistinctPerRange(opts) \ ((opts) && (((BloomOptions *) (opts))->nDistinctPerRange != 0) ? \ @@ -227,6 +229,10 @@ typedef struct BloomOptions (((BloomOptions *) (opts))->falsePositiveRate) : \ BLOOM_DEFAULT_FALSE_POSITIVE_RATE) +#define BloomGetSortMode(opts) \ + ((opts) ? (((BloomOptions *) (opts))->sortMode) : \ + BLOOM_DEFAULT_SORT_MODE) + /* * Bloom Filter * @@ -302,7 +308,7 @@ static BloomFilter *bloom_switch_to_hashing(BloomFilter *filter); * varlena. */ static BloomFilter * -bloom_init(int ndistinct, double false_positive_rate) +bloom_init(bool sort_mode, int ndistinct, double false_positive_rate) { Size len; BloomFilter *filter; @@ -336,17 +342,26 @@ bloom_init(int ndistinct, double false_positive_rate) k = (k - floor(k) >= 0.5) ? ceil(k) : floor(k); /* - * Allocate the bloom filter (initially it's just a header, we'll make - * it larger as needed). + * When sort phase is enabled, allocate just the header - we'll make + * it larger as needed. In hash mode we allocate the whole filter. + * + * XXX We might add some sort of sparse bitmap, which might work for + * bloom filters with only a couple items. */ - len = offsetof(BloomFilter, data); + if (sort_mode) + len = offsetof(BloomFilter, data); + else + len = offsetof(BloomFilter, data) + (m / 8); filter = (BloomFilter *) palloc0(len); - filter->flags = 0; /* implies SORTED phase */ + filter->flags = 0; filter->nhashes = (int) k; filter->nbits = m; + if (!sort_mode) + filter->flags |= BLOOM_FLAG_PHASE_HASH; + SET_VARSIZE(filter, len); return filter; @@ -757,7 +772,6 @@ brin_bloom_get_ndistinct(BrinDesc *bdesc, BloomOptions *opts) return (int) ndistinct; } - /* * Examine the given index tuple (which contains partial status of a certain * page range) by comparing it to the given value that comes from another heap @@ -790,7 +804,8 @@ brin_bloom_add_value(PG_FUNCTION_ARGS) */ if (column->bv_allnulls) { - filter = bloom_init(brin_bloom_get_ndistinct(bdesc, opts), + filter = bloom_init(BloomGetSortMode(opts), + brin_bloom_get_ndistinct(bdesc, opts), BloomGetFalsePositiveRate(opts)); column->bv_values[0] = PointerGetDatum(filter); column->bv_allnulls = false; @@ -1022,6 +1037,11 @@ brin_bloom_options(PG_FUNCTION_ARGS) BLOOM_MAX_FALSE_POSITIVE_RATE, offsetof(BloomOptions, falsePositiveRate)); + add_local_bool_reloption(relopts, "sort_mode", + "start the bloom filter in sort mode", + BLOOM_DEFAULT_SORT_MODE, + offsetof(BloomOptions, sortMode)); + PG_RETURN_VOID(); } -- 2.26.2 --------------CF71AF65F7C337C37C24B045 Content-Type: text/x-patch; charset=UTF-8; name="0007-BRIN-minmax-multi-indexes-20210112.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0007-BRIN-minmax-multi-indexes-20210112.patch"