Skip to content

Getting started

pg_rebac is slice 1 and pre-release. Everything on this page was run against Postgres 17 with pgvector 0.8.

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.

Terminal window
psql -v ON_ERROR_STOP=1 -f sql/rebac.sql

Running it again is safe: tables are created if not exists and functions are replaced.

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 walkthrough
update rebac.config set value = '0' where key = 'max_staleness_ms';
-- tenant 'acme', principal 'priya', two containers she may view
select 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, pricing
select rebac.breadth('priya'); -- 2
commit;

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 projected
HINT: ask the daemon (POST /lookup) to project it, then retry

See the readable set for the three answers a read can give.

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 passage
where 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 2027

The 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).