-
Notifications
You must be signed in to change notification settings - Fork 45
Use native PG17+ logical slot failover; retire spock worker on PG18. #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ibrarahmad
wants to merge
12
commits into
main
Choose a base branch
from
SPOC-74
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
479b506
Use native PG17+ logical slot failover; retire spock worker on PG18.
2b552c2
Use parenthesised (FAILOVER) syntax in CREATE_REPLICATION_SLOT and ad…
4060397
Address review feedback and fix PQserverVersion on replication connec…
c4c05a7
Fix InvalidTransactionId handling in failover slot XID comparisons.
e871808
Fix PG15/16 failover slot loss when primary stops before promotion
73675cb
Merge branch 'main' into SPOC-74
ibrarahmad c11a699
Update for PostgreSQL 17
3f9f8d1
Merge branch 'main' into SPOC-74
ibrarahmad c834f3c
Fix failover slot issue.
c505652
Update pg_failover_slot.
2e3b360
Fix restart_lsn floor check to use redo pointer unconditionally.
32da0a1
Guard against zero WAL flush LSN in slot synchronization.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # Logical Slot Failover | ||
|
|
||
| Spock creates logical replication slots on each provider node. For high | ||
| availability with a physical standby, these slots must be synchronized to the | ||
| standby so that replication can resume without data loss after a failover. | ||
|
|
||
| ## How It Works | ||
|
|
||
| When a primary server fails and a physical standby is promoted, any active | ||
| logical subscribers must be able to continue replicating from the new primary. | ||
| This requires the logical replication slots — which track each subscriber's | ||
| replication position — to be present and up to date on the standby before the | ||
| failover occurs. | ||
|
|
||
| Without slot synchronization, a failover would require manual slot recreation | ||
| and a full re-sync of all subscriber tables. | ||
|
|
||
| ## PostgreSQL Version Behaviour | ||
|
|
||
| | PostgreSQL | Slot sync mechanism | Spock worker | | ||
| |---|---|---| | ||
| | 15, 16 | Spock built-in `spock_failover_slots` worker | Always runs on standby | | ||
| | 17 | Spock worker **or** native `sync_replication_slots` | Yields to native if enabled | | ||
| | 18+ | Native `sync_replication_slots` (required) | Not registered | | ||
|
|
||
| On **PostgreSQL 17+**, Spock marks every logical slot with the `FAILOVER` flag | ||
| at creation time. This enables PostgreSQL's built-in slotsync worker to pick | ||
| them up automatically. | ||
|
|
||
| On **PostgreSQL 18+**, Spock's own failover worker is not registered at all. | ||
| The native slotsync worker is the only mechanism. | ||
|
|
||
| ## Setup: PostgreSQL 18+ (Native) | ||
|
|
||
| ### 1. Create a physical replication slot on the primary | ||
|
|
||
| ```sql | ||
| SELECT pg_create_physical_replication_slot('spock_standby_slot'); | ||
| ``` | ||
|
|
||
| ### 2. Configure the primary (`postgresql.conf`) | ||
|
|
||
| ```ini | ||
| # Hold walsenders back until the standby has confirmed this LSN, | ||
| # preventing logical subscribers from getting ahead of the standby. | ||
| synchronized_standby_slots = 'spock_standby_slot' | ||
| ``` | ||
|
|
||
| ### 3. Configure the standby (`postgresql.conf`) | ||
|
|
||
| ```ini | ||
| sync_replication_slots = on | ||
| primary_conninfo = 'host=<primary_host> port=5432 dbname=<dbname> user=replicator' | ||
| primary_slot_name = 'spock_standby_slot' | ||
| hot_standby_feedback = on | ||
| ``` | ||
|
|
||
| ### 4. Verify slot synchronization | ||
|
|
||
| On the standby, confirm that Spock's logical slots are synchronized: | ||
|
|
||
| ```sql | ||
| SELECT slot_name, synced, failover, invalidation_reason | ||
| FROM pg_replication_slots | ||
| WHERE NOT temporary; | ||
| ``` | ||
|
|
||
| All Spock slots should show `synced = true` and `failover = true`. | ||
|
|
||
| ### 5. After failover | ||
|
|
||
| After promoting the standby, subscribers only need to update their connection | ||
| string to point to the new primary. Replication resumes from the last | ||
| synchronized LSN with no data loss and no slot recreation required. | ||
|
|
||
| ## Setup: PostgreSQL 15 and 16 (Spock Worker) | ||
|
|
||
| On PostgreSQL 15 and 16, the `spock_failover_slots` background worker runs | ||
| on the standby and periodically copies slot state from the primary. | ||
|
|
||
| ### Requirements | ||
|
|
||
| - `hot_standby_feedback = on` on the standby (required for the worker to run) | ||
| - The standby must be able to connect to the primary | ||
|
|
||
| ### Configuration GUCs | ||
|
|
||
| | GUC | Default | Description | | ||
| |---|---|---| | ||
| | `spock.synchronize_slot_names` | `name_like:%%` | Slot name patterns to sync (all by default) | | ||
| | `spock.drop_extra_slots` | `on` | Drop standby slots not matching the pattern | | ||
| | `spock.primary_dsn` | `''` | DSN to connect to primary (falls back to `primary_conninfo`) | | ||
| | `spock.pg_standby_slot_names` | `''` | Physical slots that must confirm LSN before logical replication advances | | ||
| | `spock.standby_slots_min_confirmed` | `-1` | How many slots from `pg_standby_slot_names` must confirm (`-1` = all) | | ||
|
|
||
| ### Example (`postgresql.conf` on standby) | ||
|
|
||
| ```ini | ||
| hot_standby_feedback = on | ||
| spock.synchronize_slot_names = 'name_like:%%' | ||
| spock.drop_extra_slots = on | ||
|
|
||
| # Optional: hold walsenders on primary until this standby confirms | ||
| # (set this on the PRIMARY, not the standby) | ||
| # spock.pg_standby_slot_names = 'physical_slot_name' | ||
| ``` | ||
|
|
||
| ## Monitoring | ||
|
|
||
| ### Check slot sync status (PG17+) | ||
|
|
||
| ```sql | ||
| SELECT slot_name, | ||
| failover, | ||
| synced, | ||
| active, | ||
| invalidation_reason, | ||
| confirmed_flush_lsn | ||
| FROM pg_replication_slots | ||
| WHERE NOT temporary | ||
| ORDER BY slot_name; | ||
| ``` | ||
|
|
||
| ### Check if native slotsync worker is active (PG17+) | ||
|
|
||
| ```sql | ||
| SELECT pid, wait_event_type, wait_event, state | ||
| FROM pg_stat_activity | ||
| WHERE backend_type = 'slot sync worker'; | ||
| ``` | ||
|
|
||
| ### Check spock worker is running (PG15/16) | ||
|
|
||
| ```sql | ||
| SELECT pid, application_name, state | ||
| FROM pg_stat_activity | ||
| WHERE application_name = 'spock_failover_slots worker'; | ||
| ``` | ||
|
|
||
| ## FAQ | ||
|
|
||
| **Q: Do I need to do anything after a failover?** | ||
|
|
||
| On PG17+: Just update the subscriber's `host=` in their DSN. No slot | ||
| recreation needed. | ||
|
|
||
| On PG15/16: Spock's worker on the standby (now primary) stops running | ||
| since it is no longer in recovery. Subscribers reconnect automatically. | ||
|
|
||
| **Q: What if `sync_replication_slots` is not configured on PG18?** | ||
|
|
||
| Spock's worker is not registered on PG18. If `sync_replication_slots = on` | ||
| is not set, logical slots will **not** be synchronized to standbys, and a | ||
| failover will require manual slot recreation and table re-sync. | ||
|
|
||
| **Q: Can I use both mechanisms on PG17?** | ||
|
|
||
| No. If `sync_replication_slots = on` is set on PG17, Spock's worker detects | ||
| this and skips its sync loop, deferring to the native worker entirely. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.