This is the multi-page printable view of this section.
Tutorials
These tutorials run against a development tenant and write keys under the /tutorial/ prefix. Reserve /tutorial/ for these exercises.
- Complete Quick start first if you do not have a running tenant, and keep the endpoint address and tenant client certificates ready.
- Follow Use etcdctl to read and write keys, watch prefix changes from a snapshot revision, and attach or revoke a Lease.
- Follow Use the Go client to execute a conditional
Txnand watch a key from a known revision withclientv3.
For the full list of supported RPCs and limits, see Compatibility.
1 - Use etcdctl
This exercise writes a key, watches changes from a snapshot revision, and deletes a key by revoking its Lease. It requires a running development tenant (Quick start), etcdctl 3.6.15 (or 3.7.2), and jq. Keep the /tutorial/ prefix free for this walkthrough.
Configure the client
Export the etcd v3 environment variables in each terminal, replacing the endpoint and certificate paths with those for your tenant:
The endpoint hostname or IP must match the gateway server certificate (server.crt), and the client certificate (tenant-client.crt) must carry the tenant’s URI SAN (spiffe://pgetcd/tenant/demo). ETCDCTL_CACERT points to the CA that signed the gateway server certificate.
etcdctl endpoint status prints the tenant’s logical member ID, current revision, advertised version (3.6.0), and PostgreSQL relation size. etcdctl endpoint health reports healthy from v0.1.0-rc2; on v0.1.0-rc1 it fails because Maintenance.Alarm was unimplemented. If endpoint status fails, see Troubleshooting.
Read and write keys
Write and read back a key under /tutorial/:
The first command prints OK. The second prints the key and world. The JSON output shows header.revision along with the key’s create_revision, mod_revision, and version (keys and values in JSON output are base64-encoded).
Write the same value a second time:
Both version and mod_revision advance even when the value does not change. Do not hard-code revision numbers in scripts; concurrent writes or Lease expirations in the same tenant also advance the revision counter.
Read a snapshot, then watch
In the first terminal, read a prefix snapshot and start a Watch at header.revision + 1:
In a second terminal with the same ETCDCTL_* variables, update and delete the key:
The first terminal prints PUT and DELETE events with their previous key-value pairs (--prev-kv). Passing --rev="$TUTORIAL_START" ensures that any write committed between the get snapshot and the watch registration is still delivered, provided that revision has not been logically compacted.
Press Ctrl-C to stop the Watch. If the requested start revision has been compacted, take a fresh get snapshot and start a new Watch. For Watch stream and buffer limits, see Troubleshooting — Capacity.
jq uses IEEE 754 double-precision numbers. For production applications where revisions can exceed $2^{53} - 1$, compute revision + 1 in a client that preserves 64-bit integers, such as the Go client example.
Attach and revoke a Lease
Grant a 30-second Lease:
Copy the hexadecimal Lease ID from the output and attach /tutorial/leased to it:
Revoking the Lease atomically deletes /tutorial/leased in the same PostgreSQL transaction, so the final get returns no rows.
To keep a Lease alive continuously, grant a new Lease and run etcdctl --command-timeout=0s lease keep-alive <lease-id>. For Lease recovery behavior after a PostgreSQL restart, see Watch, Lease and history.
Clean up
Delete any remaining keys under /tutorial/:
Next, see Use the Go client for conditional transactions.
2 - Use the Go client
This example uses the official go.etcd.io/etcd/client/v3 package (v3.7.2 in the pgetcd go.mod) to connect to a running development tenant over mutual TLS, execute a conditional Txn on /tutorial/go-client, and watch the next write starting from Header.Revision + 1.
Export the ETCDCTL_* environment variables from Use etcdctl before running the program.
Write the program
Create .cache/tutorial-client/main.go inside the pgetcd checkout (.cache/ is ignored by Git):
Run the program
From the pgetcd repository root, run:
Pass a single endpoint URL in ETCDCTL_ENDPOINTS. The first run prints created=true followed by the PUT event for updated. Subsequent runs print created=false because Version(key) is no longer 0.
Starting the Watch at initial.Header.Revision + 1 avoids a race between Watch registration and Put: even if Put commits before the Watch stream finishes registering, pg_etcd replays retained events from initial.Header.Revision + 1 onward.
All operations share a 15-second context timeout. If a write times out or returns an unknown outcome, do not blindly replay a non-idempotent write; inspect the committed state first.
Clean up and production notes
Delete the tutorial key when done:
When writing production clientv3 consumers:
- Persist your consumer’s resume revision only after processing all events of a complete revision.
- If a Watch response carries
CompactRevision > 0(ErrCompacted), take a freshGetsnapshot and open a new Watch fromHeader.Revision + 1. - If stream fragmentation is enabled, wait until
More == falsebefore treating a revision as complete.
For more details on Watch tailing and Lease lifecycles, see Watch, Lease and history.