Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid split brain in PostgreSQL without 3rd party tool

I have 2 PostgreSQL servers and I have set up streaming replication between them. I have built a shell script which will ping to master server every minute and will promote the slave as master when master won't respond. I am using rh-postgresql95 and 3rd party tools are not working with this version.

My JDBC connection string has comma separated nodes with targetServerType=master like below

jdbc:postgresql://node1,node2/accounting?targetServerType=master. 

I just want to know how can I avoid split brain scenario if the slave is promoted to master and old master also comes up somehow?

or

Is there anyway so that old master never comes up automatically?

EDIT

node1 is master and node2 is the slave in my JDBC connection string.

I stopped postgres service on master and promoted the slave to New Master. In this case, service was pointing to the new master.

Then I restarted postgres service on old master and service started pointing to the old master(node1 is old master's ip and it comes first in JDBC connection string.).

So, I didn't get split brain issue, but this scenario will lead to a data inconsistency.

like image 938
YogeshR Avatar asked Feb 18 '26 15:02

YogeshR


1 Answers

As an idea, your ping script could check if both servers think they are masters:

select pg_is_in_recovery();

A server that is not in recovery is a master. Then you could check the last received WAL's number:

select pg_last_wal_receive_lsn();

The server with the highest LSN is the server that was promoted last. You could then shut down the other server.

If you change your mind about third party options, have a look at the PostgresSQL wiki.

like image 87
Andomar Avatar answered Feb 20 '26 06:02

Andomar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!