Sub topic 7
Combining Permissive and Restrictive Policies
Prerequisites
This chapter assumes that you know basic SQL, PostgreSQL roles and grants, and the difference between a USING expression and a WITH CHECK expression. Row-level security (RLS) must already be familiar: it controls which rows a role can read or change after normal table privileges allow the operation.
A table can have several policies for the same command. PostgreSQL combines those policies according to their type:
- Permissive policies are combined with
OR. A row is allowed when at least one applicable permissive policy allows it. - Restrictive policies are combined with
AND. A row must pass every applicable restrictive policy. - At least one permissive policy must allow the row. A restrictive policy cannot grant access by itself.
- Policies that do not apply to the current command or role are ignored.
This gives you a useful pattern: permissive policies describe who may access a row, while restrictive policies add conditions that everyone must satisfy.
How the combination works
Suppose a document can be accessed by its owner or by a member of a support role. Those are alternative ways to gain access, so they should be permissive policies:
CREATE POLICY documents_owner
ON documents
AS PERMISSIVE
FOR SELECT
TO app_user
USING (owner_name = current_user);
CREATE POLICY documents_support
ON documents
AS PERMISSIVE
FOR SELECT
TO app_user
USING (current_user = 'support_user');The effective condition is:
(owner_name = current_user) OR (current_user = 'support_user')You can then add a restrictive policy that blocks archived documents for everyone:
CREATE POLICY documents_not_archived
ON documents
AS RESTRICTIVE
FOR SELECT
TO app_user
USING (NOT archived);The complete rule is equivalent to:
(owner_name = current_user OR current_user = 'support_user')
AND NOT archivedThe TO clause matters. A policy applies only to the listed roles, or to PUBLIC when no role list is specified. Make sure the intended role has both the required table privilege and an applicable policy.
| Policy type | Multiple policies combine as | Can grant access alone? |
|---|---|---|
| Permissive | `OR` | Yes |
| Restrictive | `AND` | No |
A tenant policy with an additional restriction
A common design uses a permissive policy for tenant membership and a restrictive policy for business rules. This example allows a session to see rows from its tenant, but never archived invoices:
CREATE TABLE invoices (
id bigserial PRIMARY KEY,
tenant_id uuid NOT NULL,
amount_cents bigint NOT NULL,
archived boolean NOT NULL DEFAULT false
);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY invoices_tenant
ON invoices
AS PERMISSIVE
FOR SELECT
TO app_user
USING (
tenant_id =
NULLIF(current_setting('app.tenant_id', true), '')::uuid
);
CREATE POLICY invoices_not_archived
ON invoices
AS RESTRICTIVE
FOR SELECT
TO app_user
USING (NOT archived);An unset tenant setting produces NULL, so the tenant condition matches no rows. A session therefore cannot see invoices until the application sets a valid tenant value.
The restrictive policy applies only to SELECT here. If archived invoices must also be blocked from updates or deletes, create corresponding policies for those commands, or use FOR ALL when the same rule is appropriate for every operation.
Visualizing the rule
Common mistakes
- Adding two permissive policies when you intended both conditions to be required. Use one policy with
AND, or add a restrictive policy. - Applying a restrictive policy only to
SELECTand assuming it also protectsUPDATEorDELETE. - Forgetting that
WITH CHECKcontrols rows created or produced by writes. AUSINGcondition alone may not prevent a user from assigning a row to another tenant. - Testing as a superuser or a role with
BYPASSRLS, which can hide policy mistakes.
Exercise
A table has two applicable permissive policies, department_id = 10 and is_public, plus one restrictive policy, NOT deleted. Which rows can the role read?
Show answer
Rows where (department_id = 10 OR is_public) is true and deleted is false.