Supabase “infinite recursion detected in policy for relation” – cause and fix

By Abheesht Tripathi · Published

infinite recursion detected in policy for relation "members"  (code 42P17, HTTP 500)

What it means

To decide whether you may see a row of members, Postgres evaluates the policy. The policy itself selects from members, which needs the policy again, which selects from members again. Postgres detects the loop and aborts the whole query. Through the Supabase API it arrives as HTTP 500.

The same happens across two tables: a policy on projects reads members, and a policy on members reads projects.

The policy that causes it

The typical team or organisation membership check:

create policy "see my team" on public.members
  for select to authenticated
  using (
    team_id in (
      select team_id from public.members where user_id = auth.uid()
    )
  );

The subquery reads members under RLS, so it triggers this same policy.

The other classic is an admin check on the table that stores the role:

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

Same loop, same fix: move the lookup into a function such as private.is_admin() built like the one below, and call (select private.is_admin()) in the policy.

The fix: look up membership in a security definer function

A security definer function runs with its owner's rights. Owned by postgres (the table owner), its query is not filtered by RLS, so the loop is gone. Put it in a schema that is not exposed through the API, pin its search_path, and let only signed-in users call it:

create schema if not exists private;

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.members where user_id = (select auth.uid());
$$;

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

drop policy "see my team" on public.members;

create policy "see my team" on public.members
  for select to authenticated
  using (team_id in (select private.my_team_ids()));

The function still filters by auth.uid(), so it only ever returns the caller's own teams. Reuse it in the policies of every table that is scoped by team (projects, invoices, files), which removes the cross-table loops too.

Why the private schema matters

Supabase's RLS guide warns that a security definer function in an exposed schema (such as public) can be called over the Data API with the creator's privileges. A function that only returns the caller's team ids is harmless, but the habit is dangerous: keep these helpers out of public. And with search_path = '', every table name inside must be schema-qualified (public.members), or the function fails with “relation does not exist”.

How to verify

Run the query as a real user, inside a transaction that is rolled back:

begin;
set local role authenticated;
select set_config('request.jwt.claims',
  json_build_object('sub', '<a-real-user-uuid>', 'role', 'authenticated')::text, true);
select * from public.members limit 5;
rollback;

You should get that user's team rows and no 42P17. Also run it with a user who belongs to no team: you should get zero rows, not an error.

Performance note

Wrapping calls as (select auth.uid()) and (select private.my_team_ids()) lets Postgres evaluate them once per statement instead of once per row. Add an index on members (user_id) so the lookup stays fast as the table grows.

Related guides

More in Supabase RLS and Storage · all guides