LockAcquireExtended() dontWait vs weaker lock levels than already held

Поиск
Список
Период
Сортировка
От Andres Freund
Тема LockAcquireExtended() dontWait vs weaker lock levels than already held
Дата
Msg-id 20220322174318.u6xknglvu5y65vcy@alap3.anarazel.de
обсуждение исходный текст
Ответы Re: LockAcquireExtended() dontWait vs weaker lock levels than already held  (Robert Haas <robertmhaas@gmail.com>)
Список pgsql-hackers
Hi,

When LockAcquireExtended(dontWait=false) acquires a lock where we already hold
stronger lock and somebody else is also waiting for that lock, it goes through
a fairly circuitous path to acquire the lock:

A conflicting lock is detected: if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
LockAcquireExtended() -> WaitOnLock() -> ProcSleep()
ProcSleep() follows this special path:
     * Special case: if I find I should go in front of some waiter, check to
     * see if I conflict with already-held locks or the requests before that
     * waiter.  If not, then just grant myself the requested lock immediately.
and grants the lock.


However, in dontWait mode, there's no such path. Which means that
LockAcquireExtended() returns LOCKACQUIRE_NOT_AVAIL despite the fact that dontWait=false
would succeed in granting us the lock.

This seems decidedly suboptimal.

For one, the code flow to acquire a lock we already hold in some form is
unnecessarily hard to understand and expensive. There's no comment in
LockAcquireExtended() explaining that WaitOnLock() might immediately grant us
the lock in that case, we emit bogus TRACE_POSTGRESQL_LOCK_WAIT_START() etc.

For another, LockAcquireExtended(dontWait=true) returning spuriously is quite
confusing behaviour, and quite plausibly could cause bugs in fairly random
places.


Not planning to do anything about this for now, but I did want something I can
find if I hit such a problem in the future...


Greetings,

Andres Freund



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Maxim Orlov
Дата:
Сообщение: Re: XID formatting and SLRU refactorings (was: Add 64-bit XIDs into PostgreSQL 15)
Следующее
От: Andrew Dunstan
Дата:
Сообщение: Re: New Object Access Type hooks