Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- TRUNCATE on CFS relations, adapted from
-- src/test/regress/sql/truncate.sql.
--
-- TRUNCATE swaps the relation to a fresh, empty relfilenode.  For a CFS
-- table that means the sidecar trio (.cfp/.cfsmap/.cfsjrn) must be reset
-- together with the main fork, on commit as well as on rollback:
--
--   * committed TRUNCATE: new relfilenode with fresh empty sidecars, old
--     relfilenode's files removed, table reusable and readable through
--     decompression afterwards;
--
--   * rolled-back TRUNCATE: data and sidecars of the original
--     relfilenode intact, the transient relfilenode's files removed at
--     abort;
--
--   * TRUNCATE of an empty CFS table: still a valid no-op storage swap.
--
-- 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).
--
-- Read-back checks evict the relation from shared_buffers via
-- pg_buffercache_evict_relation(), so the verifying SELECTs go through
-- smgrread()/CFS decompression (see cfs_create_table.sql).
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
CREATE FUNCTION cfs_evict(p_rel regclass)
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
  PERFORM pg_buffercache_evict_relation(p_rel);
END;
$$;
 
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 || '(\..*)?$');
$$;
 
-- Committed TRUNCATE -------------------------------------------------------
 
CREATE TABLE tr_cfs (id int, payload text) WITH (algorithm = 'lz4');
 
INSERT INTO tr_cfs
  SELECT g, md5(g::text) FROM generate_series(1, 300) g;
 
SELECT relfilenode AS old_node FROM pg_class
  WHERE oid = 'tr_cfs'::regclass \gset
 
TRUNCATE tr_cfs;
 
-- A fresh relfilenode with fresh sidecars, no rows.
 
SELECT relfilenode <> :old_node AS node_changed FROM pg_class
  WHERE oid = 'tr_cfs'::regclass;
 
CHECKPOINT;
 
SELECT cfs_relation_has_sidecars('tr_cfs') AS sidecars,
       (SELECT count(*) FROM tr_cfs) AS rows;
 
SELECT cfs_base_relation_files(:old_node) AS old_node_leftovers;
 
-- The policy survives the truncate.
 
SELECT * FROM cfs_relation_policy('tr_cfs');
 
-- The table is writable and readable (through decompression) again.
 
INSERT INTO tr_cfs
  SELECT g, md5(g::text) FROM generate_series(1001, 1100) g;
 
CHECKPOINT;
 
SELECT cfs_evict('tr_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(1001, 1100) g) AS data_ok
  FROM tr_cfs;
 
-- Rolled-back TRUNCATE -----------------------------------------------------
 
SELECT relfilenode AS stable_node FROM pg_class
  WHERE oid = 'tr_cfs'::regclass \gset
 
BEGIN;
 
TRUNCATE tr_cfs;
 
-- Inside the transaction the transient relfilenode is visible.
 
SELECT relfilenode <> :stable_node AS node_differs FROM pg_class
  WHERE oid = 'tr_cfs'::regclass;
 
SELECT relfilenode AS transient_node FROM pg_class
  WHERE oid = 'tr_cfs'::regclass \gset
 
ROLLBACK;
 
-- After the abort the original relfilenode is back, its data and
-- sidecars are intact, and the transient relfilenode left no files.
 
SELECT relfilenode = :stable_node AS node_restored FROM pg_class
  WHERE oid = 'tr_cfs'::regclass;
 
CHECKPOINT;
 
SELECT cfs_relation_has_sidecars('tr_cfs') AS sidecars;
 
SELECT cfs_base_relation_files(:transient_node) AS transient_leftovers;
 
SELECT cfs_evict('tr_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(1001, 1100) g) AS data_ok
  FROM tr_cfs;
 
-- TRUNCATE of an empty CFS table -------------------------------------------
 
CREATE TABLE tr_empty (id int) WITH (algorithm = 'zstd');
 
TRUNCATE tr_empty;
 
CHECKPOINT;
 
SELECT cfs_relation_has_sidecars('tr_empty') AS sidecars,
       (SELECT count(*) FROM tr_empty) AS rows;
 
-- TRUNCATE of a CFS table created in the same transaction ------------------
--
-- heap_truncate_one_rel() truncates in place through the smgrtruncate()
-- bridge, which runs inside a critical section; a table created and
-- truncated in the same transaction has never been written through CFS.
-- The sidecars must already exist at CREATE time (the policy publication
-- materializes them, like mdcreate() materializes the base file), so the
-- bridge opens existing files instead of creating them in the critical
-- section.
 
BEGIN;
 
CREATE TABLE tr_same_txn (id int) WITH (algorithm = 'lz4');
 
SELECT cfs_relation_has_sidecars('tr_same_txn') AS sidecars;
 
TRUNCATE tr_same_txn;
 
TRUNCATE tr_same_txn;
 
SELECT cfs_relation_has_sidecars('tr_same_txn') AS sidecars,
       (SELECT count(*) FROM tr_same_txn) AS rows;
 
COMMIT;
 
SELECT cfs_relation_has_sidecars('tr_same_txn') AS sidecars,
       (SELECT count(*) FROM tr_same_txn) AS rows;
 
DROP TABLE tr_same_txn;
 
-- A committed table truncated twice inside one transaction: the second
-- TRUNCATE runs the in-place path on the fresh relfilenode the first one
-- assigned, whose sidecars must likewise exist since the relfilenode swap.
 
CREATE TABLE tr_twice (id int) WITH (algorithm = 'zstd');
 
BEGIN;
 
TRUNCATE tr_twice;
 
TRUNCATE tr_twice;
 
SELECT cfs_relation_has_sidecars('tr_twice') AS sidecars,
       (SELECT count(*) FROM tr_twice) AS rows;
 
COMMIT;
 
DROP TABLE tr_twice;
 
DROP TABLE tr_cfs, tr_empty;
 
DROP FUNCTION cfs_evict(regclass);
 
DROP FUNCTION cfs_base_relation_files(oid);
 
DROP EXTENSION pg_buffercache;