我需要确定当前为我的数据库中的某些物化视图授予的权限。
对表格或标准视图执行此操作的查询非常简单:
SELECT grantee, string_agg(privilege_type, ', ') AS privileges
FROM information_schema.table_privileges
WHERE table_schema = 'some_schema' AND table_name = 'some_table'
GROUP by grantee;
尽管如此,似乎并不是物化视图的类似表格。 PostgreSQL在哪里存储这些信息?
答案 0 :(得分:4)
Postgres system catalogs是有关安装和数据库的基本完整信息集。系统目录是最可靠的信息来源。 Information schema作为辅助功能基于系统目录,并提供与其他RDBM的兼容性:
信息模式在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)