pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Asynchronous trigger
11+ messages / 9 participants
[nested] [flat]

* Asynchronous trigger
@ 2006-09-06 21:29  Jean-Christophe Praud <jc@praud.com>
  0 siblings, 3 replies; 11+ messages in thread

From: Jean-Christophe Praud @ 2006-09-06 21:29 UTC (permalink / raw)
  To: pgsql-general

Hi all,

I'm planning to convert an application to postgresql 8.1 (from mysql).
Currently we have some recursive procedures done on the application side 
we would want to rewrite as stored pl/pgsql procedures called by triggers...

Is it possible for these triggers to be asynchronous, in order not to 
block the client application during the recursive process ?

Regards,


-- 
Jean-Christophe Praud         -      http://shub-niggurath.com
Conseil & Développement Informatique      http://www.praud.com
Ph'nglui mglw'nafh Cthulhu n'gah Bill R'lyeh Wgah'nagl fhtagn!




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous trigger
@ 2006-09-06 22:03  Jeff Davis <pgsql@j-davis.com>
  parent: Jean-Christophe Praud <jc@praud.com>
  2 siblings, 0 replies; 11+ messages in thread

From: Jeff Davis @ 2006-09-06 22:03 UTC (permalink / raw)
  To: Jean-Christophe Praud <jc@praud.com>; +Cc: pgsql-general

On Wed, 2006-09-06 at 23:29 +0200, Jean-Christophe Praud wrote:
> Hi all,
> 
> I'm planning to convert an application to postgresql 8.1 (from mysql).
> Currently we have some recursive procedures done on the application side 
> we would want to rewrite as stored pl/pgsql procedures called by triggers...
> 
> Is it possible for these triggers to be asynchronous, in order not to 
> block the client application during the recursive process ?
> 

You can send the query itself asynchronously using PQsendQuery (for
libpq, see your client library's docs for a similar function). That
allows your application to continue processing, but doesn't allow you to
issue any more queries over that connection. 

You could make your trigger send a notification to an application that
just handles that one task in the background.

Regards,
	Jeff Davis






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous trigger
@ 2006-09-07 04:15  Merlin Moncure <mmoncure@gmail.com>
  parent: Jean-Christophe Praud <jc@praud.com>
  2 siblings, 0 replies; 11+ messages in thread

From: Merlin Moncure @ 2006-09-07 04:15 UTC (permalink / raw)
  To: Jean-Christophe Praud <jc@praud.com>; +Cc: pgsql-general

On 9/6/06, Jean-Christophe Praud <jc@praud.com> wrote:
> Hi all,
>
> I'm planning to convert an application to postgresql 8.1 (from mysql).
> Currently we have some recursive procedures done on the application side
> we would want to rewrite as stored pl/pgsql procedures called by triggers...
>
> Is it possible for these triggers to be asynchronous, in order not to
> block the client application during the recursive process ?

Have you considered doing the recursion on the server side?

http://people.planetpostgresql.org/merlin/index.php?/archives/2-Dealing-With-Recursive-Sets-With-PLP...

merlin



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous trigger
@ 2006-09-07 11:47  Sim Zacks <sim@compulab.co.il>
  parent: Jean-Christophe Praud <jc@praud.com>
  2 siblings, 0 replies; 11+ messages in thread

From: Sim Zacks @ 2006-09-07 11:47 UTC (permalink / raw)
  To: pgsql-general

You want to use the Notify/Listen framework.
1) You need a rule on a table that says on Update (or insert or delete) 
do also notify {key}
2) You need a daemon running against the server that calls listen {key}
3) when the table is updated (or inserted or deleted) then your 
application will be able to check the table and see what needs to be done.

I just did this for a process that needs to update a denormalized 
statistics table every time the data changes and it seems to be working 
great. My query takes 2.5 seconds and the users didn't want to wait that 
long each time they modified a record. They also refused to tolerate it 
being updated every 10 minutes. They wanted it in as close to real time 
as it could be (with good reason, i might add). So the daemon calls a 
function and it works just fine.

Sim

Jean-Christophe Praud wrote:
> Hi all,
> 
> I'm planning to convert an application to postgresql 8.1 (from mysql).
> Currently we have some recursive procedures done on the application side 
> we would want to rewrite as stored pl/pgsql procedures called by 
> triggers...
> 
> Is it possible for these triggers to be asynchronous, in order not to 
> block the client application during the recursive process ?
> 
> Regards,
> 
> 



^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Asynchronous Trigger?
@ 2018-03-29 22:29  Cory Tucker <cory.tucker@gmail.com>
  0 siblings, 4 replies; 11+ messages in thread

From: Cory Tucker @ 2018-03-29 22:29 UTC (permalink / raw)
  To: pgsql-general

Is it possible to have the execution of a trigger (or any function) not
block the completion of the statement they are associated with?

A pattern I had hoped to implement was to do a quick update of rows that
signaled they needed attention, and then an async per-row trigger would
come and do the maintenance (in this case, make an expensive materialized
view).

Any suggestions welcome.

thanks!
--Cory

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous Trigger?
@ 2018-03-30 05:49  Laurenz Albe <laurenz.albe@cybertec.at>
  parent: Cory Tucker <cory.tucker@gmail.com>
  3 siblings, 0 replies; 11+ messages in thread

From: Laurenz Albe @ 2018-03-30 05:49 UTC (permalink / raw)
  To: Cory Tucker <cory.tucker@gmail.com>; pgsql-general

Cory Tucker wrote:
> Is it possible to have the execution of a trigger (or any function) not block the
> completion of the statement they are associated with?

Constraint triggers can be deferred to the end of the transaction, but I
am certain that is not what you are looking for.

> A pattern I had hoped to implement was to do a quick update of rows that signaled
> they needed attention, and then an async per-row trigger would come and do the
> maintenance (in this case, make an expensive materialized view).
> 
> Any suggestions welcome.

This seems like a really bad idea - even if it happens "in the background", would
you want an expensive materialized view to be updated whenever a row is modified?
If you modify three rows, that would happen three times in a row!

One solution would be to set a flag somewhere that marks the materialized view
dirty, and a regular cron-job that checks the flag and rebuilds the view.
With the asynchronous solution you had in mind, the materialized view would not
be up to date anyway.

Other than that, if your materialized view definition is simple enough, you might
be able to use a regular table and a trigger that updates the table to reflect
the modifications caused by the insert, update or delete.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous Trigger?
@ 2018-03-30 10:00  Adam Tauno Williams <awilliam@whitemice.org>
  parent: Cory Tucker <cory.tucker@gmail.com>
  3 siblings, 0 replies; 11+ messages in thread

From: Adam Tauno Williams @ 2018-03-30 10:00 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Thu, 2018-03-29 at 22:29 +0000, Cory Tucker wrote:
> Is it possible to have the execution of a trigger (or any function)
> not block the completion of the statement they are associated with?
> A pattern I had hoped to implement was to do a quick update of rows
> that signaled they needed attention, and then an async per-row
> trigger would come and do the maintenance (in this case, make an
> expensive materialized view).

This is a good use of a Notification.  A trigger can queue a
notification, which can be received by a listening process - which can
then determine and do whatever is necessary.

See the "LISTEN" documentation.


-- 
Adam Tauno Williams <mailto:awilliam@whitemice.org> GPG D95ED383
OpenGroupware Developer <http://www.opengroupware.us/;




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous Trigger?
@ 2018-04-27 10:46  Olleg Samoylov <splarv@ya.ru>
  parent: Cory Tucker <cory.tucker@gmail.com>
  3 siblings, 1 reply; 11+ messages in thread

From: Olleg Samoylov @ 2018-04-27 10:46 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>


  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    Try to look at PGQ from SkyTools.<br />
    <br />
    <div>On 2018-03-30 01:29, Cory Tucker wrote:<br />
    </div>
    <blockquote type="cite" cite="mid:CAG_=8kAdbc_DoJWy11xh5J8b+v-ZWSt3Ty-1Ffdn4scgq3wK6Q@mail.gmail.com">
      <div dir="ltr">
        <div>Is it possible to have the execution of a trigger (or any
          function) not block the completion of the statement they are
          associated with?</div>
        <div><br />
        </div>
        <div>A pattern I had hoped to implement was to do a quick update
          of rows that signaled they needed attention, and then an async
          per-row trigger would come and do the maintenance (in this
          case, make an expensive materialized view).</div>
        <div><br />
        </div>
        <div>Any suggestions welcome.</div>
        <div><br />
        </div>
        <div>thanks!</div>
        <div>--Cory</div>
      </div>
    </blockquote>
    <br />
  </div>





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous Trigger?
@ 2018-04-27 15:59  Merlin Moncure <mmoncure@gmail.com>
  parent: Cory Tucker <cory.tucker@gmail.com>
  3 siblings, 0 replies; 11+ messages in thread

From: Merlin Moncure @ 2018-04-27 15:59 UTC (permalink / raw)
  To: Cory Tucker <cory.tucker@gmail.com>; +Cc: pgsql-general

On Thu, Mar 29, 2018 at 5:29 PM, Cory Tucker <cory.tucker@gmail.com> wrote:
> Is it possible to have the execution of a trigger (or any function) not
> block the completion of the statement they are associated with?
>
> A pattern I had hoped to implement was to do a quick update of rows that
> signaled they needed attention, and then an async per-row trigger would come
> and do the maintenance (in this case, make an expensive materialized view).
>
> Any suggestions welcome.

Generally the idea is to (in the trigger) invoke some low risk quick
action such as inserting a record in to a 'stuff to do' table.  Then,
some other process comes around and does the heavy lifting.

merlin




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous Trigger?
@ 2018-04-27 17:25  Michael Loftis <mloftis@wgops.com>
  parent: Olleg Samoylov <splarv@ya.ru>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Loftis @ 2018-04-27 17:25 UTC (permalink / raw)
  To: Olleg Samoylov <splarv@ya.ru>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

As suggested, note in ToDo table, also maybe look at LISTEN and NOTIFY
and have a job runner process LISTENing (and cleaning up the queue,
or, marking an item as in progress if you've multiple workers)  The
work queue table is to help maintain state...if noone is LISTENing
then the table acts as backup so when a worker fires up it can start
working on whatever's already there and empty that.

On Fri, Apr 27, 2018 at 4:46 AM, Olleg Samoylov <splarv@ya.ru> wrote:
> Try to look at PGQ from SkyTools.
>
> On 2018-03-30 01:29, Cory Tucker wrote:
>
> Is it possible to have the execution of a trigger (or any function) not
> block the completion of the statement they are associated with?
>
> A pattern I had hoped to implement was to do a quick update of rows that
> signaled they needed attention, and then an async per-row trigger would come
> and do the maintenance (in this case, make an expensive materialized view).
>
> Any suggestions welcome.
>
> thanks!
> --Cory
>
>



-- 

"Genius might be described as a supreme capacity for getting its possessors
into trouble of all kinds."
-- Samuel Butler




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Asynchronous Trigger?
@ 2018-04-27 19:57  Adam Tauno Williams <awilliam@whitemice.org>
  parent: Michael Loftis <mloftis@wgops.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Adam Tauno Williams @ 2018-04-27 19:57 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Fri, 2018-04-27 at 11:25 -0600, Michael Loftis wrote:
> As suggested, note in ToDo table, also maybe look at LISTEN and
> NOTIFY and have a job runner process LISTENing 

We use a simple python process to listen with a PostgreSQL cursor for
NOTIFY events, and push them into RabbitMQ (message broker).  From
there you can do a lot of asynchronous work;  it will store messages,
optionally with expiration, you have point-to-point or broadcast,
etc...  It is a very durable, scalable, and flexible way to built up an
event driven system.

-- 
Meetings Coordinator, Michigan Association of Railroad Passengers
537 Shirley St NE Grand Rapids, MI 49503-1754 Phone: 616.581.8010
E-mail: awilliam@whitemice.org GPG#D95ED383 Web: http://www.marp.org




^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2018-04-27 19:57 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-09-06 21:29 Asynchronous trigger Jean-Christophe Praud <jc@praud.com>
2006-09-06 22:03 ` Jeff Davis <pgsql@j-davis.com>
2006-09-07 04:15 ` Merlin Moncure <mmoncure@gmail.com>
2006-09-07 11:47 ` Sim Zacks <sim@compulab.co.il>
2018-03-29 22:29 Asynchronous Trigger? Cory Tucker <cory.tucker@gmail.com>
2018-03-30 05:49 ` Re: Asynchronous Trigger? Laurenz Albe <laurenz.albe@cybertec.at>
2018-03-30 10:00 ` Re: Asynchronous Trigger? Adam Tauno Williams <awilliam@whitemice.org>
2018-04-27 10:46 ` Re: Asynchronous Trigger? Olleg Samoylov <splarv@ya.ru>
2018-04-27 17:25   ` Re: Asynchronous Trigger? Michael Loftis <mloftis@wgops.com>
2018-04-27 19:57     ` Re: Asynchronous Trigger? Adam Tauno Williams <awilliam@whitemice.org>
2018-04-27 15:59 ` Re: Asynchronous Trigger? Merlin Moncure <mmoncure@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox