如何在redshift / postgresql中获取表或任何对象的DDL的全长

时间:2014-04-16 23:57:54

标签: amazon-redshift

队 我正在研究redshift(8.0.2)。我想在redshift中为任何对象类型(表/视图...)安装DDL命令。

我有一个。但它没有提供全文。

select s.userid,u.usename,s.starttime, s.type, rtrim(s.text) from svl_statementtext s, pg_user u
 where u.usesysid = s.userid
 and s.type = 'DDL' 
 and s.text like '%table11%' 
 order by s.starttime asc;

 userid |                                                             usename                                                              |         starttime          | type |                                                                                                   text
--------+----------------------------------------------------------------------------------------------------------------------------------+----------------------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    143 | user11                                                                                                                           | 2014-04-16 23:42:06.227296 | DDL  | CREATE TABLE table11 ( log_time date, user_name text, database_name text, process_id integer, connection_from text, session_id text, session_line_num bigint, command_tag text, session_start_time
    143 | user11                                                                                                                           | 2014-04-16 23:42:06.234987 | DDL  | CREATE TABLE table11 ( log_time date, user_name text, database_name text, process_id integer, connection_from text, session_id text, session_line_num bigint, command_tag text, session_start_time
(2 rows)
在Oracle中,我们有DDL_METADATA.GET_DDL pkg。它会给出全文。我想也一样。我试过STL_DDLTEXT。文字修剪得很多。

select xid, starttime, sequence, substring(text,1,40) as text
from stl_ddltext where  userid = 100 and text like '%table11%'  order by xid desc, sequence;
  xid   |         starttime          | sequence |                   text
--------+----------------------------+----------+------------------------------------------
 135475 | 2014-04-16 23:42:06.234987 |        0 | CREATE TABLE table11 ( log_time dat
 135475 | 2014-04-16 23:42:06.227296 |        0 | CREATE TABLE table11 ( log_time dat
(2 rows)

我对第一个查询输出有更多疑问。 “usename”的列长度太高。如何修剪它。如果我查询pg_user,它会在内部进行修剪。在oracle,我们可以为例如

" col <col_name> for a80  " 

第二个疑问:我得到2排。实际上我只创建了一个表。输出中有2行的原因是什么?

例如在物理postgre数据库中,如果我们想为一个函数生成任何ddl, 我们可以在下面使用。

在下面,函数名称为“add”

SELECT pg_catalog.pg_get_functiondef('add'::regproc);

像这样,我们在Redshift中有表格/视图的任何pkg吗?

谢谢

2 个答案:

答案 0 :(得分:10)

对于DDL:

答案 1 :(得分:3)

我没有在Redshift中找到提供此功能的单个功能。您可以使用pg_get_viewdef函数获取视图的完整定义:

SELECT 'create view '|| nc.nspname::information_schema.sql_identifier ||'.'|| c.relname::information_schema.sql_identifier ||' as '||
 pg_get_viewdef(c.oid)::information_schema.character_data AS view_definition
FROM pg_namespace nc, pg_class c, pg_user u
WHERE c.relnamespace = nc.oid AND u.usesysid = c.relowner AND c.relkind = 'v'::"char" 
AND nc.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema');

对于表格定义,我已将一个查询放在一起,但仍需要一些工作来填写注释行中所述的一些细节:

select tm.schemaname||'.'||tm.tablename, 'create table '||tm.schemaname||'.'||tm.tablename
||' ('
||cp.coldef
-- primary key
-- diststyle
-- dist key
||d.distkey
--sort key 
|| (select 
' sortkey(' ||substr(array_to_string(
                 array( select ','||cast(column_name as varchar(100))  as str from
                       (select column_name from information_schema.columns col where  col.table_schema= tm.schemaname and col.table_name=tm.tablename) c2
                        join 
                        (-- gives sort cols
                          select attrelid as tableid, attname as colname, attsortkeyord as sort_col_order from pg_attribute pa where 
                          pa.attnum > 0  AND NOT pa.attisdropped AND pa.attsortkeyord > 0
                        ) st on tm.tableid=st.tableid and c2.column_name=st.colname   order by sort_col_order
                      )
                ,'')
              ,2,10000) || ')'
)
||';'
from 
-- t  master table list
(
SELECT substring(n.nspname,1,100) as schemaname, substring(c.relname,1,100) as tablename, c.oid as tableid 
FROM pg_namespace n, pg_class c
WHERE n.oid = c.relnamespace 
AND nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema')
AND c.relname <> 'temp_staging_tables_1'
-- and c.relname in ('f_recipient_registration','ht_base_document','ht_folder','ht_logical_file','ht_transaction_addresses','ht_ysi_batch','ht_ysi_batch_messages','ht_ysi_files') 
) tm 
-- cp  creates the col params for the create string
join
(select 
substr(str,(charindex('QQQ',str)+3),(charindex('ZZZ',str))-(charindex('QQQ',str)+3)) as tableid
,substr(replace(replace(str,'ZZZ',''),'QQQ'||substr(str,(charindex('QQQ',str)+3),(charindex('ZZZ',str))-(charindex('QQQ',str)+3)),''),2,10000) as coldef
from
( select array_to_string(array(
SELECT  'QQQ'||cast(t.tableid as varchar(10))||'ZZZ'|| ','||column_name||' '|| decode(udt_name,'bpchar','char',udt_name) || decode(character_maximum_length,null,'', '('||cast(character_maximum_length as varchar(9))||')'   )
-- default
|| decode(substr(column_default,2,8),'identity','',null,'',' default '||column_default||' ')
-- nullable
|| decode(is_nullable,'YES',' NULL ','NO',' NOT NULL ') 
-- identity 
|| decode(substr(column_default,2,8),'identity',' identity('||substr(column_default,(charindex('''',column_default)+1), (length(column_default)-charindex('''',reverse(column_default))-charindex('''',column_default)   ) )  ||') ', '') as str 
from  
-- ci  all the col info
(
select cast(t.tableid as int), cast(table_schema as varchar(100)), cast(table_name as varchar(100)), cast(column_name as varchar(100)), 
cast(ordinal_position as int), cast(column_default as varchar(100)), cast(is_nullable as varchar(20)) , cast(udt_name as varchar(50))  ,cast(character_maximum_length as int),
sort_col_order  , decode(d.colname,null,0,1) dist_key 
from (select * from information_schema.columns c where  c.table_schema= t.schemaname and c.table_name=t.tablename) c
left join 
(-- gives sort cols
select attrelid as tableid, attname as colname, attsortkeyord as sort_col_order from  pg_attribute a where 
 a.attnum > 0  AND NOT a.attisdropped AND a.attsortkeyord > 0
) s on t.tableid=s.tableid and c.column_name=s.colname
left join 
-- gives dist col
(select attrelid as tableid, attname as colname from pg_attribute a where
  a.attnum > 0 AND NOT a.attisdropped  AND a.attisdistkey = 't'
) d on t.tableid=d.tableid and c.column_name=d.colname
order by ordinal_position
) ci 
-- for the working array funct
), '') as str
from 
(-- need tableid
SELECT substring(n.nspname,1,100) as schemaname, substring(c.relname,1,100) as tablename, c.oid as tableid 
FROM pg_namespace n, pg_class c
WHERE n.oid = c.relnamespace 
AND nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema')
) t 
-- for the agg functions that dont exist
-- ) group by table_schema, table_name 
)) cp on tm.tableid=cp.tableid
-- add in primary key query here
-- dist key
left join
(  select 
-- close off the col defs after the primary key 
')' ||
' distkey('|| cast(column_name as varchar(100)) ||')'  as distkey, t.tableid
from information_schema.columns c
join 
(-- need tableid
  SELECT substring(n.nspname,1,100) as schemaname, substring(c.relname,1,100) as tablename, c.oid as tableid 
  FROM pg_namespace n, pg_class c
  WHERE n.oid = c.relnamespace 
  AND nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema')
) t on c.table_schema= t.schemaname and c.table_name=t.tablename
join 
-- gives dist col
(select attrelid as tableid, attname as colname from pg_attribute a where
  a.attnum > 0 AND NOT a.attisdropped  AND a.attisdistkey = 't'
  ) d on t.tableid=d.tableid and c.column_name=d.colname    
) d on tm.tableid=d.tableid