From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Palak Chaturvedi Date: Mon, 14 Sep 2026 15:33:01 +0000 Subject: [PATCH] buffermgr: recompute pin limit after resize MaxProportionalPins is a per-backend pin limit derived from NBuffers at InitBufferManagerAccess() time and never touched again. After a resize changes NBuffers, the stale value under- or over-estimates the limit for the rest of the backend's lifetime. Factor the computation out into RecomputeMaxProportionalPins() and call it both at init and from ProcessBarrierBufferPoolSize(), which is where every backend learns the buffer pool size actually changed. --- src/backend/storage/buffer/buf_resize.c | 1 + src/backend/storage/buffer/bufmgr.c | 11 ++++++++++- src/include/storage/bufmgr.h | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/buffer/buf_resize.c b/src/backend/storage/buffer/buf_resize.c index 90f5fb1c71d..d8b5a0ad845 100644 --- a/src/backend/storage/buffer/buf_resize.c +++ b/src/backend/storage/buffer/buf_resize.c @@ -503,6 +503,7 @@ ProcessBarrierBufferPoolSize(void) Assert(activeNBuffers == pg_atomic_read_u32(&BufferControl->activeNBuffers)); NBuffers = pg_atomic_read_u32(&BufferControl->currentNBuffers); + RecomputeMaxProportionalPins(); return true; } diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index bb436734585..92b245c6c43 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -275,6 +275,15 @@ static int PrivateRefCountEntryLast = -1; static uint32 MaxProportionalPins; +/* + * Recompute the per-backend pin limit after a buffer pool resize. + */ +void +RecomputeMaxProportionalPins(void) +{ + MaxProportionalPins = NBuffers / (MaxBackends + NUM_AUXILIARY_PROCS); +} + static void ReservePrivateRefCountEntry(void); static PrivateRefCountEntry *NewPrivateRefCountEntry(Buffer buffer); static PrivateRefCountEntry *GetPrivateRefCountEntry(Buffer buffer, bool do_move); @@ -4328,7 +4337,7 @@ InitBufferManagerAccess(void) * allow plenty of pins. LimitAdditionalPins() and * GetAdditionalPinLimit() can be used to check the remaining balance. */ - MaxProportionalPins = NBuffers / (MaxBackends + NUM_AUXILIARY_PROCS); + RecomputeMaxProportionalPins(); memset(&PrivateRefCountArray, 0, sizeof(PrivateRefCountArray)); memset(&PrivateRefCountArrayKeys, 0, sizeof(PrivateRefCountArrayKeys)); diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index 187e9b29a9e..db04a260870 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -272,6 +272,7 @@ extern Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags); +extern void RecomputeMaxProportionalPins(void); extern BlockNumber ExtendBufferedRelBy(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, -- 2.43.0