Supabase error: infinite recursion detected in policy for relation

By AI App Medic · Updated · Supabase · RLS · Security · Lovable

A list, a dashboard or a file upload suddenly fails. The browser may show a generic 500 or an empty state. In the Supabase API response or the Postgres logs you see:

ERROR: 42P17: infinite recursion detected in policy for relation "profiles"

The relation name will be whatever table is looping: profiles, list_members, organization_members and so on. This is not a missing policy (that is a 42501 / "new row violates row-level security policy" error) and it is not a policy that is too open. Postgres has found a cycle in the policy expressions and has stopped the query so it cannot run forever.

How the loop starts

A policy runs for every row a role tries to read or write. If that policy itself queries a table that has RLS, those policies run too. A cycle forms in two common shapes, and both show up a lot in Lovable and Bolt apps that add teams, shared lists or an admin flag:

The fastest "fix" a chat builder offers is alter table … disable row level security. That stops the error because there is no policy left to recurse into, and it also publishes the table to anyone with the anon key. Do not do that. The right fix is to break the cycle.

Step 1: find the policies in the cycle

Run this in the Supabase SQL editor. The error message already names one table; look at that table and every table its expressions mention:

select schemaname, tablename, policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname in ('public', 'storage')
order by tablename, policyname;

qual is the using expression and with_check is the insert/update check. Search those expressions for the table name from the error, and for joins to a membership table. You are looking for a policy that queries its own table, or a pair that points at each other.

Step 2: fix a self-check on profiles

This is the policy that almost always recurses:

-- Reject: reading profiles from a profiles policy.
create policy "admins can read all profiles"
on public.profiles
for select
to authenticated
using (
  exists (
    select 1 from public.profiles as p
    where p.id = (select auth.uid()) and p.is_admin
  )
);

Move the admin lookup into a function that runs as its owner. On Supabase the owner is postgres, which bypasses RLS, so the inner read does not re-enter the policy. Put the function in a schema that is not exposed through the Data API, pin an empty search_path, and grant execute only to the role that needs it:

create schema if not exists private;

create or replace function private.is_admin()
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
  select coalesce(
    (
      select p.is_admin
      from public.profiles as p
      where p.id = (select auth.uid())
    ),
    false
  );
$$;

revoke all on function private.is_admin() from public, anon;
grant usage on schema private to authenticated;
grant execute on function private.is_admin() to authenticated;

drop policy if exists "admins can read all profiles" on public.profiles;

create policy "users read their own profile"
on public.profiles
for select
to authenticated
using ((select auth.uid()) = id);

create policy "admins can read all profiles"
on public.profiles
for select
to authenticated
using (private.is_admin());

Keep the function narrow: it should answer one question about the current user, not return other people's rows. Do not grant it to anon. And make sure ordinary users cannot set is_admin on themselves with an update policy; that leak is covered in RLS is enabled but the data is still public.

Step 3: fix membership tables that point at each other

The shared-list version looks like this and fails for every role the policies apply to:

-- Reject: lists reads list_members, list_members reads lists.
create policy "members read lists"
on public.lists
for select to authenticated
using (
  exists (
    select 1 from public.list_members as m
    where m.list_id = lists.id
      and m.user_id = (select auth.uid())
  )
);

create policy "members read membership"
on public.list_members
for select to authenticated
using (
  exists (
    select 1 from public.lists as l
    where l.id = list_members.list_id
      and l.owner_id = (select auth.uid())
  )
);

Replace both lookups with one helper that returns the current user's list ids, then use that set on both tables:

create schema if not exists private;

create or replace function private.user_list_ids()
returns setof uuid
language sql
stable
security definer
set search_path = ''
as $$
  select m.list_id
  from public.list_members as m
  where m.user_id = (select auth.uid())
$$;

revoke all on function private.user_list_ids() from public, anon;
grant usage on schema private to authenticated;
grant execute on function private.user_list_ids() to authenticated;

drop policy if exists "members read lists" on public.lists;
drop policy if exists "members read membership" on public.list_members;

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()));

Three details from Supabase's own RLS guidance that are easy to skip:

Step 4: test, and check Storage if uploads were the symptom

Sign in as a member, as a second user who should see nothing, and while signed out. Repeat the query that used to throw 42P17. Members should see only their rows; a stranger should see an empty result, not an error. The Table Editor is not a valid test because it bypasses RLS.

If the failure was a Storage upload, open the policies on storage.objects as well. A Storage policy that asks "is this user a member of the project?" will fail with the same 42P17 when the project or membership policies recurse. Fix the public-table cycle first, then retry the upload.

Empty results after the recursion is gone usually mean the new policy is too strict or the user is not in the membership table. That case is covered in Lovable + Supabase RLS not working. Insert failures with 42501 are covered in new row violates row-level security policy.

Prevention

When you add a team, org or admin feature, write the helper first and point every policy at it. Never approve a migration that disables RLS, or one that adds using (true), to make a recursion error disappear. After a large prompt that "fixes permissions", re-run the pg_policies query and retry the member / non-member test before you ship.

FAQ

Can I just turn RLS off on the membership table?

No. The membership table is usually the map of who belongs to which team. With RLS off, anyone who has your public anon key can read it. The anon key is in your frontend bundle, so that is a public leak.

Why does a policy on profiles recurse when it checks is_admin?

To evaluate "admins can read every profile", Postgres must read profiles to see whether the caller is an admin. That read triggers the same policy again. Put the admin check in a security definer function so the inner read does not go through the policy.

The error appears when I upload a file, not when I query a table. Is that still RLS?

Yes. Storage policies on storage.objects often consult a public table such as projects or memberships. If that table's policies recurse, the upload fails with the same 42P17 in the logs.

Related guides