infinite recursion detected in policy for relation

Supabase error 42P17 "infinite recursion detected in policy for relation"? Two RLS policies check each other. Break the loop with a function.

Reviewed October 14, 2026

What it means in plain English

Two tables' security rules each look at the other table, so Postgres goes round in circles and gives up. Common with 'team' or 'shared list' features.

Likely causes

  • Policy on table A queries table B, and table B's policy queries table A.
  • A policy on a table queries the same table.

How to fix it, step by step

  1. Find the two policies involved (Database → Policies).
  2. Create a security definer function in a non-exposed schema that reads the membership table directly:
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;
  1. Rewrite both policies to use id in (select private.user_list_ids()) / list_id in (...).
  2. Never create security definer functions in an exposed schema like public.

How to check it worked

  • Queries on both tables return results without the 42P17 error.
  • A non-member still sees nothing.

Sources

Can't fix it? Get help.

If the steps didn't clear the error, send what the screen says. A person can take a look. This is a request for a paid fix, not an instant chat.

Request a fix