Skip to content

Commit ad8b674

Browse files
committed
Shut down transaction tracking at startup process exit.
Maxim Orlov reported that the shutdown of standby server could result in the following assertion failure. The cause of this issue was that, when the shutdown caused the startup process to exit, recovery-time transaction tracking was not shut down even if it's already initialized, and some locks the tracked transactions were holding could not be released. At this situation, if other process was invoked and the PGPROC entry that the startup process used was assigned to it, it found such unreleased locks and caused the assertion failure, during the initialization of it. TRAP: FailedAssertion("SHMQueueEmpty(&(MyProc->myProcLocks[i]))" This commit fixes this issue by making the startup process shut down transaction tracking and release all locks, at the exit of it. Back-patch to all supported branches. Reported-by: Maxim Orlov Author: Fujii Masao Reviewed-by: Maxim Orlov Discussion: https://postgr.es/m/ad4ce692cc1d89a093b471ab1d969b0b@postgrespro.ru
1 parent 6734e80 commit ad8b674

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/backend/postmaster/startup.c

+19
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ static volatile sig_atomic_t in_restore_command = false;
6262
static void StartupProcTriggerHandler(SIGNAL_ARGS);
6363
static void StartupProcSigHupHandler(SIGNAL_ARGS);
6464

65+
/* Callbacks */
66+
static void StartupProcExit(int code, Datum arg);
67+
6568

6669
/* --------------------------------
6770
* signal handler routines
@@ -183,13 +186,29 @@ HandleStartupProcInterrupts(void)
183186
}
184187

185188

189+
/* --------------------------------
190+
* signal handler routines
191+
* --------------------------------
192+
*/
193+
static void
194+
StartupProcExit(int code, Datum arg)
195+
{
196+
/* Shutdown the recovery environment */
197+
if (standbyState != STANDBY_DISABLED)
198+
ShutdownRecoveryTransactionEnvironment();
199+
}
200+
201+
186202
/* ----------------------------------
187203
* Startup Process main entry point
188204
* ----------------------------------
189205
*/
190206
void
191207
StartupProcessMain(void)
192208
{
209+
/* Arrange to clean up at startup process exit */
210+
on_shmem_exit(StartupProcExit, 0);
211+
193212
/*
194213
* Properly accept or ignore signals the postmaster might send us.
195214
*/

src/backend/storage/ipc/standby.c

+15
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,25 @@ InitRecoveryTransactionEnvironment(void)
127127
*
128128
* Prepare to switch from hot standby mode to normal operation. Shut down
129129
* recovery-time transaction tracking.
130+
*
131+
* This must be called even in shutdown of startup process if transaction
132+
* tracking has been initialized. Otherwise some locks the tracked
133+
* transactions were holding will not be released and and may interfere with
134+
* the processes still running (but will exit soon later) at the exit of
135+
* startup process.
130136
*/
131137
void
132138
ShutdownRecoveryTransactionEnvironment(void)
133139
{
140+
/*
141+
* Do nothing if RecoveryLockLists is NULL because which means that
142+
* transaction tracking has not been yet initialized or has been already
143+
* shutdowned. This prevents transaction tracking from being shutdowned
144+
* unexpectedly more than once.
145+
*/
146+
if (RecoveryLockLists == NULL)
147+
return;
148+
134149
/* Mark all tracked in-progress transactions as finished. */
135150
ExpireAllKnownAssignedTransactionIds();
136151

0 commit comments

Comments
 (0)