【PostgreSQL】PostgreSQL批量删除多余的备份表

【PostgreSQL】PostgreSQL批量删除多余的备份表


PostgreSQL批量删除多余的备份表

/* PostgreSQL批量删除多余的备份表 */
DO $$
DECLARE 
  record_tmp RECORD;
  cursor_tmp CURSOR FOR
    select * from pg_tables t
     where schemaname = CURRENT_SCHEMA
       and tablename not like 'act\_%'
       and tablename not like 'tpl\_%'
       -- and tablename like 'tmc%'
       and (tablename not like '%\_t' and tablename not like '%\_ti' and tablename not like '%\_tmp')
       -- 把分区表过滤掉
       -- 查结尾不是"p+4位数字"的,例如:p2023
       and not tablename ~ '[p][0-9]{4}$'
       -- 查结尾不是"p+5位数字"的,例如:p20231
       and not tablename ~ '[p][0-9]{5}$'
       -- 查结尾6位字符不是"others"的
       and not tablename ~ '[others]{6}$'
       -- 查结尾5位字符不是"other"的
       and not tablename ~ '[other]{5}$'
       -- 查结尾是数字的表名
       and (tablename ~ '[0-9]{1}$' or tablename like '%\_bak')
    ;
  i int8:=0;
BEGIN
  -- 打开游标
  OPEN cursor_tmp;
  -- RAISE NOTICE 'i=%',i;
  LOOP
    -- 获取游标中的记录
    FETCH cursor_tmp INTO record_tmp;
    -- 没有找到更多数据时退出循环
    EXIT WHEN NOT FOUND;
    i:=i+1;
    EXECUTE 'DROP TABLE '||record_tmp.tablename||' CASCADE';
    RAISE NOTICE '% - DROP TABLE % CASCADE',i,record_tmp.tablename;
  END LOOP;
  CLOSE cursor_tmp;
END $$;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tzq@2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值