@@ -160,6 +160,9 @@ BgwPoolMainLoop(BgwPool* poolDesc)
160160 ProcessConfigFile (PGC_SIGHUP );
161161 }
162162
163+ CHECK_FOR_INTERRUPTS ();
164+ AcceptInvalidationMessages ();
165+
163166 // XXX: change to LWLock
164167 LWLockAcquire (& poolDesc -> lock , LW_EXCLUSIVE );
165168
@@ -184,14 +187,17 @@ BgwPoolMainLoop(BgwPool* poolDesc)
184187 LWLockRelease (& poolDesc -> lock );
185188
186189 ConditionVariableSleep (& poolDesc -> available_cv , PG_WAIT_EXTENSION );
190+ ConditionVariableCancelSleep ();
187191 continue ;
188192 }
189193
190194 /* Wait for end of the node joining operation */
191195 while (poolDesc -> n_holders > 0 && !poolDesc -> shutdown )
192196 {
197+ ConditionVariablePrepareToSleep (& Mtm -> receiver_barrier_cv );
193198 LWLockRelease (& poolDesc -> lock );
194199 ConditionVariableSleep (& Mtm -> receiver_barrier_cv , PG_WAIT_EXTENSION );
200+ ConditionVariableCancelSleep ();
195201 LWLockAcquire (& poolDesc -> lock , LW_EXCLUSIVE );
196202 }
197203
@@ -259,7 +265,7 @@ static void BgwStartExtraWorker(BgwPool* pool)
259265{
260266 BackgroundWorker worker ;
261267 BackgroundWorkerHandle * handle ;
262- MemoryContext oldcontext ;
268+ pid_t pid ;
263269
264270 if (pool -> nWorkers >= MtmMaxWorkers )
265271 return ;
@@ -268,21 +274,20 @@ static void BgwStartExtraWorker(BgwPool* pool)
268274 worker .bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION ;
269275 worker .bgw_start_time = BgWorkerStart_ConsistentState ;
270276 worker .bgw_restart_time = BGW_NEVER_RESTART ;
277+ worker .bgw_notify_pid = MyProcPid ;
271278 worker .bgw_main_arg = PointerGetDatum (pool );
272279 sprintf (worker .bgw_library_name , "multimaster" );
273280 sprintf (worker .bgw_function_name , "BgwPoolDynamicWorkerMainLoop" );
274281 snprintf (worker .bgw_name , BGW_MAXLEN , "%s-dynworker-%d" , pool -> poolName , (int ) pool -> nWorkers + 1 );
275282
276283 pool -> lastDynamicWorkerStartTime = GetCurrentTimestamp ();
277284
278- oldcontext = MemoryContextSwitchTo (TopMemoryContext );
279-
280285 if (RegisterDynamicBackgroundWorker (& worker , & handle ))
281286 pool -> bgwhandles [pool -> nWorkers ++ ] = handle ;
282287 else
283288 elog (WARNING , "Failed to start dynamic background worker" );
284289
285- MemoryContextSwitchTo ( oldcontext );
290+ WaitForBackgroundWorkerStartup ( handle , & pid );
286291}
287292
288293/*
@@ -365,6 +370,7 @@ BgwPoolExecute(BgwPool* poolDesc, void* work, int size, MtmReceiverContext *ctx)
365370 ConditionVariablePrepareToSleep (& poolDesc -> overflow_cv );
366371 LWLockRelease (& poolDesc -> lock );
367372 ConditionVariableSleep (& poolDesc -> overflow_cv , PG_WAIT_EXTENSION );
373+ ConditionVariableCancelSleep ();
368374 LWLockAcquire (& poolDesc -> lock , LW_EXCLUSIVE );
369375 }
370376 }
0 commit comments