Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Deleting data that are older than an hour, and then inserting new data to the same table

Tags:

sql

postgresql

I wrote a function called insert_offset_data(text, double precision) as the following:

BEGIN
INSERT INTO tempoffset(id, location, offset_factor, ts_insert) 
VALUES (uuid_generate_v4(), location_in, offset_in, (now() at time zone 'utc'));

RETURN 1;
END;

However, since this function is used in an API call whenever users insert data from an iOS application, I would love to delete data that are older than 1 hour in the table before inserting new data to it since the iOS application doesn't take into account data that are older than an hour. How do I write code to delete old data before I insert a new data in the same function?

like image 200
Sai Wai Maung Avatar asked Dec 19 '22 11:12

Sai Wai Maung


1 Answers

Add this:

DELETE FROM tempoffset WHERE ts_insert < now()-'1 hour'::interval;
like image 127
Dwayne Towell Avatar answered Apr 13 '23 00:04

Dwayne Towell