PostgreSQL Source Code  git master
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
 
struct  ParallelWorkerContext
 

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
 
typedef struct ParallelWorkerContext ParallelWorkerContext
 

Functions

ParallelContextCreateParallelContext (const char *library_name, const char *function_name, int nworkers)
 
void InitializeParallelDSM (ParallelContext *pcxt)
 
void ReinitializeParallelDSM (ParallelContext *pcxt)
 
void ReinitializeParallelWorkers (ParallelContext *pcxt, int nworkers_to_launch)
 
void LaunchParallelWorkers (ParallelContext *pcxt)
 
void WaitForParallelWorkersToAttach (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
 
PGDLLIMPORT int ParallelWorkerNumber
 
PGDLLIMPORT bool InitializingParallelWorker
 

Macro Definition Documentation

◆ IsParallelWorker

Typedef Documentation

◆ parallel_worker_main_type

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

Definition at line 23 of file parallel.h.

◆ ParallelContext

◆ ParallelWorkerContext

◆ ParallelWorkerInfo

Function Documentation

◆ AtEOSubXact_Parallel()

void AtEOSubXact_Parallel ( bool  isCommit,
SubTransactionId  mySubId 
)

Definition at line 1204 of file parallel.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

1205 {
1206  while (!dlist_is_empty(&pcxt_list))
1207  {
1208  ParallelContext *pcxt;
1209 
1211  if (pcxt->subid != mySubId)
1212  break;
1213  if (isCommit)
1214  elog(WARNING, "leaked parallel context");
1215  DestroyParallelContext(pcxt);
1216  }
1217 }
SubTransactionId subid
Definition: parallel.h:35
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:905
#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
#define elog(elevel,...)
Definition: elog.h:228
static dlist_head pcxt_list
Definition: parallel.c:124

◆ AtEOXact_Parallel()

void AtEOXact_Parallel ( bool  isCommit)

Definition at line 1223 of file parallel.c.

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

Referenced by AbortTransaction(), and CommitTransaction().

1224 {
1225  while (!dlist_is_empty(&pcxt_list))
1226  {
1227  ParallelContext *pcxt;
1228 
1230  if (isCommit)
1231  elog(WARNING, "leaked parallel context");
1232  DestroyParallelContext(pcxt);
1233  }
1234 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:905
#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
#define elog(elevel,...)
Definition: elog.h:228
static dlist_head pcxt_list
Definition: parallel.c:124

◆ CreateParallelContext()

ParallelContext* CreateParallelContext ( const char *  library_name,
const char *  function_name,
int  nworkers 
)

Definition at line 164 of file parallel.c.

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

Referenced by _bt_begin_parallel(), begin_parallel_vacuum(), and ExecInitParallelPlan().

166 {
167  MemoryContext oldcontext;
168  ParallelContext *pcxt;
169 
170  /* It is unsafe to create a parallel context if not in parallel mode. */
172 
173  /* Number of workers should be non-negative. */
174  Assert(nworkers >= 0);
175 
176  /* We might be running in a short-lived memory context. */
178 
179  /* Initialize a new ParallelContext. */
180  pcxt = palloc0(sizeof(ParallelContext));
182  pcxt->nworkers = nworkers;
183  pcxt->nworkers_to_launch = nworkers;
184  pcxt->library_name = pstrdup(library_name);
185  pcxt->function_name = pstrdup(function_name);
188  dlist_push_head(&pcxt_list, &pcxt->node);
189 
190  /* Restore previous memory context. */
191  MemoryContextSwitchTo(oldcontext);
192 
193  return pcxt;
194 }
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
char * pstrdup(const char *in)
Definition: mcxt.c:1187
shm_toc_estimator estimator
Definition: parallel.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int nworkers_to_launch
Definition: parallel.h:37
SubTransactionId subid
Definition: parallel.h:35
ErrorContextCallback * error_context_stack
Definition: elog.c:93
bool IsInParallelMode(void)
Definition: xact.c:1012
char * function_name
Definition: parallel.h:40
#define shm_toc_initialize_estimator(e)
Definition: shm_toc.h:49
void * palloc0(Size size)
Definition: mcxt.c:981
char * library_name
Definition: parallel.h:39
#define Assert(condition)
Definition: c.h:792
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:723
dlist_node node
Definition: parallel.h:34
ErrorContextCallback * error_context_stack
Definition: parallel.h:41
static dlist_head pcxt_list
Definition: parallel.c:124

◆ DestroyParallelContext()

void DestroyParallelContext ( ParallelContext pcxt)

Definition at line 905 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, ParallelContext::nworkers_launched, pfree(), ParallelContext::private_memory, RESUME_INTERRUPTS, ParallelContext::seg, shm_mq_detach(), TerminateBackgroundWorker(), WaitForParallelWorkersToExit(), and ParallelContext::worker.

Referenced by _bt_begin_parallel(), _bt_end_parallel(), AtEOSubXact_Parallel(), AtEOXact_Parallel(), end_parallel_vacuum(), and ExecParallelCleanup().

906 {
907  int i;
908 
909  /*
910  * Be careful about order of operations here! We remove the parallel
911  * context from the list before we do anything else; otherwise, if an
912  * error occurs during a subsequent step, we might try to nuke it again
913  * from AtEOXact_Parallel or AtEOSubXact_Parallel.
914  */
915  dlist_delete(&pcxt->node);
916 
917  /* Kill each worker in turn, and forget their error queues. */
918  if (pcxt->worker != NULL)
919  {
920  for (i = 0; i < pcxt->nworkers_launched; ++i)
921  {
922  if (pcxt->worker[i].error_mqh != NULL)
923  {
925 
926  shm_mq_detach(pcxt->worker[i].error_mqh);
927  pcxt->worker[i].error_mqh = NULL;
928  }
929  }
930  }
931 
932  /*
933  * If we have allocated a shared memory segment, detach it. This will
934  * implicitly detach the error queues, and any other shared memory queues,
935  * stored there.
936  */
937  if (pcxt->seg != NULL)
938  {
939  dsm_detach(pcxt->seg);
940  pcxt->seg = NULL;
941  }
942 
943  /*
944  * If this parallel context is actually in backend-private memory rather
945  * than shared memory, free that memory instead.
946  */
947  if (pcxt->private_memory != NULL)
948  {
949  pfree(pcxt->private_memory);
950  pcxt->private_memory = NULL;
951  }
952 
953  /*
954  * We can't finish transaction commit or abort until all of the workers
955  * have exited. This means, in particular, that we can't respond to
956  * interrupts at this stage.
957  */
958  HOLD_INTERRUPTS();
961 
962  /* Free the worker array itself. */
963  if (pcxt->worker != NULL)
964  {
965  pfree(pcxt->worker);
966  pcxt->worker = NULL;
967  }
968 
969  /* Free memory. */
970  pfree(pcxt->library_name);
971  pfree(pcxt->function_name);
972  pfree(pcxt);
973 }
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:817
dsm_segment * seg
Definition: parallel.h:43
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:865
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:119
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:1057
char * function_name
Definition: parallel.h:40
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:38
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1228
char * library_name
Definition: parallel.h:39
dlist_node node
Definition: parallel.h:34
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:117
int i
void * private_memory
Definition: parallel.h:44

◆ HandleParallelMessageInterrupt()

void HandleParallelMessageInterrupt ( void  )

Definition at line 992 of file parallel.c.

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

Referenced by procsignal_sigusr1_handler().

993 {
994  InterruptPending = true;
995  ParallelMessagePending = true;
996  SetLatch(MyLatch);
997 }
void SetLatch(Latch *latch)
Definition: latch.c:505
volatile bool ParallelMessagePending
Definition: parallel.c:115
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
struct Latch * MyLatch
Definition: globals.c:55

◆ HandleParallelMessages()

void HandleParallelMessages ( void  )

Definition at line 1003 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(), ParallelContext::nworkers_launched, ParallelMessagePending, pfree(), RESUME_INTERRUPTS, shm_mq_receive(), SHM_MQ_SUCCESS, SHM_MQ_WOULD_BLOCK, TopMemoryContext, and ParallelContext::worker.

Referenced by ProcessInterrupts().

1004 {
1005  dlist_iter iter;
1006  MemoryContext oldcontext;
1007 
1008  static MemoryContext hpm_context = NULL;
1009 
1010  /*
1011  * This is invoked from ProcessInterrupts(), and since some of the
1012  * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
1013  * for recursive calls if more signals are received while this runs. It's
1014  * unclear that recursive entry would be safe, and it doesn't seem useful
1015  * even if it is safe, so let's block interrupts until done.
1016  */
1017  HOLD_INTERRUPTS();
1018 
1019  /*
1020  * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
1021  * don't want to risk leaking data into long-lived contexts, so let's do
1022  * our work here in a private context that we can reset on each use.
1023  */
1024  if (hpm_context == NULL) /* first time through? */
1026  "HandleParallelMessages",
1028  else
1029  MemoryContextReset(hpm_context);
1030 
1031  oldcontext = MemoryContextSwitchTo(hpm_context);
1032 
1033  /* OK to process messages. Reset the flag saying there are more to do. */
1034  ParallelMessagePending = false;
1035 
1036  dlist_foreach(iter, &pcxt_list)
1037  {
1038  ParallelContext *pcxt;
1039  int i;
1040 
1041  pcxt = dlist_container(ParallelContext, node, iter.cur);
1042  if (pcxt->worker == NULL)
1043  continue;
1044 
1045  for (i = 0; i < pcxt->nworkers_launched; ++i)
1046  {
1047  /*
1048  * Read as many messages as we can from each worker, but stop when
1049  * either (1) the worker's error queue goes away, which can happen
1050  * if we receive a Terminate message from the worker; or (2) no
1051  * more messages can be read from the worker without blocking.
1052  */
1053  while (pcxt->worker[i].error_mqh != NULL)
1054  {
1055  shm_mq_result res;
1056  Size nbytes;
1057  void *data;
1058 
1059  res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes,
1060  &data, true);
1061  if (res == SHM_MQ_WOULD_BLOCK)
1062  break;
1063  else if (res == SHM_MQ_SUCCESS)
1064  {
1065  StringInfoData msg;
1066 
1067  initStringInfo(&msg);
1068  appendBinaryStringInfo(&msg, data, nbytes);
1069  HandleParallelMessage(pcxt, i, &msg);
1070  pfree(msg.data);
1071  }
1072  else
1073  ereport(ERROR,
1074  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1075  errmsg("lost connection to parallel worker")));
1076  }
1077  }
1078  }
1079 
1080  MemoryContextSwitchTo(oldcontext);
1081 
1082  /* Might as well clear the context on our way out */
1083  MemoryContextReset(hpm_context);
1084 
1086 }
#define AllocSetContextCreate
Definition: memutils.h:170
#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:704
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:137
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:119
volatile bool ParallelMessagePending
Definition: parallel.c:115
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:45
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static void HandleParallelMessage(ParallelContext *pcxt, int i, StringInfo msg)
Definition: parallel.c:1092
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:38
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
dlist_node * cur
Definition: ilist.h:161
#define ereport(elevel,...)
Definition: elog.h:155
shm_mq_result
Definition: shm_mq.h:36
size_t Size
Definition: c.h:528
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:117
int i
static dlist_head pcxt_list
Definition: parallel.c:124
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition: shm_mq.c:548
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ InitializeParallelDSM()

void InitializeParallelDSM ( ParallelContext pcxt)

Definition at line 202 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_HANDLE_INVALID, dsm_segment_address(), ParallelWorkerInfo::error_mqh, EstimateComboCIDStateSpace(), EstimateGUCStateSpace(), EstimateLibraryStateSpace(), EstimatePendingSyncsSpace(), EstimateReindexStateSpace(), EstimateRelationMapSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), EstimateUncommittedEnumsSpace(), ParallelContext::estimator, ParallelContext::function_name, GetActiveSnapshot(), GetAuthenticatedUserId(), GetCurrentRoleId(), GetCurrentStatementStartTimestamp(), GetCurrentTransactionStartTimestamp(), GetSessionDsmHandle(), GetTempNamespaceState(), GetTransactionSnapshot(), GetUserIdAndSecContext(), i, FixedParallelState::is_superuser, FixedParallelState::last_xlog_end, ParallelContext::library_name, MemoryContextAlloc(), MemoryContextSwitchTo(), mul_size(), FixedParallelState::mutex, MyBackendId, MyDatabaseId, MyProc, MyProcPid, ParallelContext::nworkers, FixedParallelState::outer_user_id, 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_PENDING_SYNCS, PARALLEL_KEY_REINDEX_STATE, PARALLEL_KEY_RELMAPPER_STATE, PARALLEL_KEY_SESSION_DSM, PARALLEL_KEY_TRANSACTION_SNAPSHOT, PARALLEL_KEY_TRANSACTION_STATE, PARALLEL_KEY_UNCOMMITTEDENUMS, FixedParallelState::parallel_leader_backend_id, FixedParallelState::parallel_leader_pgproc, FixedParallelState::parallel_leader_pid, PARALLEL_MAGIC, ParallelContext::private_memory, FixedParallelState::sec_context, ParallelContext::seg, FixedParallelState::serializable_xact_handle, SerializeComboCIDState(), SerializeGUCState(), SerializeLibraryState(), SerializePendingSyncs(), SerializeReindexState(), SerializeRelationMap(), SerializeSnapshot(), SerializeTransactionState(), SerializeUncommittedEnums(), session_auth_is_superuser, ShareSerializableXact(), 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::stmt_ts, FixedParallelState::temp_namespace_id, FixedParallelState::temp_toast_namespace_id, ParallelContext::toc, TopMemoryContext, TopTransactionContext, ParallelContext::worker, and FixedParallelState::xact_ts.

Referenced by _bt_begin_parallel(), begin_parallel_vacuum(), and ExecInitParallelPlan().

203 {
204  MemoryContext oldcontext;
205  Size library_len = 0;
206  Size guc_len = 0;
207  Size combocidlen = 0;
208  Size tsnaplen = 0;
209  Size asnaplen = 0;
210  Size tstatelen = 0;
211  Size pendingsyncslen = 0;
212  Size reindexlen = 0;
213  Size relmapperlen = 0;
214  Size uncommittedenumslen = 0;
215  Size segsize = 0;
216  int i;
217  FixedParallelState *fps;
218  dsm_handle session_dsm_handle = DSM_HANDLE_INVALID;
219  Snapshot transaction_snapshot = GetTransactionSnapshot();
220  Snapshot active_snapshot = GetActiveSnapshot();
221 
222  /* We might be running in a very short-lived memory context. */
224 
225  /* Allow space to store the fixed-size parallel state. */
227  shm_toc_estimate_keys(&pcxt->estimator, 1);
228 
229  /*
230  * Normally, the user will have requested at least one worker process, but
231  * if by chance they have not, we can skip a bunch of things here.
232  */
233  if (pcxt->nworkers > 0)
234  {
235  /* Get (or create) the per-session DSM segment's handle. */
236  session_dsm_handle = GetSessionDsmHandle();
237 
238  /*
239  * If we weren't able to create a per-session DSM segment, then we can
240  * continue but we can't safely launch any workers because their
241  * record typmods would be incompatible so they couldn't exchange
242  * tuples.
243  */
244  if (session_dsm_handle == DSM_HANDLE_INVALID)
245  pcxt->nworkers = 0;
246  }
247 
248  if (pcxt->nworkers > 0)
249  {
250  /* Estimate space for various kinds of state sharing. */
251  library_len = EstimateLibraryStateSpace();
252  shm_toc_estimate_chunk(&pcxt->estimator, library_len);
253  guc_len = EstimateGUCStateSpace();
254  shm_toc_estimate_chunk(&pcxt->estimator, guc_len);
255  combocidlen = EstimateComboCIDStateSpace();
256  shm_toc_estimate_chunk(&pcxt->estimator, combocidlen);
257  tsnaplen = EstimateSnapshotSpace(transaction_snapshot);
258  shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen);
259  asnaplen = EstimateSnapshotSpace(active_snapshot);
260  shm_toc_estimate_chunk(&pcxt->estimator, asnaplen);
261  tstatelen = EstimateTransactionStateSpace();
262  shm_toc_estimate_chunk(&pcxt->estimator, tstatelen);
264  pendingsyncslen = EstimatePendingSyncsSpace();
265  shm_toc_estimate_chunk(&pcxt->estimator, pendingsyncslen);
266  reindexlen = EstimateReindexStateSpace();
267  shm_toc_estimate_chunk(&pcxt->estimator, reindexlen);
268  relmapperlen = EstimateRelationMapSpace();
269  shm_toc_estimate_chunk(&pcxt->estimator, relmapperlen);
270  uncommittedenumslen = EstimateUncommittedEnumsSpace();
271  shm_toc_estimate_chunk(&pcxt->estimator, uncommittedenumslen);
272  /* If you add more chunks here, you probably need to add keys. */
273  shm_toc_estimate_keys(&pcxt->estimator, 11);
274 
275  /* Estimate space need for error queues. */
278  "parallel error queue size not buffer-aligned");
281  pcxt->nworkers));
282  shm_toc_estimate_keys(&pcxt->estimator, 1);
283 
284  /* Estimate how much we'll need for the entrypoint info. */
285  shm_toc_estimate_chunk(&pcxt->estimator, strlen(pcxt->library_name) +
286  strlen(pcxt->function_name) + 2);
287  shm_toc_estimate_keys(&pcxt->estimator, 1);
288  }
289 
290  /*
291  * Create DSM and initialize with new table of contents. But if the user
292  * didn't request any workers, then don't bother creating a dynamic shared
293  * memory segment; instead, just use backend-private memory.
294  *
295  * Also, if we can't create a dynamic shared memory segment because the
296  * maximum number of segments have already been created, then fall back to
297  * backend-private memory, and plan not to use any workers. We hope this
298  * won't happen very often, but it's better to abandon the use of
299  * parallelism than to fail outright.
300  */
301  segsize = shm_toc_estimate(&pcxt->estimator);
302  if (pcxt->nworkers > 0)
304  if (pcxt->seg != NULL)
306  dsm_segment_address(pcxt->seg),
307  segsize);
308  else
309  {
310  pcxt->nworkers = 0;
313  segsize);
314  }
315 
316  /* Initialize fixed-size state in shared memory. */
317  fps = (FixedParallelState *)
318  shm_toc_allocate(pcxt->toc, sizeof(FixedParallelState));
319  fps->database_id = MyDatabaseId;
332  SpinLockInit(&fps->mutex);
333  fps->last_xlog_end = 0;
335 
336  /* We can skip the rest of this if we're not budgeting for any workers. */
337  if (pcxt->nworkers > 0)
338  {
339  char *libraryspace;
340  char *gucspace;
341  char *combocidspace;
342  char *tsnapspace;
343  char *asnapspace;
344  char *tstatespace;
345  char *pendingsyncsspace;
346  char *reindexspace;
347  char *relmapperspace;
348  char *error_queue_space;
349  char *session_dsm_handle_space;
350  char *entrypointstate;
351  char *uncommittedenumsspace;
352  Size lnamelen;
353 
354  /* Serialize shared libraries we have loaded. */
355  libraryspace = shm_toc_allocate(pcxt->toc, library_len);
356  SerializeLibraryState(library_len, libraryspace);
357  shm_toc_insert(pcxt->toc, PARALLEL_KEY_LIBRARY, libraryspace);
358 
359  /* Serialize GUC settings. */
360  gucspace = shm_toc_allocate(pcxt->toc, guc_len);
361  SerializeGUCState(guc_len, gucspace);
362  shm_toc_insert(pcxt->toc, PARALLEL_KEY_GUC, gucspace);
363 
364  /* Serialize combo CID state. */
365  combocidspace = shm_toc_allocate(pcxt->toc, combocidlen);
366  SerializeComboCIDState(combocidlen, combocidspace);
367  shm_toc_insert(pcxt->toc, PARALLEL_KEY_COMBO_CID, combocidspace);
368 
369  /* Serialize transaction snapshot and active snapshot. */
370  tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen);
371  SerializeSnapshot(transaction_snapshot, tsnapspace);
373  tsnapspace);
374  asnapspace = shm_toc_allocate(pcxt->toc, asnaplen);
375  SerializeSnapshot(active_snapshot, asnapspace);
376  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace);
377 
378  /* Provide the handle for per-session segment. */
379  session_dsm_handle_space = shm_toc_allocate(pcxt->toc,
380  sizeof(dsm_handle));
381  *(dsm_handle *) session_dsm_handle_space = session_dsm_handle;
383  session_dsm_handle_space);
384 
385  /* Serialize transaction state. */
386  tstatespace = shm_toc_allocate(pcxt->toc, tstatelen);
387  SerializeTransactionState(tstatelen, tstatespace);
389 
390  /* Serialize pending syncs. */
391  pendingsyncsspace = shm_toc_allocate(pcxt->toc, pendingsyncslen);
392  SerializePendingSyncs(pendingsyncslen, pendingsyncsspace);
394  pendingsyncsspace);
395 
396  /* Serialize reindex state. */
397  reindexspace = shm_toc_allocate(pcxt->toc, reindexlen);
398  SerializeReindexState(reindexlen, reindexspace);
399  shm_toc_insert(pcxt->toc, PARALLEL_KEY_REINDEX_STATE, reindexspace);
400 
401  /* Serialize relmapper state. */
402  relmapperspace = shm_toc_allocate(pcxt->toc, relmapperlen);
403  SerializeRelationMap(relmapperlen, relmapperspace);
405  relmapperspace);
406 
407  /* Serialize uncommitted enum state. */
408  uncommittedenumsspace = shm_toc_allocate(pcxt->toc,
409  uncommittedenumslen);
410  SerializeUncommittedEnums(uncommittedenumsspace, uncommittedenumslen);
412  uncommittedenumsspace);
413 
414  /* Allocate space for worker information. */
415  pcxt->worker = palloc0(sizeof(ParallelWorkerInfo) * pcxt->nworkers);
416 
417  /*
418  * Establish error queues in dynamic shared memory.
419  *
420  * These queues should be used only for transmitting ErrorResponse,
421  * NoticeResponse, and NotifyResponse protocol messages. Tuple data
422  * should be transmitted via separate (possibly larger?) queues.
423  */
424  error_queue_space =
425  shm_toc_allocate(pcxt->toc,
427  pcxt->nworkers));
428  for (i = 0; i < pcxt->nworkers; ++i)
429  {
430  char *start;
431  shm_mq *mq;
432 
433  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
434  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
436  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
437  }
438  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ERROR_QUEUE, error_queue_space);
439 
440  /*
441  * Serialize entrypoint information. It's unsafe to pass function
442  * pointers across processes, as the function pointer may be different
443  * in each process in EXEC_BACKEND builds, so we always pass library
444  * and function name. (We use library name "postgres" for functions
445  * in the core backend.)
446  */
447  lnamelen = strlen(pcxt->library_name);
448  entrypointstate = shm_toc_allocate(pcxt->toc, lnamelen +
449  strlen(pcxt->function_name) + 2);
450  strcpy(entrypointstate, pcxt->library_name);
451  strcpy(entrypointstate + lnamelen + 1, pcxt->function_name);
452  shm_toc_insert(pcxt->toc, PARALLEL_KEY_ENTRYPOINT, entrypointstate);
453  }
454 
455  /* Restore previous memory context. */
456  MemoryContextSwitchTo(oldcontext);
457 }
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:55
shm_toc * shm_toc_create(uint64 magic, void *address, Size nbytes)
Definition: shm_toc.c:40
int MyProcPid
Definition: globals.c:41
BackendId MyBackendId
Definition: globals.c:82
MemoryContext TopTransactionContext
Definition: mcxt.c:49
uint32 dsm_handle
Definition: dsm_impl.h:55
void SerializeUncommittedEnums(void *space, Size size)
Definition: pg_enum.c:709
PGPROC * parallel_leader_pgproc
Definition: parallel.c:92
PGPROC * MyProc
Definition: proc.c:68
dsm_segment * seg
Definition: parallel.h:43
void SerializeReindexState(Size maxsize, char *start_address)
Definition: index.c:4133
shm_toc_estimator estimator
Definition: parallel.h:42
#define SpinLockInit(lock)
Definition: spin.h:60
void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
Definition: namespace.c:3312
Oid authenticated_user_id
Definition: parallel.c:85
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
Oid temp_toast_namespace_id
Definition: parallel.c:89
#define PARALLEL_KEY_RELMAPPER_STATE
Definition: parallel.c:77
Size shm_toc_estimate(shm_toc_estimator *e)
Definition: shm_toc.c:263
void SerializeTransactionState(Size maxsize, char *start_address)
Definition: xact.c:5237
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
Oid GetCurrentRoleId(void)
Definition: miscinit.c:835
SerializableXactHandle serializable_xact_handle
Definition: parallel.c:97
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:906
ParallelWorkerInfo * worker
Definition: parallel.h:46
void SerializeLibraryState(Size maxsize, char *start_address)
Definition: dfmgr.c:726
Oid GetAuthenticatedUserId(void)
Definition: miscinit.c:535
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:70
char * function_name
Definition: parallel.h:40
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2098
#define PARALLEL_KEY_UNCOMMITTEDENUMS
Definition: parallel.c:78
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:170
#define PARALLEL_KEY_FIXED
Definition: parallel.c:65
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:66
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:581
shm_mq_handle * error_mqh
Definition: parallel.h:28
#define PARALLEL_KEY_GUC
Definition: parallel.c:68
void SerializeRelationMap(Size maxSize, char *startAddress)
Definition: relmapper.c:657
Size EstimateGUCStateSpace(void)
Definition: guc.c:10398
Size EstimateComboCIDStateSpace(void)
Definition: combocid.c:297
XLogRecPtr last_xlog_end
Definition: parallel.c:103
Size EstimateReindexStateSpace(void)
Definition: index.c:4122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798
Size EstimateUncommittedEnumsSpace(void)
Definition: pg_enum.c:695
Size EstimateSnapshotSpace(Snapshot snap)
Definition: snapmgr.c:2074
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
void * palloc0(Size size)
Definition: mcxt.c:981
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:487
Size EstimatePendingSyncsSpace(void)
Definition: storage.c:511
void SerializePendingSyncs(Size maxSize, char *startAddress)
Definition: storage.c:524
Oid MyDatabaseId
Definition: globals.c:86
Size EstimateLibraryStateSpace(void)
Definition: dfmgr.c:709
#define PARALLEL_MAGIC
Definition: parallel.c:58
TimestampTz xact_ts
Definition: parallel.c:95
BackendId parallel_leader_backend_id
Definition: parallel.c:94
char * library_name
Definition: parallel.h:39
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1054
Size EstimateTransactionStateSpace(void)
Definition: xact.c:5209
#define PARALLEL_KEY_REINDEX_STATE
Definition: parallel.c:76
size_t Size
Definition: c.h:528
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size EstimateRelationMapSpace(void)
Definition: relmapper.c:646
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:283
#define PARALLEL_KEY_TRANSACTION_STATE
Definition: parallel.c:72
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:797
int i
TimestampTz stmt_ts
Definition: parallel.c:96
Definition: shm_mq.c:71
#define PARALLEL_KEY_SESSION_DSM
Definition: parallel.c:74
#define BUFFERALIGN(LEN)
Definition: c.h:747
pid_t parallel_leader_pid
Definition: parallel.c:93
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:199
dsm_handle GetSessionDsmHandle(void)
Definition: session.c:70
#define PARALLEL_KEY_PENDING_SYNCS
Definition: parallel.c:75
bool session_auth_is_superuser
Definition: guc.c:537
SerializableXactHandle ShareSerializableXact(void)
Definition: predicate.c:5186
#define PARALLEL_KEY_ENTRYPOINT
Definition: parallel.c:73
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:69
void SerializeGUCState(Size maxsize, char *start_address)
Definition: guc.c:10540
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:71
void SerializeComboCIDState(Size maxsize, char *start_address)
Definition: combocid.c:316
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:67
shm_toc * toc
Definition: parallel.h:45
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:807
void * private_memory
Definition: parallel.h:44

◆ LaunchParallelWorkers()

void LaunchParallelWorkers ( ParallelContext pcxt)

Definition at line 528 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, BackgroundWorker::bgw_type, ParallelWorkerInfo::bgwhandle, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_CLASS_PARALLEL, BGWORKER_SHMEM_ACCESS, BgWorkerStart_ConsistentState, dsm_segment_handle(), ParallelWorkerInfo::error_mqh, i, ParallelContext::known_attached_workers, MemoryContextSwitchTo(), MyProcPid, ParallelContext::nknown_attached_workers, ParallelContext::nworkers, ParallelContext::nworkers_launched, ParallelContext::nworkers_to_launch, palloc0(), RegisterDynamicBackgroundWorker(), ParallelContext::seg, shm_mq_detach(), shm_mq_set_handle(), snprintf, sprintf, TopTransactionContext, UInt32GetDatum, and ParallelContext::worker.

Referenced by _bt_begin_parallel(), ExecGather(), ExecGatherMerge(), and lazy_parallel_vacuum_indexes().

529 {
530  MemoryContext oldcontext;
531  BackgroundWorker worker;
532  int i;
533  bool any_registrations_failed = false;
534 
535  /* Skip this if we have no workers. */
536  if (pcxt->nworkers == 0 || pcxt->nworkers_to_launch == 0)
537  return;
538 
539  /* We need to be a lock group leader. */
541 
542  /* If we do have workers, we'd better have a DSM segment. */
543  Assert(pcxt->seg != NULL);
544 
545  /* We might be running in a short-lived memory context. */
547 
548  /* Configure a worker. */
549  memset(&worker, 0, sizeof(worker));
550  snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
551  MyProcPid);
552  snprintf(worker.bgw_type, BGW_MAXLEN, "parallel worker");
553  worker.bgw_flags =
558  sprintf(worker.bgw_library_name, "postgres");
559  sprintf(worker.bgw_function_name, "ParallelWorkerMain");
561  worker.bgw_notify_pid = MyProcPid;
562 
563  /*
564  * Start workers.
565  *
566  * The caller must be able to tolerate ending up with fewer workers than
567  * expected, so there is no need to throw an error here if registration
568  * fails. It wouldn't help much anyway, because registering the worker in
569  * no way guarantees that it will start up and initialize successfully.
570  */
571  for (i = 0; i < pcxt->nworkers_to_launch; ++i)
572  {
573  memcpy(worker.bgw_extra, &i, sizeof(int));
574  if (!any_registrations_failed &&
576  &pcxt->worker[i].bgwhandle))
577  {
579  pcxt->worker[i].bgwhandle);
580  pcxt->nworkers_launched++;
581  }
582  else
583  {
584  /*
585  * If we weren't able to register the worker, then we've bumped up
586  * against the max_worker_processes limit, and future
587  * registrations will probably fail too, so arrange to skip them.
588  * But we still have to execute this code for the remaining slots
589  * to make sure that we forget about the error queues we budgeted
590  * for those workers. Otherwise, we'll wait for them to start,
591  * but they never will.
592  */
593  any_registrations_failed = true;
594  pcxt->worker[i].bgwhandle = NULL;
595  shm_mq_detach(pcxt->worker[i].error_mqh);
596  pcxt->worker[i].error_mqh = NULL;
597  }
598  }
599 
600  /*
601  * Now that nworkers_launched has taken its final value, we can initialize
602  * known_attached_workers.
603  */
604  if (pcxt->nworkers_launched > 0)
605  {
606  pcxt->known_attached_workers =
607  palloc0(sizeof(bool) * pcxt->nworkers_launched);
608  pcxt->nknown_attached_workers = 0;
609  }
610 
611  /* Restore previous memory context. */
612  MemoryContextSwitchTo(oldcontext);
613 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
int MyProcPid
Definition: globals.c:41
MemoryContext TopTransactionContext
Definition: mcxt.c:49
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:817
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:1082
int bgw_restart_time
Definition: bgworker.h:94
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
int nknown_attached_workers
Definition: parallel.h:47
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
int nworkers_to_launch
Definition: parallel.h:37
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
#define sprintf
Definition: port.h:217
ParallelWorkerInfo * worker
Definition: parallel.h:46
Datum bgw_main_arg
Definition: bgworker.h:97
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:493
int nworkers_launched
Definition: parallel.h:38
void BecomeLockGroupLeader(void)
Definition: proc.c:1917
bool * known_attached_workers
Definition: parallel.h:48
void * palloc0(Size size)
Definition: mcxt.c:981
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:311
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:792
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:977
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
pid_t bgw_notify_pid
Definition: bgworker.h:99
int i
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95
#define snprintf
Definition: port.h:215

◆ ParallelContextActive()

bool ParallelContextActive ( void  )

Definition at line 979 of file parallel.c.

References dlist_is_empty().

Referenced by AtPrepare_PredicateLocks(), ExitParallelMode(), and ReleasePredicateLocks().

980 {
981  return !dlist_is_empty(&pcxt_list);
982 }
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
static dlist_head pcxt_list
Definition: parallel.c:124

◆ ParallelWorkerMain()

void ParallelWorkerMain ( Datum  main_arg)

Definition at line 1240 of file parallel.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AttachSerializableXact(), AttachSession(), FixedParallelState::authenticated_user_id, BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), BecomeLockGroupMember(), BackgroundWorker::bgw_extra, CommitTransactionCommand(), FixedParallelState::current_user_id, CurrentMemoryContext, FixedParallelState::database_id, DatumGetUInt32, DetachSession(), die, dsm_attach(), dsm_segment_address(), EndParallelWorkerTransaction(), EnterParallelMode(), ereport, errcode(), errmsg(), ERROR, ExitParallelMode(), GetDatabaseEncoding(), InitializingParallelWorker, InvalidateSystemCaches(), FixedParallelState::is_superuser, LookupParallelWorkerFunction(), MyBgworkerEntry, MyCancelKey, MyProc, MyProcPid, on_shmem_exit(), FixedParallelState::outer_user_id, 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_PENDING_SYNCS, PARALLEL_KEY_REINDEX_STATE, PARALLEL_KEY_RELMAPPER_STATE, PARALLEL_KEY_SESSION_DSM, PARALLEL_KEY_TRANSACTION_SNAPSHOT, PARALLEL_KEY_TRANSACTION_STATE, PARALLEL_KEY_UNCOMMITTEDENUMS, FixedParallelState::parallel_leader_backend_id, FixedParallelState::parallel_leader_pgproc, FixedParallelState::parallel_leader_pid, PARALLEL_MAGIC, ParallelLeaderBackendId, ParallelLeaderPid, ParallelWorkerNumber, ParallelWorkerShutdown(), PopActiveSnapshot(), pq_beginmessage(), pq_endmessage(), pq_putmessage, pq_redirect_to_shm_mq(), pq_sendint32(), pq_set_parallel_leader(), pqsignal(), PushActiveSnapshot(), RestoreComboCIDState(), RestoreGUCState(), RestoreLibraryState(), RestorePendingSyncs(), RestoreReindexState(), RestoreRelationMap(), RestoreSnapshot(), RestoreTransactionSnapshot(), RestoreUncommittedEnums(), FixedParallelState::sec_context, FixedParallelState::serializable_xact_handle, SetClientEncoding(), SetCurrentRoleId(), SetParallelStartTimestamps(), SetTempNamespaceState(), SetUserIdAndSecContext(), shm_mq_attach(), shm_mq_set_sender(), shm_toc_attach(), shm_toc_lookup(), StartParallelWorkerTransaction(), StartTransactionCommand(), FixedParallelState::stmt_ts, FixedParallelState::temp_namespace_id, FixedParallelState::temp_toast_namespace_id, TopMemoryContext, and FixedParallelState::xact_ts.

1241 {
1242  dsm_segment *seg;
1243  shm_toc *toc;
1244  FixedParallelState *fps;
1245  char *error_queue_space;
1246  shm_mq *mq;
1247  shm_mq_handle *mqh;
1248  char *libraryspace;
1249  char *entrypointstate;
1250  char *library_name;
1251  char *function_name;
1252  parallel_worker_main_type entrypt;
1253  char *gucspace;
1254  char *combocidspace;
1255  char *tsnapspace;
1256  char *asnapspace;
1257  char *tstatespace;
1258  char *pendingsyncsspace;
1259  char *reindexspace;
1260  char *relmapperspace;
1261  char *uncommittedenumsspace;
1262  StringInfoData msgbuf;
1263  char *session_dsm_handle_space;
1264 
1265  /* Set flag to indicate that we're initializing a parallel worker. */
1267 
1268  /* Establish signal handlers. */
1269  pqsignal(SIGTERM, die);
1271 
1272  /* Determine and set our parallel worker number. */
1274  memcpy(&ParallelWorkerNumber, MyBgworkerEntry->bgw_extra, sizeof(int));
1275 
1276  /* Set up a memory context to work in, just for cleanliness. */
1278  "Parallel worker",
1280 
1281  /*
1282  * Attach to the dynamic shared memory segment for the parallel query, and
1283  * find its table of contents.
1284  *
1285  * Note: at this point, we have not created any ResourceOwner in this
1286  * process. This will result in our DSM mapping surviving until process
1287  * exit, which is fine. If there were a ResourceOwner, it would acquire
1288  * ownership of the mapping, but we have no need for that.
1289  */
1290  seg = dsm_attach(DatumGetUInt32(main_arg));
1291  if (seg == NULL)
1292  ereport(ERROR,
1293  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1294  errmsg("could not map dynamic shared memory segment")));
1296  if (toc == NULL)
1297  ereport(ERROR,
1298  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1299  errmsg("invalid magic number in dynamic shared memory segment")));
1300 
1301  /* Look up fixed parallel state. */
1302  fps = shm_toc_lookup(toc, PARALLEL_KEY_FIXED, false);
1303  MyFixedParallelState = fps;
1304 
1305  /* Arrange to signal the leader if we exit. */
1309 
1310  /*
1311  * Now we can find and attach to the error queue provided for us. That's
1312  * good, because until we do that, any errors that happen here will not be
1313  * reported back to the process that requested that this worker be
1314  * launched.
1315  */
1316  error_queue_space = shm_toc_lookup(toc, PARALLEL_KEY_ERROR_QUEUE, false);
1317  mq = (shm_mq *) (error_queue_space +
1320  mqh = shm_mq_attach(mq, seg, NULL);
1321  pq_redirect_to_shm_mq(seg, mqh);
1324 
1325  /*
1326  * Send a BackendKeyData message to the process that initiated parallelism
1327  * so that it has access to our PID before it receives any other messages
1328  * from us. Our cancel key is sent, too, since that's the way the
1329  * protocol message is defined, but it won't actually be used for anything
1330  * in this case.
1331  */
1332  pq_beginmessage(&msgbuf, 'K');
1333  pq_sendint32(&msgbuf, (int32) MyProcPid);
1334  pq_sendint32(&msgbuf, (int32) MyCancelKey);
1335  pq_endmessage(&msgbuf);
1336 
1337  /*
1338  * Hooray! Primary initialization is complete. Now, we need to set up our
1339  * backend-local state to match the original backend.
1340  */
1341 
1342  /*
1343  * Join locking group. We must do this before anything that could try to
1344  * acquire a heavyweight lock, because any heavyweight locks acquired to
1345  * this point could block either directly against the parallel group
1346  * leader or against some process which in turn waits for a lock that
1347  * conflicts with the parallel group leader, causing an undetected
1348  * deadlock. (If we can't join the lock group, the leader has gone away,
1349  * so just exit quietly.)
1350  */
1352  fps->parallel_leader_pid))
1353  return;
1354 
1355  /*
1356  * Restore transaction and statement start-time timestamps. This must
1357  * happen before anything that would start a transaction, else asserts in
1358  * xact.c will fire.
1359  */
1361 
1362  /*
1363  * Identify the entry point to be called. In theory this could result in
1364  * loading an additional library, though most likely the entry point is in
1365  * the core backend or in a library we just loaded.
1366  */
1367  entrypointstate = shm_toc_lookup(toc, PARALLEL_KEY_ENTRYPOINT, false);
1368  library_name = entrypointstate;
1369  function_name = entrypointstate + strlen(library_name) + 1;
1370 
1371  entrypt = LookupParallelWorkerFunction(library_name, function_name);
1372 
1373  /* Restore database connection. */
1375  fps->authenticated_user_id,
1376  0);
1377 
1378  /*
1379  * Set the client encoding to the database encoding, since that is what
1380  * the leader will expect.
1381  */
1383 
1384  /*
1385  * Load libraries that were loaded by original backend. We want to do
1386  * this before restoring GUCs, because the libraries might define custom
1387  * variables.
1388  */
1389  libraryspace = shm_toc_lookup(toc, PARALLEL_KEY_LIBRARY, false);
1391  RestoreLibraryState(libraryspace);
1392 
1393  /* Restore GUC values from launching backend. */
1394  gucspace = shm_toc_lookup(toc, PARALLEL_KEY_GUC, false);
1395  RestoreGUCState(gucspace);
1397 
1398  /* Crank up a transaction state appropriate to a parallel worker. */
1399  tstatespace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_STATE, false);
1400  StartParallelWorkerTransaction(tstatespace);
1401 
1402  /* Restore combo CID state. */
1403  combocidspace = shm_toc_lookup(toc, PARALLEL_KEY_COMBO_CID, false);
1404  RestoreComboCIDState(combocidspace);
1405 
1406  /* Attach to the per-session DSM segment and contained objects. */
1407  session_dsm_handle_space =
1409  AttachSession(*(dsm_handle *) session_dsm_handle_space);
1410 
1411  /* Restore transaction snapshot. */
1412  tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, false);
1414  fps->parallel_leader_pgproc);
1415 
1416  /* Restore active snapshot. */
1417  asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, false);
1418  PushActiveSnapshot(RestoreSnapshot(asnapspace));
1419 
1420  /*
1421  * We've changed which tuples we can see, and must therefore invalidate
1422  * system caches.
1423  */
1425 
1426  /*
1427  * Restore current role id. Skip verifying whether session user is
1428  * allowed to become this role and blindly restore the leader's state for
1429  * current role.
1430  */
1432 
1433  /* Restore user ID and security context. */
1435 
1436  /* Restore temp-namespace state to ensure search path matches leader's. */
1439 
1440  /* Restore pending syncs. */
1441  pendingsyncsspace = shm_toc_lookup(toc, PARALLEL_KEY_PENDING_SYNCS,
1442  false);
1443  RestorePendingSyncs(pendingsyncsspace);
1444 
1445  /* Restore reindex state. */
1446  reindexspace = shm_toc_lookup(toc, PARALLEL_KEY_REINDEX_STATE, false);
1447  RestoreReindexState(reindexspace);
1448 
1449  /* Restore relmapper state. */
1450  relmapperspace = shm_toc_lookup(toc, PARALLEL_KEY_RELMAPPER_STATE, false);
1451  RestoreRelationMap(relmapperspace);
1452 
1453  /* Restore uncommitted enums. */
1454  uncommittedenumsspace = shm_toc_lookup(toc, PARALLEL_KEY_UNCOMMITTEDENUMS,
1455  false);
1456  RestoreUncommittedEnums(uncommittedenumsspace);
1457 
1458  /* Attach to the leader's serializable transaction, if SERIALIZABLE. */
1460 
1461  /*
1462  * We've initialized all of our state now; nothing should change
1463  * hereafter.
1464  */
1467 
1468  /*
1469  * Time to do the real work: invoke the caller-supplied code.
1470  */
1471  entrypt(seg, toc);
1472 
1473  /* Must exit parallel mode to pop active snapshot. */
1474  ExitParallelMode();
1475 
1476  /* Must pop active snapshot so snapmgr.c doesn't complain. */
1478 
1479  /* Shut down the parallel-worker transaction. */
1481 
1482  /* Detach from the per-session DSM segment. */
1483  DetachSession();
1484 
1485  /* Report success. */
1486  pq_putmessage('X', NULL, 0);
1487 }
static pid_t ParallelLeaderPid
Definition: parallel.c:127
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
#define DatumGetUInt32(X)
Definition: postgres.h:486
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:55
#define AllocSetContextCreate
Definition: memutils.h:170
int MyProcPid
Definition: globals.c:41
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2157
uint32 dsm_handle
Definition: dsm_impl.h:55
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:588
PGPROC * parallel_leader_pgproc
Definition: parallel.c:92
void AttachSerializableXact(SerializableXactHandle handle)
Definition: predicate.c:5195
PGPROC * MyProc
Definition: proc.c:68
void CommitTransactionCommand(void)
Definition: xact.c:2948
void EndParallelWorkerTransaction(void)
Definition: xact.c:5332
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:631
Oid authenticated_user_id
Definition: parallel.c:85
int errcode(int sqlerrcode)
Definition: elog.c:704
Oid temp_toast_namespace_id
Definition: parallel.c:89
void DetachSession(void)
Definition: session.c:201
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:194
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1947
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
#define PARALLEL_KEY_RELMAPPER_STATE
Definition: parallel.c:77
void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
Definition: snapmgr.c:2222
void InvalidateSystemCaches(void)
Definition: inval.c:649
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
void RestoreComboCIDState(char *comboCIDstate)
Definition: combocid.c:342
signed int int32
Definition: c.h:417
void RestorePendingSyncs(char *startAddress)
Definition: storage.c:575
SerializableXactHandle serializable_xact_handle
Definition: parallel.c:97
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
#define ERROR
Definition: elog.h:45
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition: parallel.c:70
int32 MyCancelKey
Definition: globals.c:45
#define PARALLEL_KEY_UNCOMMITTEDENUMS
Definition: parallel.c:78
void ExitParallelMode(void)
Definition: xact.c:992
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define PARALLEL_KEY_FIXED
Definition: parallel.c:65
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:66
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
Definition: namespace.c:3328
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
#define PARALLEL_KEY_GUC
Definition: parallel.c:68
void RestoreUncommittedEnums(void *space)
Definition: pg_enum.c:741
int SetClientEncoding(int encoding)
Definition: mbutils.c:208
int ParallelWorkerNumber
Definition: parallel.c:112
void AttachSession(dsm_handle handle)
Definition: session.c:155
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void ParallelWorkerShutdown(int code, Datum arg)
Definition: parallel.c:1512
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:217
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static parallel_worker_main_type LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
Definition: parallel.c:1538
void RestoreLibraryState(char *start_address)
Definition: dfmgr.c:748
uintptr_t Datum
Definition: postgres.h:367
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition: shm_toc.c:64
int GetDatabaseEncoding(void)
Definition: mbutils.c:1151
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition: postmaster.c:5702
#define PARALLEL_MAGIC
Definition: parallel.c:58
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition: pqmq.c:55
#define ereport(elevel,...)
Definition: elog.h:155
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimestampTz xact_ts
Definition: parallel.c:95
BackendId parallel_leader_backend_id
Definition: parallel.c:94
BackendId ParallelLeaderBackendId
Definition: globals.c:84
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1054
#define Assert(condition)
Definition: c.h:792
void StartParallelWorkerTransaction(char *tstatespace)
Definition: xact.c:5307
void StartTransactionCommand(void)
Definition: xact.c:2847
#define PARALLEL_KEY_REINDEX_STATE
Definition: parallel.c:76
void EnterParallelMode(void)
Definition: xact.c:979
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:283
void SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts)
Definition: xact.c:787
#define PARALLEL_KEY_TRANSACTION_STATE
Definition: parallel.c:72
void RestoreReindexState(void *reindexstate)
Definition: index.c:4151
int errmsg(const char *fmt,...)
Definition: elog.c:915
void(* parallel_worker_main_type)(dsm_segment *seg, shm_toc *toc)
Definition: parallel.h:23
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
static FixedParallelState * MyFixedParallelState
Definition: parallel.c:121
void SetCurrentRoleId(Oid roleid, bool is_superuser)
Definition: miscinit.c:856
bool InitializingParallelWorker
Definition: parallel.c:118
TimestampTz stmt_ts
Definition: parallel.c:96
Definition: shm_mq.c:71
void RestoreRelationMap(char *startAddress)
Definition: relmapper.c:674
#define PARALLEL_KEY_SESSION_DSM
Definition: parallel.c:74
void pq_set_parallel_leader(pid_t pid, BackendId backend_id)
Definition: pqmq.c:80
pid_t parallel_leader_pid
Definition: parallel.c:93
#define pq_putmessage(msgtype, s, len)
Definition: libpq.h:42
#define PARALLEL_KEY_PENDING_SYNCS
Definition: parallel.c:75
#define PARALLEL_KEY_ENTRYPOINT
Definition: parallel.c:73
#define PARALLEL_KEY_COMBO_CID
Definition: parallel.c:69
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition: parallel.c:71
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define die(msg)
Definition: pg_test_fsync.c:97
#define PARALLEL_KEY_LIBRARY
Definition: parallel.c:67
void RestoreGUCState(void *gucstate)
Definition: guc.c:10620
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5731

◆ ParallelWorkerReportLastRecEnd()

void ParallelWorkerReportLastRecEnd ( XLogRecPtr  last_xlog_end)

Definition at line 1494 of file parallel.c.

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

Referenced by CommitTransaction().

1495 {
1497 
1498  Assert(fps != NULL);
1499  SpinLockAcquire(&fps->mutex);
1500  if (fps->last_xlog_end < last_xlog_end)
1501  fps->last_xlog_end = last_xlog_end;
1502  SpinLockRelease(&fps->mutex);
1503 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr last_xlog_end
Definition: parallel.c:103
#define SpinLockRelease(lock)
Definition: spin.h:64
#define Assert(condition)
Definition: c.h:792
static FixedParallelState * MyFixedParallelState
Definition: parallel.c:121

◆ ReinitializeParallelDSM()

void ReinitializeParallelDSM ( ParallelContext pcxt)

Definition at line 464 of file parallel.c.

References ParallelWorkerInfo::error_mqh, i, ParallelContext::known_attached_workers, FixedParallelState::last_xlog_end, MyProc, ParallelContext::nknown_attached_workers, ParallelContext::nworkers, ParallelContext::nworkers_launched, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, pfree(), 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(), and lazy_parallel_vacuum_indexes().

465 {
466  FixedParallelState *fps;
467 
468  /* Wait for any old workers to exit. */
469  if (pcxt->nworkers_launched > 0)
470  {
473  pcxt->nworkers_launched = 0;
474  if (pcxt->known_attached_workers)
475  {
477  pcxt->known_attached_workers = NULL;
478  pcxt->nknown_attached_workers = 0;
479  }
480  }
481 
482  /* Reset a few bits of fixed parallel state to a clean state. */
483  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
484  fps->last_xlog_end = 0;
485 
486  /* Recreate error queues (if they exist). */
487  if (pcxt->nworkers > 0)
488  {
489  char *error_queue_space;
490  int i;
491 
492  error_queue_space =
494  for (i = 0; i < pcxt->nworkers; ++i)
495  {
496  char *start;
497  shm_mq *mq;
498 
499  start = error_queue_space + i * PARALLEL_ERROR_QUEUE_SIZE;
500  mq = shm_mq_create(start, PARALLEL_ERROR_QUEUE_SIZE);
502  pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
503  }
504  }
505 }
#define PARALLEL_ERROR_QUEUE_SIZE
Definition: parallel.c:55
PGPROC * MyProc
Definition: proc.c:68
dsm_segment * seg
Definition: parallel.h:43
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition: parallel.c:865
int nknown_attached_workers
Definition: parallel.h:47
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition: parallel.c:751
ParallelWorkerInfo * worker
Definition: parallel.h:46
void pfree(void *pointer)
Definition: mcxt.c:1057
shm_mq * shm_mq_create(void *address, Size size)
Definition: shm_mq.c:170
#define PARALLEL_KEY_FIXED
Definition: parallel.c:65
#define PARALLEL_KEY_ERROR_QUEUE
Definition: parallel.c:66
shm_mq_handle * error_mqh
Definition: parallel.h:28
int nworkers_launched
Definition: parallel.h:38
XLogRecPtr last_xlog_end
Definition: parallel.c:103
bool * known_attached_workers
Definition: parallel.h:48
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:283
int i
Definition: shm_mq.c:71
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:199
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
shm_toc * toc
Definition: parallel.h:45

◆ ReinitializeParallelWorkers()

void ReinitializeParallelWorkers ( ParallelContext pcxt,
int  nworkers_to_launch 
)

Definition at line 514 of file parallel.c.

References Assert, ParallelContext::nworkers, and ParallelContext::nworkers_to_launch.

Referenced by lazy_parallel_vacuum_indexes().

515 {
516  /*
517  * The number of workers that need to be launched must be less than the
518  * number of workers with which the parallel context is initialized.
519  */
520  Assert(pcxt->nworkers >= nworkers_to_launch);
521  pcxt->nworkers_to_launch = nworkers_to_launch;
522 }
int nworkers_to_launch
Definition: parallel.h:37
#define Assert(condition)
Definition: c.h:792

◆ WaitForParallelWorkersToAttach()

void WaitForParallelWorkersToAttach ( ParallelContext pcxt)

Definition at line 648 of file parallel.c.

References Assert, BGWH_STARTED, BGWH_STOPPED, ParallelWorkerInfo::bgwhandle, CHECK_FOR_INTERRUPTS, ereport, errcode(), errhint(), errmsg(), ERROR, ParallelWorkerInfo::error_mqh, GetBackgroundWorkerPid(), i, ParallelContext::known_attached_workers, MyLatch, ParallelContext::nknown_attached_workers, ParallelContext::nworkers_launched, ResetLatch(), shm_mq_get_queue(), shm_mq_get_sender(), status(), WAIT_EVENT_BGWORKER_STARTUP, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and ParallelContext::worker.

Referenced by _bt_begin_parallel().

649 {
650  int i;
651 
652  /* Skip this if we have no launched workers. */
653  if (pcxt->nworkers_launched == 0)
654  return;
655 
656  for (;;)
657  {
658  /*
659  * This will process any parallel messages that are pending and it may
660  * also throw an error propagated from a worker.
661  */
663 
664  for (i = 0; i < pcxt->nworkers_launched; ++i)
665  {
667  shm_mq *mq;
668  int rc;
669  pid_t pid;
670 
671  if (pcxt->known_attached_workers[i])
672  continue;
673 
674  /*
675  * If error_mqh is NULL, then the worker has already exited
676  * cleanly.
677  */
678  if (pcxt->worker[i].error_mqh == NULL)
679  {
680  pcxt->known_attached_workers[i] = true;
681  ++pcxt->nknown_attached_workers;
682  continue;
683  }
684 
685  status = GetBackgroundWorkerPid(pcxt->worker[i].bgwhandle, &pid);
686  if (status == BGWH_STARTED)
687  {
688  /* Has the worker attached to the error queue? */
689  mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
690  if (shm_mq_get_sender(mq) != NULL)
691  {
692  /* Yes, so it is known to be attached. */
693  pcxt->known_attached_workers[i] = true;
694  ++pcxt->nknown_attached_workers;
695  }
696  }
697  else if (status == BGWH_STOPPED)
698  {
699  /*
700  * If the worker stopped without attaching to the error queue,
701  * throw an error.
702  */
703  mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
704  if (shm_mq_get_sender(mq) == NULL)
705  ereport(ERROR,
706  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
707  errmsg("parallel worker failed to initialize"),
708  errhint("More details may be available in the server log.")));
709 
710  pcxt->known_attached_workers[i] = true;
711  ++pcxt->nknown_attached_workers;
712  }
713  else
714  {
715  /*
716  * Worker not yet started, so we must wait. The postmaster
717  * will notify us if the worker's state changes. Our latch
718  * might also get set for some other reason, but if so we'll
719  * just end up waiting for the same worker again.
720  */
721  rc = WaitLatch(MyLatch,
724 
725  if (rc & WL_LATCH_SET)
727  }
728  }
729 
730  /* If all workers are known to have started, we're done. */
731  if (pcxt->nknown_attached_workers >= pcxt->nworkers_launched)
732  {
734  break;
735  }
736  }
737 }
int errhint(const char *fmt,...)
Definition: elog.c:1162
PGPROC * shm_mq_get_sender(shm_mq *mq)
Definition: shm_mq.c:250
int errcode(int sqlerrcode)
Definition: elog.c:704
int nknown_attached_workers
Definition: parallel.h:47
void ResetLatch(Latch *latch)
Definition: latch.c:588
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
ParallelWorkerInfo * worker
Definition: parallel.h:46
#define ERROR
Definition: elog.h:45
shm_mq_handle * error_mqh
Definition: parallel.h:28
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:38
BgwHandleStatus
Definition: bgworker.h:102
bool * known_attached_workers
Definition: parallel.h:48
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:792
shm_mq * shm_mq_get_queue(shm_mq_handle *mqh)
Definition: shm_mq.c:872
int errmsg(const char *fmt,...)
Definition: elog.c:915
int i
Definition: shm_mq.c:71
struct Latch * MyLatch
Definition: globals.c:55
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define WL_LATCH_SET
Definition: latch.h:124
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1089
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

◆ WaitForParallelWorkersToFinish()

void WaitForParallelWorkersToFinish ( ParallelContext pcxt)

Definition at line 751 of file parallel.c.

References Assert, BGWH_STOPPED, ParallelWorkerInfo::bgwhandle, CHECK_FOR_INTERRUPTS, ereport, errcode(), errhint(), errmsg(), ERROR, ParallelWorkerInfo::error_mqh, GetBackgroundWorkerPid(), i, ParallelContext::known_attached_workers, FixedParallelState::last_xlog_end, MyLatch, ParallelContext::nworkers_launched, PARALLEL_KEY_FIXED, ResetLatch(), shm_mq_get_queue(), shm_mq_get_sender(), shm_toc_lookup(), ParallelContext::toc, WAIT_EVENT_PARALLEL_FINISH, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, ParallelContext::worker, and XactLastRecEnd.

Referenced by _bt_end_parallel(), ExecParallelFinish(), lazy_parallel_vacuum_indexes(), and ReinitializeParallelDSM().

752 {
753  for (;;)
754  {
755  bool anyone_alive = false;
756  int nfinished = 0;
757  int i;
758 
759  /*
760  * This will process any parallel messages that are pending, which may
761  * change the outcome of the loop that follows. It may also throw an
762  * error propagated from a worker.
763  */
765 
766  for (i = 0; i < pcxt->nworkers_launched; ++i)
767  {
768  /*
769  * If error_mqh is NULL, then the worker has already exited
770  * cleanly. If we have received a message through error_mqh from
771  * the worker, we know it started up cleanly, and therefore we're
772  * certain to be notified when it exits.
773  */
774  if (pcxt->worker[i].error_mqh == NULL)
775  ++nfinished;
776  else if (pcxt->known_attached_workers[i])
777  {
778  anyone_alive = true;
779  break;
780  }
781  }
782 
783  if (!anyone_alive)
784  {
785  /* If all workers are known to have finished, we're done. */
786  if (nfinished >= pcxt->nworkers_launched)
787  {
788  Assert(nfinished == pcxt->nworkers_launched);
789  break;
790  }
791 
792  /*
793  * We didn't detect any living workers, but not all workers are
794  * known to have exited cleanly. Either not all workers have
795  * launched yet, or maybe some of them failed to start or
796  * terminated abnormally.
797  */
798  for (i = 0; i < pcxt->nworkers_launched; ++i)
799  {
800  pid_t pid;
801  shm_mq *mq;
802 
803  /*
804  * If the worker is BGWH_NOT_YET_STARTED or BGWH_STARTED, we
805  * should just keep waiting. If it is BGWH_STOPPED, then
806  * further investigation is needed.
807  */
808  if (pcxt->worker[i].error_mqh == NULL ||
809  pcxt->worker[i].bgwhandle == NULL ||
811  &pid) != BGWH_STOPPED)
812  continue;
813 
814  /*
815  * Check whether the worker ended up stopped without ever
816  * attaching to the error queue. If so, the postmaster was
817  * unable to fork the worker or it exited without initializing
818  * properly. We must throw an error, since the caller may
819  * have been expecting the worker to do some work before
820  * exiting.
821  */
822  mq = shm_mq_get_queue(pcxt->worker[i].error_mqh);
823  if (shm_mq_get_sender(mq) == NULL)
824  ereport(ERROR,
825  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
826  errmsg("parallel worker failed to initialize"),
827  errhint("More details may be available in the server log.")));
828 
829  /*
830  * The worker is stopped, but is attached to the error queue.
831  * Unless there's a bug somewhere, this will only happen when
832  * the worker writes messages and terminates after the
833  * CHECK_FOR_INTERRUPTS() near the top of this function and
834  * before the call to GetBackgroundWorkerPid(). In that case,
835  * or latch should have been set as well and the right things
836  * will happen on the next pass through the loop.
837  */
838  }
839  }
840 
844  }
845 
846  if (pcxt->toc != NULL)
847  {
848  FixedParallelState *fps;
849 
850  fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
851  if (fps->last_xlog_end > XactLastRecEnd)
853  }
854 }
int errhint(const char *fmt,...)
Definition: elog.c:1162
XLogRecPtr XactLastRecEnd
Definition: xlog.c:362
PGPROC * shm_mq_get_sender(shm_mq *mq)
Definition: shm_mq.c:250
int errcode(int sqlerrcode)
Definition: elog.c:704
void ResetLatch(Latch *latch)
Definition: latch.c:588
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
ParallelWorkerInfo * worker
Definition: parallel.h:46
#define ERROR
Definition: elog.h:45
#define PARALLEL_KEY_FIXED
Definition: parallel.c:65
shm_mq_handle * error_mqh
Definition: parallel.h:28
BackgroundWorkerHandle * bgwhandle
Definition: parallel.h:27
int nworkers_launched
Definition: parallel.h:38
XLogRecPtr last_xlog_end
Definition: parallel.c:103
bool * known_attached_workers
Definition: parallel.h:48
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:792
shm_mq * shm_mq_get_queue(shm_mq_handle *mqh)
Definition: shm_mq.c:872
int errmsg(const char *fmt,...)
Definition: elog.c:915
int i
Definition: shm_mq.c:71
struct Latch * MyLatch
Definition: globals.c:55
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
#define WL_LATCH_SET
Definition: latch.h:124
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1089
shm_toc * toc
Definition: parallel.h:45
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

Variable Documentation

◆ InitializingParallelWorker

PGDLLIMPORT bool InitializingParallelWorker

◆ ParallelMessagePending

volatile bool ParallelMessagePending

◆ ParallelWorkerNumber