DEV Community

Cenk KURTOĞLU
Cenk KURTOĞLU

Posted on

Wrapped but still row-dependent: the RLS helper call that runs per row

On a PostgreSQL 17 fixture, an RLS policy written as USING (is_org_member(orders.org_id)) deparses to ( SELECT is_org_member(orders.org_id) AS is_org_member). Wrapping it in a scalar subquery does not make it run once: the argument is a column of the table the policy protects, so the planner has a correlated SubPlan and calls the helper for every candidate row.

The row-independent form deparses differently: (owner_id = ( SELECT auth.uid() AS uid)). The subquery has no reference to the outer row and can be evaluated once. For membership checks the same idea looks like org_id IN (SELECT org_id FROM memberships WHERE user_id = (SELECT auth.uid())) — the row set is built once, then matched.

I added a 13th read-only catalog check (M) to a nightly RLS drift monitor. It reads pg_policies.qual / with_check text and flags a scalar subquery whose function call receives a column of the policy's own table, using pg_attribute for the column names. It reads no application rows and needs no service_role.

Honest limits: this is a catalog heuristic, not a profiler result. It does not run EXPLAIN on your workload, does not measure time, and was verified only on a synthetic local PostgreSQL 17 fixture. Hosted build revisions are not visible from a catalog query. A flagged row is a policy worth reading; zero findings does not mean safe.

Free workflow + standalone SQL (13 checks A–M): https://kurtoglu1.gumroad.com/l/supabase-rls-drift-monitor-free

Top comments (0)