Supabase database

“Infinite recursion detected in policy for relation” in Supabase: what it means and the safe fix

Your row level security (RLS) policy reads the same table it protects, or a table whose policy reads back, so Postgres would check policies forever and stops with error 42P17. The fix is to move the lookup into a small security definer function in a schema the API doesn’t expose, as Supabase’s docs show.

TL;DR

  • The usual trigger is an “admins can read all profiles” policy that looks up the caller’s role in profiles itself.
  • In our lab every signed-in request failed with HTTP 500, admin included; logged-out requests got [] because no policy applied to them.
  • Two common AI fixes, turning RLS off and using (true), let a logged-out visitor read every email and role. A third, a helper in public that takes any user id, let a visitor look up anyone’s role.
  • The fix: private.is_admin() with no arguments, set search_path = '' and execute granted only to authenticated.

Why the policy loops

To decide whether you may read a row in profiles, Postgres runs the table’s policies. If one of them runs select … from profiles, that inner query needs the same policies, which run the same query again. Postgres spots the loop and stops. Supabase’s troubleshooting page says it happens when a policy queries the same table, or another table whose policies lead back to the first.

The classic case in AI-built apps is an admin check:

the policy that loops
create policy "admins read all profiles" on public.profiles
  for select to authenticated
  using (exists (select 1 from public.profiles p
                 where p.id = (select auth.uid()) and p.role = 'admin'));

Team apps hit the two-table version: lists checks list_members, and list_members checks lists. Supabase’s RLS guide uses that example.

What we saw in the lab

lab output, 5 Dec 2026 (Postgres 17.11 and 18.6, PostgREST 12.2.12)
bob GET /profiles:           {"code":"42P17","message":"infinite recursion detected in policy for relation \"profiles\""} [HTTP 500]
alice (admin) GET /profiles: {"code":"42P17", … } [HTTP 500]
anon GET /profiles (no policy applies to anon): [] [HTTP 200]

Every signed-in user is locked out, so the whole app looks broken. Logged-out requests don’t fail, because no policy is written for anon.

Three quick fixes, tested

We applied each to a fresh copy and called the API.

quick fix A (don't): turn RLS off
alter table public.profiles disable row level security;
quick fix B (don't): swap the admin check for "true"
create policy "Enable read access for all users" on public.profiles for select using (true);
lab output, after A or B
anon GET /profiles: [{"email":"alice@example.com","role":"admin"},
                     {"email":"bob@example.com","role":"user"},
                     {"email":"stranger@example.com","role":"user"}] [HTTP 200]
quick fix C (don't): a definer helper in public that takes any user id
create function public.get_user_role(uid uuid) returns text
  language sql security definer stable
  as $$ select role from profiles where id = uid $$;
create policy "admins read all profiles" on public.profiles
  for select to authenticated using (public.get_user_role((select auth.uid())) = 'admin');
lab output
bob GET /profiles:           [{"full_name":"Bob"}] [HTTP 200]
alice (admin) GET /profiles: three rows [HTTP 200]
anon POST /rpc/get_user_role for alice's id: "admin" [HTTP 200]

Fix C stops the loop, but the helper sits in public, so the API exposes it at /rest/v1/rpc/get_user_role. Anyone can ask for any user’s role, and Supabase’s Security Advisor would flag it twice: no fixed search path, and callable by anon. A helper that returned emails would leak emails the same way.

The fix from Supabase’s docs

supabase/migrations/…_profiles_admin_policy.sql
create schema if not exists private;  -- keep it out of "Exposed schemas" in API settings
create function private.is_admin()
returns boolean
language sql
security definer
set search_path = ''
stable
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 if exists "admins read all profiles" on public.profiles;
create policy "admins read all profiles" on public.profiles
  for select to authenticated using ((select private.is_admin()));

Why it works: the function runs as its owner. On Supabase that is postgres, which has bypassrls, so the lookup inside doesn’t trigger the policy again. It takes no arguments, so it can only answer about the caller. Supabase warns never to create a security definer function in an exposed schema, which is why it lives in private.

Keep the owner policy too (using ((select auth.uid()) = id)) so ordinary users still read their own profile. And make sure users can’t set their own role: see can a user make themselves admin?

How to check it worked

lab output after the fix
alice (admin) GET /profiles: [{"full_name":"Alice Admin"},{"full_name":"Bob"},{"full_name":"Sam"}] [HTTP 200]
bob GET /profiles:           [{"full_name":"Bob"}] [HTTP 200]
anon GET /profiles:          [] [HTTP 200]
anon POST /rpc/is_admin:     {"code":"PGRST202","message":"Could not find the function public.is_admin without parameters in the schema cache"} [HTTP 404]

In your app, sign in as an admin and as an ordinary user and compare. Then run Supabase’s Security Advisor: it should not list is_admin under “Function Search Path Mutable” or “Public Can Execute SECURITY DEFINER Function”.

Ask your AI builder to fix it

Prompt for your AI builder
My Supabase app fails with "infinite recursion detected in policy for relation profiles" (42P17). Do not disable RLS and do not replace any policy with using (true). Create a schema called private (not exposed to the API) and a function private.is_admin() with no arguments, language sql, security definer, set search_path = '', stable, that returns whether public.profiles has a row with id = (select auth.uid()) and role = 'admin'. Revoke execute from public, grant usage on schema private and execute on the function to authenticated only. Rewrite the admin policy to use ((select private.is_admin())) and keep the owner-only policy. Write it as a new migration and show me the SQL before you apply it.

Questions people ask

What does "infinite recursion detected in policy for relation" mean?

A row level security policy queries the table it protects, or a table whose policy queries back, so checking the policy would never end. Postgres stops the query with error 42P17. Supabase's troubleshooting page describes both shapes.

Is it safe to disable RLS to stop the recursion error?

No. In our lab, turning RLS off on profiles let a logged-out visitor read every email and role with only the public key. Replacing the admin check with using (true) did the same.

Why does a security definer function fix infinite recursion?

It runs as its owner. On Supabase the owner is postgres, which has bypassrls, so the lookup inside the function doesn't run the policy again. Supabase's docs say a function owned by a role without bypassrls stays recursive.

Where should the security definer function go?

In a schema the Data API doesn't expose, such as private, with search_path set to an empty string and execute granted only to the roles that need it. Supabase's docs say never to create one in an exposed schema.

Sources and lab

The checks behind this guide, each with example code, a self-check and the fix.

Free check

Check your migrations for this, free.

Paste a public GitHub repo. The free RLS check reads its supabase/migrations and flags tables with RLS off or no policies, always-true policies, update policies with no WITH CHECK, SECURITY DEFINER functions anyone can call or with no fixed search_path, views that skip RLS and public buckets. It never connects to your database.

Free, no sign-upReads migration files onlyHow it works

Written by Manraj Singh Dhillon. Last checked 5 Dec 2026. Code samples are synthetic and run against your own project; nothing here names a real app. Spotted a mistake? Tell us.