# Supabase "infinite recursion detected in policy for relation": the fix, and the shortcuts that open your database

You added a sharing feature, or an admin role, or a team table, and now your Supabase app returns this error on every request that touches it:

```plaintext
infinite recursion detected in policy for relation "list_members"
```

The Postgres error code is `42P17`. It's one of the most common errors in AI-built apps, because "let members see their lists" is exactly the kind of policy a builder writes by asking one table about another. It's also an error where the quick fixes are dangerous. This is what it means, the fix Supabase documents, and the shortcuts to avoid.

## What the error means

Supabase's troubleshooting page describes it as a circular dependency between Row Level Security (RLS) policies: a policy queries a table that has RLS enabled, and that table's policies end up triggering the original policy again. Postgres notices the loop and stops the query instead of running forever. It can happen when:

- a policy queries the same table it protects
- a policy queries another table whose policies eventually reference the original

Supabase's own RLS guide shows the typical shape. A policy on `lists` checks `list_members` to find who a list is shared with, and a policy on `list_members` checks `lists` to find who owns it:

```sql
-- Each policy reads the table the other one protects.
create policy "members read lists" on lists for select
to authenticated
using (
  exists (
    select 1 from list_members m
    where m.list_id = lists.id and m.user_id = (select auth.uid())
  )
);

create policy "members read membership" on list_members for select
to authenticated
using (
  exists (
    select 1 from lists l
    where l.id = list_members.list_id and l.owner_id = (select auth.uid())
  )
);
```

Supabase's guide says that when two policies read each other they "never resolve," and the query fails for every role the policies apply to. It can also show up in unrelated places: Supabase notes it can surface during Storage uploads when a Storage policy references a table with recursive policies.

## The fix Supabase documents

Break the cycle with a `security definer` function. It runs as its owner, so the lookup inside it doesn't evaluate the policy again. On Supabase the owner is `postgres`, which has `bypassrls`. This is the version from Supabase's RLS guide:

```sql
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()) );
```

Why each line matters, per the same guide:

- `private` **schema.** A `security definer` function in an exposed schema "is callable over the Data API with the creator's privileges." Never put one in a schema listed under Exposed schemas in your API settings.
- `set search_path = ''` and fully qualified names 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.
- `(select auth.uid())` **inside the function.** The function skips RLS, so it must only ever return the caller's own rows.
- `revoke ... from public`**, then grant to** `authenticated` **only,** so signed-out visitors can't call it.

Supabase also warns that this works only when the function's owner can bypass RLS. A function owned by a role without `bypassrls`, or one reading a table set to `force row level security`, evaluates the policy again and stays recursive.

## The shortcuts that make it worse

When the error blocks your app, AI builders and forum answers often suggest one of these. Each one trades an error for an open database:

- **Turn RLS off on the table.** The error disappears because there are no policies to evaluate. Supabase's docs say a table in an exposed schema without RLS is "readable and writable by any role with a grant on it." Here's what that looks like from outside: <https://proofforge.live/fix/supabase-rls-disabled>
- **Replace the policy with** `using (true)`**.** The query works for everyone, including signed-out visitors if the policy applies to `anon`: <https://proofforge.live/fix/supabase-rls-using-true>
- **Put the** `service_role` **key in the frontend** because it bypasses RLS and "just works." That key gives full database access to anyone who opens your JavaScript: <https://proofforge.live/fix/service-role-key-exposed>
- **Create the helper function in** `public` because the builder defaulted there. It becomes a callable endpoint, as described above.

If your error message is different, one of these may be closer: <https://proofforge.live/errors/new-row-violates-row-level-security-policy> for failed inserts, or <https://proofforge.live/errors/permission-denied-for-table> for `42501`, which is a grant problem, not a policy problem.

## How to test the fix without fooling yourself

The Supabase SQL editor runs as `postgres`, which skips RLS, so queries there succeed whether your policies are right or not. Test as a real user instead:

1. In the app, sign in as user A and open the screen that failed. It should load now.
2. Sign in as user B, who isn't a member of A's list. B should see none of A's lists or members.
3. Signed out, request the same data. You should get nothing.

Supabase recommends adding allow and deny cases for each policy to a test file under `supabase/tests/`, run with `supabase test db`. There's a read-only helper for steps 2 and 3 that builds a script to run on your own machine: <https://proofforge.live/two-account-test>

## What the scan can and can't tell you

The recursion error itself isn't a vulnerability, since it fails closed: nothing is readable while it's happening. The risk is the fix. After you resolve it, check from outside that you didn't open something:

- ProofForge's free scan reads what an anonymous visitor can receive from your live URL, with no login. It flags secret keys in your JavaScript and Supabase tables the public key can read. It doesn't sign in as your users, so it can't tell whether user B can see user A's rows. That's what the two-account test is for. Run the scan here: <https://proofforge.live>
- You can also check your tables by hand with the free RLS checker: <https://proofforge.live/tools/supabase-rls-checker>

## The $9 part

If this is the third round of "fix the policy," "now it's open," "now it's broken," each round is spent in builder credits. ProofForge's free scan shows your grade, every finding's severity and impact, and the top fix in full. **$9 once** unlocks every finding with exact code fixes and prompts written for your builder. **Fix + Guard ($29 once)** adds deploy-hook rescans for 30 days. There's a 14-day refund and no subscription: <https://proofforge.live/pricing>

*Sources, checked December 1, 2026:*

- *Supabase docs, "Row Level Security" (avoid recursive policies, security definer functions, default grants),* [*supabase.com/docs/guides/database/postgres/row-level-security*](http://supabase.com/docs/guides/database/postgres/row-level-security)
- *Supabase Knowledge Base, "Infinite recursion detected in policy for relation,"* [*supabase.com/kb/troubleshooting/storage-error-database-schema-is-incompatible-when-uploading-files-988baa*](http://supabase.com/kb/troubleshooting/storage-error-database-schema-is-incompatible-when-uploading-files-988baa)
- *ProofForge tiers, proofforge.live/pricing.json*
