Skip to content

Objects from SQL

Write and read objects inside a PostgreSQL transaction with silo_api.
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 first so the silo database and plaintext attachments bucket exist.

Create an application SQL role

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

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:

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 for the complete permission mapping.

Reconnect as app_sql and create the business table:

psql 'host=pg.example.net port=5432 dbname=silo user=app_sql sslmode=verify-full sslrootcert=/path/to/pg-ca.crt' -W
\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

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

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:

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:

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

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

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 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

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

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:

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 and the SQL API reference.

Scan to open this page
https://mimus.pgsty.com/docs/s3/tutorial/sql/