← Back to PostgreSQL, page 4

postgresql-0068 · Hard senior

Implement tenant isolation with row-level security driven by a session setting, and avoid the table-owner bypass.

ALTER TABLE ... ENABLE ROW LEVEL SECURITY plus a policy USING (tenant_id = current_setting('app.tenant_id')::int) makes every query see only the current tenant's rows, and WITH CHECK stops writes into other tenants. Table owners and superusers bypass RLS unless FORCE ROW LEVEL SECURITY is set. The application sets the tenant per transaction with SET LOCAL, which must happen on every pooled connection.

Full answer

Test data:

sql
CREATE TABLE invoices (id int, tenant_id int, amount int);
INSERT INTO invoices VALUES (1, 10, 500), (2, 10, 700), (3, 20, 900);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
  USING (tenant_id = current_setting('app.tenant_id', true)::int)
  WITH CHECK (tenant_id = current_setting('app.tenant_id', true)::int);
-- a non-superuser application role that owns the table (superusers always bypass RLS)
DO $$ BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'ibank_app') THEN CREATE ROLE ibank_app NOLOGIN; END IF;
  EXECUTE format('GRANT USAGE ON SCHEMA %I TO ibank_app', current_schema());
END $$;
ALTER TABLE invoices OWNER TO ibank_app;
sql
SET ROLE ibank_app; SELECT count(*) AS visible_as_owner FROM invoices;

Result:

text
 visible_as_owner
------------------
 3
(1 row)
sql
ALTER TABLE invoices FORCE ROW LEVEL SECURITY; SELECT count(*) AS visible_without_tenant FROM invoices;

Output:

text
 visible_without_tenant
------------------------
 0
(1 row)
sql
BEGIN; SET LOCAL app.tenant_id = '10'; SELECT id, amount FROM invoices ORDER BY id;

PostgreSQL returns:

text
 id | amount
----+--------
 1  | 500
 2  | 700
(2 rows)
sql
INSERT INTO invoices VALUES (4, 20, 100);

Result on PostgreSQL 17:

text
ERROR:  new row violates row-level security policy for table "invoices"
sql
ROLLBACK; BEGIN; SET LOCAL app.tenant_id = '20'; UPDATE invoices SET amount = amount + 1 RETURNING id, tenant_id, amount;

Result:

text
 id | tenant_id | amount
----+-----------+--------
 3  | 20        | 901
(1 row)
sql
COMMIT; RESET ROLE;

Output:

text
RESET

The demo switches to ibank_app, an ordinary role that owns the table, because superusers bypass RLS in every case. Even with RLS enabled, the table owner still sees all three rows, a common surprise in development where the app connects as the owner. After FORCE ROW LEVEL SECURITY, a session without a tenant setting sees nothing, because current_setting(..., true) returns NULL and the policy is never true. With tenant 10 set locally, only invoices 1 and 2 are visible, and inserting a row for tenant 20 fails with 'new row violates row-level security policy'. The UPDATE as tenant 20 touches only invoice 3, though it has no WHERE clause. Use a separate non-owner role for the application, SET LOCAL inside each transaction so pooled connections do not leak tenants, and index tenant_id.

sql
CREATE TABLE invoices (id int, tenant_id int, amount int);
INSERT INTO invoices VALUES (1, 10, 500), (2, 10, 700), (3, 20, 900);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY tenant_isolation ON invoices
  USING (tenant_id = current_setting('app.tenant_id', true)::int)
  WITH CHECK (tenant_id = current_setting('app.tenant_id', true)::int);
-- a non-superuser application role that owns the table (superusers always bypass RLS)
DO $$ BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'ibank_app') THEN CREATE ROLE ibank_app NOLOGIN; END IF;
  EXECUTE format('GRANT USAGE ON SCHEMA %I TO ibank_app', current_schema());
END $$;
ALTER TABLE invoices OWNER TO ibank_app;

SET ROLE ibank_app; SELECT count(*) AS visible_as_owner FROM invoices;

ALTER TABLE invoices FORCE ROW LEVEL SECURITY; SELECT count(*) AS visible_without_tenant FROM invoices;

BEGIN; SET LOCAL app.tenant_id = '10'; SELECT id, amount FROM invoices ORDER BY id;

INSERT INTO invoices VALUES (4, 20, 100);

ROLLBACK; BEGIN; SET LOCAL app.tenant_id = '20'; UPDATE invoices SET amount = amount + 1 RETURNING id, tenant_id, amount;

COMMIT; RESET ROLE;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Policies filter rows for every query
  • WITH CHECK guards writes
  • Owners bypass RLS unless FORCE is set
  • SET LOCAL per transaction with pooling
  • Missing setting means no rows

Common mistakes

  • Testing RLS as the table owner
  • Using session-level SET with transaction pooling

Follow-ups

  • How does RLS affect query plans and performance?
  • How would you give support staff cross-tenant read access?

Tips

  • Show the owner bypass