Learn SQL
-- security.row_level_security

Row-Level Security (RLS) in SQL

Database-enforced tenant isolation — survives buggy queries

--concept

PostgreSQL RLS policies (USING for reads, WITH CHECK for writes) enforce per-role row visibility inside the engine — tenant_id = current_setting('app.tenant'). Views with security_barrier plus roles work elsewhere. Tenant-id columns plus policies give multi-tenant isolation that survives every buggy query and leaked connection.

--how it works
1

Enable per table

ALTER TABLE … ENABLE ROW LEVEL SECURITY — no policy means no rows (deny by default).

2

USING gates reads

tenant_id = current_setting(app.tenant) appended invisibly to every read.

3

WITH CHECK gates writes

Inserts/updates cannot smuggle other tenants’ rows.

4

Test per role

Same SELECT, different sessions — policy bugs leak silently without role tests.

--visualization
app.tenant=7 — same SELECT, only tenant_7 rows (policy appended invisibly)
order_idtenant_id
5017
5027
Stage 1 / 1
USING gates reads, WITH CHECK gates writes. Test as each role — leaks are silent.
--example
query.sql
1-- PostgreSQL RLS:
2CREATE POLICY tenant_iso ON orders USING (tenant_id = current_setting('app.tenant')::int);
Result
sessionvisibility
tenant_7 sessionsees tenant_7 rows only
tenant_9 sessionsees tenant_9 rows only

Same SELECT text returns different rows per session setting. A forgotten WHERE tenant_id = … no longer leaks — the policy appends it invisibly and un-bypassably.

--variants

Tenant isolation policy

Database, not app, guarantees separation.

query.sql
1CREATE POLICY tenant_iso ON orders USING (tenant_id = current_setting('app.tenant')::int);
Result
sessionvisibility
tenant_7own rows only
tenant_9own rows only
1App sets app.tenant per request.
2Policy appends the predicate un-bypassably.
3Forgotten WHERE no longer leaks.

Write guard twin

Cannot insert sideways.

query.sql
1CREATE POLICY tenant_write ON orders FOR INSERT WITH CHECK (tenant_id = current_setting('app.tenant')::int);
Result
writeverdict
own tenant insertallowed
foreign insertrejected
1USING covers SELECT/UPDATE/DELETE visibility.
2WITH CHECK covers INSERT/UPDATE new-row values.
3Both needed for full isolation.
--key facts
USINGread visibility gate
WITH CHECKwrite gate — cannot smuggle rows
Test per rolepolicy bugs leak silently
--use cases
SaaS multi-tenancy without schema fan-out
Support tooling with per-customer visibility
--under the hood
RLS predicates plan into every query — index tenant_id or all reads degrade.
security_barrier views prevent optimizer from leaking via functions on other tenants’ rows.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.