在Postgres中,
系统目录是有关安装和数据库的基本完整信息。系统目录是最可靠的信息来源。
信息模式作为辅助功能基于系统目录,并提供与其他RDBMs的兼容性。
信息模式在SQL标准中定义,因此可以期望其具有可移植性并保持稳定性 - 与系统目录不同,后者特定于PostgreSQL,并且基于实现考虑进行建模。然而,信息模式视图不包含有关PostgreSQL特定功能的信息; 要查询这些信息,您需要查询系统目录或其他PostgreSQL特定视图。
材料化视图不是SQL标准对象,因此信息模式不包含有关它们的信息。
系统目录
pg_class
包含列
relacl
中权限的所有信息。
如果列为
null
,则所有者具有所有权限。
在
acl
字符串中,用户名为空字符串表示
public
。
create materialized view test_view as select 1;
grant select on test_view to public;
grant delete on test_view to a_user;
select
coalesce(nullif(s[1], ''), 'public') as grantee,
s[2] as privileges
from
pg_class c
join pg_namespace n on n.oid = relnamespace
join pg_roles r on r.oid = relowner,
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
regexp_split_to_array(acl, '=|/') s
where nspname = 'public' and relname = 'test_view';
grantee | privileges
postgres | arwdDxt
public | r
a_user | d
(3 rows)
你需要一个函数来以易读的格式显示权限:
create or replace function priviliges_from_acl(text)
returns text language sql as $$
select string_agg(privilege, ', ')
from (
select
case ch
when 'r' then 'SELECT'
when 'w' then 'UPDATE'
when 'a' then 'INSERT'
when 'd' then 'DELETE'
when 'D' then 'TRUNCATE'
when 'x' then 'REFERENCES'
when 't' then 'TRIGGER'
end privilege
from
regexp_split_to_table($1, '') ch
) s
$$;
使用:
select
coalesce(nullif(s[1], ''), 'public') as grantee,
priviliges_from_acl(s[2]) as privileges
from
pg_class c
join pg_namespace n on n.oid = relnamespace
join pg_roles r on r.oid = relowner,
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
regexp_split_to_array(acl, '=|/') s
where nspname = 'public' and relname = 'test_view';
grantee | privileges
postgres | INSERT, SELECT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER
public | SELECT
a_user | DELETE
(3 rows)