在 PostgreSQL 中,我想创建一个函数和触发器,它可以从一个表(包含很多行)中删除 8 天之前的旧数据,并将这些数据插入到一个新的别名表中。我想每天执行这个触发器。
最佳答案
历史表创建:
create table b as select * from a where false;
移动数据
begin;
insert into b select * from a where ts < (now() - '8 days'::interval);
delete from a where ts < (now() - '8 days'::interval);
end;
调度
- 创建一个文件a.sql,将上面的交易放入其中
- 添加到 cron
psql -f a.sql
笔记 创建将删除每个语句上的数据的触发器是个坏主意
关于sql - 创建 postgresql 触发器或函数以从一个表中删除旧数据并插入到别名表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42995933/