PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parallel.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/namespace.h"
#include "commands/async.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "libpq/pqmq.h"
#include "miscadmin.h"
#include "optimizer/planmain.h"
#include "pgstat.h"
#include "storage/ipc.h"
#include "storage/sinval.h"
#include "storage/spin.h"
#include "tcop/tcopprot.h"
#include "utils/combocid.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
#include "utils/snapmgr.h"
Include dependency graph for parallel.c:

Go to the source code of this file.

Data Structures

struct  FixedParallelState
 

Macros

#define PARALLEL_ERROR_QUEUE_SIZE   16384
 
#define PARALLEL_MAGIC   0x50477c7c
 
#define PARALLEL_KEY_FIXED   UINT64CONST(0xFFFFFFFFFFFF0001)
 
#define PARALLEL_KEY_ERROR_QUEUE   UINT64CONST(0xFFFFFFFFFFFF0002)
 
#define PARALLEL_KEY_LIBRARY   UINT64CONST(0xFFFFFFFFFFFF0003)
 
#define PARALLEL_KEY_GUC   UINT64CONST(0xFFFFFFFFFFFF0004)
 
#define PARALLEL_KEY_COMBO_CID   UINT64CONST(0xFFFFFFFFFFFF0005)
 
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0006)
 
#define PARALLEL_KEY_ACTIVE_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0007)
 
#define PARALLEL_KEY_TRANSACTION_STATE   UINT64CONST(0xFFFFFFFFFFFF0008)
 
#define PARALLEL_KEY_EXTENSION_TRAMPOLINE   UINT64CONST(0xFFFFFFFFFFFF0009)
 

Typedefs

typedef struct FixedParallelState FixedParallelState
 

Functions

static void HandleParallelMessage (ParallelContext *pcxt, int i, StringInfo msg)
 
static void ParallelExtensionTrampoline (dsm_segment *seg, shm_toc *toc)
 
static void ParallelWorkerMain (Datum main_arg)
 
static void WaitForParallelWorkersToExit (ParallelContext *pcxt)
 
ParallelContextCreateParallelContext (parallel_worker_main_type entrypoint, int nworkers)
 
ParallelContextCreateParallelContextForExternalFunction (char *library_name, 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 AtEOSubXact_Parallel (bool isCommit, SubTransactionId mySubId)
 
void AtEOXact_Parallel (bool isCommit)
 
void ParallelWorkerReportLastRecEnd (XLogRecPtr last_xlog_end)
 

Variables

int ParallelWorkerNumber = -1
 
volatile bool ParallelMessagePending = false
 
bool InitializingParallelWorker = false
 
static FixedParallelStateMyFixedParallelState
 
static dlist_head pcxt_list = DLIST_STATIC_INIT(pcxt_list)
 

Macro Definition Documentation

#define PARALLEL_ERROR_QUEUE_SIZE   16384

Definition at line 47 of file parallel.c.

Referenced by InitializeParallelDSM(), ParallelWorkerMain(), and ReinitializeParallelDSM().

#define PARALLEL_KEY_ACTIVE_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0007)

Definition at line 62 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

#define PARALLEL_KEY_COMBO_CID   UINT64CONST(0xFFFFFFFFFFFF0005)

Definition at line 60 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

#define PARALLEL_KEY_ERROR_QUEUE   UINT64CONST(0xFFFFFFFFFFFF0002)

Definition at line 57 of file parallel.c.

Referenced by InitializeParallelDSM(), ParallelWorkerMain(), and ReinitializeParallelDSM().

#define PARALLEL_KEY_EXTENSION_TRAMPOLINE   UINT64CONST(0xFFFFFFFFFFFF0009)

Definition at line 64 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelExtensionTrampoline().

#define PARALLEL_KEY_FIXED   UINT64CONST(0xFFFFFFFFFFFF0001)
#define PARALLEL_KEY_GUC   UINT64CONST(0xFFFFFFFFFFFF0004)

Definition at line 59 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

#define PARALLEL_KEY_LIBRARY   UINT64CONST(0xFFFFFFFFFFFF0003)

Definition at line 58 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

#define PARALLEL_KEY_TRANSACTION_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0006)

Definition at line 61 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

#define PARALLEL_KEY_TRANSACTION_STATE   UINT64CONST(0xFFFFFFFFFFFF0008)

Definition at line 63 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

#define PARALLEL_MAGIC   0x50477c7c

Definition at line 50 of file parallel.c.

Referenced by InitializeParallelDSM(), and ParallelWorkerMain().

Typedef Documentation

Function Documentation

void AtEOSubXact_Parallel ( bool  isCommit,
SubTransactionId  mySubId 
)

Definition at line 899 of file parallel.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

900 {
901  while (!dlist_is_empty(&pcxt_list))
902  {
903  ParallelContext *pcxt;
904 
906  if (pcxt->subid != mySubId)
907  break;
908  if (isCommit)
909  elog(WARNING, "leaked parallel context");
911  }
912 }
SubTransactionId subid
Definition: parallel.h:35
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:609
#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:108
#define elog
Definition: elog.h:219
void AtEOXact_Parallel ( bool  isCommit)

Definition at line 918 of file parallel.c.

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

Referenced by AbortTransaction(), and CommitTransaction().

919 {
920  while (!dlist_is_empty(&pcxt_list))
921  {
922  ParallelContext *pcxt;
923 
925  if (isCommit)
926  elog(WARNING, "leaked parallel context");
928  }
929 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:609
#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:108
#define elog
Definition: elog.h:219
ParallelContext* CreateParallelContext ( parallel_worker_main_type  entrypoint,
int  nworkers 
)

Definition at line 123 of file parallel.c.

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

Referenced by CreateParallelContextForExternalFunction(), and ExecInitParallelPlan().

124 {
125  MemoryContext oldcontext;
126  ParallelContext *pcxt;
127 
128  /* It is unsafe to create a parallel context if not in parallel mode. */
130 
131  /* Number of workers should be non-negative. */
132  Assert(nworkers >= 0);
133 
134  /*
135  * If dynamic shared memory is not available, we won't be able to use
136  * background workers.
137  */
139  nworkers = 0;
140 
141  /*
142  * If we are running under serializable isolation, we can't use parallel
143  * workers, at least not until somebody enhances that mechanism to be
144  * parallel-aware.
145  */
147  nworkers = 0;
148 
149  /* We might be running in a short-lived memory context. */
151 
152  /* Initialize a new ParallelContext. */
153  pcxt = palloc0(sizeof(ParallelContext));
155  pcxt->nworkers = nworkers;
156  pcxt->entrypoint = entrypoint;
159  dlist_push_head(&pcxt_list, &pcxt->node);
160 
161  /* Restore previous memory context. */
162  MemoryContextSwitchTo(oldcontext);
163 
164  return pcxt;
165 }
parallel_worker_main_type entrypoint
Definition: parallel.h:38
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
shm_toc_estimator estimator
Definition: parallel.h:42
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:912
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
#define shm_toc_initialize_estimator(e)
Definition: shm_toc.h:47
void * palloc0(Size size)
Definition: mcxt.c:920
#define Assert(condition)
Definition: c.h:671
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:648
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
dlist_node node
Definition: parallel.h:34
ErrorContextCallback * error_context_stack
Definition: parallel.h:41
#define IsolationIsSerializable()
Definition: xact.h:44
static dlist_head pcxt_list
Definition: parallel.c:108
ParallelContext* CreateParallelContextForExternalFunction ( char *  library_name,
char *  function_name,
int  nworkers 
)

Definition at line 173 of file parallel.c.

References CreateParallelContext(), ParallelContext::function_name, ParallelContext::library_name, MemoryContextSwitchTo(), ParallelExtensionTrampoline(), pstrdup(), and TopTransactionContext.

176 {
177  MemoryContext oldcontext;
178  ParallelContext *pcxt;
179 
180  /* We might be running in a very short-lived memory context. */
182 
183  /* Create the context. */
185  pcxt->library_name = pstrdup(library_name);
186  pcxt->function_name = pstrdup(function_name);
187 
188  /* Restore previous memory context. */
189  MemoryContextSwitchTo(oldcontext);
190 
191  return pcxt;
192 }
MemoryContext TopTransactionContext
Definition: mcxt.c:48
char * pstrdup(const char *in)
Definition: mcxt.c:1165
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
char * function_name
Definition: parallel.h:40
char * library_name
Definition: parallel.h:39
ParallelContext * CreateParallelContext(parallel_worker_main_type entrypoint, int nworkers)
Definition: parallel.c:123
static void ParallelExtensionTrampoline(dsm_segment *seg, shm_toc *toc)
Definition: parallel.c:1134
void DestroyParallelContext ( ParallelContext pcxt)

Definition at line 609 of file parallel.c.

References ParallelWorkerInfo::bgwhandle, dlist_delete(), dsm_detach(), ParallelWorkerInfo::error_mqh, HOLD_INTERRUPTS, i, 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().

610 {
611  int i;
612 
613  /*
614  * Be careful about order of operations here! We remove the parallel
615  * context from the list before we do anything else; otherwise, if an
616  * error occurs during a subsequent step, we might try to nuke it again
617  * from AtEOXact_Parallel or AtEOSubXact_Parallel.
618  */
619  dlist_delete(&pcxt->node);
620 
621  /* Kill each worker in turn, and forget their error queues. */
622  if (pcxt->worker != NULL)
623  {
624  for (i = 0; i < pcxt->nworkers_launched; ++i)
625  {
626  if (pcxt->worker[i].error_mqh != NULL)
627  {
629 
630  pfree(pcxt->worker[i].error_mqh);
631  pcxt->worker[i].error_mqh = NULL;
632  }
633  }
634  }
635 
636  /*
637  * If we have allocated a shared memory segment, detach it. This will
638  * implicitly detach the error queues, and any other shared memory queues,
639  * stored there.
640  */
641  if (pcxt->seg != NULL)
642  {
643  dsm_detach(pcxt->seg);
644  pcxt->seg = NULL;
645  }
646 
647  /*
648  * If this parallel context is actually in backend-private memory rather
649  * than shared memory, free that memory instead.
650  */
651  if (pcxt->private_memory != NULL)
652  {
653  pfree(pcxt->private_memory);
654  pcxt->private_memory = NULL;
655  }
656 
657  /*
658  * We can't finish transaction commit or abort until all of the workers
659  * have exited. This means, in particular, that we can't respond to
660  * interrupts at this stage.
661  */
662  HOLD_INTERRUPTS();
665 
666  /* Free the worker array itself. */
667  if (pcxt->worker != NULL)
668  {
669  pfree(pcxt->worker);
670  pcxt->worker = NULL;
671  }
672 
673  /* Free memory. */
674  pfree(pcxt);
675 }
dsm_segment * seg
Definition: parallel.h:43
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:569
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:992
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:1099
#define NULL
Definition: c.h:226
dlist_node node
Definition: parallel.h:34
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:715
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
int i
void * private_memory
Definition: parallel.h:44
static void HandleParallelMessage ( ParallelContext pcxt,
int  i,
StringInfo  msg 
)
static

Definition at line 794 of file parallel.c.

References _, ErrorData::context, ErrorData::elevel, elog, ERROR, ParallelContext::error_context_stack, error_context_stack, ParallelWorkerInfo::error_mqh, force_parallel_mode, FORCE_PARALLEL_REGRESS, i, StringInfoData::len, Min, NotifyMyFrontEnd(), NULL, pfree(), ParallelWorkerInfo::pid, pq_endmessage(), pq_getmsgbyte(), pq_getmsgend(), pq_getmsgint(), pq_getmsgrawstring(), pq_parse_errornotice(), psprintf(), pstrdup(), ThrowErrorData(), and ParallelContext::worker.

Referenced by HandleParallelMessages().

795 {
796  char msgtype;
797 
798  msgtype = pq_getmsgbyte(msg);
799 
800  switch (msgtype)
801  {
802  case 'K': /* BackendKeyData */
803  {
804  int32 pid = pq_getmsgint(msg, 4);
805 
806  (void) pq_getmsgint(msg, 4); /* discard cancel key */
807  (void) pq_getmsgend(msg);
808  pcxt->worker[i].pid = pid;
809  break;
810  }
811 
812  case 'E': /* ErrorResponse */
813  case 'N': /* NoticeResponse */
814  {
815  ErrorData edata;
816  ErrorContextCallback *save_error_context_stack;
817 
818  /* Parse ErrorResponse or NoticeResponse. */
819  pq_parse_errornotice(msg, &edata);
820 
821  /* Death of a worker isn't enough justification for suicide. */
822  edata.elevel = Min(edata.elevel, ERROR);
823 
824  /*
825  * If desired, add a context line to show that this is a
826  * message propagated from a parallel worker. Otherwise, it
827  * can sometimes be confusing to understand what actually
828  * happened. (We don't do this in FORCE_PARALLEL_REGRESS mode
829  * because it causes test-result instability depending on
830  * whether a parallel worker is actually used or not.)
831  */
833  {
834  if (edata.context)
835  edata.context = psprintf("%s\n%s", edata.context,
836  _("parallel worker"));
837  else
838  edata.context = pstrdup(_("parallel worker"));
839  }
840 
841  /*
842  * Context beyond that should use the error context callbacks
843  * that were in effect when the ParallelContext was created,
844  * not the current ones.
845  */
846  save_error_context_stack = error_context_stack;
848 
849  /* Rethrow error or print notice. */
850  ThrowErrorData(&edata);
851 
852  /* Not an error, so restore previous context stack. */
853  error_context_stack = save_error_context_stack;
854 
855  break;
856  }
857 
858  case 'A': /* NotifyResponse */
859  {
860  /* Propagate NotifyResponse. */
861  int32 pid;
862  const char *channel;
863  const char *payload;
864 
865  pid = pq_getmsgint(msg, 4);
866  channel = pq_getmsgrawstring(msg);
867  payload = pq_getmsgrawstring(msg);
868  pq_endmessage(msg);
869 
870  NotifyMyFrontEnd(channel, payload, pid);
871 
872  break;
873  }
874 
875  case 'X': /* Terminate, indicating clean exit */
876  {
877  pfree(pcxt->worker[i].error_mqh);
878  pcxt->worker[i].error_mqh = NULL;
879  break;
880  }
881 
882  default:
883  {
884  elog(ERROR, "unrecognized message type received from parallel worker: %c (message length %d bytes)",
885  msgtype, msg->len);
886  }
887  }
888 }
char * pstrdup(const char *in)
Definition: mcxt.c:1165
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define Min(x, y)
Definition: c.h:802
const char * pq_getmsgrawstring(StringInfo msg)
Definition: pqformat.c:650
signed int int32
Definition: c.h:253
ErrorContextCallback * error_context_stack
Definition: elog.c:88
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
void pq_parse_errornotice(StringInfo msg, ErrorData *edata)
Definition: pqmq.c:219
shm_mq_handle * error_mqh
Definition: parallel.h:28
void ThrowErrorData(ErrorData *edata)
Definition: elog.c:1612
int elevel
Definition: elog.h:331
int pq_getmsgbyte(StringInfo msg)
Definition: pqformat.c:432
#define NULL
Definition: c.h:226
int force_parallel_mode
Definition: planner.c:62
char * context
Definition: elog.h:347
ErrorContextCallback * error_context_stack
Definition: parallel.h:41
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
#define elog
Definition: elog.h:219
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:677
#define _(x)
Definition: elog.c:84
void NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
Definition: async.c:2077
void HandleParallelMessageInterrupt ( void  )

Definition at line 694 of file parallel.c.

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

Referenced by procsignal_sigusr1_handler().

695 {
696  InterruptPending = true;
697  ParallelMessagePending = true;
698  SetLatch(MyLatch);
699 }
volatile bool ParallelMessagePending
Definition: parallel.c:99
volatile bool InterruptPending
Definition: globals.c:29
void SetLatch(volatile Latch *latch)
Definition: latch.c:380
struct Latch * MyLatch
Definition: globals.c:51
void HandleParallelMessages ( void  )

Definition at line 705 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().

706 {
707  dlist_iter iter;
708  MemoryContext oldcontext;
709 
710  static MemoryContext hpm_context = NULL;
711 
712  /*
713  * This is invoked from ProcessInterrupts(), and since some of the
714  * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
715  * for recursive calls if more signals are received while this runs. It's
716  * unclear that recursive entry would be safe, and it doesn't seem useful
717  * even if it is safe, so let's block interrupts until done.
718  */
719  HOLD_INTERRUPTS();
720 
721  /*
722  * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
723  * don't want to risk leaking data into long-lived contexts, so let's do
724  * our work here in a private context that we can reset on each use.
725  */
726  if (hpm_context == NULL) /* first time through? */
728  "HandleParallelMessages",
730  else
731  MemoryContextReset(hpm_context);
732 
733  oldcontext = MemoryContextSwitchTo(hpm_context);
734 
735  /* OK to process messages. Reset the flag saying there are more to do. */
736  ParallelMessagePending = false;
737 
738  dlist_foreach(iter, &pcxt_list)
739  {
740  ParallelContext *pcxt;
741  int i;
742 
743  pcxt = dlist_container(ParallelContext, node, iter.cur);
744  if (pcxt->worker == NULL)
745  continue;
746 
747  for (i = 0; i < pcxt->nworkers_launched; ++i)
748  {
749  /*
750  * Read as many messages as we can from each worker, but stop when
751  * either (1) the worker's error queue goes away, which can happen
752  * if we receive a Terminate message from the worker; or (2) no
753  * more messages can be read from the worker without blocking.
754  */
755  while (pcxt->worker[i].error_mqh != NULL)
756  {
757  shm_mq_result res;
758  Size nbytes;
759  void *data;
760 
761  res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes,
762  &data, true);
763  if (res == SHM_MQ_WOULD_BLOCK)
764  break;
765  else if (res == SHM_MQ_SUCCESS)
766  {
767  StringInfoData msg;
768 
769  initStringInfo(&msg);
770  appendBinaryStringInfo(&msg, data, nbytes);
771  HandleParallelMessage(pcxt, i, &msg);
772  pfree(msg.data);
773  }
774  else
775  ereport(ERROR,
776  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
777  errmsg("lost connection to parallel worker")));
778  }
779  }
780  }
781 
782  MemoryContextSwitchTo(oldcontext);
783 
784  /* Might as well clear the context on our way out */
785  MemoryContextReset(hpm_context);
786 
788 }
#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:116
volatile bool ParallelMessagePending
Definition: parallel.c:99
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
static void HandleParallelMessage(ParallelContext *pcxt, int i, StringInfo msg)
Definition: parallel.c:794
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:65
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
dlist_node * cur
Definition: ilist.h:161
shm_mq_result
Definition: shm_mq.h:36
#define NULL
Definition: c.h:226
size_t Size
Definition: c.h:353
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
int i
static dlist_head pcxt_list
Definition: parallel.c:108
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:240
void InitializeParallelDSM ( ParallelContext pcxt)

Definition at line 200 of file parallel.c.

References Assert, FixedParallelState::authenticated_user_id, BUFFERALIGN, FixedParallelState::current_user_id, FixedParallelState::database_id, dsm_create(), DSM_CREATE_NULL_IF_MAXSEGMENTS, dsm_segment_address(), ParallelContext::entrypoint, FixedParallelState::entrypoint, 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_ERROR_QUEUE, PARALLEL_KEY_EXTENSION_TRAMPOLINE, 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, ParallelExtensionTrampoline(), 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().

201 {
202  MemoryContext oldcontext;
203  Size library_len = 0;
204  Size guc_len = 0;
205  Size combocidlen = 0;
206  Size tsnaplen = 0;
207  Size asnaplen = 0;
208  Size tstatelen = 0;
209  Size segsize = 0;
210  int i;
211  FixedParallelState *fps;
212  Snapshot transaction_snapshot = GetTransactionSnapshot();
213  Snapshot active_snapshot = GetActiveSnapshot();
214 
215  /* We might be running in a very short-lived memory context. */
217 
218  /* Allow space to store the fixed-size parallel state. */
220  shm_toc_estimate_keys(&pcxt->estimator, 1);
221 
222  /*
223  * Normally, the user will have requested at least one worker process, but
224  * if by chance they have not, we can skip a bunch of things here.
225  */
226  if (pcxt->nworkers > 0)
227  {
228  /* Estimate space for various kinds of state sharing. */
229  library_len = EstimateLibraryStateSpace();
230  shm_toc_estimate_chunk(&pcxt->estimator, library_len);
231  guc_len = EstimateGUCStateSpace();
232  shm_toc_estimate_chunk(&pcxt->estimator, guc_len);
233  combocidlen = EstimateComboCIDStateSpace();
234  shm_toc_estimate_chunk(&pcxt->estimator, combocidlen);
235  tsnaplen = EstimateSnapshotSpace(transaction_snapshot);
236  shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen);
237  asnaplen = EstimateSnapshotSpace(active_snapshot);
238  shm_toc_estimate_chunk(&pcxt->estimator, asnaplen);
239  tstatelen = EstimateTransactionStateSpace();
240  shm_toc_estimate_chunk(&pcxt->estimator, tstatelen);
241  /* If you add more chunks here, you probably need to add keys. */
242  shm_toc_estimate_keys(&pcxt->estimator, 6);
243 
244  /* Estimate space need for error queues. */
247  "parallel error queue size not buffer-aligned");
250  pcxt->nworkers));
251  shm_toc_estimate_keys(&pcxt->estimator, 1);
252 
253  /* Estimate how much we'll need for extension entrypoint info. */
254  if (pcxt->library_name != NULL)
255  {
257  Assert(pcxt->function_name != NULL);
258  shm_toc_estimate_chunk(&pcxt->estimator, strlen(pcxt->library_name)
259  + strlen(pcxt->function_name) + 2);
260  shm_toc_estimate_keys(&pcxt->estimator, 1);
261  }
262  }
263 
264  /*
265  * Create DSM and initialize with new table of contents. But if the user
266  * didn't request any workers, then don't bother creating a dynamic shared
267  * memory segment; instead, just use backend-private memory.
268  *
269  * Also, if we can't create a dynamic shared memory segment because the
270  * maximum number of segments have already been created, then fall back to
271  * backend-private memory, and plan not to use any workers. We hope this
272  * won't happen very often, but it's better to abandon the use of
273  * parallelism than to fail outright.
274  */
275  segsize = shm_toc_estimate(&pcxt->estimator);
276  if (pcxt->nworkers > 0)
278  if (pcxt->seg != NULL)
280  dsm_segment_address(pcxt->seg),
281  segsize);
282  else
283  {
284  pcxt->nworkers = 0;
287  segsize);
288  }
289 
290  /* Initialize fixed-size state in shared memory. */
291  fps = (FixedParallelState *)
292  shm_toc_allocate(pcxt->toc, sizeof(FixedParallelState));
293  fps->database_id = MyDatabaseId;
301  fps->entrypoint = pcxt->entrypoint;
302  SpinLockInit(&fps->mutex);
303  fps->last_xlog_end = 0;
305 
306  /* We can skip the rest of this if we're not budgeting for any workers. */
307  if (pcxt->nworkers > 0)
308  {
309  char *libraryspace;
310  char *gucspace;
311  char *combocidspace;
312  char *tsnapspace;
313  char *asnapspace;
314  char *tstatespace;
315  char *error_queue_space;
316 
317  /* Serialize shared libraries we have loaded. */
318  libraryspace = shm_toc_allocate(pcxt->toc, library_len);
319  SerializeLibraryState(library_len, libraryspace);
320  shm_toc_insert(pcxt->toc, PARALLEL_KEY_LIBRARY, libraryspace);
321 
322  /* Serialize GUC settings. */
323  gucspace = shm_toc_allocate(pcxt->toc, guc_len);
324  SerializeGUCState(guc_len, gucspace);
325  shm_toc_insert(pcxt->toc, PARALLEL_KEY_GUC, gucspace);
326 
327  /* Serialize combo CID state. */
328  combocidspace = shm_toc_allocate(pcxt->toc, combocidlen);
329  SerializeComboCIDState(combocidlen, combocidspace);
330  shm_toc_insert(pcxt->toc, PARALLEL_KEY_COMBO_CID, combocidspace);
331 
332  /* Serialize transaction snapshot and active snapshot. */
333  tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen);
334  SerializeSnapshot(transaction_snapshot, tsnapspace);
336  tsnapspace);
337  asnapspace = shm_toc_allocate(pcxt->toc, asnaplen);
338  SerializeSnapshot(active_snapshot, asnapspace);
339  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace);
340 
341  /* Serialize transaction state. */
342  tstatespace = shm_toc_allocate(pcxt->toc, tstatelen);
343  SerializeTransactionState(tstatelen, tstatespace);
345 
346  /* Allocate space for worker information. */
347  pcxt->worker = palloc0(sizeof(ParallelWorkerInfo) * pcxt->nworkers);
348 
349  /*
350  * Establish error queues in dynamic shared memory.
351  *
352  * These queues should be used only for transmitting ErrorResponse,
353  * NoticeResponse, and NotifyResponse protocol messages. Tuple data
354  * should be transmitted via separate (possibly larger?) queues.
355  */
356  error_queue_space =
357  shm_toc_allocate(pcxt->toc,
359  pcxt->nworkers));
360  for (i = 0; i < pcxt->nworkers; ++i)
361  {
362  char *start;
363  shm_mq *mq;
364 
365  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
366  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
368  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
369  }
370  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ERROR_QUEUE, error_queue_space);
371 
372  /* Serialize extension entrypoint information. */
373  if (pcxt->library_name != NULL)
374  {
375  Size lnamelen = strlen(pcxt->library_name);
376  char *extensionstate;
377 
378  extensionstate = shm_toc_allocate(pcxt->toc, lnamelen
379  + strlen(pcxt->function_name) + 2);
380  strcpy(extensionstate, pcxt->library_name);
381  strcpy(extensionstate + lnamelen + 1, pcxt->function_name);
383  extensionstate);
384  }
385  }
386 
387  /* Restore previous memory context. */
388  MemoryContextSwitchTo(oldcontext);
389 }
parallel_worker_main_type entrypoint
Definition: parallel.h:38
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:47
shm_toc * shm_toc_create(uint64 magic, void *address, Size nbytes)
Definition: shm_toc.c:40
int MyProcPid
Definition: globals.c:38
BackendId MyBackendId
Definition: globals.c:72
MemoryContext TopTransactionContext
Definition: mcxt.c:48
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:43
shm_toc_estimator estimator
Definition: parallel.h:42
#define SpinLockInit(lock)
Definition: spin.h:60
void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
Definition: namespace.c:3068
Oid authenticated_user_id
Definition: parallel.c:71
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:834
PGPROC * parallel_master_pgproc
Definition: parallel.c:76
Oid temp_toast_namespace_id
Definition: parallel.c:74
Size shm_toc_estimate(shm_toc_estimator *e)
Definition: shm_toc.c:241
void SerializeTransactionState(Size maxsize, char *start_address)
Definition: xact.c:4847
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:49
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
parallel_worker_main_type entrypoint
Definition: parallel.c:81
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:753
ParallelWorkerInfo * worker
Definition: parallel.h:46
void SerializeLibraryState(Size maxsize, char *start_address)
Definition: dfmgr.c:723
Oid GetAuthenticatedUserId(void)
Definition: miscinit.c:342
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:61
char * function_name
Definition: parallel.h:40
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2016
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:167
#define PARALLEL_KEY_FIXED
Definition: parallel.c:56
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:57
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:388
shm_mq_handle * error_mqh
Definition: parallel.h:28
#define PARALLEL_KEY_GUC
Definition: parallel.c:59
Size EstimateGUCStateSpace(void)
Definition: guc.c:9044
Size EstimateComboCIDStateSpace(void)
Definition: combocid.c:299
XLogRecPtr last_xlog_end
Definition: parallel.c:87
MemoryContext TopMemoryContext
Definition: mcxt.c:43
Size EstimateSnapshotSpace(Snapshot snap)
Definition: snapmgr.c:1992
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
void * palloc0(Size size)
Definition: mcxt.c:920
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:459
Oid MyDatabaseId
Definition: globals.c:76
pid_t parallel_master_pid
Definition: parallel.c:77
Size EstimateLibraryStateSpace(void)
Definition: dfmgr.c:706
#define PARALLEL_MAGIC
Definition: parallel.c:50
char * library_name
Definition: parallel.h:39
BackendId parallel_master_backend_id
Definition: parallel.c:78
#define NULL
Definition: c.h:226
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:989
#define Assert(condition)
Definition: c.h:671
#define PARALLEL_KEY_EXTENSION_TRAMPOLINE
Definition: parallel.c:64
Size EstimateTransactionStateSpace(void)
Definition: xact.c:4814
size_t Size
Definition: c.h:353
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:52
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:83
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:63
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:161
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
int i
Definition: shm_mq.c:69
#define BUFFERALIGN(LEN)
Definition: c.h:586
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:196
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:60
void SerializeGUCState(Size maxsize, char *start_address)
Definition: guc.c:9191
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:62
void SerializeComboCIDState(Size maxsize, char *start_address)
Definition: combocid.c:318
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:58
shm_toc * toc
Definition: parallel.h:45
void * private_memory
Definition: parallel.h:44
static void ParallelExtensionTrampoline(dsm_segment *seg, shm_toc *toc)
Definition: parallel.c:1134
void LaunchParallelWorkers ( ParallelContext pcxt)

Definition at line 433 of file parallel.c.

References Assert, BecomeLockGroupLeader(), BackgroundWorker::bgw_extra, BGW_EXTRALEN, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_main, 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, ParallelWorkerMain(), pfree(), RegisterDynamicBackgroundWorker(), ParallelContext::seg, shm_mq_set_handle(), snprintf(), TopTransactionContext, UInt32GetDatum, and ParallelContext::worker.

Referenced by ExecGather().

434 {
435  MemoryContext oldcontext;
436  BackgroundWorker worker;
437  int i;
438  bool any_registrations_failed = false;
439 
440  /* Skip this if we have no workers. */
441  if (pcxt->nworkers == 0)
442  return;
443 
444  /* We need to be a lock group leader. */
446 
447  /* If we do have workers, we'd better have a DSM segment. */
448  Assert(pcxt->seg != NULL);
449 
450  /* We might be running in a short-lived memory context. */
452 
453  /* Configure a worker. */
454  snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
455  MyProcPid);
456  worker.bgw_flags =
461  worker.bgw_main = ParallelWorkerMain;
463  worker.bgw_notify_pid = MyProcPid;
464  memset(&worker.bgw_extra, 0, BGW_EXTRALEN);
465 
466  /*
467  * Start workers.
468  *
469  * The caller must be able to tolerate ending up with fewer workers than
470  * expected, so there is no need to throw an error here if registration
471  * fails. It wouldn't help much anyway, because registering the worker in
472  * no way guarantees that it will start up and initialize successfully.
473  */
474  for (i = 0; i < pcxt->nworkers; ++i)
475  {
476  memcpy(worker.bgw_extra, &i, sizeof(int));
477  if (!any_registrations_failed &&
479  &pcxt->worker[i].bgwhandle))
480  {
482  pcxt->worker[i].bgwhandle);
483  pcxt->nworkers_launched++;
484  }
485  else
486  {
487  /*
488  * If we weren't able to register the worker, then we've bumped up
489  * against the max_worker_processes limit, and future
490  * registrations will probably fail too, so arrange to skip them.
491  * But we still have to execute this code for the remaining slots
492  * to make sure that we forget about the error queues we budgeted
493  * for those workers. Otherwise, we'll wait for them to start,
494  * but they never will.
495  */
496  any_registrations_failed = true;
497  pcxt->worker[i].bgwhandle = NULL;
498  pfree(pcxt->worker[i].error_mqh);
499  pcxt->worker[i].error_mqh = NULL;
500  }
501  }
502 
503  /* Restore previous memory context. */
504  MemoryContextSwitchTo(oldcontext);
505 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
int MyProcPid
Definition: globals.c:38
MemoryContext TopTransactionContext
Definition: mcxt.c:48
dsm_segment * seg
Definition: parallel.h:43
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1017
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
ParallelWorkerInfo * worker
Definition: parallel.h:46
static void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:935
Datum bgw_main_arg
Definition: bgworker.h:97
void pfree(void *pointer)
Definition: mcxt.c:992
bgworker_main_type bgw_main
Definition: bgworker.h:94
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:501
int nworkers_launched
Definition: parallel.h:37
void BecomeLockGroupLeader(void)
Definition: proc.c:1788
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:311
#define NULL
Definition: c.h:226
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:671
#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:864
pid_t bgw_notify_pid
Definition: bgworker.h:99
int i
#define BGW_EXTRALEN
Definition: bgworker.h:86
bool ParallelContextActive ( void  )

Definition at line 681 of file parallel.c.

References dlist_is_empty().

Referenced by ExitParallelMode().

682 {
683  return !dlist_is_empty(&pcxt_list);
684 }
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static dlist_head pcxt_list
Definition: parallel.c:108
static void ParallelExtensionTrampoline ( dsm_segment seg,
shm_toc toc 
)
static

Definition at line 1134 of file parallel.c.

References Assert, load_external_function(), NULL, PARALLEL_KEY_EXTENSION_TRAMPOLINE, and shm_toc_lookup().

Referenced by CreateParallelContextForExternalFunction(), and InitializeParallelDSM().

1135 {
1136  char *extensionstate;
1137  char *library_name;
1138  char *function_name;
1139  parallel_worker_main_type entrypt;
1140 
1141  extensionstate = shm_toc_lookup(toc, PARALLEL_KEY_EXTENSION_TRAMPOLINE);
1142  Assert(extensionstate != NULL);
1143  library_name = extensionstate;
1144  function_name = extensionstate + strlen(library_name) + 1;
1145 
1146  entrypt = (parallel_worker_main_type)
1147  load_external_function(library_name, function_name, true, NULL);
1148  entrypt(seg, toc);
1149 }
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
PGFunction load_external_function(char *filename, char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define PARALLEL_KEY_EXTENSION_TRAMPOLINE
Definition: parallel.c:64
void(* parallel_worker_main_type)(dsm_segment *seg, shm_toc *toc)
Definition: parallel.h:23
static void ParallelWorkerMain ( Datum  main_arg)
static

Definition at line 935 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(), FixedParallelState::entrypoint, ereport, errcode(), errmsg(), ERROR, ExitParallelMode(), GetDatabaseEncoding(), InitializingParallelWorker, InvalidateSystemCaches(), MyBgworkerEntry, MyCancelKey, MyProc, MyProcPid, NULL, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ACTIVE_SNAPSHOT, PARALLEL_KEY_COMBO_CID, 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.

Referenced by LaunchParallelWorkers().

936 {
937  dsm_segment *seg;
938  shm_toc *toc;
939  FixedParallelState *fps;
940  char *error_queue_space;
941  shm_mq *mq;
942  shm_mq_handle *mqh;
943  char *libraryspace;
944  char *gucspace;
945  char *combocidspace;
946  char *tsnapspace;
947  char *asnapspace;
948  char *tstatespace;
949  StringInfoData msgbuf;
950 
951  /* Set flag to indicate that we're initializing a parallel worker. */
953 
954  /* Establish signal handlers. */
955  pqsignal(SIGTERM, die);
957 
958  /* Determine and set our parallel worker number. */
960  memcpy(&ParallelWorkerNumber, MyBgworkerEntry->bgw_extra, sizeof(int));
961 
962  /* Set up a memory context and resource owner. */
964  CurrentResourceOwner = ResourceOwnerCreate(NULL, "parallel toplevel");
966  "Parallel worker",
968 
969  /*
970  * Now that we have a resource owner, we can attach to the dynamic shared
971  * memory segment and read the table of contents.
972  */
973  seg = dsm_attach(DatumGetUInt32(main_arg));
974  if (seg == NULL)
975  ereport(ERROR,
976  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
977  errmsg("could not map dynamic shared memory segment")));
979  if (toc == NULL)
980  ereport(ERROR,
981  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
982  errmsg("invalid magic number in dynamic shared memory segment")));
983 
984  /* Look up fixed parallel state. */
986  Assert(fps != NULL);
987  MyFixedParallelState = fps;
988 
989  /*
990  * Now that we have a worker number, we can find and attach to the error
991  * queue provided for us. That's good, because until we do that, any
992  * errors that happen here will not be reported back to the process that
993  * requested that this worker be launched.
994  */
995  error_queue_space = shm_toc_lookup(toc, PARALLEL_KEY_ERROR_QUEUE);
996  mq = (shm_mq *) (error_queue_space +
999  mqh = shm_mq_attach(mq, seg, NULL);
1000  pq_redirect_to_shm_mq(seg, mqh);
1003 
1004  /*
1005  * Send a BackendKeyData message to the process that initiated parallelism
1006  * so that it has access to our PID before it receives any other messages
1007  * from us. Our cancel key is sent, too, since that's the way the
1008  * protocol message is defined, but it won't actually be used for anything
1009  * in this case.
1010  */
1011  pq_beginmessage(&msgbuf, 'K');
1012  pq_sendint(&msgbuf, (int32) MyProcPid, sizeof(int32));
1013  pq_sendint(&msgbuf, (int32) MyCancelKey, sizeof(int32));
1014  pq_endmessage(&msgbuf);
1015 
1016  /*
1017  * Hooray! Primary initialization is complete. Now, we need to set up our
1018  * backend-local state to match the original backend.
1019  */
1020 
1021  /*
1022  * Join locking group. We must do this before anything that could try to
1023  * acquire a heavyweight lock, because any heavyweight locks acquired to
1024  * this point could block either directly against the parallel group
1025  * leader or against some process which in turn waits for a lock that
1026  * conflicts with the parallel group leader, causing an undetected
1027  * deadlock. (If we can't join the lock group, the leader has gone away,
1028  * so just exit quietly.)
1029  */
1031  fps->parallel_master_pid))
1032  return;
1033 
1034  /*
1035  * Load libraries that were loaded by original backend. We want to do
1036  * this before restoring GUCs, because the libraries might define custom
1037  * variables.
1038  */
1039  libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY);
1040  Assert(libraryspace != NULL);
1041  RestoreLibraryState(libraryspace);
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);
1055  Assert(gucspace != NULL);
1057  RestoreGUCState(gucspace);
1059 
1060  /* Crank up a transaction state appropriate to a parallel worker. */
1061  tstatespace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_STATE);
1062  StartParallelWorkerTransaction(tstatespace);
1063 
1064  /* Restore combo CID state. */
1065  combocidspace = shm_toc_lookup(toc, PARALLEL_KEY_COMBO_CID);
1066  Assert(combocidspace != NULL);
1067  RestoreComboCIDState(combocidspace);
1068 
1069  /* Restore transaction snapshot. */
1071  Assert(tsnapspace != NULL);
1073  fps->parallel_master_pgproc);
1074 
1075  /* Restore active snapshot. */
1076  asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT);
1077  Assert(asnapspace != NULL);
1078  PushActiveSnapshot(RestoreSnapshot(asnapspace));
1079 
1080  /*
1081  * We've changed which tuples we can see, and must therefore invalidate
1082  * system caches.
1083  */
1085 
1086  /* Restore user ID and security context. */
1088 
1089  /* Restore temp-namespace state to ensure search path matches leader's. */
1092 
1093  /* Set ParallelMasterBackendId so we know how to address temp relations. */
1095 
1096  /*
1097  * We've initialized all of our state now; nothing should change
1098  * hereafter.
1099  */
1102 
1103  /*
1104  * Time to do the real work: invoke the caller-supplied code.
1105  *
1106  * If you get a crash at this line, see the comments for
1107  * ParallelExtensionTrampoline.
1108  */
1109  fps->entrypoint(seg, toc);
1110 
1111  /* Must exit parallel mode to pop active snapshot. */
1112  ExitParallelMode();
1113 
1114  /* Must pop active snapshot so resowner.c doesn't complain. */
1116 
1117  /* Shut down the parallel-worker transaction. */
1119 
1120  /* Report success. */
1121  pq_putmessage('X', NULL, 0);
1122 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
#define DatumGetUInt32(X)
Definition: postgres.h:494
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:47
int MyProcPid
Definition: globals.c:38
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2072
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:395
PGPROC * MyProc
Definition: proc.c:67
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void CommitTransactionCommand(void)
Definition: xact.c:2745
void EndParallelWorkerTransaction(void)
Definition: xact.c:4938
void RestoreTransactionSnapshot(Snapshot snapshot, void *master_pgproc)
Definition: snapmgr.c:2135
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:550
Oid authenticated_user_id
Definition: parallel.c:71
int errcode(int sqlerrcode)
Definition: elog.c:575
PGPROC * parallel_master_pgproc
Definition: parallel.c:76
Oid temp_toast_namespace_id
Definition: parallel.c:74
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:189
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1818
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
void InvalidateSystemCaches(void)
Definition: inval.c:634
parallel_worker_main_type entrypoint
Definition: parallel.c:81
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:253
#define ERROR
Definition: elog.h:43
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:61
int32 MyCancelKey
Definition: globals.c:41
void ExitParallelMode(void)
Definition: xact.c:892
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define PARALLEL_KEY_FIXED
Definition: parallel.c:56
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:57
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
Definition: namespace.c:3084
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
#define PARALLEL_KEY_GUC
Definition: parallel.c:59
int SetClientEncoding(int encoding)
Definition: mbutils.c:212
int ParallelWorkerNumber
Definition: parallel.c:96
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
Definition: postmaster.c:5468
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:440
void RestoreLibraryState(char *start_address)
Definition: dfmgr.c:745
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition: shm_toc.c:59
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
pid_t parallel_master_pid
Definition: parallel.c:77
#define PARALLEL_MAGIC
Definition: parallel.c:50
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:78
#define NULL
Definition: c.h:226
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:989
#define Assert(condition)
Definition: c.h:671
BackendId ParallelMasterBackendId
Definition: globals.c:74
void StartParallelWorkerTransaction(char *tstatespace)
Definition: xact.c:4915
void StartTransactionCommand(void)
Definition: xact.c:2675
void EnterParallelMode(void)
Definition: xact.c:879
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:63
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:105
void die(SIGNAL_ARGS)
Definition: postgres.c:2617
bool InitializingParallelWorker
Definition: parallel.c:102
Definition: shm_mq.c:69
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:43
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:60
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:62
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:58
void RestoreGUCState(void *gucstate)
Definition: guc.c:9269
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5497
void ParallelWorkerReportLastRecEnd ( XLogRecPtr  last_xlog_end)

Definition at line 1156 of file parallel.c.

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

Referenced by CommitTransaction().

1157 {
1159 
1160  Assert(fps != NULL);
1161  SpinLockAcquire(&fps->mutex);
1162  if (fps->last_xlog_end < last_xlog_end)
1163  fps->last_xlog_end = last_xlog_end;
1164  SpinLockRelease(&fps->mutex);
1165 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr last_xlog_end
Definition: parallel.c:87
#define SpinLockRelease(lock)
Definition: spin.h:64
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static FixedParallelState * MyFixedParallelState
Definition: parallel.c:105
void ReinitializeParallelDSM ( ParallelContext pcxt)

Definition at line 396 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().

397 {
398  FixedParallelState *fps;
399  char *error_queue_space;
400  int i;
401 
402  /* Wait for any old workers to exit. */
403  if (pcxt->nworkers_launched > 0)
404  {
407  pcxt->nworkers_launched = 0;
408  }
409 
410  /* Reset a few bits of fixed parallel state to a clean state. */
411  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED);
412  fps->last_xlog_end = 0;
413 
414  /* Recreate error queues. */
415  error_queue_space =
417  for (i = 0; i < pcxt->nworkers; ++i)
418  {
419  char *start;
420  shm_mq *mq;
421 
422  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
423  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
425  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
426  }
427 }
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:47
PGPROC * MyProc
Definition: proc.c:67
dsm_segment * seg
Definition: parallel.h:43
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:569
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:519
ParallelWorkerInfo * worker
Definition: parallel.h:46
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:167
#define PARALLEL_KEY_FIXED
Definition: parallel.c:56
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:57
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:37
XLogRecPtr last_xlog_end
Definition: parallel.c:87
#define NULL
Definition: c.h:226
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
shm_toc * toc
Definition: parallel.h:45
static void WaitForParallelWorkersToExit ( ParallelContext pcxt)
static

Definition at line 569 of file parallel.c.

References BGWH_POSTMASTER_DIED, ParallelWorkerInfo::bgwhandle, ereport, errcode(), errmsg(), FATAL, i, NULL, ParallelContext::nworkers_launched, pfree(), status(), WaitForBackgroundWorkerShutdown(), and ParallelContext::worker.

Referenced by DestroyParallelContext(), and ReinitializeParallelDSM().

570 {
571  int i;
572 
573  /* Wait until the workers actually die. */
574  for (i = 0; i < pcxt->nworkers_launched; ++i)
575  {
577 
578  if (pcxt->worker == NULL || pcxt->worker[i].bgwhandle == NULL)
579  continue;
580 
582 
583  /*
584  * If the postmaster kicked the bucket, we have no chance of cleaning
585  * up safely -- we won't be able to tell when our workers are actually
586  * dead. This doesn't necessitate a PANIC since they will all abort
587  * eventually, but we can't safely continue this session.
588  */
589  if (status == BGWH_POSTMASTER_DIED)
590  ereport(FATAL,
591  (errcode(ERRCODE_ADMIN_SHUTDOWN),
592  errmsg("postmaster exited during a parallel transaction")));
593 
594  /* Release memory. */
595  pfree(pcxt->worker[i].bgwhandle);
596  pcxt->worker[i].bgwhandle = NULL;
597  }
598 }
int errcode(int sqlerrcode)
Definition: elog.c:575
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:992
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1060
#define FATAL
Definition: elog.h:52
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:37
BgwHandleStatus
Definition: bgworker.h:102
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
void WaitForParallelWorkersToFinish ( ParallelContext pcxt)

Definition at line 519 of file parallel.c.

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

Referenced by ExecParallelFinish(), and ReinitializeParallelDSM().

520 {
521  for (;;)
522  {
523  bool anyone_alive = false;
524  int i;
525 
526  /*
527  * This will process any parallel messages that are pending, which may
528  * change the outcome of the loop that follows. It may also throw an
529  * error propagated from a worker.
530  */
532 
533  for (i = 0; i < pcxt->nworkers_launched; ++i)
534  {
535  if (pcxt->worker[i].error_mqh != NULL)
536  {
537  anyone_alive = true;
538  break;
539  }
540  }
541 
542  if (!anyone_alive)
543  break;
544 
548  }
549 
550  if (pcxt->toc != NULL)
551  {
552  FixedParallelState *fps;
553 
554  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED);
555  if (fps->last_xlog_end > XactLastRecEnd)
557  }
558 }
XLogRecPtr XactLastRecEnd
Definition: xlog.c:336
PGPROC * MyProc
Definition: proc.c:67
void ResetLatch(volatile Latch *latch)
Definition: latch.c:462
Latch procLatch
Definition: proc.h:93
ParallelWorkerInfo * worker
Definition: parallel.h:46
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:301
#define PARALLEL_KEY_FIXED
Definition: parallel.c:56
void * shm_toc_lookup(shm_toc *toc, uint64 key)
Definition: shm_toc.c:218
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:37
XLogRecPtr last_xlog_end
Definition: parallel.c:87
#define NULL
Definition: c.h:226
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define WL_LATCH_SET
Definition: latch.h:124
shm_toc * toc
Definition: parallel.h:45

Variable Documentation

bool InitializingParallelWorker = false
FixedParallelState* MyFixedParallelState
static

Definition at line 105 of file parallel.c.

Referenced by ParallelWorkerReportLastRecEnd().

volatile bool ParallelMessagePending = false
int ParallelWorkerNumber = -1
dlist_head pcxt_list = DLIST_STATIC_INIT(pcxt_list)
static

Definition at line 108 of file parallel.c.