🌐 AI搜索 & 代理 主页
Skip to content

Commit 57df5ab

Browse files
Switch memory contexts in ReinitializeParallelDSM.
We already do this in CreateParallelContext, InitializeParallelDSM, and LaunchParallelWorkers. I suspect the reason why the matching logic was omitted from ReinitializeParallelDSM is that I failed to realize that any memory allocation was happening here -- but shm_mq_attach does allocate, which could result in a shm_mq_handle being allocated in a shorter-lived context than the ParallelContext which points to it. That could result in a crash if the shorter-lived context is freed before the parallel context is destroyed. As far as I am currently aware, there is no way to reach a crash using only code that is present in core PostgreSQL, but extensions could potentially trip over this. Fixing this in the back-branches appears low-risk, so back-patch to all supported versions. Author: Jakub Wartak <jakub.wartak@enterprisedb.com> Co-authored-by: Jeevan Chalke <jeevan.chalke@enterprisedb.com> Backpatch-through: 14 Discussion: http://postgr.es/m/CAKZiRmwfVripa3FGo06=5D1EddpsLu9JY2iJOTgbsxUQ339ogQ@mail.gmail.com
1 parent b30089f commit 57df5ab

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/backend/access/transam/parallel.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,12 @@ InitializeParallelDSM(ParallelContext *pcxt)
507507
void
508508
ReinitializeParallelDSM(ParallelContext *pcxt)
509509
{
510+
MemoryContext oldcontext;
510511
FixedParallelState *fps;
511512

513+
/* We might be running in a very short-lived memory context. */
514+
oldcontext = MemoryContextSwitchTo(TopTransactionContext);
515+
512516
/* Wait for any old workers to exit. */
513517
if (pcxt->nworkers_launched > 0)
514518
{
@@ -546,6 +550,9 @@ ReinitializeParallelDSM(ParallelContext *pcxt)
546550
pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
547551
}
548552
}
553+
554+
/* Restore previous memory context. */
555+
MemoryContextSwitchTo(oldcontext);
549556
}
550557

551558
/*

0 commit comments

Comments
 (0)