Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * parallel.c
4 : : * Infrastructure for launching parallel workers
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/access/transam/parallel.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include "access/brin.h"
18 : : #include "access/gin.h"
19 : : #include "access/nbtree.h"
20 : : #include "access/parallel.h"
21 : : #include "access/session.h"
22 : : #include "access/xact.h"
23 : : #include "access/xlog.h"
24 : : #include "catalog/index.h"
25 : : #include "catalog/namespace.h"
26 : : #include "catalog/pg_enum.h"
27 : : #include "catalog/storage.h"
28 : : #include "commands/async.h"
29 : : #include "commands/vacuum.h"
30 : : #include "executor/execParallel.h"
31 : : #include "libpq/libpq.h"
32 : : #include "libpq/pqformat.h"
33 : : #include "libpq/pqmq.h"
34 : : #include "miscadmin.h"
35 : : #include "optimizer/optimizer.h"
36 : : #include "pgstat.h"
37 : : #include "storage/ipc.h"
38 : : #include "storage/predicate.h"
39 : : #include "storage/proc.h"
40 : : #include "tcop/tcopprot.h"
41 : : #include "utils/combocid.h"
42 : : #include "utils/guc.h"
43 : : #include "utils/inval.h"
44 : : #include "utils/memutils.h"
45 : : #include "utils/relmapper.h"
46 : : #include "utils/snapmgr.h"
47 : : #include "utils/wait_event.h"
48 : :
49 : : /*
50 : : * We don't want to waste a lot of memory on an error queue which, most of
51 : : * the time, will process only a handful of small messages. However, it is
52 : : * desirable to make it large enough that a typical ErrorResponse can be sent
53 : : * without blocking. That way, a worker that errors out can write the whole
54 : : * message into the queue and terminate without waiting for the user backend.
55 : : */
56 : : #define PARALLEL_ERROR_QUEUE_SIZE 16384
57 : :
58 : : /* Magic number for parallel context TOC. */
59 : : #define PARALLEL_MAGIC 0x50477c7c
60 : :
61 : : /*
62 : : * Magic numbers for per-context parallel state sharing. Higher-level code
63 : : * should use smaller values, leaving these very large ones for use by this
64 : : * module.
65 : : */
66 : : #define PARALLEL_KEY_FIXED UINT64CONST(0xFFFFFFFFFFFF0001)
67 : : #define PARALLEL_KEY_ERROR_QUEUE UINT64CONST(0xFFFFFFFFFFFF0002)
68 : : #define PARALLEL_KEY_LIBRARY UINT64CONST(0xFFFFFFFFFFFF0003)
69 : : #define PARALLEL_KEY_GUC UINT64CONST(0xFFFFFFFFFFFF0004)
70 : : #define PARALLEL_KEY_COMBO_CID UINT64CONST(0xFFFFFFFFFFFF0005)
71 : : #define PARALLEL_KEY_TRANSACTION_SNAPSHOT UINT64CONST(0xFFFFFFFFFFFF0006)
72 : : #define PARALLEL_KEY_ACTIVE_SNAPSHOT UINT64CONST(0xFFFFFFFFFFFF0007)
73 : : #define PARALLEL_KEY_TRANSACTION_STATE UINT64CONST(0xFFFFFFFFFFFF0008)
74 : : #define PARALLEL_KEY_ENTRYPOINT UINT64CONST(0xFFFFFFFFFFFF0009)
75 : : #define PARALLEL_KEY_SESSION_DSM UINT64CONST(0xFFFFFFFFFFFF000A)
76 : : #define PARALLEL_KEY_PENDING_SYNCS UINT64CONST(0xFFFFFFFFFFFF000B)
77 : : #define PARALLEL_KEY_REINDEX_STATE UINT64CONST(0xFFFFFFFFFFFF000C)
78 : : #define PARALLEL_KEY_RELMAPPER_STATE UINT64CONST(0xFFFFFFFFFFFF000D)
79 : : #define PARALLEL_KEY_UNCOMMITTEDENUMS UINT64CONST(0xFFFFFFFFFFFF000E)
80 : : #define PARALLEL_KEY_CLIENTCONNINFO UINT64CONST(0xFFFFFFFFFFFF000F)
81 : :
82 : : /* Fixed-size parallel state. */
83 : : typedef struct FixedParallelState
84 : : {
85 : : /* Fixed-size state that workers must restore. */
86 : : Oid database_id;
87 : : Oid authenticated_user_id;
88 : : Oid session_user_id;
89 : : Oid outer_user_id;
90 : : Oid current_user_id;
91 : : Oid temp_namespace_id;
92 : : Oid temp_toast_namespace_id;
93 : : int sec_context;
94 : : bool session_user_is_superuser;
95 : : bool role_is_superuser;
96 : : PGPROC *parallel_leader_pgproc;
97 : : pid_t parallel_leader_pid;
98 : : ProcNumber parallel_leader_proc_number;
99 : : TimestampTz xact_ts;
100 : : TimestampTz stmt_ts;
101 : : SerializableXactHandle serializable_xact_handle;
102 : :
103 : : /* Maximum XactLastRecEnd of any worker. */
104 : : pg_atomic_uint64 last_xlog_end;
105 : : } FixedParallelState;
106 : :
107 : : /*
108 : : * Our parallel worker number. We initialize this to -1, meaning that we are
109 : : * not a parallel worker. In parallel workers, it will be set to a value >= 0
110 : : * and < the number of workers before any user code is invoked; each parallel
111 : : * worker will get a different parallel worker number.
112 : : */
113 : : int ParallelWorkerNumber = -1;
114 : :
115 : : /* Is there a parallel message pending which we need to receive? */
116 : : volatile sig_atomic_t ParallelMessagePending = false;
117 : :
118 : : /* Are we initializing a parallel worker? */
119 : : bool InitializingParallelWorker = false;
120 : :
121 : : /* Pointer to our fixed parallel state. */
122 : : static FixedParallelState *MyFixedParallelState;
123 : :
124 : : /* List of active parallel contexts. */
125 : : static dlist_head pcxt_list = DLIST_STATIC_INIT(pcxt_list);
126 : :
127 : : /* Backend-local copy of data from FixedParallelState. */
128 : : static pid_t ParallelLeaderPid;
129 : :
130 : : /*
131 : : * List of internal parallel worker entry points. We need this for
132 : : * reasons explained in LookupParallelWorkerFunction(), below.
133 : : */
134 : : static const struct
135 : : {
136 : : const char *fn_name;
137 : : parallel_worker_main_type fn_addr;
138 : : } InternalParallelWorkers[] =
139 : :
140 : : {
141 : : {
142 : : "ParallelQueryMain", ParallelQueryMain
143 : : },
144 : : {
145 : : "_bt_parallel_build_main", _bt_parallel_build_main
146 : : },
147 : : {
148 : : "_brin_parallel_build_main", _brin_parallel_build_main
149 : : },
150 : : {
151 : : "_gin_parallel_build_main", _gin_parallel_build_main
152 : : },
153 : : {
154 : : "parallel_vacuum_main", parallel_vacuum_main
155 : : }
156 : : };
157 : :
158 : : /* Private functions. */
159 : : static void ProcessParallelMessage(ParallelContext *pcxt, int i, StringInfo msg);
160 : : static void WaitForParallelWorkersToExit(ParallelContext *pcxt);
161 : : static parallel_worker_main_type LookupParallelWorkerFunction(const char *libraryname, const char *funcname);
162 : : static void ParallelWorkerShutdown(int code, Datum arg);
163 : :
164 : :
165 : : /*
166 : : * Establish a new parallel context. This should be done after entering
167 : : * parallel mode, and (unless there is an error) the context should be
168 : : * destroyed before exiting the current subtransaction.
169 : : */
170 : : ParallelContext *
171 : 669 : CreateParallelContext(const char *library_name, const char *function_name,
172 : : int nworkers)
173 : : {
174 : : MemoryContext oldcontext;
175 : : ParallelContext *pcxt;
176 : :
177 : : /* It is unsafe to create a parallel context if not in parallel mode. */
178 : : Assert(IsInParallelMode());
179 : :
180 : : /* Number of workers should be non-negative. */
181 : : Assert(nworkers >= 0);
182 : :
183 : : /* We might be running in a short-lived memory context. */
184 : 669 : oldcontext = MemoryContextSwitchTo(TopTransactionContext);
185 : :
186 : : /* Initialize a new ParallelContext. */
187 : 669 : pcxt = palloc0_object(ParallelContext);
188 : 669 : pcxt->subid = GetCurrentSubTransactionId();
189 : 669 : pcxt->nworkers = nworkers;
190 : 669 : pcxt->nworkers_to_launch = nworkers;
191 : 669 : pcxt->library_name = pstrdup(library_name);
192 : 669 : pcxt->function_name = pstrdup(function_name);
193 : 669 : pcxt->error_context_stack = error_context_stack;
194 : 669 : shm_toc_initialize_estimator(&pcxt->estimator);
195 : 669 : dlist_push_head(&pcxt_list, &pcxt->node);
196 : :
197 : : /* Restore previous memory context. */
198 : 669 : MemoryContextSwitchTo(oldcontext);
199 : :
200 : 669 : return pcxt;
201 : : }
202 : :
203 : : /*
204 : : * Establish the dynamic shared memory segment for a parallel context and
205 : : * copy state and other bookkeeping information that will be needed by
206 : : * parallel workers into it.
207 : : */
208 : : void
209 : 669 : InitializeParallelDSM(ParallelContext *pcxt)
210 : : {
211 : : MemoryContext oldcontext;
212 : 669 : Size library_len = 0;
213 : 669 : Size guc_len = 0;
214 : 669 : Size combocidlen = 0;
215 : 669 : Size tsnaplen = 0;
216 : 669 : Size asnaplen = 0;
217 : 669 : Size tstatelen = 0;
218 : 669 : Size pendingsyncslen = 0;
219 : 669 : Size reindexlen = 0;
220 : 669 : Size relmapperlen = 0;
221 : 669 : Size uncommittedenumslen = 0;
222 : 669 : Size clientconninfolen = 0;
223 : 669 : Size segsize = 0;
224 : : int i;
225 : : FixedParallelState *fps;
226 : 669 : dsm_handle session_dsm_handle = DSM_HANDLE_INVALID;
227 : 669 : Snapshot transaction_snapshot = GetTransactionSnapshot();
228 : 669 : Snapshot active_snapshot = GetActiveSnapshot();
229 : :
230 : : /* We might be running in a very short-lived memory context. */
231 : 669 : oldcontext = MemoryContextSwitchTo(TopTransactionContext);
232 : :
233 : : /* Allow space to store the fixed-size parallel state. */
234 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, sizeof(FixedParallelState));
235 : 669 : shm_toc_estimate_keys(&pcxt->estimator, 1);
236 : :
237 : : /*
238 : : * If we manage to reach here while non-interruptible, it's unsafe to
239 : : * launch any workers: we would fail to process interrupts sent by them.
240 : : * We can deal with that edge case by pretending no workers were
241 : : * requested.
242 : : */
243 [ + - + - : 669 : if (!INTERRUPTS_CAN_BE_PROCESSED())
- + ]
244 : 0 : pcxt->nworkers = 0;
245 : :
246 : : /*
247 : : * Normally, the user will have requested at least one worker process, but
248 : : * if by chance they have not, we can skip a bunch of things here.
249 : : */
250 [ + - ]: 669 : if (pcxt->nworkers > 0)
251 : : {
252 : : /* Get (or create) the per-session DSM segment's handle. */
253 : 669 : session_dsm_handle = GetSessionDsmHandle();
254 : :
255 : : /*
256 : : * If we weren't able to create a per-session DSM segment, then we can
257 : : * continue but we can't safely launch any workers because their
258 : : * record typmods would be incompatible so they couldn't exchange
259 : : * tuples.
260 : : */
261 [ - + ]: 669 : if (session_dsm_handle == DSM_HANDLE_INVALID)
262 : 0 : pcxt->nworkers = 0;
263 : : }
264 : :
265 [ + - ]: 669 : if (pcxt->nworkers > 0)
266 : : {
267 : : StaticAssertDecl(BUFFERALIGN(PARALLEL_ERROR_QUEUE_SIZE) ==
268 : : PARALLEL_ERROR_QUEUE_SIZE,
269 : : "parallel error queue size not buffer-aligned");
270 : :
271 : : /* Estimate space for various kinds of state sharing. */
272 : 669 : library_len = EstimateLibraryStateSpace();
273 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, library_len);
274 : 669 : guc_len = EstimateGUCStateSpace();
275 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, guc_len);
276 : 669 : combocidlen = EstimateComboCIDStateSpace();
277 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, combocidlen);
278 [ + + ]: 669 : if (IsolationUsesXactSnapshot())
279 : : {
280 : 11 : tsnaplen = EstimateSnapshotSpace(transaction_snapshot);
281 : 11 : shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen);
282 : : }
283 : 669 : asnaplen = EstimateSnapshotSpace(active_snapshot);
284 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, asnaplen);
285 : 669 : tstatelen = EstimateTransactionStateSpace();
286 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, tstatelen);
287 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, sizeof(dsm_handle));
288 : 669 : pendingsyncslen = EstimatePendingSyncsSpace();
289 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, pendingsyncslen);
290 : 669 : reindexlen = EstimateReindexStateSpace();
291 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, reindexlen);
292 : 669 : relmapperlen = EstimateRelationMapSpace();
293 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, relmapperlen);
294 : 669 : uncommittedenumslen = EstimateUncommittedEnumsSpace();
295 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, uncommittedenumslen);
296 : 669 : clientconninfolen = EstimateClientConnectionInfoSpace();
297 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, clientconninfolen);
298 : : /* If you add more chunks here, you probably need to add keys. */
299 : 669 : shm_toc_estimate_keys(&pcxt->estimator, 12);
300 : :
301 : : /* Estimate space need for error queues. */
302 : 669 : shm_toc_estimate_chunk(&pcxt->estimator,
303 : : mul_size(PARALLEL_ERROR_QUEUE_SIZE,
304 : : pcxt->nworkers));
305 : 669 : shm_toc_estimate_keys(&pcxt->estimator, 1);
306 : :
307 : : /* Estimate how much we'll need for the entrypoint info. */
308 : 669 : shm_toc_estimate_chunk(&pcxt->estimator, strlen(pcxt->library_name) +
309 : : strlen(pcxt->function_name) + 2);
310 : 669 : shm_toc_estimate_keys(&pcxt->estimator, 1);
311 : : }
312 : :
313 : : /*
314 : : * Create DSM and initialize with new table of contents. But if the user
315 : : * didn't request any workers, then don't bother creating a dynamic shared
316 : : * memory segment; instead, just use backend-private memory.
317 : : *
318 : : * Also, if we can't create a dynamic shared memory segment because the
319 : : * maximum number of segments have already been created, then fall back to
320 : : * backend-private memory, and plan not to use any workers. We hope this
321 : : * won't happen very often, but it's better to abandon the use of
322 : : * parallelism than to fail outright.
323 : : */
324 : 669 : segsize = shm_toc_estimate(&pcxt->estimator);
325 [ + - ]: 669 : if (pcxt->nworkers > 0)
326 : 669 : pcxt->seg = dsm_create(segsize, DSM_CREATE_NULL_IF_MAXSEGMENTS);
327 [ + - ]: 669 : if (pcxt->seg != NULL)
328 : 669 : pcxt->toc = shm_toc_create(PARALLEL_MAGIC,
329 : : dsm_segment_address(pcxt->seg),
330 : : segsize);
331 : : else
332 : : {
333 : 0 : pcxt->nworkers = 0;
334 : 0 : pcxt->private_memory = MemoryContextAlloc(TopMemoryContext, segsize);
335 : 0 : pcxt->toc = shm_toc_create(PARALLEL_MAGIC, pcxt->private_memory,
336 : : segsize);
337 : : }
338 : :
339 : : /* Initialize fixed-size state in shared memory. */
340 : : fps = (FixedParallelState *)
341 : 669 : shm_toc_allocate(pcxt->toc, sizeof(FixedParallelState));
342 : 669 : fps->database_id = MyDatabaseId;
343 : 669 : fps->authenticated_user_id = GetAuthenticatedUserId();
344 : 669 : fps->session_user_id = GetSessionUserId();
345 : 669 : fps->outer_user_id = GetCurrentRoleId();
346 : 669 : GetUserIdAndSecContext(&fps->current_user_id, &fps->sec_context);
347 : 669 : fps->session_user_is_superuser = GetSessionUserIsSuperuser();
348 : 669 : fps->role_is_superuser = current_role_is_superuser;
349 : 669 : GetTempNamespaceState(&fps->temp_namespace_id,
350 : : &fps->temp_toast_namespace_id);
351 : 669 : fps->parallel_leader_pgproc = MyProc;
352 : 669 : fps->parallel_leader_pid = MyProcPid;
353 : 669 : fps->parallel_leader_proc_number = MyProcNumber;
354 : 669 : fps->xact_ts = GetCurrentTransactionStartTimestamp();
355 : 669 : fps->stmt_ts = GetCurrentStatementStartTimestamp();
356 : 669 : fps->serializable_xact_handle = ShareSerializableXact();
357 : 669 : pg_atomic_init_u64(&fps->last_xlog_end, InvalidXLogRecPtr);
358 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_FIXED, fps);
359 : :
360 : : /* We can skip the rest of this if we're not budgeting for any workers. */
361 [ + - ]: 669 : if (pcxt->nworkers > 0)
362 : : {
363 : : char *libraryspace;
364 : : char *gucspace;
365 : : char *combocidspace;
366 : : char *tsnapspace;
367 : : char *asnapspace;
368 : : char *tstatespace;
369 : : char *pendingsyncsspace;
370 : : char *reindexspace;
371 : : char *relmapperspace;
372 : : char *error_queue_space;
373 : : char *session_dsm_handle_space;
374 : : char *entrypointstate;
375 : : char *uncommittedenumsspace;
376 : : char *clientconninfospace;
377 : : Size lnamelen;
378 : :
379 : : /* Serialize shared libraries we have loaded. */
380 : 669 : libraryspace = shm_toc_allocate(pcxt->toc, library_len);
381 : 669 : SerializeLibraryState(library_len, libraryspace);
382 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_LIBRARY, libraryspace);
383 : :
384 : : /* Serialize GUC settings. */
385 : 669 : gucspace = shm_toc_allocate(pcxt->toc, guc_len);
386 : 669 : SerializeGUCState(guc_len, gucspace);
387 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_GUC, gucspace);
388 : :
389 : : /* Serialize combo CID state. */
390 : 669 : combocidspace = shm_toc_allocate(pcxt->toc, combocidlen);
391 : 669 : SerializeComboCIDState(combocidlen, combocidspace);
392 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_COMBO_CID, combocidspace);
393 : :
394 : : /*
395 : : * Serialize the transaction snapshot if the transaction isolation
396 : : * level uses a transaction snapshot.
397 : : */
398 [ + + ]: 669 : if (IsolationUsesXactSnapshot())
399 : : {
400 : 11 : tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen);
401 : 11 : SerializeSnapshot(transaction_snapshot, tsnapspace);
402 : 11 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT,
403 : : tsnapspace);
404 : : }
405 : :
406 : : /* Serialize the active snapshot. */
407 : 669 : asnapspace = shm_toc_allocate(pcxt->toc, asnaplen);
408 : 669 : SerializeSnapshot(active_snapshot, asnapspace);
409 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace);
410 : :
411 : : /* Provide the handle for per-session segment. */
412 : 669 : session_dsm_handle_space = shm_toc_allocate(pcxt->toc,
413 : : sizeof(dsm_handle));
414 : 669 : *(dsm_handle *) session_dsm_handle_space = session_dsm_handle;
415 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_SESSION_DSM,
416 : : session_dsm_handle_space);
417 : :
418 : : /* Serialize transaction state. */
419 : 669 : tstatespace = shm_toc_allocate(pcxt->toc, tstatelen);
420 : 669 : SerializeTransactionState(tstatelen, tstatespace);
421 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_TRANSACTION_STATE, tstatespace);
422 : :
423 : : /* Serialize pending syncs. */
424 : 669 : pendingsyncsspace = shm_toc_allocate(pcxt->toc, pendingsyncslen);
425 : 669 : SerializePendingSyncs(pendingsyncslen, pendingsyncsspace);
426 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_PENDING_SYNCS,
427 : : pendingsyncsspace);
428 : :
429 : : /* Serialize reindex state. */
430 : 669 : reindexspace = shm_toc_allocate(pcxt->toc, reindexlen);
431 : 669 : SerializeReindexState(reindexlen, reindexspace);
432 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_REINDEX_STATE, reindexspace);
433 : :
434 : : /* Serialize relmapper state. */
435 : 669 : relmapperspace = shm_toc_allocate(pcxt->toc, relmapperlen);
436 : 669 : SerializeRelationMap(relmapperlen, relmapperspace);
437 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_RELMAPPER_STATE,
438 : : relmapperspace);
439 : :
440 : : /* Serialize uncommitted enum state. */
441 : 669 : uncommittedenumsspace = shm_toc_allocate(pcxt->toc,
442 : : uncommittedenumslen);
443 : 669 : SerializeUncommittedEnums(uncommittedenumsspace, uncommittedenumslen);
444 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_UNCOMMITTEDENUMS,
445 : : uncommittedenumsspace);
446 : :
447 : : /* Serialize our ClientConnectionInfo. */
448 : 669 : clientconninfospace = shm_toc_allocate(pcxt->toc, clientconninfolen);
449 : 669 : SerializeClientConnectionInfo(clientconninfolen, clientconninfospace);
450 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_CLIENTCONNINFO,
451 : : clientconninfospace);
452 : :
453 : : /* Allocate space for worker information. */
454 : 669 : pcxt->worker = palloc0_array(ParallelWorkerInfo, pcxt->nworkers);
455 : :
456 : : /*
457 : : * Establish error queues in dynamic shared memory.
458 : : *
459 : : * These queues should be used only for transmitting ErrorResponse,
460 : : * NoticeResponse, and NotifyResponse protocol messages. Tuple data
461 : : * should be transmitted via separate (possibly larger?) queues.
462 : : */
463 : : error_queue_space =
464 : 669 : shm_toc_allocate(pcxt->toc,
465 : : mul_size(PARALLEL_ERROR_QUEUE_SIZE,
466 : 669 : pcxt->nworkers));
467 [ + + ]: 2159 : for (i = 0; i < pcxt->nworkers; ++i)
468 : : {
469 : : char *start;
470 : : shm_mq *mq;
471 : :
472 : 1490 : start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
473 : 1490 : mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
474 : 1490 : shm_mq_set_receiver(mq, MyProc);
475 : 1490 : pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
476 : : }
477 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_ERROR_QUEUE, error_queue_space);
478 : :
479 : : /*
480 : : * Serialize entrypoint information. It's unsafe to pass function
481 : : * pointers across processes, as the function pointer may be different
482 : : * in each process in EXEC_BACKEND builds, so we always pass library
483 : : * and function name. (We use library name "postgres" for functions
484 : : * in the core backend.)
485 : : */
486 : 669 : lnamelen = strlen(pcxt->library_name);
487 : 669 : entrypointstate = shm_toc_allocate(pcxt->toc, lnamelen +
488 : 669 : strlen(pcxt->function_name) + 2);
489 : 669 : strcpy(entrypointstate, pcxt->library_name);
490 : 669 : strcpy(entrypointstate + lnamelen + 1, pcxt->function_name);
491 : 669 : shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
492 : : }
493 : :
494 : : /* Update nworkers_to_launch, in case we changed nworkers above. */
495 : 669 : pcxt->nworkers_to_launch = pcxt->nworkers;
496 : :
497 : : /* Restore previous memory context. */
498 : 669 : MemoryContextSwitchTo(oldcontext);
499 : 669 : }
500 : :
501 : : /*
502 : : * Reinitialize the dynamic shared memory segment for a parallel context such
503 : : * that we could launch workers for it again.
504 : : */
505 : : void
506 : 173 : ReinitializeParallelDSM(ParallelContext *pcxt)
507 : : {
508 : : MemoryContext oldcontext;
509 : : FixedParallelState *fps;
510 : :
511 : : /* We might be running in a very short-lived memory context. */
512 : 173 : oldcontext = MemoryContextSwitchTo(TopTransactionContext);
513 : :
514 : : /* Wait for any old workers to exit. */
515 [ + - ]: 173 : if (pcxt->nworkers_launched > 0)
516 : : {
517 : 173 : WaitForParallelWorkersToFinish(pcxt);
518 : 173 : WaitForParallelWorkersToExit(pcxt);
519 : 173 : pcxt->nworkers_launched = 0;
520 [ + - ]: 173 : if (pcxt->known_attached_workers)
521 : : {
522 : 173 : pfree(pcxt->known_attached_workers);
523 : 173 : pcxt->known_attached_workers = NULL;
524 : 173 : pcxt->nknown_attached_workers = 0;
525 : : }
526 : : }
527 : :
528 : : /* Reset a few bits of fixed parallel state to a clean state. */
529 : 173 : fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
530 : 173 : pg_atomic_write_u64(&fps->last_xlog_end, InvalidXLogRecPtr);
531 : :
532 : : /* Recreate error queues (if they exist). */
533 [ + - ]: 173 : if (pcxt->nworkers > 0)
534 : : {
535 : : char *error_queue_space;
536 : : int i;
537 : :
538 : : error_queue_space =
539 : 173 : shm_toc_lookup(pcxt->toc, PARALLEL_KEY_ERROR_QUEUE, false);
540 [ + + ]: 723 : for (i = 0; i < pcxt->nworkers; ++i)
541 : : {
542 : : char *start;
543 : : shm_mq *mq;
544 : :
545 : 550 : start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
546 : 550 : mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
547 : 550 : shm_mq_set_receiver(mq, MyProc);
548 : 550 : pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
549 : : }
550 : : }
551 : :
552 : : /* Restore previous memory context. */
553 : 173 : MemoryContextSwitchTo(oldcontext);
554 : 173 : }
555 : :
556 : : /*
557 : : * Reinitialize parallel workers for a parallel context such that we could
558 : : * launch a different number of workers. This is required for cases where
559 : : * we need to reuse the same DSM segment, but the number of workers can
560 : : * vary from run-to-run.
561 : : */
562 : : void
563 : 27 : ReinitializeParallelWorkers(ParallelContext *pcxt, int nworkers_to_launch)
564 : : {
565 : : /*
566 : : * The number of workers that need to be launched must be less than the
567 : : * number of workers with which the parallel context is initialized. But
568 : : * the caller might not know that InitializeParallelDSM reduced nworkers,
569 : : * so just silently trim the request.
570 : : */
571 : 27 : pcxt->nworkers_to_launch = Min(pcxt->nworkers, nworkers_to_launch);
572 : 27 : }
573 : :
574 : : /*
575 : : * Launch parallel workers.
576 : : */
577 : : void
578 : 842 : LaunchParallelWorkers(ParallelContext *pcxt)
579 : : {
580 : : MemoryContext oldcontext;
581 : : BackgroundWorker worker;
582 : : int i;
583 : 842 : bool any_registrations_failed = false;
584 : :
585 : : /* Skip this if we have no workers. */
586 [ + - - + ]: 842 : if (pcxt->nworkers == 0 || pcxt->nworkers_to_launch == 0)
587 : 0 : return;
588 : :
589 : : /* We need to be a lock group leader. */
590 : 842 : BecomeLockGroupLeader();
591 : :
592 : : /* If we do have workers, we'd better have a DSM segment. */
593 : : Assert(pcxt->seg != NULL);
594 : :
595 : : /* We might be running in a short-lived memory context. */
596 : 842 : oldcontext = MemoryContextSwitchTo(TopTransactionContext);
597 : :
598 : : /* Configure a worker. */
599 : 842 : memset(&worker, 0, sizeof(worker));
600 : 842 : snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
601 : : MyProcPid);
602 : 842 : snprintf(worker.bgw_type, BGW_MAXLEN, "parallel worker");
603 : 842 : worker.bgw_flags =
604 : : BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION
605 : : | BGWORKER_CLASS_PARALLEL;
606 : 842 : worker.bgw_start_time = BgWorkerStart_ConsistentState;
607 : 842 : worker.bgw_restart_time = BGW_NEVER_RESTART;
608 : 842 : sprintf(worker.bgw_library_name, "postgres");
609 : 842 : sprintf(worker.bgw_function_name, "ParallelWorkerMain");
610 : 842 : worker.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(pcxt->seg));
611 : 842 : worker.bgw_notify_pid = MyProcPid;
612 : :
613 : : /*
614 : : * Start workers.
615 : : *
616 : : * The caller must be able to tolerate ending up with fewer workers than
617 : : * expected, so there is no need to throw an error here if registration
618 : : * fails. It wouldn't help much anyway, because registering the worker in
619 : : * no way guarantees that it will start up and initialize successfully.
620 : : */
621 [ + + ]: 2881 : for (i = 0; i < pcxt->nworkers_to_launch; ++i)
622 : : {
623 : 2039 : memcpy(worker.bgw_extra, &i, sizeof(int));
624 [ + + + + ]: 4040 : if (!any_registrations_failed &&
625 : 2001 : RegisterDynamicBackgroundWorker(&worker,
626 : 2001 : &pcxt->worker[i].bgwhandle))
627 : : {
628 : 1978 : shm_mq_set_handle(pcxt->worker[i].error_mqh,
629 : 1978 : pcxt->worker[i].bgwhandle);
630 : 1978 : pcxt->nworkers_launched++;
631 : : }
632 : : else
633 : : {
634 : : /*
635 : : * If we weren't able to register the worker, then we've bumped up
636 : : * against the max_worker_processes limit, and future
637 : : * registrations will probably fail too, so arrange to skip them.
638 : : * But we still have to execute this code for the remaining slots
639 : : * to make sure that we forget about the error queues we budgeted
640 : : * for those workers. Otherwise, we'll wait for them to start,
641 : : * but they never will.
642 : : */
643 : 61 : any_registrations_failed = true;
644 : 61 : pcxt->worker[i].bgwhandle = NULL;
645 : 61 : shm_mq_detach(pcxt->worker[i].error_mqh);
646 : 61 : pcxt->worker[i].error_mqh = NULL;
647 : : }
648 : : }
649 : :
650 : : /*
651 : : * Now that nworkers_launched has taken its final value, we can initialize
652 : : * known_attached_workers.
653 : : */
654 [ + + ]: 842 : if (pcxt->nworkers_launched > 0)
655 : : {
656 : 829 : pcxt->known_attached_workers = palloc0_array(bool, pcxt->nworkers_launched);
657 : 829 : pcxt->nknown_attached_workers = 0;
658 : : }
659 : :
660 : : /* Restore previous memory context. */
661 : 842 : MemoryContextSwitchTo(oldcontext);
662 : : }
663 : :
664 : : /*
665 : : * Wait for all workers to attach to their error queues, and throw an error if
666 : : * any worker fails to do this.
667 : : *
668 : : * Callers can assume that if this function returns successfully, then the
669 : : * number of workers given by pcxt->nworkers_launched have initialized and
670 : : * attached to their error queues. Whether or not these workers are guaranteed
671 : : * to still be running depends on what code the caller asked them to run;
672 : : * this function does not guarantee that they have not exited. However, it
673 : : * does guarantee that any workers which exited must have done so cleanly and
674 : : * after successfully performing the work with which they were tasked.
675 : : *
676 : : * If this function is not called, then some of the workers that were launched
677 : : * may not have been started due to a fork() failure, or may have exited during
678 : : * early startup prior to attaching to the error queue, so nworkers_launched
679 : : * cannot be viewed as completely reliable. It will never be less than the
680 : : * number of workers which actually started, but it might be more. Any workers
681 : : * that failed to start will still be discovered by
682 : : * WaitForParallelWorkersToFinish and an error will be thrown at that time,
683 : : * provided that function is eventually reached.
684 : : *
685 : : * In general, the leader process should do as much work as possible before
686 : : * calling this function. fork() failures and other early-startup failures
687 : : * are very uncommon, and having the leader sit idle when it could be doing
688 : : * useful work is undesirable. However, if the leader needs to wait for
689 : : * all of its workers or for a specific worker, it may want to call this
690 : : * function before doing so. If not, it must make some other provision for
691 : : * the failure-to-start case, lest it wait forever. On the other hand, a
692 : : * leader which never waits for a worker that might not be started yet, or
693 : : * at least never does so prior to WaitForParallelWorkersToFinish(), need not
694 : : * call this function at all.
695 : : */
696 : : void
697 : 130 : WaitForParallelWorkersToAttach(ParallelContext *pcxt)
698 : : {
699 : : int i;
700 : :
701 : : /* Skip this if we have no launched workers. */
702 [ - + ]: 130 : if (pcxt->nworkers_launched == 0)
703 : 0 : return;
704 : :
705 : : for (;;)
706 : : {
707 : : /*
708 : : * This will process any parallel messages that are pending and it may
709 : : * also throw an error propagated from a worker.
710 : : */
711 [ + + ]: 6345871 : CHECK_FOR_INTERRUPTS();
712 : :
713 [ + + ]: 13194330 : for (i = 0; i < pcxt->nworkers_launched; ++i)
714 : : {
715 : : BgwHandleStatus status;
716 : : shm_mq *mq;
717 : : int rc;
718 : : pid_t pid;
719 : :
720 [ + + ]: 6848459 : if (pcxt->known_attached_workers[i])
721 : 382232 : continue;
722 : :
723 : : /*
724 : : * If error_mqh is NULL, then the worker has already exited
725 : : * cleanly.
726 : : */
727 [ - + ]: 6466227 : if (pcxt->worker[i].error_mqh == NULL)
728 : : {
729 : 0 : pcxt->known_attached_workers[i] = true;
730 : 0 : ++pcxt->nknown_attached_workers;
731 : 0 : continue;
732 : : }
733 : :
734 : 6466227 : status = GetBackgroundWorkerPid(pcxt->worker[i].bgwhandle, &pid);
735 [ + + ]: 6466227 : if (status == BGWH_STARTED)
736 : : {
737 : : /* Has the worker attached to the error queue? */
738 : 6466141 : mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
739 [ + + ]: 6466141 : if (shm_mq_get_sender(mq) != NULL)
740 : : {
741 : : /* Yes, so it is known to be attached. */
742 : 122 : pcxt->known_attached_workers[i] = true;
743 : 122 : ++pcxt->nknown_attached_workers;
744 : : }
745 : : }
746 [ - + ]: 86 : else if (status == BGWH_STOPPED)
747 : : {
748 : : /*
749 : : * If the worker stopped without attaching to the error queue,
750 : : * throw an error.
751 : : */
752 : 0 : mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
753 [ # # ]: 0 : if (shm_mq_get_sender(mq) == NULL)
754 [ # # ]: 0 : ereport(ERROR,
755 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
756 : : errmsg("parallel worker failed to initialize"),
757 : : errhint("More details may be available in the server log.")));
758 : :
759 : 0 : pcxt->known_attached_workers[i] = true;
760 : 0 : ++pcxt->nknown_attached_workers;
761 : : }
762 : : else
763 : : {
764 : : /*
765 : : * Worker not yet started, so we must wait. The postmaster
766 : : * will notify us if the worker's state changes. Our latch
767 : : * might also get set for some other reason, but if so we'll
768 : : * just end up waiting for the same worker again.
769 : : */
770 : 86 : rc = WaitLatch(MyLatch,
771 : : WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
772 : : -1, WAIT_EVENT_BGWORKER_STARTUP);
773 : :
774 [ + - ]: 86 : if (rc & WL_LATCH_SET)
775 : 86 : ResetLatch(MyLatch);
776 : : }
777 : : }
778 : :
779 : : /* If all workers are known to have started, we're done. */
780 [ + + ]: 6345871 : if (pcxt->nknown_attached_workers >= pcxt->nworkers_launched)
781 : : {
782 : : Assert(pcxt->nknown_attached_workers == pcxt->nworkers_launched);
783 : 130 : break;
784 : : }
785 : : }
786 : : }
787 : :
788 : : /*
789 : : * Wait for all workers to finish computing.
790 : : *
791 : : * Even if the parallel operation seems to have completed successfully, it's
792 : : * important to call this function afterwards. We must not miss any errors
793 : : * the workers may have thrown during the parallel operation, or any that they
794 : : * may yet throw while shutting down.
795 : : *
796 : : * Also, we want to update our notion of XactLastRecEnd based on worker
797 : : * feedback.
798 : : */
799 : : void
800 : 1007 : WaitForParallelWorkersToFinish(ParallelContext *pcxt)
801 : : {
802 : : for (;;)
803 : 787 : {
804 : 1794 : bool anyone_alive = false;
805 : 1794 : int nfinished = 0;
806 : : int i;
807 : :
808 : : /*
809 : : * This will process any parallel messages that are pending, which may
810 : : * change the outcome of the loop that follows. It may also throw an
811 : : * error propagated from a worker.
812 : : */
813 [ + + ]: 1794 : CHECK_FOR_INTERRUPTS();
814 : :
815 : : /*
816 : : * An autovacuum worker running a parallel vacuum (leader) propagates
817 : : * changes to the cost-based delay parameters to its parallel workers
818 : : * at its own cost delay points, which it no longer reaches while
819 : : * waiting here. Do it here instead, on every wakeup, so that a config
820 : : * reload or a change in the number of autovacuum workers sharing the
821 : : * cost limit reaches the parallel workers before they finish.
822 : : */
823 [ + + ]: 1794 : if (AmAutoVacuumWorkerProcess())
824 : 13 : parallel_vacuum_refresh_cost_params();
825 : :
826 [ + + ]: 6297 : for (i = 0; i < pcxt->nworkers_launched; ++i)
827 : : {
828 : : /*
829 : : * If error_mqh is NULL, then the worker has already exited
830 : : * cleanly. If we have received a message through error_mqh from
831 : : * the worker, we know it started up cleanly, and therefore we're
832 : : * certain to be notified when it exits.
833 : : */
834 [ + + ]: 4543 : if (pcxt->worker[i].error_mqh == NULL)
835 : 3679 : ++nfinished;
836 [ + + ]: 864 : else if (pcxt->known_attached_workers[i])
837 : : {
838 : 40 : anyone_alive = true;
839 : 40 : break;
840 : : }
841 : : }
842 : :
843 [ + + ]: 1794 : if (!anyone_alive)
844 : : {
845 : : /* If all workers are known to have finished, we're done. */
846 [ + + ]: 1754 : if (nfinished >= pcxt->nworkers_launched)
847 : : {
848 : : Assert(nfinished == pcxt->nworkers_launched);
849 : 1007 : break;
850 : : }
851 : :
852 : : /*
853 : : * We didn't detect any living workers, but not all workers are
854 : : * known to have exited cleanly. Either not all workers have
855 : : * launched yet, or maybe some of them failed to start or
856 : : * terminated abnormally.
857 : : */
858 [ + + ]: 2735 : for (i = 0; i < pcxt->nworkers_launched; ++i)
859 : : {
860 : : pid_t pid;
861 : : shm_mq *mq;
862 : :
863 : : /*
864 : : * If the worker is BGWH_NOT_YET_STARTED or BGWH_STARTED, we
865 : : * should just keep waiting. If it is BGWH_STOPPED, then
866 : : * further investigation is needed.
867 : : */
868 [ + + ]: 1988 : if (pcxt->worker[i].error_mqh == NULL ||
869 [ + - + - ]: 1648 : pcxt->worker[i].bgwhandle == NULL ||
870 : 824 : GetBackgroundWorkerPid(pcxt->worker[i].bgwhandle,
871 : : &pid) != BGWH_STOPPED)
872 : 1988 : continue;
873 : :
874 : : /*
875 : : * Check whether the worker ended up stopped without ever
876 : : * attaching to the error queue. If so, the postmaster was
877 : : * unable to fork the worker or it exited without initializing
878 : : * properly. We must throw an error, since the caller may
879 : : * have been expecting the worker to do some work before
880 : : * exiting.
881 : : */
882 : 0 : mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
883 [ # # ]: 0 : if (shm_mq_get_sender(mq) == NULL)
884 [ # # ]: 0 : ereport(ERROR,
885 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
886 : : errmsg("parallel worker failed to initialize"),
887 : : errhint("More details may be available in the server log.")));
888 : :
889 : : /*
890 : : * The worker is stopped, but is attached to the error queue.
891 : : * Unless there's a bug somewhere, this will only happen when
892 : : * the worker writes messages and terminates after the
893 : : * CHECK_FOR_INTERRUPTS() near the top of this function and
894 : : * before the call to GetBackgroundWorkerPid(). In that case,
895 : : * our latch should have been set as well and the right things
896 : : * will happen on the next pass through the loop.
897 : : */
898 : : }
899 : : }
900 : :
901 : 787 : (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, -1,
902 : : WAIT_EVENT_PARALLEL_FINISH);
903 : 787 : ResetLatch(MyLatch);
904 : : }
905 : :
906 [ + - ]: 1007 : if (pcxt->toc != NULL)
907 : : {
908 : : FixedParallelState *fps;
909 : : XLogRecPtr last_xlog_end;
910 : :
911 : 1007 : fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
912 : 1007 : last_xlog_end = pg_atomic_read_u64(&fps->last_xlog_end);
913 [ + + ]: 1007 : if (last_xlog_end > XactLastRecEnd)
914 : 51 : XactLastRecEnd = last_xlog_end;
915 : : }
916 : 1007 : }
917 : :
918 : : /*
919 : : * Wait for all workers to exit.
920 : : *
921 : : * This function ensures that workers have been completely shutdown. The
922 : : * difference between WaitForParallelWorkersToFinish and this function is
923 : : * that the former just ensures that last message sent by a worker backend is
924 : : * received by the leader backend whereas this ensures the complete shutdown.
925 : : */
926 : : static void
927 : 842 : WaitForParallelWorkersToExit(ParallelContext *pcxt)
928 : : {
929 : : int i;
930 : :
931 : : /* Wait until the workers actually die. */
932 [ + + ]: 2820 : for (i = 0; i < pcxt->nworkers_launched; ++i)
933 : : {
934 : : BgwHandleStatus status;
935 : :
936 [ + - - + ]: 1978 : if (pcxt->worker == NULL || pcxt->worker[i].bgwhandle == NULL)
937 : 0 : continue;
938 : :
939 : 1978 : status = WaitForBackgroundWorkerShutdown(pcxt->worker[i].bgwhandle);
940 : :
941 : : /*
942 : : * If the postmaster kicked the bucket, we have no chance of cleaning
943 : : * up safely -- we won't be able to tell when our workers are actually
944 : : * dead. This doesn't necessitate a PANIC since they will all abort
945 : : * eventually, but we can't safely continue this session.
946 : : */
947 [ - + ]: 1978 : if (status == BGWH_POSTMASTER_DIED)
948 [ # # ]: 0 : ereport(FATAL,
949 : : (errcode(ERRCODE_ADMIN_SHUTDOWN),
950 : : errmsg("postmaster exited during a parallel transaction")));
951 : :
952 : : /* Release memory. */
953 : 1978 : pfree(pcxt->worker[i].bgwhandle);
954 : 1978 : pcxt->worker[i].bgwhandle = NULL;
955 : : }
956 : 842 : }
957 : :
958 : : /*
959 : : * Destroy a parallel context.
960 : : *
961 : : * If expecting a clean exit, you should use WaitForParallelWorkersToFinish()
962 : : * first, before calling this function. When this function is invoked, any
963 : : * remaining workers are forcibly killed; the dynamic shared memory segment
964 : : * is unmapped; and we then wait (uninterruptibly) for the workers to exit.
965 : : */
966 : : void
967 : 669 : DestroyParallelContext(ParallelContext *pcxt)
968 : : {
969 : : int i;
970 : :
971 : : /*
972 : : * Be careful about order of operations here! We remove the parallel
973 : : * context from the list before we do anything else; otherwise, if an
974 : : * error occurs during a subsequent step, we might try to nuke it again
975 : : * from AtEOXact_Parallel or AtEOSubXact_Parallel.
976 : : */
977 : 669 : dlist_delete(&pcxt->node);
978 : :
979 : : /* Kill each worker in turn, and forget their error queues. */
980 [ + - ]: 669 : if (pcxt->worker != NULL)
981 : : {
982 [ + + ]: 2103 : for (i = 0; i < pcxt->nworkers_launched; ++i)
983 : : {
984 [ + + ]: 1434 : if (pcxt->worker[i].error_mqh != NULL)
985 : : {
986 : 8 : TerminateBackgroundWorker(pcxt->worker[i].bgwhandle);
987 : :
988 : 8 : shm_mq_detach(pcxt->worker[i].error_mqh);
989 : 8 : pcxt->worker[i].error_mqh = NULL;
990 : : }
991 : : }
992 : : }
993 : :
994 : : /*
995 : : * If we have allocated a shared memory segment, detach it. This will
996 : : * implicitly detach the error queues, and any other shared memory queues,
997 : : * stored there.
998 : : */
999 [ + - ]: 669 : if (pcxt->seg != NULL)
1000 : : {
1001 : 669 : dsm_detach(pcxt->seg);
1002 : 669 : pcxt->seg = NULL;
1003 : : }
1004 : :
1005 : : /*
1006 : : * If this parallel context is actually in backend-private memory rather
1007 : : * than shared memory, free that memory instead.
1008 : : */
1009 [ - + ]: 669 : if (pcxt->private_memory != NULL)
1010 : : {
1011 : 0 : pfree(pcxt->private_memory);
1012 : 0 : pcxt->private_memory = NULL;
1013 : : }
1014 : :
1015 : : /*
1016 : : * We can't finish transaction commit or abort until all of the workers
1017 : : * have exited. This means, in particular, that we can't respond to
1018 : : * interrupts at this stage.
1019 : : */
1020 : 669 : HOLD_INTERRUPTS();
1021 : 669 : WaitForParallelWorkersToExit(pcxt);
1022 : 669 : RESUME_INTERRUPTS();
1023 : :
1024 : : /* Free the worker array itself. */
1025 [ + - ]: 669 : if (pcxt->worker != NULL)
1026 : : {
1027 : 669 : pfree(pcxt->worker);
1028 : 669 : pcxt->worker = NULL;
1029 : : }
1030 : :
1031 : : /* Free memory. */
1032 : 669 : pfree(pcxt->library_name);
1033 : 669 : pfree(pcxt->function_name);
1034 : 669 : pfree(pcxt);
1035 : 669 : }
1036 : :
1037 : : /*
1038 : : * Are there any parallel contexts currently active?
1039 : : */
1040 : : bool
1041 : 0 : ParallelContextActive(void)
1042 : : {
1043 : 0 : return !dlist_is_empty(&pcxt_list);
1044 : : }
1045 : :
1046 : : /*
1047 : : * Handle receipt of an interrupt indicating a parallel worker message.
1048 : : *
1049 : : * Note: this is called within a signal handler! All we can do is set
1050 : : * a flag that will cause the next CHECK_FOR_INTERRUPTS() to invoke
1051 : : * ProcessParallelMessages().
1052 : : */
1053 : : void
1054 : 2081 : HandleParallelMessageInterrupt(void)
1055 : : {
1056 : 2081 : InterruptPending = true;
1057 : 2081 : ParallelMessagePending = true;
1058 : : /* latch will be set by procsignal_sigusr1_handler */
1059 : 2081 : }
1060 : :
1061 : : /*
1062 : : * Process any queued protocol messages received from parallel workers.
1063 : : */
1064 : : void
1065 : 2030 : ProcessParallelMessages(void)
1066 : : {
1067 : : dlist_iter iter;
1068 : : MemoryContext oldcontext;
1069 : :
1070 : : static MemoryContext hpm_context = NULL;
1071 : :
1072 : : /*
1073 : : * This is invoked from ProcessInterrupts(), and since some of the
1074 : : * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
1075 : : * for recursive calls if more signals are received while this runs. It's
1076 : : * unclear that recursive entry would be safe, and it doesn't seem useful
1077 : : * even if it is safe, so let's block interrupts until done.
1078 : : */
1079 : 2030 : HOLD_INTERRUPTS();
1080 : :
1081 : : /*
1082 : : * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
1083 : : * don't want to risk leaking data into long-lived contexts, so let's do
1084 : : * our work here in a private context that we can reset on each use.
1085 : : */
1086 [ + + ]: 2030 : if (hpm_context == NULL) /* first time through? */
1087 : 115 : hpm_context = AllocSetContextCreate(TopMemoryContext,
1088 : : "ProcessParallelMessages",
1089 : : ALLOCSET_DEFAULT_SIZES);
1090 : : else
1091 : 1915 : MemoryContextReset(hpm_context);
1092 : :
1093 : 2030 : oldcontext = MemoryContextSwitchTo(hpm_context);
1094 : :
1095 : : /* OK to process messages. Reset the flag saying there are more to do. */
1096 : 2030 : ParallelMessagePending = false;
1097 : :
1098 [ + - + + ]: 4187 : dlist_foreach(iter, &pcxt_list)
1099 : : {
1100 : : ParallelContext *pcxt;
1101 : : int i;
1102 : :
1103 : 2165 : pcxt = dlist_container(ParallelContext, node, iter.cur);
1104 [ - + ]: 2165 : if (pcxt->worker == NULL)
1105 : 0 : continue;
1106 : :
1107 [ + + ]: 8708 : for (i = 0; i < pcxt->nworkers_launched; ++i)
1108 : : {
1109 : : /*
1110 : : * Read as many messages as we can from each worker, but stop when
1111 : : * either (1) the worker's error queue goes away, which can happen
1112 : : * if we receive a Terminate message from the worker; or (2) no
1113 : : * more messages can be read from the worker without blocking.
1114 : : */
1115 [ + + ]: 8528 : while (pcxt->worker[i].error_mqh != NULL)
1116 : : {
1117 : : shm_mq_result res;
1118 : : Size nbytes;
1119 : : void *data;
1120 : :
1121 : 3857 : res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes,
1122 : : &data, true);
1123 [ + + ]: 3857 : if (res == SHM_MQ_WOULD_BLOCK)
1124 : 1872 : break;
1125 [ + - ]: 1985 : else if (res == SHM_MQ_SUCCESS)
1126 : : {
1127 : : StringInfoData msg;
1128 : :
1129 : 1985 : initStringInfo(&msg);
1130 : 1985 : appendBinaryStringInfo(&msg, data, nbytes);
1131 : 1985 : ProcessParallelMessage(pcxt, i, &msg);
1132 : 1977 : pfree(msg.data);
1133 : : }
1134 : : else
1135 [ # # ]: 0 : ereport(ERROR,
1136 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1137 : : errmsg("lost connection to parallel worker")));
1138 : : }
1139 : : }
1140 : : }
1141 : :
1142 : 2022 : MemoryContextSwitchTo(oldcontext);
1143 : :
1144 : : /* Might as well clear the context on our way out */
1145 : 2022 : MemoryContextReset(hpm_context);
1146 : :
1147 : 2022 : RESUME_INTERRUPTS();
1148 : 2022 : }
1149 : :
1150 : : /*
1151 : : * Process a single protocol message received from a single parallel worker.
1152 : : */
1153 : : static void
1154 : 1985 : ProcessParallelMessage(ParallelContext *pcxt, int i, StringInfo msg)
1155 : : {
1156 : : char msgtype;
1157 : :
1158 [ + - ]: 1985 : if (pcxt->known_attached_workers != NULL &&
1159 [ + + ]: 1985 : !pcxt->known_attached_workers[i])
1160 : : {
1161 : 1856 : pcxt->known_attached_workers[i] = true;
1162 : 1856 : pcxt->nknown_attached_workers++;
1163 : : }
1164 : :
1165 : 1985 : msgtype = pq_getmsgbyte(msg);
1166 : :
1167 [ + - + + : 1985 : switch (msgtype)
- ]
1168 : : {
1169 : 8 : case PqMsg_ErrorResponse:
1170 : : case PqMsg_NoticeResponse:
1171 : : {
1172 : : ErrorData edata;
1173 : : ErrorContextCallback *save_error_context_stack;
1174 : :
1175 : : /* Parse ErrorResponse or NoticeResponse. */
1176 : 8 : pq_parse_errornotice(msg, &edata);
1177 : :
1178 : : /* Death of a worker isn't enough justification for suicide. */
1179 : 8 : edata.elevel = Min(edata.elevel, ERROR);
1180 : :
1181 : : /*
1182 : : * If desired, add a context line to show that this is a
1183 : : * message propagated from a parallel worker. Otherwise, it
1184 : : * can sometimes be confusing to understand what actually
1185 : : * happened. (We don't do this in DEBUG_PARALLEL_REGRESS mode
1186 : : * because it causes test-result instability depending on
1187 : : * whether a parallel worker is actually used or not.)
1188 : : */
1189 [ + - ]: 8 : if (debug_parallel_query != DEBUG_PARALLEL_REGRESS)
1190 : : {
1191 [ + + ]: 8 : if (edata.context)
1192 : 4 : edata.context = psprintf("%s\n%s", edata.context,
1193 : : _("parallel worker"));
1194 : : else
1195 : 4 : edata.context = pstrdup(_("parallel worker"));
1196 : : }
1197 : :
1198 : : /*
1199 : : * Context beyond that should use the error context callbacks
1200 : : * that were in effect when the ParallelContext was created,
1201 : : * not the current ones.
1202 : : */
1203 : 8 : save_error_context_stack = error_context_stack;
1204 : 8 : error_context_stack = pcxt->error_context_stack;
1205 : :
1206 : : /* Rethrow error or print notice. */
1207 : 8 : ThrowErrorData(&edata);
1208 : :
1209 : : /* Not an error, so restore previous context stack. */
1210 : 0 : error_context_stack = save_error_context_stack;
1211 : :
1212 : 0 : break;
1213 : : }
1214 : :
1215 : 0 : case PqMsg_NotificationResponse:
1216 : : {
1217 : : /* Propagate NotifyResponse. */
1218 : : int32 pid;
1219 : : const char *channel;
1220 : : const char *payload;
1221 : :
1222 : 0 : pid = pq_getmsgint(msg, 4);
1223 : 0 : channel = pq_getmsgrawstring(msg);
1224 : 0 : payload = pq_getmsgrawstring(msg);
1225 : 0 : pq_endmessage(msg);
1226 : :
1227 : 0 : NotifyMyFrontEnd(channel, payload, pid);
1228 : :
1229 : 0 : break;
1230 : : }
1231 : :
1232 : 7 : case PqParallelMsg_Progress:
1233 : : {
1234 : : /*
1235 : : * Only incremental progress reporting is currently supported.
1236 : : * However, it's possible to add more fields to the message to
1237 : : * allow for handling of other backend progress APIs.
1238 : : */
1239 : 7 : int index = pq_getmsgint(msg, 4);
1240 : 7 : int64 incr = pq_getmsgint64(msg);
1241 : :
1242 : 7 : pq_getmsgend(msg);
1243 : :
1244 : 7 : pgstat_progress_incr_param(index, incr);
1245 : :
1246 : 7 : break;
1247 : : }
1248 : :
1249 : 1970 : case PqMsg_Terminate:
1250 : : {
1251 : 1970 : shm_mq_detach(pcxt->worker[i].error_mqh);
1252 : 1970 : pcxt->worker[i].error_mqh = NULL;
1253 : 1970 : break;
1254 : : }
1255 : :
1256 : 0 : default:
1257 : : {
1258 [ # # ]: 0 : elog(ERROR, "unrecognized message type received from parallel worker: %c (message length %d bytes)",
1259 : : msgtype, msg->len);
1260 : : }
1261 : : }
1262 : 1977 : }
1263 : :
1264 : : /*
1265 : : * End-of-subtransaction cleanup for parallel contexts.
1266 : : *
1267 : : * Here we remove only parallel contexts initiated within the current
1268 : : * subtransaction.
1269 : : */
1270 : : void
1271 : 22812 : AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
1272 : : {
1273 [ + + ]: 22816 : while (!dlist_is_empty(&pcxt_list))
1274 : : {
1275 : : ParallelContext *pcxt;
1276 : :
1277 : 4 : pcxt = dlist_head_element(ParallelContext, node, &pcxt_list);
1278 [ - + ]: 4 : if (pcxt->subid != mySubId)
1279 : 0 : break;
1280 [ - + ]: 4 : if (isCommit)
1281 [ # # ]: 0 : elog(WARNING, "leaked parallel context");
1282 : 4 : DestroyParallelContext(pcxt);
1283 : : }
1284 : 22812 : }
1285 : :
1286 : : /*
1287 : : * End-of-transaction cleanup for parallel contexts.
1288 : : *
1289 : : * We nuke all remaining parallel contexts.
1290 : : */
1291 : : void
1292 : 640961 : AtEOXact_Parallel(bool isCommit)
1293 : : {
1294 [ + + ]: 640966 : while (!dlist_is_empty(&pcxt_list))
1295 : : {
1296 : : ParallelContext *pcxt;
1297 : :
1298 : 5 : pcxt = dlist_head_element(ParallelContext, node, &pcxt_list);
1299 [ - + ]: 5 : if (isCommit)
1300 [ # # ]: 0 : elog(WARNING, "leaked parallel context");
1301 : 5 : DestroyParallelContext(pcxt);
1302 : : }
1303 : 640961 : }
1304 : :
1305 : : /*
1306 : : * Main entrypoint for parallel workers.
1307 : : */
1308 : : void
1309 : 1978 : ParallelWorkerMain(Datum main_arg)
1310 : : {
1311 : : dsm_segment *seg;
1312 : : shm_toc *toc;
1313 : : FixedParallelState *fps;
1314 : : char *error_queue_space;
1315 : : shm_mq *mq;
1316 : : shm_mq_handle *mqh;
1317 : : char *libraryspace;
1318 : : char *entrypointstate;
1319 : : char *library_name;
1320 : : char *function_name;
1321 : : parallel_worker_main_type entrypt;
1322 : : char *gucspace;
1323 : : char *combocidspace;
1324 : : char *tsnapspace;
1325 : : char *asnapspace;
1326 : : char *tstatespace;
1327 : : char *pendingsyncsspace;
1328 : : char *reindexspace;
1329 : : char *relmapperspace;
1330 : : char *uncommittedenumsspace;
1331 : : char *clientconninfospace;
1332 : : char *session_dsm_handle_space;
1333 : : Snapshot tsnapshot;
1334 : : Snapshot asnapshot;
1335 : :
1336 : : /* Set flag to indicate that we're initializing a parallel worker. */
1337 : 1978 : InitializingParallelWorker = true;
1338 : :
1339 : : /* Establish signal handlers. */
1340 : 1978 : BackgroundWorkerUnblockSignals();
1341 : :
1342 : : /* Determine and set our parallel worker number. */
1343 : : Assert(ParallelWorkerNumber == -1);
1344 : 1978 : memcpy(&ParallelWorkerNumber, MyBgworkerEntry->bgw_extra, sizeof(int));
1345 : :
1346 : : /* Set up a memory context to work in, just for cleanliness. */
1347 : 1978 : CurrentMemoryContext = AllocSetContextCreate(TopMemoryContext,
1348 : : "Parallel worker",
1349 : : ALLOCSET_DEFAULT_SIZES);
1350 : :
1351 : : /*
1352 : : * Attach to the dynamic shared memory segment for the parallel query, and
1353 : : * find its table of contents.
1354 : : *
1355 : : * Note: at this point, we have not created any ResourceOwner in this
1356 : : * process. This will result in our DSM mapping surviving until process
1357 : : * exit, which is fine. If there were a ResourceOwner, it would acquire
1358 : : * ownership of the mapping, but we have no need for that.
1359 : : */
1360 : 1978 : seg = dsm_attach(DatumGetUInt32(main_arg));
1361 [ - + ]: 1978 : if (seg == NULL)
1362 [ # # ]: 0 : ereport(ERROR,
1363 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1364 : : errmsg("could not map dynamic shared memory segment")));
1365 : 1978 : toc = shm_toc_attach(PARALLEL_MAGIC, dsm_segment_address(seg));
1366 [ - + ]: 1978 : if (toc == NULL)
1367 [ # # ]: 0 : ereport(ERROR,
1368 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1369 : : errmsg("invalid magic number in dynamic shared memory segment")));
1370 : :
1371 : : /* Look up fixed parallel state. */
1372 : 1978 : fps = shm_toc_lookup(toc, PARALLEL_KEY_FIXED, false);
1373 : 1978 : MyFixedParallelState = fps;
1374 : :
1375 : : /* Arrange to signal the leader if we exit. */
1376 : 1978 : ParallelLeaderPid = fps->parallel_leader_pid;
1377 : 1978 : ParallelLeaderProcNumber = fps->parallel_leader_proc_number;
1378 : 1978 : before_shmem_exit(ParallelWorkerShutdown, PointerGetDatum(seg));
1379 : :
1380 : : /*
1381 : : * Now we can find and attach to the error queue provided for us. That's
1382 : : * good, because until we do that, any errors that happen here will not be
1383 : : * reported back to the process that requested that this worker be
1384 : : * launched.
1385 : : */
1386 : 1978 : error_queue_space = shm_toc_lookup(toc, PARALLEL_KEY_ERROR_QUEUE, false);
1387 : 1978 : mq = (shm_mq *) (error_queue_space +
1388 : 1978 : ParallelWorkerNumber * PARALLEL_ERROR_QUEUE_SIZE);
1389 : 1978 : shm_mq_set_sender(mq, MyProc);
1390 : 1978 : mqh = shm_mq_attach(mq, seg, NULL);
1391 : 1978 : pq_redirect_to_shm_mq(seg, mqh);
1392 : 1978 : pq_set_parallel_leader(fps->parallel_leader_pid,
1393 : : fps->parallel_leader_proc_number);
1394 : :
1395 : : /*
1396 : : * Hooray! Primary initialization is complete. Now, we need to set up our
1397 : : * backend-local state to match the original backend.
1398 : : */
1399 : :
1400 : : /*
1401 : : * Join locking group. We must do this before anything that could try to
1402 : : * acquire a heavyweight lock, because any heavyweight locks acquired to
1403 : : * this point could block either directly against the parallel group
1404 : : * leader or against some process which in turn waits for a lock that
1405 : : * conflicts with the parallel group leader, causing an undetected
1406 : : * deadlock. (If we can't join the lock group, the leader has gone away,
1407 : : * so just exit quietly.)
1408 : : */
1409 [ - + ]: 1978 : if (!BecomeLockGroupMember(fps->parallel_leader_pgproc,
1410 : : fps->parallel_leader_pid))
1411 : 0 : return;
1412 : :
1413 : : /*
1414 : : * Restore transaction and statement start-time timestamps. This must
1415 : : * happen before anything that would start a transaction, else asserts in
1416 : : * xact.c will fire.
1417 : : */
1418 : 1978 : SetParallelStartTimestamps(fps->xact_ts, fps->stmt_ts);
1419 : :
1420 : : /*
1421 : : * Identify the entry point to be called. In theory this could result in
1422 : : * loading an additional library, though most likely the entry point is in
1423 : : * the core backend or in a library we just loaded.
1424 : : */
1425 : 1978 : entrypointstate = shm_toc_lookup(toc, PARALLEL_KEY_ENTRYPOINT, false);
1426 : 1978 : library_name = entrypointstate;
1427 : 1978 : function_name = entrypointstate + strlen(library_name) + 1;
1428 : :
1429 : 1978 : entrypt = LookupParallelWorkerFunction(library_name, function_name);
1430 : :
1431 : : /*
1432 : : * Restore current session authorization and role id. No verification
1433 : : * happens here, we just blindly adopt the leader's state. Note that this
1434 : : * has to happen before InitPostgres, since InitializeSessionUserId will
1435 : : * not set these variables.
1436 : : */
1437 : 1978 : SetAuthenticatedUserId(fps->authenticated_user_id);
1438 : 1978 : SetSessionAuthorization(fps->session_user_id,
1439 : 1978 : fps->session_user_is_superuser);
1440 : 1978 : SetCurrentRoleId(fps->outer_user_id, fps->role_is_superuser);
1441 : :
1442 : : /*
1443 : : * Restore database connection. We skip connection authorization checks,
1444 : : * reasoning that (a) the leader checked these things when it started, and
1445 : : * (b) we do not want parallel mode to cause these failures, because that
1446 : : * would make use of parallel query plans not transparent to applications.
1447 : : */
1448 : 1978 : BackgroundWorkerInitializeConnectionByOid(fps->database_id,
1449 : : fps->authenticated_user_id,
1450 : : BGWORKER_BYPASS_ALLOWCONN |
1451 : : BGWORKER_BYPASS_ROLELOGINCHECK);
1452 : :
1453 : : /*
1454 : : * Set the client encoding to the database encoding, since that is what
1455 : : * the leader will expect. (We're cheating a bit by not calling
1456 : : * PrepareClientEncoding first. It's okay because this call will always
1457 : : * result in installing a no-op conversion. No error should be possible,
1458 : : * but check anyway.)
1459 : : */
1460 [ - + ]: 1978 : if (SetClientEncoding(GetDatabaseEncoding()) < 0)
1461 [ # # ]: 0 : elog(ERROR, "SetClientEncoding(%d) failed", GetDatabaseEncoding());
1462 : :
1463 : : /*
1464 : : * Load libraries that were loaded by original backend. We want to do
1465 : : * this before restoring GUCs, because the libraries might define custom
1466 : : * variables.
1467 : : */
1468 : 1978 : libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY, false);
1469 : 1978 : StartTransactionCommand();
1470 : 1978 : RestoreLibraryState(libraryspace);
1471 : 1978 : CommitTransactionCommand();
1472 : :
1473 : : /* Crank up a transaction state appropriate to a parallel worker. */
1474 : 1978 : tstatespace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_STATE, false);
1475 : 1978 : StartParallelWorkerTransaction(tstatespace);
1476 : :
1477 : : /*
1478 : : * Restore state that affects catalog access. Ideally we'd do this even
1479 : : * before calling InitPostgres, but that has order-of-initialization
1480 : : * problems, and also the relmapper would get confused during the
1481 : : * CommitTransactionCommand call above.
1482 : : */
1483 : 1978 : pendingsyncsspace = shm_toc_lookup(toc, PARALLEL_KEY_PENDING_SYNCS,
1484 : : false);
1485 : 1978 : RestorePendingSyncs(pendingsyncsspace);
1486 : 1978 : relmapperspace = shm_toc_lookup(toc, PARALLEL_KEY_RELMAPPER_STATE, false);
1487 : 1978 : RestoreRelationMap(relmapperspace);
1488 : 1978 : reindexspace = shm_toc_lookup(toc, PARALLEL_KEY_REINDEX_STATE, false);
1489 : 1978 : RestoreReindexState(reindexspace);
1490 : 1978 : combocidspace = shm_toc_lookup(toc, PARALLEL_KEY_COMBO_CID, false);
1491 : 1978 : RestoreComboCIDState(combocidspace);
1492 : :
1493 : : /* Attach to the per-session DSM segment and contained objects. */
1494 : : session_dsm_handle_space =
1495 : 1978 : shm_toc_lookup(toc, PARALLEL_KEY_SESSION_DSM, false);
1496 : 1978 : AttachSession(*(dsm_handle *) session_dsm_handle_space);
1497 : :
1498 : : /*
1499 : : * If the transaction isolation level is REPEATABLE READ or SERIALIZABLE,
1500 : : * the leader has serialized the transaction snapshot and we must restore
1501 : : * it. At lower isolation levels, there is no transaction-lifetime
1502 : : * snapshot, but we need TransactionXmin to get set to a value which is
1503 : : * less than or equal to the xmin of every snapshot that will be used by
1504 : : * this worker. The easiest way to accomplish that is to install the
1505 : : * active snapshot as the transaction snapshot. Code running in this
1506 : : * parallel worker might take new snapshots via GetTransactionSnapshot()
1507 : : * or GetLatestSnapshot(), but it shouldn't have any way of acquiring a
1508 : : * snapshot older than the active snapshot.
1509 : : */
1510 : 1978 : asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, false);
1511 : 1978 : tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, true);
1512 : 1978 : asnapshot = RestoreSnapshot(asnapspace);
1513 [ + + ]: 1978 : tsnapshot = tsnapspace ? RestoreSnapshot(tsnapspace) : asnapshot;
1514 : 1978 : RestoreTransactionSnapshot(tsnapshot,
1515 : 1978 : fps->parallel_leader_pgproc);
1516 : 1978 : PushActiveSnapshot(asnapshot);
1517 : :
1518 : : /*
1519 : : * We've changed which tuples we can see, and must therefore invalidate
1520 : : * system caches.
1521 : : */
1522 : 1978 : InvalidateSystemCaches();
1523 : :
1524 : : /*
1525 : : * Restore GUC values from launching backend. We can't do this earlier,
1526 : : * because GUC check hooks that do catalog lookups need to see the same
1527 : : * database state as the leader. Also, the check hooks for
1528 : : * session_authorization and role assume we already set the correct role
1529 : : * OIDs.
1530 : : */
1531 : 1978 : gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1532 : 1978 : RestoreGUCState(gucspace);
1533 : :
1534 : : /*
1535 : : * Restore current user ID and security context. No verification happens
1536 : : * here, we just blindly adopt the leader's state. We can't do this till
1537 : : * after restoring GUCs, else we'll get complaints about restoring
1538 : : * session_authorization and role. (In effect, we're assuming that all
1539 : : * the restored values are okay to set, even if we are now inside a
1540 : : * restricted context.)
1541 : : */
1542 : 1978 : SetUserIdAndSecContext(fps->current_user_id, fps->sec_context);
1543 : :
1544 : : /* Restore temp-namespace state to ensure search path matches leader's. */
1545 : 1978 : SetTempNamespaceState(fps->temp_namespace_id,
1546 : : fps->temp_toast_namespace_id);
1547 : :
1548 : : /* Restore uncommitted enums. */
1549 : 1978 : uncommittedenumsspace = shm_toc_lookup(toc, PARALLEL_KEY_UNCOMMITTEDENUMS,
1550 : : false);
1551 : 1978 : RestoreUncommittedEnums(uncommittedenumsspace);
1552 : :
1553 : : /* Restore the ClientConnectionInfo. */
1554 : 1978 : clientconninfospace = shm_toc_lookup(toc, PARALLEL_KEY_CLIENTCONNINFO,
1555 : : false);
1556 : 1978 : RestoreClientConnectionInfo(clientconninfospace);
1557 : :
1558 : : /*
1559 : : * Initialize SystemUser now that MyClientConnectionInfo is restored. Also
1560 : : * ensure that auth_method is actually valid, aka authn_id is not NULL.
1561 : : */
1562 [ + + ]: 1978 : if (MyClientConnectionInfo.authn_id)
1563 : 2 : InitializeSystemUser(MyClientConnectionInfo.authn_id,
1564 : : hba_authname(MyClientConnectionInfo.auth_method));
1565 : :
1566 : : /* Attach to the leader's serializable transaction, if SERIALIZABLE. */
1567 : 1978 : AttachSerializableXact(fps->serializable_xact_handle);
1568 : :
1569 : : /*
1570 : : * We've initialized all of our state now; nothing should change
1571 : : * hereafter.
1572 : : */
1573 : 1978 : InitializingParallelWorker = false;
1574 : 1978 : EnterParallelMode();
1575 : :
1576 : : /*
1577 : : * Time to do the real work: invoke the caller-supplied code.
1578 : : */
1579 : 1978 : entrypt(seg, toc);
1580 : :
1581 : : /* Must exit parallel mode to pop active snapshot. */
1582 : 1970 : ExitParallelMode();
1583 : :
1584 : : /* Must pop active snapshot so snapmgr.c doesn't complain. */
1585 : 1970 : PopActiveSnapshot();
1586 : :
1587 : : /* Shut down the parallel-worker transaction. */
1588 : 1970 : EndParallelWorkerTransaction();
1589 : :
1590 : : /* Detach from the per-session DSM segment. */
1591 : 1970 : DetachSession();
1592 : :
1593 : : /* Report success. */
1594 : 1970 : pq_putmessage(PqMsg_Terminate, NULL, 0);
1595 : : }
1596 : :
1597 : : /*
1598 : : * Update shared memory with the ending location of the last WAL record we
1599 : : * wrote, if it's greater than the value already stored there.
1600 : : */
1601 : : void
1602 : 1970 : ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
1603 : : {
1604 : 1970 : FixedParallelState *fps = MyFixedParallelState;
1605 : :
1606 : : Assert(fps != NULL);
1607 : 1970 : pg_atomic_monotonic_advance_u64(&fps->last_xlog_end, last_xlog_end);
1608 : 1970 : }
1609 : :
1610 : : /*
1611 : : * Make sure the leader tries to read from our error queue one more time.
1612 : : * This guards against the case where we exit uncleanly without sending an
1613 : : * ErrorResponse to the leader, for example because some code calls proc_exit
1614 : : * directly.
1615 : : *
1616 : : * Also explicitly detach from dsm segment so that subsystems using
1617 : : * on_dsm_detach() have a chance to send stats before the stats subsystem is
1618 : : * shut down as part of a before_shmem_exit() hook.
1619 : : *
1620 : : * One might think this could instead be solved by carefully ordering the
1621 : : * attaching to dsm segments, so that the pgstats segments get detached from
1622 : : * later than the parallel query one. That turns out to not work because the
1623 : : * stats hash might need to grow which can cause new segments to be allocated,
1624 : : * which then will be detached from earlier.
1625 : : */
1626 : : static void
1627 : 1978 : ParallelWorkerShutdown(int code, Datum arg)
1628 : : {
1629 : 1978 : SendProcSignal(ParallelLeaderPid,
1630 : : PROCSIG_PARALLEL_MESSAGE,
1631 : : ParallelLeaderProcNumber);
1632 : :
1633 : 1978 : dsm_detach((dsm_segment *) DatumGetPointer(arg));
1634 : 1978 : }
1635 : :
1636 : : /*
1637 : : * Look up (and possibly load) a parallel worker entry point function.
1638 : : *
1639 : : * For functions contained in the core code, we use library name "postgres"
1640 : : * and consult the InternalParallelWorkers array. External functions are
1641 : : * looked up, and loaded if necessary, using load_external_function().
1642 : : *
1643 : : * The point of this is to pass function names as strings across process
1644 : : * boundaries. We can't pass actual function addresses because of the
1645 : : * possibility that the function has been loaded at a different address
1646 : : * in a different process. This is obviously a hazard for functions in
1647 : : * loadable libraries, but it can happen even for functions in the core code
1648 : : * on platforms using EXEC_BACKEND (e.g., Windows).
1649 : : *
1650 : : * At some point it might be worthwhile to get rid of InternalParallelWorkers[]
1651 : : * in favor of applying load_external_function() for core functions too;
1652 : : * but that raises portability issues that are not worth addressing now.
1653 : : */
1654 : : static parallel_worker_main_type
1655 : 1978 : LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
1656 : : {
1657 : : /*
1658 : : * If the function is to be loaded from postgres itself, search the
1659 : : * InternalParallelWorkers array.
1660 : : */
1661 [ + - ]: 1978 : if (strcmp(libraryname, "postgres") == 0)
1662 : : {
1663 [ + - ]: 2350 : for (size_t i = 0; i < lengthof(InternalParallelWorkers); i++)
1664 : : {
1665 [ + + ]: 2350 : if (strcmp(InternalParallelWorkers[i].fn_name, funcname) == 0)
1666 : 1978 : return InternalParallelWorkers[i].fn_addr;
1667 : : }
1668 : :
1669 : : /* We can only reach this by programming error. */
1670 [ # # ]: 0 : elog(ERROR, "internal function \"%s\" not found", funcname);
1671 : : }
1672 : :
1673 : : /* Otherwise load from external library. */
1674 : 0 : return (parallel_worker_main_type)
1675 : 0 : load_external_function(libraryname, funcname, true, NULL);
1676 : : }
|