# Schema

> The seven tables of the etcd schema and what each one holds.

---

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

---

`pg_etcd` installs the `etcd` schema in each tenant database. All seven persistent tables are regular WAL-logged PostgreSQL tables. The extension's SQL installation file defines schema version `2`, while the JSONB request envelope uses internal SQL ABI version `1`.

## Tables {#tables}

| Table | Contents and invariant |
| --- | --- |
| `etcd.meta` | Single serialization row (`singleton = true`). Stores current revision `R`, compact revision `C`, history floor `F`, active `anchor_generation`, `cluster_id`, `member_id`, and `schema_version` |
| `etcd.kv` | Live binary key/value pairs (`key`, `value`, `create_revision`, `mod_revision`, `version`, `lease_id`) |
| `etcd.events` | Change log ordered by `(revision, sub)`. Stores event type (`PUT` or `DELETE`), current tuple fields, and complete previous tuple (`prev_*`) |
| `etcd.revision_manifest` | Per-revision summary (`revision`, `event_count`, `tail_bytes`, `watch_bytes`) for one complete revision |
| `etcd.history_anchor` | Complete live-key baseline at floor revision `F`, partitioned logically by `generation` |
| `etcd.leases` | Durable Lease facts (`lease_id`, 128-bit grant generation `grant_hi`/`grant_lo`, `granted_ttl_sec`, `renew_seq`, `attached_keys`, `revoke_bytes`) |
| `etcd.publication_barrier` | Single-row logged counter (`singleton = true`, `seq`) updated after state observations that require certification |
{.wide}

Keys and values use `bytea`. Revision and Lease ID fields use `bigint`. Cluster and member IDs use 8-byte `bytea` values (`cluster_id`, `member_id`) so the full unsigned 64-bit etcd identity is preserved without signed-integer overflow.

The `etcd.kv` primary key uses PostgreSQL binary `bytea` ordering. Keys contain `1` to `2,048` bytes. A partial index on nonzero `lease_id` values (`etcd_kv_lease_idx`) supports Lease attachment lookups and revocation. The index `etcd_events_key_rev_sub_idx` on `(key, revision DESC, sub DESC)` supports historical `Range` reconstruction.

## Revision and event invariants {#invariants}

Fresh metadata in `etcd.meta` starts at `R = 1`, `C = -1`, `F = 0`, and `anchor_generation = 0`. The metadata row serializes revision allocation; no PostgreSQL `SEQUENCE` or `BIGSERIAL` is used.

A mutation commits live state in `etcd.kv`, complete events in `etcd.events`, and its summary row in `etcd.revision_manifest` in the same transaction. The manifest row must match the exact event count and conservative Tail/Watch encoded byte budgets (`<= 8 MiB`). `etcd._tail_v2` recomputes these values when reading a revision and rejects any mismatch.

A `DELETE` event in `etcd.events` records a tombstone (`is_delete = true`) at its deletion revision together with a complete previous tuple (`prev_present = true`, `prev_create_revision`, `prev_mod_revision`, `prev_version`, `prev_lease_id`, `prev_value`). Its new `create_revision`, `version`, and `lease_id` fields are `0`. This allows historical `Range` reconstruction to hide an older baseline value while Watch still delivers the deletion event and optional `prev_kv`.

Triggers on `etcd.kv` (`etcd_kv_lease_ledger_trg`) and `etcd.leases` maintain exact `attached_keys` counts and `revoke_bytes` budgets for every writer. Protocol paths check these bounds before mutating rows, and table `CHECK` constraints provide a final guard if an invariant is violated.

## SQL interface {#abi}

The Go gateway calls the extension through a narrow internal SQL ABI:

| Entry point | Use |
| --- | --- |
| `etcd._abi()` | Return extension version, schema version (`2`), supported SQL ABI (`1`), Tail ABI (`2`), Lease worker status, and compiled limits |
| `etcd._execute(req jsonb)` | Execute one KV, Lease, or diagnostic request envelope |
| `etcd._tail_v2(after_rev bigint, max_events integer)` | Read the next complete revision batch under Tail ABI 2 |
| `etcd._publication_barrier()` | Advance `etcd.publication_barrier.seq` to certify controlled observations |
| `etcd.get`, `etcd.put`, `etcd.delete_range`, `etcd.txn`, `etcd.compact` | SQL convenience wrappers over the same C request engine |
{.wide}

Legacy `etcd._tail` and experimental `etcd._lease_probe` remain in the extension SQL file for frozen test inputs; they are not used by the active Watch and Lease RPC paths.

An `etcd._execute` request envelope has this shape:

```json
{"abi":"1","method":"kv.put","request":{"key":"YQ==","value":"Yg==","lease":"0"}}
```

64-bit integers cross the JSONB envelope as canonical base-10 strings. Binary `bytea` fields use canonical RFC 4648 padded base64. Booleans retain JSON boolean types, and unknown JSON fields fail with an explicit error. This envelope is an internal gateway-to-extension ABI, not a public HTTP/JSON etcd endpoint. See [SQL interface reference](/docs/etcd/reference/sql/) for full function signatures.

Receiving a row from `etcd._execute` does not complete publication; the caller must still complete a synchronous `COMMIT` without warnings. See [Transactions and revisions](/docs/etcd/architecture/transactions/).

## Privileges and direct SQL {#privileges}

Installing the extension requires an existing `pgetcd_owner` role configured with `NOLOGIN`, `NOSUPERUSER`, `NOCREATEDB`, `NOCREATEROLE`, and `NOREPLICATION`. The extension script assigns ownership of all `etcd` tables and functions to `pgetcd_owner` and revokes `PUBLIC` access.

Protocol entry points run as `SECURITY DEFINER` with `SET search_path = pg_catalog` and schema-qualified table references. The gateway runtime role needs only `USAGE` on schema `etcd` and `EXECUTE` on the approved entry functions (`_abi`, `_execute`, `_tail_v2`, `_publication_barrier`). It must not own the extension, inherit `pgetcd_owner`, or have direct `SELECT` or DML privileges on the underlying tables.

Direct DML against `etcd.*` tables can bypass protocol preflight checks, revision ordering, and event manifest construction. Treat the seven tables as internal extension state. The SQL convenience wrappers (`etcd.get`, `etcd.put`, etc.) are development helpers subject to the same one-call-per-transaction rules, not a bulk data-import API.

Source: `extension/pg_etcd--0.1.0.sql`, `extension/pg_etcd.c`, and `extension/pg_kvcore/revision_budget.c`.

---

Backlinks:

- [Architecture](/docs/etcd/architecture/)
- [Watch, Lease and history](/docs/etcd/architecture/watch-lease/)
- [PostgreSQL as the authority](/docs/etcd/design/postgresql-authority/)
