What's changed: Initial version (topic G4)
4.3Replication failures and recovery
Learn how to handle failures while running replication: pg_ctl promote, which promotes a standby to primary; pg_rewind, which re-synchronizes an old primary as a standby of the new one; pg_receivewal, which continuously receives WAL directly; the conflicts unique to logical replication; and error handling for streaming replication.
Handling failures in a replication setup adds a dimension beyond single-server failure handling: how do you reorganize the primary/standby relationship? Promotion (failover) when the primary is completely lost, re-synchronizing an old primary back into the new topology, guarding against interruptions in WAL delivery, and the apply-time conflicts unique to logical replication—these concentrate much of the hands-on judgment within Gold's failure-response domain.
4.3.1Promotion and re-sync: pg_ctl promote and pg_rewind
- pg_ctl promote—the command that promotes a streaming-replication standby to become the new primary. It is the core operation of failover on primary failure: after running it, the standby exits read-only mode and begins operating as an ordinary read/write primary.
- pg_rewind—a tool that re-configures the old primary (the original primary, once recovered from its failure) as a standby following the new primary's timeline. It rewinds and re-synchronizes only the divergent portion of the WAL history between the old and new primaries, so it restores the old primary faster than rebuilding it entirely from a base backup. It does, however, require the old primary's WAL, so it must be used soon after the divergence occurs.
- pg_receivewal—a standalone client tool (formerly pg_receivexlog) that continuously receives the WAL stream from the primary and keeps saving it as files. Besides serving as a dedicated WAL archive destination, it is also used as a supplementary way to preserve WAL while normal archiving (archive_command) is not functioning for some reason.
4.3.2Logical replication conflicts and error handling
- Logical replication conflicts—a state where the subscriber fails to apply a change. This can occur, for example, when a subscriber-side table has a UNIQUE constraint that does not exist on the publisher side, and a replicated INSERT violates it. Once a conflict occurs, the apply worker stops at that point, and every subsequent change stalls too, so an ignored conflict brings replication to a complete halt.
- Resolving a conflict requires manual intervention on the subscriber side (deleting or fixing the conflicting data, or skipping the offending transaction) to let replication resume. It is important that this is a failure mode unique to logical replication, with no equivalent in physical copying like streaming replication.
- Streaming replication error handling—when the connection between the standby's walreceiver and the primary's walsender drops, the basic behavior is to automatically attempt reconnection, depending on configuration. If the cause is transient (a network blip, a temporarily overloaded primary), reconnecting resolves it—but if WAL beyond the needed range has already been discarded (the standby fell behind past a retention window such as
wal_keep_size), reconnection alone cannot recover, and you must take a fresh base backup.
The staples: pg_ctl promote promotes a standby to primary; pg_rewind fast-re-syncs the old primary as a standby of the new one (faster than retaking a base backup); pg_receivewal continuously receives WAL as an archive supplement; a logical replication conflict stops the apply worker and requires manual intervention; streaming replication reconnects automatically by default, but a fresh base backup is needed if WAL has been discarded. The choice between pg_rewind (differential re-sync) and PITR/re-taking a base backup (full rebuild) is a classic comparison point.
Let's trace a failover response end to end. Suppose the primary becomes unresponsive due to a hardware failure. Monitoring detects the anomaly, and you select the standby that has received the most recent WAL for promotion (with multiple standbys, the replication lag you had been watching via pg_stat_replication is the deciding factor). Running pg_ctl promote on the chosen standby lets it start accepting writes as the new primary. The complication arises when the original primary turns out not to be completely destroyed and can later be recovered. Simply restarting the old primary alongside the new one creates a split-brain situation where both accept writes and consistency collapses—this must be avoided at all costs. The correct procedure is to re-fold the old primary in as a standby subordinate to the new primary. This is where pg_rewind comes in. The old and new primaries diverge into separate timelines the instant promotion happens, but pg_rewind fetches and overwrites only the data blocks that differ since the divergence, from the new primary, letting you reshape the old primary into "a healthy standby following the new primary" faster than rebuilding it entirely from a base backup. This does assume the old primary still retains the post-divergence WAL, so you need to act soon after detecting the failure. If the post-divergence WAL is already gone, or the divergence is too large for pg_rewind to handle, it is more reliable to simply retake a base backup with pg_basebackup. One more often-overlooked point in replication operations is the role of pg_receivewal. Even if normal WAL archiving via archive_command stops working for a while due to some misconfiguration, running pg_receivewal in parallel preserves that period's WAL as files, preventing a gap that would otherwise bite you the next time PITR is needed.
| Tool | Purpose | Nature |
|---|---|---|
| pg_ctl promote | Promote a standby to primary | The core failover operation |
| pg_rewind | Re-configure the old primary as a standby of the new one | Differential re-sync; faster than re-basing, but needs the old primary's WAL |
| pg_receivewal | Continuously receive and save WAL | Also usable as a supplement when archive_command is not working |
Trap: "once the old primary recovers, you can just start it back up and keep it running as a primary" is wrong—if the old primary accepts writes alongside the already-promoted new primary, you get split-brain and consistency collapses; the old primary must be re-configured as a standby of the new primary, e.g. via pg_rewind. Also, "a logical replication conflict resolves itself automatically and processing continues" is wrong—a conflict stops the apply worker and does not clear without manual intervention. "pg_receivewal is exclusively for streaming replication and cannot substitute for archiving" is also wrong—in practice it is used as a supplementary way to preserve WAL during periods when archive_command is not working.
4.3.3Section summary
- pg_ctl promote = promoting a standby to primary (the core of failover). pg_rewind = differentially re-syncing the old primary as a standby of the new one (faster than re-basing, but assumes the old primary's WAL is available)
- pg_receivewal continuously receives WAL and can also preserve it when archive_command is malfunctioning. A logical replication conflict stops the apply worker and requires manual intervention. Streaming replication reconnects automatically by default, but WAL loss requires retaking a base backup
Sign in to track progress — Log in.
Quick check
(just a quick review)Q1. The primary server has become completely unresponsive due to a hardware failure. Which command should you run to make the running standby writable as the new primary?
Q2. After a promotion, you want to bring the still-operable old primary back as a standby subordinate to the new primary. Which method re-synchronizes faster than retaking a base backup from scratch?
Q3. On a logical replication subscriber, a change arrives that violates a UNIQUE constraint not present on the publisher, and the apply worker stops. What is the correct description of this situation and the right response?
Keep track of your progress
The full study guide is free to read. Sign up free to practice with the question bank, track what you have read, review your mistakes, and highlight passages.

