2

I want to make my PostgreSQL trigger function do something like incrementing counters etc (something complex with multiple queries etc) thread-safely (so the same function triggered concurrently won't mess things up), is it possible to do it with advisory lock like this?

BEGIN
    SELECT pg_advisory_xact_lock(1);
    -- do something thread-safely here
    ...
END;
-1

According to this post, all triggers execute as part of the same transaction as the original query, so you would not need a lock statement:

https://stackoverflow.com/questions/10727335/after-insert-trigger-in-separate-transaction

1
  • transactions usually don't provide thread-safety (depends on transaction isolation level) – Mary May 7 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.