Row-level security
Overview
Row-level security (RLS) limits eligible rows in addition to object privileges. PostgreSQL policies use USING to restrict existing rows and WITH CHECK to constrain new row versions; enabling RLS without applicable policies normally denies access. Objective: preserve tenant isolation in a scoped query.
First example
SELECT id,title FROM sec_tenant1 ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1, "O'Brien" ], [ 2, "Guide" ]]How it works
Superusers and BYPASSRLS roles bypass RLS; owners normally bypass it unless FORCE ROW LEVEL SECURITY applies. Derive tenant identity from trusted authentication, not an arbitrary request field. The fixed SQLite tenant view is only a demonstration of filtering; base-table access is not restricted by it. RLS does not replace SQL parameterization or privilege review.
Practical example
SELECT tenant,id,title FROM sec_doc WHERE tenant=2 AND id=1;Observed SQLite result (rows in JSON):
[ [ 2, 1, "Guide" ]]Practice and mistakes
Practice: locate document 1 for tenant 2 only. Hint 1: combine tenant and id. Hint 2: compare to an id-only query. Check: one row, not two. Mistake: testing only as table owner.
PostgreSQL 17 example — NOT RUN
SELECT schemaname,tablename,policyname,cmd FROM pg_policies ORDER BY 1,2,3;Expected result, not executed: "Lists policies, possibly empty; does not verify enforcement."