如何在PostgreSQL中获取行策略列表?

时间:2016-06-29 15:35:21

标签: postgresql postgresql-9.5

PostgreSQL-Documentation描述了如何创建或删除行策略如何获取现有策略列表。 我的目的是通过使用pgAdmin知道是否存在影响表及其工作方式的行策略。

谢谢!

3 个答案:

答案 0 :(得分:3)

有一个目录视图可以保存您想要的内容:pg_policy。它有点模糊,但它能满足您的需求。

# \d pg_policy
      Table "pg_catalog.pg_policy"
    Column    |     Type     | Modifiers 
--------------+--------------+-----------
 polname      | name         | not null
 polrelid     | oid          | not null
 polcmd       | "char"       | not null
 polroles     | oid[]        | 
 polqual      | pg_node_tree | 
 polwithcheck | pg_node_tree | 
Indexes:
    "pg_policy_oid_index" UNIQUE, btree (oid)
    "pg_policy_polrelid_polname_index" UNIQUE, btree (polrelid, polname)

答案 1 :(得分:1)

比目录视图pg_policy更有帮助的是pg_policies。您可以看到它适用于哪个表,甚至是策略的作用(尽管这似乎会被截断)。

所以只需运行:select * from pg_policies

实际归结为此(感谢@jmelesky):

 SELECT n.nspname AS schemaname,
    c.relname AS tablename,
    pol.polname AS policyname,
        CASE
            WHEN pol.polpermissive THEN 'PERMISSIVE'::text
            ELSE 'RESTRICTIVE'::text
        END AS permissive,
        CASE
            WHEN pol.polroles = '{0}'::oid[] THEN string_to_array('public'::text, ''::text)::name[]
            ELSE ARRAY( SELECT pg_authid.rolname
               FROM pg_authid
              WHERE pg_authid.oid = ANY (pol.polroles)
              ORDER BY pg_authid.rolname)
        END AS roles,
        CASE pol.polcmd
            WHEN 'r'::"char" THEN 'SELECT'::text
            WHEN 'a'::"char" THEN 'INSERT'::text
            WHEN 'w'::"char" THEN 'UPDATE'::text
            WHEN 'd'::"char" THEN 'DELETE'::text
            WHEN '*'::"char" THEN 'ALL'::text
            ELSE NULL::text
        END AS cmd,
    pg_get_expr(pol.polqual, pol.polrelid) AS qual,
    pg_get_expr(pol.polwithcheck, pol.polrelid) AS with_check
   FROM pg_policy pol
     JOIN pg_class c ON c.oid = pol.polrelid
     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace;

答案 2 :(得分:0)