Sub topic 9
Passing the Current User or Tenant into a Policy
Prerequisites
Assume you know basic SQL, PostgreSQL tables and indexes, roles, GRANT, and WHERE clauses. You should also know that row-level security (RLS) policies run inside the database for every statement.
A policy needs a value to compare with each row. That value might be the logged-in database role, or it might be a tenant identifier supplied by the application.
Using the Database User
PostgreSQL provides current_user, which returns the role currently being used for authorization. A policy can compare it with an owner column:
CREATE TABLE documents (
id bigserial PRIMARY KEY,
owner_name text NOT NULL DEFAULT current_user,
title text NOT NULL,
body text
);
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
CREATE POLICY documents_owner ON documents
FOR ALL
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);USING filters existing rows. A user can select, update, or delete only documents whose owner_name matches the current role. WITH CHECK validates rows created or changed by an operation.
The default makes inserts convenient, but the policy still validates the value. A role cannot set owner_name to another user merely by supplying a different value.
Passing a Tenant Through Session Context
In a shared database, many application requests may use the same database role. In that case, current_user does not identify the tenant. PostgreSQL custom settings can carry request context:
CREATE TABLE invoices (
id bigserial PRIMARY KEY,
tenant_id uuid NOT NULL,
amount_cents bigint NOT NULL,
archived boolean NOT NULL DEFAULT false
);
CREATE INDEX invoices_tenant_id_idx ON invoices (tenant_id);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
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
);current_setting reads a session setting. The second argument, true, means that an unset setting returns NULL instead of raising an error. Since NULL matches no tenant_id, a request that forgets to set its tenant sees no rows and cannot insert rows accidentally.
The application must set the tenant before querying:
BEGIN;
SELECT set_config(
'app.tenant_id',
'3f2b8c1e-5a4d-4e0b-9f6a-1c2d3e4f5a6b',
true
);
SELECT id, amount_cents
FROM invoices;
COMMIT;The third argument to set_config is is_local. When it is true, the value lasts only for the current transaction. This matters when a connection pool reuses one database connection for different users.
Choosing the Context Source
Use current_user when PostgreSQL roles represent the users or services that own rows. Use a custom setting when one database role serves multiple tenants. In both designs, apply the value in both USING and WITH CHECK; otherwise users may read safely but create or update rows outside their permitted scope.
Exercise
Why should a tenant policy use set_config(..., true) instead of leaving the setting at session scope when connections are pooled?
Show answer
The transaction-local value is cleared at commit or rollback, so the next request cannot inherit the previous request's tenant identifier.