Исходный код СУБД 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- ----------------------------------------------------------------------
-- SQL reloptions, GUC and relation predicate
-- ----------------------------------------------------------------------
-- GUC defaults (per-algorithm level GUCs).
SHOW cfs_default_lz4_level;
 cfs_default_lz4_level 
-----------------------
 1
(1 row)
 
SHOW cfs_default_zstd_level;
 cfs_default_zstd_level 
------------------------
 3
(1 row)
 
SHOW cfs_default_min_gain_bytes;
 cfs_default_min_gain_bytes 
----------------------------
 512
(1 row)
 
-- A CFS relation enabled by the "algorithm" reloption (defaults from GUC).
CREATE TABLE cfs_lz4_t (id int, payload text) WITH (algorithm = 'lz4');
SELECT * FROM cfs_relation_policy('cfs_lz4_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     1 |            512
(1 row)
 
-- LZ4 supports levels too (1..12).  Explicit level for lz4 is valid.
CREATE TABLE cfs_lz4_lvl (id int, payload text) WITH (algorithm = 'lz4', level = 9);
SELECT * FROM cfs_relation_policy('cfs_lz4_lvl');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     9 |            512
(1 row)
 
-- Explicit level / min_gain_bytes for zstd.
CREATE TABLE cfs_zstd_t (id int, payload text)
  WITH (algorithm = 'zstd', level = 3, min_gain_bytes = 128);
SELECT * FROM cfs_relation_policy('cfs_zstd_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | zstd      |     3 |            128
(1 row)
 
-- GUC fallback: no explicit level / min_gain_bytes -> the per-algorithm
-- GUC values for this session.
SET cfs_default_lz4_level = 5;
SET cfs_default_min_gain_bytes = 64;
CREATE TABLE cfs_guc_t (id int, payload text) WITH (algorithm = 'lz4');
SELECT * FROM cfs_relation_policy('cfs_guc_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     5 |             64
(1 row)
 
RESET cfs_default_lz4_level;
RESET cfs_default_min_gain_bytes;
-- The generic cfs_default_level GUC is gone: the per-algorithm GUCs
-- replaced it.
SET cfs_default_level = 5;
ERROR:  unrecognized configuration parameter "cfs_default_level"
-- A plain table is not a CFS relation.
CREATE TABLE plain_t (id int, payload text);
SELECT * FROM cfs_relation_policy('plain_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
-- TOAST relation inherits the parent table's CFS policy.
SELECT * FROM cfs_relation_policy(
  (SELECT reltoastrelid FROM pg_class WHERE oid = 'cfs_lz4_t'::regclass));
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     1 |            512
(1 row)
 
-- An index on a CFS table inherits the table policy.
CREATE INDEX cfs_lz4_idx ON cfs_lz4_t (id);
SELECT * FROM cfs_relation_policy('cfs_lz4_idx');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     1 |            512
(1 row)
 
-- An index on a plain table is not CFS.
CREATE INDEX plain_idx ON plain_t (id);
SELECT * FROM cfs_relation_policy('plain_idx');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
-- A partitioned table stores a default policy but is not itself physical CFS.
CREATE TABLE part_t (id int, payload text) PARTITION BY RANGE (id)
  WITH (algorithm = 'lz4');
SELECT * FROM cfs_relation_policy('part_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
-- "algorithm" is required for "level" / "min_gain_bytes".
CREATE TABLE bad_level_t (id int) WITH (level = 3);
ERROR:  CFS compression algorithm must be specified
DETAIL:  Option "level" requires option "algorithm".
-- Only "lz4" / "zstd" are accepted; "raw" is an internal mode.
CREATE TABLE bad_raw_t (id int) WITH (algorithm = 'raw');
ERROR:  invalid value for enum option "algorithm": raw
DETAIL:  Valid values are: "lz4", "zstd".
-- TEMP and UNLOGGED relations cannot use CFS.
CREATE TEMP TABLE bad_temp_t (id int) WITH (algorithm = 'lz4');
ERROR:  CFS is not supported for temporary tables
CREATE UNLOGGED TABLE bad_unlogged_t (id int) WITH (algorithm = 'lz4');
ERROR:  CFS is not supported for unlogged tables
-- Non-CFS storage parameters are still rejected on partitioned tables.
CREATE TABLE bad_part_t (id int) PARTITION BY RANGE (id) WITH (fillfactor = 50);
ERROR:  cannot specify storage parameters for a partitioned table
HINT:  Specify storage parameters for its leaf partitions instead.
-- Per-algorithm level range: LZ4 max is 12.
CREATE TABLE bad_lz4_level (id int) WITH (algorithm = 'lz4', level = 15);
ERROR:  CFS compression level 15 is out of range for algorithm "lz4"
DETAIL:  Valid levels for algorithm "lz4" are 1..12.
-- Per-algorithm level range: Zstd max is 22.
CREATE TABLE bad_zstd_level (id int) WITH (algorithm = 'zstd', level = 25);
ERROR:  value 25 out of bounds for option "level"
DETAIL:  Valid values are between "1" and "22".
-- CFS options in the "toast" namespace are rejected: a TOAST relation has
-- no independent CFS policy and always inherits from the main table.
CREATE TABLE bad_toast_algo (a text) WITH (toast.algorithm = 'lz4');
ERROR:  CFS compression options cannot be specified for the TOAST table
DETAIL:  The TOAST table compression policy is inherited from the main table.
CREATE TABLE bad_toast_level (a text) WITH (toast.level = 5);
ERROR:  CFS compression options cannot be specified for the TOAST table
DETAIL:  The TOAST table compression policy is inherited from the main table.
CREATE TABLE bad_toast_mgb (a text) WITH (toast.min_gain_bytes = 128);
ERROR:  CFS compression options cannot be specified for the TOAST table
DETAIL:  The TOAST table compression policy is inherited from the main table.
-- Even when the main table is itself CFS, toast.* CFS options are rejected.
CREATE TABLE bad_toast_algo2 (a text) WITH (algorithm = 'lz4', toast.algorithm = 'zstd');
ERROR:  CFS compression options cannot be specified for the TOAST table
DETAIL:  The TOAST table compression policy is inherited from the main table.
-- CTAS path is also covered.
CREATE TABLE bad_toast_ctas WITH (toast.algorithm = 'lz4') AS SELECT 1::text;
ERROR:  CFS compression options cannot be specified for the TOAST table
DETAIL:  The TOAST table compression policy is inherited from the main table.
-- ----------------------------------------------------------------------
-- ALTER TABLE reloptions validation (catalog update only;
-- physical rewrite is a separate step).
-- ----------------------------------------------------------------------
-- zstd supports level changes without a rewrite (catalog update only).
ALTER TABLE cfs_zstd_t SET (level = 9);
SELECT * FROM cfs_relation_policy('cfs_zstd_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | zstd      |     9 |            128
(1 row)
 
-- lz4 supports level changes too.
ALTER TABLE cfs_lz4_t SET (level = 6);
SELECT * FROM cfs_relation_policy('cfs_lz4_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     6 |            512
(1 row)
 
-- min_gain_bytes can always be changed.
ALTER TABLE cfs_lz4_t SET (min_gain_bytes = 256);
SELECT * FROM cfs_relation_policy('cfs_lz4_t');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 t      | lz4       |     6 |            256
(1 row)
 
-- LZ4 level above 12 must error.
ALTER TABLE cfs_lz4_t SET (level = 15);
ERROR:  CFS compression level 15 is out of range for algorithm "lz4"
DETAIL:  Valid levels for algorithm "lz4" are 1..12.
-- "level" without "algorithm" must error.
ALTER TABLE plain_t SET (level = 5);
ERROR:  CFS compression algorithm must be specified
DETAIL:  Option "level" requires option "algorithm".
-- CFS options in the "toast" namespace are also rejected on ALTER TABLE.
ALTER TABLE plain_t SET (toast.algorithm = 'lz4');
ERROR:  CFS compression options cannot be specified for the TOAST table
DETAIL:  The TOAST table compression policy is inherited from the main table.
DROP TABLE cfs_lz4_t, cfs_lz4_lvl, cfs_zstd_t, cfs_guc_t, plain_t, part_t;
-- ----------------------------------------------------------------------
-- Relations whose rd_options is NOT StdRdOptions (views, composite types,
-- sequences, foreign tables) must never be cast to StdRdOptions: that would
-- read unrelated memory and yield a random CFS policy.  All CFS information
-- functions must return is_cfs=false with zeroed fields and never ERROR.
-- See: CfsEffectiveOptionsFromRelation / RelationUsesCfs relkind gating.
-- ----------------------------------------------------------------------
-- A view carries ViewOptions (security_barrier / check_option / ...), no CFS
-- fields at all.  Casting its rd_options to StdRdOptions would read
-- garbage and produce a random, run-to-run-varying policy.
CREATE VIEW cfs_v AS SELECT 1 AS a;
CREATE OR REPLACE VIEW cfs_v WITH (security_barrier = true) AS SELECT 1 AS a;
SELECT * FROM cfs_relation_policy('cfs_v');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes,
       is_toast, is_index, is_partitioned_root, is_supported
  FROM pg_cfs_relation_info('cfs_v');
 relkind | is_cfs | algorithm | level | min_gain_bytes | is_toast | is_index | is_partitioned_root | is_supported 
---------+--------+-----------+-------+----------------+----------+----------+---------------------+--------------
 v       | f      |           |       |                | f        | f        | f                   | f
(1 row)
 
SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_v');
 ok | is_cfs | errors |       details       
----+--------+--------+---------------------
 t  | f      |      0 | relation is not CFS
(1 row)
 
-- security_invoker is another ViewOptions field; exercise it too.
CREATE OR REPLACE VIEW cfs_v WITH (security_invoker = true) AS SELECT 1 AS a;
SELECT * FROM cfs_relation_policy('cfs_v');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_v');
 ok | is_cfs | errors |       details       
----+--------+--------+---------------------
 t  | f      |      0 | relation is not CFS
(1 row)
 
-- An automatically updatable view with check_option set.
CREATE TABLE cfs_v_base (a int);
CREATE VIEW cfs_v2 AS SELECT a FROM cfs_v_base WHERE a > 0 WITH CHECK OPTION;
SELECT * FROM cfs_relation_policy('cfs_v2');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_v2');
 ok | is_cfs | errors |       details       
----+--------+--------+---------------------
 t  | f      |      0 | relation is not CFS
(1 row)
 
-- A composite type has a different reloptions struct as well.
CREATE TYPE cfs_typ AS (a int, b text);
SELECT * FROM cfs_relation_policy('cfs_typ');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_typ');
 ok | is_cfs | errors |       details       
----+--------+--------+---------------------
 t  | f      |      0 | relation is not CFS
(1 row)
 
-- A sequence is not StdRdOptions-compatible either.
CREATE SEQUENCE cfs_seq;
SELECT * FROM cfs_relation_policy('cfs_seq');
 is_cfs | algorithm | level | min_gain_bytes 
--------+-----------+-------+----------------
 f      |           |       |               
(1 row)
 
SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_seq');
 ok | is_cfs | errors |       details       
----+--------+--------+---------------------
 t  | f      |      0 | relation is not CFS
(1 row)
 
DROP VIEW cfs_v;
DROP VIEW cfs_v2;
DROP TABLE cfs_v_base;
DROP TYPE cfs_typ;
DROP SEQUENCE cfs_seq;