Is it possible to find out whether, say ALTER TABLE ADD CONSTRAINT
will acquire a lock immediately, or will have to wait on a different session?
I think that the best solution is to try and see. For that purpose, set lock_timeout
to 1 millisecond, so that the ALTER TABLE
statement fails if it cannot acquire a lock immediately:
BEGIN; -- start transaction
SET LOCAL lock_timeout = 1; -- only for this transaction
ALTER TABLE ...
COMMIT; -- will rollback if there was an error