Исходный код СУБД 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
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
-- ----------------------------------------------------------------------
-- CFS reloptions validation and SET/RESET semantics, adapted from
-- src/test/regress/sql/reloptions.sql.
--
-- The core test validates option parsing against the toast.* namespace;
-- here the same cases are mirrored against the CFS options "algorithm"
-- (enum lz4/zstd), "level" (int, 1..22) and "min_gain_bytes" (int,
-- 0..BLCKSZ) as declared in src/backend/access/common/reloptions.c.
--
-- The fork-specific rules under test (develop/iface_sql.md):
--   section 4: unspecified level / min_gain_bytes are frozen from GUC
--              into reloptions at DDL time;
--   section 7: RESET (level) / RESET (min_gain_bytes) do not remove the
--              option but freeze the current GUC value into reloptions;
--   section 9: a TOAST relation stores a verbatim copy of the table's
--              CFS reloptions, updated on every policy change.
--
-- This test is metadata-only (reloptions and policy inspection), so no
-- buffer eviction is needed.
-- ----------------------------------------------------------------------
 
-- Value range checks: level must be within 1..22, min_gain_bytes within
-- 0..BLCKSZ (8192).
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', level = 0);
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', level = 23);
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', min_gain_bytes = -1);
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', min_gain_bytes = 8193);
 
-- Improper value types and unknown enum values.
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', level = 'string');
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', level = true);
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'gzip');
 
-- An option may not be specified twice.
 
CREATE TABLE ro_bad (i int) WITH (algorithm = 'lz4', algorithm = 'zstd');
 
-- Specifying the name only works only for Boolean options.
 
CREATE TABLE ro_bad (i int) WITH (algorithm);
 
-- Boundary values are accepted.
 
CREATE TABLE ro_bounds_zstd (i int)
  WITH (algorithm = 'zstd', level = 22, min_gain_bytes = 8192);
 
SELECT * FROM cfs_relation_policy('ro_bounds_zstd');
 
CREATE TABLE ro_bounds_lz4 (i int)
  WITH (algorithm = 'lz4', level = 1, min_gain_bytes = 0);
 
SELECT * FROM cfs_relation_policy('ro_bounds_lz4');
 
-- Option names are case-insensitive (mirrors the FiLLFaCToR case).
 
CREATE TABLE ro_case (i int) WITH (AlGorithm = 'lz4', LeVeL = 4);
 
SELECT * FROM cfs_relation_policy('ro_case');
 
-- Simple ALTER TABLE: SET replaces only the named options.
 
CREATE TABLE ro_alter (i int) WITH (algorithm = 'lz4');
 
ALTER TABLE ro_alter SET (level = 5, min_gain_bytes = 128);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'ro_alter'::regclass))
  ORDER BY option_name;
 
-- RESET of a parameter removes the option from the catalog and returns
-- the field to runtime resolve; no GUC value is frozen in its place.
 
ALTER TABLE ro_alter RESET (level);
 
SELECT * FROM cfs_relation_policy('ro_alter');
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'ro_alter'::regclass))
  ORDER BY option_name;
 
-- RESET fails if a value is specified.
 
ALTER TABLE ro_alter RESET (level = 12);
 
-- TOAST follows the owning table at runtime: the toast relation stores no
-- CFS options of its own; only its genuine toast.* options are stored.
 
CREATE TABLE ro_toast (s text)
  WITH (algorithm = 'zstd', level = 6, toast.autovacuum_enabled = false);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = (SELECT reltoastrelid FROM pg_class
                                             WHERE oid = 'ro_toast'::regclass)))
  ORDER BY option_name;
 
-- No rewrite for a parameter change; the toast's stored options do not
-- change either (the policy follows the table at runtime).
 
ALTER TABLE ro_toast SET (level = 8);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = (SELECT reltoastrelid FROM pg_class
                                             WHERE oid = 'ro_toast'::regclass)))
  ORDER BY option_name;
 
-- RESET (algorithm) rewrites the table back to plain storage; the toast's
-- stored options do not change, its effective policy stops being CFS.
 
ALTER TABLE ro_toast RESET (algorithm);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = (SELECT reltoastrelid FROM pg_class
                                             WHERE oid = 'ro_toast'::regclass)))
  ORDER BY option_name;
 
-- CREATE INDEX / ALTER INDEX with own CFS policy on a plain table
-- (mirrors the btree reloptions section of the core test).
 
CREATE TABLE ro_idx_t (i int);
 
CREATE INDEX ro_idx ON ro_idx_t (i) WITH (algorithm = 'zstd', level = 5);
 
SELECT * FROM cfs_relation_policy('ro_idx');
 
-- Changing a parameter touches only reloptions.
 
ALTER INDEX ro_idx SET (level = 7);
 
SELECT * FROM cfs_relation_policy('ro_idx');
 
-- Resetting the algorithm rebuilds the index back to plain storage.
 
ALTER INDEX ro_idx RESET (algorithm);
 
SELECT * FROM cfs_relation_policy('ro_idx');
 
-- ----------------------------------------------------------------------
-- Toast mirror acceptance: vanilla pg_dump dumps a TOAST relation's
-- CFS reloptions back into the table's WITH clause as toast.* options, so
-- the server must accept a toast.* triple that verbatim mirrors the table's
-- own options, and still reject any independent (differing or orphaned)
-- toast policy.
-- ----------------------------------------------------------------------
 
-- toast.* CFS options are rejected unconditionally: a TOAST relation has
-- no independent CFS policy; it follows the owning table's policy at
-- runtime and stores no CFS options of its own.
CREATE TABLE ro_mirror (s text)
  WITH (algorithm = 'zstd', level = 3, min_gain_bytes = 512);
 
-- Even a verbatim mirror of the table's triple is rejected now.
CREATE TABLE ro_bad (s text)
  WITH (algorithm = 'zstd', level = 3, min_gain_bytes = 512,
        toast.algorithm = 'zstd', toast.level = '3', toast.min_gain_bytes = '512');
 
-- A toast.* CFS option on a table with no CFS policy at all is rejected
-- as well.
CREATE TABLE ro_bad (s text)
  WITH (toast.algorithm = 'zstd', toast.level = '3', toast.min_gain_bytes = '512');
 
-- ALTER: any toast.* CFS option is rejected, even one that would mirror
-- the table's current policy.
ALTER TABLE ro_mirror SET (toast.level = '3');
 
-- ALTER: RESET of a toast.* CFS option is rejected too.
ALTER TABLE ro_mirror RESET (toast.algorithm);
 
-- The toast relation of a CFS table carries no CFS options at all: its
-- policy is resolved from the owning table at runtime.
SELECT reloptions FROM pg_class
  WHERE oid = (SELECT reltoastrelid FROM pg_class
                 WHERE oid = 'ro_mirror'::regclass);
 
DROP TABLE ro_bounds_zstd, ro_bounds_lz4, ro_case, ro_alter, ro_toast, ro_idx_t, ro_mirror;