Postgresql – recover slave after out of sync with master using WAL

All we need is an easy explanation of the problem, so here it is.

I’ve setup a master / slave Postgresql (13.4) group, 1 master and 1 slave, using WAL warm standby.
(refer: https://postgresql.org/docs/current/warm-standby.html#STANDBY-SERVER-SETUP)

But, due to some reason, the slave is out of sync for a while, then due to the WAL file not exists anymore on the master, the slave can’t sync anymore.

Question:
A. Is there anyway to recover the slave, to get back to sync ?
B. Or is there anyway to re-create the slave without stop the running master?

Either A or B is acceptable.

Thanks.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Is there anyway to recover the slave, to get back to sync ?

No, unless you have archive WAL of all segments between current standby and primary server

Or is there anyway to re-create the slave without stop the running master?

Just simple native pg_basebackup command.

  • stop current replica
  • remove datadir (and tablespaces if any) on replica
  • run pg_basebackup with appropriate oprions (typically I use -R -X stream -c fast -P -U <replicationuser> -h source_addr, -R is mandatory to start replica, not new primary)
  • start DB and watch its logs

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply