# Recovery design

> Point-in-time recovery and revision journals. Designed, not in the release.

---

LLMS index: [llms.txt](/llms.txt)

---

> [!IMPORTANT]
> 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 {#problem}

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_exposed` with different keys and values, violating etcd revision monotonicity.
- Active Watch clients holding cursors at `R_exposed` would 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) {#journal}

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`):

1. At startup, the gateway records the source database and cluster identity in its local journal file before opening client listeners.
2. After each normal synchronous `COMMIT`, the wrapper writes any new high-water revision to the journal and calls `fsync` *before* returning the KV, Lease, Watch, or diagnostic result to the client.
3. If writing or syncing the journal fails (for example, `ENOSPC`), the wrapper withholds the gRPC response and closes admission for subsequent SQL calls.
{.steps}

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) {#restore}

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`):

1. Lock `etcd.meta` and take a fresh snapshot.
2. Verify that the restored database's system identifier and prior logical identity match the exported recovery manifest.
3. Compute the new baseline revision `R_new = max(R_exposed, R_restored) + 1`, checking for 64-bit integer overflow.
4. Copy all live rows from `etcd.kv` into a complete new baseline generation in `etcd.history_anchor`.
5. Set current revision `R = R_new`, compact revision `C = R_new`, and history floor `F = R_new`.
6. Delete all prior rows in `etcd.events`, `etcd.revision_manifest`, and older `etcd.history_anchor` generations.
7. Generate fresh `cluster_id` and `member_id` values in `etcd.meta`, while preserving live `etcd.kv` rows and durable `etcd.leases` facts.
8. Write a certification update to `etcd.publication_barrier` and complete a synchronous `COMMIT`.
{.steps}

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 {#evidence}

In single-container Linux amd64 tests on PostgreSQL 18.6 (2026-10-14):

- **Design 0029 (`1212/v5`, 6/6 PASS)**: Restored a `pg_verifybackup`-verified backup to revision `7` after clients had observed revision `10`; protocol rebase established baseline revision `11` with new cluster/member IDs, compacted old Watch cursors, reconstructed Leases with full TTL, and delivered the next Watch write at revision `12`. 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 bounds `10` and `11`), rebasing physical PITR revision `7` to `12`, 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.

---

Backlinks:

- [Design](/docs/etcd/design/)
- [Monitoring](/docs/etcd/operations/monitoring/)
- [Status](/docs/etcd/status/)
