Team, I am working on redshift ( 8.0.2 ). I would like to have DDL command in place for any object type ( table / view...) in redshift.
I have below one. but it is not giving the full text.
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)
in Oracle we have DDL_METADATA.GET_DDL pkg. it will give the full text. I would like to have the same. I tried with STL_DDLTEXT. text is much trimmed.
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)
I have few more doubts on the first query output. the column lenght of "usename" is too high. how to trim that. If i query pg_user, it is trimmed internally. IN oracle we can have for e.g.
" col <col_name> for a80 "
second doubt: i am getting 2 rows. actually i created only one table. Any reason for 2 rows in the output ?
for e.g. in physical postgre db, if we want to generate any ddl for one function, we can use below.
in the below, function name is "add"
SELECT pg_catalog.pg_get_functiondef('add'::regproc);
like this, do we have any pkg in Redshift for table/views ?
Thanks
For DDL:
First create the admin view here: https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_generate_tbl_ddl.sql
Next write a query like this:
I have not found a single function in Redshift that provides this functionality. You can get the full definition of views by using the pg_get_viewdef function:
For table definitions I've put together a query, but it still needs a little work to fill in some details as noted in the commented lines: