我认为这很简单,但我找不到在 Redshift 中列出所有表及其创建者(或所有者)的方法。欢迎任何帮助/见解。
这是 pg_tables 表,这是 SQL:
select tablename, tableowner From pg_tables
您可以通过运行以下脚本列出 Redshift 表、视图及其所有者:
SELECT n.nspname AS schema_name
, pg_get_userbyid(c.relowner) AS table_owner
, c.relname AS table_name
, CASE WHEN c.relkind = 'v' THEN 'view' ELSE 'table' END
AS table_type
, d.description AS table_description
FROM pg_class As c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
LEFT JOIN pg_description As d
ON (d.objoid = c.oid AND d.objsubid = 0)
WHERE c.relkind IN('r', 'v')
ORDER BY n.nspname, c.relname ;
如果您要从特定模式搜索视图和过程源代码,您可以使用以下查询。我发现它非常有用。
SELECT
n.nspname AS schema_name
, 'view' AS object_type
, c.relname AS object_name
, pg_get_userbyid(c.relowner) AS object_owner
, v.view_definition AS object_definition
FROM
pg_class AS c
LEFT JOIN pg_namespace n
ON n.oid = c.relnamespace
LEFT JOIN information_schema.VIEWS v
ON c.relname = v.table_name
WHERE
c.relkind IN ('v') /* view */
AND n.nspname = '<schema_name>'
AND REGEXP_INSTR(v.view_definition, 'regular expression search string here') > 0
UNION
SELECT
n.nspname AS schema_name
, 'procedure' AS object_type
, p.proname AS object_name
, pg_get_userbyid(p.proowner) AS object_owner
, p.prosrc AS object_definition
FROM
pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p
ON pronamespace = n.oid
JOIN pg_user b
ON b.usesysid = p.proowner
WHERE
n.nspname = '<schema_name>'
AND REGEXP_INSTR(p.prosrc, 'regular expression search string here') > 0
ORDER BY 1,2,3,4