20

If I set up an AFTER trigger in PostgreSQL to fire after an insert/update, will the calling software have to wait for the trigger to finish before returning control to the calling software? Or will the trigger run on its own behind the scenes?

iandouglas
  • 4,146
  • 2
  • 33
  • 33
  • 5
    If you need something to finish "behind the scenes" your trigger could use dblink_send_query http://www.postgresql.org/docs/current/static/contrib-dblink-send-query.html to send an async request in your trigger - depending on your requirements. – rfusca Jun 29 '10 at 17:20

1 Answers1

18

Yes, because it's executed within the same transaction. If the trigger fails, the insert/update will also fail. Just do a test executing a query that will fail (SELECT a table that does not exist) and you can see how things work and how your application will behave.

CREATE OR REPLACE FUNCTION foo() RETURNS TRIGGER
AS
$$
BEGIN
  EXECUTE 'SELECT fail';
END;
$$
LANGUAGE plpgsql;
Frank Heikens
  • 117,544
  • 24
  • 142
  • 135
  • 3
    is there a way to avoid this? Meaning the insert/update operation shouldn't fail even if trigger fails. – Zoran777 Jul 13 '17 at 09:46