Skip to content

Commit 4a0585d

Browse files
anarazelCommitfest Bot
authored andcommitted
bufmgr: Add BufferLockHeldByMe()
Author: Reviewed-by: Discussion: https://postgr.es/m/ Backpatch:
1 parent 3b759bb commit 4a0585d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5873,6 +5873,47 @@ IsBufferCleanupOK(Buffer buffer)
58735873
return false;
58745874
}
58755875

5876+
/*
5877+
* BufferLockHeldByMe - does the backend have the buffer locked?
5878+
*
5879+
* This likely should only be used for asserts etc.
5880+
*
5881+
* Note that this can only be called for non-temp buffers - there is no
5882+
* correct value to return for temporary buffers. One might think that just
5883+
* returning true for temp buffers would work, but the caller might assert
5884+
* that a lock is *not* held.
5885+
*/
5886+
bool
5887+
BufferLockHeldByMe(Buffer buffer, int mode)
5888+
{
5889+
BufferDesc *buf;
5890+
LWLockMode lwmode;
5891+
5892+
/*
5893+
* Can't hold a lock without a pin, there never should be uncertainty
5894+
* about having a pin.
5895+
*/
5896+
Assert(BufferIsPinned(buffer));
5897+
5898+
/* there'd be no correct value to return */
5899+
Assert(!BufferIsLocal(buffer));
5900+
5901+
buf = GetBufferDescriptor(buffer - 1);
5902+
5903+
if (mode == BUFFER_LOCK_EXCLUSIVE)
5904+
lwmode = LW_EXCLUSIVE;
5905+
else if (mode == BUFFER_LOCK_SHARE)
5906+
lwmode = LW_SHARED;
5907+
else
5908+
{
5909+
Assert(false);
5910+
pg_unreachable();
5911+
lwmode = LW_EXCLUSIVE; /* assuage compiler */
5912+
}
5913+
5914+
return LWLockHeldByMeInMode(BufferDescriptorGetContentLock(buf), lwmode);
5915+
}
5916+
58765917

58775918
/*
58785919
* 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
@@ -294,6 +294,7 @@ extern void LockBufferForCleanup(Buffer buffer);
294294
extern bool ConditionalLockBufferForCleanup(Buffer buffer);
295295
extern bool IsBufferCleanupOK(Buffer buffer);
296296
extern bool HoldingBufferPinThatDelaysRecovery(void);
297+
extern bool BufferLockHeldByMe(Buffer buffer, int mode);
297298

298299
extern bool BgBufferSync(struct WritebackContext *wb_context);
299300

0 commit comments

Comments
 (0)