Skip to content

perf(rls): wrap current_setting() in the generated tenant policy for per-statement evaluation#639

Open
dmitrymaranik wants to merge 2 commits into
ruvnet:mainfrom
dmitrymaranik:perf/wrap-rls-initplan
Open

perf(rls): wrap current_setting() in the generated tenant policy for per-statement evaluation#639
dmitrymaranik wants to merge 2 commits into
ruvnet:mainfrom
dmitrymaranik:perf/wrap-rls-initplan

Conversation

@dmitrymaranik

Copy link
Copy Markdown

ruvnet/RuVector's RLS policy generator emits current_setting('<tenant setting>') unwrapped in the policy predicate. Postgres re-evaluates a bare current_setting(...) once per row the policy scans; wrapping it in a scalar subquery — (select current_setting(...)) — makes it a per-statement InitPlan the planner evaluates once and caches. It's predicate-equivalent (tenant isolation unchanged) and the documented Postgres RLS performance pattern; the benefit grows with table size.

This wraps the generated call and keeps the test assertions in sync (rls.rs, isolation.rs). I couldn't run the Rust test suite locally — if CI surfaces another assertion on the generated policy SQL, point me at it and I'll update it.

Spotted with pgrls (an open-source Postgres RLS analyzer). Happy to adjust.

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.

1 participant