PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
This change builds on the applied parallel-wal-segment-uploads: it reads that change's archive progress point and upload snapshot. Backend commands run in backend/ unless a task says otherwise, frontend commands in frontend/. <pg> stands for ./internal/features/backups/backups/usecases/physical/postgresql/..., <core> for ./internal/features/backups/backups/core/physical/... and <backuping> for ./internal/features/backups/backups/backuping/physical/....
Every frontend task that introduces a translation key adds it to all six dictionaries in the same task, written in each language rather than word for word, because the keys are typed against en.ts and every other dictionary is typed typeof en.
1. Storage
1.1 Create the migration from the repository root with
goose -dir backend/migrations create add_physical_wal_stream_samples sqland fill it per design D1 and D5: the samples table, its foreign key todatabaseswith ON DELETE CASCADE, the(database_id, sampled_at)and(sampled_at)indexes, the nullablefailing_sinceTIMESTAMPTZ column onphysical_wal_streamers, and aDownsection that drops them. From the repository root, with.envexported byset -a; source .env; set +a:- run
goose -dir backend/migrations up, thendown, thenupagainst the dev database; - apply it to every test slot database, because
just test-packagedoes not migrate them:for ((i = 0; i < ${TEST_PARALLEL_WORKERS:-8}; i++)); do GOOSE_DRIVER=postgres GOOSE_DBSTRING="$(sed -E "s#/([^/?]+)\?#/\1_w$i?#" <<<"$GOOSE_TEST_DBSTRING")" goose -dir backend/migrations up; done, the loop fromscripts/test.sh:49-54.
Verify that every goose command exits with status 0.
- run
1.2 Add the
PhysicalWalStreamSamplemodel incore/physical/modelsand its repository incore/physical/repositories: insert, delete older than a time across all databases, newest for a database, samples since a time, session starts since a time (bysession_started_at), and the bucketed series (D10). Addfailing_sinceto the streamer model, makeMarkFailedandMarkStaleRunningFailedset it withCOALESCE, and add a method that clears it. The controller tests in section 5 pin the series query. Verify withjust lintinbackend/and withjust test-package <core>passing, the existing streamer repository tests included.
2. Shared health rules
- 2.1 Create the
core/physical/wal_healthpackage (D8). MovewarningSlotStatusHoldPeriod,DefaultArchiveStalenessThresholdandstreamerHeartbeatStalenessinto it as exported constants, and have<pg>and<backuping>use them. ReplaceisWalStreamCandidatewithPhysicalBackupConfig.IsWalStreamingExpected(), which also checksIsBackupsEnabled, and use it in the supervisor. Verify withjust test-package <pg> <backuping> ./internal/features/backups/config/physical/...passing, the existing classifier, staleness and supervisor tests included. - 2.2 Add the streamer state rule and the verdict function (D8) as pure functions. The verdict is complex rule logic, and testing each threshold through HTTP would need a seeded database per case, so it gets a table-driven unit test in
wal_health. Verify withjust test-package <core>passing, covering:- every reason at each of its levels, with values just below and just above each threshold;
- the two-sample rule: "Single unusual sample";
- the spec scenarios "Everything in order", "Short network blip", "Source unreachable for a few minutes", "Source down for a while", "Archive stuck", "Streamer stopped", "Restart after a deploy", "Crash loop on a new database", "Slot removed on the source", "Just enabled", "Backups disabled", "Several problems at once", "Busy cluster with steady extra WAL", "Paused receiver above the rebuild lag", "No limit on the source" and "Slot already lost";
- the gap input set for "Slot rebuilt" gives
WAL_CHAIN_BROKEN, and the same input without a gap gives none; NO_FULL_BACKUP_YETlisted without a completed full backup and the verdict unchanged by it;- "Stale heartbeat" and "Streamer never started" for the streamer state;
- a growth of four segments plus one in the unarchived WAL's sawtooth stays below the backlog threshold.
- 2.3 Add the keeping-up summary (D9) to the same package. Verify with a table-driven test in
wal_health, run throughjust test-package <core>, covering the spec scenarios "Archiving keeps up", "Archiving falls behind", "Catching up after a burst", "Running out of allowance", "Quiet database" and "Quiet database with a stuck backlog", the unknown state for a window without write values, the first-match order for a shrinking backlog on a quiet source, both rates sharing one denominator when the window has a hole, and no rebuild estimate while the slot's status isextended.
3. Sampling inputs in the streamer
- 3.1 Add
safe_wal_sizeandpg_current_wal_lsn()toInspectSlot(D3). Use the real current position in the staleness tracker. Verify with tests against the test PostgreSQL container, run throughjust test-package <pg>: the allowance is NULL withmax_slot_wal_keep_size = -1and positive with a limit set, and the returned position is not below the position read just before the call. The existing staleness tests must still pass. - 3.2 Make the slot probe return reachable with a slot state, reachable without the slot, or unreachable, with a 10-second timeout on its context (D2). Map "without the slot" to the current behavior in the slot-LSN watcher and the resume path. Verify with tests in
slot_lsn_watcher_test.go, run throughjust test-package <pg>:- "Slot removed on the source": dropping the slot on a reachable source gives the "without the slot" outcome;
- "Silent source": a probe against a local TCP listener that accepts and never answers returns unreachable within 11 seconds;
- the existing watcher and resume tests still pass.
- 3.3 Count receiver restarts and pauses with atomic counters that the sampler takes and resets (D5). Verify with a test in
wal_stream_test.go, run throughjust test-package <pg>: one back-pressure pause and one receiver exit of its own give one pause and one restart, and the next take gives zero of each ("Pause for a full local queue"). - 3.4 Set an atomic flag when the rotation loop gets the insufficient-privilege refusal (D5). Verify with a test in
wal_rotation_test.go, run throughjust test-package <pg>, against a test source whose backup user lacksEXECUTEonpg_switch_wal(): the flag is set after the refusal.
4. Sampling in the streamer
- 4.1 Add the pure sample builder (D2, D3, D4). Verify with unit tests in
lag_monitor_test.go, run throughjust test-package <pg>, covering:- "Slot lost on the source": a
loststate keeps the real current position and gives no negative unarchived value; - "Source position moves backwards": no write value and no negative one;
- "Source write volume after a restart" and "Source reachable again";
- no unarchived value while the archive progress point is 0;
- source-side columns NULL for the unreachable and the without-slot outcomes, other columns filled.
- "Slot lost on the source": a
- 4.2 Turn the lag monitor's tick body into a method that takes
now, records a sample built by 4.1 after classifying and before acting (D2), carries the trackers' times, keepssource_unreachable_sinceand seeds it from the newest stored sample at session start, and clearsfailing_sinceon a session's first sample. Pass the repository throughWalStreamSpec. Verify with tests inlag_monitor_test.gothat call the tick method against the test source, run throughjust test-package <pg>, covering:- "Healthy streaming": four ticks give four reachable samples with the listed values;
- "Source unreachable": ticks against a stopped source give unreachable samples sharing one unreachable-since time;
- "Unreachable across a session restart";
- a slot held in
extendedgetsslot_extra_wal_sinceequal to the time the classifier started its hold; - a rebuild in a tick leaves that tick's sample with the slot values probed before the rebuild;
- the first sample of a session clears
failing_since.
- 4.3 Wire the repository in
backuping/physical/wal_supervisor.go. Verify with tests inwal_supervisor_test.go, run throughjust test-package <backuping>, covering:- "Database not streaming": reconcile a full + incremental database alongside a WAL streaming control database, wait until the control database has its first sample, and assert that the full + incremental database has none;
- "Crash loop before the first sample": sessions that fail at start keep the first
failing_since.
- 4.4 Delete samples older than 7 days on each retention cleaner tick (D7). Verify with tests in
cleaner_test.go, run throughjust test-package <backuping>, covering "Expired samples" and "Streaming switched off".
5. Statistics API
- 5.1 Add the read method on the physical core service and
GetWalStreamStatisticsonPhysicalBackupServicewith the current values and the series, authorizing throughdatabaseService.GetDatabaseand refusing a database that is not PostgreSQL physical (D10). Add theGET /backups/physical/database/:id/wal-stream/statisticsroute with Swagger annotations. Seed samples through a helper incore/physical/testing. Verify with controller tests incontrollers/physical/controller_test.go, run throughjust test-package ./internal/features/backups/backups/controllers/physical/..., covering:- "Member of the database's workspace" gets current values and series;
- "User from another workspace", "Unauthenticated request", "Nonexistent database" and "Logical database" are refused, the nonexistent one with no statistics in the body;
- "Unknown period" returns 400;
- "Week view": at most 360 points, each archive rate equal to the seeded WAL bytes of its interval divided by the seconds its samples cover;
- the hour view has 60 one-minute points and the last point contains the current moment;
- "Streamer was down for an hour": the points covering the hole carry NULL values;
- "Database removed": deleting the database leaves no samples;
lastArchivedWalAtequals the newest committed segment'sreceived_at.
- 5.2 Add
ChainViewService.GetNewestCompletedFullWithGaps(D8). Addhealth,streamerandkeepingUpto the response, with the full backup and chain gap inputs from that method,isFullBackupPendingfrom the config'sForceFullRequestedAtand the in-flight backup, and the rebuild lag from the backup config. Add Swagger enum annotations for every code. Verify with controller tests in the same file, run through the same command, covering:- "Codes, not text": a needs-attention database returns a level, reasons and notices whose codes are all in the documented lists, and no string field outside the codes, times and IDs;
- "Streamer stopped": a
FAILEDrow and a newest sample ten minutes old giveBROKENwithSTREAMING_STOPPEDand that sample's time; - "Slot rebuilt": seeded segments with a gap below the progress point after the newest full give
WAL_CHAIN_BROKEN, and seeding a newer completed full clears it; - the same gap with an incremental of the newest full marked chain-broken still gives
WAL_CHAIN_BROKENand noNO_FULL_BACKUP_YET; isFullBackupPendingis true withForceFullRequestedAtset and false without it or an in-flight full;- "Backups disabled" and "Switched out of WAL streaming":
NOT_STREAMINGwith the seeded series still returned; - "Stale heartbeat": a
RUNNINGrow with an old heartbeat gives the not running state.
- 5.3 Regenerate the API docs with
swag init -d . -g cmd/main.go -o swaggerinbackend/. Verify thatgrep -c 'wal-stream/statistics' swagger/swagger.jsonprints at least 1, and thatgrep -c 'WAL_CHAIN_BROKEN' swagger/swagger.jsonprints at least 1.
6. Frontend
- 6.1 Add
shared/lib/formatBytes.ts(D13). Verify with a vitest test next to it covering the B, KB, MB, GB and TB boundaries with base 1024, thenpnpm testandpnpm typecheckpassing. - 6.2 Add the response types, one per file under
entity/backups/physical/model, the label key tables for every code enum with their keys, andphysicalWalStreamStatisticsApiunderentity/backups/physical/api, exported through the slice'sindex.ts. Verify withpnpm typecheckandpnpm testpassing;dictionaries.test.tschecks the placeholders. - 6.3 Add the pure display rules from design D13, except the refresh schedule, to the monitoring slice's
lib/, with the reason, notice, keeping-up and banner sentence keys from the D11 table. Verify with vitest tests next to them, run throughpnpm test, covering:- the tab rules for a logical database, a physical database without WAL streaming and a WAL streaming database;
- "Healthy database" and "Problems listed": the banner tone per level, broken reasons first;
- "Every reason has an action": every reason and notice code maps to both a "what happened" and a "what to do" key;
- "Broken chain without a pending full backup":
WAL_CHAIN_BROKENandSLOT_MISSINGmap to the "start a full backup" action without a pending full and to the "on its way" action with one; - "Plain names": no default label or sentence in the English dictionary for this slice contains "WAL", "slot", "LSN" or "lag", except "what to do" sentences that name a setting;
- "Falling behind explained": parameters of about 9 MB/s, 3.4 GB and about 32 GB an hour;
- "Consequence only when near": the consequence sentence present for an estimate under a day and absent for three days;
- "Backlog in the local queue": the Databasus stage in the needs-attention color and the source stage in the healthy color;
- an amount above its limit marked as over the limit;
- "Archiving falls behind on the chart": rows mapped from 21 and 12 MB/s points put the archive series below the source series at every point and keep the not-saved-yet series rising;
- NULL points staying gaps, and "No history yet" when no point has values;
- "Refresh keeps failing": a last successful refresh older than 2 minutes gives the "cannot confirm" banner with the last known level;
- durations rounded to minutes under an hour, hours and minutes under a day, days and hours beyond.
- 6.4 Add the refresh schedule (D12) as a small controller in the slice's
lib/with the timer and the page visibility injected. Verify with vitest tests using fake timers, run throughpnpm test, covering: a refresh every 30 seconds in every period ("Status changes while open"); none while hidden and one at once when visible again ("Browser tab hidden"); one at once on a period change; a failed refresh keeps the previous response and the next one still runs. - 6.5 Build the monitoring slice's UI per design D11 and D12: the verdict banner with its live indicator and
aria-liveheadline, the reasons and notices, the path of changes, the period selector, the three synchronized charts with formatted axes and the empty state, the collapsed details, the first-load skeleton and error with Retry, the token-based colors and the reduced-motion rules. Take every display decision from 6.3 and the schedule from 6.4. Verify withpnpm lint,pnpm typecheckandpnpm testpassing. - 6.6 Add the
'monitoring'tab toDatabaseComponentbehind the predicate from 6.3, and let the not streaming banner's link switch to the Config tab. Verify withpnpm typecheckandpnpm testpassing.
7. Wrap-up
- 7.1 Run the formatters and linters for both modules. Verify with
just formatandjust lintat the repository root finishing clean.
Change verification
just test-package ./internal/features/backups/backups/core/physical/...
just test-package ./internal/features/backups/backups/usecases/physical/postgresql/...
just test-package ./internal/features/backups/backups/backuping/physical/...
just test-package ./internal/features/backups/config/physical/...
just test-package ./internal/features/backups/backups/controllers/physical/...
pnpm --dir frontend test