In PostgreSQL i want to create function and trigger which can delete old data of before 8 day from A table(which contain many rows) and insert this data into one new alias table. and i want to execute this trigger everyday.
1 Answers
2
votes
historical table creation:
create table b as select * from a where false;
moving data
begin;
insert into b select * from a where ts < (now() - '8 days'::interval);
delete from a where ts < (now() - '8 days'::interval);
end;
scheduling
- create a file a.sql and put the above transaction to it
- add to cron
psql -f a.sql
notes creating trigger that will delete data on each statement is a bad idea
begin; insert into at select ..where ts < now() - '8 days'::interval; delete from t where ts < now() - '8 days'::interval; end;- Vao Tsun