Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd18427

Browse files
committed
Handle elog(FATAL) during ROLLBACK more robustly.
Stress testing by Andreas Seltenreich disclosed longstanding problems thatoccur if a FATAL exit (e.g. due to receipt of SIGTERM) occurs while we aretrying to execute a ROLLBACK of an already-failed transaction. In such acase, xact.c is in TBLOCK_ABORT state, so that AbortOutOfAnyTransactionwould skip AbortTransaction and go straight to CleanupTransaction. Thisled to an assert failure in an assert-enabled build (due to the ROLLBACK'sportal still having a cleanup hook) or without assertions, to a FATAL exitcomplaining about "cannot drop active portal". The latter's notdisastrous, perhaps, but it's messy enough to want to improve it.We don't really want to run all of AbortTransaction in this code path.The minimum required to clean up the open portal safely is to doAtAbort_Memory and AtAbort_Portals. It seems like a good idea todo AtAbort_Memory unconditionally, to be entirely sure that we arestarting with a safe CurrentMemoryContext. That means that if themain loop in AbortOutOfAnyTransaction does nothing, we need an extrastep at the bottom to restore CurrentMemoryContext = TopMemoryContext,which I chose to do by invoking AtCleanup_Memory. This'll result incalling AtCleanup_Memory twice in many of the paths through this function,but that seems harmless and reasonably inexpensive.The original motivation for the assertion in AtCleanup_Portals was thatwe wanted to be sure that any user-defined code executed as a consequenceof the cleanup hook runs during AbortTransaction not CleanupTransaction.That still seems like a valid concern, and now that we've seen one caseof the assertion firing --- which means that exactly that would havehappened in a production build --- let's replace the Assert with a runtimecheck. If we see the cleanup hook still set, we'll emit a WARNING andjust drop the hook unexecuted.This has been like this a long time, so back-patch to all supportedbranches.Discussion:https://postgr.es/m/877ey7bmun.fsf@ansel.ydns.eu
1 parent25169b9 commitcd18427

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

‎src/backend/access/transam/xact.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3893,6 +3893,9 @@ AbortOutOfAnyTransaction(void)
38933893
{
38943894
TransactionStates=CurrentTransactionState;
38953895

3896+
/* Ensure we're not running in a doomed memory context */
3897+
AtAbort_Memory();
3898+
38963899
/*
38973900
* Get out of any transaction or nested transaction
38983901
*/
@@ -3933,7 +3936,14 @@ AbortOutOfAnyTransaction(void)
39333936
break;
39343937
caseTBLOCK_ABORT:
39353938
caseTBLOCK_ABORT_END:
3936-
/* AbortTransaction already done, still need Cleanup */
3939+
3940+
/*
3941+
* AbortTransaction is already done, still need Cleanup.
3942+
* However, if we failed partway through running ROLLBACK,
3943+
* there will be an active portal running that command, which
3944+
* we need to shut down before doing CleanupTransaction.
3945+
*/
3946+
AtAbort_Portals();
39373947
CleanupTransaction();
39383948
s->blockState=TBLOCK_DEFAULT;
39393949
break;
@@ -3956,6 +3966,14 @@ AbortOutOfAnyTransaction(void)
39563966
caseTBLOCK_SUBABORT_END:
39573967
caseTBLOCK_SUBABORT_RESTART:
39583968
/* As above, but AbortSubTransaction already done */
3969+
if (s->curTransactionOwner)
3970+
{
3971+
/* As in TBLOCK_ABORT, might have a live portal to zap */
3972+
AtSubAbort_Portals(s->subTransactionId,
3973+
s->parent->subTransactionId,
3974+
s->curTransactionOwner,
3975+
s->parent->curTransactionOwner);
3976+
}
39593977
CleanupSubTransaction();
39603978
s=CurrentTransactionState;/* changed by pop */
39613979
break;
@@ -3964,6 +3982,9 @@ AbortOutOfAnyTransaction(void)
39643982

39653983
/* Should be out of all subxacts now */
39663984
Assert(s->parent==NULL);
3985+
3986+
/* If we didn't actually have anything to do, revert to TopMemoryContext */
3987+
AtCleanup_Memory();
39673988
}
39683989

39693990
/*

‎src/backend/utils/mmgr/portalmem.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ MarkPortalDone(Portal portal)
440440
* well do that now, since the portal can't be executed any more.
441441
*
442442
* In some cases involving execution of a ROLLBACK command in an already
443-
* aborted transaction, thisprevents an assertion failure caused by
444-
*reaching AtCleanup_Portalswith the cleanup hook still unexecuted.
443+
* aborted transaction, thisis necessary, or we'd reach AtCleanup_Portals
444+
* with the cleanup hook still unexecuted.
445445
*/
446446
if (PointerIsValid(portal->cleanup))
447447
{
@@ -468,8 +468,8 @@ MarkPortalFailed(Portal portal)
468468
* well do that now, since the portal can't be executed any more.
469469
*
470470
* In some cases involving cleanup of an already aborted transaction, this
471-
*prevents an assertion failure caused by reachingAtCleanup_Portals with
472-
*the cleanup hookstill unexecuted.
471+
*is necessary, or we'd reachAtCleanup_Portals with the cleanup hook
472+
* still unexecuted.
473473
*/
474474
if (PointerIsValid(portal->cleanup))
475475
{
@@ -846,8 +846,15 @@ AtCleanup_Portals(void)
846846
if (portal->portalPinned)
847847
portal->portalPinned= false;
848848

849-
/* We had better not be calling any user-defined code here */
850-
Assert(portal->cleanup==NULL);
849+
/*
850+
* We had better not call any user-defined code during cleanup, so if
851+
* the cleanup hook hasn't been run yet, too bad; we'll just skip it.
852+
*/
853+
if (PointerIsValid(portal->cleanup))
854+
{
855+
elog(WARNING,"skipping cleanup for portal \"%s\"",portal->name);
856+
portal->cleanup=NULL;
857+
}
851858

852859
/* Zap it. */
853860
PortalDrop(portal, false);
@@ -1025,8 +1032,15 @@ AtSubCleanup_Portals(SubTransactionId mySubid)
10251032
if (portal->portalPinned)
10261033
portal->portalPinned= false;
10271034

1028-
/* We had better not be calling any user-defined code here */
1029-
Assert(portal->cleanup==NULL);
1035+
/*
1036+
* We had better not call any user-defined code during cleanup, so if
1037+
* the cleanup hook hasn't been run yet, too bad; we'll just skip it.
1038+
*/
1039+
if (PointerIsValid(portal->cleanup))
1040+
{
1041+
elog(WARNING,"skipping cleanup for portal \"%s\"",portal->name);
1042+
portal->cleanup=NULL;
1043+
}
10301044

10311045
/* Zap it. */
10321046
PortalDrop(portal, false);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp