Supabase: "infinite recursion detected in policy for relation"
A query that worked yesterday now fails with infinite recursion detected in policy for relation "profiles" (Postgres code 42P17). It usually appears right after you, or Lovable, Bolt or Cursor, added an "admins can see everything" or "team members can see each other" policy. The cause is almost always the same: a Row Level Security policy on a table reads from that same table, so checking the policy triggers the policy again, forever.
Why it happens
A typical policy that causes it:
create policy "Admins can read all profiles"
on profiles for select
using (
exists (select 1 from profiles
where id = auth.uid() and role = 'admin')
);
To read profiles, Postgres checks this policy. The policy reads profiles, which needs the policy checked again, and so on. The same loop happens across two tables: team_members checks teams, and teams checks team_members.
The fix: move the lookup into a security definer function
A security definer function runs as its owner, so the lookup inside it doesn't trigger RLS again. Keep it small, read-only, and with an empty search_path:
create or replace function public.is_admin()
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1 from public.profiles
where id = (select auth.uid()) and role = 'admin'
);
$$;
revoke execute on function public.is_admin() from anon;
drop policy if exists "Admins can read all profiles" on profiles;
create policy "Admins can read all profiles"
on profiles for select to authenticated
using ( (select public.is_admin()) );
For teams, the same pattern works with a function such as public.my_team_ids() that returns the team ids of auth.uid(), used as team_id in (select public.my_team_ids()).
Better still: don't keep roles in a table users can edit
If users can update their own profiles row, check they can't change role. Otherwise anyone can make themselves an admin with one request from the browser. Keep roles in a separate user_roles table with no INSERT or UPDATE policy for users, or block the column with a policy's with check. A lot of AI-generated admin panels get this wrong.
Fixes to avoid
- Turning RLS off on the table. The error goes away because every row becomes readable by anyone with your anon key, which ships in your frontend.
using (true)for select. Same result: all profiles, including emails and phone numbers, become public.- Checking the role in the frontend only. Hiding the admin page doesn't protect the data. Anyone can query the table directly.
- Using the service_role key in the browser to skip RLS. It bypasses every policy for everyone. If that already happened, see what to do when the service_role key is exposed.
Getting a different RLS error? See 42501 permission denied, select returns an empty array or new row violates row-level security policy.
A prompt to paste into Lovable, Bolt or Cursor
Supabase returns "infinite recursion detected in policy for relation
profiles". Fix it safely:
1. List every policy on profiles and on any table its policies query.
2. Replace policies that query their own table with a small
security definer function (stable, set search_path = '') that
returns what the policy needs, e.g. public.is_admin().
3. Make sure users cannot change their own role column.
4. Keep RLS on. Do not use using (true), do not disable RLS, and do
not use the service_role key in the frontend.
Want to check nothing else is exposed? LeakPatrol's free scan checks your live app for leaked keys, public files and other common mistakes in about a minute.
Check your app in a minute
Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.
Get the 10-point checklist
Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.
Plus the odd security tip. Unsubscribe any time.
Questions
What does infinite recursion detected in policy for relation mean in Supabase?
A Row Level Security policy reads from the same table it protects (or two tables check each other), so checking the policy triggers it again forever. Postgres stops with error 42P17.
How do I fix infinite recursion in a Supabase RLS policy?
Move the lookup into a small security definer function with an empty search_path, such as public.is_admin(), and call it from the policy. The function reads the table without triggering RLS again.
Is it safe to disable RLS to fix infinite recursion?
No. With RLS off, anyone with your anon key, which is in your frontend, can read and change every row.
Can users make themselves admin if the role is in profiles?
Yes, if they can update their own profiles row and nothing stops them changing the role column. Keep roles in a separate table users cannot write to.
More guides
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase's October 30 change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase "Database error saving new user": find the cause and fix it safely
- Supabase Storage upload: "new row violates row-level security policy" fixed
- Supabase Edge Function "blocked by CORS policy" fixed
- Supabase "Forbidden use of secret API key in browser" (401) fixed
- Supabase "Invalid API key" and "Legacy API keys are disabled" fixed
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)