Deutsch
Error messageSupabase · RLS

Fix “infinite recursion detected in policy for relation” in Supabase

Why a Supabase team app fails with infinite recursion detected in policy for relation members, and the helper function that fixes it without opening member lists.

The message you seePostgres · 42P17

infinite recursion detected in policy for relation "members"

By Tim GeithnerPublished 4 min read

Short answer

Postgres stopped because a row level security policy on a table reads that same table, directly or through another table whose policy reads back. The classic case is a team app where the policy on members asks members which teams the user belongs to. Every table whose policy checks members fails with the same message. Fix it with a security definer helper function that returns the caller’s team ids, with a fixed search_path, in a schema the API does not expose, and use it in the policies. Never fix it with using (true) on members.

Ask an AI about this page:ChatGPTClaude

On this page

Every query on a table with row level security runs the table’s policies. If a policy on members contains a query on members, that inner query has to run the same policy again, which contains the same query, and so on. Postgres sees the loop before running anything and stops with code 42P17 and this message. Nothing was read or written. The quoted name is the table where the loop is, which is not always the table you queried.

Find your cause in 30 seconds

What you seeMost likely cause
Any query on members failsA policy on members reads members
A query on projects or another table names membersOther tables fail with the same message
The message names teams or flips between two tablesTwo tables whose policies read each other

We ran all three on Postgres with Supabase’s roles, using the usual team structure:

The tables used on this page
create table public.teams (
  id uuid primary key default gen_random_uuid(),
  name text not null
);

create table public.members (
  team_id uuid not null references public.teams (id) on delete cascade,
  user_id uuid not null references auth.users (id) on delete cascade,
  role text not null default 'member',
  primary key (team_id, user_id)
);

create table public.projects (
  id uuid primary key default gen_random_uuid(),
  team_id uuid not null references public.teams (id) on delete cascade,
  name text not null
);

alter table public.teams enable row level security;
alter table public.members enable row level security;
alter table public.projects enable row level security;

The three causes

1. A policy on members reads members

The goal is sensible: members should see the other members of their own teams. So the AI builder writes a policy that asks members which teams the user belongs to:

VulnerableThe policies that cause the recursion
create policy "Members can see their team's members"
  on public.members for select
  to authenticated
  using (team_id in (
    select team_id from public.members
    where user_id = (select auth.uid())
  ));

create policy "Members can see their team's projects"
  on public.projects for select
  to authenticated
  using (team_id in (
    select team_id from public.members
    where user_id = (select auth.uid())
  ));

The inner select ... from public.members is a normal query, so it runs the policy on members, which contains the same inner query. That is the loop.

2. Other tables fail with the same message

The policy on projects above is fine on its own. But its lookup in members runs the broken members policy, so select * from projects fails with infinite recursion detected in policy for relation "members". Fix the policy on members and every table that checks membership works again.

3. Two tables whose policies read each other

The loop can also span two tables. Here the policy on members checks teams, and the policy on teams checks members. Each query starts the other’s policy, and Postgres names the table you queried:

VulnerableTwo policies that read each other
create policy "Members of visible teams"
  on public.members for select
  to authenticated
  using (exists (select 1 from public.teams t where t.id = team_id));

create policy "Teams I belong to"
  on public.teams for select
  to authenticated
  using (id in (
    select team_id from public.members
    where user_id = (select auth.uid())
  ));

How to fix it

Move the lookup “which teams am I in?” into one function that reads members without running its policy, and use that function in every policy:

SafeFix: a helper function and policies that use 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 m.team_id
  from public.members m
  where m.user_id = (select auth.uid());
$$;

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

drop policy if exists "Members can see their team's members" on public.members;
drop policy if exists "Members can see their team's projects" on public.projects;
drop policy if exists "Members of visible teams" on public.members;
drop policy if exists "Teams I belong to" on public.teams;

create policy "Members can see their team's members"
  on public.members for select
  to authenticated
  using (team_id in (select private.my_team_ids()));

create policy "Members can see their team's projects"
  on public.projects for select
  to authenticated
  using (team_id in (select private.my_team_ids()));

create policy "Members can see their teams"
  on public.teams for select
  to authenticated
  using (id in (select private.my_team_ids()));

Each line has a job:

  • security definer runs the lookup as the function’s owner. The owner of members is not subject to its policies, so the loop cannot start. Supabase’s RLS docs use the same pattern.
  • where m.user_id = (select auth.uid()) is the function’s own check. It only ever returns the caller’s teams, so it cannot be used to list other teams.
  • set search_path = '' with public.members written out means nobody can slip in a look-alike table. The Security Advisor flags definer functions without it.
  • The private schema is not exposed through the Data API. Supabase’s docs say such functions should never live in an exposed schema. The revoke and grant lines make sure only signed-in users can run it, which the policies need. Without that grant, every query fails with permission denied for function instead.

The guide to multi-tenant data isolation builds the same pattern out for inserts, updates and roles inside a team.

VulnerableThe fix that opens every team
drop policy "Members can see their team's members" on public.members;

create policy "Anyone signed in can see members"
  on public.members for select
  to authenticated
  using (true);
Fix infinite recursion in RLS policies without opening member lists

My Supabase app fails with “infinite recursion detected in policy for relation members”. Find every policy in the migrations that queries the members table, including policies on other tables, and policies on two tables that query each other.

Fix it in a new migration: create a schema called private if it does not exist, and in it a function private.my_team_ids() returning setof uuid, language sql, stable, security definer, set search_path = ‘’, that selects team_id from public.members where user_id = (select auth.uid()). Revoke all on it from public and anon, grant execute to authenticated. Then drop the recursive policies and recreate them for the authenticated role with team_id in (select private.my_team_ids()).

Do not disable RLS, do not use using (true), do not create the helper in the public schema and do not edit existing migrations. Show me the migration before applying it.

Check that the fix works

In the SQL Editor, read the tables as a signed-in member. Use a real user id from Authentication → Users:

Check: what a member sees
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
select team_id, user_id from public.members;
select name from public.projects;
rollback;

Both queries return rows instead of the error, and only rows of that user’s teams. Run it again with the id of a user from another team: none of the first team’s members or projects may appear. That second run is the one that proves the fix did not open anything. The guide to testing RLS policies has the full set of checks.

Frequently asked questions

Why does a query on projects complain about members?

The policy on projects looks up the user’s teams in members. That lookup is itself subject to the policy on members, and that policy reads members again. Postgres names the table where the loop is, not the table you queried.

Can I just turn off RLS on the members table?

No. Then anyone with your public anon key can list every member of every team, which usually means every customer’s user ids and roles. The helper function on this page fixes the loop and keeps each team’s members private.

Why does the helper function need security definer?

It must read members without the members policy, otherwise the loop starts again. A security definer function runs as its owner, and the owner of the table is not subject to its policies. That is also why it needs set search_path = ‘’ and limited execute rights.

Is the helper function safe to call from the browser?

It only returns the caller’s own team ids, so it leaks nothing. Keep it in a schema that is not exposed through the Data API anyway, and grant execute to authenticated only, so it is a building block for policies and not an endpoint.

Sources

  1. 1.Row Level Security — Supabase Docs
  2. 2.Row Security Policies — PostgreSQL Documentation
  3. 3.PostgreSQL Error Codes — PostgreSQL Documentation
  4. 4.Database functions — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt