【发布时间】:2020-02-19 19:52:48
【问题描述】:
我有已启用行级安全性和相关政策的表 - 工作得非常好。
我的问题是,有时,基于某些条件,我想在函数执行期间绕过特定语句的策略。
类似:
...
statement 1
statement 2
if (some cond) then
disable rls temporarily
statement 3 -- mostly delete rows the user can't normally see
enable rls
else
statement 3
end if
我实现它的方式是创建一个函数check_cond,它返回一个布尔值评估some cond,并创建了一个额外的选择策略,调用这个check_cond。
它有效 - 但实际问题是查询 select * from tab 现在看起来像这样:
select * from tab where <original policy condition> or check_cond()
这个or check_cond() 导致postges 总是进行全表扫描,因为它无法评估预先计划的结果。
如果我能够在策略中编写“动态”代码,我将能够根据 check_cond() 的值添加/删除条件,但据我所知这是不可能的。
有什么聪明的方法可以让我在不牺牲性能的情况下暂时禁用 rls 或动态添加条件?
谢谢。
【问题讨论】:
标签: postgresql row-level-security