# Objects from SQL

> Write and read objects inside a PostgreSQL transaction with silo_api.

---

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

---

> [!NOTE]
> The SQL transaction API (`silo_api`) and PostgreSQL native authorization (`silo_bucket`) are **Preview** features in the MVP scope.

This tutorial writes an invoice row and its attachment in one PostgreSQL transaction, verifies both from SQL and S3, rolls back a second transaction, and reads paginated lists and object segments. Complete the [Quick start](/docs/s3/quickstart/) first so the `silo` database and plaintext `attachments` bucket exist.

## Create an application SQL role {#create-sql-role}

Connect to `silo` as a database administrator and create an unprivileged login role (`app_sql`) and application schema (`app_demo`):

```sql
CREATE ROLE app_sql LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;
\password app_sql
CREATE SCHEMA app_demo AUTHORIZATION app_sql;
```

The application role needs no superuser rights, no membership in `silo_owner`, and no direct access to the private `silo` schema.

Next, connect to `silo` as the installation owner (`silo_owner`) and grant `EXECUTE` on the required `silo_api` functions plus `SELECT` and `INSERT` on the ACL relation for `attachments`:

```sql
GRANT USAGE ON SCHEMA silo_api, silo_bucket TO app_sql;
GRANT EXECUTE ON FUNCTION
  silo_api.put_object(text,text,bytea,text,jsonb,boolean,text),
  silo_api.get_object(text,text,uuid),
  silo_api.stat_object(text,text,uuid),
  silo_api.read_object(text,text,bigint,bigint,uuid,bigint),
  silo_api.list_objects(text,text,text,integer)
TO app_sql;
SELECT format('GRANT SELECT, INSERT ON TABLE %I.%I TO %I',
              n.nspname, c.relname, 'app_sql')
FROM silo.bucket_acl AS a
JOIN silo.buckets AS b ON b.id = a.bucket_id
JOIN pg_catalog.pg_class AS c ON c.oid = a.relation_oid
JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
WHERE b.name = 'attachments' AND n.nspname = 'silo_bucket' \gexec
```

In `psql`, `\gexec` executes the generated `GRANT` statement. Because a bucket's ACL relation name in `silo_bucket` is decoupled from the bucket name, `silo_owner` looks it up through `silo.bucket_acl`. See [Authorization](/docs/s3/reference/authorization/) for the complete permission mapping.

Reconnect as `app_sql` and create the business table:

```bash
psql 'host=pg.example.net port=5432 dbname=silo user=app_sql sslmode=verify-full sslrootcert=/path/to/pg-ca.crt' -W
```

```sql
\set ON_ERROR_STOP on
SELECT session_user, current_user;
CREATE TABLE app_demo.invoices (
    invoice_id bigint PRIMARY KEY,
    object_key text NOT NULL,
    object_id bigint NOT NULL,
    object_version uuid,
    amount numeric(12,2) NOT NULL
);
```

`silo_api` checks the login-time `session_user` and rejects `SET ROLE` impersonation.

## Commit a business row and object together {#commit-together}

In the `app_sql` session, write an object and insert the referencing invoice row in one Read Committed transaction:

```sql
BEGIN ISOLATION LEVEL READ COMMITTED;
WITH written AS (
  SELECT * FROM silo_api.put_object(
    'attachments', 'invoices/1001.txt',
    convert_to('invoice 1001 amount 199.00', 'UTF8'),
    'text/plain', '{"invoice_id":"1001"}'::jsonb
  )
)
INSERT INTO app_demo.invoices
  (invoice_id, object_key, object_id, object_version, amount)
SELECT 1001, 'invoices/1001.txt', object_id, version_id, 199.00
FROM written;
COMMIT;
```

Read the committed object body and metadata from SQL:

```sql
SELECT convert_from(
  silo_api.get_object('attachments', 'invoices/1001.txt'), 'UTF8'
);
SELECT i.invoice_id, i.amount, o.object_id, o.version_id, o.size, o.etag
FROM app_demo.invoices AS i
CROSS JOIN LATERAL silo_api.stat_object('attachments', i.object_key) AS o
WHERE i.invoice_id = 1001;
```

Read the same object over S3:

```bash
mcli_pg cat pg-app/attachments/invoices/1001.txt
```

`get_object` returns `bytea`. Use `convert_from` only when the payload is known UTF-8 text.

## Roll back a business row and object together {#rollback-together}

Start a second transaction, write both the object and the invoice row, and roll back:

```sql
BEGIN ISOLATION LEVEL READ COMMITTED;
WITH written AS (
  SELECT * FROM silo_api.put_object(
    'attachments', 'invoices/1002.txt',
    convert_to('this invoice will be rolled back', 'UTF8'), 'text/plain'
  )
)
INSERT INTO app_demo.invoices
  (invoice_id, object_key, object_id, object_version, amount)
SELECT 1002, 'invoices/1002.txt', object_id, version_id, 299.00
FROM written;
ROLLBACK;
SELECT count(*) FROM app_demo.invoices WHERE invoice_id = 1002;
SELECT count(*) FROM silo_api.list_objects('attachments', 'invoices/1002.txt');
```

Both counts return `0`. Neither the relational row nor the S3 object is published.

If your application needs a permanent reference to an immutable object body, [enable bucket versioning](/docs/s3/tutorial/versioning/) and pass the saved `version_id` when calling `get_object`, `stat_object` or `read_object`. Overwriting an unversioned object replaces its body.

## Paginate listings and read segments {#paginate-and-stream}

List the first page of objects under the `invoices/` prefix:

```sql
SELECT key, size, etag FROM silo_api.list_objects(
  'attachments', 'invoices/', NULL, 100
);
```

To fetch the next page, pass the final `key` of the current page as the third argument (`start_after`). Each call returns at most 1,000 live objects in `C` byte order and omits delete markers.

To read a large object in bounded chunks without materializing the full body in one call, open a read-only Repeatable Read snapshot and call `read_object`:

```sql
BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY;
SELECT object_id, version_id, offset_bytes, octet_length(data)
FROM silo_api.read_object('attachments', 'invoices/1001.txt', 0, 8388608);
-- Advance offset_bytes on subsequent calls within the same transaction.
COMMIT;
```

`read_object` returns at most 8 MiB (`8388608` bytes) per call. Use `REPEATABLE READ` only for read-only transactions; SQL writes through `silo_api` require `READ COMMITTED`.

Direct SQL writes do not emit S3 bucket notifications. To publish events atomically with SQL object writes, insert into a transactional outbox table in the same transaction; see [Multipart, Object Lock and STS](/docs/s3/tutorial/workflows/#sql-outbox) and the [SQL API reference](/docs/s3/reference/sql-api/).

---

Backlinks:

- [Quick start](/docs/s3/quickstart/)
- [SQL API](/docs/s3/reference/sql-api/)
- [Tutorials](/docs/s3/tutorial/)
- [Multipart, Object Lock and STS](/docs/s3/tutorial/workflows/)
