Tenant isolation with row level security: four conditions
Building a multi tenant application leaves two options: a database per customer, or one database with a tenant column. The second is cheaper and more dangerous. This text describes the conditions under which it still holds.
The mistake that never shows
The obvious solution is a tenant column plus the discipline to carry it in every query. That works as long as nobody writes a query without it.
That mistake is the most dangerous one these systems have, because it does not behave like a mistake. A forgotten condition raises no exception and no type error. It returns more rows than allowed, and more rows look like a successful call. It gets discovered the moment one customer has another customer’s data on screen.
Row level security moves the responsibility: the database filters, not the query. A policy per table compares the tenant column against a value from the session. Forget the condition and you still only get your own rows.
The four conditions without which it does not hold
Turning row level security on is the smaller part of the work. These four points decide whether it actually protects anything.
- Forced, not merely enabled. A table owner is exempt from its own policies by default. Without the forced variant, the very role many applications use out of convenience sees everything.
- No omnipotent role for application queries. A role allowed to bypass policies cancels the whole protection. Migrations may need such a role, running traffic may not.
- The tenant value must be scoped to the transaction. Set it for the connection and it survives the return into the connection pool. The next request receives a connection still bound to the previous tenant. This is not a theoretical risk, it is the classic path by which row level security turns into security theatre.
- The policy has to hit every table, including the new one. A table without a policy does not stand out, because nothing fails. That is why the check belongs in the test run and not in the documentation.
Ordering with vector search
As soon as semantic search enters, a second and less known problem appears. A similarity search over an index works with a candidate list: the index returns the nearest neighbours, and filtering happens afterwards.
If the tenant filter only applies after the index pass, the candidate list can consist entirely of other tenants’ rows. The result is then not wrong in the sense of leaking, but empty although matching rows of your own exist. The user does not see a gap, the user sees a bad product.
The way out is to bind the tenant context before the query so the database can push the filter into the search pass, and to use an index setting that keeps searching on filtered queries until enough permitted matches are found.
What this means when choosing a vendor
Asking whether the data is separated is too coarse to get a useful answer. Three sharper questions sort vendors quickly.
First: does the application run under a database role allowed to bypass row level security? A yes means the separation lives in the application layer, not in the database.
Second: how long does the tenant context last? If the answer is per connection, the connection pool is worth a follow up question.
Third: what happens when a new table arrives without a policy? The good answer is a test run that goes red. Every other answer describes a state that depends on attention.
Frequently asked questions
- Is a database per customer not simply safer?
- For separation yes, for everything else no. Schema migrations, backups, monitoring and shipping fixes multiply with the number of customers, and a tenant forgotten during a migration is its own failure path. Row level security with the four conditions above is the more maintainable choice, and cells holding several tenants each are the middle ground.
- Does row level security cost performance?
- Measurably, but manageably, as long as the tenant column comes first in the matching index. It gets expensive only when the filter cannot use the index, and that is readable in the query plan rather than from a hunch.