-
Notifications
You must be signed in to change notification settings - Fork 52
feat(module-postgres): early warning and mitigation for WAL slot invalidation during snapshot #554
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f7d42b3
test(module-postgres): add failing tests for mid-snapshot WAL slot in…
Sleepful f62b55c
test(module-postgres): add shouldRetryReplication() stub and failing …
Sleepful 1766336
feat(module-postgres): detect WAL slot invalidation during snapshot
Sleepful 948b075
feat(module-postgres): implement conditional retry for WAL slot inval…
Sleepful a5d1288
feat(module-postgres): add WAL budget reporting during snapshot
Sleepful f58bd78
feat(module-postgres): add PSYNC_S1146 error code and actionable erro…
Sleepful e74b110
feat(diagnostics): surface WAL budget fields in diagnostics API
Sleepful 8e3d1a6
fix(module-postgres): tailor slot-invalidation guidance to the invali…
Sleepful 8c0e9a6
fix(changeset): add missing service-errors package and broaden summary
Sleepful bf18b01
refactor(module-postgres): make MissingReplicationSlotError fields re…
Sleepful 48f42d6
refactor(module-postgres): throttle slot health check, remove docs UR…
Sleepful aed41f0
refactor(module-postgres): replace string literals with WalStatus and…
Sleepful 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@powersync/service-types': patch | ||
| '@powersync/service-core': patch | ||
| '@powersync/service-module-postgres': patch | ||
| '@powersync/service-errors': patch | ||
| --- | ||
|
|
||
| Detect WAL slot invalidation mid-snapshot, warn on WAL budget depletion, block futile retries, and surface WAL budget in the diagnostics API. |
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
59 changes: 59 additions & 0 deletions
59
modules/module-postgres/src/replication/MissingReplicationSlotError.ts
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,59 @@ | ||
| /** | ||
| * Replication slot WAL status from `pg_replication_slots.wal_status` (PG 13+). | ||
| * - `reserved`: WAL needed by the slot is within `max_wal_size` | ||
| * - `extended`: WAL needed exceeds `max_wal_size` but is protected by `max_slot_wal_keep_size` | ||
| * - `unreserved`: WAL may be removed at next checkpoint — slot is at risk | ||
| * - `lost`: WAL has been removed — slot is invalidated and unusable | ||
| * - `missing`: synthetic value — the slot row is absent from `pg_replication_slots` | ||
| */ | ||
| export type WalStatus = 'reserved' | 'extended' | 'unreserved' | 'lost' | 'missing'; | ||
|
|
||
| /** | ||
| * Replication phase when the error was detected. | ||
| * - `snapshot`: during an active full-table scan (snapshotTable) — long-running, retry may be futile | ||
| * - `streaming`: during WAL streaming, at startup, or between replication cycles — retry is safe | ||
| */ | ||
| export type ReplicationPhase = 'snapshot' | 'streaming'; | ||
|
|
||
| export class MissingReplicationSlotError extends Error { | ||
| /** Slot WAL status at the time the error was detected. */ | ||
| walStatus: WalStatus; | ||
| /** Replication phase when the error occurred — controls retry behavior. */ | ||
| phase: ReplicationPhase; | ||
| /** PG 14+ invalidation reason from `pg_replication_slots.invalidation_reason`. */ | ||
| invalidationReason?: string; | ||
|
|
||
| constructor( | ||
| message: string, | ||
| options: { | ||
| walStatus: WalStatus; | ||
| phase: ReplicationPhase; | ||
| invalidationReason?: string; | ||
| cause?: any; | ||
| } | ||
| ) { | ||
| super(message); | ||
| this.cause = options.cause; | ||
| this.walStatus = options.walStatus; | ||
| this.phase = options.phase; | ||
| this.invalidationReason = options.invalidationReason; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether replication should be retried after a slot invalidation. | ||
| * | ||
| * Returns false when retry would be futile (e.g. slot lost during snapshot | ||
| * due to WAL budget exhaustion — retrying would repeat the same long snapshot | ||
| * and likely fail again). | ||
| * | ||
| * Blocks retry when walStatus is 'lost' during snapshot phase (unless the | ||
| * invalidation reason is 'rows_removed', which is not a WAL budget issue). | ||
| * Allows retry in all other cases. | ||
| */ | ||
| export function shouldRetryReplication(error: MissingReplicationSlotError): boolean { | ||
| if (error.walStatus === 'lost' && error.phase === 'snapshot' && error.invalidationReason !== 'rows_removed') { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
Sleepful marked this conversation as resolved.
|
||
2 changes: 1 addition & 1 deletion
2
modules/module-postgres/src/replication/PostgresErrorRateLimiter.ts
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.