LeakPatrol › Guides

Supabase: "infinite recursion detected in policy for relation"

By LeakPatrol · Updated 20 November 2026

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

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.

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