Hello @MRizwanShaukat you can use the parameters “synchronous_mode”, “synchronous_commit” “synchronous_standby_names”, and patroni will take care of the standby names. For example:
bootstrap:
dcs:
ttl: 30
maximum_lag_on_failover: 1048576
synchronous_mode: "on"
postgresql:
use_pg_rewind: true
use_slots: true
parameters:
wal_level: replica
hot_standby: "on"
synchronous_commit: "on"
synchronous_standby_names: "*"
Note that in the below example I’m using synchronous_standby_names: “*” this way Patroni will take care of the standby name. In this example I have a setup with 3 nodes (node1, node2, node3) and node1 is the primary. If I check the param I get:
test=> show synchronous_standby_names;
-[ RECORD 1 ]-------------+------------
synchronous_standby_names | "node2"
(1 row)
root@lab-node2:~# patronictl -c /percona/config/patroni.yml list
+ Cluster: cluster_1 (7208383656948843317) --------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-----------+-------------+--------------+---------+----+-----------+
| node1 | 10.0.100.13 | Leader | running | 1 | |
| node2 | 10.0.100.11 | Sync Standby | running | 1 | 0 |
| node3 | 10.0.100.6 | Replica | running | 1 | 0 |
+-----------+-------------+--------------+---------+----+-----------+
If I cause node2 to crash then I get:
test=> show synchronous_standby_names;
synchronous_standby_names
---------------------------
"node3"
(1 row)
root@lab-node2:~# patronictl -c /percona/config/patroni.yml list
+ Cluster: cluster_1 (7208383656948843317) --------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-----------+-------------+--------------+---------+----+-----------+
| node1 | 10.0.100.13 | Leader | running | 1 | |
| node3 | 10.0.100.6 | Sync Standby | running | 1 | 0 |
+-----------+-------------+--------------+---------+----+-----------+
As we can see, Patroni took care of the Sync Standby.
Now, in the case I lose all my nodes (I’m using more ETCD servers than PG nodes in this example to be able to have only one PG node running as leader) Patroni will empty the config:
test=> show synchronous_standby_names;
synchronous_standby_names
---------------------------
(1 row)
test=> select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
test=> insert into t1 select from generate_series(1,1000);
INSERT 0 1000
root@lab-node2:~# patronictl -c /percona/config/patroni.yml list
+ Cluster: cluster_1 (7208383656948843317) --+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-----------+-------------+--------+---------+----+-----------+
| node1 | 10.0.100.13 | Leader | running | 1 | |
+-----------+-------------+--------+---------+----+-----------+
My primary is still running and responsive as if it wasn’t using sync replicas.
Note that I am NOT using “synchronous_mode_strict” because this parameter prevents Patroni from switching off the synchronous replication on the primary when no synchronous standby candidates are available.
You may find more information in Patroni’s documentation here[1].
Hope this helps.
Charly
[1] Replication modes — Patroni 3.0.1 documentation