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

Commit64b2e7a

Browse files
committed
Pass extra data to bgworkers, and use this to fix parallel contexts.
Up until now, the total amount of data that could be passed to abackground worker at startup was one datum, which can be a small as4 bytes on some systems. That's enough to pass a dsm_handle or anarray index, but not much else. Add a bgw_extra flag to theBackgroundWorker struct, allowing up to 128 bytes to be passed toa new worker on any platform.Use this to fix a problem I recently discovered with the parallelcontext machinery added in 9.5: the master assigns each worker anarray index, and each worker subsequently assigns itself an arrayindex, and there's nothing to guarantee that the two sets of indexesmatch, leading to chaos.Normally, I would not back-patch the change to add bgw_extra, since itis basically a feature addition. However, since 9.5 is still in betaand there seems to be no other sensible way to repair the brokenparallel context machinery, back-patch to 9.5. Existing backgroundworker code can ignore the bgw_extra field without a problem, butmight need to be recompiled since the structure size has changed.Report and patch by me. Review by Amit Kapila.
1 parent59464bd commit64b2e7a

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

‎doc/src/sgml/bgworker.sgml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct BackgroundWorker
5858
char bgw_library_name[BGW_MAXLEN]; /* only if bgw_main is NULL */
5959
char bgw_function_name[BGW_MAXLEN]; /* only if bgw_main is NULL */
6060
Datum bgw_main_arg;
61+
char bgw_extra[BGW_EXTRALEN];
6162
int bgw_notify_pid;
6263
} BackgroundWorker;
6364
</programlisting>
@@ -182,6 +183,13 @@ typedef struct BackgroundWorker
182183
new background worker process.
183184
</para>
184185

186+
<para>
187+
<structfield>bgw_extra</structfield> can contain extra data to be passed
188+
to the background worker. Unlike <structfield>bgw_main_arg</>, this data
189+
is not passed as an argument to the worker's main function, but it can be
190+
accessed via <literal>MyBgworkerEntry</literal>, as discussed above.
191+
</para>
192+
185193
<para>
186194
<structfield>bgw_notify_pid</structfield> is the PID of a PostgreSQL
187195
backend process to which the postmaster should send <literal>SIGUSR1</>

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ typedef struct FixedParallelState
7777
/* Mutex protects remaining fields. */
7878
slock_tmutex;
7979

80-
/* Track whether workers have attached. */
81-
intworkers_expected;
82-
intworkers_attached;
83-
8480
/* Maximum XactLastRecEnd of any worker. */
8581
XLogRecPtrlast_xlog_end;
8682
}FixedParallelState;
@@ -295,8 +291,6 @@ InitializeParallelDSM(ParallelContext *pcxt)
295291
fps->parallel_master_backend_id=MyBackendId;
296292
fps->entrypoint=pcxt->entrypoint;
297293
SpinLockInit(&fps->mutex);
298-
fps->workers_expected=pcxt->nworkers;
299-
fps->workers_attached=0;
300294
fps->last_xlog_end=0;
301295
shm_toc_insert(pcxt->toc,PARALLEL_KEY_FIXED,fps);
302296

@@ -403,7 +397,6 @@ ReinitializeParallelDSM(ParallelContext *pcxt)
403397

404398
/* Reset a few bits of fixed parallel state to a clean state. */
405399
fps=shm_toc_lookup(pcxt->toc,PARALLEL_KEY_FIXED);
406-
fps->workers_attached=0;
407400
fps->last_xlog_end=0;
408401

409402
/* Recreate error queues. */
@@ -455,6 +448,7 @@ LaunchParallelWorkers(ParallelContext *pcxt)
455448
worker.bgw_main=ParallelWorkerMain;
456449
worker.bgw_main_arg=UInt32GetDatum(dsm_segment_handle(pcxt->seg));
457450
worker.bgw_notify_pid=MyProcPid;
451+
memset(&worker.bgw_extra,0,BGW_EXTRALEN);
458452

459453
/*
460454
* Start workers.
@@ -466,6 +460,7 @@ LaunchParallelWorkers(ParallelContext *pcxt)
466460
*/
467461
for (i=0;i<pcxt->nworkers;++i)
468462
{
463+
memcpy(worker.bgw_extra,&i,sizeof(int));
469464
if (!any_registrations_failed&&
470465
RegisterDynamicBackgroundWorker(&worker,
471466
&pcxt->worker[i].bgwhandle))
@@ -891,6 +886,10 @@ ParallelWorkerMain(Datum main_arg)
891886
pqsignal(SIGTERM,die);
892887
BackgroundWorkerUnblockSignals();
893888

889+
/* Determine and set our parallel worker number. */
890+
Assert(ParallelWorkerNumber==-1);
891+
memcpy(&ParallelWorkerNumber,MyBgworkerEntry->bgw_extra,sizeof(int));
892+
894893
/* Set up a memory context and resource owner. */
895894
Assert(CurrentResourceOwner==NULL);
896895
CurrentResourceOwner=ResourceOwnerCreate(NULL,"parallel toplevel");
@@ -915,18 +914,9 @@ ParallelWorkerMain(Datum main_arg)
915914
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
916915
errmsg("bad magic number in dynamic shared memory segment")));
917916

918-
/*Determine and set our worker number. */
917+
/*Look up fixed parallel state. */
919918
fps=shm_toc_lookup(toc,PARALLEL_KEY_FIXED);
920919
Assert(fps!=NULL);
921-
Assert(ParallelWorkerNumber==-1);
922-
SpinLockAcquire(&fps->mutex);
923-
if (fps->workers_attached<fps->workers_expected)
924-
ParallelWorkerNumber=fps->workers_attached++;
925-
SpinLockRelease(&fps->mutex);
926-
if (ParallelWorkerNumber<0)
927-
ereport(ERROR,
928-
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
929-
errmsg("too many parallel workers already attached")));
930920
MyFixedParallelState=fps;
931921

932922
/*

‎src/backend/postmaster/bgworker.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ BackgroundWorkerStateChange(void)
314314
rw->rw_worker.bgw_restart_time=slot->worker.bgw_restart_time;
315315
rw->rw_worker.bgw_main=slot->worker.bgw_main;
316316
rw->rw_worker.bgw_main_arg=slot->worker.bgw_main_arg;
317+
memcpy(rw->rw_worker.bgw_extra,slot->worker.bgw_extra,BGW_EXTRALEN);
317318

318319
/*
319320
* Copy the PID to be notified about state changes, but only if the

‎src/include/postmaster/bgworker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ typedef enum
7474
#defineBGW_DEFAULT_RESTART_INTERVAL60
7575
#defineBGW_NEVER_RESTART-1
7676
#defineBGW_MAXLEN64
77+
#defineBGW_EXTRALEN128
7778

7879
typedefstructBackgroundWorker
7980
{
@@ -85,6 +86,7 @@ typedef struct BackgroundWorker
8586
charbgw_library_name[BGW_MAXLEN];/* only if bgw_main is NULL */
8687
charbgw_function_name[BGW_MAXLEN];/* only if bgw_main is NULL */
8788
Datumbgw_main_arg;
89+
charbgw_extra[BGW_EXTRALEN];
8890
pid_tbgw_notify_pid;/* SIGUSR1 this backend on start/stop */
8991
}BackgroundWorker;
9092

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp