Recovery design
The offline protocol recovery tool (pgetcd-restore) and per-gateway publication revision journals (PGETCD_REVISION_JOURNAL) were prototyped in P3 research candidates (1212/v5 and 1216/v4, 2026-10-14) and are not included in the MVP release tree (v0.1.0-rc1, v0.1.0-rc2). This page documents their design as part of the post-MVP roadmap.
Why physical PITR needs protocol rebasing
When PostgreSQL restores a physical base backup and replays archived WAL to a recovery target (Point-in-Time Recovery, or PITR), the restored database state can end at a revision R_restored that is lower than the highest revision R_exposed already returned to etcd clients before the outage.
If a gateway reopened traffic directly on R_restored:
- Subsequent writes would reuse revisions
R_restored + 1 .. R_exposedwith different keys and values, violating etcd revision monotonicity. - Active Watch clients holding cursors at
R_exposedwould either stall waiting for future revisions or silently miss the state rollback.
Safe recovery therefore requires knowing a trustworthy upper bound on R_exposed, rebasing the database’s current and compact revisions strictly above max(R_exposed, R_restored), and rotating the logical cluster_id and member_id so clients detect a new cluster incarnation and relist.
Per-gateway revision journals (Design 0030)
If the original PostgreSQL primary is still accessible in maintenance mode after traffic drains, an operator can query its final etcd.meta.revision directly. If the primary disk is lost, however, the backup alone does not record how many later revisions gateways published before the crash.
Design 0030 (prototyped in candidate 1216/v4) addresses this by wrapping the gateway’s Publisher with an optional per-gateway revision journal (PGETCD_REVISION_JOURNAL):
- At startup, the gateway records the source database and cluster identity in its local journal file before opening client listeners.
- After each normal synchronous
COMMIT, the wrapper writes any new high-water revision to the journal and callsfsyncbefore returning the KV, Lease, Watch, or diagnostic result to the client. - If writing or syncing the journal fails (for example,
ENOSPC), the wrapper withholds the gRPC response and closes admission for subsequent SQL calls.
During recovery after source loss, the operator collects the journal files from all gateways that served the tenant and merges them with the verified physical backup_manifest (pgetcd-restore export-journals) to establish a conservative upper bound R_exposed without needing a connection to the lost primary.
Transactional protocol rebase (Design 0029)
Once PostgreSQL finishes physical PITR in maintenance mode (with shared_preload_libraries = '' so the Lease worker does not start prematurely), Design 0029 (prototyped in candidate 1212/v5) runs a single READ COMMITTED transaction (pgetcd-restore restore):
- Lock
etcd.metaand take a fresh snapshot. - Verify that the restored database’s system identifier and prior logical identity match the exported recovery manifest.
- Compute the new baseline revision
R_new = max(R_exposed, R_restored) + 1, checking for 64-bit integer overflow. - Copy all live rows from
etcd.kvinto a complete new baseline generation inetcd.history_anchor. - Set current revision
R = R_new, compact revisionC = R_new, and history floorF = R_new. - Delete all prior rows in
etcd.events,etcd.revision_manifest, and olderetcd.history_anchorgenerations. - Generate fresh
cluster_idandmember_idvalues inetcd.meta, while preserving liveetcd.kvrows and durableetcd.leasesfacts. - Write a certification update to
etcd.publication_barrierand complete a synchronousCOMMIT.
When PostgreSQL restarts with pg_etcd preloaded, the Lease background worker reconstructs surviving Leases with a full TTL under a new owner generation. Clients reconnecting to the gateway observe the new cluster_id and compact_revision = R_new; old Watch cursors below R_new receive an immediate compaction cancellation (compact_revision = R_new), prompting clients (such as kube-apiserver) to relist at R_new and resume watching from R_new + 1.
Prototype verification and remaining work
In single-container Linux amd64 tests on PostgreSQL 18.6 (2026-10-14):
- Design 0029 (
1212/v5, 6/6 PASS): Restored apg_verifybackup-verified backup to revision7after clients had observed revision10; protocol rebase established baseline revision11with new cluster/member IDs, compacted old Watch cursors, reconstructed Leases with full TTL, and delivered the next Watch write at revision12. Injecting a late constraint failure (SQLSTATE 23514) rolled back all seven tables cleanly. - Design 0030 (
1216/v4, 8/8 PASS): Recovered after stopping the source PostgreSQL instance using two gateway journals (at bounds10and11), rebasing physical PITR revision7to12, and verified that a full journal tmpfs (ENOSPC) withholds the gRPC response after SQL commit.
Bringing pgetcd-restore and revision journals into a future post-MVP release requires packaging the CLI in the main release tree, automating gateway journal inventory across hosts, and integrating post-restore identity re-enrollment.
