加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

列出PostgreSQL中物化视图的授权和权限

发布时间:2020-12-13 16:33:39 所属栏目:百科 来源:网络整理
导读:我需要确定在我的数据库中为某些物化视图授予的特权. 为表或标准视图执行此操作的查询非常简单: SELECT grantee,string_agg(privilege_type,',') AS privilegesFROM information_schema.table_privilegesWHERE table_schema = 'some_schema' AND table_name
我需要确定在我的数据库中为某些物化视图授予的特权.

为表或标准视图执行此操作的查询非常简单:

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在哪里存储这些信息?

Postgres system catalogs是有关安装和数据库的完整信息的基本信息.
Information schema作为辅助功能是基于系统目录,并提供与其他RDBM的兼容性.
系统目录是最可靠的信息来源.

您可以将information_schema.table_privileges视为基于pg_class和pg_roles的特定视图.
显然,在执行中必须有一个小错误(这是在正确的地方缺少一个字母的问题).

系统目录pg_class包含关于列relacl中所有权限的所有信息.

如果列为空,则所有者具有所有权限.

在acl字符串中用作用户名的空字符串表示公开.

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],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,'=|/') 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读