Sub topic 14
Policies That Query Other Tables
This chapter assumes you know basic SQL, PostgreSQL roles, GRANT, and how WHERE clauses filter rows. It focuses on policies whose expressions look up related data in another table.
Use a membership table for access decisions
A policy can contain a subquery. A subquery is a query nested inside another query. This lets PostgreSQL ask whether the current role appears in an access-control table.
For example, a project can be visible when the current role is a member of that project:
CREATE TABLE projects (
id bigserial PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE project_members (
project_id bigint NOT NULL REFERENCES projects(id),
user_name name NOT NULL,
PRIMARY KEY (project_id, user_name)
);
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
CREATE POLICY project_members_can_read
ON projects
FOR SELECT
USING (
EXISTS (
SELECT 1
FROM project_members AS member
WHERE member.project_id = projects.id
AND member.user_name = current_user
)
);EXISTS returns true when the subquery finds at least one row. PostgreSQL evaluates the policy for each candidate project. The role sees only projects with a matching membership row.
The role still needs table privileges. RLS does not replace GRANT:
GRANT SELECT ON projects TO alice, bob;
GRANT SELECT ON project_members TO alice, bob;The second grant matters because the policy reads project_members. The membership table may also have its own RLS policy. If it does, that policy affects which membership rows the subquery can see.
Avoid recursive policy evaluation
A policy must not indirectly ask itself the same question. For example, suppose a policy on project_members queries projects, while the policy on projects queries project_members. Evaluating either policy can require evaluating the other forever. PostgreSQL detects many direct cases and reports an infinite-recursion error.
Keep the authorization relationship in a table with a simple, nonrecursive policy. Often, the membership table is maintained by a trusted application or administrative role and is not exposed for ordinary updates.
When a helper function is appropriate
For more complex checks, a policy can call a function. A SECURITY DEFINER function runs with its owner's privileges, which can avoid recursive RLS checks on a carefully controlled lookup table.
Use this pattern sparingly. The function owner must be trusted, the function should qualify object names, and callers should not be able to alter objects that the function uses. Do not use a security-definer function as a shortcut for unclear permissions.
A simpler alternative is to give the application role read access to a dedicated membership table and write a direct EXISTS subquery. Direct policies are easier to inspect with pg_policies and easier to reason about during an audit.
Performance and correctness
Index the columns used to connect the tables:
CREATE INDEX project_members_user_project_idx
ON project_members (user_name, project_id);Use EXPLAIN with a representative role and session state. Check that PostgreSQL can find membership rows efficiently. Also consider whether changes to membership should take effect immediately. A policy reads the current database state, so removing a membership normally prevents later queries from returning that project.
Exercise
Why can a policy on projects fail if its EXISTS subquery reads projectmembers, but projectmembers has no SELECT privilege for the role?
Show answer
The role still needs ordinary table privilege on project_members. RLS filters rows after privilege checks; it does not grant SELECT on the referenced table.