skip to content

In PostgreSQL, how are several row-level security policies on one table combined when some are PERMISSIVE and others are RESTRICTIVE?

level: middleimportance: should knowfreq 45%

answer

  1. OR inside one kind, AND across
  2. one door must open first
  3. ALL counts as every command
  4. UPDATE also needs SELECT policies

basics

~20 s

Applicable PERMISSIVE policies are OR-ed, applicable RESTRICTIVE policies are AND-ed, and the two results are AND-ed. With no permissive policy for that command, access is denied, so restrictive policies can only narrow what a permissive one grants.

solid answer

~40 s

A policy applies when its `TO` roles include the current role and its `FOR` command matches. An `ALL` policy counts as whichever command type is being evaluated. Within one command type, a row passes if **any** permissive policy passes **and every** restrictive policy passes. If no permissive policy applies, nothing passes. Policies are permissive unless created `AS RESTRICTIVE`. When one statement needs two kinds of access, the kinds are AND-ed. For example, an `UPDATE` whose `WHERE` or `RETURNING` reads columns must pass both the `SELECT`/`ALL` policies and the `UPDATE`/`ALL` policies. A common layout makes tenant isolation **restrictive** and feature access **permissive**. Then a later permissive policy, such as guest read access to shared projects, can never reach across tenants. `pg_policies.permissive` shows each policy's mode.

code

sql · 13 lines
sql
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;

CREATE POLICY org_isolation ON projects AS RESTRICTIVE
  USING (org_id = NULLIF(current_setting('app.org_id', true), '')::uuid)
  WITH CHECK (org_id = NULLIF(current_setting('app.org_id', true), '')::uuid);

CREATE POLICY member_access ON projects
  TO app_member
  USING (true);

CREATE POLICY guest_read ON projects
  FOR SELECT TO app_guest
  USING (shared_with_guests);

go deeper

for a junior

Remember that permissive policies are OR-ed, restrictive ones AND-ed, and that permissive is the default mode.

for a middle

Explain why restrictive-only tables show nothing, how ALL policies join each command type, and why an UPDATE also needs SELECT policies.

for a senior

Lay out policies so tenant isolation is restrictive and features are permissive, and predict a role's access by reading pg_policies as one formula.

for a principal

Set a policy-authoring convention for the team so that adding a feature policy can never widen tenant boundaries, and review it as part of schema changes.

## Which policies apply A table can carry many named policies. For each statement, PostgreSQL first narrows them to the **applicable** ones: - **Role:** the policy's `TO` list must include the current role, directly or through inherited membership. `PUBLIC`, the default, matches everyone. - **Command:** the policy's `FOR` clause (`SELECT`, `INSERT`, `UPDATE`, `DELETE` or `ALL`) must match the kind of access being checked. `ALL` is the default. - **`ALL` policies** count as whichever command type is being evaluated. A `FOR ALL` policy and a `FOR SELECT` policy are therefore both SELECT policies when rows are read, and both apply. - **Mode:** `AS PERMISSIVE`, the default, or `AS RESTRICTIVE`. ## The combination rule Within one command type, the applicable policies combine like this: ```sql -- per command type (restrictive_1 AND restrictive_2 AND ...) AND (permissive_1 OR permissive_2 OR ...) ``` Three consequences follow: 1. **Permissive policies add access.** Each new one widens what the role sees. 2. **Restrictive policies remove access.** Every one must pass, so each new one narrows the set. 3. **No permissive policy means no access.** The OR of an empty list is false, so a table with only restrictive policies for a command is in default deny for it. ## Statements that need more than one kind of access If a statement needs two kinds of access, both groups must pass, and the groups are AND-ed. The manual's per-command rules for a multi-tenant project-management `tasks` table: | Statement | Existing rows | New rows | |---|---|---| | `SELECT` | filtered by SELECT/ALL `USING` | none | | `INSERT` | none | checked by INSERT/ALL `WITH CHECK` (error) | | `UPDATE ... WHERE ...` | filtered by UPDATE/ALL `USING` **and** SELECT/ALL `USING` | checked by UPDATE/ALL `WITH CHECK` (error) | | `DELETE ... WHERE ...` | filtered by DELETE/ALL `USING` **and** SELECT/ALL `USING` | none | | any write with `RETURNING` | as above | must also pass SELECT policies, or the statement errors | The SELECT policies join in whenever the statement reads columns of the table: a `WHERE`, a `RETURNING`, or a `SET` expression that uses a column. That is why a role with a perfect `UPDATE` policy and no `SELECT` policy updates nothing. One exception changes filtering into an error. With `INSERT ... ON CONFLICT DO UPDATE`, if the existing row fails the `UPDATE` policies' `USING`, the statement errors instead of skipping it. The manual says that path is "never silently avoided". The same holds for a `MERGE` update action. ## A tenant-safe layout The combination rule suggests a structure that holds up as policies accumulate: 1. Put **tenant isolation** in one `RESTRICTIVE` policy with no `TO` list, so it binds every role. 2. Express **features** (member access, guest read access, archived projects read-only) as `PERMISSIVE` policies scoped by role and command. 3. When someone adds a permissive policy next year, it is still AND-ed with isolation and cannot cross tenants. If isolation were permissive instead, a new `guest_read` policy with `USING (shared_with_guests)` would be OR-ed with it, and guests would see shared projects of **every** organization. ## Mistakes the rule exposes - Writing only restrictive policies and wondering why nothing is visible. - Expecting two permissive policies to both be required. They are alternatives. - Forgetting that a `FOR ALL` policy keeps applying next to command-specific ones. - Granting an `UPDATE` policy without a `SELECT` policy, then seeing `UPDATE 0`. ## Inspecting and predicting The `pg_policies` view lists `policyname`, `permissive` (`PERMISSIVE` or `RESTRICTIVE`), `roles`, `cmd`, `qual` and `with_check` for each table. Reading it as the formula above is the quickest way to predict what a role sees. Take the `projects` policies in the example below: - **A guest's `SELECT`:** `org_isolation` (restrictive, all roles, `ALL`) AND `guest_read` (permissive, `app_guest`, `SELECT`). `member_access` is not applicable because its `TO` list names only `app_member`. Result: the shared projects of the guest's own organization. - **A guest's `UPDATE`:** no permissive `UPDATE` or `ALL` policy applies to `app_guest`, so the permissive side is empty and the update touches nothing, whatever `org_isolation` says. - **A member's `INSERT`:** `member_access` is an `ALL` policy whose `USING (true)` doubles as its check, AND-ed with the restrictive `WITH CHECK`. A row for another organization fails with the row-level security error.

  • If org_isolation above were PERMISSIVE, what would guest_read let a guest see?
    Every shared project in every organization. Permissive policies are OR-ed, so passing `guest_read` alone admits a row, whatever its `org_id`. Making isolation `RESTRICTIVE` keeps it AND-ed with every permissive policy added later.
  • Why can INSERT ... ON CONFLICT DO UPDATE raise an error where a plain UPDATE would silently skip the row?
    For the auxiliary update, PostgreSQL checks the existing row against the `UPDATE` policies' `USING` expressions and raises an error if it fails instead of filtering it. The manual says that path is never silently avoided. Rows proposed for insertion are also checked against the `INSERT` `WITH CHECK` whether or not a conflict occurs.

Permissive policies are entrance doors and restrictive policies are checkpoints inside. Any one door lets you in, every checkpoint must wave you through, and with no door at all you never reach the checkpoints.

  • Restrictive policies on their own are enough to grant access
  • Two permissive policies on a table must both pass for a row
  • A FOR ALL policy stops applying once a FOR SELECT policy exists
  • An UPDATE needs only an UPDATE policy, whatever its WHERE reads
  • Policies are restrictive unless created AS PERMISSIVE