# PostgreSQL as the authority

> One Go gateway and one extension; a meta-row lock and a fresh snapshot per request; no sequences.

---

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

---

> [!NOTE]
> Condensed from **Design 0005** (accepted 2026-10-08, following architecture v1.0 dated 2026-10-05).

## Decision {#decision}

pgetcd splits the etcd v3 server into a stateless Go protocol gateway (`pgetcd`) and a single C/PGXS PostgreSQL extension (`pg_etcd`, with internal C helpers in `pg_kvcore` linked into the same shared library). PostgreSQL 18 is the implementation baseline.

All authoritative state—current revision `R`, compact revision `C`, history floor `F`, live KV rows, complete event history, revision manifests, Lease records, and cluster identity—lives in regular WAL-logged PostgreSQL tables inside the tenant database. Protocol reads and writes both route to the PostgreSQL primary.

Each logical etcd request executes inside one `READ COMMITTED` transaction:

1. Lock the single row in `etcd.meta` (`SELECT ... FOR UPDATE`).
2. Acquire a fresh PostgreSQL snapshot after the lock wait completes.
3. Evaluate the request against that snapshot.
4. Allocate at most one revision (`R + 1`) if the request performs one or more actual KV mutations; leave `R` unchanged on reads, no-op deletes, Lease-only operations, `Compact`, or rollbacks.
{.steps}

## Why sequences and gateway caches are rejected {#reason}

PostgreSQL `SEQUENCE` (`nextval`) advances outside transactional commit order: if two transactions call `nextval` concurrently or one rolls back, committed rows can appear out of revision order or contain uncompacted revision gaps, breaking etcd Watch cursors and `Txn` revision comparisons.

Similarly, caching the current revision or Lease expiration clock inside the Go gateway breaks as soon as two gateway processes share a tenant database or a gateway restarts. Keeping `etcd.meta` and `etcd.leases` in logged tables makes PostgreSQL the single source of truth across gateway restarts and multi-gateway deployments.

Keeping `pg_kvcore` as an internal static library inside `pg_etcd` avoids stabilizing a second public C/SQL ABI before another extension needs it.

## Costs and trade-offs {#tradeoffs}

Locking `etcd.meta` per request provides strict per-tenant serial ordering and a simple post-lock snapshot invariant, at the cost of serializing requests within a single tenant database.

Every mutation writes live KV state, complete new/previous event tuples, and a revision manifest row into WAL, requiring regular PostgreSQL `VACUUM` and sufficient background worker capacity. Multi-request batching and table partitioning require separate designs and are outside the MVP scope. See [Transactions and revisions](/docs/etcd/architecture/transactions/) and [Schema](/docs/etcd/architecture/schema/).

---

Backlinks:

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