Fix guides · Lovable, Bolt, Cursor, Claude Code ·

Fix Supabase "infinite recursion detected in policy" error

infinite recursion detected in policy for relation "profiles"

Supabase stops your query with PostgreSQL error 42P17 because a Row Level Security (RLS) policy reads the same table it guards, either directly or through another table's policy. The policy's subquery becomes another query subject to the same RLS, which becomes another query, without end. Usually a profiles or users policy joins back to its own table instead of comparing auth.uid() to a column, or two tables' policies reference each other. Fix by writing policies that only use auth.uid() or auth.jwt() claims, never the table they protect.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Policy reads the same table it guards

How to tell: The policy's USING or WITH CHECK clause contains a subquery that SELECTs from the table the policy is defined on (e.g., USING (EXISTS (SELECT 1 FROM profiles WHERE id = auth.uid()))). The subquery is itself subject to RLS on the same table, so the same policy applies again and again. PostgreSQL stops the recursion with 42P17.

Fix: Compare auth.uid() directly to the column that stores the user's id. For profiles, that column is usually id: create policy on public.profiles for select to authenticated using ((select auth.uid()) = id);

2. Two tables' policies reference each other

How to tell: The profiles policy reads teams, and the teams policy reads profiles. Both have USING clauses that join the other table, creating a cycle. The error names one of the two tables.

Fix: Pick one table as the source of truth. Remove the cross-table join from the other table's policy and compare auth.uid() directly to its own user-id column.

3. Helper function called by the policy reads the same table

How to tell: The USING clause calls a custom SQL function, and that function queries the table the policy is on. The error still points at the table, not the function.

Fix: Rewrite the function to avoid reading the guarded table, or inline the logic directly as a simple auth.uid() comparison.

How to fix it

  1. Open the Supabase dashboard, go to the Policies page and find the table named in the error (usually profiles or users). Note the exact policy name and its USING or WITH CHECK expression.
  2. Read the expression: if it selects from the same table (e.g., SELECT 1 FROM profiles WHERE id = auth.uid()), that is the recursion. Policies should use auth.uid() or auth.jwt() claims — not a subquery against the table they are defined on, since the authenticated role usually lacks SELECT on auth.users too.
  3. In the SQL Editor, drop the recursive policy and create a new one that compares auth.uid() directly to the column that holds the user's id: create policy "Users can view own profile" on public.profiles for select to authenticated using ((select auth.uid()) = id);
  4. If two tables reference each other (profiles policy reads teams, teams policy reads profiles), pick one table to be the authority and remove the cross-reference from the other.
  5. Test as a signed-in user: your own row should be visible, another user's row should return nothing. No 42P17 error should appear in the logs.

Using Lovable, Bolt or v0?

In Lovable or Bolt, tell the tool: "Supabase throws 42P17 infinite recursion on profiles. Find the RLS policy that reads profiles, and rewrite it to compare auth.uid() = id without selecting from profiles. Don't disable RLS." Then open the Policies page in the Supabase dashboard and confirm the new policy only uses auth.uid() and constants.

Using Cursor, Claude Code or Windsurf?

Ask Cursor or Claude Code to list every policy on the error table (via pg_policies) and trace any helper functions they call. Have it produce a SQL migration that drops the recursive policy and creates the direct auth.uid() comparison. Verify by running the app as a test user and checking the Supabase logs for 42P17.

The “fix” to avoid

Agents often 'fix' this by disabling RLS on the table (ALTER TABLE profiles DISABLE ROW LEVEL SECURITY) or adding a policy with USING (true). Both make the error disappear and expose every user's data to anyone with your anon key. Keep RLS on and fix the policy expression instead.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

Supabase returns "infinite recursion detected in policy for relation 'profiles'" (error 42P17). Inspect the policies on that table and every table they reference. Find the policy whose USING or WITH CHECK expression reads the same table (directly or through another table's policy). Rewrite that policy to compare auth.uid() directly to the user-id column instead of selecting from the guarded table. If two tables reference each other, break the cycle by making only one table the authority. Do not disable RLS. Show me the SQL migration, then verify by signing in as a test user and confirming the query works without the 42P17 error.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Why does the error say 'infinite recursion detected' if my policy only runs once?

PostgreSQL applies RLS policies as part of the query. If a policy's condition reads the same table, the subquery is another query subject to the same policy, which is another query, without end. The database stops this with 42P17 before the original query can return data.

Can I use a security definer function to bypass the recursion check?

Actually, yes. A security definer function runs as its owner (typically the postgres role or the table owner), and those roles bypass RLS by default. A function owned by such a role that queries the table will not trigger the same policy again, breaking the cycle. A security definer helper is a documented, common fix for this error.

Official documentation

Related errors

Get fixes like this every week