# SQL API

> The eleven silo_api functions, their arguments, permissions and limits.

---

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

---

> [!NOTE]
> The public `silo_api` SQL interface is a **Preview** feature in the MVP release. Callers must connect with an ordinary application SQL role (never the installation owner) and execute write functions under `Read Committed` isolation.

The public SQL API consists of the 11 functions in the `silo_api` schema listed below. Functions and tables in the `silo` schema are private implementation internals and must not be called by applications.

## Calling contract {#calling-contract}

- Authorization checks `session_user`; `SET ROLE` is rejected. The installation owner (`silo_owner`) and its role members cannot call `silo_api` functions.
- Callers need `USAGE` on `silo_api`, `EXECUTE` on each required function, and the corresponding table privileges on the bucket's ACL relation in `silo_bucket`. Functions are not granted to `PUBLIC`. See [Authorization](/docs/s3/reference/authorization/).
- Write functions require `Read Committed` (or PostgreSQL `Read Uncommitted`, which behaves identically). Calling write functions under `Repeatable Read` or `Serializable` raises `SQLSTATE 0A000`.
- Object mutations and application table rows commit or roll back atomically in the same SQL transaction. Independent S3 HTTP requests do not join that transaction. Direct SQL writes do not emit S3 bucket event notifications.
- Object keys are 1–1024 UTF-8 bytes; zero-length `bytea` bodies are valid. Passing SQL `NULL` for `p_version_id` targets the current version. To target an S3 `null` version explicitly, pass UUID `'00000000-0000-0000-0000-000000000000'` (not the string `'null'`).
- Inline `put_object`, `get_object`, and `copy_object` bodies are capped at `min(SILO_PG_MAX_OBJECT_SIZE, 256 MiB)`. `read_object` returns at most `8 MiB` (`8388608` bytes) per call.
- SQL functions do not decrypt SSE-encrypted bodies or allow plaintext writes to bypass default bucket encryption. All SQL mutations obey synchronized bucket policies, hard bucket quotas, Object Lock retention, and legal holds, with no governance bypass parameter.

## Object functions {#object-functions}

### `put_object` {#put-object}

```sql
silo_api.put_object(
  p_bucket text, p_key text, p_data bytea,
  p_content_type text DEFAULT 'application/octet-stream',
  p_user_meta jsonb DEFAULT '{}',
  p_if_none_match boolean DEFAULT false,
  p_if_match text DEFAULT NULL
)
RETURNS TABLE (
  object_id bigint, version_id uuid, etag text,
  size bigint, mod_time timestamptz
)
```

Requires `INSERT` on the destination bucket ACL relation. Setting `p_if_none_match = true` succeeds only when no current object body exists (a current delete marker does not block the write). `p_if_match` takes an unquoted ETag string, or `'*'` to require an existing current body. The two condition parameters are mutually exclusive and each additionally requires `SELECT` on the bucket. Conditions are evaluated atomically at publication.

`p_user_meta` must be a flat JSON object with string values (for example, `'{"invoice-id":"1001"}'`). Omit the `x-amz-meta-` prefix; the function normalizes keys and rejects reserved names, case-insensitive duplicates, CR/LF characters, and non-string values. Total normalized user metadata is capped at 2048 bytes, and `p_content_type` is capped at 1024 bytes without CR/LF.

### `stat_object` {#stat-object}

```sql
silo_api.stat_object(
  p_bucket text, p_key text, p_version_id uuid DEFAULT NULL
)
RETURNS TABLE (
  object_id bigint, version_id uuid,
  is_latest boolean, is_delete_marker boolean,
  size bigint, actual_size bigint, etag text, content_type text,
  user_meta jsonb, user_tags text, mod_time timestamptz,
  commit_seq bigint, encrypted boolean
)
```

Requires `SELECT` on the bucket. Returns metadata (including whether the selected version is a delete marker) without reading the object body or exposing private encryption keys. `user_meta` contains user metadata stripped of `x-amz-meta-` prefixes. `size` is the stored payload size and `actual_size` is the logical plaintext size.

### `get_object` {#get-object}

```sql
silo_api.get_object(
  p_bucket text, p_key text, p_version_id uuid DEFAULT NULL
) RETURNS bytea
```

Requires `SELECT` on the bucket. Returns the complete plaintext body within the 256 MiB inline limit. Raises `SO001` if the selected version is a delete marker or missing, and `0A000` if the object is encrypted.

### `read_object` {#read-object}

```sql
silo_api.read_object(
  p_bucket text, p_key text,
  p_offset bigint DEFAULT 0, p_length bigint DEFAULT 8388608,
  p_version_id uuid DEFAULT NULL, p_object_id bigint DEFAULT NULL
)
RETURNS TABLE (
  object_id bigint, version_id uuid, offset_bytes bigint, data bytea
)
```

Requires `SELECT` on the bucket. `p_offset` must be `>= 0` and not past the end of the object; `p_length` must be `0`–`8388608` (8 MiB). Reads crossing the end of the object are truncated. When `p_object_id` is non-NULL, the function verifies that the selected version still has that `object_id` and raises `SO002` on mismatch. To keep multi-chunk reads consistent across statements, pin an immutable `version_id` or run inside a read-only `Repeatable Read` transaction.

### `list_objects` {#list-objects}

```sql
silo_api.list_objects(
  p_bucket text, p_prefix text DEFAULT '',
  p_after_key text DEFAULT NULL, p_limit integer DEFAULT 1000
)
RETURNS TABLE (
  object_id bigint, key text, version_id uuid, size bigint,
  etag text, content_type text, mod_time timestamptz, commit_seq bigint
)
```

Requires `SELECT` on the bucket. Lists current visible objects (excluding delete markers) ordered by `key COLLATE "C"`. `p_limit` accepts `0`–`1000`. Pass the last `key` of a page as `p_after_key` to fetch the next page. Historical versions and S3 delimiter rollups are not exposed here.

### `delete_object` {#delete-object}

```sql
silo_api.delete_object(
  p_bucket text, p_key text,
  p_version_id uuid DEFAULT NULL, p_if_match text DEFAULT NULL
)
RETURNS TABLE (
  deleted boolean, version_id uuid, is_delete_marker boolean
)
```

Requires `DELETE` on the bucket (plus `SELECT` when `p_if_match` is set). Omitting `p_version_id` either removes the current object or inserts a delete marker according to the bucket's versioning state. Specifying `p_version_id` deletes that version unless blocked by Object Lock retention or legal hold (`SL003`).

### `copy_object` {#copy-object}

```sql
silo_api.copy_object(
  p_src_bucket text, p_src_key text,
  p_dst_bucket text, p_dst_key text,
  p_src_version_id uuid DEFAULT NULL,
  p_if_none_match boolean DEFAULT false,
  p_if_match text DEFAULT NULL
)
RETURNS TABLE (
  object_id bigint, version_id uuid, etag text,
  size bigint, mod_time timestamptz
)
```

Requires `SELECT` on the source bucket and `INSERT` on the destination bucket (plus destination `SELECT` when a destination condition is set). Reads the source body, tags, public metadata, and annotations from a single snapshot and publishes the destination object and annotations atomically. Rejects encrypted source objects or encrypted annotations.

## Bucket functions {#bucket-functions}

### `create_bucket` {#create-bucket}

```sql
silo_api.create_bucket(
  p_name text, p_region text DEFAULT 'us-east-1'
)
RETURNS TABLE (
  name text, created_at timestamptz, region text, acl_relation regclass
)
```

Controlled by `EXECUTE` privilege on `silo_api.create_bucket(text, text)`. Bucket names must be valid lowercase S3 names (3–63 bytes). Empty region defaults to `'us-east-1'`; `'EU'` maps to `'eu-west-1'`. The creator receives `SELECT`, `INSERT`, and `DELETE` on the new `acl_relation` in `silo_bucket`.

### `bucket_info` and `list_buckets` {#bucket-info-and-list}

```sql
silo_api.bucket_info(p_name text)
RETURNS TABLE (
  name text, created_at timestamptz, region text,
  versioning text, acl_relation regclass
)

silo_api.list_buckets()
RETURNS TABLE (
  name text, created_at timestamptz, region text,
  versioning text, acl_relation regclass
)
```

Filtered by the caller's bucket privileges: holding at least one of `SELECT`, `INSERT`, or `DELETE` on a bucket's ACL relation makes that bucket visible. `bucket_info` raises `42501` if the bucket does not exist or the caller lacks privileges.

### `drop_bucket` {#drop-bucket}

```sql
silo_api.drop_bucket(p_name text) RETURNS void
```

Requires `DELETE` on the bucket ACL relation and requires the bucket to be completely empty (no current objects, historical versions, or delete markers). There is no `force` parameter in SQL.

## Errors and retries {#errors-and-retries}

Classify failures by `SQLSTATE` code rather than error message text:

| SQLSTATE | Meaning | Application handling |
| --- | --- | --- |
| `42501` | Role, function, or bucket permission denied | Fix role bindings and `GRANT` statements; do not retry |
| `22023` | Invalid argument, byte range, condition, or metadata | Fix caller input |
| `0A000` | Unsupported write isolation level, encrypted payload, or unsynchronized bucket policy | Use `Read Committed` for writes; use S3 API for encrypted objects |
| `SO001` | Object body or version does not exist (or is a delete marker) | Handle missing object |
| `SO002` | ETag condition or `p_object_id` mismatch | Re-read current state and decide whether to retry |
| `SO003` | Object exceeds the 256 MiB inline SQL limit | Use `read_object` chunked reads or the S3 API |
| `SO004` | Incomplete object segment data | Abort and inspect storage consistency |
| `SB001` / `SB002` / `SB003` | Bucket missing / bucket not empty / bucket already exists | Handle at the application level |
| `SL003` | Version protected by Object Lock retention or legal hold | Keep protected version; SQL has no governance bypass |
| `SL004` | Bucket or object lock state not yet synchronized | Verify compatible gateway processes are running |
| `SQ001` | Hard bucket quota exceeded | Free logical space or increase bucket quota |
| `SA004` | Annotation payload integrity failure | Reject corrupt payload and inspect storage |
{.wide}

See the [SQL Object Operations](/docs/s3/tutorial/sql/) tutorial for end-to-end transactional examples.

---

Backlinks:

- [Architecture](/docs/s3/architecture/)
- [Quick start](/docs/s3/quickstart/)
- [Reference](/docs/s3/reference/)
- [Objects from SQL](/docs/s3/tutorial/sql/)
