"infinite recursion detected in policy for relation" in a Supabase app
You added sharing, or teams, or an admin role. Now a page that worked yesterday loads nothing at all, and the console shows this: infinite recursion detected in policy for relation "profiles" with the code 42P17 . Nothing is corrupted and no data is lost, but as Supabase's own documentation puts it, "the query fails for every role the policies apply to", so this is not one unlucky user. One of…
The error "infinite recursion detected in policy for relation profiles" resulted from a Supabase app's row level security (RLS) policy. The policy asked the database a question it couldn't answer, causing PostgreSQL to stop the query to prevent an infinite loop. This issue arose due to a subquery referencing the same table (profiles) within the policy.
To resolve this, administrators were advised to use a security definer function that runs as the role that created it, bypassing RLS. This function checks if the current user is an admin and can then return the appropriate result without triggering the infinite recursion error. By implementing these changes, the policy errors were resolved, and the Supabase app could function correctly.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.