summaryrefslogtreecommitdiff
path: root/src/test/recovery
diff options
context:
space:
mode:
authorAmit Kapila2024-01-30 11:01:09 +0000
committerAmit Kapila2024-01-30 11:19:28 +0000
commit776621a5e4796fa214b6b29a7ca134f6c138572a (patch)
treeba58dcb69a247bc073eca865879e730c5790ce3a /src/test/recovery
parentb527ebc1d37aa82b771dc9c76111bed1bce35a05 (diff)
Add a failover option to subscriptions.
This commit introduces a new subscription option named 'failover', which provides users with the ability to set the failover property of the replication slot on the publisher when creating or altering a subscription. This uses the replication commands introduced by commit 7329240437 to enable the failover option for a logical replication slot. If the failover option is set to true, the associated replication slots (i.e. the main slot and the table sync slots) in the upstream database are enabled to be synchronized to the standbys. Note that the capability to sync the replication slots will be added in subsequent commits. Thanks to Masahiko Sawada for the design inputs. Author: Shveta Malik, Hou Zhijie, Ajin Cherian Reviewed-by: Peter Smith, Bertrand Drouvot, Dilip Kumar, Masahiko Sawada, Nisha Moond, Kuroda Hayato, Amit Kapila Discussion: https://postgr.es/m/514f6f2f-6833-4539-39f1-96cd1e011f23@enterprisedb.com
Diffstat (limited to 'src/test/recovery')
-rw-r--r--src/test/recovery/meson.build1
-rw-r--r--src/test/recovery/t/040_standby_failover_slots_sync.pl100
2 files changed, 101 insertions, 0 deletions
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index 88fb0306f54..bf087ac2a9e 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -45,6 +45,7 @@ tests += {
't/037_invalid_database.pl',
't/038_save_logical_slots_shutdown.pl',
't/039_end_of_wal.pl',
+ 't/040_standby_failover_slots_sync.pl',
],
},
}
diff --git a/src/test/recovery/t/040_standby_failover_slots_sync.pl b/src/test/recovery/t/040_standby_failover_slots_sync.pl
new file mode 100644
index 00000000000..bc58ff4cab2
--- /dev/null
+++ b/src/test/recovery/t/040_standby_failover_slots_sync.pl
@@ -0,0 +1,100 @@
+
+# Copyright (c) 2024, PostgreSQL Global Development Group
+
+use strict;
+use warnings;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+##################################################
+# Test that when a subscription with failover enabled is created, it will alter
+# the failover property of the corresponding slot on the publisher.
+##################################################
+
+# Create publisher
+my $publisher = PostgreSQL::Test::Cluster->new('publisher');
+$publisher->init(allows_streaming => 'logical');
+$publisher->start;
+
+$publisher->safe_psql('postgres',
+ "CREATE PUBLICATION regress_mypub FOR ALL TABLES;");
+
+my $publisher_connstr = $publisher->connstr . ' dbname=postgres';
+
+# Create a subscriber node, wait for sync to complete
+my $subscriber1 = PostgreSQL::Test::Cluster->new('subscriber1');
+$subscriber1->init;
+$subscriber1->start;
+
+# Create a slot on the publisher with failover disabled
+$publisher->safe_psql('postgres',
+ "SELECT 'init' FROM pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, false);"
+);
+
+# Confirm that the failover flag on the slot is turned off
+is( $publisher->safe_psql(
+ 'postgres',
+ q{SELECT failover from pg_replication_slots WHERE slot_name = 'lsub1_slot';}
+ ),
+ "f",
+ 'logical slot has failover false on the publisher');
+
+# Create a subscription (using the same slot created above) that enables
+# failover.
+$subscriber1->safe_psql('postgres',
+ "CREATE SUBSCRIPTION regress_mysub1 CONNECTION '$publisher_connstr' PUBLICATION regress_mypub WITH (slot_name = lsub1_slot, copy_data=false, failover = true, create_slot = false, enabled = false);"
+);
+
+# Confirm that the failover flag on the slot has now been turned on
+is( $publisher->safe_psql(
+ 'postgres',
+ q{SELECT failover from pg_replication_slots WHERE slot_name = 'lsub1_slot';}
+ ),
+ "t",
+ 'logical slot has failover true on the publisher');
+
+##################################################
+# Test that changing the failover property of a subscription updates the
+# corresponding failover property of the slot.
+##################################################
+
+# Disable failover
+$subscriber1->safe_psql('postgres',
+ "ALTER SUBSCRIPTION regress_mysub1 SET (failover = false)");
+
+# Confirm that the failover flag on the slot has now been turned off
+is( $publisher->safe_psql(
+ 'postgres',
+ q{SELECT failover from pg_replication_slots WHERE slot_name = 'lsub1_slot';}
+ ),
+ "f",
+ 'logical slot has failover false on the publisher');
+
+# Enable failover
+$subscriber1->safe_psql('postgres',
+ "ALTER SUBSCRIPTION regress_mysub1 SET (failover = true)");
+
+# Confirm that the failover flag on the slot has now been turned on
+is( $publisher->safe_psql(
+ 'postgres',
+ q{SELECT failover from pg_replication_slots WHERE slot_name = 'lsub1_slot';}
+ ),
+ "t",
+ 'logical slot has failover true on the publisher');
+
+##################################################
+# Test that the failover option cannot be changed for enabled subscriptions.
+##################################################
+
+# Enable subscription
+$subscriber1->safe_psql('postgres',
+ "ALTER SUBSCRIPTION regress_mysub1 ENABLE");
+
+# Disable failover for enabled subscription
+my ($result, $stdout, $stderr) = $subscriber1->psql('postgres',
+ "ALTER SUBSCRIPTION regress_mysub1 SET (failover = false)");
+ok( $stderr =~ /ERROR: cannot set failover for enabled subscription/,
+ "altering failover is not allowed for enabled subscription");
+
+done_testing();