← Back to Fixes
Supabase RLS Infinite Recursion: Root Cause and Production Fix Guide
Fix 'infinite recursion detected in policy' in Supabase with safer policy design and SQL validation checks.
Tweetable Insight#
Most RLS recursion bugs are architecture bugs, not syntax bugs.
One-Sentence Definition#
RLS recursion occurs when a policy references data paths that re-trigger policy evaluation on the same table graph.
Production Risk Warning#
This can block reads/writes in production and trigger auth failures that look like app bugs.
Problem (Search Intent First)#
Supabase throws: infinite recursion detected in policy and queries fail.
Why It Happens#
Policies often call nested subqueries against the same protected table, creating recursive evaluation loops.
Production-Grade Fix#
- Move recursive logic to helper tables/views.
- Keep policy predicates flat and indexed.
- Split membership checks from row filters.
Copy-Paste Solution#
sql
alter policy "team_select_policy" on public.tasks
using (
exists (
select 1
from public.team_memberships tm
where tm.team_id = tasks.team_id
and tm.user_id = auth.uid()
)
);
Edge Cases#
- Missing indexes on
team_idanduser_idcan still degrade performance. with checkclauses can reintroduce recursion patterns if copied blindly.