跳到正文
DEV Community· Sunnatulla·· 4 小時前AI 評分66

Supabase RLS 政策合併未提速,改用陣列並補齊索引後查詢降至 1.4 ms

Merging my Supabase RLS policies didn't make them faster. One other line did

AI 導讀

作者在 Supabase RLS 實測中發現,合併 permissive policies 本身沒有加速;在 Postgres 17、500k 行的測試中,改用 `= any(array(select fn()))` 並為 OR 各分支配置索引後,耗時由 85.8 ms 降至 1.4 ms。

正文

Supabase's Performance Advisor flags multiple_permissive_policies when a table has more than one permissive policy for the same role and action. The fix it suggests is to merge them into one policy with OR. I had a test table that hit that warning, so I merged the policies and timed it before and after.

Merging changed nothing. 85.8 ms with three policies, 84.6 ms with one. A different line in the same policy took it to 1.4 ms.

The setup

Postgres 17 (embedded-postgres 17.12), 500k rows in projects, 5,000 users, 2,000 teams. Each user owns 100 projects, belongs to one team (250 projects), and 1,000 projects are public. So a signed-in user should see 1,300 rows.

Three policies, the way most people write them:

create policy owners on projects for select to authenticated
  using ((select auth.uid()) = user_id);

create policy team on projects for select to authenticated
  using (team_id in (select public.user_team_ids()));

create policy pub on projects for select to authenticated
  using (is_public);

user_team_ids() is a security definer SQL function returning setof int, the same pattern the Supabase RLS docs use to avoid recursive policies (list_id in (select private.user_list_ids())). There are btree indexes on user_id and team_id.

I ran select count(*) from projects as authenticated with a JWT sub set, best of 5.

What I measured

policy time plan
3 policies, team_id in (select fn()) 85.8 ms Seq Scan
merged into 1 with OR, same in (select fn()) 84.6 ms Seq Scan
merged, plus a partial index on is_public 83.8 ms Seq Scan
merged, team_id = any(select fn()) 87.8 ms Seq Scan
merged, team_id = any(array(select fn())) + partial index 1.6 ms BitmapOr
3 separate policies, = any(array(select fn())) + partial index 1.4 ms BitmapOr
merged, = any(array(...)), no index on is_public 71.2 ms Seq Scan

The merge does nothing because Postgres already ORs permissive policies together. Three policies and one merged policy end up as the same filter in the plan:

Seq Scan on projects
  Filter: (((InitPlan 1).col1 = user_id) OR is_public
           OR (ANY (team_id = (hashed SubPlan 2).col1)))
  Rows Removed by Filter: 498700

The part that matters is hashed SubPlan. With in (select fn()) Postgres builds a hash of the team ids and checks every row against it. That's a filter, not an index condition, so the index on team_id is never used, and once one branch of the OR can't use an index, the whole OR falls back to a seq scan. I also tried the team policy alone, no OR at all: still a seq scan, 70.4 ms for 250 rows.

= any(array(select fn())) runs the function once as an InitPlan, gets an int[], and now team_id = ANY($array) is something a btree index can answer:

Bitmap Heap Scan on projects
  ->  BitmapOr
        ->  Bitmap Index Scan on projects_user_id_idx
              Index Cond: (user_id = (InitPlan 1).col1)
        ->  Bitmap Index Scan on projects_is_public_idx
              Index Cond: (is_public = true)
        ->  Bitmap Index Scan on projects_team_id_idx
              Index Cond: (team_id = ANY ((InitPlan 2).col1))

Note that = any(select fn()) without array(...) is just IN with different spelling. It got the same hashed SubPlan and 87.8 ms.

The last row of the table is the other catch. BitmapOr needs every branch of the OR to have an index. Without one on is_public the planner goes back to a seq scan even with the array fix (71.2 ms). A partial index is enough:

create index on projects (is_public) where is_public;

So should you merge?

Merge when it makes the rules easier to read, or when overlapping policies give away more access than you meant, which the advisor docs also point out. Don't expect it to make anything faster on its own. In my test the policy count didn't matter at all, three separate ones with the array form were the fastest row.

If a policy has column in (select some_function()), try column = any(array(select some_function())), make sure every OR branch has an index, and look at explain analyze as the authenticated role, not as postgres (which bypasses RLS and shows you a plan your users never get).

One limit: array(...) loads the whole id list into memory once per query. For a user in a few teams that's nothing. If a function can return tens of thousands of ids, measure that case separately.

The repro is one Node script (embedded-postgres + pg) that creates the table, seeds 500k rows, swaps the policies and prints each plan. If you want to run it on your own Postgres version, ask in the comments and I will paste it.

This post was written with AI tools. The numbers come from running the script above.

來源:DEV Community · dev.to