Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- CLUSTER on CFS relations, adapted from src/test/regress/sql/cluster.sql.
--
-- CLUSTER is a full table rewrite into a new relfilenode — the same
-- kind of atomic swap the CFS compactor performs.  The checks:
--
--   * a shuffled CFS table clustered on an index: rows come back in
--     index order, the sidecars are rebuilt for the new relfilenode and
--     the old relfilenode's files are gone;
--
--   * toasted values are copied correctly by the rewrite;
--
--   * MVCC safety: dead and rolled-back row versions must not leak into
--     the rewritten storage;
--
--   * edge cases: CLUSTER on a table never clustered before (error) and
--     CLUSTER of an empty CFS table.
--
-- 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;
$$;
 
-- Basic CLUSTER with relfilenode swap -------------------------------------
 
-- Insert rows in reverse key order so clustering has visible work to do.
 
CREATE TABLE cl_cfs (id int, payload text) WITH (algorithm = 'lz4');
 
INSERT INTO cl_cfs
  SELECT 301 - g, md5(g::text) FROM generate_series(1, 300) g;
 
CREATE INDEX cl_cfs_idx ON cl_cfs (id);
 
SELECT relfilenode AS old_node FROM pg_class
  WHERE oid = 'cl_cfs'::regclass \gset
 
CLUSTER cl_cfs USING cl_cfs_idx;
 
-- CLUSTER rewrote the table and marked the index as clustered.
 
SELECT relfilenode <> :old_node AS node_changed FROM pg_class
  WHERE oid = 'cl_cfs'::regclass;
 
SELECT indisclustered FROM pg_index
  WHERE indexrelid = 'cl_cfs_idx'::regclass;
 
CHECKPOINT;
 
SELECT cfs_relation_has_sidecars('cl_cfs') AS sidecars_rebuilt;
 
-- Old relfilenode's files (main fork and sidecars) are gone.
 
SELECT count(*) AS old_node_leftovers
  FROM pg_ls_dir('base/' || (SELECT oid FROM pg_database
                               WHERE datname = current_database())) AS f
 WHERE f ~ ('^' || :old_node::text || '(\..*)?$');
 
-- Physical order now follows the index: with index scans disabled, the
-- sequential scan returns the rows in clustered order, read back through
-- decompression.
 
SELECT cfs_evict('cl_cfs');
 
SET enable_indexscan = off;
 
SET enable_bitmapscan = off;
 
SELECT id = 1 AS first_is_one, payload = md5('300') AS payload_ok
  FROM cl_cfs LIMIT 1;
 
SELECT count(*) AS rows,
       md5(string_agg(payload, '' ORDER BY id))
         = (SELECT md5(string_agg(md5((301 - g)::text), '' ORDER BY g))
              FROM generate_series(1, 300) g) AS data_ok
  FROM cl_cfs;
 
RESET enable_indexscan;
 
RESET enable_bitmapscan;
 
-- TOAST values survive the rewrite ----------------------------------------
 
-- The big column is built from distinct md5 hashes so that PGLZ cannot
-- squeeze it inline and it really lands in the toast relation.
 
CREATE TABLE cl_toast (id int, big text) WITH (algorithm = 'zstd');
 
INSERT INTO cl_toast
  SELECT g, (SELECT string_agg(md5((g * 1000 + i)::text), '')
               FROM generate_series(1, 150) i)
    FROM generate_series(1, 10) g;
 
CREATE INDEX cl_toast_idx ON cl_toast (id);
 
CLUSTER cl_toast USING cl_toast_idx;
 
CHECKPOINT;
 
SELECT cfs_evict('cl_toast');
 
SELECT cfs_evict((SELECT reltoastrelid FROM pg_class
                    WHERE oid = 'cl_toast'::regclass));
 
SELECT count(*) AS rows,
       md5(string_agg(big, '' ORDER BY id))
         = (SELECT md5(string_agg((SELECT string_agg(md5((g * 1000 + i)::text), '')
                                     FROM generate_series(1, 150) i),
                                  '' ORDER BY g))
              FROM generate_series(1, 10) g) AS toast_ok
  FROM cl_toast;
 
-- MVCC safety -------------------------------------------------------------
 
-- Deleted and rolled-back row versions must not reappear in the
-- rewritten CFS storage.
 
CREATE TABLE cl_mvcc (id int, payload text) WITH (algorithm = 'lz4');
 
INSERT INTO cl_mvcc
  SELECT g, md5(g::text) FROM generate_series(1, 100) g;
 
CREATE INDEX cl_mvcc_idx ON cl_mvcc (id);
 
DELETE FROM cl_mvcc WHERE id > 50;
 
BEGIN;
 
UPDATE cl_mvcc SET payload = 'rolled-back' WHERE id <= 25;
 
ABORT;
 
CLUSTER cl_mvcc USING cl_mvcc_idx;
 
SELECT cfs_evict('cl_mvcc');
 
SELECT count(*) AS rows,
       count(*) FILTER (WHERE payload = 'rolled-back') AS rolled_back_rows,
       md5(string_agg(payload, '' ORDER BY id))
         = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
              FROM generate_series(1, 50) g) AS data_ok
  FROM cl_mvcc;
 
-- Edge cases ---------------------------------------------------------------
 
-- A table never clustered cannot be re-clustered without an index name.
 
CREATE TABLE cl_edge (id int) WITH (algorithm = 'lz4');
 
CLUSTER cl_edge;
 
-- An empty CFS table can be clustered; it ends up with valid sidecars
-- and no rows.
 
CREATE INDEX cl_edge_idx ON cl_edge (id);
 
CLUSTER cl_edge USING cl_edge_idx;
 
CHECKPOINT;
 
SELECT cfs_relation_has_sidecars('cl_edge') AS sidecars,
       (SELECT count(*) FROM cl_edge) AS rows;
 
DROP TABLE cl_cfs, cl_toast, cl_mvcc, cl_edge;
 
DROP FUNCTION cfs_evict(regclass);
 
DROP EXTENSION pg_buffercache;