Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- ----------------------------------------------------------------------
-- BRIN bloom op-class through the CFS storage manager.
--
-- A BRIN bloom index uses a bloom filter (a varlena summary page, larger
-- than a minmax summary) to answer equality probes.  The CFS-relevant
-- questions are the same shape as the minmax BRIN test (cfs_brin.sql) but
-- exercise the bloom-specific code paths:
--
--   1. bloom op-class reloptions (n_distinct_per_range, false_positive_rate)
--      coexist with the CFS reloptions inherited from a CFS heap in one
--      reloptions list;
--   2. build produces a CFS-compressed bloom index with sidecars;
--   3. summarize/desummarize after data growth on the CFS bloom index;
--   4. bitmap-scan equality correctness (and no false positives) after
--      evicting the compressed bloom pages, forcing filter lookups through
--      decompression;
--   5. a bloom index on a *plain* table with its own explicit CFS reloptions
--      is itself CFS (rule 1 of iface_sql "CREATE INDEX");
--   6. bloom op-class validation still fires when CFS reloptions are present.
--
-- Based on regress/sql/brin_bloom.sql, reduced to the build/scan/summarize
-- paths that exercise the CFS storage manager.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- =====================================================================
-- 1. A BRIN bloom index on a CFS heap: bloom op-class reloptions and the
--    inherited CFS reloptions both end up in the index's reloptions list.
-- =====================================================================
CREATE TABLE cfsbloom_t (id int, val int) WITH (algorithm = 'lz4');
INSERT INTO cfsbloom_t SELECT g, g * 7 FROM generate_series(1, 2000) g;
 
CREATE INDEX cfsbloom_t_idx ON cfsbloom_t USING brin
  (val int4_bloom_ops(n_distinct_per_range = 100, false_positive_rate = 0.01))
  WITH (pages_per_range = 8);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'cfsbloom_t_idx'::regclass))
  ORDER BY option_name;
SELECT cfs_relation_policy('cfsbloom_t_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('cfsbloom_t_idx'::regclass) AS has_sidecars;
 
-- =====================================================================
-- 2. summarize/desummarize after data growth on the CFS bloom index.
-- =====================================================================
INSERT INTO cfsbloom_t SELECT g, g * 7 FROM generate_series(2001, 3000) g;
SELECT brin_desummarize_range('cfsbloom_t_idx', 0);
VACUUM cfsbloom_t;
SELECT brin_summarize_range('cfsbloom_t_idx', 0) AS resummarized_blocks;
 
-- =====================================================================
-- 3. Bitmap-scan equality correctness and false-positive control through
--    the compressed bloom filter, after evicting it.
-- =====================================================================
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_idx
  FROM pg_buffercache_evict_relation('cfsbloom_t_idx'::regclass);
SELECT buffers_evicted > 0 AS evicted_tbl
  FROM pg_buffercache_evict_relation('cfsbloom_t');
 
-- Equality on a value that exists: bitmap scan and seq scan agree.
SET enable_seqscan TO off;
SELECT count(*) AS bitmap_present FROM cfsbloom_t WHERE val = 7007;
RESET enable_seqscan;
SELECT count(*) AS seq_present FROM cfsbloom_t WHERE val = 7007;
 
-- Equality on a value that does not exist: the bloom filter may produce a
-- false range hint, but the bitmap heap recheck must filter it out to zero
-- rows -- the CFS decompression of the filter must not corrupt the lookup.
SET enable_seqscan TO off;
SELECT count(*) AS bitmap_absent FROM cfsbloom_t WHERE val = 999999;
RESET enable_seqscan;
SELECT count(*) AS seq_absent FROM cfsbloom_t WHERE val = 999999;
 
-- =====================================================================
-- 4. A bloom index on a *plain* table with its own explicit CFS reloptions
--    is itself a CFS index (the index's explicit reloptions win, rule 1 of
--    iface_sql "CREATE INDEX").
-- =====================================================================
CREATE TABLE cfsbloom_plain (id int, val int);
CREATE INDEX cfsbloom_plain_idx ON cfsbloom_plain USING brin
  (val int4_bloom_ops)
  WITH (algorithm = 'zstd', level = 7, pages_per_range = 4);
 
SELECT cfs_relation_policy('cfsbloom_plain_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('cfsbloom_plain_idx'::regclass) AS has_sidecars;
 
-- =====================================================================
-- 5. Bloom op-class validation still fires with CFS reloptions present.
-- =====================================================================
CREATE INDEX cfsbloom_bad ON cfsbloom_t USING brin
  (val int4_bloom_ops(n_distinct_per_range = -2));
 
DROP TABLE cfsbloom_t, cfsbloom_plain;
DROP EXTENSION pg_buffercache;