LWLockAcquire and LockBuffer mode argument

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: LWLockAcquire and LockBuffer mode argument
Date: 2020-08-24 22:34:58
Message-ID: 20200824223458.2uscftzqrw6acaud@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Currently lwlock acquisition, whether direct, or via the LockBuffer()
wrapper, have a mode argument. I don't like that much, for three
reasons:

1) I've tried to add annotations for static analyzers to help with
locking correctness. The ones I looked at don't support annotating
shared/exclusive locks where the mode is specified as a variable.
2) When doing performance analysis it's quite useful to be able to see
the difference between exlusive and shared acquisition. Typically all
one has access to is the symbol name though.
3) I don't like having the unnecessary branches for the lock mode, after
all a lot of the lock protected code is fairly hot. It's pretty
unnecessary because the caller almost (?) always uses a static lock
mode.

Therefore I'd like to replace the current lock functions with ones where
the lock mode is specified as part of the function name rather than an
argument.

To avoid unnecessary backward compat pains it seems best to first
introduce compat wrappers using the current signature, and then
subsequently replace in-core callers with the direct calls.

There's several harder calls though:
1) All of the above would benefit from lock release also being annotated
with the lock mode. That'd be a lot more invasive however. I think
it'd be best to add explicit functions (which would just assert
held_lwlocks[] being correct), but keep a wrapper that determines the
current lock level using held_lwlocks.

2) For performance it'd be nice if we could move the BufferIsLocal()
checks for LockBuffer* into the caller. Even better would be if we
made them inline wrappers around
LWLockAcquire(Shared|Exclusive). However, as the latter would require
making BufferDescriptorGetContentLock() available in bufmgr.h I think
that's not worth it. So I think we'd be best off having
LockBufferExclusive() be a static inline wrapper doing the
BufferIsLocal() check and then calling LockBufferExclusiveImpl
which'd do the LWLockAcquireExclusive().

Thoughts?

Greetings,

Andres Freund

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2020-08-24 23:19:02 Re: proposal - function string_to_table
Previous Message Andres Freund 2020-08-24 21:21:27 Re: Continuing instability in insert-conflict-specconflict test