What is a process for recovering a failed master from a slave with PostgreSQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In any database setup, ensuring high availability and disaster recovery is crucial for maintaining the integrity and accessibility of data. One of the common practices to achieve this in PostgreSQL is using a master-slave replication architecture. This setup helps in disaster scenarios, where a master database fails, allowing a slave database to be promoted as a master. Here's a detailed process to recover a failed master from a slave in PostgreSQL.
Understanding Master-Slave Replication
Before diving into the recovery process, it's crucial to understand how master-slave replication works in PostgreSQL. In this setup:
- Master Database: The primary server that handles data modifications.
- Slave Database: Receives data updates from the master through a continuous replication process, often using the built-in streaming replication feature.
Process for Promoting a Slave to Master
Step 1: Verify the Slave Status
Ensure that the slave database is fully synchronized with the master before promotion. Check the replication lag, which indicates the current state of synchronization.
Step 2: Perform a Failover
Once you are confident about the slave's readiness:
- Promote the Slave: Execute the
pg_ctlcommand to promote the slave to a master.
Alternatively, you can use the following SQL command if you have configured trigger_file:
- Verify the Promotion: Make sure the database is now the master by checking the server's role.
A result of false implies that the server is now the master.
Step 3: Reconfigure the Failed Master as a Slave
- Shutdown the Old Master: Ensure the old master is shut down to prevent any data inconsistencies:
- Reinitialize the Old Master Server Data: Assuming data synchronization, reinitialize the database with the base backup from the new master.
- Configure the Recovery Settings: Review and modify the
recovery.conffile as needed to perform to act as a slave.
Step 4: Restart Database Services
After configuring, restart the services:
Step 5: Verify the Setup
Ensure the old master is now functioning as a slave. Use replication monitoring queries:
Check for the presence of the old master as a replication client.
Key Points Summary
| Task | Description | Tools/Commands |
| Verify Slave Status | Ensure slave's readiness for promotion | SQL queries on replication status |
| Failover Procedure | Promote a slave to a master | pg_ctl promote, |
| Reconfigure Old Master | Convert the old master into a slave | pg_basebackup, recovery.conf |
| Restart and Verify | Restart services and check the configuration | pg_ctl start, replication monitoring |
Final Thoughts
Database failover and recovery processes are crucial in high-availability setups. While the procedure outlined targets PostgreSQL versions generally supporting streaming replication, some refinements may vary based on the configuration. Always ensure a robust monitoring system is in place to detect the state and health of the database cluster in real-time.

