Исходный код СУБД 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
-- ----------------------------------------------------------------------
-- CFS relations and tablespaces, adapted from
-- src/test/regress/sql/tablespace.sql.
--
-- A CFS relation is not just its main fork: the .cfp/.cfsmap/.cfsjrn
-- sidecar files must follow every tablespace move.  Mirroring the core
-- scenarios:
--
--   * ALTER TABLE ... SET TABLESPACE on a CFS table.  As in vanilla,
--     the move assigns a new relfilenode and only the table (with its
--     toast relation) moves; indexes stay behind.  The sidecars must
--     appear under the new relfilenode in the new tablespace, the old
--     relfilenode must vanish from the old one, the policy must survive
--     and the data must still read back through decompression.
--
--   * The same move rolled back: the relation stays put and no stray
--     files are left behind in either tablespace.
--
--   * REINDEX (TABLESPACE ...): the index is rebuilt under a new
--     relfilenode in the target tablespace; its sidecars are rebuilt
--     there and the old relfilenode's files disappear.
--
-- In this fork the files of a replaced relfilenode are unlinked at the
-- next checkpoint, not at commit, so file-count checks run after an
-- explicit CHECKPOINT (see cfs_alter_table.sql).
--
-- Before reading data back, the relation is evicted from shared_buffers
-- via pg_buffercache_evict_relation(), so the verifying SELECTs go
-- through smgrread()/CFS decompression (see cfs_create_table.sql).  The
-- number of evicted buffers is not asserted: a tablespace move writes
-- the new files without going through shared_buffers, so there may be
-- nothing to evict.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- In-place tablespace (a real directory inside pg_tblspc), same approach
-- as the core regress suite.
 
SET allow_in_place_tablespaces = true;
 
CREATE TABLESPACE cfs_ts LOCATION '';
 
-- Helpers ----------------------------------------------------------------
 
-- Number of files belonging to relfilenode p_node in the default
-- tablespace directory of the current database.
 
CREATE FUNCTION cfs_base_relation_files(p_node oid)
RETURNS bigint
LANGUAGE sql AS $$
  SELECT count(*)
    FROM pg_ls_dir('base/' || (SELECT oid FROM pg_database
                                 WHERE datname = current_database())) AS f
   WHERE f ~ ('^' || p_node::text || '(\..*)?$');
$$;
 
-- Directory of the current database inside tablespace p_spcname.
 
CREATE FUNCTION cfs_ts_dbdir(p_spcname text)
RETURNS text
LANGUAGE sql AS $$
  SELECT 'pg_tblspc/' || oid || '/' ||
         (SELECT f FROM pg_ls_dir('pg_tblspc/' || oid) AS f LIMIT 1) || '/' ||
         (SELECT oid FROM pg_database WHERE datname = current_database())
    FROM pg_tablespace
   WHERE spcname = p_spcname;
$$;
 
-- Files belonging to relfilenode p_node inside tablespace p_spcname:
-- total count and how many of them are CFS sidecars.
 
CREATE FUNCTION cfs_ts_relation_files(p_spcname text, p_node oid,
                                      OUT total bigint, OUT sidecars bigint)
LANGUAGE sql AS $$
  SELECT count(*),
         count(*) FILTER (WHERE f ~ '\.(cfp|cfsmap|cfsjrn)(\.[0-9]+)?$')
    FROM pg_ls_dir(cfs_ts_dbdir(p_spcname)) AS f
   WHERE f ~ ('^' || p_node::text || '(\..*)?$');
$$;
 
-- Evict a relation from shared_buffers without asserting how many
-- buffers were there.
 
CREATE FUNCTION cfs_evict(p_rel regclass)
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
  PERFORM pg_buffercache_evict_relation(p_rel);
END;
$$;
 
-- Fixture: a CFS table (with an index) and a plain twin with identical
-- contents.
 
CREATE TABLE ts_cfs (id int, payload text) WITH (algorithm = 'lz4');
 
INSERT INTO ts_cfs
  SELECT g, md5(g::text) FROM generate_series(1, 300) g;
 
CREATE INDEX ts_cfs_idx ON ts_cfs (id);
 
CREATE TABLE ts_plain (id int, payload text);
 
INSERT INTO ts_plain
  SELECT g, md5(g::text) FROM generate_series(1, 300) g;
 
CREATE INDEX ts_plain_idx ON ts_plain (id);
 
-- ALTER TABLE ... SET TABLESPACE ------------------------------------------
 
SELECT relfilenode AS cfs_old_node FROM pg_class
  WHERE oid = 'ts_cfs'::regclass \gset
 
ALTER TABLE ts_cfs SET TABLESPACE cfs_ts;
 
ALTER TABLE ts_plain SET TABLESPACE cfs_ts;
 
-- As in vanilla, the move assigns a new relfilenode.
 
SELECT relfilenode <> :cfs_old_node AS node_changed FROM pg_class
  WHERE oid = 'ts_cfs'::regclass;
 
SELECT relfilenode AS cfs_node FROM pg_class
  WHERE oid = 'ts_cfs'::regclass \gset
 
-- The CFS policy rides along unchanged.
 
SELECT * FROM cfs_relation_policy('ts_cfs');
 
-- The tables moved; the indexes stayed in pg_default; the toast
-- relation follows its table.
 
SELECT regexp_replace(c.relname, 'pg_toast_[0-9]+', 'pg_toast_NNN') AS relname,
       coalesce(s.spcname, 'pg_default') AS spcname
  FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid
  WHERE c.oid IN ('ts_cfs'::regclass, 'ts_cfs_idx'::regclass,
                  'ts_plain'::regclass, 'ts_plain_idx'::regclass,
                  (SELECT reltoastrelid FROM pg_class
                     WHERE oid = 'ts_cfs'::regclass),
                  (SELECT reltoastrelid FROM pg_class
                     WHERE oid = 'ts_plain'::regclass))
  ORDER BY 1;
 
-- The sidecar files moved with the CFS table; the old relfilenode is
-- gone from the default tablespace (replaced relfilenodes are unlinked
-- at the next checkpoint).
 
CHECKPOINT;
 
SELECT * FROM cfs_ts_relation_files('cfs_ts', :cfs_node);
 
SELECT cfs_base_relation_files(:cfs_old_node) AS old_node_leftovers;
 
-- Data reads back through decompression from the new location.
 
SELECT cfs_evict('ts_cfs');
 
SELECT count(*) AS rows,
       md5(string_agg(payload, '' ORDER BY id))
         = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
              FROM generate_series(1, 300) g) AS data_ok,
       cfs_relation_has_sidecars('ts_cfs') AS has_sidecars
  FROM ts_cfs;
 
-- Rolled-back move leaves everything where it was -------------------------
 
BEGIN;
 
ALTER TABLE ts_cfs SET TABLESPACE pg_default;
 
ROLLBACK;
 
SELECT relfilenode = :cfs_node AS node_kept FROM pg_class
  WHERE oid = 'ts_cfs'::regclass;
 
SELECT coalesce(s.spcname, 'pg_default') AS spcname
  FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid
  WHERE c.oid = 'ts_cfs'::regclass;
 
CHECKPOINT;
 
SELECT sidecars FROM cfs_ts_relation_files('cfs_ts', :cfs_node);
 
SELECT cfs_base_relation_files(:cfs_node) AS cfs_files_in_base;
 
SELECT count(*) AS rows FROM ts_cfs;
 
-- REINDEX (TABLESPACE ...) ------------------------------------------------
 
-- The index is rebuilt under a new relfilenode in the target tablespace;
-- its CFS sidecars (frozen policy copy, iface_sql.md section 8) are
-- rebuilt there and the old relfilenode disappears from pg_default.
 
SELECT relfilenode AS idx_old_node FROM pg_class
  WHERE oid = 'ts_cfs_idx'::regclass \gset
 
REINDEX (TABLESPACE cfs_ts) TABLE ts_cfs;
 
SELECT relfilenode <> :idx_old_node AS idx_new_node FROM pg_class
  WHERE oid = 'ts_cfs_idx'::regclass;
 
CHECKPOINT;
 
SELECT cfs_relation_has_sidecars('ts_cfs_idx') AS idx_sidecars;
 
SELECT sidecars FROM cfs_ts_relation_files('cfs_ts',
         (SELECT relfilenode FROM pg_class
            WHERE oid = 'ts_cfs_idx'::regclass));
 
SELECT cfs_base_relation_files(:idx_old_node) AS idx_old_node_leftovers;
 
SELECT * FROM cfs_relation_policy('ts_cfs_idx');
 
-- Combined ALTER INDEX ... SET TABLESPACE, SET/RESET (algorithm) ------------
 
-- A single command that both moves the index and changes its compression
-- policy must do both: the policy rebuild consumes the queue entry, so the
-- pending tablespace has to be handed to the rebuild (the
-- REINDEX (TABLESPACE ...) way) instead of being silently dropped.
 
CREATE TABLE ts_combo (id int, payload text);
 
INSERT INTO ts_combo
  SELECT g, md5(g::text) FROM generate_series(1, 300) g;
 
CREATE INDEX ts_combo_idx ON ts_combo (id)
  WITH (algorithm = 'lz4');
 
ALTER INDEX ts_combo_idx SET TABLESPACE cfs_ts, SET (algorithm = 'zstd');
 
SELECT coalesce(s.spcname, 'pg_default') AS spcname,
       cfs_relation_policy(c.oid) AS policy
  FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid
  WHERE c.oid = 'ts_combo_idx'::regclass;
 
SELECT pg_relation_filepath('ts_combo_idx') ~ '^pg_tblspc/[0-9]+/'
  AS in_target_ts;
 
-- RESET flavor: back to plain storage, still in the target tablespace.
 
ALTER INDEX ts_combo_idx RESET (algorithm);
 
SELECT coalesce(s.spcname, 'pg_default') AS spcname,
       cfs_relation_policy(c.oid) AS policy
  FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid
  WHERE c.oid = 'ts_combo_idx'::regclass;
 
-- The index stays usable in both flavors.
 
SET enable_seqscan TO off;
 
SELECT count(*) AS rows,
       md5(string_agg(payload, '' ORDER BY id))
         = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
              FROM generate_series(1, 300) g) AS data_ok
  FROM ts_combo;
 
RESET enable_seqscan;
 
DROP TABLE ts_combo;
 
-- Move the table back and verify the data one last time -------------------
 
ALTER TABLE ts_cfs SET TABLESPACE pg_default;
 
SELECT relfilenode AS cfs_node_back FROM pg_class
  WHERE oid = 'ts_cfs'::regclass \gset
 
CHECKPOINT;
 
SELECT total FROM cfs_ts_relation_files('cfs_ts', :cfs_node_back);
 
SELECT cfs_relation_has_sidecars('ts_cfs') AS sidecars_back_in_default;
 
SELECT cfs_evict('ts_cfs');
 
SELECT count(*) AS rows,
       md5(string_agg(payload, '' ORDER BY id))
         = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
              FROM generate_series(1, 300) g) AS data_ok
  FROM ts_cfs;
 
-- Cleanup: dropping the relations must empty the tablespace, so
-- DROP TABLESPACE succeeds.
 
DROP TABLE ts_cfs, ts_plain;
 
CHECKPOINT;
 
DROP TABLESPACE cfs_ts;
 
DROP FUNCTION cfs_base_relation_files(oid);
 
DROP FUNCTION cfs_ts_dbdir(text);
 
DROP FUNCTION cfs_ts_relation_files(text, oid);
 
DROP FUNCTION cfs_evict(regclass);
 
DROP EXTENSION pg_buffercache;