Potential data loss when admitting synchronous replicas
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:
- A new replica is joining up the cluster
- Patroni adds it to the SSN list
- Patroni waits for 100ms
- Patroni assumes that SSN is in effect and records primary's flush LSN (say, L1)
- The replica is at L1 - it is connected and streaming even though Postres is still not waiting for it to acknowledge
- Patroni adds replica to
sync_standby - A new transaction (L2) comes in
- Since SSN is still not in effect this transaction is immediately acknowledged to the client
- The primary crashes at that point
- 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)?
- Create an HA Patroni cluster
- 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.
- Terminate the replica's Postgres
This is done so that
synchronous_standby_namesis 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. - Verify that the replication has stopped on the primary, i.e.: empty
pg_stat_replication, emptysynchronous_standby_names - Freeze the primary's checkpointer background process This is done to 'simulate' a slow checkpointer. E.g.:
kill -STOP $(psql -t -A -c "select pid from pg_stat_activity where backend_type = 'checkpointer'")- 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 syncConfigMap(log: Synchronous standby status assigned to [...]). You can validate SSN on the primary (show synchronous_standby_names) and thesync_standbykey in theConfigMap. - 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.
- 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.
- Fence the primary to force a failover Fencing requires to just suspend the Patroni agent.
- Observe that the replica promotes in ~30s.
- 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
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: defaultpatronictl show-config
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: 30Patroni log files
<Irrelevant but will provide if necessary>PostgreSQL log files
<Irrelevant but will provide if necessary>Have you tried to use GitHub issue search?
- Yes
Anything else we need to know?
No response
Source: patroni/patroni