diff --git a/patroni/postgresql.py b/patroni/postgresql.py index 816474a3..e15a4996 100644 --- a/patroni/postgresql.py +++ b/patroni/postgresql.py @@ -391,21 +391,24 @@ recovery_target_timeline = 'latest' def sync_replication_slots(self, cluster): if self.use_slots: - self.load_replication_slots() - slots = [m.name for m in cluster.members if m.name != self.name] if self.role == 'master' else [] - # drop unused slots - for slot in set(self.replication_slots) - set(slots): - self.query("""SELECT pg_drop_replication_slot(%s) - WHERE EXISTS(SELECT 1 FROM pg_replication_slots - WHERE slot_name = %s)""", slot, slot) + try: + self.load_replication_slots() + slots = [m.name for m in cluster.members if m.name != self.name] if self.role == 'master' else [] + # drop unused slots + for slot in set(self.replication_slots) - set(slots): + self.query("""SELECT pg_drop_replication_slot(%s) + WHERE EXISTS(SELECT 1 FROM pg_replication_slots + WHERE slot_name = %s)""", slot, slot) - # create new slots - for slot in set(slots) - set(self.replication_slots): - self.query("""SELECT pg_create_physical_replication_slot(%s) - WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots - WHERE slot_name = %s)""", slot, slot) + # create new slots + for slot in set(slots) - set(self.replication_slots): + self.query("""SELECT pg_create_physical_replication_slot(%s) + WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots + WHERE slot_name = %s)""", slot, slot) - self.replication_slots = slots + self.replication_slots = slots + except: + logger.exception('Exception when changing replication slots') def last_operation(self): return str(self.xlog_position()) diff --git a/tests/test_patroni.py b/tests/test_patroni.py index 2c7998c4..1c82799b 100644 --- a/tests/test_patroni.py +++ b/tests/test_patroni.py @@ -66,7 +66,6 @@ class TestPatroni(unittest.TestCase): @patch('time.sleep', Mock(side_effect=SleepException())) def test_run(self): self.p.touch_member = self.touch_member - self.p.ha.state_handler.sync_replication_slots = time_sleep self.p.ha.dcs.watch = time_sleep self.assertRaises(SleepException, self.p.run) diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index e7e39594..90874288 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -162,6 +162,9 @@ class TestPostgresql(unittest.TestCase): self.p.start() cluster = Cluster(True, self.leader, 0, [self.me, self.other, self.leadermem], None) self.p.sync_replication_slots(cluster) + self.p.query = Mock(side_effect=psycopg2.OperationalError) + self.p.schedule_load_slots = True + self.p.sync_replication_slots(cluster) @patch.object(MockConnect, 'closed', 2) def test__query(self):