postgresql/src
Andres Freund c75ebc657f bufmgr: Allow some buffer state modifications while holding header lock
Until now BufferDesc.state was not allowed to be modified while the buffer
header spinlock was held. This meant that operations like unpinning buffers
needed to use a CAS loop, waiting for the buffer header spinlock to be
released before updating.

The benefit of that restriction is that it allowed us to unlock the buffer
header spinlock with just a write barrier and an unlocked write (instead of a
full atomic operation). That was important to avoid regressions in
48354581a4. However, since then the hottest buffer header spinlock uses have
been replaced with atomic operations (in particular, the most common use of
PinBuffer_Locked(), in GetVictimBuffer() (formerly in BufferAlloc()), has been
removed in 5e89985928).

This change will allow, in a subsequent commit, to release buffer pins with a
single atomic-sub operation. This previously was not possible while such
operations were not allowed while the buffer header spinlock was held, as an
atomic-sub would not have allowed a race-free check for the buffer header lock
being held.

Using atomic-sub to unpin buffers is a nice scalability win, however it is not
the primary motivation for this change (although it would be sufficient). The
primary motivation is that we would like to merge the buffer content lock into
BufferDesc.state, which will result in more frequent changes of the state
variable, which in some situations can cause a performance regression, due to
an increased CAS failure rate when unpinning buffers.  The regression entirely
vanishes when using atomic-sub.

Naively implementing this would require putting CAS loops in every place
modifying the buffer state while holding the buffer header lock. To avoid
that, introduce UnlockBufHdrExt(), which can set/add flags as well as the
refcount, together with releasing the lock.

Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com>
Discussion: https://postgr.es/m/fvfmkr5kk4nyex56ejgxj3uzi63isfxovp2biecb4bspbjrze7@az2pljabhnff
2025-11-06 16:42:10 -05:00
..
backend bufmgr: Allow some buffer state modifications while holding header lock 2025-11-06 16:42:10 -05:00
bin Use XLogRecPtrIsValid() in various places 2025-11-06 20:33:57 +01:00
common Use C11 char16_t and char32_t for Unicode code points. 2025-10-29 14:17:13 -07:00
fe_utils Add \pset options for boolean value display 2025-11-03 17:40:39 +01:00
include bufmgr: Allow some buffer state modifications while holding header lock 2025-11-06 16:42:10 -05:00
interfaces libpq: Improve error handling in passwordFromFile() 2025-11-04 20:12:48 +09:00
makefiles Remove traces of support for Sun Studio compiler 2025-09-12 07:39:05 +02:00
pl Avoid memory leak in validation of a PL/Python trigger function. 2025-10-23 14:23:26 -04:00
port Fix POSIX compliance in pgwin32_unsetenv() for "name" argument 2025-10-21 08:05:28 +09:00
template Remove traces of support for Sun Studio compiler 2025-09-12 07:39:05 +02:00
test bufmgr: Allow some buffer state modifications while holding header lock 2025-11-06 16:42:10 -05:00
timezone Generate GUC tables from .dat file 2025-09-03 09:45:17 +02:00
tools ci: Improve OpenBSD core dump backtrace handling. 2025-11-06 21:14:05 +13:00
tutorial Fix compile of src/tutorial/funcs.c 2025-10-07 10:45:57 +02:00
.gitignore
DEVELOPERS
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
Makefile.global.in Remove traces of support for Sun Studio compiler 2025-09-12 07:39:05 +02:00
Makefile.shlib Use exported symbols list on macOS for loadable modules as well 2025-06-10 07:04:43 +02:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
nls-global.mk Fix update-po for the PGXS case 2025-10-16 20:21:05 +02:00