Исходный код СУБД 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
-- ----------------------------------------------------------------------
-- Shared-memory map cache, statistics, eviction, cached I/O
-- ----------------------------------------------------------------------
-- The CRC-invalidation case deliberately corrupts a .cfp record; silence
-- the resulting WARNINGs so the expected output stays deterministic.
SET client_min_messages = error;
SELECT count(*) AS shmem_failures
FROM cfs_run_shmem_tests()
WHERE result <> 'ok';
 shmem_failures 
----------------
              0
(1 row)
 
SELECT test_name, result, detail
FROM cfs_run_shmem_tests()
ORDER BY test_name;
             test_name              | result |                        detail                         
------------------------------------+--------+-------------------------------------------------------
 bg_pause_expiry                    | ok     | pause/expiry/status ok
 read_cached_entry_corruption       | ok     | corrupt cached entry rejected and invalidated
 shared_map_api                     | ok     | segment pool API ok
 shared_map_clock_sweep             | ok     | clock sweep kept the hot segment
 shared_map_drop_database           | ok     | drop database kept other databases
 shared_map_ensure                  | ok     | segment load ok
 shared_map_eviction                | ok     | eviction ok
 shared_map_eviction_pinned         | ok     | pinned eviction ok
 shared_map_fallback_read           | ok     | fallback_reads +1, data ok
 shared_map_generation_gate         | ok     | generation gate ok
 shared_map_invalidate_index        | ok     | relation index scoped the invalidation
 shared_map_journal_aba             | ok     | journal truncation vs segment sync ok
 shared_map_nblocks_gate            | ok     | nblocks gate ok
 shared_map_pool_exhaustion         | ok     | pinned admission ok
 shared_map_replay_corruption       | ok     | mid-journal corruption rejected
 shared_map_resident_store_no_evict | ok     | resident store evictions 0
 shared_map_short_read              | ok     | torn tail ignored, healed from journal (healed)
 shared_map_truncate_resurrect      | ok     | truncate resurrection ok
 shared_map_unhealed_corruption     | ok     | block quarantined: auth 1/1 pool 1/1 again 1 lookup 1
 shared_map_write_no_load           | ok     | write loaded 0 segments, reads ok
 shmem_cached_io                    | ok     | cached write/read reads=2 writes=1
 shmem_chains                       | ok     | 64 colliding keys, chains keep exact slots
 shmem_crc_point_invalidate         | ok     | CRC error dropped one entry; meta slot survived
 shmem_crc_survives_invalidate      | ok     | crc_failures survives invalidate, reset clears
 shmem_generation_check             | ok     | stale-generation hit rejected
 shmem_invalidate                   | ok     | invalidate ok
 shmem_meta_overflow                | ok     | excess relation rejected with ERROR
 shmem_meta_pin                     | ok     | meta/pin/release ok
 shmem_pin_reaper                   | ok     | pin reaper ok
 shmem_publish_postcommit_overflow  | ok     | post-commit overflow degraded to a WARNING and a skip
 shmem_stale_handle_hit             | ok     | stale handle reopened on hit ok
 shmem_stats                        | ok     | counters reads=0 writes=0 after reset reads=0
 shmem_truncate_meta                | ok     | truncate shrank meta, invalidate dropped it
(33 rows)
 
-- ----------------------------------------------------------------------
-- Deferred policy publication: an ALTER changing only the tuning
-- (level) queues the new policy for the end of the transaction instead of
-- publishing it into shared memory immediately.  A committed transaction
-- publishes the new level; an aborted one leaves the previous level in the
-- shared cache.
-- ----------------------------------------------------------------------
CREATE TABLE cfs_defer (i int) WITH (algorithm = 'lz4', level = 3);
-- Touch the relation through the smgr layer so its frozen policy (level 3)
-- is published into the shared cache.
INSERT INTO cfs_defer VALUES (1);
-- The shared cache now holds the table's frozen policy (level 3).
SELECT cfs_test_shared_level('cfs_defer') AS shared_level;
 shared_level 
--------------
            3
(1 row)
 
-- Defer a level change inside a transaction: the shared cache must still
-- show the old level, the pending flag must be set, and COMMIT publishes 9.
BEGIN;
SELECT cfs_test_defer_publish('cfs_defer', 9);
 cfs_test_defer_publish 
------------------------
 
(1 row)
 
SELECT cfs_test_publish_pending('cfs_defer') AS pending;
 pending 
---------
 t
(1 row)
 
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_before_commit;
 shared_level_before_commit 
----------------------------
                          3
(1 row)
 
COMMIT;
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_commit;
 shared_level_after_commit 
---------------------------
                         9
(1 row)
 
-- A deferred change that is rolled back must not reach the shared cache.
BEGIN;
SELECT cfs_test_defer_publish('cfs_defer', 11);
 cfs_test_defer_publish 
------------------------
 
(1 row)
 
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_before_rollback;
 shared_level_before_rollback 
------------------------------
                            9
(1 row)
 
ROLLBACK;
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_rollback;
 shared_level_after_rollback 
-----------------------------
                           9
(1 row)
 
-- A repeated defer within one transaction keeps only the last version.
BEGIN;
SELECT cfs_test_defer_publish('cfs_defer', 5);
 cfs_test_defer_publish 
------------------------
 
(1 row)
 
SELECT cfs_test_defer_publish('cfs_defer', 6);
 cfs_test_defer_publish 
------------------------
 
(1 row)
 
COMMIT;
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_double_defer;
 shared_level_after_double_defer 
---------------------------------
                               6
(1 row)
 
DROP TABLE cfs_defer;
-- ----------------------------------------------------------------------
-- The same semantics through the real DDL path (ALTER TABLE), including
-- subtransaction rollback and turning CFS off in the same
-- transaction: the deferred list must follow the transaction tree,
-- publishing exactly what the committed catalog says.
-- ----------------------------------------------------------------------
CREATE TABLE cfs_defer_ddl (i int) WITH (algorithm = 'lz4', level = 3);
-- Publish the frozen policy (level 3) through the smgr layer.
INSERT INTO cfs_defer_ddl VALUES (1);
-- An autocommitted tuning ALTER publishes the new level after COMMIT.
ALTER TABLE cfs_defer_ddl SET (level = 4);
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_real_alter;
 shared_after_real_alter 
-------------------------
                       4
(1 row)
 
-- A top-level ROLLBACK of a real tuning ALTER leaves the shared policy
-- untouched as well: the publication is deferred to PRE_COMMIT, which an
-- aborted transaction never reaches.  Subsequent writes through the smgr
-- layer keep resolving the old level.
BEGIN;
ALTER TABLE cfs_defer_ddl SET (level = 10);
SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_inside_alter;
 pending_inside_alter 
----------------------
 t
(1 row)
 
ROLLBACK;
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_top_rollback;
 shared_after_top_rollback 
---------------------------
                         4
(1 row)
 
INSERT INTO cfs_defer_ddl VALUES (2);
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_rollback_write;
 shared_after_rollback_write 
-----------------------------
                           4
(1 row)
 
-- An ALTER rolled back to a savepoint must not leave a pending
-- publication, and the commit must publish the surviving catalog level.
BEGIN;
SAVEPOINT sp;
ALTER TABLE cfs_defer_ddl SET (level = 9);
SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_inside_savepoint;
 pending_inside_savepoint 
--------------------------
 t
(1 row)
 
ROLLBACK TO SAVEPOINT sp;
SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_after_sub_rollback;
 pending_after_sub_rollback 
----------------------------
 f
(1 row)
 
COMMIT;
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_sub_rollback;
 shared_after_sub_rollback 
---------------------------
                         4
(1 row)
 
-- A RESET (algorithm) rolled back to a savepoint must not eat the pending
-- publication of the outer level: a transition to non-CFS queues nothing
-- (the entries of the old node publish harmlessly at commit and the unlink
-- cleans up), so the outer entry survives to the commit untouched.
SELECT pg_relation_filenode('cfs_defer_ddl') AS nnf2_node \gset
BEGIN;
ALTER TABLE cfs_defer_ddl SET (level = 7);
SAVEPOINT nnf2_sp;
ALTER TABLE cfs_defer_ddl RESET (algorithm);
SELECT cfs_test_publish_pending_at(:nnf2_node) AS pending_inside_reset;
 pending_inside_reset 
----------------------
 t
(1 row)
 
ROLLBACK TO SAVEPOINT nnf2_sp;
COMMIT;
SELECT cfs_test_shared_level_at(:nnf2_node) AS shared_after_reset_rollback;
 shared_after_reset_rollback 
-----------------------------
                           7
(1 row)
 
-- A RESET (algorithm) whose subtransaction is RELEASEd (committed) leaves
-- the outer entry queued for the replaced node: it publishes harmlessly at
-- commit — the vanilla double-entry answer — because the replaced node is
-- unlinked at the same commit and its slot is forgotten with the sidecars.
SELECT pg_relation_filenode('cfs_defer_ddl') AS nnf3_node \gset
BEGIN;
ALTER TABLE cfs_defer_ddl SET (level = 6);
SAVEPOINT nnf3_sp;
ALTER TABLE cfs_defer_ddl RESET (algorithm);
SELECT cfs_test_publish_pending_at(:nnf3_node) AS pending_inside_release;
 pending_inside_release 
------------------------
 t
(1 row)
 
RELEASE SAVEPOINT nnf3_sp;
SELECT cfs_test_publish_pending_at(:nnf3_node) AS pending_after_release;
 pending_after_release 
-----------------------
 t
(1 row)
 
COMMIT;
-- The old node is replaced and unlinked at commit; its slot is forgotten
-- with the sidecar files, so no CFS policy remains for it.
SELECT cfs_test_shared_level_at(:nnf3_node) AS shared_after_release_commit;
 shared_after_release_commit 
-----------------------------
                            
(1 row)
 
-- Restore the CFS state (algorithm and level) for the sections below; the
-- non-CFS -> CFS transition rewrites the relation into a new storage node.
ALTER TABLE cfs_defer_ddl SET (algorithm = 'lz4', level = 3);
-- Turning CFS off in the same transaction leaves the earlier entry queued
-- for the old storage node (the transition always rewrites into a new
-- one): the entry publishes harmlessly at commit, and the unlink of the
-- replaced node forgets the slot.
SELECT pg_relation_filenode('cfs_defer_ddl') AS old_node \gset
BEGIN;
ALTER TABLE cfs_defer_ddl SET (level = 5);
SELECT cfs_test_publish_pending_at(:old_node) AS pending_before_disable;
 pending_before_disable 
------------------------
 t
(1 row)
 
ALTER TABLE cfs_defer_ddl RESET (algorithm);
SELECT cfs_test_publish_pending_at(:old_node) AS pending_after_disable;
 pending_after_disable 
-----------------------
 t
(1 row)
 
COMMIT;
-- The old node is gone at commit; no CFS policy may remain for it.
SELECT cfs_test_shared_level_at(:old_node) AS shared_old_node_after_commit;
 shared_old_node_after_commit 
------------------------------
                             
(1 row)
 
-- Regression guard: the unlink of CFS sidecars must be decided physically,
-- from the files on disk, never from the policy cache — even a slot lying
-- is_cfs=false for a node whose sidecar files still exist must not leak
-- them (the mdunlink() principle — files are removed unconditionally).  The
-- lie is planted after the last catalog phase of the DROP, in the same
-- transaction: the window where the deferred-publication commit callbacks
-- run before smgrDoPendingDeletes().
CREATE TABLE cfs_unlink_guard (a int) WITH (algorithm = 'lz4');
INSERT INTO cfs_unlink_guard SELECT g FROM generate_series(1, 1000) g;
SELECT cfs_relation_has_sidecars('cfs_unlink_guard') AS guard_sidecars;
 guard_sidecars 
----------------
 t
(1 row)
 
SELECT pg_relation_filenode('cfs_unlink_guard') AS guard_node \gset
BEGIN;
DROP TABLE cfs_unlink_guard;
SELECT cfs_test_publish_false_at(:guard_node);
 cfs_test_publish_false_at 
---------------------------
 
(1 row)
 
COMMIT;
CHECKPOINT;
SELECT cfs_test_sidecars_at(:guard_node) AS leaked_sidecars;
 leaked_sidecars 
-----------------
 f
(1 row)
 
-- Regression guard, part two: whatever the physical predicate says, the
-- unlink must always forget the cached policy slot of the unlinked node —
-- a dropped node must not leave a stale slot behind even when it never had
-- sidecar files.  A plain non-CFS table with a planted lying slot (and no
-- sidecars on disk) discriminates the old early-return behavior, which
-- skipped cfs_forget_options() together with the sidecar removal.
CREATE TABLE cfs_unlink_slot_guard (a int);
SELECT pg_relation_filenode('cfs_unlink_slot_guard') AS slot_guard_node \gset
BEGIN;
SELECT cfs_test_publish_false_at(:slot_guard_node);
 cfs_test_publish_false_at 
---------------------------
 
(1 row)
 
SELECT cfs_test_slot_present_at(:slot_guard_node) AS slot_planted;
 slot_planted 
--------------
 t
(1 row)
 
DROP TABLE cfs_unlink_slot_guard;
COMMIT;
SELECT cfs_test_slot_present_at(:slot_guard_node) AS stale_slot_left;
 stale_slot_left 
-----------------
 f
(1 row)
 
DROP TABLE cfs_defer_ddl;