Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a PostgreSQL trigger asynchronous?

Tags:

postgresql

Sorry but I am new to SQL triggers and need some help.

I am after creating a trigger on UPDATE to one of my PostgreSQL tables. The trigger calls DELETE on a different table. My question is: Is the trigger portion of the request asynchronous or synchronous to the UPDATE request?

For more information here are my trigger and function:

CREATE OR REPLACE FUNCTION expire_table() RETURNS trigger AS $expire_table$
  BEGIN
     DELETE FROM object_store WHERE p_time < NOW() - INTERVAL '6 months';
     RETURN NEW;
  END;
$expire_table$ LANGUAGE 'plpgsql';

CREATE TRIGGER expire_table_trigger
  AFTER UPDATE ON objects
EXECUTE PROCEDURE expire_table();

My hope is that the trigger portion is asynchronous or that there is a way of making it so.

like image 851
mrmannione Avatar asked Jul 30 '18 15:07

mrmannione


1 Answers

The trigger is always executed in the same transaction as the statement, so it must needs be synchronous. No way around that.

Instead of a trigger, you might consider using a queue that a worker process handles asynchronously or an architecture that uses LISTEN/NOTIFY.

like image 61
Laurenz Albe Avatar answered Oct 10 '22 16:10

Laurenz Albe