Skip to content

Commit e7bd2d6

Browse files
committed
Avoid improbable PANIC during heap_update, redux.
Commit 34f581c intended to ensure that RelationGetBufferForTuple would acquire a visibility-map page pin in case the otherBuffer's all-visible bit had become set since we last had lock on that page. But I missed a case: when we're extending the relation, VM concerns were dealt with only in the relatively-less-likely case that we fail to conditionally lock the otherBuffer. I think I'd believed that we couldn't need to worry about it if the conditional lock succeeds, which is true for the target buffer; but the otherBuffer was unlocked for awhile so its bit might be set anyway. So we need to do the GetVisibilityMapPins dance, and then also recheck the page's free space, in both cases. Per report from Jaime Casanova. Back-patch to v12 as the previous patch was (although there's still no evidence that the bug is reachable pre-v14). Discussion: https://postgr.es/m/E1lWLjP-00006Y-Ml@gemulon.postgresql.org
1 parent d9102e4 commit e7bd2d6

File tree

1 file changed

+23
-18
lines changed
  • src/backend/access/heap

1 file changed

+23
-18
lines changed

src/backend/access/heap/hio.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -643,29 +643,34 @@ RelationGetBufferForTuple(Relation relation, Size len,
643643
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
644644
LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
645645
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
646+
}
646647

647-
/*
648-
* Because the buffers were unlocked for a while, it's possible,
649-
* although unlikely, that an all-visible flag became set or that
650-
* somebody used up the available space in the new page. We can
651-
* use GetVisibilityMapPins to deal with the first case. In the
652-
* second case, just retry from start.
653-
*/
654-
GetVisibilityMapPins(relation, otherBuffer, buffer,
655-
otherBlock, targetBlock, vmbuffer_other,
656-
vmbuffer);
648+
/*
649+
* Because the buffers were unlocked for a while, it's possible,
650+
* although unlikely, that an all-visible flag became set or that
651+
* somebody used up the available space in the new page. We can use
652+
* GetVisibilityMapPins to deal with the first case. In the second
653+
* case, just retry from start.
654+
*/
655+
GetVisibilityMapPins(relation, otherBuffer, buffer,
656+
otherBlock, targetBlock, vmbuffer_other,
657+
vmbuffer);
657658

658-
if (len > PageGetHeapFreeSpace(page))
659-
{
660-
LockBuffer(otherBuffer, BUFFER_LOCK_UNLOCK);
661-
UnlockReleaseBuffer(buffer);
659+
/*
660+
* Note that we have to check the available space even if our
661+
* conditional lock succeeded, because GetVisibilityMapPins might've
662+
* transiently released lock on the target buffer to acquire a VM pin
663+
* for the otherBuffer.
664+
*/
665+
if (len > PageGetHeapFreeSpace(page))
666+
{
667+
LockBuffer(otherBuffer, BUFFER_LOCK_UNLOCK);
668+
UnlockReleaseBuffer(buffer);
662669

663-
goto loop;
664-
}
670+
goto loop;
665671
}
666672
}
667-
668-
if (len > PageGetHeapFreeSpace(page))
673+
else if (len > PageGetHeapFreeSpace(page))
669674
{
670675
/* We should not get here given the test at the top */
671676
elog(PANIC, "tuple is too big: size %zu", len);

0 commit comments

Comments
 (0)