Skip to content

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

Open
dmitrymaranik wants to merge 1 commit into
scosman:mainfrom
dmitrymaranik:perf/wrap-rls-initplan
Open

perf(rls): wrap auth.uid() in (select ...) for per-statement evaluation#213
dmitrymaranik wants to merge 1 commit into
scosman:mainfrom
dmitrymaranik:perf/wrap-rls-initplan

Conversation

@dmitrymaranik

Copy link
Copy Markdown

Closes #212.

The RLS policies call auth.uid() directly in USING/WITH CHECK, which Postgres re-evaluates once per row. This wraps each call in (select auth.uid()) so the planner evaluates it once per statement (an InitPlan) and caches it -- the Supabase-documented RLS perf pattern.

  • Predicate-equivalent -- (select auth.uid()) returns the same value, so row visibility is unchanged.
  • Verified with pgrls: the PERF001 findings clear to 0 after the change, with nothing else altered.
  • Added as a new migration (20260628000000_wrap_rls_perf_initplan.sql); the SQL is pgrls fix --rule PERF001 output.

Happy to adjust the migration filename/placement to your conventions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant