security_invoker Views Run As Caller
security_invoker views execute with the caller's privileges, so row level security follows the caller instead of being bypassed by the view owner.
Code
-- security_invoker lets the view run with the caller's privileges,
-- so RLS follows the caller instead of being bypassed by the view owner
CREATE TABLE tickets (
id int PRIMARY KEY,
tenant text NOT NULL,
title text NOT NULL
);
INSERT INTO tickets VALUES
(1, 'acme', 'login loop'),
(2, 'acme', 'export stuck'),
(3, 'globex', 'billing mismatch');
CREATE ROLE tenant_acme;
GRANT USAGE ON SCHEMA public TO tenant_acme;
ALTER TABLE tickets ENABLE ROW LEVEL SECURITY;
CREATE POLICY tickets_tenant_isolation ON tickets
USING (tenant = current_setting('app.tenant', true));
GRANT SELECT ON tickets TO tenant_acme;
CREATE VIEW tickets_classic AS
SELECT id, tenant, title FROM tickets;
CREATE VIEW tickets_invoker WITH (security_invoker = true) AS
SELECT id, tenant, title FROM tickets;
GRANT SELECT ON tickets_classic, tickets_invoker TO tenant_acme;
-- 视图的 reloptions 直接记录这个属性
SELECT relname, reloptions
FROM pg_class
WHERE relname IN ('tickets_classic', 'tickets_invoker');
GRANT tenant_acme TO current_user;
SET ROLE tenant_acme;
SET app.tenant = 'acme';
-- classic:以定义者(owner)身份执行,RLS 不作用于 owner → 三行全可见
SELECT id, tenant, title FROM tickets_classic ORDER BY id;
-- invoker:以调用者身份执行,RLS 按调用者生效 → 只看到 acme 的两行
SELECT id, tenant, title FROM tickets_invoker ORDER BY id;
RESET ROLE;
DROP VIEW tickets_invoker, tickets_classic;
DROP TABLE tickets;
DROP ROLE tenant_acme;Two views, one table, two identities
tickets has row level security enabled with a tenant-isolation policy: USING (tenant = current_setting('app.tenant', true)). Two views select the same rows — tickets_classic plainly, tickets_invoker with WITH (security_invoker = true). The catalog query against pg_class shows the difference durably: reloptions for tickets_invoker records {security_invoker=true} while tickets_classic has none. A classic view executes with its owner’s privileges, so RLS does not apply to it; the invoker view runs as the caller.
What the role sees through each view
After GRANT tenant_acme TO current_user, SET ROLE tenant_acme, and SET app.tenant = 'acme', the two SELECTs behave differently exactly as the file’s comments predict:
-- tickets_classic: owner identity bypasses RLS → all three rows
id | tenant | title
----+--------+-----------------
1 | acme | login loop
2 | acme | export stuck
3 | globex | billing mismatch
-- tickets_invoker: caller identity, RLS applies → only acme rows
id | tenant | title
----+--------+-----------------
1 | acme | login loop
2 | acme | export stuck
The same GRANT SELECT on both views, yet the classic one leaks the globex row — the bypass the feature exists to close.
The line that flips the execution identity
CREATE VIEW tickets_invoker WITH (security_invoker = true) AS
SELECT id, tenant, title FROM tickets;
One view option, PostgreSQL 15+, is the entire difference: policies evaluate against the querying role, so per-tenant views stop bypassing the table’s own row security.
Interactive Tools for This Topic
FAQ
- What are the relations in play here?
- `tickets_classic`, `tickets_invoker` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.