DEV Community

Cover image for Why Your Supabase RLS Policies Are Slow (and 7 Fixes That Actually Work)
Hugo Rus
Hugo Rus

Posted on

Why Your Supabase RLS Policies Are Slow (and 7 Fixes That Actually Work)

RLS policies run as part of every query, so a policy that's fine on 1,000 rows can crawl on 1,000,000. The usual culprits: auth.uid() evaluated once per row, missing indexes on the columns policies filter by, queries without explicit filters, policies that apply to roles they don't need to, joins inside policies and several overlapping permissive policies. Measure with EXPLAIN ANALYZE as a real user, then apply the fixes below. Most are one-line changes.

RLS is the right way to secure Supabase. But because policies are invisible in your queries, their cost is invisible too, until a list screen that took 50 ms in development takes seconds in production.

How do RLS policies affect query performance?

Postgres adds your policy's using expression to every query on the table, like an extra WHERE clause. That means:

  • The expression is evaluated as the query runs, potentially once per row.
  • Whatever it filters on needs an index, like any WHERE clause.
  • Subqueries and joins inside policies run as part of every query.

So the same rules that make SQL fast apply to policies. You just can't see them in your application code.

How do I measure RLS performance?

Run the query as a real user, because RLS doesn't apply to the postgres role you use in the SQL editor:

begin;
set local role authenticated;
set local request.jwt.claims to '{"sub":"USER-UUID","role":"authenticated"}';

explain analyze
select * from public.projects order by created_at desc limit 20;

rollback;
Enter fullscreen mode Exit fullscreen mode

Look for sequential scans on large tables and for functions evaluated on every row. Also check Supabase's Performance Advisor in the dashboard: it flags several of the problems below automatically.

Fix 1: Wrap auth.uid() in a select

-- Slow: auth.uid() can be called for every row
using ( auth.uid() = user_id )

-- Fast: evaluated once per query and cached
using ( (select auth.uid()) = user_id )
Enter fullscreen mode Exit fullscreen mode

Wrapping the function in (select ...) lets Postgres treat the result as a constant for the whole query (an "initPlan"). The same applies to auth.jwt() and to your own functions that don't depend on the row. This is the single most common fix, and the Performance Advisor flags it.

Fix 2: Index the columns your policies use

If a policy filters on user_id, team_id or org_id, that column needs an index, or every query scans the whole table:

create index projects_user_id_idx on public.projects (user_id);
create index projects_team_id_idx on public.projects (team_id);
Enter fullscreen mode Exit fullscreen mode

If your app usually sorts or filters on other columns too, a composite index like (user_id, created_at desc) can serve both the policy and the query.

Fix 3: Add explicit filters to your queries

Even though RLS already restricts rows, add the same filter in your query:

// Relies only on RLS
const { data } = await supabase.from('projects').select('*');

// Faster: gives the planner a clear, indexable condition
const { data } = await supabase.from('projects').select('*').eq('user_id', userId);
Enter fullscreen mode Exit fullscreen mode

It looks redundant, but it helps the query planner pick the index instead of relying on the policy alone. RLS still protects the data either way.

Fix 4: Specify roles in every policy

create policy "Read own projects"
  on public.projects for select
  to authenticated
  using ( (select auth.uid()) = user_id );
Enter fullscreen mode Exit fullscreen mode

Without to authenticated, the policy applies to every role, including anon, and is evaluated for requests that could never match. Naming the role skips it entirely for others.

Fix 5: Rewrite joins inside policies

Team-based policies often join the membership table to the row being checked:

-- Slow: correlated subquery evaluated per row
using (
  (select auth.uid()) in (
    select user_id from public.team_members
    where team_members.team_id = projects.team_id
  )
)
Enter fullscreen mode Exit fullscreen mode

Flip it so the subquery runs once and produces a small set to match against:

-- Faster: get the user's teams once, then filter
using (
  team_id in (
    select team_id from public.team_members
    where user_id = (select auth.uid())
  )
)
Enter fullscreen mode Exit fullscreen mode

Make sure team_members has an index on user_id.

Fix 6: Use a security definer function for complex checks

When a check needs other RLS-protected tables, the nested policies run too, and they can be expensive or even recursive. A small security definer function can do the check once without triggering them:

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

create policy "Members read team projects"
  on public.projects for select to authenticated
  using ( team_id in (select public.user_team_ids()) );
Enter fullscreen mode Exit fullscreen mode

Keep these functions tiny, always set search_path = '', and make them answer questions only about the current user, since they bypass RLS.

Fix 7: Merge overlapping permissive policies

Policies are permissive by default: if a table has three select policies for the same role, Postgres evaluates all three and allows the row if any passes. Each one adds cost to every query.

-- Three separate policies for the same role and action...
-- "Owners can read", "Team members can read", "Public projects are readable"

-- ...can often become one:
create policy "Read projects"
  on public.projects for select to authenticated
  using (
    (select auth.uid()) = user_id
    or is_public
    or team_id in (select public.user_team_ids())
  );
Enter fullscreen mode Exit fullscreen mode

The Performance Advisor flags multiple permissive policies on the same table, role and action.

A quick RLS performance checklist

  1. auth.uid(), auth.jwt() and row-independent functions wrapped in (select ...).
  2. Indexes on every column policies filter by.
  3. Explicit filters in client queries that match the policy.
  4. to authenticated (or the right role) on every policy.
  5. Subqueries that fetch the user's IDs once, not per row.
  6. Security definer helpers for checks across protected tables.
  7. One permissive policy per table, role and action where possible.
  8. EXPLAIN ANALYZE run as a real user on your heaviest screens.

Don't trade security for speed

The tempting "fix" for a slow policy is loosening it, or using the service role key in the app. Both remove the protection RLS exists for. Every fix above keeps the same security rules and only changes how efficiently Postgres checks them.

Conclusion

Slow RLS is almost always ordinary SQL performance in disguise: functions evaluated per row, missing indexes, unhelpful query shapes and too many policies. Measure as a real user, apply the seven fixes and re-run EXPLAIN ANALYZE. Your policies stay just as strict, and your queries get fast again.

What was the slowest RLS policy you've fixed, and what was the cause? Share it in the comments.

Top comments (1)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

On fix 7, I checked on PG 17 with 500k rows: three permissive policies and the merged one give the same seq scan filter, since Postgres ORs them anyway (84 ms both). What changed it was team_id = any(array(select public.user_team_ids())) instead of in (select ...). Then it did a BitmapOr over the three indexes (is_public needs a partial one), 1.4 ms.