PostgreSQL RLS + Partitioning: Safe Multi-Tenant SaaS
Meta description: Learn how PostgreSQL RLS policies interact with declarative partitioning, avoid silent data leaks on new partitions, and optimize tenant isolation with partition pruning.
Tags: backend, saas, architecture, microservices, api
TL;DR
Combining PostgreSQL declarative partitioning with Row-Level Security is powerful, right up until a new partition silently inherits no policies and leaks cross-tenant data. Policy definitions live on the parent table but pg_policy visibility is per-child. If you’re not explicitly verifying RLS on every attached partition, you have a silent data leak waiting to happen in production.
The appeal is obvious, until it isn’t
In my experience building production multi-tenant systems, declarative table partitioning and Row-Level Security feel like the perfect pair. Horizontal scale via partition pruning on tenant_id, policy-enforced isolation without application-layer gymnastics. On a 500M-row events table with range partitioning, pruning can drop query cost from a full sequential scan to a single child-table scan — often 20–40x in wall clock time.
The mistake most teams make: assuming RLS on the parent table automatically and completely protects every child partition. It does, mostly. “Mostly” isn’t acceptable when you’re handling tenant data.
How policy inheritance actually works
PostgreSQL’s declarative partitioning propagates RLS enablement from the parent to child tables. When you run:
ALTER TABLE events ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON events
USING (tenant_id = current_setting('app.current_tenant')::uuid);
Child partitions inherit the RLS enabled flag. Here’s the counter-intuitive part: policies themselves are defined on the parent and apply when queries hit the parent. When the planner prunes to a child partition and executes directly against it, PostgreSQL re-checks RLS on the child — and if that child has no policies of its own, the behavior depends on the FORCE ROW LEVEL SECURITY setting and whether the role is a table owner.
The result: a BYPASSRLS superuser role, a misconfigured connection pool role, or a direct query against the child table can bypass tenant isolation entirely.
The pg_policy visibility gotcha
This is the silent failure mode. Run this after attaching a new partition:
SELECT tablename, policyname, cmd, qual
FROM pg_policies
WHERE tablename = 'events_2024_q4';
You’ll often see zero rows. The parent’s policy doesn’t appear in pg_policies for the child. This creates a false negative in any security audit or automated compliance check that queries pg_policy per-table. The partition looks unprotected — because for direct child-table access, it effectively is.
Partition pruning: the performance you can’t afford to lose
The entire reason you partitioned was pruning. If your RLS policy predicate doesn’t include tenant_id in a form the planner can use for constraint exclusion, you lose the benefit:
| Predicate Form | Partition Pruning | Notes |
|---|---|---|
tenant_id = current_setting(...)::uuid | Yes (planning time) | Preferred; planner evaluates at parse |
tenant_id = get_tenant_id() (stable fn) | Yes (execution time) | Works, slightly later |
tenant_id = ANY(get_tenant_ids()) | Partial | Only if list is static |
| Complex subquery join | No | Planner cannot prune |
Keep your USING clause simple and type-consistent. A cast mismatch between uuid and text will kill pruning silently and produce sequential scans at scale.
The safe pattern: policy per partition, automated verification
Treat each partition as a first-class security boundary:
-- After attaching a new partition:
ALTER TABLE events_2024_q4 ENABLE ROW LEVEL SECURITY;
ALTER TABLE events_2024_q4 FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON events_2024_q4
USING (tenant_id = current_setting('app.current_tenant')::uuid);
Then enforce this in your partition management automation. A CI check or a migration hook that queries pg_policies for every child table and fails the deploy if any partition is unprotected. Don’t treat it as optional.
A production-ready verification query
SELECT
c.relname AS partition,
rls.rowsecurity,
COUNT(p.policyname) AS policy_count
FROM pg_inherits i
JOIN pg_class c ON c.oid = i.inhrelid
JOIN pg_class r ON r.oid = i.inhparent AND r.relname = 'events'
JOIN pg_tables rls ON rls.tablename = c.relname
LEFT JOIN pg_policies p ON p.tablename = c.relname
GROUP BY c.relname, rls.rowsecurity
HAVING COUNT(p.policyname) = 0 OR rls.rowsecurity = false;
If this query returns any rows in production, you have an unprotected partition.
Three things worth repeating
-
Never rely on parent-only policy definitions. Create and verify RLS policies on every child partition at attach time. Automate it — make it a required step in your partition management workflow, not an afterthought.
-
Query
pg_policiesper child, not per parent. Build a nightly or deploy-gate audit that flags any partition with zero policies orrowsecurity = false. Treat a failed audit as a P0 incident. -
Keep your USING predicate simple and type-consistent. Partition pruning is the performance contract you signed when you chose this architecture. A cast mismatch or a complex subquery in the RLS policy quietly voids it at scale.