Re: [HACKERS] Two pass CheckDeadlock in contentent case

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Ashutosh Bapat <ashutosh(dot)bapat(at)enterprisedb(dot)com>
Cc: Yura Sokolov <funny(dot)falcon(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [HACKERS] Two pass CheckDeadlock in contentent case
Date: 2018-07-24 19:19:24
Message-ID: CA+TgmobPju0gsFQ+jopoakzC=m6Ew59SAvkZ5RsJcs2M4VULdA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Jul 23, 2018 at 5:14 AM, Ashutosh Bapat
<ashutosh(dot)bapat(at)enterprisedb(dot)com> wrote:
> I think the case I am talking about is somewhere between these two -
> the backend which has imprecisely decided that there's a deadlock will
> start taking exclusive locks and will have to wait for all the
> backends with shared locks to release there locks. This will never
> happen if there is a stream of backends which keeps on taking shared
> locks. This is classical problem of starvation because of lock
> upgrade. Taking exclusive lock to begin with avoids this problem.

I don't quite see how this could happen in this case. IIUC, if a
backend takes the shared locks and finds that there is a deadlock, it
will then take exclusive locks. If it takes the shared locks and
finds that there is no deadlock, then it will just wait to be woken
up. After some small fraction of a second, every backend in the
system that was taking short locks will have taken them and either
started waiting for an exclusive lock or on the process semaphore.
It's not like ProcArrayLock or CLogControlLock where there can be an
endless stream of new requests.

>>> /*
>>> * And release locks. We do this in reverse order for two reasons: (1)
>>> * Anyone else who needs more than one of the locks will be trying to lock
>>> * them in increasing order; we don't want to release the other process
>>> * until it can get all the locks it needs.
>
> Your patch is breaking this assumption. We may end up in a situation
> where more than two backends have shared locks. One of the backend
> starts releasing its locks and releases even the first lock. Some
> different backend gets its first exclusive lock because of that but
> can not get the next one since it's held by the second backend holding
> shared locks. This might not happen if by "more than one" the comment
> means all the locks.

I don't see the problem. We can't get the first exclusive lock until
all processes have released their shared locks, and since they release
in decreasing order, nobody holds any lock at that point. Unless I'm
confused?

I think this patch is probably a good idea in some form. Whether it's
got all the details exactly right I'm not sure.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2018-07-24 19:27:40 Re: BUG #15293: Stored Procedure Triggered by Logical Replication is Unable to use Notification Events
Previous Message David G. Johnston 2018-07-24 18:57:44 Re: Stored procedures and out parameters