Quick revision
The short answer
The slowdown comes from how PostgreSQL applies policies: it adds the policy's condition to every query and evaluates it before user conditions that call non-LEAKPROOF functions, so if the policy uses a volatile function or the indexes do not start with tenant_id, the planner falls back to scanning. Keeping the policy a simple comparison with current_setting, marking helper functions STABLE and leading indexes with tenant_id fixes it, which EXPLAIN confirms. The reporting job sees everything because table owners bypass RLS unless FORCE ROW LEVEL SECURITY is set, and superusers or BYPASSRLS roles always do.
What to remember
In this answer, we explained both symptoms after enabling RLS. The slowdown came from a policy that used a VOLATILE helper and from indexes that did not start with tenant_id, so the policy became a per-row filter on a scan; a STABLE comparison with current_setting and a (tenant_id, created_at) index turned it back into an index condition, confirmed with EXPLAIN. The reporting job saw every tenant because owners bypass RLS unless it is forced, and superusers and BYPASSRLS roles always do, so we forced RLS, gave reporting a dedicated role with an explicit policy, and added tests per role.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The slowdown comes from how PostgreSQL applies policies: it adds the policy's condition to every query and evaluates it before user conditions that call non-LEAKPROOF functions, so if the policy uses a volatile function or the indexes do not start with tenant_id, the planner falls back to scanning. Keeping the policy a simple comparison with current_setting, marking helper functions STABLE and leading indexes with tenant_id fixes it, which EXPLAIN confirms. The reporting job sees everything because table owners bypass RLS unless FORCE ROW LEVEL SECURITY is set, and superusers or BYPASSRLS roles always do.
Why this question is asked
Row-level security is attractive because it promises tenant isolation without touching every query, so teams often switch it on late in a product's life, on tables with hundreds of millions of rows. Two surprises then show up together: some queries get much slower, and some jobs ignore the policies completely. Interviewers ask this debugging question because explaining both requires knowing how the planner treats policies and who RLS applies to. In this answer, we will recap how policies are applied, reproduce the slow plan, fix it step by step, explain the owner bypass, and set up reporting and tests the right way.
1A Quick Recap of How Policies Are Applied
When RLS is enabled on a table, PostgreSQL rewrites each query on it by adding the policy's USING condition, as if we had written it in the WHERE clause ourselves. So a policy is not free: it is an extra predicate that must be evaluated for the rows a query reads, and the planner must be able to turn it into an index condition to stay fast.
There is one more rule that matters for performance. To stop a query from leaking rows of other tenants through side effects, PostgreSQL evaluates the policy condition before any user condition that uses a function or operator not marked LEAKPROOF. A leakproof function is one that reveals nothing about its input except through its result, for example it never raises an error that contains the value. Simple equality on integers or uuids is leakproof; many others, including some text functions and every user-written function by default, are not.
2Reproducing the Slow Query
Suppose the policy was written with a helper function in PL/pgSQL, which is a common first version because it looks tidy.
Two things go wrong. First, a function declared without a volatility category is VOLATILE, which tells the planner its result may change on every call. A volatile expression cannot be used as an index condition, so tenant_id = current_tenant() becomes a filter run for each of the millions of rows. Second, the indexes were built before RLS, for queries that did not need a tenant, so even a well-written policy has no index that starts with tenant_id to use. Before RLS, the query used the created_at index; now it reads far more rows and checks the policy on each one, and 20 times slower is a typical result.
3Fixing the Plan
We make the policy something the planner can use as an index condition and give it the index to use.
- Run EXPLAIN (ANALYZE, BUFFERS)as the application role, tenant set
- Policy shown as Filter, not Index Cond?YesCheck function volatility and indexNoCheck user conditions
- Policy calls a VOLATILE function?YesInline current_setting or mark STABLENoLook at indexes
- Index leads with tenant_id?YesCompare plan againNoCreate (tenant_id, ...) index
4 steps in order. Play them, or step through them one at a time.
Always test as the real role: the owner may bypass the policy and show a misleading plan.
Now the policy's value is computed once per statement and both conditions go into the index scan, so the query reads only one tenant's recent rows. Notice that the fix has two parts and neither is enough alone: a STABLE policy with no tenant-leading index still scans, and a good index with a volatile policy is never used for the tenant condition.
The LEAKPROOF rule can also bite queries with their own conditions. If a query filters with a non-leakproof function, for example a case-insensitive search, that condition is applied only after the policy, so an index on that expression may not help. We check such queries one by one with EXPLAIN (ANALYZE, BUFFERS). Marking a function LEAKPROOF requires a superuser and is only correct if the function really cannot leak, so it is a last resort rather than a quick fix.
4Why the Reporting Job Sees Every Tenant
The second symptom has a simpler cause. RLS does not apply to the table owner by default, on the assumption that the owner manages the table. The reporting job connects as the owner, so for it the policy simply does not exist. Superusers and roles with the BYPASSRLS attribute also skip all policies, always, even with FORCE.
So the policy was working for the application role all along, and only the owner saw everything. That is also a warning about testing: if we tested the policy while connected as the owner, we would have seen all rows and concluded RLS was broken, or worse, we might have tested performance as the owner and seen a plan without the policy at all.
5Running Reporting the Intended Way
Cross-tenant reporting may be a legitimate need, but it should be a deliberate, visible permission rather than a side effect of owning the table. We create a dedicated reporting role with SELECT only, and a policy that grants it all rows explicitly.
Policies for the same command are combined with OR by default, so report_job sees all rows through its own policy while the application role is still limited to one tenant. Because the permission is a named policy, it shows up in reviews and audits. Finally, we add tests per role: the application role with a tenant sees only that tenant, without one sees zero rows, the reporting role sees all, and a check in CI fails if any table with RLS enabled is not forced or if a runtime role gains BYPASSRLS.
6Follow-up questions
Why does the owner bypass RLS by default at all?
Because the owner usually runs maintenance such as migrations, backups with pg_dump and data fixes, which need to see every row. PostgreSQL chose the convenient default and gives FORCE ROW LEVEL SECURITY for tables where even the owner must be restricted. The cleaner design is that no application or job connects as the owner.
Would a policy with a subquery against a tenant membership table be slower?
Often, yes. A condition like tenant_id IN (SELECT tenant_id FROM memberships WHERE user_name = current_user) is evaluated as a join or a subplan for every query, and it is harder for the planner to use an index on tenant_id. Setting a single tenant id per transaction keeps the policy a simple equality.
How would you roll out the index fix on a large table without downtime?
Create the new index with CREATE INDEX CONCURRENTLY, which does not block writes, check that it is valid, switch the policy, compare plans, and only then drop old indexes that no longer get used, after watching pg_stat_user_indexes for a while.
7Conclusion
In this answer, we explained both symptoms after enabling RLS. The slowdown came from a policy that used a VOLATILE helper and from indexes that did not start with tenant_id, so the policy became a per-row filter on a scan; a STABLE comparison with current_setting and a (tenant_id, created_at) index turned it back into an index condition, confirmed with EXPLAIN. The reporting job saw every tenant because owners bypass RLS unless it is forced, and superusers and BYPASSRLS roles always do, so we forced RLS, gave reporting a dedicated role with an explicit policy, and added tests per role.
Open with
For the slowdown, I would first run EXPLAIN ANALYZE as the application role with a tenant set. RLS adds the policy's condition to every query, and it is evaluated before user conditions that use non-leakproof functions.
Then, if they want more
A common mistake is a policy that calls a helper function written in PL/pgSQL, which is VOLATILE by default, so the planner cannot use it as an index condition and evaluates it per row on a scan. Also, the existing indexes, like one on created_at, were built for queries without a tenant, so there is no index starting with tenant_id.
I fix it by writing the policy as a simple comparison with current_setting, which is STABLE, or marking the helper STABLE, and creating indexes that lead with tenant_id, concurrently, then checking that the plan shows an index condition. Marking functions LEAKPROOF is a superuser-only last resort.
For the reporting job, the cause is that table owners bypass RLS unless FORCE ROW LEVEL SECURITY is set, and superusers and BYPASSRLS roles always bypass it. So I would force RLS, stop connecting jobs as the owner, and if cross-tenant reporting is intended, give it a dedicated read-only role with an explicit policy using true for that role. Then I add tests per role to catch any bypass.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After enabling row-level security on a large orders table, a common query became 20...": https://scrivio-interview-library.netlify.app/databases/database-security/9ec566b42f/