Search
 
SCRIPT & CODE EXAMPLE
 

SQL

postgres row expiration

CREATE TABLE expire_table (
    timestamp timestamp NOT NULL DEFAULT NOW(),
    name TEXT NOT NULL
);

INSERT INTO expire_table (name) VALUES ('a');
INSERT INTO expire_table (name) VALUES ('b');
INSERT INTO expire_table (name) VALUES ('c');

select * from expire_table;
         timestamp          | name 
----------------------------+------
 2014-09-26 15:33:43.243356 | a
 2014-09-26 15:33:45.222202 | b
 2014-09-26 15:33:47.347131 | c
(3 rows)

CREATE FUNCTION expire_table_delete_old_rows() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
  DELETE FROM expire_table WHERE timestamp < NOW() - INTERVAL '1 minute';
  RETURN NEW;
END;
$$;

CREATE TRIGGER expire_table_delete_old_rows_trigger
    AFTER INSERT ON expire_table
    EXECUTE PROCEDURE expire_table_delete_old_rows();

INSERT INTO expire_table (name) VALUES ('d');

select * from expire_table;
         timestamp          | name 
----------------------------+------
 2014-09-26 15:36:56.132596 | d
(1 row)
Comment

PREVIOUS NEXT
Code Example
Sql :: time mysql w3 
Sql :: sql transact create cursor with dynamic tables 
Sql :: insert new department and employee record 
Sql :: ring MySQL get the result data without columns names 
Sql :: update all linkedserver tables with openquery on db 
Sql :: merge query using linked server 
Sql :: oracle sql date summer time 
Sql :: postgre regex exactly 1 characters 
Sql :: Using Set<Id in Dynamic SOQL 
Sql :: mysql beautifier terminla 
Sql :: get create sql of hibernqte entity 
Sql :: check_username 
Sql :: a query to determine the version of a database 
Sql :: Time difference in hh:mm:ss 
Sql :: SQL FULL OUTER JOIN With AS Alias 
Sql :: restarting of postgresql server when not connecting to default port 
Sql :: kannst du deine finger trainieren 
Sql :: database db connection string format 
Sql :: fonction stockée pl/sql 
Sql :: SQL Server log file truncate - Source :NAYCode.com 
Sql :: meaning of localhost in mysql 
Sql :: dump sql databse import export 
Sql :: classement rang mysql 7 
Sql :: calcul age 
Sql :: generate series sqlserver 2005 
Sql :: coursera spark sql max count 
Sql :: SQL sum column resulting from query 
Sql :: What is the difference between the LIKE and REGEXP operators in mysql? 
Sql :: oracle winter time change 
Sql :: Reduce size of SQL server log file truncate - Source :NAYCode.com 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =