fix: use atomic writes for parallel WAL restore spool#198
Open
lambcode-unified wants to merge 1 commit intocloudnative-pg:mainfrom
Open
fix: use atomic writes for parallel WAL restore spool#198lambcode-unified wants to merge 1 commit intocloudnative-pg:mainfrom
lambcode-unified wants to merge 1 commit intocloudnative-pg:mainfrom
Conversation
Fixes a race condition where MoveOut could read partially-written WAL files during parallel restore, causing "invalid checkpoint record" errors. Downloads now write to .tmp files and atomically rename on completion. Co-Authored-By: Claude <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes cloudnative-pg/cloudnative-pg#10092: a race condition where MoveOut could read partially-Summary
Fixes a race condition in parallel WAL restore (cloudnative-pg/cloudnative-pg#10092) that can cause PostgreSQL recovery to fail with "invalid checkpoint record" errors when maxParallel > 1.
Problem
When restoring WAL files in parallel, prefetched files are written directly to the spool directory with their final filename. If PostgreSQL requests a file while it's still being downloaded, MoveOut()
can copy a partially-written file to pg_wal/, causing recovery to fail.
Timeline of race condition:
T0: Goroutine starts downloading WAL-B to spool/WAL-B
T1: Download in progress (file partially written)
T2: PostgreSQL requests WAL-B
T3: MoveOut() sees file exists, copies partial file to pg_wal/
T4: PostgreSQL reads corrupt WAL → "invalid checkpoint record"
Solution
Use atomic writes with a staging pattern:
Since the temp file and final file are on the same filesystem, os.Rename() is atomic on POSIX systems - there's no window where a partial file is visible.
Changes
Testing
=== RUN TestCatalog
Ran 10 of 10 Specs in 0.003 seconds
SUCCESS! -- 10 Passed | 0 Failed | 0 Pending | 0 Skipped
Key tests that verify the fix: