infinite recursion detected in policy for relation
Last verified:
The fix
A Row Level Security policy queried a table whose policy needs the first policy again. Postgres stops it with SQLSTATE 42P17 and names the relation that looped.
- 1
Read the relation in the message, then the policies that touch it
The quoted name is the table whose policy recursed. List policies on that table and on any table mentioned inside them. A
qualorwith_checkthat selects from the same table, or from a table that selects back, is the cycle. Do not drop RLS on that table.select tablename, policyname, cmd, qual, with_check from pg_policies where schemaname = 'public' and tablename in ('lists', 'list_members'); - 2
Move the lookup into a security definer function
Follow the RLS guide's pattern. Create schema
private. The function reads the membership table as its owner, filters with(select auth.uid()), and returns only the caller's ids.set search_path = ''is required, and every table inside is schema-qualified. Revoke the default execute grant frompublic, then grant execute to the role the policy is for. Replace the cross-table subquery in the policy with a call to the function. Use your table and column names. Thelists/list_membersnames below are the guide's example.create schema if not exists private; create or replace 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; - 3
Point both policies at the function
The guide's policies call the function instead of selecting the other table. A member who is not the owner can still read the list. A non-member gets no rows, and the error is gone. If your policy is for
anonas well, grant execute toanononly if that role should see the result. The function returns ids for the currentauth.uid()only.create policy "members read lists" on public.lists for select to authenticated using ( id in (select private.user_list_ids()) ); create policy "members read membership" on public.list_members for select to authenticated using ( list_id in (select private.user_list_ids()) ); - 4
Keep private out of the Data API
In Project Settings, then API, then Exposed schemas,
privatemust not be listed. The RLS guide says a security definer function in an exposed schema is callable over the Data API as the owner. Retry as a member and as a user who is not a member. The member gets rows. The other user gets none, without42P17. If the function's owner cannot bypass RLS, or the table was created withFORCE ROW LEVEL SECURITY, the lookup evaluates the policy again and the error stays. On Supabase,postgreshasbypassrls.
Why it happens
Postgres raises infinite recursion detected in policy for relation "list_members" with SQLSTATE 42P17. Supabase's Row Level Security guide describes the shape: two tables whose policies read each other never resolve, and the query fails for every role those policies apply to. A policy that selects from its own table does it in one step. Generated sharing rules do this constantly. lists allows a select when a row exists in list_members, and list_members allows a select when the parent lists row is visible.
The same guide breaks the cycle with a security definer function in a schema the API does not expose. The function runs as its owner. On Supabase that owner is postgres, which has bypassrls, so the membership lookup does not re-enter the policy. The guide's cautions are part of the fix. Set search_path = '' and schema-qualify every name inside the function. Without a pinned search path, a caller can point an unqualified name at their own object and run it with the owner's privileges. A security definer function in an exposed schema is callable through the Data API with those privileges. private must not be listed under Exposed schemas.
Wrapping auth.uid() in a subselect, which the guide recommends for performance, does not break a cycle. Disabling RLS makes the error disappear and leaves the table readable by the anon key. A policy of using (true) does the same. new row violates row-level security policy is a different failure: the policy ran and the row lost. This one never finishes the check.
The next error you'll probably hit
Once this is fixed, the usual follow-up is new row violates row-level security policy. new row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.
Prompt to paste back into your AI tool
Paste this above the error.
Postgres returns 42P17: infinite recursion detected in policy for relation "TABLE". Do not disable RLS. Do not replace the policy with USING (true). Do not put a security definer function in the public schema or in any schema listed under Exposed schemas. Do not drop search_path = ''. Do not wrap auth.uid() in a subselect and call that the fix for the cycle. Do not grant the service role to the browser. 1. Show the policies whose qual or with_check reads TABLE or reads a table that reads back. 2. Create a security definer function in schema private, with set search_path = '', stable, and schema-qualified names. Filter on (select auth.uid()). Revoke execute from public and grant it to the policy role. 3. Rewrite the policies to call that function instead of selecting the other table. 4. Tell me to confirm private is not in Exposed schemas, then retry as a member and as a non-member. 5. Paste only that SQL.
Common questions
+The SQL Editor can select the table and the app returns 42P17. Why?
The SQL Editor runs as a privileged role that bypasses RLS, so the policies never call each other. The app uses anon or authenticated, which has to pass the policy. Run the failing query as that role, or trust the error: it already named the relation.
+I used (select auth.uid()) inside the policy and it still recurses. Why?
That wrapper is how the RLS guide avoids re-evaluating auth.uid() on every row. It does not stop a policy from selecting a table that selects back. The lookup has to move into a security definer function whose owner bypasses RLS.
+Is this the same as new row violates row-level security policy?
No. That message means a policy finished and rejected the row. 42P17 means the policy never finished because it needed itself. Fix the cycle. A missing GRANT is permission denied for table, code 42501.
Related errors
- new row violates row-level security policynew row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.CursorLovableBoltSupabasePostgresOct 15, 2026
- RLS Disabled in Public: Table public.x is public, but RLS has not been enabledRLS Disabled in Public. Table public.x is public, but RLS has not been enabled. Enable Row Level Security and add a policy in the same change. The anon key can read that table until you do.CursorLovableBoltSupabasePostgresNov 3, 2026
- permission denied for table (42501)permission denied for table (42501): the Supabase role has no GRANT. Postgres stops before Row Level Security. Grant that command and leave RLS on.CursorLovableBoltSupabasePostgresOct 15, 2026
- Auth session missing after refreshAuth session missing: sign-in worked in the browser, but Next.js cannot see the Supabase cookie. Use the @supabase/ssr server client and refresh.CursorLovableBoltSupabaseAuthOct 14, 2026