SQL ABI
The SQL ABI is the contract between the Go pgetcd gateway and the C pg_etcd extension (pg_kvcore is an internal C module compiled into pg_etcd.so, not a separate extension). The gateway translates etcd v3 protobuf messages into JSONB calls over a pgx connection pool.
Extension boundary and role grants
| SQL function | Signature | Purpose |
|---|---|---|
etcd._abi | etcd._abi() RETURNS jsonb | Report SQL ABI version, schema version, supported capabilities, and byte/cardinality bounds |
etcd._execute | etcd._execute(request jsonb) RETURNS jsonb | Execute one logical KV, Lease, or diag.status operation |
etcd._tail_v2 | etcd._tail_v2(after_revision bigint, max_envelope_bytes integer) RETURNS jsonb | Read the next complete revision (> after_revision) and its manifest for Watch |
etcd._publication_barrier | etcd._publication_barrier() RETURNS void | Write a logged publication barrier row when certifying startup or standalone probes |
The extension also defines SQL convenience wrappers (etcd.get, etcd.put, etcd.delete_range, etcd.txn, etcd.compact) and retains legacy etcd._tail and experimental etcd._lease_probe for internal test suites. Do not grant those functions to the gateway login role.
Role separation is mandatory:
pgetcd_owner(NOLOGIN NOSUPERUSER) owns schemaetcd, all 7 tables, and theSECURITY DEFINERentry-point functions.- Extension installation revokes all
PUBLICprivileges on schemaetcdand its objects. - The tenant gateway role (for example
pgetcd_demo_gateway) receives onlyCONNECTon the database,USAGE ON SCHEMA etcd, andEXECUTEon the four functions in the table above. It has no direct tableSELECTor DML privileges. - All public C entry points are
VOLATILE PARALLEL UNSAFE SECURITY DEFINERwith a fixedsearch_path = pg_catalog, etcd, pg_tempand execute parameterized SPI statements.
JSONB request and response envelopes
Every _execute call passes a single JSONB object of at most 2 MiB:
A state-dependent success response returns:
Encoding rules:
- All
int64anduint64fields are canonical decimal strings (no leading zeroes,+,-0, exponents, or whitespace).cluster_idandmember_idpreserve the full 64-bit unsigned range. - Byte slices (
key,range_end,value) use padded canonical RFC 4648 base64; the extension re-encodes decoded bytes to reject non-canonical padding bits. - Booleans use native JSON booleans; enums use upstream protobuf names (
Compareenums also acceptint32numbers for upstream compatibility). Unknown JSON fields fail explicitly. - State-dependent responses (both
"status": "ok"and state errors such ascompact_revision,future_revision,resource_exhausted,lease_not_found,lease_already_exists) carry"publication": "requires_commit"and"certification": "performed", alongside the observedheader. Static validation errors (invalid_argument,unimplemented,lease_ttl_too_large) that do not inspect storage return"publication": "none".
Supported _execute methods
| Method | Request fields | Notes |
|---|---|---|
kv.range | key, range_end, limit, revision, sort_order, sort_target, serializable, keys_only, count_only, min_mod_revision, max_mod_revision, min_create_revision, max_create_revision | 8 MiB response budget (p2-kv-response-v1) |
kv.put | key, value, lease, prev_kv, ignore_value, ignore_lease | Write keys capped at 2,048 decoded bytes |
kv.delete_range | key, range_end, prev_kv | Empty delete allocates no revision |
kv.txn | compare, success, failure | Max depth 8; max 128 comparisons + operations across both branches |
kv.compact | revision, physical | Logical compaction only; physical=true returns unimplemented |
lease.grant | ID, TTL | TTL < 2 normalized to 2; max 9,000,000,000 s; ID = 0 allocates a positive ID |
lease.revoke | ID | Atomically deletes attached keys and emits DELETE events in one revision |
lease.keep_alive | ID | Renews committed sequence; missing lease returns TTL = 0 |
lease.time_to_live | ID, keys | Missing lease returns TTL = -1; keys=true capped by the 2 MiB Lease response budget |
lease.leases | {} | Lists up to 1,024 active leases |
diag.status | {} | Returns ready, primary, lease_ready, not_ready_reasons, restore_generation, storage_bytes, and persistent IDs |
Transaction contract
Every gateway call runs inside one top-level READ COMMITTED transaction on the primary with synchronous_commit = on:
- Metadata lock then fresh snapshot: The extension locks the single row in
etcd.meta(FOR UPDATE), completes that SPI lock command, and registers a fresh snapshot after any lock wait. - Single-revision mutations: All
Txncomparisons evaluate against the post-lock initial snapshot. Selected branch operations run sequentially so later operations in the sameTxnobserve earlier writes from the sameTxn. All KV modifications in one_executecall share a single revisionR + 1with contiguous(R + 1, sub)event coordinates. - No-op and rollback preservation: Reads, empty
DeleteRange,Compact, Lease-only operations (Grant,KeepAlive,TimeToLive,Leases, or revoking a Lease with zero attached keys), and rolled-back transactions do not increment the KV revision. - Strict transaction guards: The extension rejects
READ UNCOMMITTED,REPEATABLE READ,SERIALIZABLE, standby/recovery sessions (pg_is_in_recovery()), subtransactions (SAVEPOINT), a second public ABI call in the same PostgreSQL transaction, andPREPARE TRANSACTION.
Certified publication
"certification": "performed" indicates that the extension wrote the required WAL records (either actual table mutations or an update to etcd.publication_barrier on read-only, no-op, _tail_v2, and state-error paths) after taking its snapshot. It does not mean that the transaction has committed.
The gateway buffers the entire response and releases it to the gRPC caller only after COMMIT returns normally with no PostgreSQL warning about canceled synchronous replication. If COMMIT fails, times out, is canceled, or loses its connection, the gateway discards the buffered response and never replays the request. See Transactions and publication.
Startup ABI checks
Before opening client listeners, the gateway calls SELECT etcd._abi() and diag.status to verify:
sql_abi = "1",schema_version = "2", andtail_abi = "2".- Resource profiles
p1-revision-v1(8 MiB/4,096events per complete revision) andp2-kv-response-v1(max_kv_response_bytes = "8388608"). max_write_key_bytes = "2048",max_lease_response_bytes = "2097152",max_leases_per_database = "1024", andmax_databases = "8".- Stored
cluster_id,member_id,database_name, andlease_ready = true.
