PostgreSQL Streaming Replication Reject Insert

153 Views Asked by At

I have Postgresql 14 and I made streaming replication (remote_apply) for 3 nodes.

When the two standby nodes are down, if I tried to do an insert command this will show up:

WARNING:  canceling wait for synchronous replication due to user request
DETAIL:  The transaction has already committed locally, but might not have been replicated to the standby.
INSERT 0 1

I don't want to insert it locally. I want to reject the transaction and show an error instead.

Is it possible to do that?

3

There are 3 best solutions below

0
Laurenz Albe On

No, there is no way to do that with synchronous replication.

0
jjanes On

I don't think you have thought through the implications of what you want. If it doesn't commit locally first, then what should happen if the master crashes after sending the transaction to the replica, but before getting back word that it was applied there? If it was committed on the replica but rejected on the master, how would they ever get back into sync?

1
Mohammed Alali On

I made a script that checks the number of standby nodes and then make the primary node read-only if the standby nodes are down.