有没有办法在redshift中找到表创建日期?

Kam*_*ani 6 amazon-redshift

我在Amazon Redshift中查找表创建日期时遇到问题.我知道svv_table_info会提供有关该表的所有信息,但会提供创建日期.任何人都可以帮忙吗?

A21*_*21z 12

在 Redshift 中有一种获取表创建日期和时间的正确方法,它不基于查询日志:

SELECT
TRIM(nspname) AS schema_name,
TRIM(relname) AS table_name,
relcreationtime AS creation_time
FROM pg_class_info
LEFT JOIN pg_namespace ON pg_class_info.relnamespace = pg_namespace.oid
WHERE reltype != 0
AND TRIM(nspname) = 'my_schema';
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它不适用于非常旧的表。我能在我的集群中找到的最旧日期是 2018 年 11 月。也许pg_class_info在该日期之前没有记录表的创建日期。


小智 10

在Redshift中,您可以通过搜索svl_qlog中运行的任何create table sql的开始和停止时间来获取表的创建时间.您可以查看其他表以获取类似数据,但这种方式的问题是它只保留了几天(3 - 5).虽然每个人都希望元数据与表本身一起存储以进行查询.亚马逊建议保留此数据,以便将要保留的日志中的数据导出到S3.然后,在我看来,你可以导入这些文件S3返回到你想叫aws_table_history什么让这个特殊的数据,你永远保存永久表.

select * from svl_qlog where substring ilike 'create table%' order by starttime desc limit 100;

select * from stl_query a, stl_querytext b where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc limit 100; 
Run Code Online (Sandbox Code Playgroud)

或者像这样得到表名和日期:

select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, 
starttime as createdate 
from stl_query a, stl_querytext b 
where a.query = b.query and b.text ilike 'create table%' order by a.starttime desc;
Run Code Online (Sandbox Code Playgroud)

使用密钥将创建的表数据历史记录导出到创建的S3存储桶中.下面的select语句将输出创建的表名和创建它的日期时间.

使用要导出到S3的数据创建临时表.

create table temp_history as 
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate 
from stl_query a, stl_querytext b 
where a.query = b.query 
and b.text ilike 'create table%' order by a.starttime desc);
Run Code Online (Sandbox Code Playgroud)

然后将此表上传到S3.

unload ('select * from temp_history') 
to 's3://tablehistory' credentials 'aws_access_key_id=myaccesskey;aws_secret_access_key=mysecretkey' 
DELIMITER '|' NULL AS '' ESCAPE ALLOWOVERWRITE;
Run Code Online (Sandbox Code Playgroud)

在AWS Redshift中创建一个新表.

CREATE TABLE aws_table_history
(
tablename VARCHAR(150),
createdate DATETIME
);
Run Code Online (Sandbox Code Playgroud)

然后将其重新导入到自定义表中.

copy aws_table_history from 's3://tablehistory' credentials 'aws_access_key_id=MYKEY;aws_secret_access_key=MYID'
emptyasnull
blanksasnull
removequotes
escape
dateformat 'YYYY-MM-DD'
timeformat 'YYYY-MM-DD HH:MI:SS'
maxerror 20;
delimiter '|';
Run Code Online (Sandbox Code Playgroud)

我测试了这一切,它对我们有用.我希望这可以帮助一些人.最后一个更简单的方法是使用Talend Big Data Open Studio并创建一个新作业,获取组件tRedshiftRow并将以下SQL粘贴到其中.然后构建作业,您可以安排在任何您想要的环境中运行.bat(windows)或.sh(unix).

INSERT INTO temp_history 
(select split_part(split_part(b.text,'table ', 2), ' ', 1) as tablename, starttime as createdate 
from stl_query a, stl_querytext b 
where a.query = b.query 
and b.text ilike 'create table%' order by a.starttime desc);
COMMIT;
insert into historytable
select distinct s.* 
from temp_history s;
COMMIT;
--remove  duplicates 
DELETE FROM historytable USING historytable a2 
WHERE historytable.tablename = a2.tablename AND
historytable.createdate < a2.createdate;
COMMIT;
---clear everything from prestage
TRUNCATE temp_history;
COMMIT;
Run Code Online (Sandbox Code Playgroud)


Mas*_*aki 6

看起来没有办法在 Redshift 中获取表的创建时间戳。一种解决方法是使用 STL_DDLTEXT 表,该表记录 DDL 的历史记录,包括CREATE TABLE.

这是一个示例(test_table是表名):

dev=> select starttime, endtime, trim(text) as ddl from stl_ddltext where text ilike '%create%table%test_table%' order by endtime desc limit 1;
         starttime          |          endtime           |                                                               ddl
----------------------------+----------------------------+----------------------------------------------------------------------------------------------------------------------------------
 2016-04-25 05:38:11.666338 | 2016-04-25 05:38:11.674947 | CREATE TABLE "test_table" (id int primary key, value varchar(24));
(1 row)
Run Code Online (Sandbox Code Playgroud)

在上述情况下,starttimeorendtime将是表创建的时间戳test_table

笔记:

  • Redshift不会长期保留STL_DDLTEXT,所以你不能永久使用这种方式。
  • 如果表是通过重命名表名等其他方式创建的,则不能使用此方式。

  • 感谢您的建议。但是,redshift 在 stl.* 表中存储信息的时间并不长(最多 3 到 5 天)。因此,不太可能从中获得所有表的创建时间戳。尽管我们可以每天/每周将这些表转储到另一个永久表中,从而确保我们随时掌握这些信息。我正在寻找更具体的东西,比如这里提到的。http://stackoverflow.com/a/2577388/4330205..但是我在 redshift 中找不到 pg_ls_dir 的替代品。 (4认同)