Supabase "infinite recursion detected in policy for relation" (error 42P17): why it happens and the fix
Short answer: infinite recursion detected in policy for relation (error 42P17) means two of your Supabase row-level security policies are checking each other. A policy on one table reads a second table, and the second table's policy reads the first one back, so the check never finishes and Postgres stops the query. It usually appears right after adding sharing, teams, members or admin roles. Supabase's fix is to move the lookup into a security definer function that both policies call. Don't turn RLS off to make it go away.
Run the free check on your live app
Fixing policies by hand? AppFirstAid's free check looks at your live site from the outside, with no sign-up. It flags leaked keys, files left in public and pages that don't open, and on a Supabase app it lists the tables it saw your site's code query, so you know which policies to review. It never logs in or reads your data.
Run the free check Free, no sign-up.Signs this is your problem
- The error text says
infinite recursion detected in policy for relationfollowed by a table name, or the code is42P17. - It started after you (or your AI builder) added sharing, teams, workspaces, members, invites or an admin role.
- Pages that use the affected tables show nothing or fail for everyone, logged in or not, while other pages still work.
Why it happens
Row-level security (RLS) runs a policy every time someone reads a table. Supabase's docs describe the loop like this: a policy on a lists table checks list_members to see who a list is shared with, and a policy on list_members checks lists to see who owns it. Reading either table triggers the other table's policy, which triggers the first one again. Postgres can't resolve that, so it raises 42P17 and the query fails for every role those policies apply to. A policy that looks up roles in the very table it protects can loop on itself in the same way.
How to find the loop
- Copy the table name from the error message. That's where the loop was noticed.
- In the Supabase dashboard, open that table's policies and read each one. Note every other table the policy mentions (look for
select ... fromorexists (...)). - Open the policies of each table you noted. If one of them mentions the first table again, you've found the loop.
- Also check whether a policy reads its own table, for example an "is admin" check on
profilesinside aprofilespolicy.
The fix: one security definer function both policies call
Supabase's docs break the loop with a security definer function. It runs as the user who created it, so it can read the membership table without triggering that table's policy again. It still filters on the signed-in user, so it returns only that user's rows. You can paste this prompt into Lovable, Bolt or your AI editor, with your real table names:
My Supabase app fails with "infinite recursion detected in policy for relation" (error 42P17). Two RLS policies read each other's tables. Please fix it the way Supabase's row-level security docs describe:
1. Create a schema called private that is NOT in the API's exposed schemas.
2. In it, create a security definer function with search_path set to '' that returns only the ids the signed-in user (auth.uid()) is a member of, reading the membership table with schema-qualified names.
3. Revoke execute on it from public, then grant usage on the private schema and execute on the function to authenticated only.
4. Rewrite both policies to use that function instead of reading each other's tables.
5. Keep RLS on for every table. Don't use the service role or secret key in the app. Show me the SQL before running it.
The shape of the SQL, adapted from Supabase's example (rename the tables and columns to yours):
create schema if not exists private;
create function private.user_list_ids()
returns setof uuid
language sql
security definer
set search_path = ''
stable
as $$
select list_id from public.list_members
where user_id = (select auth.uid())
$$;
revoke execute on function private.user_list_ids() from public;
grant usage on schema private to authenticated;
grant execute on function private.user_list_ids() to authenticated;
create policy "members read lists" on lists for select
to authenticated
using ( id in (select private.user_list_ids()) );
create policy "members read membership" on list_members for select
to authenticated
using ( list_id in (select private.user_list_ids()) );
Drop the old looping policies first (or replace them), or both versions will still apply.
What not to do
- Don't disable RLS on the table to stop the error. That can let anyone with your public key read or change every row.
- Don't put the function in the public schema or any schema exposed to the API. Supabase warns that a security definer function in an exposed schema can be called over the Data API with its creator's privileges.
- Don't skip
set search_path = ''. Supabase says to pin it on every security definer function and write table names with their schema, likepublic.list_members. - Don't switch the app to the secret or service role key. It skips RLS entirely and must never be used in the browser.
Check that it worked
- Log in as a test user who is a member of a shared item but doesn't own it. They should see it.
- Log in as a second test user who isn't a member. They should see nothing of it.
- Log out and open the same page. Logged-out visitors should see nothing private.
A brand-new table that says "permission denied" instead is a different problem: see Supabase "permission denied for table" (error 42501) on a new table. On Bolt database, see Bolt app shows a database permissions error on a new table.
Common questions
What does "infinite recursion detected in policy for relation" mean in Supabase?
It's Postgres error 42P17. Supabase's row-level security docs explain that when two tables have policies that read each other, the check never resolves, so Postgres stops and the query fails for every role those policies apply to.
Why did this start after I added sharing, teams or an admin role?
Those features usually add a second table, like a members or roles table, and a policy that looks it up. If that table's own policy looks back at the first table, the two policies keep calling each other. Supabase's docs use a shared-lists feature as the example.
How do I fix error 42P17 without turning off RLS?
Supabase's docs recommend breaking the loop with a security definer function: a small function, kept in a schema that isn't exposed to the API, that reads the membership table as its owner and returns only the signed-in user's rows. Both policies then call that function instead of reading each other's tables.
Should I just disable row-level security to make the error go away?
No. With RLS off, anyone holding your public key may be able to read or change every row in that table. Keep RLS on and fix the policies so each user still sees only what they should.
How AppFirstAid helps
Our free check reads your live site from the outside, like a visitor, with no signup. It can't see your policies and it never logs in or reads your data, so it won't spot this error by itself. It does catch leaked keys, files left in public and broken sign-up, login and checkout pages, and on a Supabase app it lists the tables your site's code queries. A journey test, on an app you've proved is yours, signs up, logs in and tries checkout like a real first-time customer, with a screenshot of each step, so if a policy loop breaks one of those steps you see exactly where. See pricing for what each costs.
Sources, read November 18, 2026: Supabase docs, "Row Level Security" (sections "Avoid recursive policies", "Use security definer functions" and "Bypassing Row Level Security": the 42P17 error and the shared-lists example, the security definer fix and its SQL, pinning search_path, never placing such a function in an exposed schema, and keeping secret keys out of the browser); PostgreSQL docs, "PostgreSQL Error Codes" (42P17 is invalid_object_definition). Supabase may change these details. AppFirstAid is not affiliated with Supabase, Lovable or Bolt.
Check your live app in under a minute
On a Supabase app, the report also lists the tables it saw your site's code query, so you can check each one still has its policies after the fix.
AppFirstAid's free check looks at your live site from the outside: leaked keys, files left in public, safety settings, and whether your sign-up, login and checkout pages open. No signup. Journey tests go further: a simulated first-time user (a real browser) signs up, opens your email, logs in and tries checkout, and shows you the step that broke. Your first journey test is free.
Run the free checkSettings move around as these tools update. If a menu name has changed, look for the same words nearby. Spotted something out of date? Tell us.
More guides
- Stripe says they paid, but nothing unlocked in my vibe-coded app
- Login broke after I added my custom domain (Supabase redirect URLs)
- What built-in security scans miss in AI-built apps
- Supabase confirmation emails not arriving for new users
- Supabase "permission denied for table" (error 42501) on a new table: why it happens and the fix
- My app works in preview but breaks in production (environment variables differ)
- Stripe webhook fails with 401 on a Supabase Edge Function
- Error 400: redirect_uri_mismatch on Google sign-in (or "The JavaScript origin in the request does not match")
- Lovable app launch checklist: how to test your Lovable app before launch (sign-up, email, login, checkout)
- Bolt app shows a database permissions error on a new table (tables private by default since Oct 30, 2026)