A support engineer exported a customer's invoice history and found a second customer's invoice in the file. Nothing had thrown, no test had failed, and no alert had fired — the endpoint had been in production for a year. The query behind it was one of roughly forty that filtered by tenant, and it was the one written in a hurry against a table with a tenant_id column rather than against a repository that required one.
That is what a cross-tenant leak usually looks like: not a broken boundary, but an absent one at a single seam, inside a request that returns 200 and a body that looks entirely normal. Ordinary authorisation fails loudly — a user without a role gets a 403, someone notices. Tenant isolation fails quietly, because the response is well-formed, the row exists, and the only person who could recognise it as wrong is the customer who should never have seen it.
So the useful question is not "is the database isolated". It is: how many places in this system can reach tenant data, and what stops each one from reaching the wrong tenant's?
Shared schema is fine; the count of enforcement points is the risk
Three models get argued about endlessly, and the argument usually mistakes operational cost for safety.
| Model | What actually isolates | What it costs | How it fails |
|---|---|---|---|
| Database per tenant | Connection string | Migrations run N times, N connection pools, cross-tenant reporting is a data project | A job pointed at the wrong connection string writes one tenant's data into another's database |
| Schema per tenant | search_path on the connection | Migration fan-out, catalogue bloat, per-tenant DDL locks | A mis-set search_path falls back to public, and every query quietly reads the shared schema |
Shared schema, tenant_id column | Every query's predicate | One enforcement point per code path | A single missing predicate, in code that looks like the other forty |
All three reduce to the same underlying problem, which is that some piece of ambient state — a connection string, a search path, a session variable — has to be correct at the moment the query runs, and nothing in the type system knows about it. Shared schema does not introduce the hazard; it just makes the hazard visible, because it lives in your own query text instead of in deployment configuration.
Shared schema plus row-level security is a reasonable default for most products, and it becomes a genuinely strong design when you stop treating it as a policy file and start treating it as one layer of a boundary with several. What follows is the order those layers need to go in.
Row-level security is a backstop, and it has three ways to be off
The appeal of row-level security is that it enforces the predicate in the database, so a forgotten WHERE tenant_id = $1 returns nothing instead of everything. That is real, and it is worth having. It is also only true if the role running your queries is subject to the policy, and there are three ordinary ways for it not to be.
- The table owner bypasses its own policies. By default RLS does not apply to the owner, which means the migration role and the runtime role must be different, and the table needs
FORCE ROW LEVEL SECURITYif they ever coincide — which they will in development, and in whichever environment someone set up by running migrations as the app user. - Superusers and
BYPASSRLSroles ignore policies entirely. A managed database often hands you a superuser and everyone uses it because it works. Then RLS is doing nothing at all, everywhere, and the only evidence is that it never complains. - Views run with the owner's privileges. A view over a tenant table evaluates policies as its owner, so a reporting view can return every tenant's rows while the underlying table is correctly protected. Postgres 15 added
security_invokerfor exactly this, and a view created before that upgrade is a leak that looks like a feature.
-- The runtime role must not own the tables, must not be a superuser,
-- and must not carry BYPASSRLS. Verify, do not assume:
-- SELECT rolname, rolsuper, rolbypassrls FROM pg_roles WHERE rolname = current_user;
CREATE ROLE app_runtime LOGIN NOBYPASSRLS;
GRANT SELECT, INSERT, UPDATE, DELETE ON invoices TO app_runtime;
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY; -- owner is subject too
CREATE POLICY tenant_isolation ON invoices
AS PERMISSIVE
FOR ALL
TO app_runtime
USING (tenant_id = current_setting('app.tenant_id')::uuid)
WITH CHECK (tenant_id = current_setting('app.tenant_id')::uuid);
Two details in that policy are deliberate. current_setting is called without the missing_ok flag, so an unset context raises an error rather than evaluating to null. A null predicate matches nothing, which is the right outcome — zero rows — but it arrives as a successful empty result, and an empty result is indistinguishable from "this tenant has no invoices". The error is better: it points at the code path that forgot.
The second detail is WITH CHECK. Without it, or with the reflexive WITH CHECK (true) that appears in a lot of examples, a tenant can insert a row carrying someone else's tenant_id and the write succeeds. Reads stay clean, so the leak is invisible until the other tenant's next export.
One more asymmetry worth knowing before you rely on this. Policies are permissive by default and additive: several permissive policies combine with OR, so a broad policy added for one role widens access for every role it applies to. And where there is no policy for a command, the command is denied — a DELETE against a table with only a SELECT policy affects zero rows and returns success. That is safe for the data and dangerous for your error handling, because a handler that checks for an exception rather than a row count will report a deletion that never happened.
The tenant context is where it actually leaks
Everything above assumes the session variable holds the right tenant at the moment the policy is evaluated. That assumption is the part that breaks in production, and it breaks in a way that only appears under concurrency, which is why it survives testing.
Consider the two ordinary ways to set the context on a pooled connection.
-- Session scope. Persists for the life of the physical connection.
SET app.tenant_id = '...';
-- Transaction scope. Ends at COMMIT or ROLLBACK.
SET LOCAL app.tenant_id = '...';
A plain SET survives the request that issued it. Behind a transaction-mode pooler the physical connection is handed to the next client when the transaction ends, and the setting goes with it. If that next client is a route that never sets a tenant — a health check, a job, an internal admin path, an endpoint added last week — the policy evaluates against the previous tenant and returns that tenant's rows. The request has no idea it is reading someone else's data, because from its point of view it asked a question and got a plausible answer.
SET LOCAL is the fix, and it has its own trap. It is a no-op outside a transaction block, and it is a no-op that some drivers merely warn about and some do not warn about at all. If your query layer runs statements in autocommit, the SET LOCAL commits immediately, the value disappears, and the query that follows evaluates the policy against whatever the connection was already holding. The script looks correct. The written-down advice — "use SET LOCAL not SET" — was followed. The boundary was not.
The dangerous state is not a missing tenant. It is an inherited one. A missing context fails closed; an inherited context fails open, with data, silently, and only for some fraction of requests.
The consequence is a rule with no exceptions: the transaction and the context are one unit. There is exactly one function that opens a transaction, sets the context as its first statement, runs the caller's work, and commits. Nothing else is allowed to check out a connection.
import { Pool, type PoolClient } from 'pg';
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
export async function withTenant<T>(
tenantId: TenantId,
work: (client: PoolClient) => Promise<T>,
): Promise<T> {
const client = await pool.connect();
try {
await client.query('BEGIN');
// Transaction-scoped, and set inside the transaction: it cannot outlive
// this unit of work, so it cannot be inherited by the next holder.
await client.query('SELECT set_config($1, $2, true)', ['app.tenant_id', tenantId]);
const result = await work(client);
await client.query('COMMIT');
return result;
} catch (error) {
await client.query('ROLLBACK');
throw error;
} finally {
client.release();
}
}
Note the argument type. TenantId should be a branded type rather than string, minted in exactly one place — where the principal's membership is verified — and accepted nowhere else. Without the brand, the parameter is interchangeable with any other string, including the tenant claim from a request body, and the easy fix for a failing query becomes "pass the id the client sent".
declare const tenantBrand: unique symbol;
export type TenantId = string & { readonly [tenantBrand]: 'TenantId' };
/** The only way to obtain a TenantId: membership already checked. */
export function resolveTenant(principal: Principal, claimed: string): TenantId {
if (!principal.memberships.some((m) => m.tenantId === claimed && m.active)) {
throw new ForbiddenTenant(claimed);
}
return claimed as TenantId;
}
The same reasoning applies to the request shape. A route that reads ?tenantId= and passes it to withTenant has moved the boundary into the client. The tenant must come from the authenticated session; the query string, if it exists at all, is only a hint that gets checked against memberships before it becomes a TenantId.
Context that survives a queue, and the jobs that never had it
Async local storage makes the wrapper comfortable inside a request: establish the tenant once in middleware, read it deep in a repository, never thread it through fifteen signatures. It also creates a false impression of coverage, because the context dies at the process boundary. A worker consuming a message from a queue starts with no ambient tenant at all.
So the message payload carries the tenant, and the consumer re-enters withTenant with it before touching anything. The payload is data, not a claim: if the job can be enqueued by a tenant's user, the enqueue call is where membership gets checked, and the payload records the outcome rather than the request.
Cron-style jobs that sweep every tenant need one transaction per tenant, not one transaction with a list of ids. The tempting shortcut is a policy expression that accepts an array — tenant_id = ANY(current_setting('app.tenant_ids')::uuid[]) — which is fine in isolation and fails closed on an empty array. It becomes a hole the moment someone adds a branch for the "system" context, because the usual implementation of that branch is a predicate that returns true when the list is empty. A sweep job should look like a loop over withTenant calls, and the policy should never learn about a privileged context at all.
Structural constraints beat review discipline
Policies and wrappers are runtime checks. A few schema decisions remove entire classes of mistake by making the wrong row unrepresentable.
- Composite foreign keys. If
invoice_lines.invoice_idreferencesinvoices.id, nothing stops a line from pointing at another tenant's invoice; the write is legal, and the read that joins them either leaks or disappears depending on whose context it runs in. Reference(tenant_id, id)instead —FOREIGN KEY (tenant_id, invoice_id) REFERENCES invoices (tenant_id, id)— with a unique constraint on the parent pair. Now a cross-tenant link cannot be written, by any code path, including the one written in a hurry. - Uniqueness scoped to the tenant.
UNIQUE (email)on a users table turns "this address already exists" into a cross-tenant existence oracle, and refuses a legitimate second tenant's user.UNIQUE (tenant_id, email)is almost always what was meant. - An immutable
tenant_id. Rows that change tenant are rows that moved between customers, which is a data-migration decision, not anUPDATE. Revoke the column from the runtime role or reject the change in a trigger. - Opaque identifiers. Sequential keys invite enumeration, and enumeration is how a bug in one endpoint becomes a survey of the whole table. UUIDs do not fix a missing predicate, but they remove the easiest way to find one.
Adding tenant_id to a table that already has rows is its own migration problem — backfill, then enforce, in separate deploys, with the column nullable and the constraint added only once the backfill is complete. That ordering is the same expand-and-contract discipline as any other zero-downtime schema change, and getting it wrong is how a migration window becomes an outage.
The paths that never touch your ORM
Every one of these is a legitimate part of a mature system, and each is a place where the database's policy does not reach.
- Caches. A key like
invoice:{id}in shared Redis is tenant-blind. Either the key carries the tenant, or the value must never be reachable by an id alone. The same applies to cache invalidation and to negative caching, which is where a "not found" for tenant A gets served to tenant B. - Object storage and signed URLs. A signed URL is a bearer token: whoever holds it reads the object, with no further check. Bucket prefixes per tenant, authorization evaluated at signing time, short lifetimes — and a revocation path, because revoking someone's membership does not revoke a URL that was already issued.
- Search and vector indexes. These need the tenant filter inside the query, before scoring, not applied to the results afterwards. Post-filtering also breaks the ranking budget: retrieve fifty globally, drop the ones the caller cannot see, and the caller may be left with two results while the answer sat at position fifty-one. Our note on RAG in production covers the same failure from the retrieval side, and the ACL metadata in the index has to be refreshed when membership changes or it serves revoked documents with confidence.
- The warehouse. Row-level security in the operational database does not follow rows into an analytics store. An export job writes tenant data into tables that analysts query across tenants by design, and the export is now the boundary. Decide deliberately: per-tenant schemas in the warehouse, or aggregated and unlinked data only.
- Logs, traces and error tracking. These are query surfaces with no policy attached. A request log that includes a payload, an exception grouped across tenants, a trace that any engineer can open and search — all of it is data access, and none of it is covered by the policy you just wrote.
- Agent tools. An LLM with a SQL or API tool runs with whatever credentials the tool has. If the agent service holds a shared runtime role, the model can read every tenant, and the only thing between it and a cross-tenant answer is a sentence in a prompt. Give the agent a tool bound to the caller's tenant context, and treat its arguments as untrusted input — the same trust-boundary reasoning as prompt injection, applied to data access rather than instructions.
Tests that can actually fail
Isolation tests are usually written as a happy path: log in as tenant A, request A's record, expect 200. That test passes on every system, including the broken ones. The test that discriminates is the negative one, and it has a specific shape.
- Two tenants, minimum. A fixture that creates both, with a record in each, and a helper that authenticates as one and requests the other's id. Three if you support an admin or impersonation path, because that path is usually the one with the widened policy.
- Assert the shape of the refusal. A 403 on a foreign id confirms that the record exists, which is itself a leak of a small but real fact. A 404 is the right answer, and the test should pin it, along with the absence of any field belonging to the other tenant in the body.
- Assert writes by their effect, not their status. A cross-tenant
DELETEblocked by a missing policy returns success with zero rows. The test must reload the row and assert it still exists, then assert the response did not claim otherwise. - A concurrency test for the pool. Fire interleaved requests for both tenants in parallel against a pool of one or two connections, and assert every response contains only its own tenant's data. This is the only test that catches a session-scoped context leak, and it needs a real database rather than a mock — which is why it belongs in the integration suite that runs against a container.
- A completeness check. Enumerate the routes from the router and fail if a route has no isolation test registered against it. A matrix that is maintained by hand rots the first week someone is busy; one that is generated from the route table cannot silently miss the endpoint that was added on Friday.
Then add the two schema assertions that make the whole thing regression-proof. Both are ordinary queries and both belong in CI, not in a runbook.
-- 1. Every tenant table must have RLS enabled AND forced.
-- Returns rows when something is wrong; the test asserts zero rows.
SELECT c.relname
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname = 'public'
AND c.relname NOT IN ('schema_migrations')
AND (NOT c.relrowsecurity OR NOT c.relforcerowsecurity);
-- 2. The runtime role must not be able to bypass anything.
SELECT rolname FROM pg_roles
WHERE rolname = current_user
AND (rolsuper OR rolbypassrls);
The first query is the one that pays for itself. A new table added without a policy is otherwise a leak that ships and waits; with this assertion the build fails on the pull request, with a table name in the message, while the person who wrote it still remembers the schema.
What isolation does not buy you
Row filtering answers "may this caller see this row". It says nothing about the questions that arrive once a shared platform has real customers on it, and it is worth being explicit so nobody assumes otherwise.
- Noisy neighbours. One tenant's reporting query can saturate shared CPU, fill the connection pool, or push the queue latency up for everyone. Per-tenant concurrency limits and fair scheduling are a separate mechanism from row-level security, and they need their own design.
- Quotas and rate limits. Keyed on the verified tenant, not on an API key that a client can rotate, and enforced at the edge so a runaway client cannot reach the database at all.
- Cost attribution. You cannot answer "what does this customer cost us" from a shared table. Tenant-tagged telemetry from the first request is much cheaper than reconstructing it later.
- Deletion. "Delete this tenant" is a cascade across every table with legal meaning and a retention obligation attached. Soft-delete, a scheduled hard-delete, and a documented list of every store that holds a copy — including backups, caches and the warehouse — is the actual work.
- Residency and encryption requirements. Some contracts force per-tenant keys or a region boundary, and both are cheap at design time and very expensive at scale.
What this means in practice
The order matters more than the individual pieces, because each layer makes the next one cheaper to trust.
First, make the tenant context impossible to omit in code: one branded type, one function that opens a transaction and sets the context, and a lint rule or code review habit that rejects a raw connection checkout anywhere else. Second, turn on row-level security with a runtime role that is neither owner nor superuser, and force it on every table. Third, add the structural constraints — composite foreign keys, tenant-scoped uniqueness, an immutable tenant_id — so the schema refuses the mistakes the code has not been written to make yet. Fourth, sweep the paths outside the database, because that is where the policy does not reach. Fifth, put the concurrency test and the two schema queries in CI, so the boundary is checked by the build rather than by memory.
What remains is a boundary you can reason about: enforced at one choke point in code, verified at the storage layer, constrained in the schema, and asserted on every pull request. That is a different thing from a boundary enforced in forty places by forty people who each believed the other thirty-nine were careful. Isolation is a protocol, and protocols are only worth what their weakest participant enforces.
If you are adding a second customer to a system that was built for one, or inheriting a schema where tenant_id exists but nothing enforces it, tell us what the table layout looks like — this is ordinary, careful platform engineering, and it is the part of a product that is expensive to retrofit and unremarkable to build correctly the first time.