简体   繁体   中英

Delete record 36 hours after insert in postgres using Triggers

I've stored some PDF exports in the repository which are generated by the scheduler. After 36 hours I need to delete those PDF's.

Table1
id(pk of table2), file_type, data

table2
id, name, label, created_date, updated_date

Now how can I write a trigger which can delete the records from Table1 and Table2 after 36 hours.

I've written this but It is executing only when an Insert is done. I wanted it to run even when none of the even is occured.

CREATE OR REPLACE FUNCTION ContentResource_Delete() RETURNS trigger AS $ContentResource_Delete$

BEGIN
    delete from jicontentresource jicr USING  jiresource jir 
    where jicr.id = jir.id and jicr.file_type='pdf' and trunc(EXTRACT(EPOCH FROM now() - creation_date)/3600) >=1 ;
    delete from jiresource where name like '%.pdf' and trunc(EXTRACT(EPOCH FROM now() - creation_date)/3600) >=1 ;   
    RETURN NULL;
END;$ContentResource_Delete$ LANGUAGE plpgsql;

CREATE TRIGGER ContentResource_Delete AFTER INSERT ON jiresource FOR EACH ROW EXECUTE PROCEDURE ContentResource_Delete();

Hi all I got the solution, instead of using triggers I've used pgAgent and configured.
If anyone facing issues while scheduling apart from default database (ie postgres) then in the step, ConnectionType should be Remote , connection string should be IP Address instead of localhost , specify the password and provide the databasename . Refer the Image

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM