Skip to content

feat(backup): create PostgreSQL archives while the Gateway runs - #185236

Merged
steipete merged 1 commit into
openclaw:mainfrom
etzelm:feat/backup-online-pg
Oct 30, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
etzelm:feat/backup-online-pg

Conversation

@etzelm

@etzelm etzelm commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Related: #183094

What Problem This Solves

openclaw backup create refuses to run while the Gateway is up once any store is on PostgreSQL, so every routine backup on a PostgreSQL install means taking the Gateway down for the whole archive.

User Impact

User impact: operators on PostgreSQL can take a verified whole-installation archive without stopping the Gateway; it keeps serving requests during the capture.

On one production install with state, agents, and Workboard on PostgreSQL, the nightly offline archive on 2026-10-26 stopped the Gateway at 05:00:04 and it was healthy again at 05:23:35 (15 GB archive, verified). The same morning's update stopped the Gateway at 07:42:39 and the archive step alone ran until 08:08:48. With this change, that nightly backup no longer needs downtime.

Unchanged and still offline-only: backup restore, Git backups, generic local snapshots that select PostgreSQL, update capture, and Doctor database ports. With the Gateway stopped, backup create keeps taking offline custody exactly as before.

Consistency note for review (per the storage review checkpoint, this touches reader consistency): with the Gateway running, each PostgreSQL store is exported from its own REPEATABLE READ READ ONLY snapshot, and stores are captured one after another, not at one shared instant. That is the same per-store consistency the archive already gives live SQLite stores. Rows committed after a store's snapshot belong to the next backup. Operators who need one quiescent point across all stores can still stop the Gateway first.

Why This Change Was Made

The PostgreSQL export already reads each store inside one repeatable-read, read-only transaction, so it is safe to run beside a live writer. The archive was refused only because it borrowed the offline-custody gate built for restore and update capture (#170828 added that gate together with the PostgreSQL backup path; its history records no separate reason to exclude online archive capture). This change lets only the archive caller run its read-only capture when a running Gateway or embedded agent owns the state, through the existing onForeignOwner hook that read-only commands such as worktrees list already use.

Running online exposed one real bug: the export read sequence allocation facts before its snapshot opened. A row committed in between was captured with an ID above the recorded allocation, so a matched restore would reissue that ID and the next insert would hit a duplicate key. Allocations are now read inside the export snapshot, and the manifest's PostgreSQL facts are read after the export; sequences only advance, so this can leave gaps but never reuse a captured ID. Offline behavior is unchanged because nothing writes between the reads.

Technical inventory
  • src/snapshot/database-backup.ts: runOfflineDatabaseBackup takes { online: true }. When set and the caller supplied its config and engine selection, a foreign owner runs the capture in the same database-backup scope instead of refusing. Nested store captures inside the archive join that scope. All other callers still pass onForeignOwner: "refuse". The in-process hasActiveGatewayStateOwner refusal (backup inside the Gateway process) is unchanged.
  • src/infra/backup-create.ts: the archive path opts in with { online: true }.
  • src/snapshot/database-backup-core.worker.ts: exportPostgresBackup no longer pre-reads allocations; materializeDatabasePort reads them inside its repeatable-read transaction, and the manifest records a post-export read.
  • Docs: docs/cli/backup.md, docs/gateway/postgresql.md, docs/install/backups.md, docs/reference/database-engines.md describe online archive capture, its per-store consistency, and which commands stay offline-only.
  • Other createBackupArchive callers inherit the behavior: backup create --to <location> and migrate apply's pre-migration backup now run beside a running Gateway; Doctor's schema-guard archive runs under its own maintenance custody and is unaffected.
  • Accepted edge case: a Gateway that stops or restarts mid-capture is not fenced beyond the existing per-store identity and catalog checks; the capture only reads, and restore re-verifies every capsule.
  • No config option, schema, retention, or archive format change.

Evidence

Live run, isolated test Gateway on PostgreSQL 17.11 (scratch cluster and state dir, all three families on PostgreSQL):

  • BEFORE (main 3b3efe9539c, Gateway up): openclaw backup create --verify refused: "This operation requires exclusive offline state ownership ... This command is offline-only." No archive.
  • AFTER (this branch, Gateway up, six Workboard writes through the Gateway during the run, 1 s /readyz probe): archive created in 6 s, Archive verification: passed, exit 0; 19/19 readyz probes returned 200; standalone openclaw backup verify passed; archive contains state, agent, and Workboard PostgreSQL capsules. The Workboard capsule holds the 5 cards committed before its snapshot. After stopping the Gateway, openclaw backup restore imported that capsule into a fresh namespace with the same 5 cards.

Regression tests, red on main and green with the fix:

  • src/snapshot/database-backup.online.test.ts: archive capture runs beside a running owner and nested store captures join it; a non-archive capture still refuses. On main: LocalStateOwnerError ... This command is offline-only.
  • src/infra/database-engine/logbook-port.postgres.test.ts (PostgreSQL conformance): a row committed just before the export snapshot opens is covered by the recorded allocation, and a restored store issues the next ID. On main: expected { value: '70', called: true } to deeply equal { value: '71', called: true }.

Consumer tests: 10 backup unit test files (192 passed, 1 skipped) and 8 PostgreSQL conformance files (27 passed) covering archive create, verify, restore, mixed engines, update capture, Git backup secrets, and agent/state/logbook ports.

Lanes: changed-lanes selected core, coreTests, docs. check:changed --staged passed format, tsgo:core, tsgo:core:test, oxlint on changed files, and the database ratchets and guards. Two steps failed, deprecated API usage (extensions/codex/harness.ts:261) and dead export scan (codex, agents, plugins exports). Both fail identically on the unmodified base and name no touched file. git diff --check is clean.

Not covered: a full production-sized (15 GB) online run, and a Gateway restart racing a running capture.

Command transcripts
$ openclaw backup create --verify --no-include-workspace --output <scratch>/after.tar.gz
Backup archive: <scratch>/after.tar.gz
Included 1 path:
- state: <scratch>/state
Created <scratch>/after.tar.gz
Archive verification: passed

$ openclaw backup verify <scratch>/after.tar.gz
Assets verified: 1
Archive entries scanned: 29
SQLite database inventory verified.

readyz probe 16:37:08 -> 16:37:28: 19 samples, all 200
created during-1 16:37:10 ... during-6 16:37:28

- backup create captures each PostgreSQL store from its own repeatable-read
  export snapshot beside a running Gateway or embedded agent owner instead of
  refusing; with no owner it keeps offline custody as before
- restore, Git backups, generic local snapshots, update capture, and Doctor
  ports keep refusing while the Gateway runs
- read sequence allocation facts inside the export snapshot so a restored
  store never reissues a captured row ID after concurrent inserts
- docs: describe online archive capture and its per-store consistency
@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime size: S labels Oct 27, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 27, 2026
@clawsweeper

clawsweeper Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge.

What this changes

Allows PostgreSQL archive creation while the Gateway runs and captures sequence allocations safely alongside concurrent inserts.

Example: Run openclaw backup create --verify with state, agents, and Workboard on PostgreSQL 17.11.

  • Before: The command refuses while the Gateway is running.
  • After: The supplied live run creates and verifies an archive while all 19 readiness probes return HTTP 200.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Focused implementation with relevant live CLI proof and no definite defect; product acceptance remains unresolved.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): Supplied terminal output and the isolated PostgreSQL 17.11 Gateway run exercise archive creation during writes, successful verification, continued readiness, and restoration of the captured Workboard rows.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Feature · Worth it: Needs a maintainer decision · Fix scope: Partial
User problem: PostgreSQL operators must stop the Gateway for the entire routine archive operation.
Reason: The downtime benefit is concrete, but changing an owner-established reader-consistency contract needs acceptance; Related appropriately leaves the broader performance issue open.

Merge readiness

⛔ Blocked before merge - 1 item remains

This PR provides useful, focused work with no definite correctness finding; accepting the changed archive consistency contract requires a maintainer decision.

Priority: P2
Reviewed head: 45721e5d2621d5ad841bee50829216b89a7d8ce9
Owner decision: Required. See Decision needed.

Decision needed

  • Question: Should PostgreSQL archive creation permit live, independent per-store snapshots instead of requiring exclusive offline custody?
  • Recommendation: Approve online archive capture: Accept the documented per-store consistency for routine archives while retaining stopped-Gateway capture for quiescent backups.
  • Why: The existing behavior is an explicit documented contract, and the storage checkpoint requires acceptance of reader-consistency changes; contributor evidence explains the tradeoff but supplies no maintainer decision.

Before merge

  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.

Findings

None.

Agent review details

How this fits together

The archive command passes captured configuration and engine selection into the existing backup owner, which exports PostgreSQL stores through workers and packages their matched anchors, images, and manifests.

flowchart TD
  A[Backup CLI] --> B[Archive planner]
  B --> C[Backup ownership scope]
  C --> D[Database backup worker]
  D --> E[PostgreSQL store snapshots]
  E --> F[Matched export capsules]
  F --> G[Verified archive]
Loading

Technical review

Best possible solution:

Accept or reject online archive consistency explicitly, retaining this focused implementation and the existing stopped-Gateway path if approved.

Do we have a high-confidence way to reproduce the issue?

Current-main source establishes the online refusal, and the contributor supplies before-and-after CLI evidence; this read-only review ran no target code.

Is this the best way to solve the issue?

The patch is a small implementation using the existing ownership callback and materializer, with distinct regressions for custody routing and allocation ordering.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 30fa910885f1.

Provenance checked

  • Offline PostgreSQL archive custody and corresponding documentation changes intended behavior with a stated reason (d0ea7ea: The original PostgreSQL backup implementation required stopped-Gateway custody for matched export and anchor capture.)
  • Allocation pre-read reused by export changes intended behavior with a stated reason (87f2bbf: The earlier performance change removed repeated allocation reads under the offline backup contract; this PR explains why online writers require revised ordering.)
  • Archive capture scope and nested PostgreSQL captures keeps the original intent (b7bb85a: Whole-archive PostgreSQL recovery uses one capture scope and producer-declared capsules; this PR retains those owners and format.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Vincent Koc: Raw commit 386b0e6 adds src/infra/backup-create.ts:809 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 386b0e6c7457; files: src/infra/backup-create.ts)

PR surface

Source +11, Tests +68, Docs +12. Total +91 across 9 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 3 24 13 +11
Tests 2 68 0 +68
Docs 4 31 19 +12
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 9 123 32 +91

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Avoiding planned backup downtime provides concrete operator value without an urgent regression or currently broken recovery path.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: ⏳ waiting on maintainer: Only explicit maintainer approvals or decisions remain; merge is still blocked.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Have the PostgreSQL backup owner decide whether to accept the documented live per-store consistency contract.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 26, 2026, 8:34 PM ET / October 27, 2026, 00:34 UTC.

@roboclaw-bot roboclaw-bot added the clawtributors PR from a clawtributor label Oct 28, 2026
@steipete
steipete merged commit f7b61f0 into openclaw:main Oct 30, 2026
138 of 140 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

@etzelm
etzelm deleted the feat/backup-online-pg branch October 30, 2026 16:36
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 31, 2026
…claw#185236)

- backup create captures each PostgreSQL store from its own repeatable-read
  export snapshot beside a running Gateway or embedded agent owner instead of
  refusing; with no owner it keeps offline custody as before
- restore, Git backups, generic local snapshots, update capture, and Doctor
  ports keep refusing while the Gateway runs
- read sequence allocation facts inside the export snapshot so a restored
  store never reissues a captured row ID after concurrent inserts
- docs: describe online archive capture and its per-store consistency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawtributors PR from a clawtributor docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants