Sub topic 7
Combining permissive and restrictive policies
This chapter assumes you can write a basic CREATE POLICY statement, you know the difference between USING and WITH CHECK, and you have used SET ROLE to test as another role. It covers how PostgreSQL combines several policies on one table, and how to choose between the two policy types.
Two kinds of policy, two combining rules
Every policy is either permissive or restrictive. Permissive is the default. You get a restrictive policy by writing AS RESTRICTIVE.
- Permissive policies are combined with OR. A row is allowed if at least one applicable permissive policy accepts it.
- Restrictive policies are combined with AND. A row is allowed only if every applicable restrictive policy accepts it.
The two groups are then joined with AND:
(permissive_1 OR permissive_2 OR ...) AND restrictive_1 AND restrictive_2 AND ...Permissive policies grant access and restrictive policies narrow it. Adding a permissive policy can only widen what a role sees. Adding a restrictive policy can only shrink it.
| Property | Permissive | Restrictive |
|---|---|---|
| How to declare it | Default, or `AS PERMISSIVE` | `AS RESTRICTIVE` |
| Combined with others of its kind by | OR | AND |
| Effect of adding one | Can allow more rows | Can allow fewer rows |
| Can grant access on its own | Yes | No |
Restrictive policies cannot grant access
Row-level security denies by default. A row is only visible if some permissive policy accepts it, and restrictive policies are checked after that. If a table has RLS enabled and only restrictive policies, no permissive policy can accept any row, so no row is ever allowed.
This is a common mistake. You add a "hide archived rows" restrictive policy to a table that has no other policies, and the table becomes empty for every ordinary role. Always make sure at least one permissive policy covers the same command and the same roles.
CREATE TABLE scratch (id int, hidden boolean NOT NULL DEFAULT false);
ALTER TABLE scratch ENABLE ROW LEVEL SECURITY;
CREATE POLICY not_hidden ON scratch
AS RESTRICTIVE
USING (NOT hidden);
-- A non-owner role with SELECT privilege sees zero rows,
-- even for rows where hidden is false.The evaluation order
The diagram shows the decision for one candidate row and one command.
Two details affect the result:
- Only policies that apply to the current command and the current role are included. A policy declared
FOR SELECTplays no part in anINSERT, and a policy declaredTO annplays no part forben. If the command has no applicable permissive policy for that role, the result is denial. USINGexpressions andWITH CHECKexpressions are combined separately. For writes, the permissiveWITH CHECKexpressions are ORed and the restrictive ones are ANDed, in the same way as forUSING.
A worked example
This script builds a notes table with three policies:
- Owners can do anything with their own notes (permissive).
- Anyone can read notes marked shared (permissive).
- Nobody sees archived notes when reading (restrictive).
The role names ann and ben are new here, so the script does not collide with roles from earlier chapters. Run it as a superuser or as the database owner.
CREATE TABLE notes (
id bigserial PRIMARY KEY,
owner_name text NOT NULL DEFAULT current_user,
shared boolean NOT NULL DEFAULT false,
archived boolean NOT NULL DEFAULT false,
body text NOT NULL
);
ALTER TABLE notes ENABLE ROW LEVEL SECURITY;
CREATE POLICY notes_owner ON notes
FOR ALL
USING (owner_name = current_user)
WITH CHECK (owner_name = current_user);
CREATE POLICY notes_shared ON notes
FOR SELECT
USING (shared);
CREATE POLICY notes_not_archived ON notes
AS RESTRICTIVE
FOR SELECT
USING (NOT archived);
CREATE ROLE ann;
CREATE ROLE ben;
GRANT SELECT, INSERT, UPDATE, DELETE ON notes TO ann, ben;
GRANT USAGE ON SEQUENCE notes_id_seq TO ann, ben;
SET ROLE ann;
INSERT INTO notes (body, shared, archived) VALUES
('ann private', false, false),
('ann shared', true, false),
('ann old', false, true);
SELECT body FROM notes ORDER BY id;
-- ann private
-- ann shared
SET ROLE ben;
INSERT INTO notes (body) VALUES ('ben private');
SELECT body FROM notes ORDER BY id;
-- ann shared
-- ben private
RESET ROLE;For ann, the SELECT evaluates (owner_name = current_user OR shared) AND NOT archived:
ann privatepasses through the owner policy.ann sharedpasses through either permissive policy.ann oldis owned byann, but the restrictive policy rejects it.
For ben, the owner policy accepts only ben private. The shared policy adds ann shared. ann private and ann old fail every permissive policy.
The INSERT of ann old succeeds even though the row is archived. The restrictive policy is declared FOR SELECT, so it does not apply to INSERT. Only the owner policy's WITH CHECK is evaluated there.
Restrictive policies and the command they cover
The FOR clause on a restrictive policy decides which operations it limits. You have three main choices:
FOR SELECThides rows from reads. This is the version in the example.FOR ALLwith onlyUSINGalso applies that expression as theWITH CHECKfor writes. WithNOT archived, this stops a role from inserting an archived row or updating a row so that it becomes archived.FOR INSERT,FOR UPDATE, orFOR DELETElimit a single operation.
A SELECT-only restrictive policy also reaches some write commands. An UPDATE or DELETE that has a WHERE clause reading column values, or a RETURNING clause, needs to read the rows, so the SELECT policies apply to it as well. In the example, ann cannot run UPDATE notes SET archived = false WHERE id = 3 to restore her archived note, because the row is invisible to the WHERE clause. If you want such an operation, give a privileged role a path to it. That could be a role that bypasses RLS, or a separate SECURITY DEFINER function.
Choosing between them
Pick the type based on what the rule means. Use a permissive policy when the rule describes one way to get access, such as "owners", "members of the team", or "rows marked shared". Several of these OR together naturally. Use a restrictive policy when the rule is a condition that must always hold, whichever grant gave access.
Typical restrictive rules include:
- Tenant isolation as a safety net on top of other grants.
- Hiding soft-deleted or archived rows.
- Requiring that a role may only see rows below a classification level.
Writing the same rule as a permissive policy is a bug waiting to happen. Suppose "tenant matches" is permissive and "is an admin" is permissive. Their OR gives every admin the rows of every tenant. Making "tenant matches" restrictive gives (admin OR owner) AND tenant matches, which is the intent.
The cost is that the rule is less obvious from reading one policy. Use pg_policies to see the whole set together:
SELECT policyname, permissive, roles, cmd, qual, with_check
FROM pg_policies
WHERE tablename = 'notes'
ORDER BY policyname;The permissive column shows PERMISSIVE or RESTRICTIVE. Read cmd and roles as well, because they decide which policies join the combination for a given session.
Common errors
- Only restrictive policies exist. The result is an empty table. Add a permissive policy.
- Adding a permissive policy to "tighten" access. Permissive policies only widen. To tighten, use a restrictive policy or edit the existing ones.
- Forgetting that
FOR ALLrestrictive policies also check writes. An expression meant to hide rows can unexpectedly reject inserts and updates. Declare itFOR SELECTif you only mean reads. - Mismatched roles. A restrictive policy
TO anndoes not constrainben. Ifbenholds another permissive grant, he is unrestricted. OmitTO(the default isPUBLIC) when the rule should apply to everybody. - Mixed-up update behavior. An
UPDATEmust pass theUSINGexpressions for the old row and theWITH CHECKexpressions for the new row. When a policy has noWITH CHECK, itsUSINGexpression is reused for that check.
Exercise
A table has permissive policy P1 with USING (a), permissive policy P2 with USING (b), and restrictive policy R1 with USING (c). All three are FOR SELECT and apply to the current role. Write the condition under which a row is visible, and say whether a row with a = false, b = true, c = false is visible.
Show answer
The condition is (a OR b) AND c. The row has b = true, so the permissive part passes, but c = false makes the restrictive policy reject it. The row is not visible.
Exercise
You enable RLS on a table and create one policy, CREATE POLICY only_active ON items AS RESTRICTIVE USING (active). A non-owner role with SELECT privilege runs SELECT * FROM items. How many rows does it get, and what is the fix?
Show answer
It gets zero rows. With no permissive policy, nothing grants access, so the restrictive policy has no rows to narrow. Fix it by adding a permissive policy for that role and command, for example CREATE POLICY items_read ON items FOR SELECT USING (true), or a narrower rule that expresses who may read. The restrictive policy then removes inactive rows from that set.