bool        result;
    PrivateRefCountEntry *ref;
 
+   Assert(!BufferIsLocal(b));
+
    ref = GetPrivateRefCountEntry(b, true);
 
    if (ref == NULL)
    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);
                                    LW_EXCLUSIVE);
 }
 
+/*
+ * Verify that this backend is pinning the buffer exactly once.
+ *
+ * NOTE: Like in BufferIsPinned(), what we check here is that *this* backend
+ * holds a pin on the buffer.  We do not care whether some other backend does.
+ */
+void
+CheckBufferIsPinnedOnce(Buffer buffer)
+{
+   if (BufferIsLocal(buffer))
+   {
+       if (LocalRefCount[-buffer - 1] != 1)
+           elog(ERROR, "incorrect local pin count: %d",
+                LocalRefCount[-buffer - 1]);
+   }
+   else
+   {
+       if (GetPrivateRefCount(buffer) != 1)
+           elog(ERROR, "incorrect local pin count: %d",
+                GetPrivateRefCount(buffer));
+   }
+}
+
 /*
  * LockBufferForCleanup - lock a buffer in preparation for deleting items
  *
    Assert(BufferIsPinned(buffer));
    Assert(PinCountWaitBuf == NULL);
 
+   CheckBufferIsPinnedOnce(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);
 
    SpinDelayStatus delayStatus;
    uint32      old_buf_state;
 
+   Assert(!BufferIsLocal(BufferDescriptorGetBuffer(desc)));
+
    init_local_spin_delay(&delayStatus);
 
    while (true)