Skip to content

Commit 01261fb

Browse files
committed
Improve buffer manager API for backend pin limits.
Previously the support functions assumed that the caller needed one pin to make progress, and could optionally use some more, allowing enough for every connection to do the same. Add a couple more functions for callers that want to know: * what the maximum possible number could be, irrespective of currently held pins, for space planning purposes * how many additional pins they could acquire right now, without the special case allowing one pin, for callers that already hold pins and could already make progress even if no extra pins are available The pin limit logic began in commit 31966b1. This refactoring is better suited to read_stream.c, which will be adjusted to respect the remaining limit as it changes over time in a follow-up commit. It also computes MaxProportionalPins up front, to avoid performing divisions whenever a caller needs to check the balance. Reviewed-by: Andres Freund <[email protected]> (earlier versions) Discussion: https://postgr.es/m/CA%2BhUKGK_%3D4CVmMHvsHjOVrK6t4F%3DLBpFzsrr3R%2BaJYN8kcTfWg%40mail.gmail.com
1 parent 7c99dc5 commit 01261fb

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

src/backend/storage/buffer/bufmgr.c

+55-25
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ static int32 PrivateRefCountOverflowed = 0;
211211
static uint32 PrivateRefCountClock = 0;
212212
static PrivateRefCountEntry *ReservedRefCountEntry = NULL;
213213

214+
static uint32 MaxProportionalPins;
215+
214216
static void ReservePrivateRefCountEntry(void);
215217
static PrivateRefCountEntry *NewPrivateRefCountEntry(Buffer buffer);
216218
static PrivateRefCountEntry *GetPrivateRefCountEntry(Buffer buffer, bool do_move);
@@ -2097,43 +2099,62 @@ GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context)
20972099
return buf;
20982100
}
20992101

2102+
/*
2103+
* Return the maximum number of buffers that a backend should try to pin once,
2104+
* to avoid exceeding its fair share. This is the highest value that
2105+
* GetAdditionalPinLimit() could ever return. Note that it may be zero on a
2106+
* system with a very small buffer pool relative to max_connections.
2107+
*/
2108+
uint32
2109+
GetPinLimit(void)
2110+
{
2111+
return MaxProportionalPins;
2112+
}
2113+
2114+
/*
2115+
* Return the maximum number of additional buffers that this backend should
2116+
* pin if it wants to stay under the per-backend limit, considering the number
2117+
* of buffers it has already pinned. Unlike LimitAdditionalPins(), the limit
2118+
* return by this function can be zero.
2119+
*/
2120+
uint32
2121+
GetAdditionalPinLimit(void)
2122+
{
2123+
uint32 estimated_pins_held;
2124+
2125+
/*
2126+
* We get the number of "overflowed" pins for free, but don't know the
2127+
* number of pins in PrivateRefCountArray. The cost of calculating that
2128+
* exactly doesn't seem worth it, so just assume the max.
2129+
*/
2130+
estimated_pins_held = PrivateRefCountOverflowed + REFCOUNT_ARRAY_ENTRIES;
2131+
2132+
/* Is this backend already holding more than its fair share? */
2133+
if (estimated_pins_held > MaxProportionalPins)
2134+
return 0;
2135+
2136+
return MaxProportionalPins - estimated_pins_held;
2137+
}
2138+
21002139
/*
21012140
* Limit the number of pins a batch operation may additionally acquire, to
21022141
* avoid running out of pinnable buffers.
21032142
*
2104-
* One additional pin is always allowed, as otherwise the operation likely
2105-
* cannot be performed at all.
2106-
*
2107-
* The number of allowed pins for a backend is computed based on
2108-
* shared_buffers and the maximum number of connections possible. That's very
2109-
* pessimistic, but outside of toy-sized shared_buffers it should allow
2110-
* sufficient pins.
2143+
* One additional pin is always allowed, on the assumption that the operation
2144+
* requires at least one to make progress.
21112145
*/
21122146
void
21132147
LimitAdditionalPins(uint32 *additional_pins)
21142148
{
2115-
uint32 max_backends;
2116-
int max_proportional_pins;
2149+
uint32 limit;
21172150

21182151
if (*additional_pins <= 1)
21192152
return;
21202153

2121-
max_backends = MaxBackends + NUM_AUXILIARY_PROCS;
2122-
max_proportional_pins = NBuffers / max_backends;
2123-
2124-
/*
2125-
* Subtract the approximate number of buffers already pinned by this
2126-
* backend. We get the number of "overflowed" pins for free, but don't
2127-
* know the number of pins in PrivateRefCountArray. The cost of
2128-
* calculating that exactly doesn't seem worth it, so just assume the max.
2129-
*/
2130-
max_proportional_pins -= PrivateRefCountOverflowed + REFCOUNT_ARRAY_ENTRIES;
2131-
2132-
if (max_proportional_pins <= 0)
2133-
max_proportional_pins = 1;
2134-
2135-
if (*additional_pins > max_proportional_pins)
2136-
*additional_pins = max_proportional_pins;
2154+
limit = GetAdditionalPinLimit();
2155+
limit = Max(limit, 1);
2156+
if (limit < *additional_pins)
2157+
*additional_pins = limit;
21372158
}
21382159

21392160
/*
@@ -3575,6 +3596,15 @@ InitBufferManagerAccess(void)
35753596
{
35763597
HASHCTL hash_ctl;
35773598

3599+
/*
3600+
* An advisory limit on the number of pins each backend should hold, based
3601+
* on shared_buffers and the maximum number of connections possible.
3602+
* That's very pessimistic, but outside toy-sized shared_buffers it should
3603+
* allow plenty of pins. LimitAdditionalPins() and
3604+
* GetAdditionalPinLimit() can be used to check the remaining balance.
3605+
*/
3606+
MaxProportionalPins = NBuffers / (MaxBackends + NUM_AUXILIARY_PROCS);
3607+
35783608
memset(&PrivateRefCountArray, 0, sizeof(PrivateRefCountArray));
35793609

35803610
hash_ctl.keysize = sizeof(int32);

src/backend/storage/buffer/localbuf.c

+16
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,22 @@ GetLocalVictimBuffer(void)
286286
return BufferDescriptorGetBuffer(bufHdr);
287287
}
288288

289+
/* see GetPinLimit() */
290+
uint32
291+
GetLocalPinLimit(void)
292+
{
293+
/* Every backend has its own temporary buffers, and can pin them all. */
294+
return num_temp_buffers;
295+
}
296+
297+
/* see GetAdditionalPinLimit() */
298+
uint32
299+
GetAdditionalLocalPinLimit(void)
300+
{
301+
Assert(NLocalPinnedBuffers <= num_temp_buffers);
302+
return num_temp_buffers - NLocalPinnedBuffers;
303+
}
304+
289305
/* see LimitAdditionalPins() */
290306
void
291307
LimitAdditionalLocalPins(uint32 *additional_pins)

src/include/storage/bufmgr.h

+4
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ extern bool HoldingBufferPinThatDelaysRecovery(void);
290290

291291
extern bool BgBufferSync(struct WritebackContext *wb_context);
292292

293+
extern uint32 GetPinLimit(void);
294+
extern uint32 GetLocalPinLimit(void);
295+
extern uint32 GetAdditionalPinLimit(void);
296+
extern uint32 GetAdditionalLocalPinLimit(void);
293297
extern void LimitAdditionalPins(uint32 *additional_pins);
294298
extern void LimitAdditionalLocalPins(uint32 *additional_pins);
295299

0 commit comments

Comments
 (0)