Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
perf(rls): wrap auth.uid() in (select ...) for per-statement evaluation (#965)
Dmitry Maranikba64bb440b92
diff --git a/packages/database/supabase/migrations/20260628085927_wrap_rls_perf_initplan.sql b/packages/database/supabase/migrations/20260628085927_wrap_rls_perf_initplan.sqlnew file mode 100644index 0000000000..1758c4c4c5--- /dev/null+++ b/packages/database/supabase/migrations/20260628085927_wrap_rls_perf_initplan.sql@@ -0,0 +1,77 @@+-- Wrap unwrapped auth.uid() calls in RLS policies in (SELECT ...) so Postgres+-- evaluates them once per statement (an InitPlan) instead of once per row -- the+-- Supabase-documented RLS perf pattern. Covers USING and WITH CHECK. Predicate-+-- equivalent (row visibility unchanged). Generated by 'pgrls fix --rule PERF001'+-- and verified on Postgres 16 (PERF001 -> 0). Refs #964.++-- [PERF001] Wrap auth function call(s) in policy 'DELETE' on public.document so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "DELETE" ON public.document+ USING ("companyId" = ANY(CAST((SELECT get_companies_with_permission(CAST('documents_delete' AS text)) AS get_companies_with_permission) AS text[])) AND (has_valid_api_key_for_company("companyId") OR (groups_for_user(CAST((SELECT auth.uid()) AS text)) && "writeGroups") = TRUE));++-- [PERF001] Wrap auth function call(s) in policy 'INSERT' on public.document so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "INSERT" ON public.document+ WITH CHECK ("companyId" = ANY(CAST((SELECT get_companies_with_permission(CAST('documents_create' AS text)) AS get_companies_with_permission) AS text[])) AND (has_valid_api_key_for_company("companyId") OR (groups_for_user(CAST((SELECT auth.uid()) AS text)) && "writeGroups") = TRUE));++-- [PERF001] Wrap auth function call(s) in policy 'SELECT' on public.document so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "SELECT" ON public.document+ USING ("companyId" = ANY(CAST((SELECT get_companies_with_permission(CAST('documents_view' AS text)) AS get_companies_with_permission) AS text[])) AND (has_valid_api_key_for_company("companyId") OR (groups_for_user(CAST((SELECT auth.uid()) AS text)) && "readGroups") = TRUE));++-- [PERF001] Wrap auth function call(s) in policy 'UPDATE' on public.document so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "UPDATE" ON public.document+ USING ("companyId" = ANY(CAST((SELECT get_companies_with_permission(CAST('documents_update' AS text)) AS get_companies_with_permission) AS text[])) AND (has_valid_api_key_for_company("companyId") OR (groups_for_user(CAST((SELECT auth.uid()) AS text)) && "writeGroups") = TRUE));++-- [PERF001] Wrap auth function call(s) in policy 'Authenticated users can view industries' on public.industry so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "Authenticated users can view industries" ON public.industry+ USING ((SELECT auth.role()) = CAST('authenticated' AS text));++-- [PERF001] Wrap auth function call(s) in policy 'SELECT' on public.notification so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "SELECT" ON public.notification+ USING ("userId" = CAST((SELECT auth.uid()) AS text));++-- [PERF001] Wrap auth function call(s) in policy 'UPDATE' on public.notification so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "UPDATE" ON public.notification+ USING ("userId" = CAST((SELECT auth.uid()) AS text));++-- [PERF001] Wrap auth function call(s) in policy 'DELETE' on public.passkeyCredential so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "DELETE" ON public."passkeyCredential"+ USING (CAST((SELECT auth.uid()) AS text) = "userId");++-- [PERF001] Wrap auth function call(s) in policy 'INSERT' on public.passkeyCredential so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "INSERT" ON public."passkeyCredential"+ WITH CHECK (CAST((SELECT auth.uid()) AS text) = "userId");++-- [PERF001] Wrap auth function call(s) in policy 'SELECT' on public.passkeyCredential so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "SELECT" ON public."passkeyCredential"+ USING (CAST((SELECT auth.uid()) AS text) = "userId");++-- [PERF001] Wrap auth function call(s) in policy 'UPDATE' on public.passkeyCredential so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "UPDATE" ON public."passkeyCredential"+ USING (CAST((SELECT auth.uid()) AS text) = "userId");++-- [PERF001] Wrap auth function call(s) in policy 'INSERT' on public.timeCardEntry so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "INSERT" ON public."timeCardEntry"+ WITH CHECK (("employeeId" = CAST((SELECT auth.uid()) AS text) AND "companyId" = ANY(CAST((SELECT get_companies_with_employee_role() AS get_companies_with_employee_role) AS text[]))) OR "companyId" = ANY(CAST((SELECT get_companies_with_employee_permission(CAST('people_create' AS text)) AS get_companies_with_employee_permission) AS text[])));++-- [PERF001] Wrap auth function call(s) in policy 'SELECT' on public.timeCardEntry so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "SELECT" ON public."timeCardEntry"+ USING (("employeeId" = CAST((SELECT auth.uid()) AS text) AND "companyId" = ANY(CAST((SELECT get_companies_with_employee_role() AS get_companies_with_employee_role) AS text[]))) OR "companyId" = ANY(CAST((SELECT get_companies_with_employee_permission(CAST('people_view' AS text)) AS get_companies_with_employee_permission) AS text[])));++-- [PERF001] Wrap auth function call(s) in policy 'UPDATE' on public.timeCardEntry so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "UPDATE" ON public."timeCardEntry"+ USING (("employeeId" = CAST((SELECT auth.uid()) AS text) AND "companyId" = ANY(CAST((SELECT get_companies_with_employee_role() AS get_companies_with_employee_role) AS text[]))) OR "companyId" = ANY(CAST((SELECT get_companies_with_employee_permission(CAST('people_update' AS text)) AS get_companies_with_employee_permission) AS text[])));++-- [PERF001] Wrap auth function call(s) in policy 'DELETE' on public.userModulePreference so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "DELETE" ON public."userModulePreference"+ USING ("userId" = CAST((SELECT auth.uid()) AS text));++-- [PERF001] Wrap auth function call(s) in policy 'INSERT' on public.userModulePreference so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "INSERT" ON public."userModulePreference"+ WITH CHECK ("userId" = CAST((SELECT auth.uid()) AS text));++-- [PERF001] Wrap auth function call(s) in policy 'SELECT' on public.userModulePreference so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "SELECT" ON public."userModulePreference"+ USING ("userId" = CAST((SELECT auth.uid()) AS text));++-- [PERF001] Wrap auth function call(s) in policy 'UPDATE' on public.userModulePreference so Postgres can cache the result for the whole statement instead of re-evaluating per row.+ALTER POLICY "UPDATE" ON public."userModulePreference"+ USING ("userId" = CAST((SELECT auth.uid()) AS text));