Skip to content

RLS perf: wrap auth.uid() in (select ...) for per-statement evaluation #50

Description

@dmitrymaranik

Hi, and thanks for core-oss. I ran a static RLS analyzer (pgrls) over core-api/supabase/migrations and found 39 RLS policies that call auth.uid() per row in their USING/WITH CHECK -- 39 policies across the schema (e.g. on access_requests).

Postgres re-evaluates a bare auth.uid() in a policy once per row. Wrapping it in a scalar subquery -- (select auth.uid()) -- makes it an InitPlan the planner evaluates once per statement and caches. It's predicate-equivalent (row visibility unchanged) and the optimization Supabase documents for RLS.

I've opened a PR with the wrap (a new migration) -- it's pgrls fix --rule PERF001 output, verified to clear the finding with nothing else changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions