pg.ddx.io patroni/patroni GitHub issues and pull requests
help / color / mirror / Atom feed[patroni/patroni] PR #3814: Reset synchronous replication state on demote
7+ messages / 3 participants
[nested] [flat]
* [patroni/patroni] PR #3814: Reset synchronous replication state on demote
@ 2026-11-19 21:59 "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
0 siblings, 0 replies; 7+ messages in thread
From: CyberDem0n (@CyberDem0n) @ 2026-11-19 21:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
`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.patch
diff --git a/patroni/postgresql/__init__.py b/patroni/postgresql/__init__.py
index 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.py
index 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.py
index 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 = [
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [patroni/patroni] PR #3814: Reset synchronous replication state on demote
@ 2026-11-19 21:59 ` "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: CyberDem0n (@CyberDem0n) @ 2026-11-19 21:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@CyberDem0n requested a review.
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [patroni/patroni] PR #3814: Reset synchronous replication state on demote
@ 2026-11-19 21:59 ` "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: CyberDem0n (@CyberDem0n) @ 2026-11-19 21:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@CyberDem0n applied label "bug".
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [patroni/patroni] PR #3814: Reset synchronous replication state on demote
@ 2026-11-19 22:07 ` "codecov[bot] (bot) (@codecov[bot])" <noreply+codecov[bot]@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: codecov[bot] (bot) (@codecov[bot]) @ 2026-11-19 22:07 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
## [Codecov](https://app.codecov.io/gh/patroni/patroni/pull/3814?dropdown=coverage&src=pr&el=h1&utm_m...) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:loudspeaker: Thoughts on this report? [Let us know!](https://github.com/codecov/feedback/issues/255)
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [patroni/patroni] PR #3814 (approved): Reset synchronous replication state on demote
@ 2026-11-20 09:25 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-20 09:25 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [patroni/patroni] PR #3814: Reset synchronous replication state on demote
@ 2026-11-20 09:25 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-20 09:25 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@hughcapet merged this (commit 94167da77872).
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [patroni/patroni] PR #3814: Reset synchronous replication state on demote
@ 2026-11-20 09:25 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-20 09:25 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@hughcapet closed this.
^ permalink raw [nested|flat] 7+ messages in thread
end of thread, other threads:[~2026-11-20 09:25 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 21:59 [patroni/patroni] PR #3814: Reset synchronous replication state on demote "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
2026-11-19 21:59 ` "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
2026-11-19 21:59 ` "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
2026-11-19 22:07 ` "codecov[bot] (bot) (@codecov[bot])" <noreply+codecov[bot]@github.com>
2026-11-20 09:25 ` Re: [patroni/patroni] PR #3814 (approved): Reset synchronous replication state on demote "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
2026-11-20 09:25 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
2026-11-20 09:25 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox