我怎样才能确保一个函数只从一个特定的模式中选择?

How can I make sure a function only SELECTs from one specific schema?

我需要做什么来确保 sandbox.execute_any_query() 只有 SELECT 来自架构 sandbox 中的表?

create function sandbox.execute_any_query(_query text) returns json as 
$$
declare
    _result json;
begin
    execute format('select row_to_json(t) from (%s) t', _query) into _result;
    return _result;
end
$$
language plpgsql;

将函数标记为 SECURITY DEFINER 并确保函数所有者对 sandbox 以外的任何架构没有权限。

然后该函数将 运行 在所有者的用户上下文中,任何访问其他模式的尝试都会导致错误。