skip to content

advisory locks

locks on numbers you make up. a mutex for cron jobs, and the session-level one that outlives your transaction.

address
everything.transactions.advisory_locks
part
part 11, transactions and isolation
chapter
chapter 11 of 11
reading time
6 min

an advisory lock is a lock on a number you pick. postgres keeps it in the same lock manager as its own locks, but never takes one for you and never checks it against any row: it means whatever your code agrees it means. pg_advisory_lock holds it for the session, through COMMIT and ROLLBACK, until you unlock or disconnect. pg_advisory_xact_lock holds it until the transaction ends. pg_try_advisory_lock never waits: it answers true or false.

the picture

you know the cron job that runs on three app servers, and the night it ran three times and sent everyone three invoices? you need a mutex that all three servers can see. they all talk to the same database, so the database can hold it.

think of a numbered "busy" sign at a reception desk (the lock manager). nobody checks what you do while you hold sign 42. the only rule is that two people can't hold it at once, and everyone agrees that sign 42 means "the nightly report".

A
·
B
·
1
SELECT pg_try_advisory_lock(42)→ t
lock manager
  • AExclusiveLockadvisory 42

the nightly report starts on server A. it asks for advisory lock 42 without waiting: true, A has it.

step 1/8
real output, a postgres 18.6 server · advisory-cron
step through. A takes lock 42 and B's try comes back false, so B skips the run. later, both use the waiting form: B's pg_advisory_lock waits in the lock manager, exactly like a row lock, until A unlocks.

the lock list on the right is pg_locks. advisory locks are ordinary entries there, with locktype = 'advisory', so the usual tools for watching locks see them, and a wait for one shows up as a wait:

advisory 42heldA ExclusivequeueB Exclusive
the moment B waits: A holds advisory lock 42, B is queued behind it. same lock manager, same queue, as the table locks.

the three questions each function answers

functionif someone else holds itreleased at
pg_advisory_lock(k)waitspg_advisory_unlock(k), or the session ending
pg_try_advisory_lock(k)returns false at oncethe same
pg_advisory_xact_lock(k)waitsCOMMIT or ROLLBACK, nothing else
pg_try_advisory_xact_lock(k)returns false at onceCOMMIT or ROLLBACK

each has a _shared twin: shared holders don't block each other, only an exclusive one. the key is one bigint or two ints. here's one taken, seen in pg_locks, and given back:

sqlempty db
SELECT pg_advisory_lock(42);
SELECT locktype, classid, objid, objsubid, mode, granted
FROM pg_locks WHERE locktype = 'advisory';
SELECT pg_advisory_unlock(42);
SELECT count(*) AS advisory_locks FROM pg_locks WHERE locktype = 'advisory';
output, from postgres 18.6
#1 · SELECT 1
pg_advisory_lockvoid
''
(1 row)
#2 · SELECT 1
locktypetextclassidoidobjidoidobjsubidint2modetextgrantedbool
advisory0421ExclusiveLockt
(1 row)
#3 · SELECT 1
pg_advisory_unlockbool
t
(1 row)
#4 · SELECT 1
advisory_locksint8
0
(1 row)

the one that outlives your transaction

a session-level lock doesn't belong to a transaction at all. take it inside BEGIN, roll back, and you still have it:

predict the output
T1 runs BEGIN; SELECT pg_advisory_lock(7); ROLLBACK. then T2 runs SELECT pg_try_advisory_lock(7). what does T2 get?
sql
/* T1 */ BEGIN;
/* T1 */ SELECT pg_advisory_lock(7);
/* T1 */ ROLLBACK;
/* T2 */ SELECT pg_try_advisory_lock(7);  -- ?
T1 takes =
T1
·
T2
·
1
BEGIN
lock manager
  • no locks

T1 opens a transaction.

step 1/6
real output, a postgres 18.6 server · advisory-session
switch the function. with pg_advisory_lock, T1's lock survives its ROLLBACK and T2 is refused until T1 unlocks. with pg_advisory_xact_lock, the ROLLBACK takes it away and T2's next try succeeds.

the same thing in the browser's one session, both kinds at once:

sqlempty db
BEGIN;
SELECT pg_advisory_lock(7);
SELECT pg_advisory_xact_lock(8);
ROLLBACK;
SELECT objid AS key, mode FROM pg_locks
WHERE locktype = 'advisory' ORDER BY objid;
output, from postgres 18.6
#1 · BEGIN
#2 · SELECT 1
pg_advisory_lockvoid
''
(1 row)
#3 · SELECT 1
pg_advisory_xact_lockvoid
''
(1 row)
#4 · ROLLBACK
#5 · SELECT 1
keyoidmodetext
7ExclusiveLock
(1 row)

key 7 is still held, key 8 went with the transaction. session-level locks also stack: take one twice and you have to unlock it twice. and unlocking one you don't hold returns false, with a warning:

sqlempty db
SELECT pg_advisory_lock(42);
SELECT pg_advisory_lock(42);
SELECT pg_advisory_unlock(42);
SELECT count(*) AS still_held FROM pg_locks WHERE locktype = 'advisory';
SELECT pg_advisory_unlock(42);
SELECT pg_advisory_unlock(42);
output, from postgres 18.6
#1 · SELECT 1
pg_advisory_lockvoid
''
(1 row)
#2 · SELECT 1
pg_advisory_lockvoid
''
(1 row)
#3 · SELECT 1
pg_advisory_unlockbool
t
(1 row)
#4 · SELECT 1
still_heldint8
1
(1 row)
#5 · SELECT 1
pg_advisory_unlockbool
t
(1 row)
#6 · SELECT 1
WARNING: you don't own a lock of type ExclusiveLock
pg_advisory_unlockbool
f
(1 row)

picking the number

the key is just a number, shared by the whole database. two features that both pick 42 will block each other and never know why. so make the key from a name: hashtextextended('nightly-report', 0) turns a string into a bigint, and the two-int form lets you put a feature number in the first half and a row id in the second (pg_advisory_xact_lock(7, account_id): "only one payout per account at a time", without locking the account row itself).

sqlempty db
SELECT hashtextextended('nightly-report', 0) AS key,
       pg_try_advisory_lock(hashtextextended('nightly-report', 0)) AS got_it;
output, from postgres 18.6
SELECT 1
keyint8got_itbool
-1761082894366742330t
(1 row)

keylines

  • an advisory lock is a lock on a number. postgres enforces only that two can't hold it at once.
  • pg_advisory_lock waits; pg_try_advisory_lock returns true or false at once.
  • session-level locks survive COMMIT and ROLLBACK. they last until unlock or disconnect.
  • _xact locks end with the transaction. prefer them behind connection pools.
  • derive keys from names, and keep one key space for the whole database.