Discussion:
Advisory lock grant order
Yossi Cohen
2014-09-09 09:44:10 UTC
Permalink
Hi,

If I request an advisory lock (pg_advisory_lock) with the same key from
several sessions; will the lock be granted in the same order as it was
requested?
I.e. if for example:

session 1: select pg_advisory_lock(1); -- acquires the lock
then
session 2: select pg_advisory_lock(1); -- blocks waiting for the lock
then
session 3: select pg_advisory_lock(1); -- blocks waiting for the lock

then
session 1: select pg_advisory_unlock(1); -- releases the lock

Is it guaranteed that now session 2 will be granted the lock because it
requested the lock before session 3?

Thanks,
Yossi
Tom Lane
2014-09-09 13:28:11 UTC
Permalink
Post by Yossi Cohen
If I request an advisory lock (pg_advisory_lock) with the same key from
several sessions; will the lock be granted in the same order as it was
requested?
Usually. IIRC, the lock code will grant locks out-of-order if a deadlock
would result without it. There might be some other exceptions but I
don't recall any.

regards, tom lane
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Loading...