← 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#

  1. Move recursive logic to helper tables/views.
  2. Keep policy predicates flat and indexed.
  3. 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_id and user_id can still degrade performance.
  • with check clauses can reintroduce recursion patterns if copied blindly.