Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- BRIN minmax-multi op-class through the CFS storage manager.
--
-- A BRIN minmax-multi index stores several min/max intervals per page range
-- (not one), and compacts them when values_per_range is exceeded -- a
-- distinct write path from the single-interval minmax summary.  On a CFS
-- heap the summary pages live in sidecar-compressed storage; this test
-- checks that the build, compaction and bitmap-scan equality paths all
-- behave the same as a plain (uncompressed) heap.
--
-- Based on regress/sql/brin_multi.sql, reduced to the build/compaction/scan
-- paths that exercise the CFS storage manager.  The oracle is a plain
-- mirror table: CFS and plain must return identical scan results.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- A CFS heap plus a plain mirror used as the scan oracle.
CREATE TABLE cfsbrinm_cfs (id int, val int) WITH (algorithm = 'lz4');
CREATE TABLE cfsbrinm_plain (id int, val int);
 
-- Many distinct values within a range so the minmax-multi summary holds
-- several intervals and exceeds values_per_range during the insert path.
INSERT INTO cfsbrinm_cfs SELECT g, (g * 7) % 50 FROM generate_series(1, 3000) g;
INSERT INTO cfsbrinm_plain SELECT * FROM cfsbrinm_cfs;
 
-- minmax-multi with a low values_per_range to force compaction.
CREATE INDEX cfsbrinm_cfs_idx ON cfsbrinm_cfs USING brin
  (val int4_minmax_multi_ops(values_per_range = 16)) WITH (pages_per_range = 1);
CREATE INDEX cfsbrinm_plain_idx ON cfsbrinm_plain USING brin
  (val int4_minmax_multi_ops(values_per_range = 16)) WITH (pages_per_range = 1);
 
SELECT cfs_relation_policy('cfsbrinm_cfs_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('cfsbrinm_cfs_idx'::regclass) AS has_sidecars;
 
-- summarize/desummarize (compaction) after data is present.
SELECT brin_desummarize_range('cfsbrinm_cfs_idx', 0);
VACUUM cfsbrinm_cfs;
SELECT brin_summarize_range('cfsbrinm_cfs_idx', 0) AS resummarized;
 
-- Evict the compressed CFS BRIN pages so the scan reads summaries through
-- CFS decompression, not a buffer cache hit.
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_idx
  FROM pg_buffercache_evict_relation('cfsbrinm_cfs_idx'::regclass);
SELECT buffers_evicted > 0 AS evicted_tbl
  FROM pg_buffercache_evict_relation('cfsbrinm_cfs');
 
-- Equality scan: CFS bitmap result must equal the plain heap seqscan.
SET enable_seqscan TO off;
SELECT count(*) AS cfs_bitmap FROM cfsbrinm_cfs WHERE val = 21;
RESET enable_seqscan;
SELECT count(*) AS plain_seq FROM cfsbrinm_plain WHERE val = 21;
 
-- A range scan that exercises multiple intervals in the summary.
SET enable_seqscan TO off;
SELECT count(*) AS cfs_range FROM cfsbrinm_cfs WHERE val BETWEEN 10 AND 20;
RESET enable_seqscan;
SELECT count(*) AS plain_range FROM cfsbrinm_plain WHERE val BETWEEN 10 AND 20;
 
DROP TABLE cfsbrinm_cfs, cfsbrinm_plain;
DROP EXTENSION pg_buffercache;