How to make a PostgreSQL trigger thread-safe 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;
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