PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
1. Pin the bug with a failing test
- 1.1 Add
Test_CleanOrphanWalForDatabase_WhenFullStartsMidSegment_KeepsBoundarySegmenttobackend/internal/features/backups/backups/backuping/physical/cleaner_test.go: seed a COMPLETED FULL whosestart_lsnandstop_lsnboth fall inside segment 1 (offset0x28past the boundary, matching the field report), seed the archived segment 1 with file-aligned bounds, runcleanOrphanWalForDatabase, assert the segment still exists. Verify withmake testinbackend/that the test fails against the current predicate and names the deleted segment in its failure message. - 1.2 Add
Test_FindWalOrphansByDatabase_WhenFullStartsMidSegment_BoundarySegmentIsNotOrphantobackend/internal/features/backups/backups/core/physical/chain_view/service_test.go, next to the aligned-LSN case at line 343 so the two alignments sit side by side. Verify it fails for the same reason viamake test.
2. Fix the predicate
2.1 Change the anti-join in
PhysicalWalSegmentRepository.FindOrphansfromf.start_lsn <= w.start_lsn(backend/internal/features/backups/backups/core/physical/repositories/wal_segment_repository.go:100) tof.start_lsn < w.end_lsn. Verify both tests from group 1 pass and the existingTest_CleanOrphanWalForDatabase_WhenWalOutsideAllChains_DeletesOrphan,Test_CleanOrphanWalForDatabase_WhenWalCoveredByChain_KeepsItandTest_FindWalOrphansByDatabase_WhenWalOutsideAllChainSpans_ReturnsOrphanstill pass.2.2 Add a second why line to the comment above
FindOrphans(wal_segment_repository.go:80) stating that the comparison anchors onend_lsnbecause a FULL'sstart_lsnsits mid-segment while a segment's bounds are file-aligned. Keep the existing sequential-scan note, which is unrelated and still accurate. Verify withmake lintinbackend/and by the group 1 tests still passing.2.3 Add
Test_DeleteFull_WhenSuccessorStartsMidSegment_KeepsSuccessorBoundarySegmenttobackend/internal/features/backups/backups/core/physical/service/service_test.go: prune a chain whose successor FULL starts mid-segment and assert the successor's boundary segment and its stored object survive. Not in the original plan, which assumedFindOrphanswas the only place the alignment mistake lives. It covers the delta spec's "The anchoring backup is pruned" scenario, which the predicate fix alone does not satisfy.2.4 Bound
deleteWalInSpanBudgetedonend_lsn <= span.Endinstead ofstart_lsn < span.End(backend/internal/features/backups/backups/core/physical/service/service.go:604), so a chain's deletion span stops at segment boundaries rather than cutting into the successor's boundary segment. Reached from retention pruning (cascadeDeleteviaGetChainSpan) and from the user-initiatedDeleteWalSegment, both of which end their span on a mid-segment anchor LSN.
3. Make the sweep drivable from the end-to-end suite
- 3.1 Add a single-pass cleaner entry point for tests to
backend/internal/features/backups/backups/backuping/physical/, alongsideStartPhysicalSchedulerForTestandStartPhysicalWalStreamSupervisorForTestinbackend/internal/features/tests/physical/postgresql/shared/setup.go.Run(cleaner.go:40) cannot serve: it is an infinite ticker that panics on a second call. Verify withmake testthat a test can run one sweep and observe its effect without waiting outcleanerTickInterval.
4. Cover the sequence end to end
- 4.1 Add an exported
Run...scenario tobackend/internal/features/tests/physical/postgresql/shared/that runs the sweep between the backup and the restore: take a FULL, stream WAL past a target time viaseedChainAndStreamPastTarget(shared/helpers.go:786), run one cleaner pass from 3.1, then request a restore token for that target and assert it is issued rather than refused with 422. Wire it into bothpg17/backup_restore_test.goandpg18/backup_restore_test.goas one-line dispatchers, matching every existing case. Verify withmake test(ormake test-fedoraon a Fedora host) that both versions pass and that they fail when the predicate from 2.1 is reverted. - 4.2 Add a case asserting the sweep still reclaims WAL that sits entirely below the earliest COMPLETED FULL's
start_lsn. The existingTest_CleanOrphanWalForDatabase_WhenWalOutsideAllChains_DeletesOrphan(cleaner_test.go:236) seeds no FULL at all, so it covers the "no COMPLETED FULL on this timeline" branch and never this one. Verify withmake test.
5. Close out
- 5.1 Run
make lintandmake testinbackend/; fix every finding before finishing. - 5.2 Run the
reviewersubagent over the working-tree diff and resolve every CHANGES REQUIRED finding. - 5.3 Put the upgrade note in the pull request description, since the repository keeps no changelog file: installations affected before the upgrade must take a fresh FULL backup, because the deleted WAL cannot be recovered, and the symptom is a
latest restorable pointequal to the FULL'sstop_lsn. Verify the PR description contains it before requesting review.
6. Cover FULL execution windows
- 6.1 Make
ResolveRestoreSetquery WAL throughLSNMaxafter selecting the completed base backup, instead of limiting PITR to the selected chain's retention span. - 6.2 Serialize FULL claim creation and orphan deletion with the per-database advisory lock, then recheck the live FULL claim and completed anchor in the deletion transaction.
- 6.3 Add regressions for PITR during a newer FULL and WAL archived while the first FULL has an active claim.
7. Preserve shared WAL during cascade deletion
- 7.1 Add a regression that deletes a successor FULL while its predecessor remains, asserts that no WAL is removed, and resolves a target from the predecessor afterward.
- 7.2 Make cascade deletion skip WAL when an older COMPLETED FULL remains on the same database and timeline. Keep the existing ownership span for a FULL with no surviving predecessor.
- 7.3 Make
GetDependentsSummaryuse the same WAL selection rule and fully-contained predicate as cascade deletion, including count and size.
8. Complete regression coverage
- 8.1 Strengthen the restore-set regression so the target lies after the successor's stop position but before its completion time, and assert that all available contiguous WAL is returned.
- 8.2 Add a restore-token controller regression for a target during a newer FULL.
- 8.3 Add a cleaner regression proving that an
IN_PROGRESSFULL row without a live claim does not retain orphan WAL.
9. Validate the completed change
- 9.1 Run focused chain-view, physical-service, cleaner, scheduler, and controller tests. Confirm the new deletion and preview regressions fail when their production change is reverted.
- 9.2 Run
make lintand the full backend test suite. On Fedora, record the expected host-nativemake testloader failure forlibedit.so.2, then run the supportedmake test-fedorashim to completion. - 9.3 Run the mandatory implementation reviewer and resolve every
CHANGES REQUIREDfinding.
10. Serialize cascade deletion with active FULL creation
- 10.1 Acquire the database backup-and-cleanup advisory lock before cascade deletion locks its target FULL, then recheck the active FULL claim inside the transaction.
- 10.2 Preserve WAL in both full-removal and
DeleteChainDependentsKeepFullpaths while a FULL claim is active. Preserve timeline history in the full-removal path until the active FULL publishes a completed anchor. - 10.3 Add a concurrency regression for both cascade modes. Hold the advisory lock, start deletion, commit a FULL row and claim, then prove deletion waited and retained the WAL row and object.
- 10.4 Make
GetDependentsSummaryuse the shared active-claim decision under the advisory lock, and cover a preview/delete pair that both report zero WAL and history while a FULL claim is active.