# SQL API design

> Why objects are reachable from SQL and how per-bucket ACL relations work.

---

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

---

> [!NOTE]
> The public `silo_api` SQL interface and `silo_bucket` ACL relations are **Preview** features in the MVP release.

Storing objects inside PostgreSQL unlocks a capability that external object stores cannot provide: applications can read, write, copy, and delete S3 objects inside the same `Read Committed` SQL transaction as ordinary relational tables. This page explains how `pgsilo` exposes that SQL interface without compromising the internal version state machine or tenant isolation.

## Three-schema separation (`silo`, `silo_api`, `silo_bucket`) {#schema-separation}

Early design drafts placed public SQL functions directly in the `silo` schema. Implementation reviews separated the database surface into three schemas:

| Schema | Purpose | Access policy |
| --- | --- | --- |
| `silo` | Private storage tables (`buckets`, `objects`, `object_segments`, `sys_files`, `bucket_acl`) and internal `SECURITY INVOKER` state-machine helpers (`commit_version`, `delete_version`, `lock_key`) | Owned by `silo_owner`; `REVOKE ALL FROM PUBLIC`; never granted to application roles |
| `silo_api` | The 11 public `SECURITY DEFINER` functions (`put_object`, `get_object`, `read_object`, `stat_object`, `list_objects`, `delete_object`, `copy_object`, `create_bucket`, `bucket_info`, `list_buckets`, `drop_bucket`) | `REVOKE ALL FROM PUBLIC` at creation; administrators explicitly `GRANT USAGE` and per-function `EXECUTE` to application SQL logins |
| `silo_bucket` | Per-bucket ACL relations (`silo_bucket.b_<bucket_id>`) created automatically when a bucket is created | Non-updatable `security_barrier` views carrying `SELECT`, `INSERT`, `UPDATE`, and `DELETE` grants |
{.wide}

Public `silo_api` functions return flat `RETURNS TABLE (...)` columns or `bytea` rather than composite types from `silo.*`, so application roles never need `USAGE` on the private `silo` schema.

## Per-bucket ACL relations (`silo_bucket.b_<bucket_id>`) {#per-bucket-acl}

Why represent bucket permissions with a view named `b_<bucket_id>` in `silo_bucket`?

1. **Identifier length and collision safety**: S3 bucket names can be up to 63 bytes (and internal table columns allow up to 255 bytes) with dots and hyphens, whereas PostgreSQL identifiers are capped at 63 bytes (`NAMEDATALEN - 1`). Naming the relation `silo_bucket.b_<bucket_id>` avoids truncation collisions while `silo.bucket_acl` maps each immutable `bucket_id` to its exact `relation_oid` (`regclass`).
2. **Non-updatable `security_barrier` views**: each `silo_bucket.b_<bucket_id>` view is a `security_barrier = true` join over `silo.objects` and `silo.buckets` without `INSTEAD OF` triggers. Granting `INSERT` or `DELETE` on the view serves purely as an authorization bit checked via `has_table_privilege(session_user_oid, relation_oid, priv) IS TRUE`; an application role cannot execute raw `INSERT`, `UPDATE`, or `DELETE` statements against the view to bypass the version state machine.
3. **Clean lifecycle on bucket recreation**: dropping a bucket drops its `silo_bucket.b_<bucket_id>` view in the same transaction. Recreating a bucket with the same name allocates a new `bucket_id` and a new ACL relation with no stale grants carried over.

## Caller identity (`session_user`) and `SECURITY DEFINER` hardening {#security-hardening}

Inside a `SECURITY DEFINER` function, `current_user` and `current_role` resolve to the function owner (`silo_owner`), not the caller. Checking privileges against `current_user` would grant every caller full owner access.

To prevent privilege escalation:

- **Fixed `session_user` principal**: every `silo_api` entry point authenticates `session_user` (resolved to its role OID) and explicitly verifies `has_function_privilege(session_user_oid, '<function_signature>'::regprocedure, 'EXECUTE') IS TRUE`.
- **Rejecting `SET ROLE` and owner logins**: if `current_setting('role')` is not `'none'`, or if `session_user` is the installation owner (`silo_owner`) or a member of it, the function rejects the call with `SQLSTATE 42501`. An unprivileged user cannot wrap a `silo_api` call inside their own `SECURITY DEFINER` function or use `SET ROLE` to impersonate another role.
- **Locked `search_path`**: all `silo_api` functions set `search_path = pg_catalog, pg_temp` and schema-qualify every table, function, and operator reference so user-created objects in `pg_temp` or `public` cannot hijack execution.
- **Immediate `REVOKE` visibility**: cached PL/pgSQL plans do not always process PostgreSQL catalog invalidation messages before evaluating `has_table_privilege`. Every `silo_api` call first runs a lightweight dynamic `EXECUTE 'SELECT 1 FROM silo.meta LIMIT 0'` to force syscache invalidation acceptance, ensuring a committed `REVOKE` takes effect on the very next statement.

## Transactional semantics and synchronized bucket policies {#transactions-and-policies}

- **`Read Committed` write contract**: `put_object`, `delete_object`, `copy_object`, `create_bucket`, and `drop_bucket` require `Read Committed` isolation. Under `Repeatable Read` or `Serializable`, a transaction waiting on `silo.lock_key` would still see its pre-lock snapshot and miss a concurrent latest version; write functions therefore reject higher isolation levels with `SQLSTATE 0A000`.
- **Deadlock-safe `copy_object`**: `silo_api.copy_object` acquires `FOR SHARE` on the source and destination bucket rows in ascending `bucket_id` order, reads the source metadata, segments, and annotations from a single snapshot without holding a source key lock across the destination write, and publishes the destination version atomically.
- **Policy synchronization (`policy_ready`)**: S3 stores bucket configuration in `.minio.sys/buckets/<bucket>/.metadata.bin`, while SQL functions inspect structured columns on `silo.buckets` (`versioning`, `object_lock`, `default_sse`, `policy_ready`). Whenever a gateway updates `.metadata.bin`, it decodes and updates those structured columns in the same `FOR UPDATE` transaction. SQL write functions require `policy_ready = true`, enforce default Object Lock retention and hard bucket quotas, and reject plaintext writes to buckets with default SSE enabled.

---

Backlinks:

- [Design](/docs/s3/design/)
