# Postgres error 42P17: infinite recursion detected in policy for relation "x"

> A row level security policy on a table queries the same table (directly or through another policy), so checking the policy triggers the policy again.

Source: https://scribase.com/postgres-errors/42p17

SQLSTATE class: Syntax Error or Access Rule Violation. Over PostgREST/supabase-js this maps to HTTP 500.

## Causes

- A policy on members that checks membership with select ... from members.
- Two tables whose policies each query the other.

## Fixes

- Move the lookup into a SECURITY DEFINER function owned by a role that bypasses RLS, with a fixed search_path, and call that function from the policy.
- Or store what the policy needs (such as the team id) in the user's JWT claims and read it with auth.jwt().

## Example

```sql
create function private.is_team_member(team uuid) returns boolean
language sql security definer set search_path = '' stable as $$
  select exists (select 1 from public.members m where m.team_id = team and m.user_id = auth.uid());
$$;

create policy "team reads" on members for select to authenticated
  using (private.is_team_member(team_id));
```

PostgREST answers 500 for this error, so it looks like a server failure in the client.