Guide
How do I fix infinite recursion detected in policy (42P17) in a Lovable or Bolt Supabase app?
MondayMedic checks for common public mistakes. It is not a full security audit, and a good score does not mean your app is safe.
Paste your policies into our free RLS checker.
Check your live app first. Paste your app's public link into the free MondayMedic check. It reads only public pages and scripts, takes about a minute and needs no account. It flags Supabase tables the public key can read and secret keys in the browser. It does not read your policies, it does not change data, and it is not a full security audit.
The short answer
Two policies are checking each other in a loop. Keep RLS on and move the lookup into a security definer function in a private schema. The error infinite recursion detected in policy for relation "profiles" (Postgres code 42P17) means a row level security policy reads a table whose own policy leads back to the first one. Postgres can't finish the check, so it stops the query. Supabase's fix is to do the lookup inside a function that runs as its owner, which skips RLS on the table it reads and breaks the loop.
Don't "fix" it by turning RLS off or swapping the policy for using (true). The error goes away, and so does the lock on your data.
Why does my app suddenly show this error?
It usually appears right after you ask the AI builder for roles, admins, teams or sharing. Supabase's troubleshooting page lists two shapes:
- A policy that queries its own table. The classic Lovable case: a policy on
profilesthat checks whether the caller is an admin by selecting fromprofiles. Checking that inner select runs the same policy again. - Two tables whose policies read each other. A policy on
projectschecksproject_membersto see who it's shared with, and a policy onproject_memberschecksprojectsto see who owns it.
Supabase notes the error can also show up during Storage uploads and downloads, if a storage policy reads a table whose policies are recursive. In the app it often looks like a blank page, an empty list or a failed save, with the real message only in the browser's network tab or the Postgres logs.
How do I find the policy that loops?
Run this in the Supabase SQL Editor. It lists every policy in public with the expressions it checks:
select tablename, policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname = 'public'
order by tablename, policyname;
Look at the table named in the error message. Any policy whose qual or with_check contains select ... from the same table is recursive. If it selects from another table, check that table's policies: if they select back from the first table, that's your loop.
How do I fix it without opening my data?
Put the lookup in a security definer function in a schema the API doesn't expose, then call that function from the policy. This is the pattern in Supabase's RLS docs. For the self-referencing admin check on profiles:
create schema if not exists private;
create function private.is_admin()
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select exists (
select 1 from public.profiles
where 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 read all profiles" on public.profiles;
create policy "admins read all profiles"
on public.profiles for select
to authenticated
using ( (select private.is_admin()) );
create policy "users read own profile"
on public.profiles for select
to authenticated
using ( (select auth.uid()) = id );
For two tables that point at each other (a sharing feature), Supabase's docs use a function that returns the ids the caller is a member of, and both policies use it:
create function private.my_project_ids()
returns setof uuid
language sql
security definer
set search_path = ''
stable
as $$
select project_id from public.project_members
where user_id = (select auth.uid())
$$;
revoke execute on function private.my_project_ids() from public;
grant execute on function private.my_project_ids() to authenticated;
create policy "members read projects" on public.projects for select
to authenticated
using ( id in (select private.my_project_ids()) );
create policy "members read membership" on public.project_members for select
to authenticated
using ( project_id in (select private.my_project_ids()) );
Drop the old recursive policies before you add the new ones. Postgres combines permissive policies with OR, so a loop left behind still breaks the query.
Why does the function have to be in a private schema?
A security definer function runs with its owner's rights. On Supabase that owner is normally postgres, which can bypass RLS. That's why it breaks the loop, and also why Supabase warns never to put one in an exposed schema like public: anyone could call it through the Data API with those rights. Two more rules from the docs:
- Set
search_path = ''and write every table aspublic.table_name, so a caller can't swap in their own object. - Make the function filter on
(select auth.uid()), so it only ever answers about the person asking.
Supabase also notes the trick only works when the function's owner can bypass RLS. If the owner can't, or the table is set to force row level security, the policy runs again inside the function and the loop comes back.
How do I fix it in Lovable or Bolt?
Paste a request like this into the chat:
Supabase returns 42P17 "infinite recursion detected in policy for relation
profiles". Do not disable RLS and do not use using (true). Find the policies
that select from the same table (or from a table whose policies select back).
Move that lookup into a security definer function in a schema called private
(not public), with set search_path = '' and schema-qualified table names,
filtered on (select auth.uid()). Revoke execute from public, grant execute to
authenticated only. Drop the old recursive policies, recreate them using the
function, and show me the final SQL before running it.
Then test it: sign in as a normal user and as an admin (or as a member and a non-member), and confirm each sees only what they should. Open the app logged out too.
Is this the same as the 42501 permission denied error?
No. 42501 means a role is missing a grant or a write failed a with check. 42P17 means the policies themselves loop. If you see 42501 on a new table, read why a new Supabase table says permission denied for table.
Related guides
- How to check and fix open Supabase RLS in a Lovable app
- Why a new Supabase table says permission denied for table (42501)
- Does a Supabase view bypass RLS?
- Are Supabase storage buckets public? Do public buckets skip RLS?
Want a weekly look from the outside? Run the free check on your live app.
FAQ
What does infinite recursion detected in policy for relation mean in Supabase?
A row level security policy on a table reads a table whose own policy leads back to the first one, so Postgres can never finish checking it. Postgres stops the query with error 42P17 (invalid_object_definition) instead of looping forever.
Why does it happen in Lovable and Bolt apps?
The usual cause is an admin or team check written inside a policy. For example, a policy on profiles that looks up the caller's role in profiles, or a policy on projects that checks project_members while project_members checks projects. Both shapes show up when an AI builder adds roles, teams or sharing.
Should I turn off RLS or use using (true) to make the error go away?
No. That removes the protection the policy was there to give, and the table becomes readable to anyone the grants allow. Keep RLS on and move the lookup into a security definer function in a private schema.
Where should the security definer function live?
In a schema that is not exposed through the Data API, such as private, with search_path set to an empty string and every table name schema-qualified. Supabase warns that a security definer function in an exposed schema can be called over the API with its owner's privileges.
Does the MondayMedic free check find recursive policies?
No. The free check reads your live app from the outside and flags Supabase tables the public key can read and secret keys in the browser. It does not read your policies. Use the SQL in this guide to find policies that query other tables.