Исходный код СУБД 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-- ----------------------------------------------------------------------
-- BRIN index through the CFS storage manager.
--
-- A BRIN index built on a CFS heap inherits the heap's compression policy
-- (iface_sql: an index with no explicit reloptions takes the policy of its
-- table), so every BRIN page -- the revmap, the regular summary tuples --
-- lives in sidecar-compressed storage and is read back through the CFS
-- decompression path on smgrread().  This test covers:
--
--   1. build (serial and parallel) producing a CFS BRIN with sidecars;
--   2. UNLOGGED being rejected for CFS (so brinbuildempty is never a CFS
--      path here);
--   3. summarize/desummarize after data growth on a CFS BRIN;
--   4. bitmap-scan correctness after evicting the compressed BRIN pages,
--      forcing the revmap/summary reads through decompression;
--   5. BRIN detoasting of out-of-line summary values through the table's
--      own (CFS) TOAST relation.
--
-- Based on regress/sql/brin.sql, reduced to the build/scan/summarize paths
-- that exercise the CFS storage manager.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- =====================================================================
-- 1. Serial BRIN build on a CFS heap: the index inherits CFS and carries
--    sidecars.
-- =====================================================================
CREATE TABLE cfsbrin_t (a int, b text) WITH (algorithm = 'lz4');
INSERT INTO cfsbrin_t SELECT g, md5(g::text) FROM generate_series(1, 500) g;
CREATE INDEX cfsbrin_t_idx ON cfsbrin_t USING brin (a) WITH (pages_per_range = 1);
 
SELECT cfs_relation_policy('cfsbrin_t_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('cfsbrin_t_idx'::regclass) AS has_sidecars;
 
-- =====================================================================
-- 2. UNLOGGED is incompatible with CFS: the init-fork / brinbuildempty
--    path is never exercised on compressed storage.
-- =====================================================================
CREATE UNLOGGED TABLE cfsbrin_unlogged (a int) WITH (algorithm = 'lz4');
 
-- =====================================================================
-- 3. Parallel BRIN build on a CFS heap: a parallel maintenance build goes
--    through the same CFS smgr write path and yields a CFS BRIN.
-- =====================================================================
CREATE TABLE cfsbrin_par (a int) WITH (algorithm = 'lz4');
INSERT INTO cfsbrin_par SELECT g FROM generate_series(1, 100000) g;
SET min_parallel_table_scan_size = 0;
SET max_parallel_maintenance_workers = 4;
CREATE INDEX cfsbrin_par_idx ON cfsbrin_par USING brin (a);
RESET min_parallel_table_scan_size;
RESET max_parallel_maintenance_workers;
 
SELECT cfs_relation_policy('cfsbrin_par_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('cfsbrin_par_idx'::regclass) AS has_sidecars;
 
-- =====================================================================
-- 4. Summarize/desummarize after data growth, then bitmap-scan
--    correctness through the compressed BRIN.
-- =====================================================================
INSERT INTO cfsbrin_t SELECT g, md5(g::text) FROM generate_series(501, 1500) g;
 
-- Desummarize a range and let VACUUM resummarize through CFS.
SELECT brin_desummarize_range('cfsbrin_t_idx', 0);
VACUUM cfsbrin_t;
SELECT brin_summarize_range('cfsbrin_t_idx', 0) AS resummarized_blocks;
 
-- Evict both the compressed BRIN pages and the heap so the bitmap scan
-- below resolves every revmap/summary reference through smgrread()/CFS
-- decompression rather than a buffer cache hit.
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_idx
  FROM pg_buffercache_evict_relation('cfsbrin_t_idx'::regclass);
SELECT buffers_evicted > 0 AS evicted_tbl
  FROM pg_buffercache_evict_relation('cfsbrin_t');
 
-- A BRIN bitmap scan is lossy (a range hint), so compare its result set
-- against a plain seqscan on the same predicate: the bitmap scan must not
-- miss or invent any rows after reading compressed summaries.
SET enable_seqscan TO off;
SET enable_bitmapscan TO on;
SELECT count(*) AS bitmap_matches FROM cfsbrin_t WHERE a BETWEEN 100 AND 200;
RESET enable_seqscan;
SELECT count(*) AS seq_matches FROM cfsbrin_t WHERE a BETWEEN 100 AND 200;
 
-- =====================================================================
-- 5. BRIN summary detoasting through the heap's own CFS TOAST relation.
--    Long varlena summary values are pushed out-of-line; the BRIN code
--    detoasts them when summarizing, which on a CFS heap means reading
--    compressed TOAST pages.
-- =====================================================================
CREATE TABLE cfsbrin_toast (a text, b text) WITH (algorithm = 'lz4');
 
-- Confirm the heap's TOAST inherited CFS, so detoast below is a CFS read.
SELECT cfs_relation_policy((SELECT reltoastrelid::regclass
                              FROM pg_class WHERE oid = 'cfsbrin_toast'::regclass))
  AS toast_policy;
 
-- A long, non-repeating value (~1920 bytes from 60 distinct md5 hashes):
-- large enough to be pushed out-of-line to the (CFS) TOAST relation, and
-- not compressible to nothing by pglz.
WITH rv AS (SELECT string_agg(md5(i::text), '') AS val FROM generate_series(1, 60) i)
INSERT INTO cfsbrin_toast SELECT val, val FROM rv;
CREATE INDEX cfsbrin_toast_idx ON cfsbrin_toast USING brin (b);
 
-- Evict the TOAST relation and the BRIN so any summary detoast on a later
-- summarize/scan goes through decompression.
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_toast
  FROM pg_buffercache_evict_relation(
    (SELECT reltoastrelid FROM pg_class WHERE oid = 'cfsbrin_toast'::regclass));
SELECT buffers_evicted > 0 AS evicted_idx
  FROM pg_buffercache_evict_relation('cfsbrin_toast_idx'::regclass);
 
-- A forced resummarize reads the row, detoasting b through the CFS TOAST.
SELECT brin_desummarize_range('cfsbrin_toast_idx', 0);
SELECT brin_summarize_range('cfsbrin_toast_idx', 0) AS toast_summary_blocks;
 
-- The data itself is intact and accessible.
SELECT a IS NOT NULL AND b IS NOT NULL AS ok FROM cfsbrin_toast;
 
DROP TABLE cfsbrin_t, cfsbrin_par, cfsbrin_toast;
DROP EXTENSION pg_buffercache;