Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
-- ----------------------------------------------------------------------
-- CFS-compressed indexes inside a partitioned index tree.
--
-- A partitioned index (relkind 'I') carries no storage itself; the physical
-- indexes live on the leaf partitions.  The CFS-relevant question is which
-- CFS policy each *leaf* index ends up with, since that is the policy that
-- actually decides whether .cfp/.cfsmap/.cfsjrn sidecars are created and
-- whether index pages are compressed.
--
-- Per the index policy contract (QDB-1507): an index is a CFS relation
-- iff its own reloptions carry a policy.  The policy is written once, at
-- CREATE INDEX time -- from the index's own WITH clause, from the
-- partitioned index's reloptions for every leaf, or by inheritance from
-- the table the index is created on -- or later with an explicit
-- ALTER INDEX ... SET.  At runtime an index without a policy of its own
-- never follows the table it is on, however CFS that table is.
--
-- Based on regress/sql/indexing.sql, reduced to the partitioned-index cases
-- that exercise the CFS storage policy; a data-correctness index scan after
-- buffer eviction validates the compressed read path through the tree.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
-- =====================================================================
-- 1. CFS reloptions on the partitioned index propagate to every leaf,
--    regardless of whether the leaf partition tables are themselves CFS.
-- =====================================================================
CREATE TABLE cfsidx_part (a int, b text) PARTITION BY RANGE (a);
CREATE INDEX cfsidx_part_b_idx ON cfsidx_part (b)
  WITH (algorithm = 'lz4', level = 5);
-- A CFS partition: its leaf index inherits the parent index's CFS policy.
CREATE TABLE cfsidx_part_0 PARTITION OF cfsidx_part
  FOR VALUES FROM (0) TO (1000) WITH (algorithm = 'lz4');
-- A plain partition: its leaf index still inherits the parent index's CFS
-- policy (the partitioned index reloptions win over a plain heap).
CREATE TABLE cfsidx_part_1 PARTITION OF cfsidx_part
  FOR VALUES FROM (1000) TO (2000);
SELECT c.relname, c.relkind,
       cfs_relation_policy(c.oid) AS policy,
       cfs_relation_has_sidecars(c.oid) AS has_sidecars
  FROM pg_class c
  JOIN pg_index i ON c.oid = i.indexrelid
  WHERE i.indrelid IN ('cfsidx_part_0'::regclass, 'cfsidx_part_1'::regclass)
  ORDER BY c.relname;
       relname       | relkind |    policy     | has_sidecars 
---------------------+---------+---------------+--------------
 cfsidx_part_0_b_idx | i       | (t,lz4,5,512) | t
 cfsidx_part_1_b_idx | i       | (t,lz4,5,512) | t
(2 rows)
 
-- =====================================================================
-- 2. A plain partitioned index (no CFS reloptions) on CFS partitions:
--    each leaf index is created with a frozen copy of its own partition
--    table's policy (CREATE INDEX-time inheritance, not a runtime
--    fallback), and a leaf that already exists without a policy stays
--    plain even when its partition later turns CFS (QDB-1507).
-- =====================================================================
CREATE TABLE cfsidx_part2 (a int, b text) PARTITION BY RANGE (a);
CREATE TABLE cfsidx_part2_0 PARTITION OF cfsidx_part2
  FOR VALUES FROM (0) TO (1000) WITH (algorithm = 'zstd', level = 9);
-- No CFS reloptions on the partitioned index itself.
CREATE INDEX cfsidx_part2_b_idx ON cfsidx_part2 (b);
SELECT c.relname,
       cfs_relation_policy(c.oid) AS policy,
       cfs_relation_has_sidecars(c.oid) AS has_sidecars
  FROM pg_class c
  JOIN pg_index i ON c.oid = i.indexrelid
  WHERE i.indrelid = 'cfsidx_part2_0'::regclass;
       relname        |     policy     | has_sidecars 
----------------------+----------------+--------------
 cfsidx_part2_0_b_idx | (t,zstd,9,512) | t
(1 row)
 
-- The partitioned index carries no reloptions of its own (it has no
-- storage); the leaf's policy was frozen from the CFS partition at
-- CREATE INDEX time and lives in the leaf's own reloptions, so the leaf
-- stays CFS whatever happens to the partition afterwards.
SELECT reloptions FROM pg_class WHERE relname = 'cfsidx_part2_b_idx';
 reloptions 
------------
 
(1 row)
 
SELECT reloptions FROM pg_class WHERE relname = 'cfsidx_part2_0_b_idx';
        reloptions        
--------------------------
 {algorithm=zstd,level=9}
(1 row)
 
-- A plain partition attached under the same partitioned index: its leaf
-- is created without any policy (nothing to inherit), and turning the
-- partition CFS afterwards must NOT compress the already-existing leaf
-- (no runtime fallback to the table).
CREATE TABLE cfsidx_part2_1 PARTITION OF cfsidx_part2
  FOR VALUES FROM (1000) TO (2000);
SELECT c.relname,
       cfs_relation_policy(c.oid) AS policy,
       cfs_relation_has_sidecars(c.oid) AS has_sidecars
  FROM pg_class c
  JOIN pg_index i ON c.oid = i.indexrelid
  WHERE i.indrelid = 'cfsidx_part2_1'::regclass;
       relname        | policy | has_sidecars 
----------------------+--------+--------------
 cfsidx_part2_1_b_idx | (f,,,) | f
(1 row)
 
ALTER TABLE cfsidx_part2_1 SET (algorithm = 'lz4');
-- The partition is CFS now, but its leaf index stays plain forever.
SELECT cfs_relation_policy(c.oid) AS partition_policy
  FROM pg_class c WHERE c.relname = 'cfsidx_part2_1';
 partition_policy 
------------------
 (t,lz4,1,512)
(1 row)
 
SELECT c.relname,
       cfs_relation_policy(c.oid) AS policy,
       cfs_relation_has_sidecars(c.oid) AS has_sidecars
  FROM pg_class c
  JOIN pg_index i ON c.oid = i.indexrelid
  WHERE i.indrelid = 'cfsidx_part2_1'::regclass;
       relname        | policy | has_sidecars 
----------------------+--------+--------------
 cfsidx_part2_1_b_idx | (f,,,) | f
(1 row)
 
-- =====================================================================
-- 3. ATTACH PARTITION: an existing CFS index on the attached table is
--    reused (not duplicated) and keeps its own CFS policy.
-- =====================================================================
CREATE TABLE cfsidx_part3 (a int, b text) PARTITION BY RANGE (a);
CREATE INDEX cfsidx_part3_b_idx ON cfsidx_part3 (b)
  WITH (algorithm = 'lz4', level = 3);
-- A standalone table with its own differently-configured CFS index.
CREATE TABLE cfsidx_attach (a int, b text);
CREATE INDEX cfsidx_attach_b_idx ON cfsidx_attach (b)
  WITH (algorithm = 'zstd', level = 11);
ALTER TABLE cfsidx_part3 ATTACH PARTITION cfsidx_attach
  FOR VALUES FROM (0) TO (1000);
-- The pre-existing index is now attached to the partitioned index (no new
-- index was created) and still carries its own zstd/11 policy.
SELECT c.relname, inhparent::regclass AS attached_to,
       cfs_relation_policy(c.oid) AS policy
  FROM pg_class c
  LEFT JOIN pg_inherits h ON c.oid = h.inhrelid
  WHERE c.relname LIKE 'cfsidx_attach%' AND c.relkind = 'i'
  ORDER BY c.relname;
       relname       |    attached_to     |     policy      
---------------------+--------------------+-----------------
 cfsidx_attach_b_idx | cfsidx_part3_b_idx | (t,zstd,11,512)
(1 row)
 
-- Exactly one index on the attached partition (no duplicate).
SELECT count(*) AS indexes_on_attach
  FROM pg_index WHERE indrelid = 'cfsidx_attach'::regclass;
 indexes_on_attach 
-------------------
                 1
(1 row)
 
-- =====================================================================
-- 4. Data correctness: an index scan through a partitioned CFS index tree
--    must return exactly the right rows once the compressed leaf index
--    pages are evicted from shared_buffers, forcing the read through the
--    CFS decompression path rather than a buffer cache hit.
-- =====================================================================
CREATE TABLE cfsidx_data (k int, v text) PARTITION BY RANGE (k);
CREATE TABLE cfsidx_data_0 PARTITION OF cfsidx_data
  FOR VALUES FROM (0) TO (100000) WITH (algorithm = 'lz4');
CREATE TABLE cfsidx_data_1 PARTITION OF cfsidx_data
  FOR VALUES FROM (100000) TO (200000) WITH (algorithm = 'lz4');
-- Leaf indexes are created with a frozen copy of the CFS partitions'
-- policy at CREATE INDEX time (not a runtime fallback), so the scan below
-- reads CFS-compressed leaves through the tree.
CREATE INDEX cfsidx_data_v_idx ON cfsidx_data (v);
INSERT INTO cfsidx_data SELECT g, md5(g::text)
  FROM generate_series(1, 199999) g;
-- Both leaf indexes are CFS-compressed and carry sidecars.
SELECT count(*) AS cfs_leaf_indexes
  FROM pg_index i
  JOIN pg_class c ON c.oid = i.indexrelid
  WHERE i.indrelid IN ('cfsidx_data_0'::regclass, 'cfsidx_data_1'::regclass)
    AND cfs_relation_has_sidecars(c.oid);
 cfs_leaf_indexes 
------------------
                2
(1 row)
 
-- Force the compressed index pages out and evict the leaf indexes so the
-- scan below goes through smgrread()/decompression.
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_idx_0
  FROM pg_buffercache_evict_relation(
    (SELECT indexrelid::regclass FROM pg_index
       WHERE indrelid = 'cfsidx_data_0'::regclass));
 evicted_idx_0 
---------------
 t
(1 row)
 
SELECT buffers_evicted > 0 AS evicted_idx_1
  FROM pg_buffercache_evict_relation(
    (SELECT indexrelid::regclass FROM pg_index
       WHERE indrelid = 'cfsidx_data_1'::regclass));
 evicted_idx_1 
---------------
 t
(1 row)
 
-- An index-only path through the compressed leaves must return every row
-- with the exact value (the md5 acts as a self-checking oracle).
SET enable_seqscan TO off;
SELECT count(*) AS found,
       count(*) FILTER (WHERE v = md5(k::text)) AS correct
  FROM cfsidx_data WHERE v > '';
 found  | correct 
--------+---------
 199999 |  199999
(1 row)
 
RESET enable_seqscan;
-- A targeted index lookup (single key) must resolve through decompression.
SELECT k, v FROM cfsidx_data WHERE v = md5(42::text);
 k  |                v                 
----+----------------------------------
 42 | a1d0c6e83f027327d8461063f4ac58a6
(1 row)
 
-- =====================================================================
-- 5. ALTER INDEX ... SET/RESET (algorithm) on the partitioned index head
--    is not a supported operation at all: the Phase 1 permissions check
--    rejects reloptions ALTERs on partitioned indexes, so nothing must be
--    rebuilt or silently skipped, and the existing leaf policies must stay
--    exactly as they were.
-- =====================================================================
CREATE TABLE cfsidx_part4 (a int, b text) PARTITION BY RANGE (a);
CREATE INDEX cfsidx_part4_b_idx ON cfsidx_part4 (b)
  WITH (algorithm = 'lz4', level = 3);
CREATE TABLE cfsidx_part4_0 PARTITION OF cfsidx_part4
  FOR VALUES FROM (0) TO (1000);
ALTER INDEX cfsidx_part4_b_idx SET (algorithm = 'zstd');
ERROR:  ALTER action SET cannot be performed on relation "cfsidx_part4_b_idx"
DETAIL:  This operation is not supported for partitioned indexes.
ALTER INDEX cfsidx_part4_b_idx RESET (algorithm);
ERROR:  ALTER action RESET cannot be performed on relation "cfsidx_part4_b_idx"
DETAIL:  This operation is not supported for partitioned indexes.
-- The head keeps its original reloptions (both ALTERs were rejected).
SELECT reloptions FROM pg_class WHERE oid = 'cfsidx_part4_b_idx'::regclass;
       reloptions        
-------------------------
 {algorithm=lz4,level=3}
(1 row)
 
-- The leaf index keeps the lz4 policy inherited from the head.
SELECT cfs_relation_policy(c.oid) AS policy,
       cfs_relation_has_sidecars(c.oid) AS has_sidecars
  FROM pg_class c
  JOIN pg_index i ON c.oid = i.indexrelid
  WHERE i.indrelid = 'cfsidx_part4_0'::regclass;
    policy     | has_sidecars 
---------------+--------------
 (t,lz4,3,512) | t
(1 row)
 
DROP TABLE cfsidx_part, cfsidx_part2, cfsidx_part3, cfsidx_data, cfsidx_part4;
DROP EXTENSION pg_buffercache;