SQL workflows, versioning, and multi-tenancy
HTTP clients see pgs3 as an S3 endpoint, but inside PostgreSQL the exact same buckets and objects are accessible through SQL functions. That unlocks capabilities a standalone object store cannot offer: atomic cross-table transactions, zero-byte bucket forks, one-call version promotion, and PostgreSQL Row-Level Security (RLS).
How storage and deduplication work
Understanding three storage details explains why pgs3 operations behave the way they do:
- Content-addressed blobs (
pgs3.blob): Every non-delete object version references a physical blob keyed by its SHA-256 digest. Uploading the same bytes twice—or copying, restoring, or forking objects—increments a reference count instead of duplicating payload bytes. - Inline vs. chunked layout: Blobs up to
pgs3.inline_threshold(default 64 KiB) store one inline copy inpgs3.blob. Larger blobs are split into 4 MiB chunks (pgs3.chunk_size) across hash-partitionedpgs3.chunktables; completed multipart uploads stitch orderedpgs3.blob_extentreferences together without rewriting the uploaded parts. - Short-transaction concurrency: Same-key writes acquire a key-level lock so precondition checks (
If-None-Match,If-Match) and version publication are serialized atomically. Streaming uploads commit each chunk in its own short transaction and stay invisible until the final commit publishes the object version.
For full schema diagrams and design rationale, see design.md, schema.md, and decisions.md.
1. Commit business rows and objects atomically
A classic failure mode with external object stores is writing a file to S3 and then failing to insert the matching database row (or rolling back the database row while leaving an orphan object behind). With pgs3, both live inside one PostgreSQL transaction.
Assume tenant_app is an unprivileged tenant role created as shown in Manage tenants and credentials:
If anything in the transaction fails before COMMIT, both the app_reports row and the pgs3 object disappear together. Once committed, any S3 client authenticated as tenant_app can immediately download reports/2026/q4-summary.json.
2. Version history and instant rollback
Every overwrite in pgs3 creates a new monotonically ordered version_id, and every ordinary delete inserts a delete marker. Calling pgs3.restore() promotes a chosen historical version back to the latest state by appending a new version row that points to the original SHA-256 blob—history is never rewritten.
You can also pass p_if_match => '<current-etag>' to pgs3.restore() to abort the rollback if another writer updated the key in the meantime. Over HTTP, pgs3 exposes the same operation via its extension route POST /{bucket}/{key}?restore&versionId={id}.
3. Fork a bucket without copying payload bytes
pgs3.fork_bucket() creates a brand-new bucket populated with the latest object versions (and delete markers) of a source bucket. Underlying SHA-256 blobs are shared via reference counting; only metadata rows and reference counts are written. Subsequent writes to either bucket are completely independent.
This is especially useful for AI agent experiment branches, pre-release staging checkpoints, or reproducible evaluation snapshots:
Keep two constraints in mind:
- The destination bucket must not already exist.
- Forking shares physical bytes, so disk growth for large blobs is minimal, but it still inserts metadata rows, updates indexes, and generates WAL proportional to the number of latest entries in the source bucket.
4. Multi-tenancy and credential lifecycle
How tenant isolation works
pgs3 separates the runtime service identity from tenant identities:
- Service role (
pgs3.server_role, defaultpgs3_server): Created automatically when the extension is installed. HTTP workers connect as this restrictedNOLOGIN NOINHERITrole only to look up credentials and switch roles. - Tenant roles: Ordinary, unprivileged
NOLOGIN NOINHERITroles that you create (one per customer, team, or agent workspace). When you bind a credential withpgs3.create_credential(),pgs3grantspgs3_serverSET-only membership in that tenant role. - Per-request RLS: After verifying a request’s SigV4 signature, the HTTP worker executes
SET LOCAL ROLE <tenant>inside the request transaction. Row-Level Security policies ensure each tenant only sees and modifies buckets owned by roles it hasUSAGEmembership in.
Never bind a SUPERUSER, BYPASSRLS, CREATEROLE, CREATEDB, or REPLICATION role to an S3 credential—pgs3.create_credential() rejects elevated roles by design.
Provision two isolated tenants
Run credential administration as a database operator (credential functions are revoked from PUBLIC):
Verify isolation in SQL—tenant_bob cannot see or read tenant_alice’s bucket:
Rotate, suspend, and audit credentials
Issue separate access keys per client or service so you can rotate or revoke one without disrupting others:
Tenant SQL functions default to EXECUTE for PUBLIC, which allows any database role with CONNECT to create its own buckets while keeping other tenants’ buckets hidden via RLS. If bucket creation must be allowlisted, revoke EXECUTE on pgs3.create_bucket(text, jsonb) from PUBLIC and grant it only to approved roles. Never grant tenants SELECT on pgs3.credential.
For the full list of SQL signatures, see the SQL reference.