473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Trigger function at particular time after inserting record in a table

Hi all,
I'm new to Postgres, and I have a requirement something like after inserting the record in a table I need to remove the record from that same table after configured time interval. So can anyone please let me know how can I achieve this?

Thanks,
Bhanu.
Apr 4 '18 #1
1 1774
mcptr
5
Interesting use case. You will have to handle it in your application instead. There is no job scheduling in the database, and triggers are executed in the same transaction as the statement.

I'm attaching a little snippet. I create the table for my data, another table to copy the data into (using a trigger), I add a trigger function and create a trigger.
The trigger works as intended, but then I add a unique constraint on the table and try to insert an existing value, in which case the transaction will get aborted. As a next example I remove that constraint and raise exception from within the trigger ("boom"). You can clearly see that the trigger fails and the "commit" statement is never executed. The transaction gets rolled back instead.

You might achieve your goal by using listen/notify in postgres, but i believe the right approach will be to introduce job scheduling (or background jobs facility) at the application level.

Expand|Select|Wrap|Line Numbers
  1. test=> CREATE TABLE IF NOT EXISTS trigger_test(
  2. test(>        id SERIAL PRIMARY KEY,
  3. test(>        content TEXT NOT NULL
  4. test(> );
  5. CREATE TABLE
  6. test=> 
  7. test=> CREATE TABLE IF NOT EXISTS trigger_log(
  8. test(>        id SERIAL PRIMARY KEY,
  9. test(>        content TEXT NOT NULL,
  10. test(>        logtime TIMESTAMP NOT NULL DEFAULT NOW()
  11. test(> );
  12. CREATE TABLE
  13. test=> 
  14. test=> CREATE OR REPLACE FUNCTION trg_log_content()
  15. test-> RETURNS TRIGGER AS $$
  16. test$> BEGIN
  17. test$>  INSERT INTO trigger_log(content)
  18. test$> VALUES(NEW.content);
  19. test$> 
  20. test$>  RETURN NEW;
  21. test$> END $$
  22. test-> LANGUAGE plpgsql;
  23. CREATE FUNCTION
  24. test=> 
  25. test=> 
  26. test=> CREATE TRIGGER trigger_demo AFTER INSERT ON trigger_test
  27. test-> FOR EACH ROW EXECUTE PROCEDURE trg_log_content();
  28. CREATE TRIGGER
  29. test=> insert into trigger_test(content) values('one');
  30. INSERT 0 1
  31. test=> select * from trigger_log;
  32.  id | content |          logtime           
  33. ----+---------+----------------------------
  34.   1 | one     | 2018-04-22 19:16:29.357786
  35. (1 row)
  36.  
  37. test=> insert into trigger_test(content) values('two');
  38. INSERT 0 1
  39. test=> select * from trigger_log;
  40.  id | content |          logtime           
  41. ----+---------+----------------------------
  42.   1 | one     | 2018-04-22 19:16:29.357786
  43.   2 | two     | 2018-04-22 19:16:40.012248
  44. (2 rows)
  45.  
  46. test=> create unique index on trigger_test (content);
  47. CREATE INDEX
  48. test=> insert into trigger_test(content) values('two');
  49. ERROR:  duplicate key value violates unique constraint "trigger_test_content_idx"
  50. DETAIL:  Key (content)=(two) already exists.
  51. test=> drop index trigger_test_content_idx ;
  52. DROP INDEX
  53. test=> CREATE OR REPLACE FUNCTION trg_log_content()
  54. test-> RETURNS TRIGGER AS $$
  55. test$> BEGIN
  56. test$>  INSERT INTO trigger_log(content)
  57. test$> VALUES(NEW.content);
  58. test$> 
  59. test$>  RAISE EXCEPTION 'boom';
  60. test$>  RETURN NEW;
  61. test$> END $$
  62. test-> LANGUAGE plpgsql;
  63. CREATE FUNCTION
  64. test=> insert into trigger_test(content) values('two');
  65. ERROR:  boom
  66. CONTEXT:  PL/pgSQL function trg_log_content() line 6 at RAISE
  67.  
  68.  
Apr 22 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andre | last post by:
Hello all, In my database I have a table with records. (Yes I have!!) Last week a record is changed, but I want to know on which date/time that was. Is there a oracle field in the table (like...
2
by: Jules Alberts | last post by:
Hello everyone, Several columns in sereval tables in my DB should always be lowercase. I now have a simple function: create or replace function code_lower() returns trigger as ' begin...
0
by: Rajat Katyal | last post by:
----- Original Message ----- From: Rajat Katyal To: pgsql-jdbc@postgresql.org Sent: Saturday, January 31, 2004 10:15 AM Subject: Trigger function aborts the transaction on exception Hi:...
5
by: Peter Erickson | last post by:
I am running postgresql 7.4.2 and having problems creating a trigger function properly. I keep getting the following error: ERROR: OLD used in query that is not in rule I have a table called...
0
by: Dennis Gearon | last post by:
If I have the following table: create table my_table( start TIME, end TIME ); and then I wanted a function as a trigger that would compare any INSERTS or UPDATES to see if there was any...
1
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO...
2
by: Reshmi Jacob | last post by:
Hello, Can any one help me in creating a trigger to update system date into a table while inserting a record into that table. I tried it like this, it is showing error !!! The following error...
1
by: djdevx | last post by:
Dear all PostgreSQL xperts! Hi, I am newbie in PostgreSQL. I am currently developing an app that use PostgreSQL as a BackEnd Database. Now I am having problem with the function trigger since last...
2
by: Question123 | last post by:
Hi i have one database table Table1.which contains almost 20000000 recordes. record to this table are inserted through storedprocedure. storedprocedure takes parameter as "value", Beginningdate,...
2
by: whsdu | last post by:
This is a really simple question and I'v been working on it for 2 hours........ The table is: CREATE TABLE download ( id integer NOT NULL DEFAULT nextval('seq'::regclass), songid integer...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.