App Finishers

Fix guide · updated 2026-11-09

Infinite recursion detected in policy for relation (Supabase): find the policy and break the loop

infinite recursion detected in policy for relation "..." · 42P17 · 500

A page stays empty or shows an error. In the browser's Network tab, the request to /rest/v1/ for one of your tables returns a 500, and the error from Supabase reads infinite recursion detected in policy for relation "profiles", code 42P17, with your own table's name in the quotes. Your data is fine. One of your table's security policies (row level security) has a loop in it, and Postgres refuses to run it.

1. What the loop is

A policy decides which rows each user can see. To check a row, Postgres runs the policy. If the policy looks something up in its own table, Postgres has to run the policy again to check that lookup, and again for the next one, forever. So it stops right away with this error. Two tables whose policies look each other up make the same loop.

We reproduced it on Postgres 16, with Supabase's roles. What it breaks:

2. Find the policy

Run this in the SQL editor (Supabase's dashboard), or ask your AI tool to list the policies of the table named in the error:

select tablename, policyname, cmd, qual, with_check
from pg_policies
where schemaname = 'public'
order by tablename, policyname;

Look at the policies of the table in the error, mainly those whose cmd is SELECT or ALL. You'll find one of two shapes.

A policy that looks up its own table. The usual one is an admin check on profiles:

create policy "Admins can view all profiles" on public.profiles
for select to authenticated
using (
  exists (
    select 1 from public.profiles
    where id = auth.uid() and role = 'admin'
  )
);

In the pg_policies list, Postgres shows the table's own name after FROM, with _1 added: FROM profiles profiles_1. Team features make the same shape: a policy on team_members that lets members see their teammates by looking up team_members.

Two tables that look each other up. Supabase's own example is a sharing feature: the policy on lists reads list_members to find who a list is shared with, and the policy on list_members reads lists to find who owns it. The error names whichever table the app asked for first: in our test, loading lists reported "lists", and loading list_members reported "list_members".

3. Break the loop with a security definer function

Supabase's documented fix moves the lookup into a function marked security definer. Such a function runs as the role that created it, postgres on Supabase, which isn't held back by policies. The lookup inside it doesn't run the policy again, so the loop is gone. For the admin check:

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 exists (
    select 1 from public.profiles
    where id = (select auth.uid()) and role = 'admin'
  )
$$;

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

drop policy "Admins can view all profiles" on public.profiles;
create policy "Admins can view all profiles" on public.profiles
for select to authenticated
using ( (select private.is_admin()) );

In our test, the admin then saw every profile and a normal user saw only their own, through their existing "view own profile" policy. For teams, the function returns the caller's teams, and every policy that needs them calls it. Use the same type as your team_id column:

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

revoke execute on function private.my_team_ids() from public;
grant usage on schema private to authenticated;
grant execute on function private.my_team_ids() to authenticated;

drop policy "Members can view their team" on public.team_members;
create policy "Members can view their team" on public.team_members
for select to authenticated
using ( team_id in (select private.my_team_ids()) );

A member then saw everyone on their own team and nobody else. Policies on other tables, like projects or tasks, can use the same team_id in (select private.my_team_ids()) test. That also breaks the two-table loop: both policies call the function instead of reading each other's table. Run the SQL in the SQL editor, or give it to your AI tool and ask it to apply it as a migration, with your own table and policy names.

Three details matter:

4. Errors you may see after the fix

permission denied for function is_admin (42501, a 403 in the app). The grant execute line is missing. Policies run with the rights of the user making the request, so that user needs the right to run the function.

stack depth limit exceeded (a 500). The function was created without security definer, so its lookup goes through the same policy again and the loop is back. It doesn't always show: in our test it failed only when the table had no "view own row" policy. So it can work for months, then break when someone edits a policy. Add security definer either way.

relation "profiles" does not exist. With set search_path = '', a table name without its schema isn't found. Write public.profiles.

5. Fixes that make it worse

Turning row level security off on the table, or replacing the policy's condition with true, makes the error go away because nothing is checked anymore. Every signed-in user, and often anyone with your app's public key, can then read and change every row. Using the service role key (or a secret key) in the app does the same thing: that key skips every policy, and anyone who opens your site can copy it. If your AI tool proposes one of these, ask it for the security definer function instead.

6. Test it as a real user

The dashboard skips policies, so test as a signed-in user. Run this in the SQL editor with the ID of an admin, then with the ID of a normal user (from Authentication → Users):

begin;
set local role authenticated;
set local request.jwt.claim.sub = 'PUT-THE-USER-ID-HERE';
select * from public.profiles;
rollback;

Both should return rows with no error, and the normal user should see only what they're allowed to. Then reload the page in your app.

Still stuck?

Write down the full error, the output of the pg_policies query for the table in the error, and the tables its policies read. That's what anyone helping you will ask for first, and following the lookups from one policy to the next usually shows where the loop closes.

Other Supabase problems? See Supabase fixes.