Contents
What pg_rebac guarantees, and what it does not
Guarantees
- G1, the fence precedes the read. Every row a query fenced by
rebac.readable_setreturns lies in a container thatrebac.readableholds for (tenant, principal), at the snapshot the query reads. No row is returned unlessrebac.checkedhas passed for that query. (A query that is empty for another reason may return empty without evaluating it.) - G2, a revocation is in force at commit. A view revocation written through
rebac.writeis absent from the projection for every affected principal when the writing transaction commits, whether or not SpiceDB or the daemon is up. That holds exactly for:- direct relations;
- the relations reached through an arrow (
workspace->admin); - memberships of subject sets.
If a lock cannot be taken within
narrow_lock_timeout_ms, the COMMIT fails; it never commits an unnarrowed revocation.
Two kinds of removal narrow EVERYONE who holds a container: an arrow tuple (a file leaving its space) and a grant to a subject set. They name no principal, so the trigger holds the TENANT exclusively before it reads who the holders are. Every projection writer (a refresh, a lazy first read, a retirement) holds its tenant shared, then its principal, from before it asks SpiceDB until it commits. So a refresh in flight commits before the holders are read (and its row is narrowed), and a refresh that starts later waits for the commit (and subtracts the revocation as pending).
tests/sql/09_revocation_race.sqldrives both orders. The cost: such a revocation waits for every refresh of its tenant in flight, withinnarrow_lock_timeout_ms. The relay’s own re-narrowing of the deletes it delivers takes no tenant lock, so a relay waiting on SpiceDB never holds such a revocation up.G2 needs READ COMMITTED (the Postgres default). Under REPEATABLE READ or SERIALIZABLE the commit-time delete reads the transaction’s first snapshot, which cannot see a refresh that committed after it, so
rebac.writerefuses a view revocation there withRB422. Grants are allowed at any isolation level.G2 for arrows, via relations and sets also needs
edgeandset_grantto be complete. Every such write must go throughwrite, or the tenant must have been backfilled. G3, no regrowth. No refresh can restore a container whose revocation is pending in the outbox. No refresh can restore anything for a retired principal.
“Pending” ends when the relay delivers the delete. A refresh looked up
at_least_as_freshan OLDER token after that may, by SpiceDB’s consistency rules, be served from a revision up to the revision quantization window (5 s by default) before the delete. For a direct relation this heals within seconds: the delivery queues that principal’s refresh at the delete’s token. For an arrow tuple or a grant to a subject set, the holders it narrowed are not named by the delivery, so a container regrown this way stays until the sweep re-sends the revocation (aftersweep_settle_ms) or re-projects them. This is reasoned from SpiceDB’s documented semantics, not observed.consistency = "fully_consistent"closes it.- G4, at least as fresh. A refresh queued by a delivery is looked up at
at_least_as_freshthe token SpiceDB wrote that delivery at. A first read, and anything with no token, is looked up fully consistent. - G5, tenant isolation. Every key carries the tenant. A tuple whose ids carry another tenant’s prefix is refused
by
rebac.write, maps to nobody, and is dropped from a lookup. G6, fail closed.
- An unknown principal is
RB404. - A missing tenant is
RB400. - A daemon that is not live, a set older than the freshness bound or of unknown age, an unavailable lookup and an
over-cap set are each
RB503. - None of these is an empty set, or a wider one.
- An unknown principal is
G7, freshness. A principal’s set is read only while the SpiceDB lookup behind it began within
max_projection_age_ms(one hour by default) of the read; a set with no lookup time is never read. The lookup time is taken from the database’s clock just before SpiceDB is asked. A lookup madefully_consistentreflects every relationship SpiceDB held at that instant. One madeat_least_as_fresha token may be served from a revision up to SpiceDB’s revision quantization older (--datastore-revision-quantization-interval, 5 s by default). Narrowing only removes rows and never moves the time.G7 is the bound for what reaches SpiceDB WITHOUT
rebac.write(another service, thezedCLI). Those changes are picked up only by a re-projection, and the daemon’s sweep re-projects everyone everysweep_interval_ms(15 minutes by default), so keep the bound above the sweep interval plus the time a sweep takes to drain. Revocations throughrebac.writenever wait on it (G2).Liveness is separate.
max_staleness_ms(5 s) fails reads closed when the daemon has not heartbeated. A beat proves only that the daemon is running and can reach SpiceDB and Postgres. It says nothing about how current any set is.On upgrade, rows written before
looked_up_atexisted have no lookup time. They read asRB503until the sweep re-projects them (orPOST /lookupre-projects one).rebac.status()counts them asunfresh_principals.G8, the closure matches the live schema. The daemon re-derives the closure whenever SpiceDB’s schema text changes: on Watch’s notice, and by re-reading it every
closure_check_ms(10 s). A change to what grants view marksrebac.closure_stateinvalid in the same transaction that stores the new list, so every read isRB503until every tenant’s relationships are re-read into the mirrors. After that, a principal looked up before the change (or of unknown lookup time) staysRB503until it is re-projected. An unsupported or unparseable schema keeps every readRB503. A daemon in shadow mode never changes the live closure state. Between a schema write in SpiceDB and the daemon noticing it (up toclosure_check_ms, or the next start), reads run on the old closure; that window is the one this guarantee does not close.G9, RLS cannot be widened. The policy
rebac.enable_rlsgenerates isRESTRICTIVE, so it is ANDed with every other policy on the table. Withprincipal_source => 'session', a role withoutEXECUTEonrebac.set_principalcannot choose the principal, and needs no access to the projection itself.
Not guaranteed
- RLS policies made by an earlier version stay permissive until
rebac.enable_rlsis run again on the table. - The session binding holds only for a role that logs in as itself. A session whose login role is trusted can
RESET ROLEafter aSET ROLEand bind any principal; SQL you do not trust must connect as the untrusted role. The principal setting is trusted input. With the default
principal_source => 'setting', and for any direct call ofrebac.readable_set(p), whoever can run SQL chooses the principal. Use that form only for an application role that sets it after authenticating the person.Grants are eventually consistent. A grant shows up after relay delivery and a refresh. Until then a reader is briefly narrower, which is the one direction a fence may err.
- Writes not made through
rebac.write(another service, thezedCLI) are counted on Watch and narrowed by the sweep only. G2 does not cover them. - The “still held” rule reads the outbox’s net state, so it is exact only when every view relation is written
through
rebac.write. - Caveats, expiring relationships and exclusion are refused at start, not evaluated.
- A read replica holds G2 only once its replay has passed the commit.
- Projection rows written by anything but
rebac.project(an application’s own mirror intorebac.readable) do not take the lock order, so G2’s race argument does not cover them.
Where slice 1 differs from the design document
- The planner-shaped primitive is the set, not the boolean. Postgres never inlines a scalar SQL function whose
body has a sub-select, so
readable_by(p, c)written asexists (...)is a per-row function call. Measured withEXPLAIN (VERBOSE),container in (select * from rebac.readable_set(p))is inlined into a semi-join.readable_byremains, as a per-row check. - The principal’s check is a pseudo-constant qual. It is evaluated once, in a gating filter. The principal is compared as the caller’s own value, so the planner sees its statistics.
- Liveness fails closed by default (5 s), as the owner decided, and so does freshness (one hour). Revocations through the outbox never wait on either.
- Consistency is
at_least_as_freshby default, as the owner decided.fully_consistentis a setting. - Subject sets, one level, are supported in slice 1. The first production schema uses them.