Getting started
pg_rebac is slice 1 and pre-release. Everything on this page was run against Postgres 17 with pgvector 0.8.
Install the SQL
Section titled “Install the SQL”The whole schema is one file of plain SQL and PL/pgSQL. It needs no C extension, so it installs on any Postgres 14
or later, hosted or not. Everything lives in the schema rebac.
psql -v ON_ERROR_STOP=1 -f sql/rebac.sqlRunning it again is safe: tables are created if not exists and functions are replaced.
A first readable set
Section titled “A first readable set”In production the rebacd daemon fills the projection from SpiceDB and heartbeats, and reads fail closed when the
heartbeat is older than max_staleness_ms (5,000 by default). To try the SQL on its own, turn the bound off and
project a principal by hand with rebac.project, the same entry point the daemon uses:
-- no daemon in this walkthroughupdate rebac.config set value = '0' where key = 'max_staleness_ms';
-- tenant 'acme', principal 'priya', two containers she may viewselect rebac.project('acme', 'priya', array['handbook', 'pricing'], null);Every read names its tenant with a transaction-local setting, then asks for the set:
begin;set local rebac.tenant = 'acme';select * from rebac.readable_set('priya'); -- handbook, pricingselect rebac.breadth('priya'); -- 2commit;Without a tenant, the read is an error (RB400), not an empty set. A principal nobody has asked about is RB404:
ERROR: rebac: this principal has not been projectedHINT: ask the daemon (POST /lookup) to project it, then retrySee the readable set for the three answers a read can give.
Fence a pgvector query
Section titled “Fence a pgvector query”Put the fence in the where clause of the query you already have. Postgres inlines rebac.readable_set into a
semi-join on the projection’s primary key, so it applies before the order by and the limit.
create extension if not exists vector;create table passage ( id bigserial primary key, tenant text not null, container text not null, body text not null, embedding vector(3) not null);create index on passage using hnsw (embedding vector_cosine_ops);insert into passage (tenant, container, body, embedding) values ('acme', 'handbook', 'Leave policy', '[1,0,0]'), ('acme', 'board', 'Board minutes', '[0.9,0.1,0]'), ('acme', 'pricing', 'Price list 2027', '[0.8,0.2,0]');
begin;set local rebac.tenant = 'acme';set local hnsw.iterative_scan = relaxed_order;select id, body from passagewhere container in (select * from rebac.readable_set('priya'))order by embedding <=> '[1,0,0]'limit 20;commit; id | body----+----------------- 1 | Leave policy 3 | Price list 2027The board minutes are closest to the query and are not returned: priya cannot view board.
Set hnsw.iterative_scan = relaxed_order on every fenced HNSW read. Without it, pgvector stops after ef_search
candidates and the fence silently returns fewer than k rows (measured).
- Narrow readers are faster and complete on the exact path: breadth and the two paths.
- Keep the projection in step with SpiceDB: rebacd.
- Write relationships through
rebac.writeso revocations are in force at commit: revocations and consistency.