Исходный код СУБД 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Streaming of in-progress transactions changing CFS-compressed tables.
#
# With streaming = on, large (or forced-to-stream) transactions are sent
# to the subscriber in chunks before commit, while the publisher hammers
# the CFS write path -- concurrent smgr extends plus compression.  The
# scenarios below cover a plain bulk stream, a stream mixing DDL and DML,
# and streams with subtransaction rollbacks (with and without DDL).
# Rolled-back changes must never appear on the subscriber, and the
# publisher's CFS sidecars must 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, extra config applied)
# and a plain subscriber, named after the scenario so sections don't
# clash.
sub start_pair
{
    my ($name, $pub_conf) = @_;
 
    my $pub = PostgreSQL::Test::Cluster->new("pub_$name");
    $pub->init(allows_streaming => 'logical');
    $pub->append_conf('postgresql.conf', $pub_conf);
    $pub->start;
 
    my $sub = PostgreSQL::Test::Cluster->new("sub_$name");
    $sub->init;
    $sub->start;
 
    return ($pub, $sub);
}
 
# Create publication tap_pub for test_tab and a streaming subscription
# tap_sub (application_name = tap_sub), then wait for the initial table
# sync.
sub setup_streaming_replication
{
    my ($pub, $sub) = @_;
 
    my $connstr = $pub->connstr . ' dbname=postgres';
    $pub->safe_psql('postgres',
        "CREATE PUBLICATION tap_pub FOR TABLE test_tab");
    $sub->safe_psql('postgres',
        "CREATE SUBSCRIPTION tap_sub CONNECTION '$connstr application_name=tap_sub' PUBLICATION tap_pub WITH (streaming = on)"
    );
    $sub->wait_for_subscription_sync($pub, 'tap_sub');
}
 
# ---------------------------------------------------------------------
# Large streamed transactions: bulk INSERT, then INSERT+UPDATE
# (formerly t/105_cfs_stream.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) =
      start_pair('stream', 'logical_decoding_work_mem = 64kB');
 
    $node_publisher->safe_psql('postgres',
        "CREATE TABLE test_tab (a int PRIMARY KEY, b bytea) WITH (algorithm = 'lz4')"
    );
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE test_tab (a int PRIMARY KEY, b bytea)");
 
    setup_streaming_replication($node_publisher, $node_subscriber);
 
    # A large transaction that must be streamed.
    $node_publisher->safe_psql(
        'postgres',
        q{BEGIN;
          INSERT INTO test_tab
            SELECT i, sha256(i::text::bytea) FROM generate_series(1, 5000) s(i);
          COMMIT;});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres',
            "SELECT count(*), count(*) FILTER (WHERE b = sha256(a::text::bytea))
               FROM test_tab"),
        '5000|5000',
        'streamed transaction fully applied on subscriber');
 
    # A second large transaction with an interleaved UPDATE load.
    $node_publisher->safe_psql(
        'postgres',
        q{BEGIN;
          INSERT INTO test_tab
            SELECT i, sha256(i::text::bytea) FROM generate_series(5001, 10000) s(i);
          UPDATE test_tab SET b = sha256((a + 1)::text::bytea)
            WHERE a BETWEEN 1 AND 2500;
          COMMIT;});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    is( $node_subscriber->safe_psql(
            'postgres',
            "SELECT count(*), count(*) FILTER (WHERE b = sha256((a + 1)::text::bytea))
               FROM test_tab"),
        '10000|2500',
        'streamed insert+update transaction applied correctly');
 
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
        't',
        'publisher sidecars intact after streaming load');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
# ---------------------------------------------------------------------
# Streamed and non-streamed transactions mixing DDL and DML
# (formerly t/106_cfs_stream_ddl.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) =
      start_pair('stream_ddl', 'logical_decoding_work_mem = 64kB');
 
    # Preexisting content on the publisher
    $node_publisher->safe_psql('postgres',
        q{CREATE TABLE test_tab (a int primary key, b varchar) WITH (algorithm = 'lz4')}
    );
    $node_publisher->safe_psql('postgres',
        "INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
 
    # Structure on subscriber
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE test_tab (a int primary key, b bytea, c INT, d INT, e INT, f INT)"
    );
 
    setup_streaming_replication($node_publisher, $node_subscriber);
 
    my $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT count(*), count(c), count(d = 999) FROM test_tab");
    is($result, qq(2|0|0),
        'initial rows present on subscriber after table sync');
 
    # a small (non-streamed) transaction with DDL and DML
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea));
ALTER TABLE test_tab ADD COLUMN c INT;
SAVEPOINT s1;
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), -4);
COMMIT;
});
 
    # large (streamed) transaction with DDL and DML
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i FROM generate_series(5, 1000) s(i);
ALTER TABLE test_tab ADD COLUMN d INT;
SAVEPOINT s1;
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i FROM generate_series(1001, 2000) s(i);
COMMIT;
});
 
    # a small (non-streamed) transaction with DDL and DML
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab VALUES (2001, sha256(2001::text::bytea), -2001, 2*2001);
ALTER TABLE test_tab ADD COLUMN e INT;
SAVEPOINT s1;
INSERT INTO test_tab VALUES (2002, sha256(2002::text::bytea), -2002, 2*2002, -3*2002);
COMMIT;
});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT count(*), count(c), count(d), count(e) FROM test_tab");
    is($result, qq(2002|1999|1002|1),
        'data replicated in streaming mode, extra columns contain local defaults'
    );
 
    # A large (streamed) transaction with DDL and DML. One of the DDL is
    # performed after DML to ensure that we invalidate the schema sent
    # for test_tab so that the next transaction has to send the schema
    # again.
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i, -3*i FROM generate_series(2003,5000) s(i);
ALTER TABLE test_tab ADD COLUMN f INT;
COMMIT;
});
 
    # A small transaction that won't get streamed. This is just to
    # ensure that we send the schema again to reflect the last column
    # added in the previous test.
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab SELECT i, sha256(i::text::bytea), -i, 2*i, -3*i, 4*i FROM generate_series(5001,5005) s(i);
COMMIT;
});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    $result = $node_subscriber->safe_psql('postgres',
        "SELECT count(*), count(c), count(d), count(e), count(f) FROM test_tab"
    );
    is($result, qq(5005|5002|4005|3004|5),
        'data replicated for both streaming and non-streaming transactions');
 
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
        't',
        'publisher sidecars intact after streamed DDL/DML mix');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
# ---------------------------------------------------------------------
# Streamed transactions with subtransaction aborts
# (formerly t/107_cfs_stream_subxact_abort.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) =
      start_pair('stream_subxact',
        'debug_logical_replication_streaming = immediate');
 
    $node_publisher->safe_psql('postgres',
        "CREATE TABLE test_tab (a int primary key, b bytea) WITH (algorithm = 'lz4')"
    );
    $node_publisher->safe_psql('postgres',
        "INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
 
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE test_tab (a int primary key, b bytea)");
 
    setup_streaming_replication($node_publisher, $node_subscriber);
 
    my $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT count(*) FROM test_tab");
    is($result, qq(2), 'initial rows present on subscriber after table sync');
 
    # streamed transaction with subtransaction ROLLBACKs: ROLLBACK TO
    # s1 undoes rows 4-7, so only rows 3, 8, 9 and 10 survive.
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea));
SAVEPOINT s1;
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea));
SAVEPOINT s2;
INSERT INTO test_tab VALUES (5, sha256(5::text::bytea));
SAVEPOINT s3;
INSERT INTO test_tab VALUES (6, sha256(6::text::bytea));
ROLLBACK TO s2;
INSERT INTO test_tab VALUES (7, sha256(7::text::bytea));
ROLLBACK TO s1;
INSERT INTO test_tab VALUES (8, sha256(8::text::bytea));
SAVEPOINT s4;
INSERT INTO test_tab VALUES (9, sha256(9::text::bytea));
SAVEPOINT s5;
INSERT INTO test_tab VALUES (10, sha256(10::text::bytea));
COMMIT;
});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT string_agg(a::text, ',' ORDER BY a) FROM test_tab");
    is($result, qq(1,2,3,8,9,10),
        'rolled-back subtransactions not replicated');
 
    # streamed transaction rolled back entirely: nothing new appears.
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab VALUES (16, sha256(16::text::bytea));
SAVEPOINT s1;
INSERT INTO test_tab VALUES (17, sha256(17::text::bytea));
SAVEPOINT s2;
INSERT INTO test_tab VALUES (18, sha256(18::text::bytea));
ROLLBACK;
});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT count(*) FROM test_tab");
    is($result, qq(6), 'fully rolled-back streamed transaction left no rows');
 
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
        't',
        'publisher sidecars intact after streamed aborts');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
# ---------------------------------------------------------------------
# A streamed transaction mixing subtransactions, DDL, DML and ROLLBACKs
# (formerly t/108_cfs_stream_subxact_ddl_abort.pl)
# ---------------------------------------------------------------------
{
    my ($node_publisher, $node_subscriber) =
      start_pair('stream_subxact_ddl',
        'debug_logical_replication_streaming = immediate');
 
    # Preexisting content on the publisher
    $node_publisher->safe_psql('postgres',
        q{CREATE TABLE test_tab (a int primary key, b bytea) WITH (algorithm = 'lz4')}
    );
    $node_publisher->safe_psql('postgres',
        "INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
 
    # Structure on subscriber
    $node_subscriber->safe_psql('postgres',
        "CREATE TABLE test_tab (a int primary key, b bytea, c INT, d INT, e INT)"
    );
 
    setup_streaming_replication($node_publisher, $node_subscriber);
 
    my $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT count(*), count(c) FROM test_tab");
    is($result, qq(2|0),
        'initial rows present on subscriber after table sync');
 
    # streamed transaction with DDL, DML and ROLLBACKs
    $node_publisher->safe_psql(
        'postgres', q{
BEGIN;
INSERT INTO test_tab VALUES (3, sha256(3::text::bytea));
ALTER TABLE test_tab ADD COLUMN c INT;
SAVEPOINT s1;
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), -4);
ALTER TABLE test_tab ADD COLUMN d INT;
SAVEPOINT s2;
INSERT INTO test_tab VALUES (5, sha256(5::text::bytea), -5, 5*2);
ALTER TABLE test_tab ADD COLUMN e INT;
SAVEPOINT s3;
INSERT INTO test_tab VALUES (6, sha256(6::text::bytea), -6, 6*2, -6*3);
ALTER TABLE test_tab DROP COLUMN c;
ROLLBACK TO s1;
INSERT INTO test_tab VALUES (4, sha256(4::text::bytea), 4);
COMMIT;
});
 
    $node_publisher->wait_for_catchup('tap_sub');
 
    $result =
      $node_subscriber->safe_psql('postgres',
        "SELECT count(*), count(c) FROM test_tab");
    is($result, qq(4|1),
        'rollback to savepoint reflected on subscriber, extra columns contain local defaults'
    );
 
    is( $node_publisher->safe_psql(
            'postgres',
            "SELECT ok FROM pg_cfs_check('test_tab'::regclass, true)"),
        't',
        'publisher sidecars intact after streamed subxact/DDL abort');
 
    $node_subscriber->stop;
    $node_publisher->stop;
}
 
done_testing();