Re: Asynchronous Trigger?

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Cory Tucker <cory(dot)tucker(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Asynchronous Trigger?
Date: 2018-03-30 05:49:38
Message-ID: 1522388978.2396.10.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thiemo Kellner 2018-03-30 07:59:12 Re: dblink: could not send query: another command is already in progress
Previous Message Laurenz Albe 2018-03-30 05:39:46 Re: dblink: could not send query: another command is already in progress