Sub topic 12
Views and RLS: `security_invoker`
This chapter assumes you know PostgreSQL roles, GRANT, views, and basic row-level security (RLS). RLS is a table feature that filters or validates rows according to the role making a query.
A view is a stored query that presents selected columns or rows. Views can simplify an API, but they can also change which role PostgreSQL uses when it checks access to the underlying table.
The default view behavior
By default, a view runs with the privileges of its owner. PostgreSQL checks access to the tables referenced by the view as though the owner queried them. This behavior is called a security-definer view.
If the view owner owns the table, has the BYPASSRLS attribute, or is a superuser, the view may not apply the RLS rules you expected. Granting a user access to the view does not automatically make the user's RLS policy control the view query.
The following example creates a view owned by the role that runs the CREATE VIEW statement:
CREATE VIEW my_documents AS
SELECT id, title
FROM documents;
GRANT SELECT ON my_documents TO alice;If the owner bypasses RLS, Alice might see rows that she could not select directly from documents.
Invoker security
PostgreSQL 15 introduced the security_invoker view option. It makes the view use the privileges and policies of the querying role for its underlying tables.
CREATE VIEW my_documents
WITH (security_invoker = true)
AS
SELECT id, title
FROM documents;
GRANT SELECT ON my_documents TO alice, bob;With this option, a query such as:
SET ROLE alice;
SELECT id, title
FROM my_documents;
RESET ROLE;checks Alice's privileges and the RLS policies that apply to Alice on documents. If the policy says that Alice can see only rows where owner_name = current_user, the view returns only those rows.
security_invoker does not grant permissions. Alice still needs permission on the view and, for an invoker-security view, the required permissions on the underlying table. RLS then applies as an additional row filter.
| View option | Access checks use | Typical RLS result |
|---|---|---|
| Default | View owner | Owner's policies or RLS bypass may apply |
| `security_invoker = true` | Querying role | Querying role's privileges and policies apply |
How the checks flow
The important distinction is which identity reaches the base table.
Common mistakes
- Granting
SELECTon a default view and assuming the caller's RLS policy will filter rows. - Giving the view owner
BYPASSRLSwithout checking which views that role owns. - Using
security_invokerbut forgetting to grant the querying role access to the base table. - Testing only as the table owner. Test as the actual application roles instead.
- Exposing sensitive columns through a view even though its row filtering is correct.
For a view over several tables, invoker security affects the underlying relations used by the view. Review permissions and RLS policies on every relation, not only the main table.
Exercise
Alice can query a default view owned by a role that bypasses RLS. Will Alice's direct-table RLS policy necessarily filter the view result?
Show answer
No. The default view uses the view owner's access context. Define the view with security_invoker = true and test that Alice has the required privileges and receives the intended policy-filtered rows.