Thread: Are advisory locks guaranteed to be First Come First Serve? And can the behavior be relied upon?
Are advisory locks guaranteed to be First Come First Serve? And can the behavior be relied upon?
From
Hemil Ruparel
Date:
I was playing with Advisory locks. I opened three connections to the database And obtained a lock with the same key. I noticed that the locks were obtained in the order of requests. I was wondering whether the locks are guaranteed to be First Come First Serve and can this behaviour be relied upon.
I read the docs here. But it didn't mention anything.
Thanks a lot for your time and efforts
Re: Are advisory locks guaranteed to be First Come First Serve? And can the behavior be relied upon?
From
Hemil Ruparel
Date:
Thanks a lot
On Thu 27 Aug, 2020, 12:23 PM Laurenz Albe, <laurenz.albe@cybertec.at> wrote:
On Thu, 2020-08-27 at 10:10 +0530, Hemil Ruparel wrote:
> I was playing with Advisory locks. I opened three connections to the database And obtained
> a lock with the same key. I noticed that the locks were obtained in the order of requests.
> I was wondering whether the locks are guaranteed to be First Come First Serve and can this
> behaviour be relied upon.
Yes - lock requests that block are queued, with later lock requests queued behind previous
ones. Once a lock gets free, the first in line gets it.
This is the same for all heavyweight locks in PostgreSQL.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
Re: Are advisory locks guaranteed to be First Come First Serve? And can the behavior be relied upon?
From
Tom Lane
Date:
Laurenz Albe <laurenz.albe@cybertec.at> writes: > On Thu, 2020-08-27 at 10:10 +0530, Hemil Ruparel wrote: >> I was playing with Advisory locks. I opened three connections to the database And obtained >> a lock with the same key. I noticed that the locks were obtained in the order of requests. >> I was wondering whether the locks are guaranteed to be First Come First Serve and can this >> behaviour be relied upon. > Yes - lock requests that block are queued, with later lock requests queued behind previous > ones. Once a lock gets free, the first in line gets it. > This is the same for all heavyweight locks in PostgreSQL. There is an exception: the queue order will be rearranged if we find that doing so is necessary to avoid a deadlock failure. See src/backend/storage/lmgr/README regards, tom lane