PostgreSQL Source Code  git master
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-2018, 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/nbtree.h"
18 #include "access/parallel.h"
19 #include "access/session.h"
20 #include "access/xact.h"
21 #include "access/xlog.h"
22 #include "catalog/index.h"
23 #include "catalog/namespace.h"
24 #include "commands/async.h"
25 #include "executor/execParallel.h"
26 #include "libpq/libpq.h"
27 #include "libpq/pqformat.h"
28 #include "libpq/pqmq.h"
29 #include "miscadmin.h"
30 #include "optimizer/planmain.h"
31 #include "pgstat.h"
32 #include "storage/ipc.h"
33 #include "storage/sinval.h"
34 #include "storage/spin.h"
35 #include "tcop/tcopprot.h"
36 #include "utils/combocid.h"
37 #include "utils/guc.h"
38 #include "utils/inval.h"
39 #include "utils/memutils.h"
40 #include "utils/resowner.h"
41 #include "utils/snapmgr.h"
42 #include "utils/typcache.h"
43 
44 
45 /*
46  * We don't want to waste a lot of memory on an error queue which, most of
47  * the time, will process only a handful of small messages. However, it is
48  * desirable to make it large enough that a typical ErrorResponse can be sent
49  * without blocking. That way, a worker that errors out can write the whole
50  * message into the queue and terminate without waiting for the user backend.
51  */
52 #define PARALLEL_ERROR_QUEUE_SIZE 16384
53 
54 /* Magic number for parallel context TOC. */
55 #define PARALLEL_MAGIC 0x50477c7c
56 
57 /*
58  * Magic numbers for per-context parallel state sharing. Higher-level code
59  * should use smaller values, leaving these very large ones for use by this
60  * module.
61  */
62 #define PARALLEL_KEY_FIXED UINT64CONST(0xFFFFFFFFFFFF0001)
63 #define PARALLEL_KEY_ERROR_QUEUE UINT64CONST(0xFFFFFFFFFFFF0002)
64 #define PARALLEL_KEY_LIBRARY UINT64CONST(0xFFFFFFFFFFFF0003)
65 #define PARALLEL_KEY_GUC UINT64CONST(0xFFFFFFFFFFFF0004)
66 #define PARALLEL_KEY_COMBO_CID UINT64CONST(0xFFFFFFFFFFFF0005)
67 #define PARALLEL_KEY_TRANSACTION_SNAPSHOT UINT64CONST(0xFFFFFFFFFFFF0006)
68 #define PARALLEL_KEY_ACTIVE_SNAPSHOT UINT64CONST(0xFFFFFFFFFFFF0007)
69 #define PARALLEL_KEY_TRANSACTION_STATE UINT64CONST(0xFFFFFFFFFFFF0008)
70 #define PARALLEL_KEY_ENTRYPOINT UINT64CONST(0xFFFFFFFFFFFF0009)
71 #define PARALLEL_KEY_SESSION_DSM UINT64CONST(0xFFFFFFFFFFFF000A)
72 #define PARALLEL_KEY_REINDEX_STATE UINT64CONST(0xFFFFFFFFFFFF000B)
73 
74 /* Fixed-size parallel state. */
75 typedef struct FixedParallelState
76 {
77  /* Fixed-size state that workers must restore. */
89 
90  /* Mutex protects remaining fields. */
92 
93  /* Maximum XactLastRecEnd of any worker. */
96 
97 /*
98  * Our parallel worker number. We initialize this to -1, meaning that we are
99  * not a parallel worker. In parallel workers, it will be set to a value >= 0
100  * and < the number of workers before any user code is invoked; each parallel
101  * worker will get a different parallel worker number.
102  */
104 
105 /* Is there a parallel message pending which we need to receive? */
106 volatile bool ParallelMessagePending = false;
107 
108 /* Are we initializing a parallel worker? */
110 
111 /* Pointer to our fixed parallel state. */
113 
114 /* List of active parallel contexts. */
116 
117 /* Backend-local copy of data from FixedParallelState. */
118 static pid_t ParallelMasterPid;
119 
120 /*
121  * List of internal parallel worker entry points. We need this for
122  * reasons explained in LookupParallelWorkerFunction(), below.
123  */
124 static const struct
125 {
126  const char *fn_name;
129 
130 {
131  {
132  "ParallelQueryMain", ParallelQueryMain
133  },
134  {
135  "_bt_parallel_build_main", _bt_parallel_build_main
136  }
137 };
138 
139 /* Private functions. */
140 static void HandleParallelMessage(ParallelContext *pcxt, int i, StringInfo msg);
142 static parallel_worker_main_type LookupParallelWorkerFunction(const char *libraryname, const char *funcname);
143 static void ParallelWorkerShutdown(int code, Datum arg);
144 
145 
146 /*
147  * Establish a new parallel context. This should be done after entering
148  * parallel mode, and (unless there is an error) the context should be
149  * destroyed before exiting the current subtransaction.
150  */
152 CreateParallelContext(const char *library_name, const char *function_name,
153  int nworkers, bool serializable_okay)
154 {
155  MemoryContext oldcontext;
156  ParallelContext *pcxt;
157 
158  /* It is unsafe to create a parallel context if not in parallel mode. */
160 
161  /* Number of workers should be non-negative. */
162  Assert(nworkers >= 0);
163 
164  /*
165  * If dynamic shared memory is not available, we won't be able to use
166  * background workers.
167  */
169  nworkers = 0;
170 
171  /*
172  * If we are running under serializable isolation, we can't use parallel
173  * workers, at least not until somebody enhances that mechanism to be
174  * parallel-aware. Utility statement callers may ask us to ignore this
175  * restriction because they're always able to safely ignore the fact that
176  * SIREAD locks do not work with parallelism.
177  */
178  if (IsolationIsSerializable() && !serializable_okay)
179  nworkers = 0;
180 
181  /* We might be running in a short-lived memory context. */
183 
184  /* Initialize a new ParallelContext. */
185  pcxt = palloc0(sizeof(ParallelContext));
187  pcxt->nworkers = nworkers;
188  pcxt->library_name = pstrdup(library_name);
189  pcxt->function_name = pstrdup(function_name);
192  dlist_push_head(&pcxt_list, &pcxt->node);
193 
194  /* Restore previous memory context. */
195  MemoryContextSwitchTo(oldcontext);
196 
197  return pcxt;
198 }
199 
200 /*
201  * Establish the dynamic shared memory segment for a parallel context and
202  * copy state and other bookkeeping information that will be needed by
203  * parallel workers into it.
204  */
205 void
207 {
208  MemoryContext oldcontext;
209  Size library_len = 0;
210  Size guc_len = 0;
211  Size combocidlen = 0;
212  Size tsnaplen = 0;
213  Size asnaplen = 0;
214  Size tstatelen = 0;
215  Size reindexlen = 0;
216  Size segsize = 0;
217  int i;
218  FixedParallelState *fps;
219  dsm_handle session_dsm_handle = DSM_HANDLE_INVALID;
220  Snapshot transaction_snapshot = GetTransactionSnapshot();
221  Snapshot active_snapshot = GetActiveSnapshot();
222 
223  /* We might be running in a very short-lived memory context. */
225 
226  /* Allow space to store the fixed-size parallel state. */
228  shm_toc_estimate_keys(&pcxt->estimator, 1);
229 
230  /*
231  * Normally, the user will have requested at least one worker process, but
232  * if by chance they have not, we can skip a bunch of things here.
233  */
234  if (pcxt->nworkers > 0)
235  {
236  /* Get (or create) the per-session DSM segment's handle. */
237  session_dsm_handle = GetSessionDsmHandle();
238 
239  /*
240  * If we weren't able to create a per-session DSM segment, then we can
241  * continue but we can't safely launch any workers because their
242  * record typmods would be incompatible so they couldn't exchange
243  * tuples.
244  */
245  if (session_dsm_handle == DSM_HANDLE_INVALID)
246  pcxt->nworkers = 0;
247  }
248 
249  if (pcxt->nworkers > 0)
250  {
251  /* Estimate space for various kinds of state sharing. */
252  library_len = EstimateLibraryStateSpace();
253  shm_toc_estimate_chunk(&pcxt->estimator, library_len);
254  guc_len = EstimateGUCStateSpace();
255  shm_toc_estimate_chunk(&pcxt->estimator, guc_len);
256  combocidlen = EstimateComboCIDStateSpace();
257  shm_toc_estimate_chunk(&pcxt->estimator, combocidlen);
258  tsnaplen = EstimateSnapshotSpace(transaction_snapshot);
259  shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen);
260  asnaplen = EstimateSnapshotSpace(active_snapshot);
261  shm_toc_estimate_chunk(&pcxt->estimator, asnaplen);
262  tstatelen = EstimateTransactionStateSpace();
263  shm_toc_estimate_chunk(&pcxt->estimator, tstatelen);
265  reindexlen = EstimateReindexStateSpace();
266  shm_toc_estimate_chunk(&pcxt->estimator, reindexlen);
267  /* If you add more chunks here, you probably need to add keys. */
268  shm_toc_estimate_keys(&pcxt->estimator, 8);
269 
270  /* Estimate space need for error queues. */
273  "parallel error queue size not buffer-aligned");
276  pcxt->nworkers));
277  shm_toc_estimate_keys(&pcxt->estimator, 1);
278 
279  /* Estimate how much we'll need for the entrypoint info. */
280  shm_toc_estimate_chunk(&pcxt->estimator, strlen(pcxt->library_name) +
281  strlen(pcxt->function_name) + 2);
282  shm_toc_estimate_keys(&pcxt->estimator, 1);
283  }
284 
285  /*
286  * Create DSM and initialize with new table of contents. But if the user
287  * didn't request any workers, then don't bother creating a dynamic shared
288  * memory segment; instead, just use backend-private memory.
289  *
290  * Also, if we can't create a dynamic shared memory segment because the
291  * maximum number of segments have already been created, then fall back to
292  * backend-private memory, and plan not to use any workers. We hope this
293  * won't happen very often, but it's better to abandon the use of
294  * parallelism than to fail outright.
295  */
296  segsize = shm_toc_estimate(&pcxt->estimator);
297  if (pcxt->nworkers > 0)
299  if (pcxt->seg != NULL)
301  dsm_segment_address(pcxt->seg),
302  segsize);
303  else
304  {
305  pcxt->nworkers = 0;
308  segsize);
309  }
310 
311  /* Initialize fixed-size state in shared memory. */
312  fps = (FixedParallelState *)
313  shm_toc_allocate(pcxt->toc, sizeof(FixedParallelState));
314  fps->database_id = MyDatabaseId;
324  SpinLockInit(&fps->mutex);
325  fps->last_xlog_end = 0;
327 
328  /* We can skip the rest of this if we're not budgeting for any workers. */
329  if (pcxt->nworkers > 0)
330  {
331  char *libraryspace;
332  char *gucspace;
333  char *combocidspace;
334  char *tsnapspace;
335  char *asnapspace;
336  char *tstatespace;
337  char *reindexspace;
338  char *error_queue_space;
339  char *session_dsm_handle_space;
340  char *entrypointstate;
341  Size lnamelen;
342 
343  /* Serialize shared libraries we have loaded. */
344  libraryspace = shm_toc_allocate(pcxt->toc, library_len);
345  SerializeLibraryState(library_len, libraryspace);
346  shm_toc_insert(pcxt->toc, PARALLEL_KEY_LIBRARY, libraryspace);
347 
348  /* Serialize GUC settings. */
349  gucspace = shm_toc_allocate(pcxt->toc, guc_len);
350  SerializeGUCState(guc_len, gucspace);
351  shm_toc_insert(pcxt->toc, PARALLEL_KEY_GUC, gucspace);
352 
353  /* Serialize combo CID state. */
354  combocidspace = shm_toc_allocate(pcxt->toc, combocidlen);
355  SerializeComboCIDState(combocidlen, combocidspace);
356  shm_toc_insert(pcxt->toc, PARALLEL_KEY_COMBO_CID, combocidspace);
357 
358  /* Serialize transaction snapshot and active snapshot. */
359  tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen);
360  SerializeSnapshot(transaction_snapshot, tsnapspace);
362  tsnapspace);
363  asnapspace = shm_toc_allocate(pcxt->toc, asnaplen);
364  SerializeSnapshot(active_snapshot, asnapspace);
365  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace);
366 
367  /* Provide the handle for per-session segment. */
368  session_dsm_handle_space = shm_toc_allocate(pcxt->toc,
369  sizeof(dsm_handle));
370  *(dsm_handle *) session_dsm_handle_space = session_dsm_handle;
372  session_dsm_handle_space);
373 
374  /* Serialize transaction state. */
375  tstatespace = shm_toc_allocate(pcxt->toc, tstatelen);
376  SerializeTransactionState(tstatelen, tstatespace);
378 
379  /* Serialize reindex state. */
380  reindexspace = shm_toc_allocate(pcxt->toc, reindexlen);
381  SerializeReindexState(reindexlen, reindexspace);
382  shm_toc_insert(pcxt->toc, PARALLEL_KEY_REINDEX_STATE, reindexspace);
383 
384  /* Allocate space for worker information. */
385  pcxt->worker = palloc0(sizeof(ParallelWorkerInfo) * pcxt->nworkers);
386 
387  /*
388  * Establish error queues in dynamic shared memory.
389  *
390  * These queues should be used only for transmitting ErrorResponse,
391  * NoticeResponse, and NotifyResponse protocol messages. Tuple data
392  * should be transmitted via separate (possibly larger?) queues.
393  */
394  error_queue_space =
395  shm_toc_allocate(pcxt->toc,
397  pcxt->nworkers));
398  for (i = 0; i < pcxt->nworkers; ++i)
399  {
400  char *start;
401  shm_mq *mq;
402 
403  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
404  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
406  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
407  }
408  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ERROR_QUEUE, error_queue_space);
409 
410  /*
411  * Serialize entrypoint information. It's unsafe to pass function
412  * pointers across processes, as the function pointer may be different
413  * in each process in EXEC_BACKEND builds, so we always pass library
414  * and function name. (We use library name "postgres" for functions
415  * in the core backend.)
416  */
417  lnamelen = strlen(pcxt->library_name);
418  entrypointstate = shm_toc_allocate(pcxt->toc, lnamelen +
419  strlen(pcxt->function_name) + 2);
420  strcpy(entrypointstate, pcxt->library_name);
421  strcpy(entrypointstate + lnamelen + 1, pcxt->function_name);
422  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
423  }
424 
425  /* Restore previous memory context. */
426  MemoryContextSwitchTo(oldcontext);
427 }
428 
429 /*
430  * Reinitialize the dynamic shared memory segment for a parallel context such
431  * that we could launch workers for it again.
432  */
433 void
435 {
436  FixedParallelState *fps;
437 
438  /* Wait for any old workers to exit. */
439  if (pcxt->nworkers_launched > 0)
440  {
443  pcxt->nworkers_launched = 0;
444  if (pcxt->known_attached_workers)
445  {
447  pcxt->known_attached_workers = NULL;
448  pcxt->nknown_attached_workers = 0;
449  }
450  }
451 
452  /* Reset a few bits of fixed parallel state to a clean state. */
453  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
454  fps->last_xlog_end = 0;
455 
456  /* Recreate error queues (if they exist). */
457  if (pcxt->nworkers > 0)
458  {
459  char *error_queue_space;
460  int i;
461 
462  error_queue_space =
464  for (i = 0; i < pcxt->nworkers; ++i)
465  {
466  char *start;
467  shm_mq *mq;
468 
469  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
470  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
472  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
473  }
474  }
475 }
476 
477 /*
478  * Launch parallel workers.
479  */
480 void
482 {
483  MemoryContext oldcontext;
484  BackgroundWorker worker;
485  int i;
486  bool any_registrations_failed = false;
487 
488  /* Skip this if we have no workers. */
489  if (pcxt->nworkers == 0)
490  return;
491 
492  /* We need to be a lock group leader. */
494 
495  /* If we do have workers, we'd better have a DSM segment. */
496  Assert(pcxt->seg != NULL);
497 
498  /* We might be running in a short-lived memory context. */
500 
501  /* Configure a worker. */
502  memset(&worker, 0, sizeof(worker));
503  snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
504  MyProcPid);
505  snprintf(worker.bgw_type, BGW_MAXLEN, "parallel worker");
506  worker.bgw_flags =
511  sprintf(worker.bgw_library_name, "postgres");
512  sprintf(worker.bgw_function_name, "ParallelWorkerMain");
514  worker.bgw_notify_pid = MyProcPid;
515 
516  /*
517  * Start workers.
518  *
519  * The caller must be able to tolerate ending up with fewer workers than
520  * expected, so there is no need to throw an error here if registration
521  * fails. It wouldn't help much anyway, because registering the worker in
522  * no way guarantees that it will start up and initialize successfully.
523  */
524  for (i = 0; i < pcxt->nworkers; ++i)
525  {
526  memcpy(worker.bgw_extra, &i, sizeof(int));
527  if (!any_registrations_failed &&
529  &pcxt->worker[i].bgwhandle))
530  {
532  pcxt->worker[i].bgwhandle);
533  pcxt->nworkers_launched++;
534  }
535  else
536  {
537  /*
538  * If we weren't able to register the worker, then we've bumped up
539  * against the max_worker_processes limit, and future
540  * registrations will probably fail too, so arrange to skip them.
541  * But we still have to execute this code for the remaining slots
542  * to make sure that we forget about the error queues we budgeted
543  * for those workers. Otherwise, we'll wait for them to start,
544  * but they never will.
545  */
546  any_registrations_failed = true;
547  pcxt->worker[i].bgwhandle = NULL;
548  shm_mq_detach(pcxt->worker[i].error_mqh);
549  pcxt->worker[i].error_mqh = NULL;
550  }
551  }
552 
553  /*
554  * Now that nworkers_launched has taken its final value, we can initialize
555  * known_attached_workers.
556  */
557  if (pcxt->nworkers_launched > 0)
558  {
559  pcxt->known_attached_workers =
560  palloc0(sizeof(bool) * pcxt->nworkers_launched);
561  pcxt->nknown_attached_workers = 0;
562  }
563 
564  /* Restore previous memory context. */
565  MemoryContextSwitchTo(oldcontext);
566 }
567 
568 /*
569  * Wait for all workers to attach to their error queues, and throw an error if
570  * any worker fails to do this.
571  *
572  * Callers can assume that if this function returns successfully, then the
573  * number of workers given by pcxt->nworkers_launched have initialized and
574  * attached to their error queues. Whether or not these workers are guaranteed
575  * to still be running depends on what code the caller asked them to run;
576  * this function does not guarantee that they have not exited. However, it
577  * does guarantee that any workers which exited must have done so cleanly and
578  * after successfully performing the work with which they were tasked.
579  *
580  * If this function is not called, then some of the workers that were launched
581  * may not have been started due to a fork() failure, or may have exited during
582  * early startup prior to attaching to the error queue, so nworkers_launched
583  * cannot be viewed as completely reliable. It will never be less than the
584  * number of workers which actually started, but it might be more. Any workers
585  * that failed to start will still be discovered by
586  * WaitForParallelWorkersToFinish and an error will be thrown at that time,
587  * provided that function is eventually reached.
588  *
589  * In general, the leader process should do as much work as possible before
590  * calling this function. fork() failures and other early-startup failures
591  * are very uncommon, and having the leader sit idle when it could be doing
592  * useful work is undesirable. However, if the leader needs to wait for
593  * all of its workers or for a specific worker, it may want to call this
594  * function before doing so. If not, it must make some other provision for
595  * the failure-to-start case, lest it wait forever. On the other hand, a
596  * leader which never waits for a worker that might not be started yet, or
597  * at least never does so prior to WaitForParallelWorkersToFinish(), need not
598  * call this function at all.
599  */
600 void
602 {
603  int i;
604 
605  /* Skip this if we have no launched workers. */
606  if (pcxt->nworkers_launched == 0)
607  return;
608 
609  for (;;)
610  {
611  /*
612  * This will process any parallel messages that are pending and it may
613  * also throw an error propagated from a worker.
614  */
616 
617  for (i = 0; i < pcxt->nworkers_launched; ++i)
618  {
620  shm_mq *mq;
621  int rc;
622  pid_t pid;
623 
624  if (pcxt->known_attached_workers[i])
625  continue;
626 
627  /*
628  * If error_mqh is NULL, then the worker has already exited
629  * cleanly.
630  */
631  if (pcxt->worker[i].error_mqh == NULL)
632  {
633  pcxt->known_attached_workers[i] = true;
634  ++pcxt->nknown_attached_workers;
635  continue;
636  }
637 
638  status = GetBackgroundWorkerPid(pcxt->worker[i].bgwhandle, &pid);
639  if (status == BGWH_STARTED)
640  {
641  /* Has the worker attached to the error queue? */
642  mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
643  if (shm_mq_get_sender(mq) != NULL)
644  {
645  /* Yes, so it is known to be attached. */
646  pcxt->known_attached_workers[i] = true;
647  ++pcxt->nknown_attached_workers;
648  }
649  }
650  else if (status == BGWH_STOPPED)
651  {
652  /*
653  * If the worker stopped without attaching to the error queue,
654  * throw an error.
655  */
656  mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
657  if (shm_mq_get_sender(mq) == NULL)
658  ereport(ERROR,
659  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
660  errmsg("parallel worker failed to initialize"),
661  errhint("More details may be available in the server log.")));
662 
663  pcxt->known_attached_workers[i] = true;
664  ++pcxt->nknown_attached_workers;
665  }
666  else
667  {
668  /*
669  * Worker not yet started, so we must wait. The postmaster
670  * will notify us if the worker's state changes. Our latch
671  * might also get set for some other reason, but if so we'll
672  * just end up waiting for the same worker again.
673  */
674  rc = WaitLatch(MyLatch,
677 
678  /* emergency bailout if postmaster has died */
679  if (rc & WL_POSTMASTER_DEATH)
680  proc_exit(1);
681 
682  if (rc & WL_LATCH_SET)
684  }
685  }
686 
687  /* If all workers are known to have started, we're done. */
688  if (pcxt->nknown_attached_workers >= pcxt->nworkers_launched)
689  {
691  break;
692  }
693  }
694 }
695 
696 /*
697  * Wait for all workers to finish computing.
698  *
699  * Even if the parallel operation seems to have completed successfully, it's
700  * important to call this function afterwards. We must not miss any errors
701  * the workers may have thrown during the parallel operation, or any that they
702  * may yet throw while shutting down.
703  *
704  * Also, we want to update our notion of XactLastRecEnd based on worker
705  * feedback.
706  */
707 void
709 {
710  for (;;)
711  {
712  bool anyone_alive = false;
713  int nfinished = 0;
714  int i;
715 
716  /*
717  * This will process any parallel messages that are pending, which may
718  * change the outcome of the loop that follows. It may also throw an
719  * error propagated from a worker.
720  */
722 
723  for (i = 0; i < pcxt->nworkers_launched; ++i)
724  {
725  /*
726  * If error_mqh is NULL, then the worker has already exited
727  * cleanly. If we have received a message through error_mqh from
728  * the worker, we know it started up cleanly, and therefore we're
729  * certain to be notified when it exits.
730  */
731  if (pcxt->worker[i].error_mqh == NULL)
732  ++nfinished;
733  else if (pcxt->known_attached_workers[i])
734  {
735  anyone_alive = true;
736  break;
737  }
738  }
739 
740  if (!anyone_alive)
741  {
742  /* If all workers are known to have finished, we're done. */
743  if (nfinished >= pcxt->nworkers_launched)
744  {
745  Assert(nfinished == pcxt->nworkers_launched);
746  break;
747  }
748 
749  /*
750  * We didn't detect any living workers, but not all workers are
751  * known to have exited cleanly. Either not all workers have
752  * launched yet, or maybe some of them failed to start or
753  * terminated abnormally.
754  */
755  for (i = 0; i < pcxt->nworkers_launched; ++i)
756  {
757  pid_t pid;
758  shm_mq *mq;
759 
760  /*
761  * If the worker is BGWH_NOT_YET_STARTED or BGWH_STARTED, we
762  * should just keep waiting. If it is BGWH_STOPPED, then
763  * further investigation is needed.
764  */
765  if (pcxt->worker[i].error_mqh == NULL ||
766  pcxt->worker[i].bgwhandle == NULL ||
768  &pid) != BGWH_STOPPED)
769  continue;
770 
771  /*
772  * Check whether the worker ended up stopped without ever
773  * attaching to the error queue. If so, the postmaster was
774  * unable to fork the worker or it exited without initializing
775  * properly. We must throw an error, since the caller may
776  * have been expecting the worker to do some work before
777  * exiting.
778  */
779  mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
780  if (shm_mq_get_sender(mq) == NULL)
781  ereport(ERROR,
782  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
783  errmsg("parallel worker failed to initialize"),
784  errhint("More details may be available in the server log.")));
785 
786  /*
787  * The worker is stopped, but is attached to the error queue.
788  * Unless there's a bug somewhere, this will only happen when
789  * the worker writes messages and terminates after the
790  * CHECK_FOR_INTERRUPTS() near the top of this function and
791  * before the call to GetBackgroundWorkerPid(). In that case,
792  * or latch should have been set as well and the right things
793  * will happen on the next pass through the loop.
794  */
795  }
796  }
797 
801  }
802 
803  if (pcxt->toc != NULL)
804  {
805  FixedParallelState *fps;
806 
807  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
808  if (fps->last_xlog_end > XactLastRecEnd)
810  }
811 }
812 
813 /*
814  * Wait for all workers to exit.
815  *
816  * This function ensures that workers have been completely shutdown. The
817  * difference between WaitForParallelWorkersToFinish and this function is
818  * that former just ensures that last message sent by worker backend is
819  * received by master backend whereas this ensures the complete shutdown.
820  */
821 static void
823 {
824  int i;
825 
826  /* Wait until the workers actually die. */
827  for (i = 0; i < pcxt->nworkers_launched; ++i)
828  {
830 
831  if (pcxt->worker == NULL || pcxt->worker[i].bgwhandle == NULL)
832  continue;
833 
835 
836  /*
837  * If the postmaster kicked the bucket, we have no chance of cleaning
838  * up safely -- we won't be able to tell when our workers are actually
839  * dead. This doesn't necessitate a PANIC since they will all abort
840  * eventually, but we can't safely continue this session.
841  */
842  if (status == BGWH_POSTMASTER_DIED)
843  ereport(FATAL,
844  (errcode(ERRCODE_ADMIN_SHUTDOWN),
845  errmsg("postmaster exited during a parallel transaction")));
846 
847  /* Release memory. */
848  pfree(pcxt->worker[i].bgwhandle);
849  pcxt->worker[i].bgwhandle = NULL;
850  }
851 }
852 
853 /*
854  * Destroy a parallel context.
855  *
856  * If expecting a clean exit, you should use WaitForParallelWorkersToFinish()
857  * first, before calling this function. When this function is invoked, any
858  * remaining workers are forcibly killed; the dynamic shared memory segment
859  * is unmapped; and we then wait (uninterruptibly) for the workers to exit.
860  */
861 void
863 {
864  int i;
865 
866  /*
867  * Be careful about order of operations here! We remove the parallel
868  * context from the list before we do anything else; otherwise, if an
869  * error occurs during a subsequent step, we might try to nuke it again
870  * from AtEOXact_Parallel or AtEOSubXact_Parallel.
871  */
872  dlist_delete(&pcxt->node);
873 
874  /* Kill each worker in turn, and forget their error queues. */
875  if (pcxt->worker != NULL)
876  {
877  for (i = 0; i < pcxt->nworkers_launched; ++i)
878  {
879  if (pcxt->worker[i].error_mqh != NULL)
880  {
882 
883  shm_mq_detach(pcxt->worker[i].error_mqh);
884  pcxt->worker[i].error_mqh = NULL;
885  }
886  }
887  }
888 
889  /*
890  * If we have allocated a shared memory segment, detach it. This will
891  * implicitly detach the error queues, and any other shared memory queues,
892  * stored there.
893  */
894  if (pcxt->seg != NULL)
895  {
896  dsm_detach(pcxt->seg);
897  pcxt->seg = NULL;
898  }
899 
900  /*
901  * If this parallel context is actually in backend-private memory rather
902  * than shared memory, free that memory instead.
903  */
904  if (pcxt->private_memory != NULL)
905  {
906  pfree(pcxt->private_memory);
907  pcxt->private_memory = NULL;
908  }
909 
910  /*
911  * We can't finish transaction commit or abort until all of the workers
912  * have exited. This means, in particular, that we can't respond to
913  * interrupts at this stage.
914  */
915  HOLD_INTERRUPTS();
918 
919  /* Free the worker array itself. */
920  if (pcxt->worker != NULL)
921  {
922  pfree(pcxt->worker);
923  pcxt->worker = NULL;
924  }
925 
926  /* Free memory. */
927  pfree(pcxt->library_name);
928  pfree(pcxt->function_name);
929  pfree(pcxt);
930 }
931 
932 /*
933  * Are there any parallel contexts currently active?
934  */
935 bool
937 {
938  return !dlist_is_empty(&pcxt_list);
939 }
940 
941 /*
942  * Handle receipt of an interrupt indicating a parallel worker message.
943  *
944  * Note: this is called within a signal handler! All we can do is set
945  * a flag that will cause the next CHECK_FOR_INTERRUPTS() to invoke
946  * HandleParallelMessages().
947  */
948 void
950 {
951  InterruptPending = true;
952  ParallelMessagePending = true;
953  SetLatch(MyLatch);
954 }
955 
956 /*
957  * Handle any queued protocol messages received from parallel workers.
958  */
959 void
961 {
962  dlist_iter iter;
963  MemoryContext oldcontext;
964 
965  static MemoryContext hpm_context = NULL;
966 
967  /*
968  * This is invoked from ProcessInterrupts(), and since some of the
969  * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
970  * for recursive calls if more signals are received while this runs. It's
971  * unclear that recursive entry would be safe, and it doesn't seem useful
972  * even if it is safe, so let's block interrupts until done.
973  */
974  HOLD_INTERRUPTS();
975 
976  /*
977  * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
978  * don't want to risk leaking data into long-lived contexts, so let's do
979  * our work here in a private context that we can reset on each use.
980  */
981  if (hpm_context == NULL) /* first time through? */
983  "HandleParallelMessages",
985  else
986  MemoryContextReset(hpm_context);
987 
988  oldcontext = MemoryContextSwitchTo(hpm_context);
989 
990  /* OK to process messages. Reset the flag saying there are more to do. */
991  ParallelMessagePending = false;
992 
993  dlist_foreach(iter, &pcxt_list)
994  {
995  ParallelContext *pcxt;
996  int i;
997 
998  pcxt = dlist_container(ParallelContext, node, iter.cur);
999  if (pcxt->worker == NULL)
1000  continue;
1001 
1002  for (i = 0; i < pcxt->nworkers_launched; ++i)
1003  {
1004  /*
1005  * Read as many messages as we can from each worker, but stop when
1006  * either (1) the worker's error queue goes away, which can happen
1007  * if we receive a Terminate message from the worker; or (2) no
1008  * more messages can be read from the worker without blocking.
1009  */
1010  while (pcxt->worker[i].error_mqh != NULL)
1011  {
1012  shm_mq_result res;
1013  Size nbytes;
1014  void *data;
1015 
1016  res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes,
1017  &data, true);
1018  if (res == SHM_MQ_WOULD_BLOCK)
1019  break;
1020  else if (res == SHM_MQ_SUCCESS)
1021  {
1022  StringInfoData msg;
1023 
1024  initStringInfo(&msg);
1025  appendBinaryStringInfo(&msg, data, nbytes);
1026  HandleParallelMessage(pcxt, i, &msg);
1027  pfree(msg.data);
1028  }
1029  else
1030  ereport(ERROR,
1031  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1032  errmsg("lost connection to parallel worker")));
1033  }
1034  }
1035  }
1036 
1037  MemoryContextSwitchTo(oldcontext);
1038 
1039  /* Might as well clear the context on our way out */
1040  MemoryContextReset(hpm_context);
1041 
1043 }
1044 
1045 /*
1046  * Handle a single protocol message received from a single parallel worker.
1047  */
1048 static void
1050 {
1051  char msgtype;
1052 
1053  if (pcxt->known_attached_workers != NULL &&
1054  !pcxt->known_attached_workers[i])
1055  {
1056  pcxt->known_attached_workers[i] = true;
1057  pcxt->nknown_attached_workers++;
1058  }
1059 
1060  msgtype = pq_getmsgbyte(msg);
1061 
1062  switch (msgtype)
1063  {
1064  case 'K': /* BackendKeyData */
1065  {
1066  int32 pid = pq_getmsgint(msg, 4);
1067 
1068  (void) pq_getmsgint(msg, 4); /* discard cancel key */
1069  (void) pq_getmsgend(msg);
1070  pcxt->worker[i].pid = pid;
1071  break;
1072  }
1073 
1074  case 'E': /* ErrorResponse */
1075  case 'N': /* NoticeResponse */
1076  {
1077  ErrorData edata;
1078  ErrorContextCallback *save_error_context_stack;
1079 
1080  /* Parse ErrorResponse or NoticeResponse. */
1081  pq_parse_errornotice(msg, &edata);
1082 
1083  /* Death of a worker isn't enough justification for suicide. */
1084  edata.elevel = Min(edata.elevel, ERROR);
1085 
1086  /*
1087  * If desired, add a context line to show that this is a
1088  * message propagated from a parallel worker. Otherwise, it
1089  * can sometimes be confusing to understand what actually
1090  * happened. (We don't do this in FORCE_PARALLEL_REGRESS mode
1091  * because it causes test-result instability depending on
1092  * whether a parallel worker is actually used or not.)
1093  */
1095  {
1096  if (edata.context)
1097  edata.context = psprintf("%s\n%s", edata.context,
1098  _("parallel worker"));
1099  else
1100  edata.context = pstrdup(_("parallel worker"));
1101  }
1102 
1103  /*
1104  * Context beyond that should use the error context callbacks
1105  * that were in effect when the ParallelContext was created,
1106  * not the current ones.
1107  */
1108  save_error_context_stack = error_context_stack;
1110 
1111  /* Rethrow error or print notice. */
1112  ThrowErrorData(&edata);
1113 
1114  /* Not an error, so restore previous context stack. */
1115  error_context_stack = save_error_context_stack;
1116 
1117  break;
1118  }
1119 
1120  case 'A': /* NotifyResponse */
1121  {
1122  /* Propagate NotifyResponse. */
1123  int32 pid;
1124  const char *channel;
1125  const char *payload;
1126 
1127  pid = pq_getmsgint(msg, 4);
1128  channel = pq_getmsgrawstring(msg);
1129  payload = pq_getmsgrawstring(msg);
1130  pq_endmessage(msg);
1131 
1132  NotifyMyFrontEnd(channel, payload, pid);
1133 
1134  break;
1135  }
1136 
1137  case 'X': /* Terminate, indicating clean exit */
1138  {
1139  shm_mq_detach(pcxt->worker[i].error_mqh);
1140  pcxt->worker[i].error_mqh = NULL;
1141  break;
1142  }
1143 
1144  default:
1145  {
1146  elog(ERROR, "unrecognized message type received from parallel worker: %c (message length %d bytes)",
1147  msgtype, msg->len);
1148  }
1149  }
1150 }
1151 
1152 /*
1153  * End-of-subtransaction cleanup for parallel contexts.
1154  *
1155  * Currently, it's forbidden to enter or leave a subtransaction while
1156  * parallel mode is in effect, so we could just blow away everything. But
1157  * we may want to relax that restriction in the future, so this code
1158  * contemplates that there may be multiple subtransaction IDs in pcxt_list.
1159  */
1160 void
1162 {
1163  while (!dlist_is_empty(&pcxt_list))
1164  {
1165  ParallelContext *pcxt;
1166 
1167  pcxt = dlist_head_element(ParallelContext, node, &pcxt_list);
1168  if (pcxt->subid != mySubId)
1169  break;
1170  if (isCommit)
1171  elog(WARNING, "leaked parallel context");
1172  DestroyParallelContext(pcxt);
1173  }
1174 }
1175 
1176 /*
1177  * End-of-transaction cleanup for parallel contexts.
1178  */
1179 void
1180 AtEOXact_Parallel(bool isCommit)
1181 {
1182  while (!dlist_is_empty(&pcxt_list))
1183  {
1184  ParallelContext *pcxt;
1185 
1186  pcxt = dlist_head_element(ParallelContext, node, &pcxt_list);
1187  if (isCommit)
1188  elog(WARNING, "leaked parallel context");
1189  DestroyParallelContext(pcxt);
1190  }
1191 }
1192 
1193 /*
1194  * Main entrypoint for parallel workers.
1195  */
1196 void
1198 {
1199  dsm_segment *seg;
1200  shm_toc *toc;
1201  FixedParallelState *fps;
1202  char *error_queue_space;
1203  shm_mq *mq;
1204  shm_mq_handle *mqh;
1205  char *libraryspace;
1206  char *entrypointstate;
1207  char *library_name;
1208  char *function_name;
1209  parallel_worker_main_type entrypt;
1210  char *gucspace;
1211  char *combocidspace;
1212  char *tsnapspace;
1213  char *asnapspace;
1214  char *tstatespace;
1215  char *reindexspace;
1216  StringInfoData msgbuf;
1217  char *session_dsm_handle_space;
1218 
1219  /* Set flag to indicate that we're initializing a parallel worker. */
1221 
1222  /* Establish signal handlers. */
1223  pqsignal(SIGTERM, die);
1225 
1226  /* Determine and set our parallel worker number. */
1228  memcpy(&ParallelWorkerNumber, MyBgworkerEntry->bgw_extra, sizeof(int));
1229 
1230  /* Set up a memory context and resource owner. */
1231  Assert(CurrentResourceOwner == NULL);
1232  CurrentResourceOwner = ResourceOwnerCreate(NULL, "parallel toplevel");
1234  "Parallel worker",
1236 
1237  /*
1238  * Now that we have a resource owner, we can attach to the dynamic shared
1239  * memory segment and read the table of contents.
1240  */
1241  seg = dsm_attach(DatumGetUInt32(main_arg));
1242  if (seg == NULL)
1243  ereport(ERROR,
1244  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1245  errmsg("could not map dynamic shared memory segment")));
1247  if (toc == NULL)
1248  ereport(ERROR,
1249  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1250  errmsg("invalid magic number in dynamic shared memory segment")));
1251 
1252  /* Look up fixed parallel state. */
1253  fps = shm_toc_lookup(toc, PARALLEL_KEY_FIXED, false);
1254  MyFixedParallelState = fps;
1255 
1256  /* Arrange to signal the leader if we exit. */
1260 
1261  /*
1262  * Now we can find and attach to the error queue provided for us. That's
1263  * good, because until we do that, any errors that happen here will not be
1264  * reported back to the process that requested that this worker be
1265  * launched.
1266  */
1267  error_queue_space = shm_toc_lookup(toc, PARALLEL_KEY_ERROR_QUEUE, false);
1268  mq = (shm_mq *) (error_queue_space +
1271  mqh = shm_mq_attach(mq, seg, NULL);
1272  pq_redirect_to_shm_mq(seg, mqh);
1275 
1276  /*
1277  * Send a BackendKeyData message to the process that initiated parallelism
1278  * so that it has access to our PID before it receives any other messages
1279  * from us. Our cancel key is sent, too, since that's the way the
1280  * protocol message is defined, but it won't actually be used for anything
1281  * in this case.
1282  */
1283  pq_beginmessage(&msgbuf, 'K');
1284  pq_sendint32(&msgbuf, (int32) MyProcPid);
1285  pq_sendint32(&msgbuf, (int32) MyCancelKey);
1286  pq_endmessage(&msgbuf);
1287 
1288  /*
1289  * Hooray! Primary initialization is complete. Now, we need to set up our
1290  * backend-local state to match the original backend.
1291  */
1292 
1293  /*
1294  * Join locking group. We must do this before anything that could try to
1295  * acquire a heavyweight lock, because any heavyweight locks acquired to
1296  * this point could block either directly against the parallel group
1297  * leader or against some process which in turn waits for a lock that
1298  * conflicts with the parallel group leader, causing an undetected
1299  * deadlock. (If we can't join the lock group, the leader has gone away,
1300  * so just exit quietly.)
1301  */
1303  fps->parallel_master_pid))
1304  return;
1305 
1306  /*
1307  * Load libraries that were loaded by original backend. We want to do
1308  * this before restoring GUCs, because the libraries might define custom
1309  * variables.
1310  */
1311  libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY, false);
1312  RestoreLibraryState(libraryspace);
1313 
1314  /*
1315  * Identify the entry point to be called. In theory this could result in
1316  * loading an additional library, though most likely the entry point is in
1317  * the core backend or in a library we just loaded.
1318  */
1319  entrypointstate = shm_toc_lookup(toc, PARALLEL_KEY_ENTRYPOINT, false);
1320  library_name = entrypointstate;
1321  function_name = entrypointstate + strlen(library_name) + 1;
1322 
1323  entrypt = LookupParallelWorkerFunction(library_name, function_name);
1324 
1325  /* Restore database connection. */
1327  fps->authenticated_user_id,
1328  0);
1329 
1330  /*
1331  * Set the client encoding to the database encoding, since that is what
1332  * the leader will expect.
1333  */
1335 
1336  /* Restore GUC values from launching backend. */
1337  gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1339  RestoreGUCState(gucspace);
1341 
1342  /* Crank up a transaction state appropriate to a parallel worker. */
1343  tstatespace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_STATE, false);
1344  StartParallelWorkerTransaction(tstatespace);
1345 
1346  /* Restore combo CID state. */
1347  combocidspace = shm_toc_lookup(toc, PARALLEL_KEY_COMBO_CID, false);
1348  RestoreComboCIDState(combocidspace);
1349 
1350  /* Attach to the per-session DSM segment and contained objects. */
1351  session_dsm_handle_space =
1353  AttachSession(*(dsm_handle *) session_dsm_handle_space);
1354 
1355  /* Restore transaction snapshot. */
1356  tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, false);
1358  fps->parallel_master_pgproc);
1359 
1360  /* Restore active snapshot. */
1361  asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, false);
1362  PushActiveSnapshot(RestoreSnapshot(asnapspace));
1363 
1364  /*
1365  * We've changed which tuples we can see, and must therefore invalidate
1366  * system caches.
1367  */
1369 
1370  /*
1371  * Restore current role id. Skip verifying whether session user is
1372  * allowed to become this role and blindly restore the leader's state for
1373  * current role.
1374  */
1376 
1377  /* Restore user ID and security context. */
1379 
1380  /* Restore temp-namespace state to ensure search path matches leader's. */
1383 
1384  /* Restore reindex state. */
1385  reindexspace = shm_toc_lookup(toc, PARALLEL_KEY_REINDEX_STATE, false);
1386  RestoreReindexState(reindexspace);
1387 
1388  /*
1389  * We've initialized all of our state now; nothing should change
1390  * hereafter.
1391  */
1394 
1395  /*
1396  * Time to do the real work: invoke the caller-supplied code.
1397  */
1398  entrypt(seg, toc);
1399 
1400  /* Must exit parallel mode to pop active snapshot. */
1401  ExitParallelMode();
1402 
1403  /* Must pop active snapshot so resowner.c doesn't complain. */
1405 
1406  /* Shut down the parallel-worker transaction. */
1408 
1409  /* Detach from the per-session DSM segment. */
1410  DetachSession();
1411 
1412  /* Report success. */
1413  pq_putmessage('X', NULL, 0);
1414 }
1415 
1416 /*
1417  * Update shared memory with the ending location of the last WAL record we
1418  * wrote, if it's greater than the value already stored there.
1419  */
1420 void
1422 {
1424 
1425  Assert(fps != NULL);
1426  SpinLockAcquire(&fps->mutex);
1427  if (fps->last_xlog_end < last_xlog_end)
1429  SpinLockRelease(&fps->mutex);
1430 }
1431 
1432 /*
1433  * Make sure the leader tries to read from our error queue one more time.
1434  * This guards against the case where we exit uncleanly without sending an
1435  * ErrorResponse to the leader, for example because some code calls proc_exit
1436  * directly.
1437  */
1438 static void
1440 {
1444 }
1445 
1446 /*
1447  * Look up (and possibly load) a parallel worker entry point function.
1448  *
1449  * For functions contained in the core code, we use library name "postgres"
1450  * and consult the InternalParallelWorkers array. External functions are
1451  * looked up, and loaded if necessary, using load_external_function().
1452  *
1453  * The point of this is to pass function names as strings across process
1454  * boundaries. We can't pass actual function addresses because of the
1455  * possibility that the function has been loaded at a different address
1456  * in a different process. This is obviously a hazard for functions in
1457  * loadable libraries, but it can happen even for functions in the core code
1458  * on platforms using EXEC_BACKEND (e.g., Windows).
1459  *
1460  * At some point it might be worthwhile to get rid of InternalParallelWorkers[]
1461  * in favor of applying load_external_function() for core functions too;
1462  * but that raises portability issues that are not worth addressing now.
1463  */
1465 LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
1466 {
1467  /*
1468  * If the function is to be loaded from postgres itself, search the
1469  * InternalParallelWorkers array.
1470  */
1471  if (strcmp(libraryname, "postgres") == 0)
1472  {
1473  int i;
1474 
1475  for (i = 0; i < lengthof(InternalParallelWorkers); i++)
1476  {
1477  if (strcmp(InternalParallelWorkers[i].fn_name, funcname) == 0)
1478  return InternalParallelWorkers[i].fn_addr;
1479  }
1480 
1481  /* We can only reach this by programming error. */
1482  elog(ERROR, "internal function \"%s\" not found", funcname);
1483  }
1484 
1485  /* Otherwise load from external library. */
1486  return (parallel_worker_main_type)
1487  load_external_function(libraryname, funcname, true, NULL);
1488 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
#define DatumGetUInt32(X)
Definition: postgres.h:469
int slock_t
Definition: s_lock.h:912
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:52
static const struct @21 InternalParallelWorkers[]
shm_toc * shm_toc_create(uint64 magic, void *address, Size nbytes)
Definition: shm_toc.c:40
int MyProcPid
Definition: globals.c:42
int errhint(const char *fmt,...)
Definition: elog.c:987
BackendId MyBackendId
Definition: globals.c:82
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2127
MemoryContext TopTransactionContext
Definition: mcxt.c:49
uint32 dsm_handle
Definition: dsm_impl.h:55
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:491
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
XLogRecPtr XactLastRecEnd
Definition: xlog.c:339
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:793
PGPROC * MyProc
Definition: proc.c:67
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
dsm_segment * seg
Definition: parallel.h:42
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:822
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
void SerializeReindexState(Size maxsize, char *start_address)
Definition: index.c:4117
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void CommitTransactionCommand(void)
Definition: xact.c:2744
shm_toc_estimator estimator
Definition: parallel.h:41
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void EndParallelWorkerTransaction(void)
Definition: xact.c:5062
#define SpinLockInit(lock)
Definition: spin.h:60
void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
Definition: namespace.c:3253
void RestoreTransactionSnapshot(Snapshot snapshot, void *master_pgproc)
Definition: snapmgr.c:2191
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:548
void _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
Definition: nbtsort.c:1528
#define Min(x, y)
Definition: c.h:857
PGPROC * shm_mq_get_sender(shm_mq *mq)
Definition: shm_mq.c:249
Oid authenticated_user_id
Definition: parallel.c:79
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers, bool serializable_okay)
Definition: parallel.c:152
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1015
int bgw_restart_time
Definition: bgworker.h:94
void proc_exit(int code)
Definition: ipc.c:104
int errcode(int sqlerrcode)
Definition: elog.c:575
PGPROC * parallel_master_pgproc
Definition: parallel.c:86
Oid temp_toast_namespace_id
Definition: parallel.c:83
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
void DetachSession(void)
Definition: session.c:201
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:191
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1854
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
int nknown_attached_workers
Definition: parallel.h:46
uint32 SubTransactionId
Definition: c.h:478
Size shm_toc_estimate(shm_toc_estimator *e)
Definition: shm_toc.c:263
#define lengthof(array)
Definition: c.h:629
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
void SerializeTransactionState(Size maxsize, char *start_address)
Definition: xact.c:4971
parallel_worker_main_type fn_addr
Definition: parallel.c:127
const char * pq_getmsgrawstring(StringInfo msg)
Definition: pqformat.c:610
unsigned int Oid
Definition: postgres_ext.h:31
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
void InvalidateSystemCaches(void)
Definition: inval.c:641
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
static pid_t ParallelMasterPid
Definition: parallel.c:118
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
void RestoreComboCIDState(char *comboCIDstate)
Definition: combocid.c:344
signed int int32
Definition: c.h:313
SubTransactionId subid
Definition: parallel.h:35
Oid GetCurrentRoleId(void)
Definition: miscinit.c:731
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
ErrorContextCallback * error_context_stack
Definition: elog.c:88
volatile bool ParallelMessagePending
Definition: parallel.c:106
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:795
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:708
#define SpinLockAcquire(lock)
Definition: spin.h:62
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:862
int SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
Definition: procsignal.c:180
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
ParallelWorkerInfo * worker
Definition: parallel.h:45
Datum bgw_main_arg
Definition: bgworker.h:97
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
void pfree(void *pointer)
Definition: mcxt.c:1031
bool IsInParallelMode(void)
Definition: xact.c:905
void SerializeLibraryState(Size maxsize, char *start_address)
Definition: dfmgr.c:728
#define ERROR
Definition: elog.h:43
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1145
void ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
Oid GetAuthenticatedUserId(void)
Definition: miscinit.c:438
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:67
char * function_name
Definition: parallel.h:39
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2068
int32 MyCancelKey
Definition: globals.c:45
void pq_parse_errornotice(StringInfo msg, ErrorData *edata)
Definition: pqmq.c:215
#define FATAL
Definition: elog.h:52
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:169
void ExitParallelMode(void)
Definition: xact.c:885
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define PARALLEL_KEY_FIXED
Definition: parallel.c:62
void HandleParallelMessages(void)
Definition: parallel.c:960
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:63
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:359
void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
Definition: namespace.c:3269
static void HandleParallelMessage(ParallelContext *pcxt, int i, StringInfo msg)
Definition: parallel.c:1049
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:484
shm_mq_handle * error_mqh
Definition: parallel.h:28
#define PARALLEL_KEY_GUC
Definition: parallel.c:65
int SetClientEncoding(int encoding)
Definition: mbutils.c:201
int ParallelWorkerNumber
Definition: parallel.c:103
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int dynamic_shared_memory_type
Definition: dsm_impl.c:114
Size EstimateGUCStateSpace(void)
Definition: guc.c:9352
void AttachSession(dsm_handle handle)
Definition: session.c:155
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define shm_toc_initialize_estimator(e)
Definition: shm_toc.h:49
Size EstimateComboCIDStateSpace(void)
Definition: combocid.c:299
#define UInt32GetDatum(X)
Definition: postgres.h:476
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void ParallelWorkerShutdown(int code, Datum arg)
Definition: parallel.c:1439
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
int nworkers_launched
Definition: parallel.h:37
XLogRecPtr last_xlog_end
Definition: parallel.c:94
BgwHandleStatus
Definition: bgworker.h:102
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
void LaunchParallelWorkers(ParallelContext *pcxt)
Definition: parallel.c:481
Size EstimateReindexStateSpace(void)
Definition: index.c:4106
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:216
void BecomeLockGroupLeader(void)
Definition: proc.c:1824
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void ThrowErrorData(ErrorData *edata)
Definition: elog.c:1612
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define DLIST_STATIC_INIT(name)
Definition: ilist.h:248
#define WARNING
Definition: elog.h:40
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:206
int elevel
Definition: elog.h:331
bool * known_attached_workers
Definition: parallel.h:47
bool ParallelContextActive(void)
Definition: parallel.c:936
#define SpinLockRelease(lock)
Definition: spin.h:64
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
Size EstimateSnapshotSpace(Snapshot snap)
Definition: snapmgr.c:2044
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
void * palloc0(Size size)
Definition: mcxt.c:955
static parallel_worker_main_type LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
Definition: parallel.c:1465
void RestoreLibraryState(char *start_address)
Definition: dfmgr.c:750
uintptr_t Datum
Definition: postgres.h:365
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:452
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition: shm_toc.c:64
int GetDatabaseEncoding(void)
Definition: mbutils.c:1004
int BackendId
Definition: backendid.h:21
Oid MyDatabaseId
Definition: globals.c:86
pid_t parallel_master_pid
Definition: parallel.c:87
Size EstimateLibraryStateSpace(void)
Definition: dfmgr.c:711
void ReinitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:434
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:310
dlist_node * cur
Definition: ilist.h:161
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5560
#define PARALLEL_MAGIC
Definition: parallel.c:55
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition: pqmq.c:55
void ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
Definition: parallel.c:1421
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1184
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
volatile bool InterruptPending
Definition: globals.c:32
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:401
shm_mq_result
Definition: shm_mq.h:36
char * library_name
Definition: parallel.h:38
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
BackendId parallel_master_backend_id
Definition: parallel.c:88
int force_parallel_mode
Definition: planner.c:64
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:987
uint64 XLogRecPtr
Definition: xlogdefs.h:21
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:699
BackendId ParallelMasterBackendId
Definition: globals.c:84
void StartParallelWorkerTransaction(char *tstatespace)
Definition: xact.c:5039
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:641
Size EstimateTransactionStateSpace(void)
Definition: xact.c:4938
void StartTransactionCommand(void)
Definition: xact.c:2673
#define PARALLEL_KEY_REINDEX_STATE
Definition: parallel.c:72
const char * fn_name
Definition: parallel.c:126
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
#define BGW_MAXLEN
Definition: bgworker.h:85
size_t Size
Definition: c.h:433
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
dlist_node node
Definition: parallel.h:34
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
shm_mq * shm_mq_get_queue(shm_mq_handle *mqh)
Definition: shm_mq.c:848
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:939
void EnterParallelMode(void)
Definition: xact.c:872
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
char * context
Definition: elog.h:347
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:282
ErrorContextCallback * error_context_stack
Definition: parallel.h:40
void pq_set_parallel_master(pid_t pid, BackendId backend_id)
Definition: pqmq.c:80
#define PARALLEL_KEY_TRANSACTION_STATE
Definition: parallel.c:69
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:713
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void RestoreReindexState(void *reindexstate)
Definition: index.c:4135
int errmsg(const char *fmt,...)
Definition: elog.c:797
void(* parallel_worker_main_type)(dsm_segment *seg, shm_toc *toc)
Definition: parallel.h:23
#define IsolationIsSerializable()
Definition: xact.h:51
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:1197
pid_t bgw_notify_pid
Definition: bgworker.h:99
static FixedParallelState * MyFixedParallelState
Definition: parallel.c:112
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
void die(SIGNAL_ARGS)
Definition: postgres.c:2657
void SetCurrentRoleId(Oid roleid, bool is_superuser)
Definition: miscinit.c:752
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
bool InitializingParallelWorker
Definition: parallel.c:109
int i
Definition: shm_mq.c:70
#define PARALLEL_KEY_SESSION_DSM
Definition: parallel.c:71
#define BUFFERALIGN(LEN)
Definition: c.h:654
void * arg
struct Latch * MyLatch
Definition: globals.c:55
void HandleParallelMessageInterrupt(void)
Definition: parallel.c:949
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:198
static dlist_head pcxt_list
Definition: parallel.c:115
#define elog
Definition: elog.h:219
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:637
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:42
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
struct FixedParallelState FixedParallelState
dsm_handle GetSessionDsmHandle(void)
Definition: session.c:70
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition: shm_mq.c:540
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95
void WaitForParallelWorkersToAttach(ParallelContext *pcxt)
Definition: parallel.c:601
bool session_auth_is_superuser
Definition: guc.c:451
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1180
Definition: proc.h:95
#define PARALLEL_KEY_ENTRYPOINT
Definition: parallel.c:70
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:66
#define WL_LATCH_SET
Definition: latch.h:124
#define _(x)
Definition: elog.c:84
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1161
void SerializeGUCState(Size maxsize, char *start_address)
Definition: guc.c:9499
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:68
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
void SerializeComboCIDState(Size maxsize, char *start_address)
Definition: combocid.c:318
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:64
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1051
void RestoreGUCState(void *gucstate)
Definition: guc.c:9564
shm_toc * toc
Definition: parallel.h:44
void NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
Definition: async.c:2096
void * private_memory
Definition: parallel.h:43
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:418
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5589