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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 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