Row-Level Security (RLS) moves tenant isolation from “every engineer remembers WHERE tenant_id = $1” to “PostgreSQL refuses to return another tenant’s rows even if a query forgets the filter.”
In a multi-tenant SaaS product, forgetting a single WHERE tenant_id = $1 in one reporting query or background job is an instant S1 data breach. In Colophon CMS, we pair type-level tenant scoping in Rust with PostgreSQL RLS so both layers have to fail simultaneously before a cross-tenant read can happen.
The four rules of production RLS
Most RLS tutorials stop right after ENABLE ROW LEVEL SECURITY, leaving three dangerous gaps open:
- Run the app as a non-superuser role without
BYPASSRLS. Superusers and roles withBYPASSRLSignore every RLS policy silently. Separate your migrator role (owns tables) from your runtime app role (least-privilegeSELECT, INSERT, UPDATE, DELETEonly). - Always add
FORCE ROW LEVEL SECURITYif the table owner could ever query it. WithoutFORCE, the table owner bypasses RLS by default. - Set tenant context with
SET LOCALinside a transaction, never session-levelSET. With connection pooling (PgPool, PgBouncer), a session-levelSET app.tenant_idleaks onto the next HTTP request that borrows that pooled connection.SET LOCALresets automatically atCOMMITorROLLBACK. - Fail closed when the setting is missing. Use
current_setting('app.tenant_id', true)and compare strictly—if the variable is unset or empty, return zero rows instead of throwing or matching everything.
Exact schema and policy setup
CREATE TABLE documents (
id uuid PRIMARY KEY,
tenant_id uuid NOT NULL,
title text NOT NULL,
body jsonb NOT NULL
);
CREATE INDEX idx_documents_tenant ON documents (tenant_id, id);
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE documents FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON documents
AS RESTRICTIVE
FOR ALL
TO app_runtime
USING (
tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid
)
WITH CHECK (
tenant_id = NULLIF(current_setting('app.tenant_id', true), '')::uuid
);
Notice both USING (guards SELECT, UPDATE, DELETE) and WITH CHECK (guards INSERT and UPDATE so Tenant A cannot insert a row claiming to belong to Tenant B).
And on every request in Rust (sqlx):
let mut tx = pool.begin().await?;
sqlx::query("SELECT set_config('app.tenant_id', $1, true)")
.bind(tenant_id.to_string())
.execute(&mut *tx)
.await?;
// All queries on &mut *tx are now strictly scoped to `tenant_id`.
// On commit or drop/rollback, the local setting disappears automatically.
The third argument true in set_config('app.tenant_id', $1, true) is the SQL equivalent of SET LOCAL.
Watch out for the Neon Postgres gotcha
If you host on Neon (where there is no superuser), do not attach SET app.tenant_id = '' directly onto a CREATE FUNCTION ... SET or ALTER DATABASE ... SET statement—Neon refuses custom parameter SET clauses for non-superusers and refuses GRANT SET ON PARAMETER. Instead, save, clear, and restore the setting inside the function body with set_config. (See Migrating to Neon Postgres without superuser.)
Prove RLS works with a cross-tenant attack test
Never assume a policy is active until a test tries to break it:
- Insert a row for Tenant A and a row for Tenant B.
- Connect using the actual
app_runtimerole (not the test superuser!). - Run
SELECT count(*) FROM documentswithout settingapp.tenant_id→ assert0. - Set
app.tenant_idto Tenant A and runSELECT * FROM documents WHERE id = $tenant_b_row_id→ assert0rows. - Attempt
INSERT INTO documents (id, tenant_id, ...) VALUES (..., $tenant_b_id, ...)while scoped to Tenant A → assert PostgreSQL rejects it with a policy violation (42501).
Pair that test with a mutation proof so you know it goes red if someone accidentally grants BYPASSRLS or drops the policy.