Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit01261fb

Browse files
committed
Improve buffer manager API for backend pin limits.
Previously the support functions assumed that the caller needed one pinto make progress, and could optionally use some more, allowing enoughfor every connection to do the same. Add a couple more functions forcallers 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 availableThe pin limit logic began in commit31966b1. This refactoring isbetter suited to read_stream.c, which will be adjusted to respect theremaining limit as it changes over time in a follow-up commit. It alsocomputes MaxProportionalPins up front, to avoid performing divisionswhenever a caller needs to check the balance.Reviewed-by: Andres Freund <andres@anarazel.de> (earlier versions)Discussion:https://postgr.es/m/CA%2BhUKGK_%3D4CVmMHvsHjOVrK6t4F%3DLBpFzsrr3R%2BaJYN8kcTfWg%40mail.gmail.com
1 parent7c99dc5 commit01261fb

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

‎src/backend/storage/buffer/bufmgr.c

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ static int32 PrivateRefCountOverflowed = 0;
211211
staticuint32PrivateRefCountClock=0;
212212
staticPrivateRefCountEntry*ReservedRefCountEntry=NULL;
213213

214+
staticuint32MaxProportionalPins;
215+
214216
staticvoidReservePrivateRefCountEntry(void);
215217
staticPrivateRefCountEntry*NewPrivateRefCountEntry(Bufferbuffer);
216218
staticPrivateRefCountEntry*GetPrivateRefCountEntry(Bufferbuffer,booldo_move);
@@ -2097,43 +2099,62 @@ GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context)
20972099
returnbuf;
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+
returnMaxProportionalPins;
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+
uint32estimated_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+
return0;
2135+
2136+
returnMaxProportionalPins-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-
uint32max_backends;
2116-
intmax_proportional_pins;
2149+
uint32limit;
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
HASHCTLhash_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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,22 @@ GetLocalVictimBuffer(void)
286286
returnBufferDescriptorGetBuffer(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+
returnnum_temp_buffers;
295+
}
296+
297+
/* see GetAdditionalPinLimit() */
298+
uint32
299+
GetAdditionalLocalPinLimit(void)
300+
{
301+
Assert(NLocalPinnedBuffers <=num_temp_buffers);
302+
returnnum_temp_buffers-NLocalPinnedBuffers;
303+
}
304+
289305
/* see LimitAdditionalPins() */
290306
void
291307
LimitAdditionalLocalPins(uint32*additional_pins)

‎src/include/storage/bufmgr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ extern bool HoldingBufferPinThatDelaysRecovery(void);
290290

291291
externboolBgBufferSync(structWritebackContext*wb_context);
292292

293+
externuint32GetPinLimit(void);
294+
externuint32GetLocalPinLimit(void);
295+
externuint32GetAdditionalPinLimit(void);
296+
externuint32GetAdditionalLocalPinLimit(void);
293297
externvoidLimitAdditionalPins(uint32*additional_pins);
294298
externvoidLimitAdditionalLocalPins(uint32*additional_pins);
295299

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp