• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

triggers in background

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.


0
Ege
Asked:
Ege
  • 3
  • 3
1 Solution
 
rjkimbleCommented:
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.
0
 
EgeAuthor Commented:
Sorry,
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.

0
 
rjkimbleCommented:
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.
0
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!

 
EgeAuthor Commented:
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.

;(
0
 
rjkimbleCommented:
Yes -- I suspect you'll be able to do what you want using C.
0
 
EgeAuthor Commented:
you will got the points by giving a running example.

Or anybody who gave a running sample, please.

;)

- eGe
0
 
gnudiffCommented:
Meaybe a working sample from documentation will work?

http://www.postgresql.org/docs/current/static/trigger-example.html

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.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now