首页 > 解决方案 > 删除距当前时间 2 个月前最近访问的 Redshift 表

问题描述

这是尝试删除在过去 2 个月内未访问的表的存储过程。

Information_schema.tables 包含我们的 redshift 集群中存在的所有表。admin.stl_query_archive 包含迄今为止运行的所有查询。

我正在尝试查找过去 2 个月内运行的查询中不存在的所有表。

CREATE OR REPLACE PROCEDURE sp_drop_tables(out_var OUT varchar(256))
-- CREATE OR REPLACE PROCEDURE sp_drop_tables_()
AS $$
DECLARE
 r_c int := 0;
 rec record;
 tbl_rows int;
BEGIN   
  for rec in select distinct table_name from information_schema.tables
  loop
      raise info 'table_name = %', rec.table_name;
      tbl_rows := 0;
      select count(*) into tbl_rows from admin.stl_query_archive where endtime > getdate() - 61 and querytxt like '%'||quote_literal(rec.table_name)||'%';
      if tbl_rows = 0 then
        raise info 'this table is old';
      end if;
  end loop;
  select into out_var count(*) from admin.stl_query_archive;
END;
$$ LANGUAGE plpgsql;

call sp_drop_tables()
;

我的方法似乎是正确的,唯一的问题是它在运行时会引发以下错误 -

    Error [XX000 / 500310]: [Amazon](500310) Invalid operation: Assert 
----------------------------------------------- error: Assert code: 1000 context: false - Unknown 
constant type to hash: 17134 query: 2122914 location: cg_hash.cpp:84 process: padbmaster [pid=124705] 
-----------------------------------------------;

谁能帮我解决这个问题?非常感谢任何反馈!

标签: stored-proceduresamazon-redshift

解决方案


我认为您不需要在 LIKE 标准中使用 quote_literal 函数。


推荐阅读