0
Challenge · 58RLS at Scale

Multi-tenancy: orgs, memberships & tenant isolation

Securityused in ~85% of apps

In an app-layer world, tenant isolation is a discipline: every single query, in every route, in every service, must remember to add `where org_id = :currentOrg`. Miss it once — one report, one admin script, one new endpoint written at 11pm — and a customer sees another customer's data. That is not a hypothetical: it is the single most common root cause of a multi-tenant SaaS data breach. RLS turns tenant isolation from something every engineer must remember into something the database refuses to violate, no matter which query, which route, or which engineer forgot.

Core idea. Multi-tenancy needs exactly three ingredients: a table of tenants (`orgs`), a join table saying who belongs to which tenant and with what power (`memberships(org_id, user_id, role)`), and every tenant-owned table carrying its own `org_id` foreign key. Isolation is then one reusable predicate — `is_org_member(org_id)`, the SECURITY DEFINER helper from concept 57 — called from every tenant table's policy, so the rule lives in exactly one place instead of being retyped, and possibly mistyped, on every table. Role-scoped writes layer a second predicate, `is_org_admin`, on top of plain membership.
Playground, this isn't a screenshot
Live demointeractive
Real Postgres · in your browseridle
Run this as each of the four identities. Alice and Carol (same org) see the Acme roadmap only. Bob sees only Globex. The stranger — who has never had a membership row anywhere — sees nothing at all, and gets no error telling them why.
Querying as
Cmd/Ctrl + Enter
↑ play a little first, then dig in