Sub topic 12
Views and RLS: security_invoker
This chapter assumes you have enabled RLS on a table, written USING and WITH CHECK policies, and know that table owners and BYPASSRLS roles skip policies unless FORCE is set. It uses the documents table, with its documents_owner policy, and the alice and bob roles from earlier chapters.
Why views are a gap in RLS
A view is a stored query. When a role queries a view, PostgreSQL checks access to the underlying tables as the view owner, not as the role running the query. This is useful for hiding columns. It also changes how RLS applies.
By default, the policies on the underlying table are applied as if the view owner were querying. Two cases follow:
- If the view owner is a superuser or has
BYPASSRLS, no policy applies to anyone who reads through the view. - If the view owner also owns the table, and the table does not use
FORCE ROW LEVEL SECURITY, no policy applies either.
The second case is the common one. A developer creates a table, enables RLS, then creates a convenience view, all as the same role. Every caller of that view sees every row.
Demonstrating the leak
Run this as the owner of documents, which is the role that created it. The role must not be alice or bob.
INSERT INTO documents (title, owner_name)
VALUES ('Alice notes', 'alice'),
('Bob todo', 'bob');
CREATE VIEW all_titles AS
SELECT id, title FROM documents;
GRANT SELECT ON all_titles TO alice, bob;
SET ROLE bob;
SELECT title FROM all_titles;
-- Alice notes
-- Bob todo
RESET ROLE;The insert works without a policy violation because the owner bypasses RLS. Bob then reads Alice's row, even though SELECT title FROM documents run directly as Bob would return only his own row.
Note that current_user inside a view still returns the calling role. The view does not change it, unlike a SECURITY DEFINER function. What changes is whose privileges, and therefore whose policies, are used for the table access. The policy expression owner_name = current_user is never evaluated here, because the owner bypasses the policy.
Fixing it with security_invoker
PostgreSQL 15 added the security_invoker view option. When it is true, the underlying tables are accessed with the privileges of the role that queries the view, and that role's policies apply.
ALTER VIEW all_titles SET (security_invoker = true);
SET ROLE bob;
SELECT title FROM all_titles;
-- Bob todo
RESET ROLE;You can also set the option when creating the view:
CREATE VIEW my_documents
WITH (security_invoker = true)
AS SELECT id, title FROM documents;The table below compares the two modes.
| Aspect | Default (`security_invoker = false`) | `security_invoker = true` |
|---|---|---|
| Privileges checked on base tables | View owner | Role running the query |
| RLS policies applied | Those of the view owner | Those of the role running the query |
| Caller needs `SELECT` on the base table | No | Yes |
| Safe when the owner is a superuser or table owner | No, policies are skipped | Yes, policies still apply to the caller |
Privileges still matter
With security_invoker = true, the caller needs table-level privileges on the base table in addition to SELECT on the view. Without them, the query fails with a permission error before RLS is considered:
CREATE ROLE carol;
GRANT SELECT ON all_titles TO carol;
SET ROLE carol;
SELECT title FROM all_titles;
-- ERROR: permission denied for table documents
RESET ROLE;This is the trade-off. A default view lets you expose a subset of columns from a table that callers cannot read at all. A security_invoker view cannot do that. For column hiding, grant column-level SELECT on the base table, or keep the default view and filter rows explicitly, as described next.
Checking which mode a view uses
The option is stored in pg_class.reloptions:
SELECT relname, reloptions
FROM pg_class
WHERE relkind = 'v'
AND relname IN ('all_titles', 'my_documents');A view with the option shows {security_invoker=true}. A view without it shows NULL in reloptions. Add this query to your audit checks so that any new view over an RLS-protected table is reviewed.
Writes through views
Simple views are automatically updatable. With security_invoker = true, an INSERT, UPDATE, or DELETE through the view is checked against the caller's policies, including WITH CHECK:
SET ROLE bob;
INSERT INTO my_documents (title) VALUES ('Bob draft'); -- allowed
UPDATE my_documents SET title = 'Renamed'; -- only Bob's rows
RESET ROLE;The my_documents view omits owner_name, so the insert uses the column default current_user, which is bob. The write passes the policy.
WITH CHECK OPTION on a view is a separate feature. It makes sure rows written through the view satisfy the view's own WHERE clause. It does not replace RLS and does not add policy checks.
When you cannot use security_invoker
Sometimes you need the default behavior, for example to expose a few columns to roles that have no access to the base table. Then you must do the filtering yourself:
CREATE VIEW my_titles
WITH (security_barrier = true)
AS SELECT id, title
FROM documents
WHERE owner_name = current_user;
GRANT SELECT ON my_titles TO alice, bob;Here the WHERE clause does the work that the policy would do. The security_barrier option stops the planner from pushing a caller's function into the table scan ahead of your filter. Without it, a function with side effects, such as one that raises a notice containing its argument, could see rows your WHERE clause was supposed to remove.
On PostgreSQL 14 and earlier, security_invoker does not exist, and this pattern is the only view-based option. The view owner's rights always apply.
Common mistakes
- Assuming policies follow the table. A view over an RLS-protected table is not protected by default. Check every view that references such a table.
- Setting the option on only one layer. If view A reads from view B, which reads from the table, each view applies its own owner or invoker rule. Setting
security_invokeron A alone does not change how B reads the table. Set it on each view in the chain. - Forgetting base-table grants. After you switch a view to
security_invoker = true, callers who relied on the view alone getpermission denied. Grant the base-table privileges the policies need, and keep the policies as the row filter. - Relying on
FORCEas a view fix.FORCE ROW LEVEL SECURITYmakes the table owner subject to policies, which closes the owner-owns-view hole. It does not help when the view owner is a superuser or hasBYPASSRLS.security_invokerhandles that case. - Assuming
CREATE OR REPLACE VIEWkeeps the old option. Always restate the options you want when you recreate a view, or verify them afterwards withpg_class.reloptions.
Decision rule
- If the view only reshapes columns or joins tables for convenience, create it with
security_invoker = trueand rely on the table's policies. - If callers must not have direct access to the base table, use a default view with an explicit
WHEREclause andsecurity_barrier = true. Own the view with a role that is neither a superuser norBYPASSRLS. - For every view over an RLS table, test as a non-owner role with
SET ROLEand compare the rows returned by the view with the rows returned by the table.
Exercise
A developer creates the table invoices and enables RLS on it, without FORCE. As the same role, they create CREATE VIEW invoice_totals AS SELECT tenant_id, sum(amount_cents) AS total FROM invoices GROUP BY tenant_id; and grant SELECT on the view to the role app_user. When app_user queries the view, it sees totals for every tenant. Explain why, and give the one-line change that makes the view respect app_user's policies. What new requirement does that change add?
Show answer
The view owner is also the table owner, and without FORCE the owner bypasses RLS. By default the view reads the table with the owner's rights, so no policy applies to app_user. Run ALTER VIEW invoice_totals SET (security_invoker = true);. After that, the table is read with app_user's rights and policies, so app_user also needs SELECT privilege on invoices itself.
Exercise
You want a role reporter, which has no privileges on documents, to read only its own rows through a view on PostgreSQL 15. Can you use security_invoker = true for this? If not, what do you write instead?
Show answer
No. With security_invoker = true, reporter needs SELECT on documents, which it lacks, so the query fails with permission denied. Use a default view with an explicit filter, owned by a role that does not bypass RLS, for example CREATE VIEW reporter_docs WITH (security_barrier = true) AS SELECT id, title FROM documents WHERE owner_name = current_user;, and grant SELECT on the view to reporter.