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

triggers in background

Posted on 2004-08-24
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.
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.


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 250 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
round to  2 digit in postgres 3 311
Mass update Linked Tables in Access 7 524
PostgreSQL Syntax 12 143
Bubble user-defined Sql RAISERROR(...) to c# exception 14 189
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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