A list, dashboard, or file upload fails. The browser may show a generic 500 or an empty state. In the API response or Postgres logs you see:
ERROR: 42P17: infinite recursion detected in policy for relation "profiles"
The relation name is the looping table: profiles, list_members, organization_members. This is not a missing policy (that is 42501 / "new row violates row-level security policy") and not a policy that is too open.
How the loop starts
A policy runs for every row a role reads or writes. If it queries a table that also has RLS, those policies run too. Two shapes show up a lot in Lovable and Bolt apps with teams or admins:
-
A table looks at itself. A policy on
profilessays "you may read every profile if your own profile hasis_admin = true". To decide that, Postgres readsprofilesand hits the same policy again. -
Two tables look at each other. A policy on
listsallows a row if you appear inlist_members. A policy onlist_membersallows a row if you own the parent inlists. Neither check can finish.
A chat builder's fastest "fix" is alter table ... disable row level security. That stops the error and also publishes the table to anyone with the anon key. Do not do that. Break the cycle.
Find the policies in the cycle
Run this in the SQL editor. The error names one table. Look at it 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 them for the table from the error, or a pair that points at each other.
Fix a self-check on profiles
This policy 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 that owner is postgres, which bypasses RLS, so the inner read does not re-enter the policy. Keep the function in an unexposed schema, 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: one question about the current user. Do not grant it to anon, and do not let users set is_admin on themselves.
Fix membership tables that point at each other
This shared-list version fails for every role the policies cover:
-- 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 it 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()));
Easy to skip: set search_path = '' and schema-qualify every table in the function. The helper only skips RLS when its owner can (postgres has bypassrls on hosted Supabase). A function owned by a role without that privilege, or a table with force row level security, will recurse again. Do not expose the function in public: anyone who can call rpc() would run it with the owner's rights.
Test, and check Storage if uploads were the symptom
Sign in as a member, as a second user who should see nothing, and 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.
If the failure was a Storage upload, check policies on storage.objects too. A policy that asks "is this user a member of the project?" fails with the same 42P17 when membership policies recurse. Fix the public-table cycle, then retry.
When you add a team or admin feature, write the helper first and point every policy at it. Never approve a migration that disables RLS or adds using (true) to hide the error.
The full guide is at https://aiappmedic.com/guides/supabase-infinite-recursion-detected-in-policy . You can also run a free scan of your app at https://aiappmedic.com/scan .
Top comments (0)