postgres row expiration

Solutions on MaxInterview for postgres row expiration by the best coders in the world

showing results for - "postgres row expiration"
Leyna
27 Mar 2019
1CREATE TABLE expire_table (
2    timestamp timestamp NOT NULL DEFAULT NOW(),
3    name TEXT NOT NULL
4);
5
6INSERT INTO expire_table (name) VALUES ('a');
7INSERT INTO expire_table (name) VALUES ('b');
8INSERT INTO expire_table (name) VALUES ('c');
9
10select * from expire_table;
11         timestamp          | name 
12----------------------------+------
13 2014-09-26 15:33:43.243356 | a
14 2014-09-26 15:33:45.222202 | b
15 2014-09-26 15:33:47.347131 | c
16(3 rows)
17
18CREATE FUNCTION expire_table_delete_old_rows() RETURNS trigger
19    LANGUAGE plpgsql
20    AS $$
21BEGIN
22  DELETE FROM expire_table WHERE timestamp < NOW() - INTERVAL '1 minute';
23  RETURN NEW;
24END;
25$$;
26
27CREATE TRIGGER expire_table_delete_old_rows_trigger
28    AFTER INSERT ON expire_table
29    EXECUTE PROCEDURE expire_table_delete_old_rows();
30
31INSERT INTO expire_table (name) VALUES ('d');
32
33select * from expire_table;
34         timestamp          | name 
35----------------------------+------
36 2014-09-26 15:36:56.132596 | d
37(1 row)
similar questions
queries leading to this page
postgres row expiration