小编Cal*_*mer的帖子

与内联版本相比,Postgres 行级安全策略优化不佳

我有一个看起来像这样的查询:

  SELECT post.id, post.author_id, post.published_at, post.content
    FROM post
   WHERE post.group_id = 1
ORDER BY post.published_at DESC, post.id
   LIMIT 5;
Run Code Online (Sandbox Code Playgroud)

(group_id, published_at DESC, id)当没有使用行级别安全性 (RLS) 策略时,此查询具有一个索引,该索引为其提供此查询计划。

 Limit  (cost=0.14..1.12 rows=5 width=143)
   ->  Index Scan using post_published_at on post  (cost=0.14..15.86 rows=80 width=143)
         Index Cond: (group_id = 1)
Run Code Online (Sandbox Code Playgroud)

然后我添加这个策略:

CREATE POLICY select_member_of ON post FOR SELECT USING
  (EXISTS (SELECT 1
             FROM group_member
            WHERE group_member.account_id = current_setting('current_account_id', false)::INT AND
                  group_member.group_id = post.group_id));
Run Code Online (Sandbox Code Playgroud)

有在化合物主键group_member.account_idgroup_member.group_idgroup_member表中。

我希望Postgres的计划此查询为仅索引扫描的group_member,因为这两个group_member.account_id和 …

postgresql performance execution-plan row-level-security explain query-performance

5
推荐指数
1
解决办法
605
查看次数