【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 not like '%\_t' and tablename not like '%\_ti' and tablename not like '%\_tmp')
and not tablename ~ '[p][0-9]{4}$'
and not tablename ~ '[p][0-9]{5}$'
and not tablename ~ '[others]{6}$'
and not tablename ~ '[other]{5}$'
and (tablename ~ '[0-9]{1}$' or tablename like '%\_bak')
;
i int8:=0;
BEGIN
OPEN cursor_tmp;
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 $$;