Consider the case in which two concurrent transactions modify a table.
(However, a transaction never conflicts with itself.
For example, it might acquire Once acquired, a lock is normally held till end of transaction.
For example, a common use of advisory locks is to emulate pessimistic locking strategies typical of so-called data management systems.
While a flag stored in a table could be used for the same purpose, advisory locks are faster, avoid table bloat, and are automatically cleaned up by the server at the end of the session.
There are two ways to acquire an advisory lock in : at session level or at transaction level.
Once acquired at session level, an advisory lock is held until explicitly released or the session ends.
The use of explicit locking can increase the likelihood of deadlocks, wherein two (or more) transactions each hold locks that the other wants.
For example, if transaction 1 acquires an exclusive lock on table A and then tries to acquire an exclusive lock on table B, while transaction 2 has already exclusive-locked table B and now wants an exclusive lock on table A, then neither one can proceed.
If it is not feasible to verify this in advance, then deadlocks can be handled on-the-fly by retrying transactions that abort due to deadlocks.
So long as no deadlock situation is detected, a transaction seeking either a table-level or row-level lock will wait indefinitely for conflicting locks to be released.
cannot safely be executed concurrently with other operations on the same table, so it obtains an exclusive lock on the table to enforce that.) To examine a list of the currently outstanding locks in a database server, use the ; the names of the lock modes are historical.