pgsql: bufmgr: Don't lock buffer header in StrategyGetBuffer() - Mailing list pgsql-committers

From Andres Freund
Subject pgsql: bufmgr: Don't lock buffer header in StrategyGetBuffer()
Date
Msg-id E1v6bkN-000jrY-2f@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
bufmgr: Don't lock buffer header in StrategyGetBuffer()

Previously StrategyGetBuffer() acquired the buffer header spinlock for every
buffer, whether it was reusable or not. If reusable, it'd be returned, with
the lock held, to GetVictimBuffer(), which then would pin the buffer with
PinBuffer_Locked(). That's somewhat violating the spirit of the guidelines for
holding spinlocks (i.e. that they are only held for a few lines of consecutive
code) and necessitates using PinBuffer_Locked(), which scales worse than
PinBuffer() due to holding the spinlock.  This alone makes it worth changing
the code.

However, the main reason to change this is that a future commit will make
PinBuffer_Locked() slower (due to making UnlockBufHdr() slower), to gain
scalability for the much more common case of pinning a pre-existing buffer. By
pinning the buffer with a single atomic operation, iff the buffer is reusable,
we avoid any potential regression for miss-heavy workloads. There strictly are
fewer atomic operations for each potential buffer after this change.

The price for this improvement is that freelist.c needs two CAS loops and
needs to be able to set up the resource accounting for pinned buffers. The
latter is achieved by exposing a new function for that purpose from bufmgr.c,
that seems better than exposing the entire private refcount infrastructure.
The improvement seems worth the complexity.

Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com>
Discussion: https://postgr.es/m/fvfmkr5kk4nyex56ejgxj3uzi63isfxovp2biecb4bspbjrze7@az2pljabhnff

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/5e89985928795f243dc287210c2aa016dfd00bfe

Modified Files
--------------
src/backend/storage/buffer/bufmgr.c   |  45 +++++-----
src/backend/storage/buffer/freelist.c | 149 ++++++++++++++++++++++++----------
src/include/storage/buf_internals.h   |   4 +
3 files changed, 132 insertions(+), 66 deletions(-)


pgsql-committers by date:

Previous
From: Andres Freund
Date:
Subject: pgsql: bufmgr: fewer calls to BufferDescriptorGetContentLock
Next
From: David Rowley
Date:
Subject: pgsql: Make truncate_useless_pathkeys() consider WindowFuncs