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