Sub topic 4
USING and WITH CHECK: filtering versus validating rows
Prerequisites
This chapter assumes that you know basic SQL, PostgreSQL roles, table privileges, and WHERE clauses. You should also know that a GRANT controls whether a role may use a table at all. Row-level security, or RLS, controls which rows that role may use after it has table privileges.
A policy has two different jobs:
- USING decides which existing rows a role may see or change.
- WITH CHECK decides whether a new row version is allowed.
The distinction matters most for INSERT and UPDATE.
USING filters existing rows
USING is a Boolean expression. PostgreSQL evaluates it against rows that already exist.
For a SELECT, rows for which the expression is false are hidden. For an UPDATE or DELETE, rows that fail the expression cannot be selected for modification. A role might have UPDATE privilege on a table but still be unable to update rows hidden by USING.
Consider a table where each row has an owner:
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);The expression compares the row's ownername with currentuser, which is the name of the role running the statement. A role named alice can see and modify only rows owned by alice.
WITH CHECK validates new row versions
WITH CHECK is evaluated against data that an INSERT or UPDATE would create.
For an INSERT, there is no existing row to filter. PostgreSQL therefore uses WITH CHECK to decide whether the proposed row may be added. For an UPDATE, PostgreSQL first uses USING to find an existing row, then uses WITH CHECK to validate the updated version.
This prevents a user from taking an allowed row and changing its owner:
-- The role alice may insert a row with its own owner name.
INSERT INTO documents (title, owner_name)
VALUES ('Private notes', 'alice');
-- This must fail under the documents_owner policy.
UPDATE documents
SET owner_name = 'bob'
WHERE owner_name = 'alice';The first statement passes the check for alice. The second statement may find alice's row through USING, but its new version fails WITH CHECK.
| Command | Existing rows tested by | New row values tested by |
|---|---|---|
| SELECT | USING | Not applicable |
| INSERT | Not applicable | WITH CHECK |
| UPDATE | USING | WITH CHECK |
| DELETE | USING | Not applicable |
If a policy has USING but no WITH CHECK, PostgreSQL uses the USING expression as the check for commands that create new row versions. Even so, writing both expressions explicitly makes the policy's intent easier to review.
Separate policies by command
A single FOR ALL policy is convenient when every operation follows the same ownership rule. Separate policies are clearer when reads and writes have different requirements:
CREATE POLICY documents_select
ON documents
FOR SELECT
TO alice, bob
USING (owner_name = current_user);
CREATE POLICY documents_insert
ON documents
FOR INSERT
TO alice, bob
WITH CHECK (owner_name = current_user);These policies do not grant table privileges. Alice and bob still need SELECT or INSERT privileges through GRANT. Also remember that permissive policies for the same command normally combine with OR. A row allowed by any applicable permissive policy is allowed, unless a restrictive policy adds another condition.
The evaluation model
The following model helps predict an UPDATE:
Exercise
Exercise
A policy uses USING (department = current_user) but has no WITH CHECK. What happens when alice updates an allowed row and changes department to bob?
Show answer
The updated row fails the effective check, because PostgreSQL uses the USING expression as the WITH CHECK expression when WITH CHECK is omitted. The update is rejected.