Supabase "infinite recursion detected in policy": how to fix it
Your Lovable, Bolt or Cursor app suddenly returns HTTP 500 with infinite recursion detected in policy for relation "…" (Postgres 42P17). It means an RLS policy is reading a table whose policies call back into the first one. Postgres stops the loop. AI tools often create this when they add an "admins can see everything" policy that checks a role column on the same table.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €491. Find the looping policy
The relation name in the error is the table whose policies loop, not always the one you queried. In the Supabase SQL editor run:
select schemaname, tablename, policyname, cmd, qual, with_check
from pg_policies
where tablename = 'profiles'; -- use the name from the error
Look for a sub-select of the same table (classic: select role from profiles where id = auth.uid() inside a policy on profiles), or of another table whose policy reads back (for example profiles ↔ team_members).
2. Prefer a simple auth.uid() check
Many "admin" policies are overkill. If each user should only see their own rows, this is enough and never recurses:
create policy "own rows"
on public.profiles for select to authenticated
using (id = (select auth.uid()));
Drop the looping policy first if it is the one breaking every query.
3. Use a security definer helper for real role checks
When you do need "admins see all", move the lookup into a function that bypasses RLS. Put it in a schema that is not listed under Exposed schemas in the API settings (a private schema is the usual choice), set search_path = '', and grant execute only to the roles that need it:
create schema if not exists private;
create or replace 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, anon;
grant execute on function private.is_admin() to authenticated;
drop policy if exists "admins can read all profiles" on public.profiles;
create policy "admins can read all profiles"
on public.profiles for select to authenticated
using ((select private.is_admin()));
Do not put this function in public if public is exposed to the Data API, or anyone could call it directly. Do not read a role from user_metadata in a policy: users can change that themselves. Prefer a column only your server writes, or a claim in app_metadata.
4. Break mutual table loops the same way
If profiles policies read team_members and team_members policies read profiles, wrap one side in a security definer function (or denormalise an owner_id / user_id column so one policy only needs auth.uid()). Storage uploads can hit the same error when a storage policy joins a table that loops back: see storage upload 403 RLS.
5. Test as a normal user, not the service role
The service role key bypasses RLS, so it hides the bug. Retest with the anon or authenticated key (or a second account in your app). Confirm a normal user gets only their rows, and an admin gets the wider set, with no 500.
Quick checklist
- Read the relation name in the error and list its policies
- Remove same-table sub-selects from policies
- Role / team checks live in a private security definer function
- No circular policies between two tables
- Retested with a normal logged-in user, not the service role
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.