aio: Add some error checking around pinning.
authorAndres Freund <[email protected]>
Thu, 2 Jul 2020 02:06:45 +0000 (19:06 -0700)
committerAndres Freund <[email protected]>
Mon, 11 Jan 2021 23:09:14 +0000 (15:09 -0800)
src/backend/storage/buffer/bufmgr.c
src/include/storage/bufmgr.h

index 8cb455df382ebde75677f621bdac81ac7a305205..c5395c3061f4be00758fc9c22cf06e0ac7cb85bb 100644 (file)
@@ -1595,6 +1595,8 @@ PinBuffer(BufferDesc *buf, BufferAccessStrategy strategy)
    bool        result;
    PrivateRefCountEntry *ref;
 
+   Assert(!BufferIsLocal(b));
+
    ref = GetPrivateRefCountEntry(b, true);
 
    if (ref == NULL)
@@ -1742,6 +1744,8 @@ UnpinBuffer(BufferDesc *buf, bool fixOwner)
    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);
@@ -3787,6 +3791,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
  *
@@ -3814,20 +3837,11 @@ LockBufferForCleanup(Buffer buffer)
    Assert(BufferIsPinned(buffer));
    Assert(PinCountWaitBuf == NULL);
 
+   BufferCheckOneLocalPin(buffer);
+
+   /* Nobody else to wait for */
    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;
-   }
-
-   /* There should be exactly one local pin */
-   if (GetPrivateRefCount(buffer) != 1)
-       elog(ERROR, "incorrect local pin count: %d",
-            GetPrivateRefCount(buffer));
 
    bufHdr = GetBufferDescriptor(buffer - 1);
 
@@ -4323,6 +4337,8 @@ LockBufHdr(BufferDesc *desc)
    SpinDelayStatus delayStatus;
    uint32      old_buf_state;
 
+   Assert(!BufferIsLocal(BufferDescriptorGetBuffer(desc)));
+
    init_local_spin_delay(&delayStatus);
 
    while (true)
index ff6cd0fc54e36483470ceb4407dd158b9bc8afb6..25008d38f905feb91942b9b4338adcfd196fbc53 100644 (file)
@@ -187,6 +187,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);