Supabase "infinite recursion detected in policy" (42P17) in a Lovable or Bolt app: fixes (2026)
Prices and facts checked on each tool's official site. Plans change often, so confirm before you pay.
Your Lovable or Bolt app suddenly shows no data, or the console says infinite recursion detected in policy for relation "profiles" (error code 42P17). It usually appears right after the AI added an admin role, team members or sharing. Here's what it means and the safe way out.
What the error means
Supabase protects tables with Row Level Security (RLS) policies. A policy can look up other tables to decide if a row is allowed. If that lookup hits a table whose own policy leads back to the first table, Postgres can never finish the check. Supabase's docs say it raises 42P17, infinite recursion detected in policy for relation, and the query fails for every role the policies apply to.
Match what you see to the cause
| What the AI just added | Why it loops |
|---|---|
An admin role stored in profiles, checked by a policy on profiles | The policy reads profiles to check the role, which runs the same policy again |
| Teams, workspaces or members | A policy on teams reads team_members, and a policy on team_members reads teams |
| Sharing a list, project or document | Same shape: the shared item and the members table check each other |
| A view or a function used inside a policy | The view or function reads a protected table whose policy leads back |
The fix: a security definer function in a private schema
Supabase's recommended fix is a small function marked security definer. It runs as the user who created it (on Supabase that's postgres, which skips RLS), so the lookup inside it doesn't trigger the policy again and the loop is broken. Their docs show it for a lists and members setup:
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()) );For an admin check, the same idea is a function like private.is_admin() that returns true when public.profiles has your id with the admin role, and the policy uses (select private.is_admin()) instead of reading profiles itself.
Three safety rules the AI often skips
- Keep the function out of exposed schemas. Supabase warns that a security definer function in an exposed schema (like
public) can be called over the API with its creator's powers. Use aprivateschema. - Set
search_path = ''and write full names likepublic.profilesinside it, as Supabase's docs require. - Only return the caller's own data. Filter on
(select auth.uid())so the function can't leak other users' rows.
Don't fix it by turning RLS off or adding using (true). That removes the error by opening the table to anyone. Our RLS policy error guide covers the other common policy mistakes.
What to paste into Lovable or Bolt
We get "infinite recursion detected in policy for relation [table]" (42P17). Don't change anything yet. First list every RLS policy on [table] and on any table it reads, and show which policies read each other. Then propose one fix: a security definer function in a private schema (not public) with set search_path = '', fully qualified table names, filtered by (select auth.uid()), with execute granted only to authenticated, and update the policies to call it. Don't disable Row Level Security or use using (true).
When it's time to get help
If it's one admin check, the fix above takes minutes. Get a second pair of eyes when:
- The AI has rewritten the policies several times and the error keeps coming back.
- The fix decides who can see whose data (teams, clients, shared documents).
- You're about to launch with real users, payments or private data.
Related guides
- Getting "new row violates row-level security policy" instead? See the RLS policy error guide.
- Seeing "permission denied for table" (42501) after Oct 30? See the grants change guide or use the free GRANT fix generator.
- Not sure what your error means? Paste it into the free error decoder, or take the 2-minute launch self-check.
Sources
- Supabase: Row Level Security (Avoid recursive policies: 42P17 and the lists/list_members example; security definer functions, private schema, search_path, exposed-schema warning, (select auth.uid()))
- PostgreSQL: error codes (42P17 invalid_object_definition)
FAQ
What does "infinite recursion detected in policy for relation" mean?
A Row Level Security policy needs to read a table whose own policy, directly or through another table, sends it back to the first one. Postgres can never finish the check, so it stops with error 42P17 and the query fails.
Why does it happen so often in Lovable and Bolt apps?
Admin checks and sharing features. A policy on profiles that looks up profiles to see if you're an admin, or a lists table and a members table whose policies read each other, both loop. AI builders write this pattern a lot.
Should I turn off RLS to make the error go away?
No. That makes the table readable and writable by anyone with your public key. Break the loop with a security definer function in a private schema, which is the fix Supabase's docs recommend.
When should I pay someone to fix it?
When the AI keeps rewriting policies and the error comes back, when the fix would touch who can see whose data, or before you launch an app with other people's data. StackMatch's App Audit is $49 for a written fix list, and fixes plus launch start at $199.
Some links are referral links; we may earn a commission at no extra cost to you. It never changes our recommendations.