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?

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

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:


  • transactions usually don't provide thread-safety (depends on transaction isolation level)
    – Mary
    May 7 '21 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.