Full answer
Schema and sample rows:
sql
CREATE TABLE tickets (id int PRIMARY KEY, team text, title text);
INSERT INTO tickets VALUES (1, 'billing', 'refund'), (2, 'support', 'login');
CREATE VIEW billing_open AS SELECT id, team, title FROM tickets WHERE team = 'billing';
CREATE VIEW billing_checked AS SELECT id, team, title FROM tickets WHERE team = 'billing' WITH CHECK OPTION;sql
INSERT INTO billing_open VALUES (3, 'support', 'sneaked in'); SELECT id, team FROM tickets ORDER BY id;Result on PostgreSQL 17:
text
id | team
----+---------
1 | billing
2 | support
3 | support
(3 rows)sql
INSERT INTO billing_checked VALUES (4, 'support', 'blocked');Result:
text
ERROR: new row violates check option for view "billing_checked"sql
UPDATE billing_checked SET team = 'support' WHERE id = 1;Output:
text
ERROR: new row violates check option for view "billing_checked"sql
UPDATE billing_checked SET title = 'refund request' WHERE id = 1 RETURNING id, title;PostgreSQL returns:
text
id | title
----+----------------
1 | refund request
(1 row)Through the unchecked view, a support ticket was inserted even though the view shows only billing tickets. The checked view rejects both the insert and an update that would move a ticket out of billing, with 'new row violates check option for view'. Updates that keep the row inside the filter work normally. For real access control, combine views with GRANTs on the view and no direct table access, or use row-level security on the table.
sql
CREATE TABLE tickets (id int PRIMARY KEY, team text, title text);
INSERT INTO tickets VALUES (1, 'billing', 'refund'), (2, 'support', 'login');
CREATE VIEW billing_open AS SELECT id, team, title FROM tickets WHERE team = 'billing';
CREATE VIEW billing_checked AS SELECT id, team, title FROM tickets WHERE team = 'billing' WITH CHECK OPTION;
INSERT INTO billing_open VALUES (3, 'support', 'sneaked in'); SELECT id, team FROM tickets ORDER BY id;
INSERT INTO billing_checked VALUES (4, 'support', 'blocked');
UPDATE billing_checked SET team = 'support' WHERE id = 1;
UPDATE billing_checked SET title = 'refund request' WHERE id = 1 RETURNING id, title;Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.
Key points
- Simple views are updatable
- Writes can escape an unchecked view
- WITH CHECK OPTION blocks escaping rows
- Applies to inserts and updates
- Combine with grants or row-level security
Common mistakes
- Relying on a view's WHERE as a security boundary
- Forgetting updates can move rows out of the view
Follow-ups
- How do LOCAL and CASCADED check options behave?
- When would you use security_barrier?
Tips
- Show the escape