Sub topic 13
Performance: Policy Predicates, Indexes, and EXPLAIN
Prerequisites: You should know basic PostgreSQL indexes, WHERE clauses, roles, and how to read a simple EXPLAIN plan.
Row-level security (RLS) adds policy expressions to a query. PostgreSQL applies these expressions when it determines which rows the current role may read or change. For example, this policy adds a tenant filter:
CREATE POLICY tenant_isolation ON invoices
USING (
tenant_id =
NULLIF(current_setting('app.tenant_id', true), '')::uuid
)
WITH CHECK (
tenant_id =
NULLIF(current_setting('app.tenant_id', true), '')::uuid
);A query such as SELECT * FROM invoices WHERE amount_cents > 1000 must satisfy both the user's WHERE clause and the policy condition.
Index the columns used by policies
An RLS predicate does not automatically create an index. If a policy frequently filters by tenant_id, create an index for that column:
CREATE INDEX invoices_tenant_id_idx
ON invoices (tenant_id);The index can help PostgreSQL find rows for the current tenant before checking other conditions. A query that filters by both tenant and status may benefit from a multicolumn index:
CREATE INDEX invoices_tenant_status_idx
ON invoices (tenant_id, archived);Choose indexes based on real query patterns. Each index increases storage use and makes inserts, updates, and deletes more expensive.
| Policy expression | Useful index |
|---|---|
| `tenant_id = ...` | `(tenant_id)` |
| `tenant_id = ... AND archived = false` | `(tenant_id, archived)` |
| `owner_id = current_user` | An index on `owner_id` |
An expression around the indexed column can prevent ordinary index use. For example, lower(email) = ... usually needs an expression index:
CREATE INDEX users_lower_email_idx
ON users (lower(email));Keep policy predicates simple when possible. Functions, casts, subqueries, and joins can make planning harder or add per-row work.
Inspect the plan as the real role
Use EXPLAIN to see how PostgreSQL plans a query:
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, amount_cents
FROM invoices
WHERE amount_cents > 1000;Run this while using the same role and session settings as the application. RLS depends on the current role and, in a tenant design, on settings such as app.tenant_id. A table owner or a role with BYPASSRLS may receive a different plan because RLS does not restrict that role.
Look for:
- An index scan or bitmap index scan using the tenant column.
- A sequential scan on a large table when an index should help.
- High “Rows Removed by Filter” values, which can indicate that many rows were scanned and rejected.
- A large difference between estimated rows and actual rows.
- Extra work from policy subqueries or functions.
EXPLAIN ANALYZE executes the statement. Use it carefully with INSERT, UPDATE, or DELETE; test those statements inside a transaction that you can roll back.
Measure before changing the policy
A policy that calls a simple comparison against a session setting is usually easier to optimize than one that runs a complex subquery for every row. If a policy depends on another table, measure both query time and buffer reads.
For a tenant policy, check the plan after creating the index and setting the tenant:
BEGIN;
SELECT set_config(
'app.tenant_id',
'3f2b8c1e-5a4d-4e0b-9f6a-1c2d3e4f5a6b',
true
);
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM invoices
WHERE amount_cents > 1000;
ROLLBACK;The transaction-local setting avoids leaving tenant context on a pooled connection.
Exercise
An RLS policy filters invoices with tenant_id = ..., but EXPLAIN shows a sequential scan on a large table. What should you check first?
Show answer
Check that an index exists on tenant_id, then rerun EXPLAIN (ANALYZE, BUFFERS) as the application role with the tenant setting configured.