gpt4 book ai didi

postgresql - 列出 PostgreSQL 中物化 View 的授权和权限

转载 作者:行者123 更新时间:2023-11-29 11:14:11 24 4
gpt4 key购买 nike

我需要确定当前为我的数据库中的某些具体化 View 授予了哪些权限。

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

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;

就是说,物化 View 似乎没有类似的表。 PostgreSQL 在哪里存储这些信息?

最佳答案

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

The information schema is defined in the SQL standard and can therefore be expected to be portable and remain stable — unlike the system catalogs, which are specific to PostgreSQL and are modeled after implementation concerns. The information schema views do not, however, contain information about PostgreSQL-specific features; to inquire about those you need to query the system catalogs or other PostgreSQL-specific views.

物化 View 不是 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)

关于postgresql - 列出 PostgreSQL 中物化 View 的授权和权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38728548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com