Debian Codegraph @

src/test/subscription/t/023_twophase_stream.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test logical replication of 2PC with streaming.
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 # Check that the parallel apply worker has finished applying the streaming
12 # transaction.
13 sub check_parallel_log
14 {
15 my ($node_subscriber, $offset, $is_parallel, $type) = @_;
16
17 if ($is_parallel)
18 {
19 $node_subscriber->wait_for_log(
20 qr/DEBUG: ( [A-Z0-9]+:)? finished processing the STREAM $type command/,
21 $offset);
22 }
23 }
24
25 # Common test steps for both the streaming=on and streaming=parallel cases.
26 sub test_streaming
27 {
28 my ($node_publisher, $node_subscriber, $appname, $is_parallel) = @_;
29
30 my $offset = 0;
31
32 ###############################
33 # Test 2PC PREPARE / COMMIT PREPARED.
34 # 1. Data is streamed as a 2PC transaction.
35 # 2. Then do commit prepared.
36 #
37 # Expect all data is replicated on subscriber side after the commit.
38 ###############################
39
40 # Check the subscriber log from now on.
41 $offset = -s $node_subscriber->logfile;
42
43 # check that 2PC gets replicated to subscriber
44 # Insert, update and delete some rows.
45 $node_publisher->safe_psql(
46 'postgres', q{
47 BEGIN;
48 INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5) s(i);
49 UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0;
50 DELETE FROM test_tab WHERE mod(a,3) = 0;
51 PREPARE TRANSACTION 'test_prepared_tab';});
52
53 $node_publisher->wait_for_catchup($appname);
54
55 check_parallel_log($node_subscriber, $offset, $is_parallel, 'PREPARE');
56
57 # check that transaction is in prepared state on subscriber
58 my $result = $node_subscriber->safe_psql('postgres',
59 "SELECT count(*) FROM pg_prepared_xacts;");
60 is($result, qq(1), 'transaction is prepared on subscriber');
61
62 # 2PC transaction gets committed
63 $node_publisher->safe_psql('postgres',
64 "COMMIT PREPARED 'test_prepared_tab';");
65
66 $node_publisher->wait_for_catchup($appname);
67
68 # check that transaction is committed on subscriber
69 $result = $node_subscriber->safe_psql('postgres',
70 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
71 is($result, qq(4|4|4),
72 'Rows inserted by 2PC have committed on subscriber, and extra columns contain local defaults'
73 );
74 $result = $node_subscriber->safe_psql('postgres',
75 "SELECT count(*) FROM pg_prepared_xacts;");
76 is($result, qq(0), 'transaction is committed on subscriber');
77
78 ###############################
79 # Test 2PC PREPARE / ROLLBACK PREPARED.
80 # 1. Table is deleted back to 2 rows which are replicated on subscriber.
81 # 2. Data is streamed using 2PC.
82 # 3. Do rollback prepared.
83 #
84 # Expect data rolls back leaving only the original 2 rows.
85 ###############################
86
87 # First, delete the data except for 2 rows (will be replicated)
88 $node_publisher->safe_psql('postgres',
89 "DELETE FROM test_tab WHERE a > 2;");
90
91 # Check the subscriber log from now on.
92 $offset = -s $node_subscriber->logfile;
93
94 # Then insert, update and delete some rows.
95 $node_publisher->safe_psql(
96 'postgres', q{
97 BEGIN;
98 INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5) s(i);
99 UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0;
100 DELETE FROM test_tab WHERE mod(a,3) = 0;
101 PREPARE TRANSACTION 'test_prepared_tab';});
102
103 $node_publisher->wait_for_catchup($appname);
104
105 check_parallel_log($node_subscriber, $offset, $is_parallel, 'PREPARE');
106
107 # check that transaction is in prepared state on subscriber
108 $result = $node_subscriber->safe_psql('postgres',
109 "SELECT count(*) FROM pg_prepared_xacts;");
110 is($result, qq(1), 'transaction is prepared on subscriber');
111
112 # 2PC transaction gets aborted
113 $node_publisher->safe_psql('postgres',
114 "ROLLBACK PREPARED 'test_prepared_tab';");
115
116 $node_publisher->wait_for_catchup($appname);
117
118 # check that transaction is aborted on subscriber
119 $result = $node_subscriber->safe_psql('postgres',
120 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
121 is($result, qq(2|2|2),
122 'Rows inserted by 2PC are rolled back, leaving only the original 2 rows'
123 );
124
125 $result = $node_subscriber->safe_psql('postgres',
126 "SELECT count(*) FROM pg_prepared_xacts;");
127 is($result, qq(0), 'transaction is aborted on subscriber');
128
129 ###############################
130 # Check that 2PC COMMIT PREPARED is decoded properly on crash restart.
131 # 1. insert, update and delete some rows.
132 # 2. Then server crashes before the 2PC transaction is committed.
133 # 3. After servers are restarted the pending transaction is committed.
134 #
135 # Expect all data is replicated on subscriber side after the commit.
136 # Note: both publisher and subscriber do crash/restart.
137 ###############################
138
139 # Check the subscriber log from now on.
140 $offset = -s $node_subscriber->logfile;
141
142 $node_publisher->safe_psql(
143 'postgres', q{
144 BEGIN;
145 INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5) s(i);
146 UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0;
147 DELETE FROM test_tab WHERE mod(a,3) = 0;
148 PREPARE TRANSACTION 'test_prepared_tab';});
149
150 $node_subscriber->stop('immediate');
151 $node_publisher->stop('immediate');
152
153 $node_publisher->start;
154 $node_subscriber->start;
155
156 # We don't try to check the log for parallel option here as the subscriber
157 # may have stopped after finishing the prepare and before logging the
158 # appropriate message.
159
160 # commit post the restart
161 $node_publisher->safe_psql('postgres',
162 "COMMIT PREPARED 'test_prepared_tab';");
163 $node_publisher->wait_for_catchup($appname);
164
165 # check inserts are visible
166 $result = $node_subscriber->safe_psql('postgres',
167 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
168 is($result, qq(4|4|4),
169 'Rows inserted by 2PC have committed on subscriber, and extra columns contain local defaults'
170 );
171
172 ###############################
173 # Do INSERT after the PREPARE but before ROLLBACK PREPARED.
174 # 1. Table is deleted back to 2 rows which are replicated on subscriber.
175 # 2. Data is streamed using 2PC.
176 # 3. A single row INSERT is done which is after the PREPARE.
177 # 4. Then do a ROLLBACK PREPARED.
178 #
179 # Expect the 2PC data rolls back leaving only 3 rows on the subscriber
180 # (the original 2 + inserted 1).
181 ###############################
182
183 # First, delete the data except for 2 rows (will be replicated)
184 $node_publisher->safe_psql('postgres',
185 "DELETE FROM test_tab WHERE a > 2;");
186
187 # Check the subscriber log from now on.
188 $offset = -s $node_subscriber->logfile;
189
190 # Then insert, update and delete some rows.
191 $node_publisher->safe_psql(
192 'postgres', q{
193 BEGIN;
194 INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5) s(i);
195 UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0;
196 DELETE FROM test_tab WHERE mod(a,3) = 0;
197 PREPARE TRANSACTION 'test_prepared_tab';});
198
199 $node_publisher->wait_for_catchup($appname);
200
201 check_parallel_log($node_subscriber, $offset, $is_parallel, 'PREPARE');
202
203 # check that transaction is in prepared state on subscriber
204 $result = $node_subscriber->safe_psql('postgres',
205 "SELECT count(*) FROM pg_prepared_xacts;");
206 is($result, qq(1), 'transaction is prepared on subscriber');
207
208 # Insert a different record (now we are outside of the 2PC transaction)
209 # Note: the 2PC transaction still holds row locks so make sure this insert is for a separate primary key
210 $node_publisher->safe_psql('postgres',
211 "INSERT INTO test_tab VALUES (99999, 'foobar')");
212
213 # 2PC transaction gets aborted
214 $node_publisher->safe_psql('postgres',
215 "ROLLBACK PREPARED 'test_prepared_tab';");
216
217 $node_publisher->wait_for_catchup($appname);
218
219 # check that transaction is aborted on subscriber,
220 # but the extra INSERT outside of the 2PC still was replicated
221 $result = $node_subscriber->safe_psql('postgres',
222 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
223 is($result, qq(3|3|3),
224 'check the outside insert was copied to subscriber');
225
226 $result = $node_subscriber->safe_psql('postgres',
227 "SELECT count(*) FROM pg_prepared_xacts;");
228 is($result, qq(0), 'transaction is aborted on subscriber');
229
230 ###############################
231 # Do INSERT after the PREPARE but before COMMIT PREPARED.
232 # 1. Table is deleted back to 2 rows which are replicated on subscriber.
233 # 2. Data is streamed using 2PC.
234 # 3. A single row INSERT is done which is after the PREPARE.
235 # 4. Then do a COMMIT PREPARED.
236 #
237 # Expect 2PC data + the extra row are on the subscriber
238 # (the 3334 + inserted 1 = 3335).
239 ###############################
240
241 # First, delete the data except for 2 rows (will be replicated)
242 $node_publisher->safe_psql('postgres',
243 "DELETE FROM test_tab WHERE a > 2;");
244
245 # Check the subscriber log from now on.
246 $offset = -s $node_subscriber->logfile;
247
248 # Then insert, update and delete some rows.
249 $node_publisher->safe_psql(
250 'postgres', q{
251 BEGIN;
252 INSERT INTO test_tab SELECT i, sha256(i::text::bytea) FROM generate_series(3, 5) s(i);
253 UPDATE test_tab SET b = sha256(b) WHERE mod(a,2) = 0;
254 DELETE FROM test_tab WHERE mod(a,3) = 0;
255 PREPARE TRANSACTION 'test_prepared_tab';});
256
257 $node_publisher->wait_for_catchup($appname);
258
259 check_parallel_log($node_subscriber, $offset, $is_parallel, 'PREPARE');
260
261 # check that transaction is in prepared state on subscriber
262 $result = $node_subscriber->safe_psql('postgres',
263 "SELECT count(*) FROM pg_prepared_xacts;");
264 is($result, qq(1), 'transaction is prepared on subscriber');
265
266 # Insert a different record (now we are outside of the 2PC transaction)
267 # Note: the 2PC transaction still holds row locks so make sure this insert is for a separate primary key
268 $node_publisher->safe_psql('postgres',
269 "INSERT INTO test_tab VALUES (99999, 'foobar')");
270
271 # 2PC transaction gets committed
272 $node_publisher->safe_psql('postgres',
273 "COMMIT PREPARED 'test_prepared_tab';");
274
275 $node_publisher->wait_for_catchup($appname);
276
277 # check that transaction is committed on subscriber
278 $result = $node_subscriber->safe_psql('postgres',
279 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
280 is($result, qq(5|5|5),
281 'Rows inserted by 2PC (as well as outside insert) have committed on subscriber, and extra columns contain local defaults'
282 );
283
284 $result = $node_subscriber->safe_psql('postgres',
285 "SELECT count(*) FROM pg_prepared_xacts;");
286 is($result, qq(0), 'transaction is committed on subscriber');
287
288 # Cleanup the test data
289 $node_publisher->safe_psql('postgres',
290 "DELETE FROM test_tab WHERE a > 2;");
291 $node_publisher->wait_for_catchup($appname);
292 }
293
294 ###############################
295 # Setup
296 ###############################
297
298 # Initialize publisher node
299 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
300 $node_publisher->init(allows_streaming => 'logical');
301 $node_publisher->append_conf(
302 'postgresql.conf', qq(
303 max_prepared_transactions = 10
304 debug_logical_replication_streaming = immediate
305 ));
306 $node_publisher->start;
307
308 # Create subscriber node
309 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
310 $node_subscriber->init;
311 $node_subscriber->append_conf(
312 'postgresql.conf', qq(
313 max_prepared_transactions = 10
314 ));
315 $node_subscriber->start;
316
317 # Create some pre-existing content on publisher
318 $node_publisher->safe_psql('postgres',
319 "CREATE TABLE test_tab (a int primary key, b bytea)");
320 $node_publisher->safe_psql('postgres',
321 "INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
322 $node_publisher->safe_psql('postgres', "CREATE TABLE test_tab_2 (a int)");
323
324 # Setup structure on subscriber (columns a and b are compatible with same table name on publisher)
325 $node_subscriber->safe_psql('postgres',
326 "CREATE TABLE test_tab (a int primary key, b bytea, c timestamptz DEFAULT now(), d bigint DEFAULT 999)"
327 );
328 $node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab_2 (a int)");
329
330 # Setup logical replication (streaming = on)
331 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
332 $node_publisher->safe_psql('postgres',
333 "CREATE PUBLICATION tap_pub FOR TABLE test_tab, test_tab_2");
334
335 my $appname = 'tap_sub';
336
337 ################################
338 # Test using streaming mode 'on'
339 ################################
340 $node_subscriber->safe_psql(
341 'postgres', "
342 CREATE SUBSCRIPTION tap_sub
343 CONNECTION '$publisher_connstr application_name=$appname'
344 PUBLICATION tap_pub
345 WITH (streaming = on, two_phase = on)");
346
347 # Wait for initial table sync to finish
348 $node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
349
350 # Also wait for two-phase to be enabled
351 my $twophase_query =
352 "SELECT count(1) = 0 FROM pg_subscription WHERE subtwophasestate NOT IN ('e');";
353 $node_subscriber->poll_query_until('postgres', $twophase_query)
354 or die "Timed out while waiting for subscriber to enable twophase";
355
356 # Check initial data was copied to subscriber
357 my $result = $node_subscriber->safe_psql('postgres',
358 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
359 is($result, qq(2|2|2), 'check initial data was copied to subscriber');
360
361 test_streaming($node_publisher, $node_subscriber, $appname, 0);
362
363 ######################################
364 # Test using streaming mode 'parallel'
365 ######################################
366 my $oldpid = $node_publisher->safe_psql('postgres',
367 "SELECT pid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';"
368 );
369
370 $node_subscriber->safe_psql('postgres',
371 "ALTER SUBSCRIPTION tap_sub SET(streaming = parallel)");
372
373 $node_publisher->poll_query_until('postgres',
374 "SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname' AND state = 'streaming';"
375 )
376 or die
377 "Timed out while waiting for apply to restart after changing SUBSCRIPTION";
378
379 # We need to check DEBUG logs to ensure that the parallel apply worker has
380 # applied the transaction. So, bump up the log verbosity.
381 $node_subscriber->append_conf('postgresql.conf', "log_min_messages = debug1");
382 $node_subscriber->reload;
383
384 # Run a query to make sure that the reload has taken effect.
385 $node_subscriber->safe_psql('postgres', q{SELECT 1});
386
387 test_streaming($node_publisher, $node_subscriber, $appname, 1);
388
389 # Test serializing changes to files and notify the parallel apply worker to
390 # apply them at the end of the transaction.
391 $node_subscriber->append_conf('postgresql.conf',
392 'debug_logical_replication_streaming = immediate');
393 # Reset the log_min_messages to default.
394 $node_subscriber->append_conf('postgresql.conf',
395 "log_min_messages = warning");
396 $node_subscriber->reload;
397
398 # Run a query to make sure that the reload has taken effect.
399 $node_subscriber->safe_psql('postgres', q{SELECT 1});
400
401 my $offset = -s $node_subscriber->logfile;
402
403 $node_publisher->safe_psql(
404 'postgres', q{
405 BEGIN;
406 INSERT INTO test_tab_2 values(1);
407 PREPARE TRANSACTION 'xact';
408 });
409
410 # Ensure that the changes are serialized.
411 $node_subscriber->wait_for_log(
412 qr/LOG: ( [A-Z0-9]+:)? logical replication apply worker will serialize the remaining changes of remote transaction \d+ to a file/,
413 $offset);
414
415 $node_publisher->wait_for_catchup($appname);
416
417 # Check that transaction is in prepared state on subscriber
418 $result = $node_subscriber->safe_psql('postgres',
419 "SELECT count(*) FROM pg_prepared_xacts;");
420 is($result, qq(1), 'transaction is prepared on subscriber');
421
422 # Check that 2PC gets committed on subscriber
423 $node_publisher->safe_psql('postgres', "COMMIT PREPARED 'xact';");
424
425 $node_publisher->wait_for_catchup($appname);
426
427 # Check that transaction is committed on subscriber
428 $result =
429 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2");
430 is($result, qq(1), 'transaction is committed on subscriber');
431
432 # Test the ability to re-apply a transaction when a parallel apply worker fails
433 # to prepare the transaction due to insufficient max_prepared_transactions
434 # setting.
435 $node_subscriber->append_conf(
436 'postgresql.conf', qq(
437 max_prepared_transactions = 0
438 debug_logical_replication_streaming = buffered
439 ));
440 $node_subscriber->restart;
441
442 $offset = -s $node_subscriber->logfile;
443
444 $node_publisher->safe_psql(
445 'postgres', q{
446 BEGIN;
447 INSERT INTO test_tab_2 values(2);
448 PREPARE TRANSACTION 'xact';
449 COMMIT PREPARED 'xact';
450 });
451
452 # Confirm the ERROR is reported because max_prepared_transactions is zero
453 $node_subscriber->wait_for_log(
454 qr/ERROR: ( [A-Z0-9]+:)? prepared transactions are disabled/,
455 $offset);
456
457 # Confirm that the parallel apply worker has encountered an error. The check
458 # focuses on the worker type as a keyword, since the error message content may
459 # differ based on whether the leader initially detected the parallel apply
460 # worker's failure or received a signal from it.
461 $node_subscriber->wait_for_log(
462 qr/ERROR: .*logical replication parallel apply worker.*/,
463 $offset);
464
465 # Set max_prepared_transactions to correct value to resume the replication
466 $node_subscriber->append_conf('postgresql.conf',
467 qq(max_prepared_transactions = 10));
468 $node_subscriber->restart;
469
470 $node_publisher->wait_for_catchup($appname);
471
472 # Check that transaction is committed on subscriber
473 $result =
474 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_2");
475 is($result, qq(2), 'transaction is committed on subscriber after retrying');
476
477 ###############################
478 # check all the cleanup
479 ###############################
480
481 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub");
482
483 $result = $node_subscriber->safe_psql('postgres',
484 "SELECT count(*) FROM pg_subscription");
485 is($result, qq(0), 'check subscription was dropped on subscriber');
486
487 $result = $node_publisher->safe_psql('postgres',
488 "SELECT count(*) FROM pg_replication_slots");
489 is($result, qq(0), 'check replication slot was dropped on publisher');
490
491 $result = $node_subscriber->safe_psql('postgres',
492 "SELECT count(*) FROM pg_subscription_rel");
493 is($result, qq(0),
494 'check subscription relation status was dropped on subscriber');
495
496 $result = $node_subscriber->safe_psql('postgres',
497 "SELECT count(*) FROM pg_replication_origin");
498 is($result, qq(0), 'check replication origin was dropped on subscriber');
499
500 $node_subscriber->stop('fast');
501 $node_publisher->stop('fast');
502
503 done_testing();