PatchPaste · Guides
Last updated 7 November 2026
"infinite recursion detected in policy for relation": the safe fix
Your Lovable or Bolt app suddenly shows nothing, and the Supabase logs say infinite recursion detected in policy for relation "profiles" (error 42P17). Here is why it happens, the fix the AI usually suggests that you should refuse, and the fix Supabase documents.
using (true): both make the table readable with your public key. Move the lookup into a security definer function in a private schema, with set search_path = '', and call that function from the policy.Why it happens
Supabase's troubleshooting note gives two causes: a policy that queries the same table, or a policy that queries another table whose policies eventually point back at the original. Its RLS guide adds that two tables whose policies read each other never resolve. Postgres raises 42P17 and the query fails for every role the policies apply to.
Two shapes come up again and again in AI-built apps:
- The admin check on profiles. A policy on
profileslike "admins can see every profile" checks the role withselect ... from profiles where id = auth.uid(). It reads the table it protects. - Sharing or teams. A policy on
listscheckslist_members, and a policy onlist_memberscheckslists. Supabase uses exactly this example in its guide.
A policy on documents that reads team_members is fine on its own. It only loops if team_members' policy reads back.
The fixes to refuse
- "Disable RLS on profiles." Supabase's Security Advisor says a public table with RLS off can be read, changed and deleted by anyone with the project URL. Your anon key ships in the app, so that is everyone.
- "Change the policy to using (true)." Same result for reads. See RLS using (true).
- "Use the service_role key for this query." That key bypasses RLS completely and must never be in frontend code. See service_role key exposed.
- "Read the role from user_metadata." Supabase's Advisor has a lint for policies that reference user metadata (
rls_references_user_metadata) because signed-in users can edit their own metadata.
The safe fix: a security definer helper
A security definer function runs as the role that created it. On Supabase that is postgres, which bypasses RLS, so the lookup inside the function doesn't trigger the policy again and the loop is broken. This is the pattern in Supabase's RLS guide.
Example: admins on a profiles table
Keep roles in their own table, which users can't edit, and ask one yes/no question:
create schema if not exists private;
create table if not exists public.user_roles (
user_id uuid references auth.users on delete cascade,
role text not null,
primary key (user_id, role)
);
alter table public.user_roles enable row level security;
-- no insert/update policies: only you (dashboard or server) assign roles
create or replace function private.is_admin()
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select exists (
select 1 from public.user_roles
where user_id = (select auth.uid()) and role = 'admin'
);
$$;
revoke execute on function private.is_admin() from public;
grant usage on schema private to authenticated;
grant execute on function private.is_admin() to authenticated;
drop policy if exists "Admins can view all profiles" on public.profiles;
create policy "Admins can view all profiles"
on public.profiles for select
to authenticated
using ( (select private.is_admin()) );
create policy "Users can view their own profile"
on public.profiles for select
to authenticated
using ( (select auth.uid()) = id );Postgres combines permissive policies with OR, so a user sees their own row and an admin sees all of them. Adjust id to whatever column holds the user id in your table.
Example: shared lists or teams
For the two-tables-reading-each-other case, Supabase's guide returns the caller's list ids from a helper and uses it on both tables:
create function private.user_list_ids() returns setof uuid language sql security definer set search_path = '' stable as $$ select list_id from public.list_members where user_id = (select auth.uid()) $$; revoke execute on function private.user_list_ids() from public; grant usage on schema private to authenticated; grant execute on function private.user_list_ids() to authenticated; create policy "members read lists" on public.lists for select to authenticated using ( id in (select private.user_list_ids()) ); create policy "members read membership" on public.list_members for select to authenticated using ( list_id in (select private.user_list_ids()) );
Rules for the helper (from Supabase's docs)
- Never put it in an exposed schema. A security definer function in a schema listed under "Exposed schemas" can be called over the Data API with the creator's rights. Use
private, notpublic. Security Advisor has lints for security definer functions thatanonorauthenticatedcan execute (anon_security_definer_function_executable,authenticated_security_definer_function_executable). - Set
search_path = ''and write every name with its schema (public.user_roles). Advisor flags functions without it (function_search_path_mutable). - Keep it narrow. Answer one question about
(select auth.uid()). Don't return other people's data from it. - Owner matters. Supabase notes it only skips RLS if its owner can. A function owned by a role without
bypassrls, or a table set toforce row level security, stays recursive.
Check it worked
- The error is gone in the app and in the Supabase Postgres logs.
- Two test accounts: a normal user sees only their own rows, an admin (or member) sees what they should, a non-member sees nothing.
- Logged out: open the live app in a private window. Nothing private should load.
- Security Advisor: no
rls_disabled_in_public,permissive_rls_policy,function_search_path_mutableor security-definer-executable warnings for the tables and functions you touched. - From outside: scan the live URL to confirm common tables don't answer to your public key without a login. That is how you catch an AI edit that quietly switched RLS off while "fixing" the error.
A prompt to paste into Lovable or Bolt
My Supabase queries fail with "infinite recursion detected in policy for relation" (42P17). Fix it without weakening security. 1) List every RLS policy on the table in the error and on any table those policies read, and show me which ones form the loop. 2) Move the lookup into a security definer function in a schema called private (create it if needed), with SET search_path = '', STABLE, every table name schema-qualified, and filtering on (select auth.uid()). Revoke execute from public and grant execute and schema usage only to authenticated. 3) Rewrite the looping policies to call that function. If roles live on the profiles table or in user_metadata, propose a separate public.user_roles table that users cannot write to, and wait for my OK. Do NOT disable RLS, do NOT use USING (true) or WITH CHECK (true) on private tables, do NOT put the function in the public schema, and do NOT use the service_role key in frontend code. Show me the final SQL.
Related: Supabase RLS check, select returns an empty array, "new row violates row-level security policy" and 42501 permission denied.
Infinite recursion in RLS FAQ
What does "infinite recursion detected in policy for relation" mean?
A Row Level Security policy reads a table whose own policy ends up running the first policy again. Supabase's troubleshooting note says this happens when a policy queries the same table, or queries another table whose policies point back at the original. Postgres spots the loop and stops the query with error code 42P17.
Should I turn RLS off to make the error go away?
No. With RLS off on a table in the public schema, Supabase's Security Advisor warns that anyone with the project URL can read, change and delete its rows, because the anon key in your app is public. The error is annoying, but it fails closed. Turning RLS off fails open.
Is using (true) an OK quick fix?
Only on tables that really are public. On a profiles or members table it means every visitor with your public key can read every row. Use a security definer helper instead.
Why does the admin check on my profiles table cause it?
A policy such as "admins can see all profiles" that checks the role with a select from profiles reads the same table it protects, so the policy runs again inside itself. Move the role check into a security definer function, ideally reading a separate roles table.
Is a security definer function safe?
It is Supabase's documented fix, with conditions: put it in a schema that is not exposed through the Data API (for example private), set search_path = '' and schema-qualify every name, keep it to one narrow yes/no or id-list question about the current user, and grant execute only to the roles that need it.
Can PatchPaste find recursive policies?
No. PatchPaste does not sign in or read your policies. What it can do, after you change policies, is check from outside whether common Supabase tables answer to your public anon key without a login, which is how you catch an AI 'fix' that switched RLS off or added using (true).
Changed your policies? Check the live app for free
Paste your live URL for a free A to F report. PatchPaste checks whether common Supabase tables answer to your public anon key without a login (read-only, at most one row, discarded), searches the JavaScript your site sends for secret keys, and flags exposed .env/.git/source maps and missing headers. It does not sign in or read your policies. Fix prompts for that report are a one-off £9 (or £29 for 10). Scans are free to rerun after each change.
Want to see one first? View a sample report.
Sources (checked 7 Nov 2026): Supabase, Row Level Security (Use security definer functions; Avoid recursive policies; 42P17; lists/list_members example; exposed-schema caution; search_path; bypassrls owner note); Supabase Knowledge Base, Infinite recursion detected in policy for relation "table_name"; Supabase, Database Advisors (lints 0011 function_search_path_mutable, 0013 rls_disabled_in_public, 0015 rls_references_user_metadata, 0028/0029 security definer function executable).