#3730·patroni

Potential data loss when admitting synchronous replicas

Author: MepemotrarialCreated Sep 23, 2026Updated Sep 25, 2026
Labelsbug

What happened?

Setting synchronous_standby_names and sleeping for 100ms is, in theory, not enough for the configuration to take effect. If the Postgres' checkpointer process is delayed for any reason for more than 100ms, a data loss window is opening up. Here is the chain of events that could lead to this:

  1. A new replica is joining up the cluster
  2. Patroni adds it to the SSN list
  3. Patroni waits for 100ms
  4. Patroni assumes that SSN is in effect and records primary's flush LSN (say, L1)
  5. The replica is at L1 - it is connected and streaming even though Postres is still not waiting for it to acknowledge
  6. Patroni adds replica to sync_standby
  7. A new transaction (L2) comes in
  8. Since SSN is still not in effect this transaction is immediately acknowledged to the client
  9. The primary crashes at that point
  10. The replica (considered in-sync due to its presence in sync_standby) is promoted but without the transaction

SSN comes in effect only after the Postgres' checkpointer has acted on it and updated the value of this configuration in the shared memory -- the value used by the comitting Postgres processes.

How can we reproduce it (as minimally and precisely as possible)?

  1. Create an HA Patroni cluster
  2. Freeze Patroni agent on the replica This is done so that we can terminate the replica's Postgres without the agent restarting it. Again, make sure that you are targeting the replica pod.
  3. Terminate the replica's Postgres This is done so that synchronous_standby_names is reset on the primary. We need this property to go from empty to a list containing the replica so that we can validate the claim that the property does not take effect until the checkpointer acts on it.
  4. Verify that the replication has stopped on the primary, i.e.: empty pg_stat_replication, empty synchronous_standby_names
  5. Freeze the primary's checkpointer background process This is done to 'simulate' a slow checkpointer. E.g.:
bash
kill -STOP $(psql -t -A -c "select pid from pg_stat_activity where backend_type = 'checkpointer'")
  1. Bring the replica back It streams, catches up, its walsender flips to sync. Patroni's next loop writes the non-empty SSN (log: Assigning synchronous_standby_names to ...), sleeps 100ms, reads it back, stamps _primary_flush_lsn, and once the replica passes that stamp writes it into the sync ConfigMap (log: Synchronous standby status assigned to [...]). You can validate SSN on the primary (show synchronous_standby_names) and the sync_standby key in the ConfigMap.
  2. Freeze the walsender on the primary This is done so that committed transactions are not streamed over, and we have time to fail/fence the primary before it had a chance to replicate. In reality this could happen in the replication connection is slow.
  3. Insert some data Observe how this transaction is not blocked on a replica acknowledgment. The checkpointer hasn't acted on the SSN config change, and it is not yet in effect, regardless of all other signals claiming otherwise.
  4. Fence the primary to force a failover Fencing requires to just suspend the Patroni agent.
  5. Observe that the replica promotes in ~30s.
  6. Restart the former primary Kill the former primary to simulate it healed and rejoined the cluster, now as replica.

After the cluster has fully recovered, you can observe that the transaction that we committed earlier and that was acknowledged to us is no longer present, hense data is lost.

While this reproduction involves a lot of process manipulation, it is only done because of the racy nature of this scenrario and does not necessarily make it impossible to happen in reality.

What did you expect to happen?

If a failover happens after a replica is admitted to sync_standby, there should not be a data loss.

Patroni/PostgreSQL/DCS version

  • Patroni version: 4.1.5
  • PostgreSQL version: 18
  • DCS (and its version): Kubernetes; Client Version: v1.31.1, Server Version: v1.31.0

Patroni configuration file

yaml
namespace: default
name: pg1

restapi:
  listen: 0.0.0.0:8008
  connect_address: 10.244.16.67:8008

postgresql:
  listen: '*:5432'
  connect_address: 10.244.16.67:5432

bootstrap:
  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    synchronous_mode: true
    postgresql:
      use_pg_rewind: true
      parameters:
        ssl: true
  initdb:

kubernetes:
  use_endpoints: false
  namespace: default

patronictl show-config

yaml
loop_wait: 10
postgresql:
  parameters:
    ssl: true
  pg_hba:
  - local all all trust
  - host all all 127.0.0.1/32 trust
  - host all all ::1/128 trust
  - host all all 0.0.0.0/0 md5
  use_pg_rewind: true
retry_timeout: 10
synchronous_mode: true
ttl: 30

Patroni log files

bash
<Irrelevant but will provide if necessary>

PostgreSQL log files

bash
<Irrelevant but will provide if necessary>

Have you tried to use GitHub issue search?

  • Yes

Anything else we need to know?

No response