Debian Codegraph @

src/test/subscription/t/021_twophase.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # logical replication of 2PC test
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 ###############################
12 # Setup
13 ###############################
14
15 # Initialize publisher node
16 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
17 $node_publisher->init(allows_streaming => 'logical');
18 $node_publisher->append_conf('postgresql.conf',
19 qq(max_prepared_transactions = 10));
20 $node_publisher->start;
21
22 # Create subscriber node
23 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
24 $node_subscriber->init;
25 $node_subscriber->append_conf('postgresql.conf',
26 qq(max_prepared_transactions = 0));
27 $node_subscriber->start;
28
29 # Create some pre-existing content on publisher
30 $node_publisher->safe_psql('postgres',
31 "CREATE TABLE tab_full (a int PRIMARY KEY)");
32 $node_publisher->safe_psql(
33 'postgres', "
34 BEGIN;
35 INSERT INTO tab_full SELECT generate_series(1,10);
36 PREPARE TRANSACTION 'some_initial_data';
37 COMMIT PREPARED 'some_initial_data';");
38
39 # Setup structure on subscriber
40 $node_subscriber->safe_psql('postgres',
41 "CREATE TABLE tab_full (a int PRIMARY KEY)");
42
43 # Setup logical replication
44 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
45 $node_publisher->safe_psql('postgres',
46 "CREATE PUBLICATION tap_pub FOR TABLE tab_full");
47
48 my $appname = 'tap_sub';
49 $node_subscriber->safe_psql(
50 'postgres', "
51 CREATE SUBSCRIPTION tap_sub
52 CONNECTION '$publisher_connstr application_name=$appname'
53 PUBLICATION tap_pub
54 WITH (two_phase = on)");
55
56 # Wait for initial table sync to finish
57 $node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
58
59 # Also wait for two-phase to be enabled
60 my $twophase_query =
61 "SELECT count(1) = 0 FROM pg_subscription WHERE subtwophasestate NOT IN ('e');";
62 $node_subscriber->poll_query_until('postgres', $twophase_query)
63 or die "Timed out while waiting for subscriber to enable twophase";
64
65 ###############################
66 # check that 2PC gets replicated to subscriber
67 # then COMMIT PREPARED
68 ###############################
69
70 # Save the log location, to see the failure of the application
71 my $log_location = -s $node_subscriber->logfile;
72
73 $node_publisher->safe_psql(
74 'postgres', "
75 BEGIN;
76 INSERT INTO tab_full VALUES (11);
77 PREPARE TRANSACTION 'test_prepared_tab_full';");
78
79 # Confirm the ERROR is reported because max_prepared_transactions is zero
80 $node_subscriber->wait_for_log(
81 qr/ERROR: ( [A-Z0-9]+:)? prepared transactions are disabled/);
82
83 # Set max_prepared_transactions to correct value to resume the replication
84 $node_subscriber->append_conf('postgresql.conf',
85 qq(max_prepared_transactions = 10));
86 $node_subscriber->restart;
87
88 $node_publisher->wait_for_catchup($appname);
89
90 # check that transaction is in prepared state on subscriber
91 my $result = $node_subscriber->safe_psql('postgres',
92 "SELECT count(*) FROM pg_prepared_xacts;");
93 is($result, qq(1), 'transaction is prepared on subscriber');
94
95 # check that 2PC gets committed on subscriber
96 $node_publisher->safe_psql('postgres',
97 "COMMIT PREPARED 'test_prepared_tab_full';");
98
99 $node_publisher->wait_for_catchup($appname);
100
101 # check that transaction is committed on subscriber
102 $result = $node_subscriber->safe_psql('postgres',
103 "SELECT count(*) FROM tab_full where a = 11;");
104 is($result, qq(1), 'Row inserted via 2PC has committed on subscriber');
105
106 $result = $node_subscriber->safe_psql('postgres',
107 "SELECT count(*) FROM pg_prepared_xacts;");
108 is($result, qq(0), 'transaction is committed on subscriber');
109
110 ###############################
111 # check that 2PC gets replicated to subscriber
112 # then ROLLBACK PREPARED
113 ###############################
114
115 $node_publisher->safe_psql(
116 'postgres', "
117 BEGIN;
118 INSERT INTO tab_full VALUES (12);
119 PREPARE TRANSACTION 'test_prepared_tab_full';");
120
121 $node_publisher->wait_for_catchup($appname);
122
123 # check that transaction is in prepared state on subscriber
124 $result = $node_subscriber->safe_psql('postgres',
125 "SELECT count(*) FROM pg_prepared_xacts;");
126 is($result, qq(1), 'transaction is prepared on subscriber');
127
128 # check that 2PC gets aborted on subscriber
129 $node_publisher->safe_psql('postgres',
130 "ROLLBACK PREPARED 'test_prepared_tab_full';");
131
132 $node_publisher->wait_for_catchup($appname);
133
134 # check that transaction is aborted on subscriber
135 $result = $node_subscriber->safe_psql('postgres',
136 "SELECT count(*) FROM tab_full where a = 12;");
137 is($result, qq(0), 'Row inserted via 2PC is not present on subscriber');
138
139 $result = $node_subscriber->safe_psql('postgres',
140 "SELECT count(*) FROM pg_prepared_xacts;");
141 is($result, qq(0), 'transaction is aborted on subscriber');
142
143 ###############################
144 # Check that ROLLBACK PREPARED is decoded properly on crash restart
145 # (publisher and subscriber crash)
146 ###############################
147
148 $node_publisher->safe_psql(
149 'postgres', "
150 BEGIN;
151 INSERT INTO tab_full VALUES (12);
152 INSERT INTO tab_full VALUES (13);
153 PREPARE TRANSACTION 'test_prepared_tab';");
154
155 $node_subscriber->stop('immediate');
156 $node_publisher->stop('immediate');
157
158 $node_publisher->start;
159 $node_subscriber->start;
160
161 # rollback post the restart
162 $node_publisher->safe_psql('postgres',
163 "ROLLBACK PREPARED 'test_prepared_tab';");
164 $node_publisher->wait_for_catchup($appname);
165
166 # check inserts are rolled back
167 $result = $node_subscriber->safe_psql('postgres',
168 "SELECT count(*) FROM tab_full where a IN (12,13);");
169 is($result, qq(0), 'Rows rolled back are not on the subscriber');
170
171 ###############################
172 # Check that COMMIT PREPARED is decoded properly on crash restart
173 # (publisher and subscriber crash)
174 ###############################
175
176 $node_publisher->safe_psql(
177 'postgres', "
178 BEGIN;
179 INSERT INTO tab_full VALUES (12);
180 INSERT INTO tab_full VALUES (13);
181 PREPARE TRANSACTION 'test_prepared_tab';");
182
183 $node_subscriber->stop('immediate');
184 $node_publisher->stop('immediate');
185
186 $node_publisher->start;
187 $node_subscriber->start;
188
189 # commit post the restart
190 $node_publisher->safe_psql('postgres',
191 "COMMIT PREPARED 'test_prepared_tab';");
192 $node_publisher->wait_for_catchup($appname);
193
194 # check inserts are visible
195 $result = $node_subscriber->safe_psql('postgres',
196 "SELECT count(*) FROM tab_full where a IN (12,13);");
197 is($result, qq(2), 'Rows inserted via 2PC are visible on the subscriber');
198
199 ###############################
200 # Check that COMMIT PREPARED is decoded properly on crash restart
201 # (subscriber only crash)
202 ###############################
203
204 $node_publisher->safe_psql(
205 'postgres', "
206 BEGIN;
207 INSERT INTO tab_full VALUES (14);
208 INSERT INTO tab_full VALUES (15);
209 PREPARE TRANSACTION 'test_prepared_tab';");
210
211 $node_subscriber->stop('immediate');
212 $node_subscriber->start;
213
214 # commit post the restart
215 $node_publisher->safe_psql('postgres',
216 "COMMIT PREPARED 'test_prepared_tab';");
217 $node_publisher->wait_for_catchup($appname);
218
219 # check inserts are visible
220 $result = $node_subscriber->safe_psql('postgres',
221 "SELECT count(*) FROM tab_full where a IN (14,15);");
222 is($result, qq(2), 'Rows inserted via 2PC are visible on the subscriber');
223
224 ###############################
225 # Check that COMMIT PREPARED is decoded properly on crash restart
226 # (publisher only crash)
227 ###############################
228
229 $node_publisher->safe_psql(
230 'postgres', "
231 BEGIN;
232 INSERT INTO tab_full VALUES (16);
233 INSERT INTO tab_full VALUES (17);
234 PREPARE TRANSACTION 'test_prepared_tab';");
235
236 $node_publisher->stop('immediate');
237 $node_publisher->start;
238
239 # commit post the restart
240 $node_publisher->safe_psql('postgres',
241 "COMMIT PREPARED 'test_prepared_tab';");
242 $node_publisher->wait_for_catchup($appname);
243
244 # check inserts are visible
245 $result = $node_subscriber->safe_psql('postgres',
246 "SELECT count(*) FROM tab_full where a IN (16,17);");
247 is($result, qq(2), 'Rows inserted via 2PC are visible on the subscriber');
248
249 ###############################
250 # Test nested transaction with 2PC
251 ###############################
252
253 # check that 2PC gets replicated to subscriber
254 $node_publisher->safe_psql(
255 'postgres', "
256 BEGIN;
257 INSERT INTO tab_full VALUES (21);
258 SAVEPOINT sp_inner;
259 INSERT INTO tab_full VALUES (22);
260 ROLLBACK TO SAVEPOINT sp_inner;
261 PREPARE TRANSACTION 'outer';
262 ");
263 $node_publisher->wait_for_catchup($appname);
264
265 # check that transaction is in prepared state on subscriber
266 $result = $node_subscriber->safe_psql('postgres',
267 "SELECT count(*) FROM pg_prepared_xacts;");
268 is($result, qq(1), 'transaction is prepared on subscriber');
269
270 # COMMIT
271 $node_publisher->safe_psql('postgres', "COMMIT PREPARED 'outer';");
272
273 $node_publisher->wait_for_catchup($appname);
274
275 # check the transaction state on subscriber
276 $result = $node_subscriber->safe_psql('postgres',
277 "SELECT count(*) FROM pg_prepared_xacts;");
278 is($result, qq(0), 'transaction is ended on subscriber');
279
280 # check inserts are visible. 22 should be rolled back. 21 should be committed.
281 $result = $node_subscriber->safe_psql('postgres',
282 "SELECT a FROM tab_full where a IN (21,22);");
283 is($result, qq(21), 'Rows committed are on the subscriber');
284
285 ###############################
286 # Test using empty GID
287 ###############################
288
289 # check that 2PC gets replicated to subscriber
290 $node_publisher->safe_psql(
291 'postgres', "
292 BEGIN;
293 INSERT INTO tab_full VALUES (51);
294 PREPARE TRANSACTION '';");
295 $node_publisher->wait_for_catchup($appname);
296
297 # check that transaction is in prepared state on subscriber
298 $result = $node_subscriber->safe_psql('postgres',
299 "SELECT count(*) FROM pg_prepared_xacts;");
300 is($result, qq(1), 'transaction is prepared on subscriber');
301
302 # ROLLBACK
303 $node_publisher->safe_psql('postgres', "ROLLBACK PREPARED '';");
304
305 # check that 2PC gets aborted on subscriber
306 $node_publisher->wait_for_catchup($appname);
307
308 $result = $node_subscriber->safe_psql('postgres',
309 "SELECT count(*) FROM pg_prepared_xacts;");
310 is($result, qq(0), 'transaction is aborted on subscriber');
311
312 ###############################
313 # Test that an empty prepared transaction is not replicated.
314 #
315 # A transaction that is assigned an XID but makes no change decoded by logical
316 # replication (here, via a row lock) must not be sent to the subscriber.
317 # Otherwise the subscriber would receive a PREPARE with no preceding BEGIN
318 # PREPARE and error out, breaking replication.
319 ###############################
320
321 # An empty prepared transaction that is committed.
322 $node_publisher->safe_psql(
323 'postgres', "
324 BEGIN;
325 SELECT a FROM tab_full WHERE a = 1 FOR SHARE;
326 PREPARE TRANSACTION 'test_empty_prepared';
327 COMMIT PREPARED 'test_empty_prepared';");
328
329 # An empty prepared transaction that is rolled back.
330 $node_publisher->safe_psql(
331 'postgres', "
332 BEGIN;
333 SELECT a FROM tab_full WHERE a = 1 FOR SHARE;
334 PREPARE TRANSACTION 'test_empty_prepared';
335 ROLLBACK PREPARED 'test_empty_prepared';");
336
337 # A subsequent normal change must still replicate. Reaching catchup confirms
338 # the apply worker was not stalled by the empty prepared transactions above.
339 $node_publisher->safe_psql('postgres', "INSERT INTO tab_full VALUES (31);");
340 $node_publisher->wait_for_catchup($appname);
341
342 # The empty transactions must not have been prepared on the subscriber.
343 $result = $node_subscriber->safe_psql('postgres',
344 "SELECT count(*) FROM pg_prepared_xacts;");
345 is($result, qq(0), 'empty prepared transaction is not replicated');
346
347 # The subsequent change is visible, so replication is healthy.
348 $result = $node_subscriber->safe_psql('postgres',
349 "SELECT count(*) FROM tab_full WHERE a = 31;");
350 is($result, qq(1),
351 'replication continues after an empty prepared transaction');
352
353 ###############################
354 # copy_data=false and two_phase
355 ###############################
356
357 #create some test tables for copy tests
358 $node_publisher->safe_psql('postgres',
359 "CREATE TABLE tab_copy (a int PRIMARY KEY)");
360 $node_publisher->safe_psql('postgres',
361 "INSERT INTO tab_copy SELECT generate_series(1,5);");
362 $node_subscriber->safe_psql('postgres',
363 "CREATE TABLE tab_copy (a int PRIMARY KEY)");
364 $node_subscriber->safe_psql('postgres', "INSERT INTO tab_copy VALUES (88);");
365 $result =
366 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_copy;");
367 is($result, qq(1), 'initial data in subscriber table');
368
369 # Setup logical replication
370 $node_publisher->safe_psql('postgres',
371 "CREATE PUBLICATION tap_pub_copy FOR TABLE tab_copy;");
372
373 my $appname_copy = 'appname_copy';
374 $node_subscriber->safe_psql(
375 'postgres', "
376 CREATE SUBSCRIPTION tap_sub_copy
377 CONNECTION '$publisher_connstr application_name=$appname_copy'
378 PUBLICATION tap_pub_copy
379 WITH (two_phase=on, copy_data=false);");
380
381 # Wait for initial table sync to finish
382 $node_subscriber->wait_for_subscription_sync($node_publisher, $appname_copy);
383
384 # Also wait for two-phase to be enabled
385 $node_subscriber->poll_query_until('postgres', $twophase_query)
386 or die "Timed out while waiting for subscriber to enable twophase";
387
388 # Check that the initial table data was NOT replicated (because we said copy_data=false)
389 $result =
390 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_copy;");
391 is($result, qq(1), 'initial data in subscriber table');
392
393 # Now do a prepare on publisher and check that it IS replicated
394 $node_publisher->safe_psql(
395 'postgres', "
396 BEGIN;
397 INSERT INTO tab_copy VALUES (99);
398 PREPARE TRANSACTION 'mygid';");
399
400 # Wait for both subscribers to catchup
401 $node_publisher->wait_for_catchup($appname_copy);
402 $node_publisher->wait_for_catchup($appname);
403
404 # Check that the transaction has been prepared on the subscriber, there will be 2
405 # prepared transactions for the 2 subscriptions.
406 $result = $node_subscriber->safe_psql('postgres',
407 "SELECT count(*) FROM pg_prepared_xacts;");
408 is($result, qq(2), 'transaction is prepared on subscriber');
409
410 # Now commit the insert and verify that it IS replicated
411 $node_publisher->safe_psql('postgres', "COMMIT PREPARED 'mygid';");
412
413 $result =
414 $node_publisher->safe_psql('postgres', "SELECT count(*) FROM tab_copy;");
415 is($result, qq(6), 'publisher inserted data');
416
417 # Wait for both subscribers to catchup
418 $node_publisher->wait_for_catchup($appname_copy);
419 $node_publisher->wait_for_catchup($appname);
420
421 # Make sure there are no prepared transactions on the subscriber
422 $result = $node_subscriber->safe_psql('postgres',
423 "SELECT count(*) FROM pg_prepared_xacts;");
424 is($result, qq(0), 'should be no prepared transactions on subscriber');
425
426 $result =
427 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_copy;");
428 is($result, qq(2), 'replicated data in subscriber table');
429
430 # Clean up
431 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub");
432
433 ###############################
434 # Alter the subscription to set two_phase to false.
435 # Verify that the altered subscription reflects the new two_phase option.
436 ###############################
437
438 # Confirm that the two-phase slot option is enabled before altering
439 $result = $node_publisher->safe_psql('postgres',
440 "SELECT two_phase FROM pg_replication_slots WHERE slot_name = 'tap_sub_copy';"
441 );
442 is($result, qq(t), 'two-phase is enabled');
443
444 # Alter subscription two_phase to false
445 $node_subscriber->safe_psql('postgres',
446 "ALTER SUBSCRIPTION tap_sub_copy DISABLE;");
447 $node_subscriber->poll_query_until('postgres',
448 "SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'logical replication apply worker'"
449 );
450 $node_subscriber->safe_psql(
451 'postgres', "
452 ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = false);
453 ALTER SUBSCRIPTION tap_sub_copy ENABLE;");
454
455 # Wait for subscription startup
456 $node_subscriber->wait_for_subscription_sync($node_publisher, $appname_copy);
457
458 # Make sure that the two-phase is disabled on the subscriber
459 $result = $node_subscriber->safe_psql('postgres',
460 "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';"
461 );
462 is($result, qq(d), 'two-phase subscription option should be disabled');
463
464 # Make sure that the two-phase slot option is also disabled
465 $result = $node_publisher->safe_psql('postgres',
466 "SELECT two_phase FROM pg_replication_slots WHERE slot_name = 'tap_sub_copy';"
467 );
468 is($result, qq(f), 'two-phase slot option should be disabled');
469
470 ###############################
471 # Now do a prepare on the publisher and verify that it is not replicated.
472 ###############################
473 $node_publisher->safe_psql(
474 'postgres', qq{
475 BEGIN;
476 INSERT INTO tab_copy VALUES (100);
477 PREPARE TRANSACTION 'newgid';
478 });
479
480 # Wait for the subscriber to catchup
481 $node_publisher->wait_for_catchup($appname_copy);
482
483 # Make sure there are no prepared transactions on the subscriber
484 $result = $node_subscriber->safe_psql('postgres',
485 "SELECT count(*) FROM pg_prepared_xacts;");
486 is($result, qq(0), 'should be no prepared transactions on subscriber');
487
488 ###############################
489 # Set two_phase to "true" and failover to "true" before the COMMIT PREPARED.
490 #
491 # This tests the scenario where both two_phase and failover are altered
492 # simultaneously.
493 ###############################
494 $node_subscriber->safe_psql('postgres',
495 "ALTER SUBSCRIPTION tap_sub_copy DISABLE;");
496 $node_subscriber->poll_query_until('postgres',
497 "SELECT count(*) = 0 FROM pg_stat_activity WHERE backend_type = 'logical replication apply worker'"
498 );
499 $node_subscriber->safe_psql(
500 'postgres', "
501 ALTER SUBSCRIPTION tap_sub_copy SET (two_phase = true, failover = true);
502 ALTER SUBSCRIPTION tap_sub_copy ENABLE;");
503
504 ###############################
505 # Now commit the insert and verify that it is replicated.
506 ###############################
507 $node_publisher->safe_psql('postgres', "COMMIT PREPARED 'newgid';");
508
509 # Wait for the subscriber to catchup
510 $node_publisher->wait_for_catchup($appname_copy);
511
512 # Make sure that the committed transaction is replicated.
513 $result =
514 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_copy;");
515 is($result, qq(3), 'replicated data in subscriber table');
516
517 # Make sure that the two-phase is enabled on the subscriber
518 $result = $node_subscriber->safe_psql('postgres',
519 "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_copy';"
520 );
521 is($result, qq(e), 'two-phase should be enabled');
522
523 $node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_copy;");
524 $node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_copy;");
525
526 ###############################
527 # check all the cleanup
528 ###############################
529
530 $result = $node_subscriber->safe_psql('postgres',
531 "SELECT count(*) FROM pg_subscription");
532 is($result, qq(0), 'check subscription was dropped on subscriber');
533
534 $result = $node_publisher->safe_psql('postgres',
535 "SELECT count(*) FROM pg_replication_slots");
536 is($result, qq(0), 'check replication slot was dropped on publisher');
537
538 $result = $node_subscriber->safe_psql('postgres',
539 "SELECT count(*) FROM pg_subscription_rel");
540 is($result, qq(0),
541 'check subscription relation status was dropped on subscriber');
542
543 $result = $node_subscriber->safe_psql('postgres',
544 "SELECT count(*) FROM pg_replication_origin");
545 is($result, qq(0), 'check replication origin was dropped on subscriber');
546
547 $node_subscriber->stop('fast');
548 $node_publisher->stop('fast');
549
550 done_testing();