PostgreSQL 15 retrospective: Preserve caller row permissions through a view
Use the PostgreSQL 15 security_invoker option with two test roles and a row-security policy, and check the distinction between view and underlying table permissions.
Archive retrospective · Topic year 2022
In this article
- The permission choice PostgreSQL 15 made explicit
- Use a view for columns and a policy for rows
- Check the exact response as two different roles
- Connect row policy to the application identity model
- Separate permissions for views and underlying relations
- Keep a small regression fixture for permission changes
The permission choice PostgreSQL 15 made explicit
PostgreSQL 15 added a view option that can check access to underlying relations using the invoking user’s permissions. A view can give applications a stable set of columns while the underlying table defines which rows each role may read. For this example two regions share a product identifier, but each role should receive a different stock row. The feature provides a way to preserve the caller’s permission context through the view. A view is not a substitute for a row policy. The view describes the shape of the output, and a policy expresses which records belong within the result for a given caller. Plan to test with the roles the application actually uses. Checking only with an administrator or table owner can give a misleading impression of ordinary access.
Use a view for columns and a policy for rows
The table stores a composite key containing tenant_role and sku. Both regions can therefore hold a row for P-10. A check constraint rejects a negative quantity. The view exposes only the product identifier and quantity to its consumers. The source table policy compares the active PostgreSQL role with the stored role for the row. This direct comparison is deliberately simple. A larger system could use a separate membership model as its source of permission facts. The view definition sets security_invoker to true. The test roles receive schema usage and select access on both the view and its underlying table. In this teaching fixture the test roles are also allowed to select from the source table, where the same row policy applies. If an application has further column or relation access constraints, account for those independently.
-- Run in an isolated PostgreSQL 15 test database using its lab owner.
BEGIN;
CREATE ROLE archive_lab_north NOLOGIN;
CREATE ROLE archive_lab_south NOLOGIN;
CREATE SCHEMA archive_lab;
CREATE TABLE archive_lab.stock (
tenant_role name NOT NULL,
sku text NOT NULL,
quantity integer NOT NULL CHECK (quantity >= 0),
PRIMARY KEY (tenant_role, sku)
);
INSERT INTO archive_lab.stock VALUES
('archive_lab_north', 'P-10', 12),
('archive_lab_north', 'P-20', 4),
('archive_lab_south', 'P-10', 3);
ALTER TABLE archive_lab.stock ENABLE ROW LEVEL SECURITY;
CREATE POLICY read_own_stock ON archive_lab.stock
FOR SELECT TO archive_lab_north, archive_lab_south
USING (tenant_role = CURRENT_USER);
CREATE VIEW archive_lab.stock_summary
WITH (security_invoker = true) AS
SELECT sku, quantity FROM archive_lab.stock;
GRANT USAGE ON SCHEMA archive_lab TO archive_lab_north, archive_lab_south;
GRANT SELECT ON archive_lab.stock, archive_lab.stock_summary
TO archive_lab_north, archive_lab_south;
SET LOCAL ROLE archive_lab_north;
SELECT sku, quantity FROM archive_lab.stock_summary ORDER BY sku;
-- P-10 | 12
-- P-20 | 4
RESET ROLE;
SET LOCAL ROLE archive_lab_south;
SELECT sku, quantity FROM archive_lab.stock_summary ORDER BY sku;
-- P-10 | 3
RESET ROLE;
ROLLBACK;
Check the exact response as two different roles
Run the script in an isolated PostgreSQL 15 test cluster using a lab account authorised to create and assume the sample roles. The transaction is rolled back at the end, including its sample schema and role changes. The example assumes those role names are unused. With the North role, the view returns P-10 with 12 and P-20 with four. With the South role, it returns only P-10 with three. Check the identifiers and the quantities independently of the number of returned rows. A different region could have the same number of records. Comparing only a count would miss that case. An additional test can assign a role no stock rows and confirm that its result is empty. Use isolated fixture data rather than an application’s live database for these checks. Roles belong to the cluster, so a separate schema alone is not the intended isolation.
Connect row policy to the application identity model
CURRENT_USER refers to the active PostgreSQL role. If a public API shares a connection role among all clients, that value will not identify individual application users by itself. Decide how an authenticated application identity relates to a database role or a policy input. Connection pools also need a clear transaction boundary and a known authorization state for each request. Table owners and certain privileged roles can see different results under row security. Test using the ordinary user identity that is expected to enforce the rule. If administrators correct data through a different workflow, write a separate test for that workflow. Keeping normal reads, operator actions and their permissions explicit makes it easier to understand whether a change affects only one group or the entire application.
Separate permissions for views and underlying relations
A security-invoker view requires the caller to have appropriate privileges on the view and the underlying objects. If a team intends for a view to be the only exposed surface, it should check that access design separately. The fixture grants select on the entire sample table for readability. A real table might need column-level restrictions or a different boundary for data that must not be returned directly. A review can record which roles read the view, which underlying objects they can access, and which rows the policy returns. These are separate checks. When a new column is added, inspect its grant and its presence in the published view. The permission review is easier to repeat when it refers to named roles and explicit relation privileges instead of relying on administrative assumptions.
Keep a small regression fixture for permission changes
The release notes establish the historical origin. The CREATE VIEW and row-security manuals explain the behavior that the fixture is designed to observe. Keep the expected result for North and South and add a role with no rows. In a test that follows the live application path, verify the connection role as well as the returned data. A successful SQL statement by itself is not proof that the appropriate user context was used. When a policy or grant changes, replay the same cases and compare identifiers and values. Add a new case whenever an application role is introduced or an existing view gains a column. The resulting check describes the behavior that matters to users. It can identify a role or schema change independently of a visual change in the application.
Reader discussion
Is the strongest mitigation in code, infrastructure, or operations?
No comments yet. Be the first to contribute.
Write a comment