# Lease, Watch and history

> Full-TTL rebuild after restart, NOTIFY as a hint, and anchor generations for compaction.

---

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

---

> [!NOTE]
> Condensed from **Design 0007** (accepted 2026-10-08).

## Lease durability and full-TTL rebuild {#lease}

pgetcd stores durable Lease facts—Lease ID, a 128-bit grant generation (`grant_hi`, `grant_lo`), granted TTL (`granted_ttl_sec`), renewal sequence (`renew_seq`), and attached-key byte ledgers—in `etcd.leases`. Monotonic expiration deadlines live in shared memory managed by a supervised PostgreSQL background worker (`pg_etcd` preload).

Before deciding whether a Lease has expired, the extension acquires locks in strict `etcd.meta`-then-`etcd.leases` order and refreshes shared-memory state from any newer committed `renew_seq`. It never holds a shared-memory lock while waiting for a PostgreSQL row lock.

When PostgreSQL or the Lease background worker restarts, tenant readiness (`/readyz`) closes while a new worker generation reconstructs runtime deadlines from `etcd.leases`. Every surviving Lease receives a full `granted_ttl_sec` starting from the reconstruction timestamp before readiness reopens. This conservative choice prevents premature Lease revocation and accidental key deletion across restarts, at the cost of delaying expiry by up to one full TTL per restart.

## Watch event boundaries and wake hints {#watch}

`etcd.events` stores both the complete new tuple and the complete previous tuple (`prev_*`) for every `PUT` and `DELETE`, paired with a per-revision summary in `etcd.revision_manifest`. The gateway's Watch service reads events through `etcd._tail_v2`, verifies the manifest's event count and byte budgets, certifies the scanned watermark, and advances watcher cursors only across complete revisions.

PostgreSQL `LISTEN` / `NOTIFY` messages are treated strictly as wake hints. A missed notification never loses events or stalls a stream permanently, because periodic polling (`watch.poll_interval_ms`) reads the durable event table directly. Stream-wide progress (`watch_id = -1`) is emitted only when every watcher on the stream is caught up; a `Created` response confirms registration only, not historical catch-up.

## History anchors and atomic compaction {#history}

Rather than mutating historical events in place, `Compact(rev)` builds a complete baseline snapshot of all live keys at revision `rev` inside `etcd.history_anchor` under a new `generation` number. Within the same transaction, it updates `etcd.meta` to switch `anchor_generation` and set `F = C = rev`, then deletes older anchor generations and events strictly below `F`.

Events at the boundary revision `F` itself—including `DELETE` tombstones—remain in `etcd.events`. Keeping boundary tombstones ensures that historical `Range` reconstruction at `F` properly masks deleted keys and that Watch streams starting at `F` still observe the deletion event. See [Watch, Lease and history](/docs/etcd/architecture/watch-lease/).

---

Backlinks:

- [Design](/docs/etcd/design/)
