From 4b1b378cd7d3b1c3f0366ccda0f1092de833774e Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Wed, 1 Jul 2020 19:06:45 -0700 Subject: [PATCH v4 02/15] Add some error checking around pinning --- src/include/storage/bufmgr.h | 1 + src/backend/storage/buffer/bufmgr.c | 42 ++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index b8a18b8081f..973547a8baf 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -134,6 +134,7 @@ extern void ReleaseBuffer(Buffer buffer); extern void UnlockReleaseBuffer(Buffer buffer); extern void MarkBufferDirty(Buffer buffer); extern void IncrBufferRefCount(Buffer buffer); +extern void BufferCheckOneLocalPin(Buffer buffer); extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum); diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 0a05577b68d..55df6a76c9c 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -1755,6 +1755,8 @@ PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy) bool result; PrivateRefCountEntry *ref; + Assert(!BufferIsLocal(b)); + ref = GetPrivateRefCountEntry(b, true); if (ref == NULL) @@ -1900,6 +1902,8 @@ UnpinBuffer(BufferDesc *buf) PrivateRefCountEntry *ref; Buffer b = BufferDescriptorGetBuffer(buf); + Assert(!BufferIsLocal(b)); + /* not moving as we're likely deleting it soon anyway */ ref = GetPrivateRefCountEntry(b, false); Assert(ref != NULL); @@ -4282,6 +4286,25 @@ ConditionalLockBuffer(Buffer buffer) LW_EXCLUSIVE); } +void +BufferCheckOneLocalPin(Buffer buffer) +{ + if (BufferIsLocal(buffer)) + { + /* There should be exactly one pin */ + if (LocalRefCount[-buffer - 1] != 1) + elog(ERROR, "incorrect local pin count: %d", + LocalRefCount[-buffer - 1]); + } + else + { + /* There should be exactly one local pin */ + if (GetPrivateRefCount(buffer) != 1) + elog(ERROR, "incorrect local pin count: %d", + GetPrivateRefCount(buffer)); + } +} + /* * LockBufferForCleanup - lock a buffer in preparation for deleting items * @@ -4309,20 +4332,11 @@ LockBufferForCleanup(Buffer buffer) Assert(BufferIsPinned(buffer)); Assert(PinCountWaitBuf == NULL); - if (BufferIsLocal(buffer)) - { - /* There should be exactly one pin */ - if (LocalRefCount[-buffer - 1] != 1) - elog(ERROR, "incorrect local pin count: %d", - LocalRefCount[-buffer - 1]); - /* Nobody else to wait for */ - return; - } + BufferCheckOneLocalPin(buffer); - /* There should be exactly one local pin */ - if (GetPrivateRefCount(buffer) != 1) - elog(ERROR, "incorrect local pin count: %d", - GetPrivateRefCount(buffer)); + /* Nobody else to wait for */ + if (BufferIsLocal(buffer)) + return; bufHdr = GetBufferDescriptor(buffer - 1); @@ -4823,6 +4837,8 @@ LockBufHdr(BufferDesc *desc) SpinDelayStatus delayStatus; uint32 old_buf_state; + Assert(!BufferIsLocal(BufferDescriptorGetBuffer(desc))); + init_local_spin_delay(&delayStatus); while (true) -- 2.38.0