Slow Supabase queries with RLS – (select auth.uid()), indexes and the other fixes

By Abheesht Tripathi · Published

Table `public.notes` has a row level security policy `read own notes` that re-evaluates current_setting() or auth.<function>() for each row. This produces suboptimal query performance at scale.

What it means

That warning comes from Supabase's database linter (auth_rls_initplan, in the Performance Advisor). An RLS policy is an extra WHERE condition on every query. If it calls auth.uid() directly, Postgres may evaluate the function once for every row it looks at. On a table with a million rows that is a million function calls for one request, and it is a common reason for slow pages and statement timeout errors once data grows.

Fix 1: wrap auth functions in a select

Writing (select auth.uid()) makes Postgres run the function once per statement (an initPlan) and reuse the result. Supabase's docs recommend this for auth.uid(), auth.jwt() and your own security definer functions:

-- before
create policy "read own notes" on public.notes
  for select using (auth.uid() = user_id);

-- after
drop policy "read own notes" on public.notes;
create policy "read own notes" on public.notes
  for select to authenticated
  using ((select auth.uid()) = user_id);

Only do this when the result doesn't depend on the row. (select auth.uid()) is safe; wrapping an expression that uses columns of the row would change its meaning.

Fix 2: index every column a policy filters on

create index if not exists notes_user_id_idx on public.notes using btree (user_id);

With the index, user_id = (select auth.uid()) becomes an index lookup instead of a scan of the whole table. For a multi-column index, the policy column needs to come first to be used on its own.

Fix 3: name the role

Add to authenticated (or to anon) to each policy. A policy for another role is skipped without being evaluated, so anonymous requests don't pay for logged-in rules.

Fix 4: replace per-row joins with a set lookup

Team or organisation policies often look like exists (select 1 from members where members.team_id = docs.team_id and members.user_id = auth.uid()), which runs per row. Turn it around so the membership list is computed once:

create schema if not exists private;

create or replace function private.my_team_ids()
returns setof bigint
language sql
stable
security definer
set search_path = ''
as $$
  select team_id from public.members where user_id = (select auth.uid());
$$;

create policy "read team docs" on public.docs
  for select to authenticated
  using (team_id in (select private.my_team_ids()));

Keep the function in a schema the API doesn't expose, and index members(user_id) and docs(team_id).

Fix 5: fewer permissive policies, and filter in the query

The linter's Multiple Permissive Policies warning means several policies for the same role and action, each run for every relevant query. Merge them into one with or. Also add the obvious filter to your client query, for example .eq('user_id', user.id): RLS still enforces access, but the planner gets a clear condition to use an index with.

How to verify

Measure as the API role, before and after:

begin;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"<user-uuid>","role":"authenticated"}', true);
explain analyze select * from public.notes;
rollback;

After the fix the plan shows an InitPlan for auth.uid() and an index scan, and the execution time drops. The Performance Advisor no longer lists auth_rls_initplan for the table.

Related guides

More in Supabase RLS and Storage · all guides