Guide · Supabase
"Infinite recursion detected in policy": what it means and the safe fix
It means one of your table rules is checking itself in a loop, so the database stops the request instead of running forever. Nothing has been exposed: the request was refused, not answered. The fix is to change how that rule checks permission. The one thing not to do is the quick fix an AI tool may reach for first: switching the rules off, or replacing them with one that lets everyone in.
What does “infinite recursion detected in policy” mean?
Row level security is the set of rules that decides who may read or change each row in a table. Some rules need to look something up first, such as “is this person an admin?”. If that lookup reads a table that has its own rules, and those rules lead back to the first one, the database would go round in circles. Postgres notices the loop and stops. The table named in the message, often "profiles" or "users", is where the loop starts.
Is my data at risk?
Not from this message. A stopped request returns nothing. The risk comes from the wrong fix. Supabase describes the same error, its two causes and the same safe fix in its own help article.
The two reasons it happens
We set up a test database the way Supabase does and caused this message both ways:
- A rule on a table looks up the same table. The classic case is “admins can see every profile”, written as a rule on
profilesthat readsprofilesto find out who is an admin. In our test the message named"profiles". - Two tables whose rules read each other. For example, a team can be seen by its members, and a member can be seen by the team owner. Each rule reads the other table, so they loop. The message names the table the app asked for first.
Why you should not switch the rules off
It makes the message disappear, which is why it gets suggested. But with row level security off, or with a rule that simply says yes to everyone, the public key that sits in every Supabase website lets anyone read that table. For a table of profiles or users, that is every customer’s details. Supabase warns about exactly this as “RLS disabled in public”.
The safe fix
Move the “is this person allowed?” lookup into a small helper that runs with the owner’s permissions, so it can check the table without triggering the rules again. Keep that helper in a private schema, so it cannot be called from outside your app. This is the method Supabase recommends. Paste this into Lovable, Claude, Bolt, Cursor or whichever tool built your app:
My app shows "infinite recursion detected in policy for relation". Do not turn row level security off and do not replace the rule with one that lets everyone in. Find the policy that reads its own table, or two tables whose policies read each other. Move that lookup into a security definer function in a private schema (not public), with search_path set to an empty string and every table name written with its schema, and allow only signed in users to run it. Call it inside the policy as (select private.function_name()). Then show me every policy on the tables involved, and test it as an admin, as an ordinary user and while signed out.
With that change in our test, the admin saw every profile, an ordinary user saw only their own, a stranger saw nothing, and a stranger who tried to call the helper directly was refused. That is the result you want.
How do I know it worked?
- The message is gone when the app loads the same screen again.
- Each person still sees only what they should. Try it as an admin, as an ordinary user and signed out.
- Check from the outside. Our free database check reads your app the way a stranger’s browser does and tells you which tables anyone can open, in plain English.
GuardCue checks apps built with Claude, Lovable, Bolt or any other AI tool and explains what it finds in plain English. Related: “new row violates row-level security policy” and row level security explained.