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

Commitf2e6a2c

Browse files
committed
Add API to check if an existing exclusive lock allows cleanup.
LockBufferForCleanup() acquires a cleanup lock unconditionally, andConditionalLockBufferForCleanup() acquires a cleanup lock if it ispossible to do so without waiting; this patch adds a new API,IsBufferCleanupOK(), which tests whether an exclusive lock alreadyheld happens to be a cleanup lock. This is possible because a cleanuplock simply means an exclusive lock plus the assurance any other pinson the buffer are newer than our own pin. Therefore, just as theexisting functions decide that the exclusive lock that they've justtaken is a cleanup lock if they observe the pin count to be 1, thisnew function allows us to observe that the pin count is 1 on a bufferwe've already locked.This is useful in situations where a backend definitely wishes tomodify the buffer and also wishes to perform cleanup operations ifpossible. The patch to eliminate heavyweight locking by hash indexesuses this, and it may have other applications as well.Amit Kapila, per a suggestion from me. Some comment adjustments by meas well.
1 parent7016e4c commitf2e6a2c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,6 +3745,55 @@ ConditionalLockBufferForCleanup(Buffer buffer)
37453745
return false;
37463746
}
37473747

3748+
/*
3749+
* IsBufferCleanupOK - as above, but we already have the lock
3750+
*
3751+
* Check whether it's OK to perform cleanup on a buffer we've already
3752+
* locked. If we observe that the pin count is 1, our exclusive lock
3753+
* happens to be a cleanup lock, and we can proceed with anything that
3754+
* would have been allowable had we sought a cleanup lock originally.
3755+
*/
3756+
bool
3757+
IsBufferCleanupOK(Bufferbuffer)
3758+
{
3759+
BufferDesc*bufHdr;
3760+
uint32buf_state;
3761+
3762+
Assert(BufferIsValid(buffer));
3763+
3764+
if (BufferIsLocal(buffer))
3765+
{
3766+
/* There should be exactly one pin */
3767+
if (LocalRefCount[-buffer-1]!=1)
3768+
return false;
3769+
/* Nobody else to wait for */
3770+
return true;
3771+
}
3772+
3773+
/* There should be exactly one local pin */
3774+
if (GetPrivateRefCount(buffer)!=1)
3775+
return false;
3776+
3777+
bufHdr=GetBufferDescriptor(buffer-1);
3778+
3779+
/* caller must hold exclusive lock on buffer */
3780+
Assert(LWLockHeldByMeInMode(BufferDescriptorGetContentLock(bufHdr),
3781+
LW_EXCLUSIVE));
3782+
3783+
buf_state=LockBufHdr(bufHdr);
3784+
3785+
Assert(BUF_STATE_GET_REFCOUNT(buf_state)>0);
3786+
if (BUF_STATE_GET_REFCOUNT(buf_state)==1)
3787+
{
3788+
/* pincount is OK. */
3789+
UnlockBufHdr(bufHdr,buf_state);
3790+
return true;
3791+
}
3792+
3793+
UnlockBufHdr(bufHdr,buf_state);
3794+
return false;
3795+
}
3796+
37483797

37493798
/*
37503799
*Functions for buffer I/O handling

‎src/include/storage/bufmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ extern void LockBuffer(Buffer buffer, int mode);
227227
externboolConditionalLockBuffer(Bufferbuffer);
228228
externvoidLockBufferForCleanup(Bufferbuffer);
229229
externboolConditionalLockBufferForCleanup(Bufferbuffer);
230+
externboolIsBufferCleanupOK(Bufferbuffer);
230231
externboolHoldingBufferPinThatDelaysRecovery(void);
231232

232233
externvoidAbortBufferIO(void);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp