0001-add-2-types-of-new-tests-for-2PC.patch
application/octet-stream
Filename: 0001-add-2-types-of-new-tests-for-2PC.patch
Type: application/octet-stream
Part: 0
Patch
Format: format-patch
Series: patch v1-0001
Subject: add 2 types of new tests for 2PC
| File | + | − |
|---|---|---|
| src/test/subscription/t/020_twophase.pl | 97 | 1 |
From 3194ddea80a15b31427a63b18bc75ccb0c8aeb23 Mon Sep 17 00:00:00 2001
From: Osumi Takamichi <osumi.takamichi@fujitsu.com>
Date: Fri, 19 Mar 2021 05:35:46 +0000
Subject: [PATCH v01] add 2 types of new tests for 2PC
Add two scenarioes into existing TAP tests for logical decoding of 2PC.
The first test is to check the data is correctly synced
when two publications are affected within one prepared transaction.
On the other hand, the second one is to check a case
when there are two standbys for one publication.
---
src/test/subscription/t/020_twophase.pl | 98 ++++++++++++++++++++++++++++++++-
1 file changed, 97 insertions(+), 1 deletion(-)
diff --git a/src/test/subscription/t/020_twophase.pl b/src/test/subscription/t/020_twophase.pl
index b135997..ac866b2 100644
--- a/src/test/subscription/t/020_twophase.pl
+++ b/src/test/subscription/t/020_twophase.pl
@@ -3,7 +3,7 @@ use strict;
use warnings;
use PostgresNode;
use TestLib;
-use Test::More tests => 20;
+use Test::More tests => 24;
###############################
# Setup
@@ -311,10 +311,105 @@ $result = $node_subscriber->safe_psql('postgres',
is($result, qq(0), 'transaction is aborted on subscriber');
###############################
+# Test multiple publications by single 2PC execution.
+# Add one more set of publication and subscription
+# and change two tables within one PREPARED TRANSACTION,
+# to affect two corresponding publications at the same time.
+###############################
+$node_publisher->safe_psql('postgres',
+ "CREATE TABLE new_tab (a int PRIMARY KEY);");
+$node_publisher->safe_psql('postgres',
+ "CREATE PUBLICATION new_tap_pub FOR TABLE new_tab;");
+$node_subscriber->safe_psql('postgres',
+ "CREATE TABLE new_tab (a int PRIMARY KEY);");
+my $new_appname = 'new_tap_sub';
+$node_subscriber->safe_psql('postgres',"
+ CREATE SUBSCRIPTION new_tap_sub
+ CONNECTION '$publisher_connstr application_name=$new_appname'
+ PUBLICATION new_tap_pub
+ WITH (two_phase = on)");
+
+my $new_caughtup_query =
+ "SELECT pg_current_wal_lsn() <= replay_lsn FROM pg_stat_replication WHERE application_name = '$new_appname';";
+$node_publisher->poll_query_until('postgres', $new_caughtup_query)
+ or die "Timed out while waiting for subscriber to catch up";
+
+# Table tab_full is connected to tap_pub publication,
+# while table new_tab is associated with new_tap_sub publication.
+$node_publisher->safe_psql('postgres', "
+ BEGIN;
+ INSERT INTO tab_full VALUES (52);
+ INSERT INTO new_tab VALUES (1);
+ PREPARE TRANSACTION 'multiple_publications';
+ COMMIT PREPARED 'multiple_publications';");
+
+$node_publisher->poll_query_until('postgres', $caughtup_query)
+ or die "Timed out while waiting for subscriber to catch up";
+
+$node_publisher->poll_query_until('postgres', $new_caughtup_query)
+ or die "Timed out while waiting for subscriber to catch up";
+
+# check that 2PC gets commited on subscriber for both subscriptions
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT count(*) FROM tab_full where a IN (52);");
+is($result, qq(1), 'first change got committed on the subscriber');
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT count(*) FROM new_tab where a IN (1);");
+is($result, qq(1), 'second change got committed on the subscriber');
+
+###############################
+# Test multiple standbys for single 2PC execution.
+# Add one more subscriber second_tap_sub, besides the existing subscriber tap_sub.
+# Then, run 2PC and check if all are synced correctly.
+###############################
+my $second_subscriber = get_new_node('second_subscriber');
+my $second_appname = 'second_app_sub';
+$second_subscriber->init(allows_streaming => 'logical');
+$second_subscriber->append_conf('postgresql.conf',
+ qq(max_prepared_transactions = 10));
+$second_subscriber->start;
+
+$second_subscriber->safe_psql('postgres',
+ "CREATE TABLE tab_full (a int PRIMARY KEY)");
+
+$second_subscriber->safe_psql('postgres',"
+ CREATE SUBSCRIPTION second_tap_sub
+ CONNECTION '$publisher_connstr application_name=$second_appname'
+ PUBLICATION tap_pub
+ WITH (two_phase = on);");
+
+my $second_caughtup_query =
+ "SELECT pg_current_wal_lsn() <= replay_lsn FROM pg_stat_replication WHERE application_name = '$second_appname';";
+$node_publisher->poll_query_until('postgres', $second_caughtup_query)
+ or die "Timed out while waiting for subscriber to catch up";
+
+$node_publisher->safe_psql('postgres',"
+ BEGIN;
+ INSERT INTO tab_full VALUES (53);
+ PREPARE TRANSACTION 'multiple_standbys';
+ COMMIT PREPARED 'multiple_standbys';");
+
+$node_publisher->poll_query_until('postgres', $second_caughtup_query)
+ or die "Timed out while waiting for subscriber to catch up";
+
+$node_publisher->poll_query_until('postgres', $caughtup_query)
+ or die "Timed out while waiting for subscriber to catch up";
+
+$result = $second_subscriber->safe_psql('postgres',
+ "SELECT count(*) FROM tab_full where a IN (53);");
+is($result, qq(1), 'the second sebscriber got the change');
+
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT count(*) FROM tab_full where a IN (53);");
+is($result, qq(1), 'the first subscriber got the change');
+
+###############################
# check all the cleanup
###############################
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub");
+$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION new_tap_sub");
+$second_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION second_tap_sub");
$result = $node_subscriber->safe_psql('postgres',
"SELECT count(*) FROM pg_subscription");
@@ -334,4 +429,5 @@ $result = $node_subscriber->safe_psql('postgres',
is($result, qq(0), 'check replication origin was dropped on subscriber');
$node_subscriber->stop('fast');
+$second_subscriber->stop('fast');
$node_publisher->stop('fast');
--
2.2.0