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