Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f2e6a2c

Browse files
committed
Add API to check if an existing exclusive lock allows cleanup.
LockBufferForCleanup() acquires a cleanup lock unconditionally, and ConditionalLockBufferForCleanup() acquires a cleanup lock if it is possible to do so without waiting; this patch adds a new API, IsBufferCleanupOK(), which tests whether an exclusive lock already held happens to be a cleanup lock. This is possible because a cleanup lock simply means an exclusive lock plus the assurance any other pins on the buffer are newer than our own pin. Therefore, just as the existing functions decide that the exclusive lock that they've just taken is a cleanup lock if they observe the pin count to be 1, this new function allows us to observe that the pin count is 1 on a buffer we've already locked. This is useful in situations where a backend definitely wishes to modify the buffer and also wishes to perform cleanup operations if possible. The patch to eliminate heavyweight locking by hash indexes uses this, and it may have other applications as well. Amit Kapila, per a suggestion from me. Some comment adjustments by me as well.
1 parent 7016e4c commit f2e6a2c

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(Buffer buffer)
3758+
{
3759+
BufferDesc *bufHdr;
3760+
uint32 buf_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
extern bool ConditionalLockBuffer(Buffer buffer);
228228
extern void LockBufferForCleanup(Buffer buffer);
229229
extern bool ConditionalLockBufferForCleanup(Buffer buffer);
230+
extern bool IsBufferCleanupOK(Buffer buffer);
230231
extern bool HoldingBufferPinThatDelaysRecovery(void);
231232

232233
extern void AbortBufferIO(void);

0 commit comments

Comments
 (0)