Исходный код СУБД 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
# Logical replication from CFS-compressed tables.
#
# The publisher stores its tables compressed (lz4/zstd); the subscriber
# holds plain uncompressed twins.  Each scenario below uses a fresh
# publisher/subscriber pair and exercises one storage event on the
# publisher side -- a server restart, a heap rewrite, a TRUNCATE,
# partition DETACH/ATTACH -- checking that the replication stream keeps
# delivering correct rows and that the publisher's CFS sidecars stay
# structurally valid (pg_cfs_check).
 
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
# Start a publisher (logical streaming enabled) and a plain subscriber,
# named after the scenario so sections don't clash.
sub start_pair
{
    my ($name) = @_;
 
    my $pub = PostgreSQL::Test::Cluster->new("pub_$name");
    $pub->init(allows_streaming => 'logical');
    $pub->start;
 
    my $sub = PostgreSQL::Test::Cluster->new("sub_$name");
    $sub->init;
    $sub->start;
 
    return ($pub, $sub);
}
 
# Create a publication on the publisher and the matching subscription on
# the subscriber, then wait for the initial table sync.
sub setup_replication
{
    my ($pub, $sub, $pub_name, $sub_name, $for_clause) = @_;
 
    my $connstr = $pub->connstr . ' dbname=postgres';
    $pub->safe_psql('postgres', "CREATE PUBLICATION $pub_name $for_clause");
    $sub->safe_psql('postgres',
        "CREATE SUBSCRIPTION $sub_name CONNECTION '$connstr' PUBLICATION $pub_name"
    );
    $sub->wait_for_subscription_sync($pub, $sub_name);
}
 
# ---------------------------------------------------------------------
# Bulk changes, a large toasted value and a publisher restart
# (formerly t/101_cfs_rep_changes.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) = start_pair('changes');
 
    # CFS table on the publisher, plain twin schema on the subscriber.
    $node_publisher->safe_psql('postgres',
        "CREATE TABLE rep_tab (a int PRIMARY KEY, b text) WITH (algorithm = 'lz4')"
    );
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE rep_tab (a int PRIMARY KEY, b text)");
 
    setup_replication($node_publisher, $node_subscriber,
        'tap_pub', 'tap_sub', 'FOR TABLE rep_tab');
 
    # Bulk rows plus one large, deliberately poorly compressible value
    # that is pushed out-of-line into the toast relation.
    $node_publisher->safe_psql(
        'postgres',
        q{INSERT INTO rep_tab SELECT g, md5(g::text) FROM generate_series(1, 1000) g;
          INSERT INTO rep_tab
            SELECT 5000, (SELECT string_agg(md5(i::text), '')
                            FROM generate_series(1, 16000) i);});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres',
            "SELECT count(*), count(*) FILTER (WHERE b = md5(a::text))
               FROM rep_tab WHERE a <= 1000"),
        '1000|1000',
        'bulk rows replicated');
 
    is( $node_subscriber->safe_psql(
            'postgres', "SELECT length(b) FROM rep_tab WHERE a = 5000"),
        '512000',
        'large toasted value replicated');
 
    # Restart the publisher, then write more; the stream must continue.
    $node_publisher->restart;
 
    $node_publisher->safe_psql('postgres',
        "INSERT INTO rep_tab SELECT g, md5(g::text) FROM generate_series(1001, 2000) g"
    );
    $node_publisher->safe_psql('postgres',
        "UPDATE rep_tab SET b = md5('updated') WHERE a = 5000");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres', "SELECT count(*) FROM rep_tab"),
        '2001',
        'rows after publisher restart replicated');
 
    is( $node_subscriber->safe_psql(
            'postgres',
            "SELECT b = md5('updated') FROM rep_tab WHERE a = 5000"),
        't',
        'update after publisher restart replicated');
 
    # The publisher's CFS storage must be sound after all of it.
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT ok FROM pg_cfs_check('rep_tab'::regclass, true)"),
        't',
        'publisher sidecars intact');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
# ---------------------------------------------------------------------
# Heap rewrite (ADD COLUMN with a volatile default) under replication
# (formerly t/102_cfs_rewrite.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) = start_pair('rewrite');
 
    $node_publisher->safe_psql('postgres',
        "CREATE TABLE test1 (a int, b text) WITH (algorithm = 'lz4');");
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE test1 (a int, b text);");
 
    setup_replication($node_publisher, $node_subscriber,
        'mypub', 'mysub', 'FOR ALL TABLES');
 
    $node_publisher->safe_psql('postgres',
        q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');});
 
    $node_publisher->wait_for_catchup('mysub');
 
    is( $node_subscriber->safe_psql('postgres', q{SELECT a, b FROM test1}),
        qq(1|one
2|two),
        'initial data replicated to subscriber');
 
    # DDL that causes a heap rewrite (and a CFS sidecar swap).  A
    # constant DEFAULT would be a fast default without rewrite since
    # PG11, so use a serial column: its volatile nextval() default
    # forces the rewrite.
    my $ddl = "ALTER TABLE test1 ADD c serial;";
    $node_subscriber->safe_psql('postgres', $ddl);
    $node_publisher->safe_psql('postgres', $ddl);
 
    $node_publisher->wait_for_catchup('mysub');
 
    $node_publisher->safe_psql('postgres',
        q{INSERT INTO test1 (a, b, c) VALUES (3, 'three', 33);});
 
    $node_publisher->wait_for_catchup('mysub');
 
    is( $node_subscriber->safe_psql('postgres', q{SELECT a, b, c FROM test1}),
        qq(1|one|1
2|two|2
3|three|33),
        'data replicated to subscriber after heap rewrite');
 
    # The rewritten table's new sidecars must be structurally valid.
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT ok FROM pg_cfs_check('test1'::regclass, true)"),
        't',
        'publisher sidecars intact after rewrite');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
# ---------------------------------------------------------------------
# TRUNCATE of CFS tables under replication
# (formerly t/103_cfs_truncate.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) = start_pair('truncate');
 
    $node_publisher->safe_psql(
        'postgres',
        q{CREATE TABLE tab1 (a int) WITH (algorithm = 'lz4');
          CREATE TABLE tab2 (a int) WITH (algorithm = 'zstd');});
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE tab1 (a int); CREATE TABLE tab2 (a int);");
 
    setup_replication($node_publisher, $node_subscriber,
        'tap_pub', 'tap_sub', 'FOR TABLE tab1, tab2');
 
    $node_publisher->safe_psql('postgres',
        "INSERT INTO tab1 SELECT generate_series(1, 100)");
    $node_publisher->safe_psql('postgres',
        "INSERT INTO tab2 SELECT generate_series(1, 100)");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1"),
        '100',
        'tab1 synced');
 
    # Truncate both tables in one command; the subscriber must follow.
    $node_publisher->safe_psql('postgres', "TRUNCATE tab1, tab2");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres',
            "SELECT (SELECT count(*) FROM tab1) + (SELECT count(*) FROM tab2)"
        ),
        '0',
        'truncate replicated');
 
    # Writes after the truncate flow again; the fresh sidecars are
    # valid.
    $node_publisher->safe_psql('postgres',
        "INSERT INTO tab1 SELECT generate_series(1, 10)");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab1"),
        '10',
        'writes after truncate replicated');
 
    is( $node_publisher->safe_psql(
            'postgres', "SELECT ok FROM pg_cfs_check('tab1'::regclass, true)"
        ),
        't',
        'tab1 sidecars intact after truncate');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
# ---------------------------------------------------------------------
# Replication from a partitioned table with CFS partitions, including
# DETACH/ATTACH PARTITION (formerly t/104_cfs_partition.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) = start_pair('partition');
 
    # The partitions are CFS relations with different policies (the
    # root carries an lz4 default, one leaf overrides it with zstd).
    $node_publisher->safe_psql(
        'postgres',
        q{CREATE TABLE tab_part (a int) PARTITION BY RANGE (a)
            WITH (algorithm = 'lz4');
          CREATE TABLE tab_part_1 PARTITION OF tab_part FOR VALUES FROM (1) TO (1000);
          CREATE TABLE tab_part_2 PARTITION OF tab_part FOR VALUES FROM (1000) TO (2000)
            WITH (algorithm = 'zstd');});
 
    $node_subscriber->safe_psql(
        'postgres',
        q{CREATE TABLE tab_part (a int) PARTITION BY RANGE (a);
          CREATE TABLE tab_part_1 PARTITION OF tab_part FOR VALUES FROM (1) TO (1000);
          CREATE TABLE tab_part_2 PARTITION OF tab_part FOR VALUES FROM (1000) TO (2000);}
    );
 
    setup_replication($node_publisher, $node_subscriber, 'tap_pub', 'tap_sub',
        'FOR TABLE tab_part WITH (publish_via_partition_root = true)');
 
    $node_publisher->safe_psql('postgres',
        "INSERT INTO tab_part SELECT generate_series(1, 1500)");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres', "SELECT count(*) FROM tab_part"),
        '1500',
        'inserts into both CFS partitions replicated');
 
    # DETACH: further writes to the detached partition are no longer
    # published.
    $node_publisher->safe_psql('postgres',
        "ALTER TABLE tab_part DETACH PARTITION tab_part_2");
    $node_publisher->safe_psql('postgres',
        "INSERT INTO tab_part_2 SELECT generate_series(1501, 1600)");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres', "SELECT count(*) FROM tab_part_1"),
        '999',
        'partition 1 unchanged after detach');
 
    # ATTACH back: the partition rejoins the publication.
    $node_publisher->safe_psql('postgres',
        "ALTER TABLE tab_part ATTACH PARTITION tab_part_2 FOR VALUES FROM (1000) TO (2000)"
    );
    $node_publisher->safe_psql('postgres',
        "INSERT INTO tab_part VALUES (1601)");
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres', "SELECT count(*) FROM tab_part_2"),
        '502',
        'partition 2 receives writes after attach');
 
    # The publisher's partition sidecars must be structurally valid.
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT string_agg((pg_cfs_check(oid, true)).ok::text, ',' ORDER BY relname)
               FROM pg_class
              WHERE relname IN ('tab_part_1', 'tab_part_2')"),
        'true,true',
        'partition sidecars intact');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
done_testing();