Some time it happens like Master is up & running but Standby gets promoted due to false failover under this situation first question raises is "how to rebuild the standby"?
Now, let's rewind the promoted cluster to make old standby as new standby of current Master.
pg_rewind: pg_rewind is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master.
pg_rewind requires that the target server either has the wal_log_hints option enabled in postgresql.conf or data checksums enabled when the cluster was initialized with initdb.
How it works:
copies file system-level changes from the source cluster to the target cluster:
It will check the divorced point of Master and Standby and from depending on divorced point it will decide from where it has to apply the changes on target cluster.
Apply the WAL from the source cluster, starting from the checkpoint created at failover.
Lets do the setup and test the pg_rewind:
On Master Set parameters:
wal_level = replica
archive_mode='on'
archive_command='cp %p /tmp/archive/%f'
wal_keep_segments = 500
max_wal_senders=4
wal_log_hints = on
Initially, I have built the replication Master:5432 and Standby:5433.
I have promoted the standby and deleted records from Standby
Master is Master and Standby is also accepting the DML's (Replication is broken)
Now instead of taking the full base backup and building the replication again just use pg_rewind and build the replication in short time.
Once pg_rewind is successfully completed then you will see the backup_label file in Standby's data directory.
I have created the recovery.conf file in Standby cluster and started it
Now let's check the status of replication?
Data is also streamed to Standby Cluster.
This ways pg_rewind helps you to rebuild the false promoted Standby or Master as standby.
In an Earlier version of PostgreSQL < 9.6 pg_rewind use to work only in one direction i.e If Standby got promoted then using pg_rewind user can build the Master as new standby for new Master.
From 9.6 user can use pg_rewind in both the direction.
Always try to Keep your Environments up-to-date to use this kind of features.
Now, let's rewind the promoted cluster to make old standby as new standby of current Master.
pg_rewind: pg_rewind is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master.
pg_rewind requires that the target server either has the wal_log_hints option enabled in postgresql.conf or data checksums enabled when the cluster was initialized with initdb.
How it works:
copies file system-level changes from the source cluster to the target cluster:
It will check the divorced point of Master and Standby and from depending on divorced point it will decide from where it has to apply the changes on target cluster.
Apply the WAL from the source cluster, starting from the checkpoint created at failover.
Lets do the setup and test the pg_rewind:
On Master Set parameters:
wal_level = replica
archive_mode='on'
archive_command='cp %p /tmp/archive/%f'
wal_keep_segments = 500
max_wal_senders=4
wal_log_hints = on
Initially, I have built the replication Master:5432 and Standby:5433.
I have promoted the standby and deleted records from Standby
Master is Master and Standby is also accepting the DML's (Replication is broken)
Now instead of taking the full base backup and building the replication again just use pg_rewind and build the replication in short time.
Once pg_rewind is successfully completed then you will see the backup_label file in Standby's data directory.
I have created the recovery.conf file in Standby cluster and started it
Now let's check the status of replication?
Data is also streamed to Standby Cluster.
This ways pg_rewind helps you to rebuild the false promoted Standby or Master as standby.
In an Earlier version of PostgreSQL < 9.6 pg_rewind use to work only in one direction i.e If Standby got promoted then using pg_rewind user can build the Master as new standby for new Master.
From 9.6 user can use pg_rewind in both the direction.
Always try to Keep your Environments up-to-date to use this kind of features.