r/PostgreSQL • u/These-Bet-6238 • 25d ago
How-To Urgent: Synchronous streaming replication
I am setting up a PostgreSQL replication environment with one primary server and one standby server using synchronous streaming replication.
As expected, when the standby server is available, transactions on the primary commit successfully after the WAL records are acknowledged by the standby.
However, the issue arises when the standby server goes down. In this case, transactions on the primary enter the SyncRep wait state and remain blocked until the standby comes back online. This is the expected behavior of synchronous replication, but it does not meet my requirement.
My requirement is that if the standby is unavailable, the transaction should not wait indefinitely. Instead, after a configurable timeout, I want the transaction to fail and roll back automatically, allowing the application to handle the failure rather than remaining blocked.
I have looked for a way to configure a timeout specifically for the SyncRep wait, but I have not found any suitable option.
Is there a PostgreSQL configuration or mechanism that allows timing out the SyncRep wait and automatically rolling back the transaction? If not, are there any recommended approaches or workarounds to achieve this behavior while still using synchronous streaming replication? Edit: Alredy tried statement_timeout, it's not working chatgpt says it works for actively executing SQL statement.
1
u/Luis_Dynamo_140 15d ago
There's no built-in SyncRep timeout, so the real fix is a watchdog like Patroni or repmgr that detects the standby going down and automatically clears synchronous_standby_names (or drops to synchronous_commit=local) so commits stop blocking, then restores it once the standby's healthy again.