PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parallel.h File Reference
#include "access/xlogdefs.h"
#include "lib/ilist.h"
#include "postmaster/bgworker.h"
#include "storage/shm_mq.h"
#include "storage/shm_toc.h"
Include dependency graph for parallel.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ParallelWorkerInfo
 
struct  ParallelContext
 

Macros

#define IsParallelWorker()   (ParallelWorkerNumber >= 0)
 

Typedefs

typedef void(* parallel_worker_main_type )(dsm_segment *seg, shm_toc *toc)
 
typedef struct ParallelWorkerInfo ParallelWorkerInfo
 
typedef struct ParallelContext ParallelContext
 

Functions

ParallelContextCreateParallelContext (const char *library_name, const char *function_name, int nworkers)
 
void InitializeParallelDSM (ParallelContext *pcxt)
 
void ReinitializeParallelDSM (ParallelContext *pcxt)
 
void LaunchParallelWorkers (ParallelContext *pcxt)
 
void WaitForParallelWorkersToFinish (ParallelContext *pcxt)
 
void DestroyParallelContext (ParallelContext *pcxt)
 
bool ParallelContextActive (void)
 
void HandleParallelMessageInterrupt (void)
 
void HandleParallelMessages (void)
 
void AtEOXact_Parallel (bool isCommit)
 
void AtEOSubXact_Parallel (bool isCommit, SubTransactionId mySubId)
 
void ParallelWorkerReportLastRecEnd (XLogRecPtr last_xlog_end)
 
void ParallelWorkerMain (Datum main_arg)
 

Variables

volatile bool ParallelMessagePending
 
int ParallelWorkerNumber
 
bool InitializingParallelWorker
 

Macro Definition Documentation

Typedef Documentation

typedef void(* parallel_worker_main_type)(dsm_segment *seg, shm_toc *toc)

Definition at line 23 of file parallel.h.

Function Documentation

void AtEOSubXact_Parallel ( bool  isCommit,
SubTransactionId  mySubId 
)

Definition at line 886 of file parallel.c.

References DestroyParallelContext(), dlist_head_element, dlist_is_empty(), elog, ParallelContext::subid, and WARNING.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

887 {
888  while (!dlist_is_empty(&pcxt_list))
889  {
890  ParallelContext *pcxt;
891 
893  if (pcxt->subid != mySubId)
894  break;
895  if (isCommit)
896  elog(WARNING, "leaked parallel context");
898  }
899 }
SubTransactionId subid
Definition: parallel.h:35
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:594
#define WARNING
Definition: elog.h:40
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static dlist_head pcxt_list
Definition: parallel.c:106
#define elog
Definition: elog.h:219
void AtEOXact_Parallel ( bool  isCommit)

Definition at line 905 of file parallel.c.

References DestroyParallelContext(), dlist_head_element, dlist_is_empty(), elog, and WARNING.

Referenced by AbortTransaction(), and CommitTransaction().

906 {
907  while (!dlist_is_empty(&pcxt_list))
908  {
909  ParallelContext *pcxt;
910 
912  if (isCommit)
913  elog(WARNING, "leaked parallel context");
915  }
916 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:594
#define WARNING
Definition: elog.h:40
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static dlist_head pcxt_list
Definition: parallel.c:106
#define elog
Definition: elog.h:219
ParallelContext* CreateParallelContext ( const char *  library_name,
const char *  function_name,
int  nworkers 
)

Definition at line 136 of file parallel.c.

References Assert, dlist_push_head(), DSM_IMPL_NONE, dynamic_shared_memory_type, ParallelContext::error_context_stack, error_context_stack, ParallelContext::estimator, ParallelContext::function_name, GetCurrentSubTransactionId(), IsInParallelMode(), IsolationIsSerializable, ParallelContext::library_name, MemoryContextSwitchTo(), ParallelContext::node, ParallelContext::nworkers, palloc0(), pstrdup(), shm_toc_initialize_estimator, ParallelContext::subid, and TopTransactionContext.

Referenced by ExecInitParallelPlan().

138 {
139  MemoryContext oldcontext;
140  ParallelContext *pcxt;
141 
142  /* It is unsafe to create a parallel context if not in parallel mode. */
144 
145  /* Number of workers should be non-negative. */
146  Assert(nworkers >= 0);
147 
148  /*
149  * If dynamic shared memory is not available, we won't be able to use
150  * background workers.
151  */
153  nworkers = 0;
154 
155  /*
156  * If we are running under serializable isolation, we can't use parallel
157  * workers, at least not until somebody enhances that mechanism to be
158  * parallel-aware.
159  */
161  nworkers = 0;
162 
163  /* We might be running in a short-lived memory context. */
165 
166  /* Initialize a new ParallelContext. */
167  pcxt = palloc0(sizeof(ParallelContext));
169  pcxt->nworkers = nworkers;
170  pcxt->library_name = pstrdup(library_name);
171  pcxt->function_name = pstrdup(function_name);
174  dlist_push_head(&pcxt_list, &pcxt->node);
175 
176  /* Restore previous memory context. */
177  MemoryContextSwitchTo(oldcontext);
178 
179  return pcxt;
180 }
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
char * pstrdup(const char *in)
Definition: mcxt.c:1077
shm_toc_estimator estimator
Definition: parallel.h:41
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SubTransactionId subid
Definition: parallel.h:35
ErrorContextCallback * error_context_stack
Definition: elog.c:88
bool IsInParallelMode(void)
Definition: xact.c:913
char * function_name
Definition: parallel.h:39
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
#define shm_toc_initialize_estimator(e)
Definition: shm_toc.h:49
void * palloc0(Size size)
Definition: mcxt.c:878
char * library_name
Definition: parallel.h:38
#define Assert(condition)
Definition: c.h:676
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
dlist_node node
Definition: parallel.h:34
ErrorContextCallback * error_context_stack
Definition: parallel.h:40
#define IsolationIsSerializable()
Definition: xact.h:44
static dlist_head pcxt_list
Definition: parallel.c:106
void DestroyParallelContext ( ParallelContext pcxt)

Definition at line 594 of file parallel.c.

References ParallelWorkerInfo::bgwhandle, dlist_delete(), dsm_detach(), ParallelWorkerInfo::error_mqh, ParallelContext::function_name, HOLD_INTERRUPTS, i, ParallelContext::library_name, ParallelContext::node, NULL, ParallelContext::nworkers_launched, pfree(), ParallelContext::private_memory, RESUME_INTERRUPTS, ParallelContext::seg, TerminateBackgroundWorker(), WaitForParallelWorkersToExit(), and ParallelContext::worker.

Referenced by AtEOSubXact_Parallel(), AtEOXact_Parallel(), and ExecParallelCleanup().

595 {
596  int i;
597 
598  /*
599  * Be careful about order of operations here! We remove the parallel
600  * context from the list before we do anything else; otherwise, if an
601  * error occurs during a subsequent step, we might try to nuke it again
602  * from AtEOXact_Parallel or AtEOSubXact_Parallel.
603  */
604  dlist_delete(&pcxt->node);
605 
606  /* Kill each worker in turn, and forget their error queues. */
607  if (pcxt->worker != NULL)
608  {
609  for (i = 0; i < pcxt->nworkers_launched; ++i)
610  {
611  if (pcxt->worker[i].error_mqh != NULL)
612  {
614 
615  pfree(pcxt->worker[i].error_mqh);
616  pcxt->worker[i].error_mqh = NULL;
617  }
618  }
619  }
620 
621  /*
622  * If we have allocated a shared memory segment, detach it. This will
623  * implicitly detach the error queues, and any other shared memory queues,
624  * stored there.
625  */
626  if (pcxt->seg != NULL)
627  {
628  dsm_detach(pcxt->seg);
629  pcxt->seg = NULL;
630  }
631 
632  /*
633  * If this parallel context is actually in backend-private memory rather
634  * than shared memory, free that memory instead.
635  */
636  if (pcxt->private_memory != NULL)
637  {
638  pfree(pcxt->private_memory);
639  pcxt->private_memory = NULL;
640  }
641 
642  /*
643  * We can't finish transaction commit or abort until all of the workers
644  * have exited. This means, in particular, that we can't respond to
645  * interrupts at this stage.
646  */
647  HOLD_INTERRUPTS();
650 
651  /* Free the worker array itself. */
652  if (pcxt->worker != NULL)
653  {
654  pfree(pcxt->worker);
655  pcxt->worker = NULL;
656  }
657 
658  /* Free memory. */
659  pfree(pcxt->library_name);
660  pfree(pcxt->function_name);
661  pfree(pcxt);
662 }
dsm_segment * seg
Definition: parallel.h:42
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:554
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
ParallelWorkerInfo * worker
Definition: parallel.h:45
void pfree(void *pointer)
Definition: mcxt.c:950
char * function_name
Definition: parallel.h:39
shm_mq_handle * error_mqh
Definition: parallel.h:28
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
int nworkers_launched
Definition: parallel.h:37
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1171
char * library_name
Definition: parallel.h:38
#define NULL
Definition: c.h:229
dlist_node node
Definition: parallel.h:34
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:726
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
int i
void * private_memory
Definition: parallel.h:43
void HandleParallelMessageInterrupt ( void  )

Definition at line 681 of file parallel.c.

References InterruptPending, MyLatch, ParallelMessagePending, and SetLatch().

Referenced by procsignal_sigusr1_handler().

682 {
683  InterruptPending = true;
684  ParallelMessagePending = true;
685  SetLatch(MyLatch);
686 }
volatile bool ParallelMessagePending
Definition: parallel.c:97
volatile bool InterruptPending
Definition: globals.c:29
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
struct Latch * MyLatch
Definition: globals.c:52
void HandleParallelMessages ( void  )

Definition at line 692 of file parallel.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), appendBinaryStringInfo(), dlist_iter::cur, StringInfoData::data, dlist_container, dlist_foreach, ereport, errcode(), errmsg(), ERROR, ParallelWorkerInfo::error_mqh, HandleParallelMessage(), HOLD_INTERRUPTS, i, initStringInfo(), MemoryContextReset(), MemoryContextSwitchTo(), NULL, ParallelContext::nworkers_launched, ParallelMessagePending, pfree(), RESUME_INTERRUPTS, shm_mq_receive(), SHM_MQ_SUCCESS, SHM_MQ_WOULD_BLOCK, TopMemoryContext, and ParallelContext::worker.

Referenced by ProcessInterrupts().

693 {
694  dlist_iter iter;
695  MemoryContext oldcontext;
696 
697  static MemoryContext hpm_context = NULL;
698 
699  /*
700  * This is invoked from ProcessInterrupts(), and since some of the
701  * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
702  * for recursive calls if more signals are received while this runs. It's
703  * unclear that recursive entry would be safe, and it doesn't seem useful
704  * even if it is safe, so let's block interrupts until done.
705  */
706  HOLD_INTERRUPTS();
707 
708  /*
709  * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
710  * don't want to risk leaking data into long-lived contexts, so let's do
711  * our work here in a private context that we can reset on each use.
712  */
713  if (hpm_context == NULL) /* first time through? */
715  "HandleParallelMessages",
717  else
718  MemoryContextReset(hpm_context);
719 
720  oldcontext = MemoryContextSwitchTo(hpm_context);
721 
722  /* OK to process messages. Reset the flag saying there are more to do. */
723  ParallelMessagePending = false;
724 
725  dlist_foreach(iter, &pcxt_list)
726  {
727  ParallelContext *pcxt;
728  int i;
729 
730  pcxt = dlist_container(ParallelContext, node, iter.cur);
731  if (pcxt->worker == NULL)
732  continue;
733 
734  for (i = 0; i < pcxt->nworkers_launched; ++i)
735  {
736  /*
737  * Read as many messages as we can from each worker, but stop when
738  * either (1) the worker's error queue goes away, which can happen
739  * if we receive a Terminate message from the worker; or (2) no
740  * more messages can be read from the worker without blocking.
741  */
742  while (pcxt->worker[i].error_mqh != NULL)
743  {
744  shm_mq_result res;
745  Size nbytes;
746  void *data;
747 
748  res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes,
749  &data, true);
750  if (res == SHM_MQ_WOULD_BLOCK)
751  break;
752  else if (res == SHM_MQ_SUCCESS)
753  {
754  StringInfoData msg;
755 
756  initStringInfo(&msg);
757  appendBinaryStringInfo(&msg, data, nbytes);
758  HandleParallelMessage(pcxt, i, &msg);
759  pfree(msg.data);
760  }
761  else
762  ereport(ERROR,
763  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
764  errmsg("lost connection to parallel worker")));
765  }
766  }
767  }
768 
769  MemoryContextSwitchTo(oldcontext);
770 
771  /* Might as well clear the context on our way out */
772  MemoryContextReset(hpm_context);
773 
775 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
volatile bool ParallelMessagePending
Definition: parallel.c:97
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
ParallelWorkerInfo * worker
Definition: parallel.h:45
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
static void HandleParallelMessage(ParallelContext *pcxt, int i, StringInfo msg)
Definition: parallel.c:781
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:37
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
dlist_node * cur
Definition: ilist.h:161
shm_mq_result
Definition: shm_mq.h:36
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
int i
static dlist_head pcxt_list
Definition: parallel.c:106
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition: shm_mq.c:518
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
void InitializeParallelDSM ( ParallelContext pcxt)

Definition at line 188 of file parallel.c.

References FixedParallelState::authenticated_user_id, BUFFERALIGN, FixedParallelState::current_user_id, FixedParallelState::database_id, dsm_create(), DSM_CREATE_NULL_IF_MAXSEGMENTS, dsm_segment_address(), ParallelWorkerInfo::error_mqh, EstimateComboCIDStateSpace(), EstimateGUCStateSpace(), EstimateLibraryStateSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), ParallelContext::estimator, ParallelContext::function_name, GetActiveSnapshot(), GetAuthenticatedUserId(), GetTempNamespaceState(), GetTransactionSnapshot(), GetUserIdAndSecContext(), i, FixedParallelState::last_xlog_end, ParallelContext::library_name, MemoryContextAlloc(), MemoryContextSwitchTo(), mul_size(), FixedParallelState::mutex, MyBackendId, MyDatabaseId, MyProc, MyProcPid, NULL, ParallelContext::nworkers, palloc0(), PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ACTIVE_SNAPSHOT, PARALLEL_KEY_COMBO_CID, PARALLEL_KEY_ENTRYPOINT, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, PARALLEL_KEY_GUC, PARALLEL_KEY_LIBRARY, PARALLEL_KEY_TRANSACTION_SNAPSHOT, PARALLEL_KEY_TRANSACTION_STATE, PARALLEL_MAGIC, FixedParallelState::parallel_master_backend_id, FixedParallelState::parallel_master_pgproc, FixedParallelState::parallel_master_pid, ParallelContext::private_memory, FixedParallelState::sec_context, ParallelContext::seg, SerializeComboCIDState(), SerializeGUCState(), SerializeLibraryState(), SerializeSnapshot(), SerializeTransactionState(), shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_allocate(), shm_toc_create(), shm_toc_estimate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), SpinLockInit, StaticAssertStmt, FixedParallelState::temp_namespace_id, FixedParallelState::temp_toast_namespace_id, ParallelContext::toc, TopMemoryContext, TopTransactionContext, and ParallelContext::worker.

Referenced by ExecInitParallelPlan().

189 {
190  MemoryContext oldcontext;
191  Size library_len = 0;
192  Size guc_len = 0;
193  Size combocidlen = 0;
194  Size tsnaplen = 0;
195  Size asnaplen = 0;
196  Size tstatelen = 0;
197  Size segsize = 0;
198  int i;
199  FixedParallelState *fps;
200  Snapshot transaction_snapshot = GetTransactionSnapshot();
201  Snapshot active_snapshot = GetActiveSnapshot();
202 
203  /* We might be running in a very short-lived memory context. */
205 
206  /* Allow space to store the fixed-size parallel state. */
208  shm_toc_estimate_keys(&pcxt->estimator, 1);
209 
210  /*
211  * Normally, the user will have requested at least one worker process, but
212  * if by chance they have not, we can skip a bunch of things here.
213  */
214  if (pcxt->nworkers > 0)
215  {
216  /* Estimate space for various kinds of state sharing. */
217  library_len = EstimateLibraryStateSpace();
218  shm_toc_estimate_chunk(&pcxt->estimator, library_len);
219  guc_len = EstimateGUCStateSpace();
220  shm_toc_estimate_chunk(&pcxt->estimator, guc_len);
221  combocidlen = EstimateComboCIDStateSpace();
222  shm_toc_estimate_chunk(&pcxt->estimator, combocidlen);
223  tsnaplen = EstimateSnapshotSpace(transaction_snapshot);
224  shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen);
225  asnaplen = EstimateSnapshotSpace(active_snapshot);
226  shm_toc_estimate_chunk(&pcxt->estimator, asnaplen);
227  tstatelen = EstimateTransactionStateSpace();
228  shm_toc_estimate_chunk(&pcxt->estimator, tstatelen);
229  /* If you add more chunks here, you probably need to add keys. */
230  shm_toc_estimate_keys(&pcxt->estimator, 6);
231 
232  /* Estimate space need for error queues. */
235  "parallel error queue size not buffer-aligned");
238  pcxt->nworkers));
239  shm_toc_estimate_keys(&pcxt->estimator, 1);
240 
241  /* Estimate how much we'll need for the entrypoint info. */
242  shm_toc_estimate_chunk(&pcxt->estimator, strlen(pcxt->library_name) +
243  strlen(pcxt->function_name) + 2);
244  shm_toc_estimate_keys(&pcxt->estimator, 1);
245  }
246 
247  /*
248  * Create DSM and initialize with new table of contents. But if the user
249  * didn't request any workers, then don't bother creating a dynamic shared
250  * memory segment; instead, just use backend-private memory.
251  *
252  * Also, if we can't create a dynamic shared memory segment because the
253  * maximum number of segments have already been created, then fall back to
254  * backend-private memory, and plan not to use any workers. We hope this
255  * won't happen very often, but it's better to abandon the use of
256  * parallelism than to fail outright.
257  */
258  segsize = shm_toc_estimate(&pcxt->estimator);
259  if (pcxt->nworkers > 0)
261  if (pcxt->seg != NULL)
263  dsm_segment_address(pcxt->seg),
264  segsize);
265  else
266  {
267  pcxt->nworkers = 0;
270  segsize);
271  }
272 
273  /* Initialize fixed-size state in shared memory. */
274  fps = (FixedParallelState *)
275  shm_toc_allocate(pcxt->toc, sizeof(FixedParallelState));
276  fps->database_id = MyDatabaseId;
284  SpinLockInit(&fps->mutex);
285  fps->last_xlog_end = 0;
287 
288  /* We can skip the rest of this if we're not budgeting for any workers. */
289  if (pcxt->nworkers > 0)
290  {
291  char *libraryspace;
292  char *gucspace;
293  char *combocidspace;
294  char *tsnapspace;
295  char *asnapspace;
296  char *tstatespace;
297  char *error_queue_space;
298  char *entrypointstate;
299  Size lnamelen;
300 
301  /* Serialize shared libraries we have loaded. */
302  libraryspace = shm_toc_allocate(pcxt->toc, library_len);
303  SerializeLibraryState(library_len, libraryspace);
304  shm_toc_insert(pcxt->toc, PARALLEL_KEY_LIBRARY, libraryspace);
305 
306  /* Serialize GUC settings. */
307  gucspace = shm_toc_allocate(pcxt->toc, guc_len);
308  SerializeGUCState(guc_len, gucspace);
309  shm_toc_insert(pcxt->toc, PARALLEL_KEY_GUC, gucspace);
310 
311  /* Serialize combo CID state. */
312  combocidspace = shm_toc_allocate(pcxt->toc, combocidlen);
313  SerializeComboCIDState(combocidlen, combocidspace);
314  shm_toc_insert(pcxt->toc, PARALLEL_KEY_COMBO_CID, combocidspace);
315 
316  /* Serialize transaction snapshot and active snapshot. */
317  tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen);
318  SerializeSnapshot(transaction_snapshot, tsnapspace);
320  tsnapspace);
321  asnapspace = shm_toc_allocate(pcxt->toc, asnaplen);
322  SerializeSnapshot(active_snapshot, asnapspace);
323  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace);
324 
325  /* Serialize transaction state. */
326  tstatespace = shm_toc_allocate(pcxt->toc, tstatelen);
327  SerializeTransactionState(tstatelen, tstatespace);
329 
330  /* Allocate space for worker information. */
331  pcxt->worker = palloc0(sizeof(ParallelWorkerInfo) * pcxt->nworkers);
332 
333  /*
334  * Establish error queues in dynamic shared memory.
335  *
336  * These queues should be used only for transmitting ErrorResponse,
337  * NoticeResponse, and NotifyResponse protocol messages. Tuple data
338  * should be transmitted via separate (possibly larger?) queues.
339  */
340  error_queue_space =
341  shm_toc_allocate(pcxt->toc,
343  pcxt->nworkers));
344  for (i = 0; i < pcxt->nworkers; ++i)
345  {
346  char *start;
347  shm_mq *mq;
348 
349  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
350  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
352  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
353  }
354  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ERROR_QUEUE, error_queue_space);
355 
356  /*
357  * Serialize entrypoint information. It's unsafe to pass function
358  * pointers across processes, as the function pointer may be different
359  * in each process in EXEC_BACKEND builds, so we always pass library
360  * and function name. (We use library name "postgres" for functions
361  * in the core backend.)
362  */
363  lnamelen = strlen(pcxt->library_name);
364  entrypointstate = shm_toc_allocate(pcxt->toc, lnamelen +
365  strlen(pcxt->function_name) + 2);
366  strcpy(entrypointstate, pcxt->library_name);
367  strcpy(entrypointstate + lnamelen + 1, pcxt->function_name);
368  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
369  }
370 
371  /* Restore previous memory context. */
372  MemoryContextSwitchTo(oldcontext);
373 }
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:48
shm_toc * shm_toc_create(uint64 magic, void *address, Size nbytes)
Definition: shm_toc.c:40
int MyProcPid
Definition: globals.c:39
BackendId MyBackendId
Definition: globals.c:73
MemoryContext TopTransactionContext
Definition: mcxt.c:48
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:42
shm_toc_estimator estimator
Definition: parallel.h:41
#define SpinLockInit(lock)
Definition: spin.h:60
void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
Definition: namespace.c:3234
Oid authenticated_user_id
Definition: parallel.c:72
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
PGPROC * parallel_master_pgproc
Definition: parallel.c:77
Oid temp_toast_namespace_id
Definition: parallel.c:75
Size shm_toc_estimate(shm_toc_estimator *e)
Definition: shm_toc.c:263
void SerializeTransactionState(Size maxsize, char *start_address)
Definition: xact.c:4873
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:758
ParallelWorkerInfo * worker
Definition: parallel.h:45
void SerializeLibraryState(Size maxsize, char *start_address)
Definition: dfmgr.c:728
Oid GetAuthenticatedUserId(void)
Definition: miscinit.c:343
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:62
char * function_name
Definition: parallel.h:39
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2062
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:167
#define PARALLEL_KEY_FIXED
Definition: parallel.c:57
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:58
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:389
shm_mq_handle * error_mqh
Definition: parallel.h:28
#define PARALLEL_KEY_GUC
Definition: parallel.c:60
Size EstimateGUCStateSpace(void)
Definition: guc.c:9102
Size EstimateComboCIDStateSpace(void)
Definition: combocid.c:299
XLogRecPtr last_xlog_end
Definition: parallel.c:85
MemoryContext TopMemoryContext
Definition: mcxt.c:43
Size EstimateSnapshotSpace(Snapshot snap)
Definition: snapmgr.c:2038
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
void * palloc0(Size size)
Definition: mcxt.c:878
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:465
Oid MyDatabaseId
Definition: globals.c:77
pid_t parallel_master_pid
Definition: parallel.c:78
Size EstimateLibraryStateSpace(void)
Definition: dfmgr.c:711
#define PARALLEL_MAGIC
Definition: parallel.c:51
char * library_name
Definition: parallel.h:38
BackendId parallel_master_backend_id
Definition: parallel.c:79
#define NULL
Definition: c.h:229
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1000
Size EstimateTransactionStateSpace(void)
Definition: xact.c:4840
size_t Size
Definition: c.h:356
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:284
#define PARALLEL_KEY_TRANSACTION_STATE
Definition: parallel.c:64
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
int i
Definition: shm_mq.c:69
#define BUFFERALIGN(LEN)
Definition: c.h:590
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:196
#define PARALLEL_KEY_ENTRYPOINT
Definition: parallel.c:65
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:61
void SerializeGUCState(Size maxsize, char *start_address)
Definition: guc.c:9249
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:63
void SerializeComboCIDState(Size maxsize, char *start_address)
Definition: combocid.c:318
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:59
shm_toc * toc
Definition: parallel.h:44
void * private_memory
Definition: parallel.h:43
void LaunchParallelWorkers ( ParallelContext pcxt)

Definition at line 417 of file parallel.c.

References Assert, BecomeLockGroupLeader(), BackgroundWorker::bgw_extra, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, ParallelWorkerInfo::bgwhandle, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_CLASS_PARALLEL, BGWORKER_SHMEM_ACCESS, BgWorkerStart_ConsistentState, dsm_segment_handle(), ParallelWorkerInfo::error_mqh, i, MemoryContextSwitchTo(), MyProcPid, NULL, ParallelContext::nworkers, ParallelContext::nworkers_launched, pfree(), RegisterDynamicBackgroundWorker(), ParallelContext::seg, shm_mq_set_handle(), snprintf(), TopTransactionContext, UInt32GetDatum, and ParallelContext::worker.

Referenced by ExecGather(), and ExecGatherMerge().

418 {
419  MemoryContext oldcontext;
420  BackgroundWorker worker;
421  int i;
422  bool any_registrations_failed = false;
423 
424  /* Skip this if we have no workers. */
425  if (pcxt->nworkers == 0)
426  return;
427 
428  /* We need to be a lock group leader. */
430 
431  /* If we do have workers, we'd better have a DSM segment. */
432  Assert(pcxt->seg != NULL);
433 
434  /* We might be running in a short-lived memory context. */
436 
437  /* Configure a worker. */
438  memset(&worker, 0, sizeof(worker));
439  snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
440  MyProcPid);
441  worker.bgw_flags =
446  sprintf(worker.bgw_library_name, "postgres");
447  sprintf(worker.bgw_function_name, "ParallelWorkerMain");
449  worker.bgw_notify_pid = MyProcPid;
450 
451  /*
452  * Start workers.
453  *
454  * The caller must be able to tolerate ending up with fewer workers than
455  * expected, so there is no need to throw an error here if registration
456  * fails. It wouldn't help much anyway, because registering the worker in
457  * no way guarantees that it will start up and initialize successfully.
458  */
459  for (i = 0; i < pcxt->nworkers; ++i)
460  {
461  memcpy(worker.bgw_extra, &i, sizeof(int));
462  if (!any_registrations_failed &&
464  &pcxt->worker[i].bgwhandle))
465  {
467  pcxt->worker[i].bgwhandle);
468  pcxt->nworkers_launched++;
469  }
470  else
471  {
472  /*
473  * If we weren't able to register the worker, then we've bumped up
474  * against the max_worker_processes limit, and future
475  * registrations will probably fail too, so arrange to skip them.
476  * But we still have to execute this code for the remaining slots
477  * to make sure that we forget about the error queues we budgeted
478  * for those workers. Otherwise, we'll wait for them to start,
479  * but they never will.
480  */
481  any_registrations_failed = true;
482  pcxt->worker[i].bgwhandle = NULL;
483  pfree(pcxt->worker[i].error_mqh);
484  pcxt->worker[i].error_mqh = NULL;
485  }
486  }
487 
488  /* Restore previous memory context. */
489  MemoryContextSwitchTo(oldcontext);
490 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:97
int MyProcPid
Definition: globals.c:39
MemoryContext TopTransactionContext
Definition: mcxt.c:48
dsm_segment * seg
Definition: parallel.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1028
int bgw_restart_time
Definition: bgworker.h:93
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:95
ParallelWorkerInfo * worker
Definition: parallel.h:45
Datum bgw_main_arg
Definition: bgworker.h:96
void pfree(void *pointer)
Definition: mcxt.c:950
shm_mq_handle * error_mqh
Definition: parallel.h:28
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define UInt32GetDatum(X)
Definition: postgres.h:499
int nworkers_launched
Definition: parallel.h:37
void BecomeLockGroupLeader(void)
Definition: proc.c:1815
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:311
#define NULL
Definition: c.h:229
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:676
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:92
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:933
pid_t bgw_notify_pid
Definition: bgworker.h:98
int i
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:94
bool ParallelContextActive ( void  )

Definition at line 668 of file parallel.c.

References dlist_is_empty().

Referenced by ExitParallelMode().

669 {
670  return !dlist_is_empty(&pcxt_list);
671 }
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static dlist_head pcxt_list
Definition: parallel.c:106
void ParallelWorkerMain ( Datum  main_arg)

Definition at line 922 of file parallel.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, FixedParallelState::authenticated_user_id, BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), BecomeLockGroupMember(), BackgroundWorker::bgw_extra, CommitTransactionCommand(), FixedParallelState::current_user_id, CurrentMemoryContext, CurrentResourceOwner, FixedParallelState::database_id, DatumGetUInt32, die(), dsm_attach(), dsm_segment_address(), EndParallelWorkerTransaction(), EnterParallelMode(), ereport, errcode(), errmsg(), ERROR, ExitParallelMode(), GetDatabaseEncoding(), InitializingParallelWorker, InvalidateSystemCaches(), LookupParallelWorkerFunction(), MyBgworkerEntry, MyCancelKey, MyProc, MyProcPid, NULL, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ACTIVE_SNAPSHOT, PARALLEL_KEY_COMBO_CID, PARALLEL_KEY_ENTRYPOINT, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, PARALLEL_KEY_GUC, PARALLEL_KEY_LIBRARY, PARALLEL_KEY_TRANSACTION_SNAPSHOT, PARALLEL_KEY_TRANSACTION_STATE, PARALLEL_MAGIC, FixedParallelState::parallel_master_backend_id, FixedParallelState::parallel_master_pgproc, FixedParallelState::parallel_master_pid, ParallelMasterBackendId, ParallelWorkerNumber, PopActiveSnapshot(), pq_beginmessage(), pq_endmessage(), pq_putmessage, pq_redirect_to_shm_mq(), pq_sendint(), pq_set_parallel_master(), pqsignal(), PushActiveSnapshot(), ResourceOwnerCreate(), RestoreComboCIDState(), RestoreGUCState(), RestoreLibraryState(), RestoreSnapshot(), RestoreTransactionSnapshot(), FixedParallelState::sec_context, SetClientEncoding(), SetTempNamespaceState(), SetUserIdAndSecContext(), shm_mq_attach(), shm_mq_set_sender(), shm_toc_attach(), shm_toc_lookup(), StartParallelWorkerTransaction(), StartTransactionCommand(), FixedParallelState::temp_namespace_id, FixedParallelState::temp_toast_namespace_id, and TopMemoryContext.

923 {
924  dsm_segment *seg;
925  shm_toc *toc;
926  FixedParallelState *fps;
927  char *error_queue_space;
928  shm_mq *mq;
929  shm_mq_handle *mqh;
930  char *libraryspace;
931  char *entrypointstate;
932  char *library_name;
933  char *function_name;
935  char *gucspace;
936  char *combocidspace;
937  char *tsnapspace;
938  char *asnapspace;
939  char *tstatespace;
940  StringInfoData msgbuf;
941 
942  /* Set flag to indicate that we're initializing a parallel worker. */
944 
945  /* Establish signal handlers. */
946  pqsignal(SIGTERM, die);
948 
949  /* Determine and set our parallel worker number. */
951  memcpy(&ParallelWorkerNumber, MyBgworkerEntry->bgw_extra, sizeof(int));
952 
953  /* Set up a memory context and resource owner. */
955  CurrentResourceOwner = ResourceOwnerCreate(NULL, "parallel toplevel");
957  "Parallel worker",
959 
960  /*
961  * Now that we have a resource owner, we can attach to the dynamic shared
962  * memory segment and read the table of contents.
963  */
964  seg = dsm_attach(DatumGetUInt32(main_arg));
965  if (seg == NULL)
966  ereport(ERROR,
967  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
968  errmsg("could not map dynamic shared memory segment")));
970  if (toc == NULL)
971  ereport(ERROR,
972  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
973  errmsg("invalid magic number in dynamic shared memory segment")));
974 
975  /* Look up fixed parallel state. */
976  fps = shm_toc_lookup(toc, PARALLEL_KEY_FIXED, false);
977  MyFixedParallelState = fps;
978 
979  /*
980  * Now that we have a worker number, we can find and attach to the error
981  * queue provided for us. That's good, because until we do that, any
982  * errors that happen here will not be reported back to the process that
983  * requested that this worker be launched.
984  */
985  error_queue_space = shm_toc_lookup(toc, PARALLEL_KEY_ERROR_QUEUE, false);
986  mq = (shm_mq *) (error_queue_space +
989  mqh = shm_mq_attach(mq, seg, NULL);
990  pq_redirect_to_shm_mq(seg, mqh);
993 
994  /*
995  * Send a BackendKeyData message to the process that initiated parallelism
996  * so that it has access to our PID before it receives any other messages
997  * from us. Our cancel key is sent, too, since that's the way the
998  * protocol message is defined, but it won't actually be used for anything
999  * in this case.
1000  */
1001  pq_beginmessage(&msgbuf, 'K');
1002  pq_sendint(&msgbuf, (int32) MyProcPid, sizeof(int32));
1003  pq_sendint(&msgbuf, (int32) MyCancelKey, sizeof(int32));
1004  pq_endmessage(&msgbuf);
1005 
1006  /*
1007  * Hooray! Primary initialization is complete. Now, we need to set up our
1008  * backend-local state to match the original backend.
1009  */
1010 
1011  /*
1012  * Join locking group. We must do this before anything that could try to
1013  * acquire a heavyweight lock, because any heavyweight locks acquired to
1014  * this point could block either directly against the parallel group
1015  * leader or against some process which in turn waits for a lock that
1016  * conflicts with the parallel group leader, causing an undetected
1017  * deadlock. (If we can't join the lock group, the leader has gone away,
1018  * so just exit quietly.)
1019  */
1021  fps->parallel_master_pid))
1022  return;
1023 
1024  /*
1025  * Load libraries that were loaded by original backend. We want to do
1026  * this before restoring GUCs, because the libraries might define custom
1027  * variables.
1028  */
1029  libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY, false);
1030  RestoreLibraryState(libraryspace);
1031 
1032  /*
1033  * Identify the entry point to be called. In theory this could result in
1034  * loading an additional library, though most likely the entry point is in
1035  * the core backend or in a library we just loaded.
1036  */
1037  entrypointstate = shm_toc_lookup(toc, PARALLEL_KEY_ENTRYPOINT, false);
1038  library_name = entrypointstate;
1039  function_name = entrypointstate + strlen(library_name) + 1;
1040 
1041  entrypt = LookupParallelWorkerFunction(library_name, function_name);
1042 
1043  /* Restore database connection. */
1045  fps->authenticated_user_id);
1046 
1047  /*
1048  * Set the client encoding to the database encoding, since that is what
1049  * the leader will expect.
1050  */
1052 
1053  /* Restore GUC values from launching backend. */
1054  gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1056  RestoreGUCState(gucspace);
1058 
1059  /* Crank up a transaction state appropriate to a parallel worker. */
1060  tstatespace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_STATE, false);
1061  StartParallelWorkerTransaction(tstatespace);
1062 
1063  /* Restore combo CID state. */
1064  combocidspace = shm_toc_lookup(toc, PARALLEL_KEY_COMBO_CID, false);
1065  RestoreComboCIDState(combocidspace);
1066 
1067  /* Restore transaction snapshot. */
1068  tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, false);
1070  fps->parallel_master_pgproc);
1071 
1072  /* Restore active snapshot. */
1073  asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, false);
1074  PushActiveSnapshot(RestoreSnapshot(asnapspace));
1075 
1076  /*
1077  * We've changed which tuples we can see, and must therefore invalidate
1078  * system caches.
1079  */
1081 
1082  /* Restore user ID and security context. */
1084 
1085  /* Restore temp-namespace state to ensure search path matches leader's. */
1088 
1089  /* Set ParallelMasterBackendId so we know how to address temp relations. */
1091 
1092  /*
1093  * We've initialized all of our state now; nothing should change
1094  * hereafter.
1095  */
1098 
1099  /*
1100  * Time to do the real work: invoke the caller-supplied code.
1101  */
1102  entrypt(seg, toc);
1103 
1104  /* Must exit parallel mode to pop active snapshot. */
1105  ExitParallelMode();
1106 
1107  /* Must pop active snapshot so resowner.c doesn't complain. */
1109 
1110  /* Shut down the parallel-worker transaction. */
1112 
1113  /* Report success. */
1114  pq_putmessage('X', NULL, 0);
1115 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:97
#define DatumGetUInt32(X)
Definition: postgres.h:492
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:48
int MyProcPid
Definition: globals.c:39
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2121
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:396
PGPROC * MyProc
Definition: proc.c:67
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void CommitTransactionCommand(void)
Definition: xact.c:2750
void EndParallelWorkerTransaction(void)
Definition: xact.c:4964
void RestoreTransactionSnapshot(Snapshot snapshot, void *master_pgproc)
Definition: snapmgr.c:2185
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:561
Oid authenticated_user_id
Definition: parallel.c:72
int errcode(int sqlerrcode)
Definition: elog.c:575
PGPROC * parallel_master_pgproc
Definition: parallel.c:77
Oid temp_toast_namespace_id
Definition: parallel.c:75
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:190
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1845
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
void InvalidateSystemCaches(void)
Definition: inval.c:641
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
void RestoreComboCIDState(char *comboCIDstate)
Definition: combocid.c:344
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:62
int32 MyCancelKey
Definition: globals.c:42
void ExitParallelMode(void)
Definition: xact.c:893
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define PARALLEL_KEY_FIXED
Definition: parallel.c:57
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:58
void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
Definition: namespace.c:3250
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
#define PARALLEL_KEY_GUC
Definition: parallel.c:60
int SetClientEncoding(int encoding)
Definition: mbutils.c:212
int ParallelWorkerNumber
Definition: parallel.c:94
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
Definition: postmaster.c:5550
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:215
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
static parallel_worker_main_type LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
Definition: parallel.c:1152
void RestoreLibraryState(char *start_address)
Definition: dfmgr.c:750
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition: shm_toc.c:64
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
pid_t parallel_master_pid
Definition: parallel.c:78
#define PARALLEL_MAGIC
Definition: parallel.c:51
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition: pqmq.c:56
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
BackendId parallel_master_backend_id
Definition: parallel.c:79
#define NULL
Definition: c.h:229
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1000
#define Assert(condition)
Definition: c.h:676
BackendId ParallelMasterBackendId
Definition: globals.c:75
void StartParallelWorkerTransaction(char *tstatespace)
Definition: xact.c:4941
void StartTransactionCommand(void)
Definition: xact.c:2680
void EnterParallelMode(void)
Definition: xact.c:880
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:284
void pq_set_parallel_master(pid_t pid, BackendId backend_id)
Definition: pqmq.c:83
#define PARALLEL_KEY_TRANSACTION_STATE
Definition: parallel.c:64
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
static FixedParallelState * MyFixedParallelState
Definition: parallel.c:103
void die(SIGNAL_ARGS)
Definition: postgres.c:2617
bool InitializingParallelWorker
Definition: parallel.c:100
Definition: shm_mq.c:69
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:42
#define PARALLEL_KEY_ENTRYPOINT
Definition: parallel.c:65
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:61
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:63
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:59
void RestoreGUCState(void *gucstate)
Definition: guc.c:9327
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
void(* parallel_worker_main_type)(dsm_segment *seg, shm_toc *toc)
Definition: parallel.h:23
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5579
void ParallelWorkerReportLastRecEnd ( XLogRecPtr  last_xlog_end)

Definition at line 1122 of file parallel.c.

References Assert, FixedParallelState::last_xlog_end, FixedParallelState::mutex, MyFixedParallelState, NULL, SpinLockAcquire, and SpinLockRelease.

Referenced by CommitTransaction().

1123 {
1125 
1126  Assert(fps != NULL);
1127  SpinLockAcquire(&fps->mutex);
1128  if (fps->last_xlog_end < last_xlog_end)
1129  fps->last_xlog_end = last_xlog_end;
1130  SpinLockRelease(&fps->mutex);
1131 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr last_xlog_end
Definition: parallel.c:85
#define SpinLockRelease(lock)
Definition: spin.h:64
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static FixedParallelState * MyFixedParallelState
Definition: parallel.c:103
void ReinitializeParallelDSM ( ParallelContext pcxt)

Definition at line 380 of file parallel.c.

References ParallelWorkerInfo::error_mqh, i, FixedParallelState::last_xlog_end, MyProc, NULL, ParallelContext::nworkers, ParallelContext::nworkers_launched, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, ParallelContext::seg, shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_lookup(), ParallelContext::toc, WaitForParallelWorkersToExit(), WaitForParallelWorkersToFinish(), and ParallelContext::worker.

Referenced by ExecParallelReinitialize().

381 {
382  FixedParallelState *fps;
383  char *error_queue_space;
384  int i;
385 
386  /* Wait for any old workers to exit. */
387  if (pcxt->nworkers_launched > 0)
388  {
391  pcxt->nworkers_launched = 0;
392  }
393 
394  /* Reset a few bits of fixed parallel state to a clean state. */
395  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
396  fps->last_xlog_end = 0;
397 
398  /* Recreate error queues. */
399  error_queue_space =
401  for (i = 0; i < pcxt->nworkers; ++i)
402  {
403  char *start;
404  shm_mq *mq;
405 
406  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
407  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
409  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
410  }
411 }
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:48
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:42
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:554
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:504
ParallelWorkerInfo * worker
Definition: parallel.h:45
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:167
#define PARALLEL_KEY_FIXED
Definition: parallel.c:57
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:58
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:37
XLogRecPtr last_xlog_end
Definition: parallel.c:85
#define NULL
Definition: c.h:229
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:284
int i
Definition: shm_mq.c:69
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:196
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44
void WaitForParallelWorkersToFinish ( ParallelContext pcxt)

Definition at line 504 of file parallel.c.

References CHECK_FOR_INTERRUPTS, ParallelWorkerInfo::error_mqh, i, FixedParallelState::last_xlog_end, MyLatch, NULL, ParallelContext::nworkers_launched, PARALLEL_KEY_FIXED, ResetLatch(), shm_toc_lookup(), ParallelContext::toc, WAIT_EVENT_PARALLEL_FINISH, WaitLatch(), WL_LATCH_SET, ParallelContext::worker, and XactLastRecEnd.

Referenced by ExecParallelFinish(), and ReinitializeParallelDSM().

505 {
506  for (;;)
507  {
508  bool anyone_alive = false;
509  int i;
510 
511  /*
512  * This will process any parallel messages that are pending, which may
513  * change the outcome of the loop that follows. It may also throw an
514  * error propagated from a worker.
515  */
517 
518  for (i = 0; i < pcxt->nworkers_launched; ++i)
519  {
520  if (pcxt->worker[i].error_mqh != NULL)
521  {
522  anyone_alive = true;
523  break;
524  }
525  }
526 
527  if (!anyone_alive)
528  break;
529 
533  }
534 
535  if (pcxt->toc != NULL)
536  {
537  FixedParallelState *fps;
538 
539  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
540  if (fps->last_xlog_end > XactLastRecEnd)
542  }
543 }
XLogRecPtr XactLastRecEnd
Definition: xlog.c:337
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
ParallelWorkerInfo * worker
Definition: parallel.h:45
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
#define PARALLEL_KEY_FIXED
Definition: parallel.c:57
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:37
XLogRecPtr last_xlog_end
Definition: parallel.c:85
#define NULL
Definition: c.h:229
int i
struct Latch * MyLatch
Definition: globals.c:52
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define WL_LATCH_SET
Definition: latch.h:124
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:44

Variable Documentation

bool InitializingParallelWorker
volatile bool ParallelMessagePending