Skip to content

Commit 3c2b97b

Browse files
committed
bufmgr: Introduce FlushUnlockedBuffer
There were several copies of code locking a buffer, flushing its contents, and unlocking the buffer. It seems worth centralizing that into a helper function. Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com> Discussion: https://postgr.es/m/fvfmkr5kk4nyex56ejgxj3uzi63isfxovp2biecb4bspbjrze7@az2pljabhnff
1 parent 819dc11 commit 3c2b97b

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ static inline BufferDesc *BufferAlloc(SMgrRelation smgr,
534534
static bool AsyncReadBuffers(ReadBuffersOperation *operation, int *nblocks_progress);
535535
static void CheckReadBuffersOperation(ReadBuffersOperation *operation, bool is_complete);
536536
static Buffer GetVictimBuffer(BufferAccessStrategy strategy, IOContext io_context);
537+
static void FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln,
538+
IOObject io_object, IOContext io_context);
537539
static void FlushBuffer(BufferDesc *buf, SMgrRelation reln,
538540
IOObject io_object, IOContext io_context);
539541
static void FindAndDropRelationBuffers(RelFileLocator rlocator,
@@ -3927,11 +3929,8 @@ SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context)
39273929
* buffer is clean by the time we've locked it.)
39283930
*/
39293931
PinBuffer_Locked(bufHdr);
3930-
LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
39313932

3932-
FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
3933-
3934-
LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
3933+
FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
39353934

39363935
tag = bufHdr->tag;
39373936

@@ -4378,6 +4377,19 @@ FlushBuffer(BufferDesc *buf, SMgrRelation reln, IOObject io_object,
43784377
error_context_stack = errcallback.previous;
43794378
}
43804379

4380+
/*
4381+
* Convenience wrapper around FlushBuffer() that locks/unlocks the buffer
4382+
* before/after calling FlushBuffer().
4383+
*/
4384+
static void
4385+
FlushUnlockedBuffer(BufferDesc *buf, SMgrRelation reln,
4386+
IOObject io_object, IOContext io_context)
4387+
{
4388+
LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_SHARED);
4389+
FlushBuffer(buf, reln, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
4390+
LWLockRelease(BufferDescriptorGetContentLock(buf));
4391+
}
4392+
43814393
/*
43824394
* RelationGetNumberOfBlocksInFork
43834395
* Determines the current number of pages in the specified relation fork.
@@ -4967,9 +4979,7 @@ FlushRelationBuffers(Relation rel)
49674979
(buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
49684980
{
49694981
PinBuffer_Locked(bufHdr);
4970-
LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
4971-
FlushBuffer(bufHdr, srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
4972-
LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
4982+
FlushUnlockedBuffer(bufHdr, srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
49734983
UnpinBuffer(bufHdr);
49744984
}
49754985
else
@@ -5064,9 +5074,7 @@ FlushRelationsAllBuffers(SMgrRelation *smgrs, int nrels)
50645074
(buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
50655075
{
50665076
PinBuffer_Locked(bufHdr);
5067-
LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
5068-
FlushBuffer(bufHdr, srelent->srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
5069-
LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
5077+
FlushUnlockedBuffer(bufHdr, srelent->srel, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
50705078
UnpinBuffer(bufHdr);
50715079
}
50725080
else
@@ -5292,9 +5300,7 @@ FlushDatabaseBuffers(Oid dbid)
52925300
(buf_state & (BM_VALID | BM_DIRTY)) == (BM_VALID | BM_DIRTY))
52935301
{
52945302
PinBuffer_Locked(bufHdr);
5295-
LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED);
5296-
FlushBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
5297-
LWLockRelease(BufferDescriptorGetContentLock(bufHdr));
5303+
FlushUnlockedBuffer(bufHdr, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
52985304
UnpinBuffer(bufHdr);
52995305
}
53005306
else
@@ -6569,10 +6575,8 @@ EvictUnpinnedBufferInternal(BufferDesc *desc, bool *buffer_flushed)
65696575
/* If it was dirty, try to clean it once. */
65706576
if (buf_state & BM_DIRTY)
65716577
{
6572-
LWLockAcquire(BufferDescriptorGetContentLock(desc), LW_SHARED);
6573-
FlushBuffer(desc, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
6578+
FlushUnlockedBuffer(desc, NULL, IOOBJECT_RELATION, IOCONTEXT_NORMAL);
65746579
*buffer_flushed = true;
6575-
LWLockRelease(BufferDescriptorGetContentLock(desc));
65766580
}
65776581

65786582
/* This will return false if it becomes dirty or someone else pins it. */

0 commit comments

Comments
 (0)