# Architecture

> Components, request paths, tenants and persistent state in pgetcd.

---

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

---

pgetcd implements an etcd v3 [service API protocol](/docs/concepts/architecture/#protocol-classes) gateway over PostgreSQL. The Go process handles gRPC and tenant connections. The `pg_etcd` C extension owns storage semantics, and PostgreSQL stores all durable state.

Each gateway process serves one configured tenant on a fixed PostgreSQL primary. Multi-node high availability and automatic primary promotion are part of the post-MVP [HA roadmap](/docs/etcd/design/ha-roadmap/). See [Status](/docs/etcd/status/) for current validation boundaries.

## Components {#components}

| Component | Responsibility |
| --- | --- |
| Go gateway | Decode protobuf requests, check input limits, authenticate clients and translate SQL envelopes |
| Publisher (gateway component) | Own SQL transactions and retain responses until synchronous `COMMIT` succeeds |
| Tenant Watch service | Read committed event batches, track each Watch and enforce queue limits |
| `pg_etcd` | Evaluate `Txn`, allocate revisions, store events, manage Lease state and compact history |
| `pg_kvcore` | Internal C helpers linked into `pg_etcd`; not a separate extension |
| PostgreSQL | Store logged tables, serialize requests, maintain snapshots and commit WAL |
| Lease launcher and workers | Supervise one background worker per allowed database and rebuild Lease deadlines |
{.wide}

The C extension has no network listener or embedded Go runtime. The Go gateway has no independent revision allocator or Lease expiry clock.

## Request path {#paths}

```text
etcd client
    │ gRPC with tenant mTLS
    ▼
Go gateway → Publisher → PostgreSQL transaction
                              │
                              ▼
                          pg_etcd
                    lock meta → fresh snapshot
                    validate → evaluate → certify
                              │
                              ▼
                       synchronous COMMIT
                              │
                              ▼
                     buffered gRPC response
```

Each logical request uses one `READ COMMITTED` transaction with `synchronous_commit = on`. The extension locks the tenant's metadata row and takes a fresh snapshot after the lock wait finishes. Any response that observes state requires a logged change after that observation and a normal synchronous `COMMIT` before publication. [Transactions and revisions](/docs/etcd/architecture/transactions/) explains this order.

Watch uses the same publication boundary. A per-tenant tailer reads complete committed revisions; PostgreSQL notifications wake the tailer, and periodic polling recovers missed notifications. Lease expiry also changes state through PostgreSQL transactions. See [Watch, Lease and history](/docs/etcd/architecture/watch-lease/).

## Tenant boundary {#tenants}

A tenant has a fixed endpoint, database, SQL role and certificate policy. One gateway process serves one tenant. Two gateway processes can serve the same database with separate connection pools. Multiple tenants use separate process configurations and database bindings.

A client request cannot select a database. Key prefixes are an application convention, not an isolation boundary. Read [Security](/docs/etcd/architecture/security/) before assigning credentials.

## Persistent state {#data}

The seven core tables in the `etcd` schema are ordinary logged PostgreSQL tables. They hold current values, complete events, revision manifests, history anchors, Lease facts, logical identity and publication barriers. See [Schema](/docs/etcd/architecture/schema/) for table definitions and invariants.

PostgreSQL sequences do not allocate protocol revisions. A rolled-back request consumes no revision, and all actual KV changes in one request share a single revision.

## Source map {#source}

Paths below are relative to the pgetcd repository:

| Path | Main content |
| --- | --- |
| `cmd/pgetcd/main.go` | Process modes, tenant startup, listeners and shutdown |
| `internal/gateway/` | gRPC service adapters |
| `internal/pgstore/`, `internal/pgflight/` | Publication and SQL transaction ownership |
| `internal/watch/`, `internal/tenantservice/` | Watch delivery and tenant readiness |
| `extension/pg_etcd.c` | SQL entry points and request execution |
| `extension/pg_etcd--0.1.0.sql` | Schema, SQL functions and privileges |
| `extension/pg_kvcore/` | Revision budgets and Lease runtime |
{.wide}

## Architecture topics {#topics}

- **[Transactions and revisions](/docs/etcd/architecture/transactions/)** — Post-lock snapshots, revision allocation, read barriers, and synchronous `COMMIT` publication.
- **[Watch, Lease and history](/docs/etcd/architecture/watch-lease/)** — Complete revision delivery, Lease facts and worker recovery, and baseline compaction.
- **[Schema](/docs/etcd/architecture/schema/)** — The seven logged tables in the `etcd` schema, invariants, and the internal SQL ABI.
- **[Security](/docs/etcd/architecture/security/)** — Tenant mTLS, certificate URI SAN matching, database role boundaries, and admission limits.
{.cards}

---

Section pages:

- [Transactions and revisions](/docs/etcd/architecture/transactions/): How one etcd request becomes one PostgreSQL transaction with a single revision.
- [Watch, Lease and history](/docs/etcd/architecture/watch-lease/): Event history, Watch delivery, lease expiry and logical compaction.
- [Schema](/docs/etcd/architecture/schema/): The seven tables of the etcd schema and what each one holds.
- [Security](/docs/etcd/architecture/security/): Tenant mTLS, certificate identities, database roles and grants.

---

Backlinks:

- [Runtime boundary](/docs/etcd/design/runtime-boundary/)
