Infinite recursion detected in policy for relation "table_name"

If your Storage or database requests encounter an error and the corresponding Postgres logs show ‘infinite recursion detected in policy for relation “table_name”’, it is because of infinite recursion in your Row-Level Security (RLS) policies.

Why Does This Happen?

This error indicates a circular dependency between RLS policies. It occurs when an RLS policy queries a table with RLS enabled, and that table’s policies end up triggering the original policy again.

This can happen when:

  • A policy queries the same table.
  • A policy queries another table whose RLS policies eventually reference the original table, creating a circular dependency.

Postgres detects the recursive policy evaluation and terminates the query to prevent infinite recursion. This error can also occur during Storage operations if a Storage RLS policy references a table with self-referencing or mutually recursive RLS policies.

How to Resolve This Issue:

Option 1: Update the table’s RLS policies to ensure they do not directly or indirectly create a circular dependency.

Option 2: Wrap the permission check in a SECURITY DEFINER function. A SECURITY DEFINER function executes with the privileges of its owner rather than the calling user. If the function is owned by a role that can bypass the target table’s RLS (for example, postgres or a dedicated helper role with the BYPASSRLS role attribute), the permission check can be performed without recursively evaluating the same RLS policy, eliminating the infinite recursion. See Use Security Definer Functions for implementation details.