From: CyberDem0n (@CyberDem0n) <noreply+CyberDem0n@github.com>
To: patroni/patroni <noreply+patroni-patroni@github.com>
Subject: [patroni/patroni] PR #3814: Reset synchronous replication state on demote
Date: Thu, 19 Nov 2026 21:59:25 +0000
Message-ID: <gh-patroni-patroni-3814@github.com> (raw)
`SyncHandler` caches the last observed value of `synchronous_standby_names`, its parsed representation, the WAL position that replicas have to reach (`_primary_flush_lsn`), and replicas that were verified to reach it (`_ready_replicas`). Nothing of that was reset when the node stopped being a primary.
Therefore, when a node was demoted and promoted again (e.g. A → B → A failovers), and `synchronous_standby_names` set before promote was equal to the last value observed during its previous primary tenure, the change wasn't detected. `_primary_flush_lsn` from the previous tenure was used, and replicas could be counted as synchronous without reaching the WAL position of the new primary.
Now `SyncHandler.reset_state()` is called from `Postgresql.set_role()` when the new role is neither primary nor promoted.
This PR is independent and can be merged on its own. It is extracted from #3467 and is the bottom of the stack that replaces it.
---
Stack replacing #3467, bottom to top:
1. #3814 Reset synchronous replication state on demote (independent, can be merged on its own) ← **this PR**
2. #3815 Verify that walsenders apply synchronous_standby_names before updating /sync
3. #3816 Make sure checkpointer enforces synchronous_standby_names
4. #3817 Make remaining quorum members catch up when quorum becomes stricter
inline patch 1: patroni/postgresql/__init__.py, patroni/postgresql/sync.py, tests/test_sync.py (+36/-0 lines) download inline-1.patchdiff --git a/patroni/postgresql/__init__.py b/patroni/postgresql/__init__.pyindex 69775e4886..da205d7647 100644--- a/patroni/postgresql/__init__.py+++ b/patroni/postgresql/__init__.py@@ -723,6 +723,9 @@ def role(self) -> PostgresqlRole:
def set_role(self, value: PostgresqlRole) -> None:
with self._role_lock:
self._role = value
+ if value not in (PostgresqlRole.PRIMARY, PostgresqlRole.MASTER, PostgresqlRole.PROMOTED) \+ and hasattr(self, 'sync_handler'):+ self.sync_handler.reset_state()
@property
def state(self) -> PostgresqlState:
diff --git a/patroni/postgresql/sync.py b/patroni/postgresql/sync.pyindex 727a2402b8..76aa1851df 100644--- a/patroni/postgresql/sync.py+++ b/patroni/postgresql/sync.py@@ -295,6 +295,16 @@ class SyncHandler(object):
def __init__(self, postgresql: 'Postgresql') -> None:
self._postgresql = postgresql
+ self.reset_state()++ def reset_state(self) -> None:+ """Forget everything known about ``synchronous_standby_names`` and replicas.++ Must be called when the node stops being a primary. Otherwise values cached during the previous primary+ tenure would be trusted after the next promotion. For example, if ``synchronous_standby_names`` set before+ promote is equal to the last value observed by the former primary, replicas would be counted as synchronous+ without reaching the WAL position of the new primary.+ """
self._synchronous_standby_names = '' # last known value of synchronous_standby_names
self._ssn_data = deepcopy(_EMPTY_SSN)
self._primary_flush_lsn = 0
diff --git a/tests/test_sync.py b/tests/test_sync.pyindex 99e4f893b2..8919372dfe 100644--- a/tests/test_sync.py+++ b/tests/test_sync.py@@ -7,6 +7,7 @@
from patroni.dcs import Cluster, ClusterConfig, Status, SyncState
from patroni.postgresql import Postgresql, PostgresqlState
from patroni.postgresql.config import ConfigHandler
+from patroni.postgresql.misc import PostgresqlRole
from patroni.postgresql.slots import SlotsHandler
from . import BaseTestPostgresql, mock_available_gucs, psycopg_connect
@@ -103,6 +104,28 @@ def test_current_state_quorum(self):
CaseInsensitiveSet([self.me.name, self.leadermem.name]),
CaseInsensitiveSet([self.leadermem.name, self.me.name])))
+ @patch('time.sleep', Mock())+ def test_current_state_after_repromote(self):+ self.cluster.config.data['synchronous_mode'] = 'quorum'+ global_config.update(self.cluster)+ a, b = self.leadermem.name, self.me.name+ state = {'synchronous_standby_names': 'ANY 1 ({0},"{1}")'.format(a, b), 'synchronous_commit': 'on'}+ with patch.object(Postgresql, '_cluster_info_state_get', side_effect=lambda name: state[name]), \+ patch.object(Postgresql, 'last_operation', Mock(return_value=1)) as mock_last_operation:+ state['pg_stat_replication'] = [+ {'pid': 100, 'application_name': a, 'sync_state': 'quorum', 'flush_lsn': 1},+ {'pid': 101, 'application_name': b, 'sync_state': 'quorum', 'flush_lsn': 1}]+ self.assertEqual(self.s.current_state(self.cluster).sync_confirmed, CaseInsensitiveSet([a, b]))++ # demoted and promoted again, synchronous_standby_names set before promote has the same value+ self.p.set_role(PostgresqlRole.REPLICA)+ self.p.set_role(PostgresqlRole.PROMOTED)+ mock_last_operation.return_value = 3+ state['pg_stat_replication'] = [+ {'pid': 200, 'application_name': a, 'sync_state': 'quorum', 'flush_lsn': 2},+ {'pid': 201, 'application_name': b, 'sync_state': 'quorum', 'flush_lsn': 3}]+ self.assertEqual(self.s.current_state(self.cluster).sync_confirmed, CaseInsensitiveSet([b]))+
@patch.object(Postgresql, 'last_operation', Mock(return_value=1))
def test_current_state_cascading(self):
pg_stat_replication = [