Fixing "infinite recursion detected in policy for relation" in Supabase (without turning RLS off)
You add one policy so admins can read every row. Suddenly the whole table fails, for everyone: code: 42P17 message: infinite recursion detected in policy for relation "profiles" "Users read their own row, admins re
You add one policy so admins can read every row. Suddenly the whole table fails, for everyone:
code: 42P17
message: infinite recursion detected in policy for relation "profiles"
"Users read their own row, admins read all rows" is one of the most common rules in Supabase apps, and the most obvious way to write it triggers this error. I reproduced it on a local Supabase, fixed it, and checked who could read what at every step.
The policy that causes it
The admin flag lives in the same table the policy protects:
create policy "read own row or admin" on public.profiles
for select to authenticated
using (
id = (select auth.uid())
or exists (
select 1 from public.profiles p
where p.id = (select auth.uid()) and p.role = 'admin'
)
);
It looks fine. But the subquery reads profiles, and reading profiles applies this same policy again. That runs the subquery again, and so on. Postgres sees the loop and stops with 42P17.
What actually happens
I ran it as a normal client and as an admin, with supabase-js and the anon key. (The script uses a temporary table, demo_recursion_profiles, so it never touches real data.)
With the subquery policy:
| Who | Rows | HTTP | Error |
|---|---|---|---|
| client | none | 500 | 42P17 |
| admin | none | 500 | 42P17 |
Two things are easy to miss:
- The admin fails too. It's not "admins can't see everything." Nobody can read the table at all.
- It's a 500, not an empty list. The whole request fails, so the app usually shows a blank screen or a generic error.
The fix: move the check into a security definer function
create function public.is_admin()
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1 from public.profiles
where id = (select auth.uid()) and role = 'admin'
);
$$;
revoke execute on function public.is_admin() from public, anon;
grant execute on function public.is_admin() to authenticated;
drop policy "read own row or admin" on public.profiles;
create policy "read own row or admin" on public.profiles
for select to authenticated
using (
id = (select auth.uid())
or (select public.is_admin())
);
A security definer function runs with its owner's rights. Here the owner is postgres, which also owns the table, so the policy is not applied inside the function, and the loop is gone.
After the fix:
| Who | Rows | HTTP | Error |
|---|---|---|---|
| client A | 1 (their own) | 200 | none |
| client B | 1 (their own) | 200 | none |
| admin | 3 (all) | 200 | none |
The wrong fix: turning RLS off
Disabling RLS also makes the error disappear. That's why it gets suggested. Here is what it does:
| Who | RLS | Rows | HTTP | Error |
|---|---|---|---|---|
| client A | disabled | 3 of 3 | 200 | none |
No error, and client A can read everyone's row. In a real profiles table, that would include every user's email and role. The script turns RLS back on right after this step.
To make sure the fix is really what removed the error, the script also puts the broken policy back (the error returns), then restores the fix (it goes away again).
Be careful with security definer
The fix works because the function skips RLS. That's also why it needs care:
-
set search_path = '', and write every name with its schema (public.profiles,auth.uid()). Otherwise a caller could make the function use their own objects. -
Grant
executeonly to the roles that need it. New functions can be run by everyone by default, so revoke frompublicandanonfirst. -
Only answer about the caller.
is_admin()checksauth.uid(), not a user ID passed in. Functions in thepublicschema can also be called directly through the API, so they shouldn't return anything a user shouldn't see.
Another option is to keep the role in the user's JWT claims and read it in the policy instead of querying the table. I used the function because the role already lives in profiles.
How my client portal does it
The portal behind these posts uses the same pattern in production. is_admin() is the function above. Company membership has the same problem (a members table whose policy needs to check membership), so it goes through is_org_member() too. And the role column itself is protected by three layers, so a client can't make themselves an admin. I wrote about that in my last post: https://dev.to/kurosworks/all-my-supabase-rls-tests-passed-a-review-still-found-6-problems-4e2a
Quick checklist
- Does any policy query the same table it protects? That's the loop.
- Move the check into a small
security definerfunction withset search_path = ''. - Revoke
executefrompublicandanon, grant it toauthenticated. - Test as a normal user and as an admin, through the API, not only in the SQL Editor (the SQL Editor skips RLS).
- Never "fix" it by turning RLS off.
The demo script and the README: https://github.com/kuros-works/client-portal-demo/pull/23
Stuck on this error, or another RLS or permission error in your Supabase app? I fix these as a small fixed-price job on Upwork: https://www.upwork.com/services/product/development-it-a-supabase-rls-or-permission-error-fixed-in-your-app-2126481891543141476
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.