triggers in background

Posted on 2004-08-24
Medium Priority
Last Modified: 2012-05-05
Hi all,

in postgresql 7.x,

is there a way to run trigger in background mode ?

What I need is :
 - When a table insert/update/delete, my trigger is running immediately.
But I don't want to wait for trigger's result. My function/procedure will return, trigger will continue to run, even it successfull or not.
Scenerio is something like this.
- I call a function
- The functions inserts or updates a table.
- Trigger relaled to this table fired AFTER INSERT/UPDATE
- Trigger return RETURN NEW always.  But need to be do some works during.
I don't care these works ended or not.

In this scenerio, since trigger running,  my function which has been insert/update the table  does NOT return.

I I want it to be finished as soon as possible. after INSERT/UPDATE without waiting trigger's operation.

Question by:Ege
  • 3
  • 3

Expert Comment

ID: 11882965
My guess is that the answer to your question is no. I have encountered situations similar to this, and my approach has been to run an outside cron job periodically -- generally every 5 minutes or so -- and look for new work to be done. You can use your trigger to insert a row in a table that represents a queue of work to be done. The cron job then comes along and looks for records in that queue. If it finds one or more records waiting, it runs a script for each. As it finishes the work for a record, it deletes that record from the table, or it sets a flag indicating that the corresponding task has been accomplished. That strategy has worked well for me.

Author Comment

ID: 11884276
Guess is not enough to accept. ;)

However, I think your guess is true (I also guess like that)
But, there may be trick. So 250points question I asked.

If not, I hope to be added later. Just running an external program with linux like permissions. Nothing more. This is something like a trigger.
For non SQL operations to be done.
At least for my scenerio, I need it. Instead of running an external program which checks the database from time to time, running it just like a trigger will be better for my project.

PostgreSQL TRUSTED LANGUAGE C supports all functioanlity of C ?

If C language (since PHP as a postgresql language has too many limitations) has no limitation, then it is good.

For example, (not with plpgsql, but C),
I an URL inserted to TABLE,
then can I connect to the URL to get some information inside trigger?
with C like created trigger.

If so, then it is also good for my project. In fact, instead of not waiting triggers result, this POINT is most usufull.

Best regards.


Expert Comment

ID: 11884432
You could install PL/Python and use it inside your trigger. That could shell out to the OS and start an external command. That might be a way to accomplish what you want. I have no experience doing that, however.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 11884476
I don't know PL/Python.
I can use, C, PL/PgSQL, PHP only.

If I can do it by using PL/Python, then I can do it by using C!

or not.


Expert Comment

ID: 11884797
Yes -- I suspect you'll be able to do what you want using C.

Author Comment

ID: 11910286
you will got the points by giving a running example.

Or anybody who gave a running sample, please.


- eGe

Accepted Solution

gnudiff earned 500 total points
ID: 11911279
Meaybe a working sample from documentation will work?


It shows a complete C example of a trigger function along with neccessary SQL, for PostGres 7.4.
Adding neccessary calls to the trigger to exec some background task (or better pass a message to some daemon which accepts and processes those independently of PG application) should be trivial.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question