2016-09-01 17 views
0

私たちはアクセス権を与えたテーブルに存在する行の総数で請求するPeriscopeを使用します。Redshift:特定のユーザーのすべてのテーブルの合計行数

私の質問は:ユーザperiscopeが与えられた場合、ユーザがperiscopeにアクセスできるテーブルの行の総数を取得するにはどうすればよいですか?

など。ユーザーならばperiscopeは2つのだけのテーブルにアクセスすることができます。私はすべてのテーブルを持って10

カウント2で数8とt2とt1は、総数があるべきこのanswer(「pg_tables」と「INFORMATION_SCHEMA」を除く)が、私は次に進む方法を知らない:

SELECT CAST(schemaname as varchar), CAST( objectname as varchar) 
FROM 
    (
    SELECT 
     schemaname 
     ,objectname 
     ,usename 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'select') AS sel 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'insert') AS ins 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'update') AS upd 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'delete') AS del 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'references') AS ref 
    FROM 
     (
     SELECT schemaname, 't' AS obj_type, tablename AS objectname, schemaname + '.' + tablename AS fullobj FROM pg_tables 
     WHERE schemaname not in ('pg_internal') 
     UNION 
     SELECT schemaname, 'v' AS obj_type, viewname AS objectname, schemaname + '.' + viewname AS fullobj FROM pg_views 
     WHERE schemaname not in ('pg_internal') 
     ) AS objs 
     ,(SELECT * FROM pg_user) AS usrs 
    ORDER BY fullobj 
    ) 
WHERE (sel = true or ins = true or upd = true or del = true or ref = true) 
and usename = 'periscope' 
and schemaname not in ('information_schema', 'pg_catalog'); 

あなたが優雅な解決策を持っているなら、私に教えてください!

答えて

1

これは、あなたが必要なものを与える必要があります。

SELECT SUM(b.reltuples) 
FROM 
(
    SELECT 
     schemaname 
     ,objectname 
     ,usename 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'select') AS sel 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'insert') AS ins 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'update') AS upd 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'delete') AS del 
     ,HAS_TABLE_PRIVILEGE(usrs.usename, fullobj, 'references') AS ref 
    FROM 
    (
     SELECT schemaname, 't' AS obj_type, tablename AS objectname, schemaname + '.' + tablename AS fullobj FROM pg_tables 
     WHERE schemaname not in ('pg_internal') 
     UNION 
     SELECT schemaname, 'v' AS obj_type, viewname AS objectname, schemaname + '.' + viewname AS fullobj FROM pg_views 
     WHERE schemaname not in ('pg_internal') 
     ) AS objs 
     ,(SELECT * FROM pg_user) AS usrs 
    ORDER BY fullobj 
) as a 
JOIN 
(
    SELECT 
     nspname AS schemaname,relname,reltuples 
    FROM pg_class C 
    LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) 
    ORDER BY reltuples DESC 
) as b 
ON (a.schemaname = b.schemaname AND a.objectname = b.relname) 
WHERE (sel = true or ins = true or upd = true or del = true or ref = true) 
and usename = 'periscope' 
and a.schemaname not in ('information_schema', 'pg_catalog'); 
関連する問題