PostgreSQL Source Code  git master
parallel.h File Reference
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  ParallelState
 

Typedefs

typedef void(* ParallelCompletionPtr) (ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)
 
typedef struct ParallelSlot ParallelSlot
 
typedef struct ParallelState ParallelState
 

Enumerations

enum  WFW_WaitOption { WFW_NO_WAIT, WFW_GOT_STATUS, WFW_ONE_IDLE, WFW_ALL_IDLE }
 

Functions

void init_parallel_dump_utils (void)
 
bool IsEveryWorkerIdle (ParallelState *pstate)
 
void WaitForWorkers (ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
 
ParallelStateParallelBackupStart (ArchiveHandle *AH)
 
void DispatchJobForTocEntry (ArchiveHandle *AH, ParallelState *pstate, TocEntry *te, T_Action act, ParallelCompletionPtr callback, void *callback_data)
 
void ParallelBackupEnd (ArchiveHandle *AH, ParallelState *pstate)
 
void set_archive_cancel_info (ArchiveHandle *AH, PGconn *conn)
 

Typedef Documentation

◆ ParallelCompletionPtr

typedef void(* ParallelCompletionPtr) (ArchiveHandle *AH, TocEntry *te, int status, void *callback_data)

Definition at line 22 of file parallel.h.

◆ ParallelSlot

typedef struct ParallelSlot ParallelSlot

Definition at line 37 of file parallel.h.

◆ ParallelState

typedef struct ParallelState ParallelState

Enumeration Type Documentation

◆ WFW_WaitOption

Enumerator
WFW_NO_WAIT 
WFW_GOT_STATUS 
WFW_ONE_IDLE 
WFW_ALL_IDLE 

Definition at line 28 of file parallel.h.

Function Documentation

◆ DispatchJobForTocEntry()

void DispatchJobForTocEntry ( ArchiveHandle AH,
ParallelState pstate,
TocEntry te,
T_Action  act,
ParallelCompletionPtr  callback,
void *  callback_data 
)

Definition at line 1218 of file parallel.c.

References buf, buildWorkerCommand(), ParallelSlot::callback, ParallelSlot::callback_data, GetIdleWorker(), NO_SLOT, ParallelState::parallelSlot, sendMessageToWorker(), ParallelState::te, WaitForWorkers(), WFW_ONE_IDLE, ParallelSlot::workerStatus, and WRKR_WORKING.

Referenced by restore_toc_entries_parallel(), and WriteDataChunks().

1224 {
1225  int worker;
1226  char buf[256];
1227 
1228  /* Get a worker, waiting if none are idle */
1229  while ((worker = GetIdleWorker(pstate)) == NO_SLOT)
1230  WaitForWorkers(AH, pstate, WFW_ONE_IDLE);
1231 
1232  /* Construct and send command string */
1233  buildWorkerCommand(AH, te, act, buf, sizeof(buf));
1234 
1235  sendMessageToWorker(pstate, worker, buf);
1236 
1237  /* Remember worker is busy, and which TocEntry it's working on */
1238  pstate->parallelSlot[worker].workerStatus = WRKR_WORKING;
1239  pstate->parallelSlot[worker].callback = callback;
1240  pstate->parallelSlot[worker].callback_data = callback_data;
1241  pstate->te[worker] = te;
1242 }
static void buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act, char *buf, int buflen)
Definition: parallel.c:1121
void * callback_data
Definition: parallel.c:96
static int GetIdleWorker(ParallelState *pstate)
Definition: parallel.c:1249
#define NO_SLOT
Definition: parallel.c:73
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
static char * buf
Definition: pg_test_fsync.c:67
ParallelSlot * parallelSlot
Definition: parallel.h:45
static void sendMessageToWorker(ParallelState *pstate, int worker, const char *str)
Definition: parallel.c:1655
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition: parallel.c:1464
TocEntry ** te
Definition: parallel.h:44
ParallelCompletionPtr callback
Definition: parallel.c:95
T_WorkerStatus workerStatus
Definition: parallel.c:92

◆ init_parallel_dump_utils()

void init_parallel_dump_utils ( void  )

Definition at line 246 of file parallel.c.

References exit_nicely, on_exit_nicely(), and pg_log_error.

Referenced by main().

247 {
248 #ifdef WIN32
249  if (!parallel_init_done)
250  {
251  WSADATA wsaData;
252  int err;
253 
254  /* Prepare for threaded operation */
255  tls_index = TlsAlloc();
256  mainThreadId = GetCurrentThreadId();
257 
258  /* Initialize socket access */
259  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
260  if (err != 0)
261  {
262  pg_log_error("WSAStartup failed: %d", err);
263  exit_nicely(1);
264  }
265  /* ... and arrange to shut it down at exit */
266  on_exit_nicely(shutdown_parallel_dump_utils, NULL);
267  parallel_init_done = true;
268  }
269 #endif
270 }
void on_exit_nicely(on_exit_nicely_callback function, void *arg)
#define pg_log_error(...)
Definition: logging.h:79
#define exit_nicely(code)
Definition: pg_dumpall.c:95

◆ IsEveryWorkerIdle()

bool IsEveryWorkerIdle ( ParallelState pstate)

Definition at line 1281 of file parallel.c.

References i, ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by ParallelBackupEnd(), restore_toc_entries_parallel(), and WaitForWorkers().

1282 {
1283  int i;
1284 
1285  for (i = 0; i < pstate->numWorkers; i++)
1286  {
1287  if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE)
1288  return false;
1289  }
1290  return true;
1291 }
ParallelSlot * parallelSlot
Definition: parallel.h:45
T_WorkerStatus workerStatus
Definition: parallel.c:92
int i
int numWorkers
Definition: parallel.h:42

◆ ParallelBackupEnd()

void ParallelBackupEnd ( ArchiveHandle AH,
ParallelState pstate 
)

Definition at line 1072 of file parallel.c.

References Assert, closesocket, free, i, IsEveryWorkerIdle(), ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::pipeRead, ParallelSlot::pipeWrite, ShutdownInformation::pstate, set_cancel_pstate(), ParallelState::te, and WaitForTerminatingWorkers().

Referenced by _CloseArchive(), and RestoreArchive().

1073 {
1074  int i;
1075 
1076  /* No work if non-parallel */
1077  if (pstate->numWorkers == 1)
1078  return;
1079 
1080  /* There should not be any unfinished jobs */
1081  Assert(IsEveryWorkerIdle(pstate));
1082 
1083  /* Close the sockets so that the workers know they can exit */
1084  for (i = 0; i < pstate->numWorkers; i++)
1085  {
1086  closesocket(pstate->parallelSlot[i].pipeRead);
1087  closesocket(pstate->parallelSlot[i].pipeWrite);
1088  }
1089 
1090  /* Wait for them to exit */
1091  WaitForTerminatingWorkers(pstate);
1092 
1093  /*
1094  * Unlink pstate from shutdown_info, so the exit handler will not try to
1095  * use it; and likewise unlink from signal_info.
1096  */
1097  shutdown_info.pstate = NULL;
1098  set_cancel_pstate(NULL);
1099 
1100  /* Release state (mere neatnik-ism, since we're about to terminate) */
1101  free(pstate->te);
1102  free(pstate->parallelSlot);
1103  free(pstate);
1104 }
int pipeRead
Definition: parallel.c:100
static void set_cancel_pstate(ParallelState *pstate)
Definition: parallel.c:798
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition: parallel.c:1281
#define closesocket
Definition: port.h:312
ParallelSlot * parallelSlot
Definition: parallel.h:45
ParallelState * pstate
Definition: parallel.c:145
#define free(a)
Definition: header.h:65
static ShutdownInformation shutdown_info
Definition: parallel.c:149
#define Assert(condition)
Definition: c.h:739
TocEntry ** te
Definition: parallel.h:44
int pipeWrite
Definition: parallel.c:101
int i
static void WaitForTerminatingWorkers(ParallelState *pstate)
Definition: parallel.c:458
int numWorkers
Definition: parallel.h:42

◆ ParallelBackupStart()

ParallelState* ParallelBackupStart ( ArchiveHandle AH)

Definition at line 906 of file parallel.c.

References ParallelSlot::AH, DumpSignalInformation::am_worker, Assert, ParallelSlot::callback, ParallelSlot::callback_data, closesocket, _archiveHandle::connection, fatal, getLocalPQExpBuffer, i, ParallelState::numWorkers, Archive::numWorkers, ParallelState::parallelSlot, pg_malloc(), pg_malloc0(), pgpipe, ParallelSlot::pid, PIPE_READ, PIPE_WRITE, ParallelSlot::pipeRead, ParallelSlot::pipeRevRead, ParallelSlot::pipeRevWrite, ParallelSlot::pipeWrite, pqsignal(), ShutdownInformation::pstate, _archiveHandle::public, RunWorker(), set_archive_cancel_info(), set_cancel_pstate(), SIG_IGN, SIGPIPE, ParallelState::te, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by _CloseArchive(), and RestoreArchive().

907 {
908  ParallelState *pstate;
909  int i;
910 
911  Assert(AH->public.numWorkers > 0);
912 
913  pstate = (ParallelState *) pg_malloc(sizeof(ParallelState));
914 
915  pstate->numWorkers = AH->public.numWorkers;
916  pstate->te = NULL;
917  pstate->parallelSlot = NULL;
918 
919  if (AH->public.numWorkers == 1)
920  return pstate;
921 
922  pstate->te = (TocEntry **)
923  pg_malloc0(pstate->numWorkers * sizeof(TocEntry *));
924  pstate->parallelSlot = (ParallelSlot *)
925  pg_malloc0(pstate->numWorkers * sizeof(ParallelSlot));
926 
927 #ifdef WIN32
928  /* Make fmtId() and fmtQualifiedId() use thread-local storage */
929  getLocalPQExpBuffer = getThreadLocalPQExpBuffer;
930 #endif
931 
932  /*
933  * Set the pstate in shutdown_info, to tell the exit handler that it must
934  * clean up workers as well as the main database connection. But we don't
935  * set this in signal_info yet, because we don't want child processes to
936  * inherit non-NULL signal_info.pstate.
937  */
938  shutdown_info.pstate = pstate;
939 
940  /*
941  * Temporarily disable query cancellation on the master connection. This
942  * ensures that child processes won't inherit valid AH->connCancel
943  * settings and thus won't try to issue cancels against the master's
944  * connection. No harm is done if we fail while it's disabled, because
945  * the master connection is idle at this point anyway.
946  */
947  set_archive_cancel_info(AH, NULL);
948 
949  /* Ensure stdio state is quiesced before forking */
950  fflush(NULL);
951 
952  /* Create desired number of workers */
953  for (i = 0; i < pstate->numWorkers; i++)
954  {
955 #ifdef WIN32
956  WorkerInfo *wi;
957  uintptr_t handle;
958 #else
959  pid_t pid;
960 #endif
961  ParallelSlot *slot = &(pstate->parallelSlot[i]);
962  int pipeMW[2],
963  pipeWM[2];
964 
965  /* Create communication pipes for this worker */
966  if (pgpipe(pipeMW) < 0 || pgpipe(pipeWM) < 0)
967  fatal("could not create communication channels: %m");
968 
969  pstate->te[i] = NULL; /* just for safety */
970 
971  slot->workerStatus = WRKR_IDLE;
972  slot->AH = NULL;
973  slot->callback = NULL;
974  slot->callback_data = NULL;
975 
976  /* master's ends of the pipes */
977  slot->pipeRead = pipeWM[PIPE_READ];
978  slot->pipeWrite = pipeMW[PIPE_WRITE];
979  /* child's ends of the pipes */
980  slot->pipeRevRead = pipeMW[PIPE_READ];
981  slot->pipeRevWrite = pipeWM[PIPE_WRITE];
982 
983 #ifdef WIN32
984  /* Create transient structure to pass args to worker function */
985  wi = (WorkerInfo *) pg_malloc(sizeof(WorkerInfo));
986 
987  wi->AH = AH;
988  wi->slot = slot;
989 
990  handle = _beginthreadex(NULL, 0, (void *) &init_spawned_worker_win32,
991  wi, 0, &(slot->threadId));
992  slot->hThread = handle;
993 #else /* !WIN32 */
994  pid = fork();
995  if (pid == 0)
996  {
997  /* we are the worker */
998  int j;
999 
1000  /* this is needed for GetMyPSlot() */
1001  slot->pid = getpid();
1002 
1003  /* instruct signal handler that we're in a worker now */
1004  signal_info.am_worker = true;
1005 
1006  /* close read end of Worker -> Master */
1007  closesocket(pipeWM[PIPE_READ]);
1008  /* close write end of Master -> Worker */
1009  closesocket(pipeMW[PIPE_WRITE]);
1010 
1011  /*
1012  * Close all inherited fds for communication of the master with
1013  * previously-forked workers.
1014  */
1015  for (j = 0; j < i; j++)
1016  {
1017  closesocket(pstate->parallelSlot[j].pipeRead);
1018  closesocket(pstate->parallelSlot[j].pipeWrite);
1019  }
1020 
1021  /* Run the worker ... */
1022  RunWorker(AH, slot);
1023 
1024  /* We can just exit(0) when done */
1025  exit(0);
1026  }
1027  else if (pid < 0)
1028  {
1029  /* fork failed */
1030  fatal("could not create worker process: %m");
1031  }
1032 
1033  /* In Master after successful fork */
1034  slot->pid = pid;
1035 
1036  /* close read end of Master -> Worker */
1037  closesocket(pipeMW[PIPE_READ]);
1038  /* close write end of Worker -> Master */
1039  closesocket(pipeWM[PIPE_WRITE]);
1040 #endif /* WIN32 */
1041  }
1042 
1043  /*
1044  * Having forked off the workers, disable SIGPIPE so that master isn't
1045  * killed if it tries to send a command to a dead worker. We don't want
1046  * the workers to inherit this setting, though.
1047  */
1048 #ifndef WIN32
1050 #endif
1051 
1052  /*
1053  * Re-establish query cancellation on the master connection.
1054  */
1056 
1057  /*
1058  * Tell the cancel signal handler to forward signals to worker processes,
1059  * too. (As with query cancel, we did not need this earlier because the
1060  * workers have not yet been given anything to do; if we die before this
1061  * point, any already-started workers will see EOF and quit promptly.)
1062  */
1063  set_cancel_pstate(pstate);
1064 
1065  return pstate;
1066 }
int pipeRead
Definition: parallel.c:100
static void set_cancel_pstate(ParallelState *pstate)
Definition: parallel.c:798
ArchiveHandle * AH
Definition: parallel.c:98
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void * callback_data
Definition: parallel.c:96
#define closesocket
Definition: port.h:312
#define PIPE_READ
Definition: parallel.c:70
#define SIGPIPE
Definition: win32_port.h:159
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
pid_t pid
Definition: parallel.c:110
static void RunWorker(ArchiveHandle *AH, ParallelSlot *slot)
Definition: parallel.c:838
int pipeRevRead
Definition: parallel.c:102
#define pgpipe(a)
Definition: parallel.c:134
#define SIG_IGN
Definition: win32_port.h:151
ParallelSlot * parallelSlot
Definition: parallel.h:45
ParallelState * pstate
Definition: parallel.c:145
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static ShutdownInformation shutdown_info
Definition: parallel.c:149
#define Assert(condition)
Definition: c.h:739
TocEntry ** te
Definition: parallel.h:44
ParallelCompletionPtr callback
Definition: parallel.c:95
int pipeRevWrite
Definition: parallel.c:103
PQExpBuffer(* getLocalPQExpBuffer)(void)
Definition: string_utils.c:27
#define fatal(...)
int numWorkers
Definition: pg_backup.h:193
static volatile DumpSignalInformation signal_info
Definition: parallel.c:170
T_WorkerStatus workerStatus
Definition: parallel.c:92
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:739
int pipeWrite
Definition: parallel.c:101
int i
#define PIPE_WRITE
Definition: parallel.c:71
int numWorkers
Definition: parallel.h:42

◆ set_archive_cancel_info()

void set_archive_cancel_info ( ArchiveHandle AH,
PGconn conn 
)

Definition at line 739 of file parallel.c.

References ParallelSlot::AH, _archiveHandle::connCancel, DumpSignalInformation::myAH, PQfreeCancel(), PQgetCancel(), and setup_cancel_handler().

Referenced by ConnectDatabase(), DisconnectDatabase(), ParallelBackupStart(), and ReconnectToServer().

740 {
741  PGcancel *oldConnCancel;
742 
743  /*
744  * Activate the interrupt handler if we didn't yet in this process. On
745  * Windows, this also initializes signal_info_lock; therefore it's
746  * important that this happen at least once before we fork off any
747  * threads.
748  */
750 
751  /*
752  * On Unix, we assume that storing a pointer value is atomic with respect
753  * to any possible signal interrupt. On Windows, use a critical section.
754  */
755 
756 #ifdef WIN32
757  EnterCriticalSection(&signal_info_lock);
758 #endif
759 
760  /* Free the old one if we have one */
761  oldConnCancel = AH->connCancel;
762  /* be sure interrupt handler doesn't use pointer while freeing */
763  AH->connCancel = NULL;
764 
765  if (oldConnCancel != NULL)
766  PQfreeCancel(oldConnCancel);
767 
768  /* Set the new one if specified */
769  if (conn)
770  AH->connCancel = PQgetCancel(conn);
771 
772  /*
773  * On Unix, there's only ever one active ArchiveHandle per process, so we
774  * can just set signal_info.myAH unconditionally. On Windows, do that
775  * only in the main thread; worker threads have to make sure their
776  * ArchiveHandle appears in the pstate data, which is dealt with in
777  * RunWorker().
778  */
779 #ifndef WIN32
780  signal_info.myAH = AH;
781 #else
782  if (mainThreadId == GetCurrentThreadId())
783  signal_info.myAH = AH;
784 #endif
785 
786 #ifdef WIN32
787  LeaveCriticalSection(&signal_info_lock);
788 #endif
789 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4269
ArchiveHandle * myAH
Definition: parallel.c:162
static void setup_cancel_handler(void)
Definition: parallel.c:617
PGcancel *volatile connCancel
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:4246
static volatile DumpSignalInformation signal_info
Definition: parallel.c:170

◆ WaitForWorkers()

void WaitForWorkers ( ArchiveHandle AH,
ParallelState pstate,
WFW_WaitOption  mode 
)

Definition at line 1464 of file parallel.c.

References Assert, do_wait, GetIdleWorker(), IsEveryWorkerIdle(), ListenToWorkers(), NO_SLOT, WFW_ALL_IDLE, WFW_GOT_STATUS, WFW_NO_WAIT, and WFW_ONE_IDLE.

Referenced by DispatchJobForTocEntry(), restore_toc_entries_parallel(), and WriteDataChunks().

1465 {
1466  bool do_wait = false;
1467 
1468  /*
1469  * In GOT_STATUS mode, always block waiting for a message, since we can't
1470  * return till we get something. In other modes, we don't block the first
1471  * time through the loop.
1472  */
1473  if (mode == WFW_GOT_STATUS)
1474  {
1475  /* Assert that caller knows what it's doing */
1476  Assert(!IsEveryWorkerIdle(pstate));
1477  do_wait = true;
1478  }
1479 
1480  for (;;)
1481  {
1482  /*
1483  * Check for status messages, even if we don't need to block. We do
1484  * not try very hard to reap all available messages, though, since
1485  * there's unlikely to be more than one.
1486  */
1487  if (ListenToWorkers(AH, pstate, do_wait))
1488  {
1489  /*
1490  * If we got a message, we are done by definition for GOT_STATUS
1491  * mode, and we can also be certain that there's at least one idle
1492  * worker. So we're done in all but ALL_IDLE mode.
1493  */
1494  if (mode != WFW_ALL_IDLE)
1495  return;
1496  }
1497 
1498  /* Check whether we must wait for new status messages */
1499  switch (mode)
1500  {
1501  case WFW_NO_WAIT:
1502  return; /* never wait */
1503  case WFW_GOT_STATUS:
1504  Assert(false); /* can't get here, because we waited */
1505  break;
1506  case WFW_ONE_IDLE:
1507  if (GetIdleWorker(pstate) != NO_SLOT)
1508  return;
1509  break;
1510  case WFW_ALL_IDLE:
1511  if (IsEveryWorkerIdle(pstate))
1512  return;
1513  break;
1514  }
1515 
1516  /* Loop back, and this time wait for something to happen */
1517  do_wait = true;
1518  }
1519 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition: parallel.c:1281
static int GetIdleWorker(ParallelState *pstate)
Definition: parallel.c:1249
#define NO_SLOT
Definition: parallel.c:73
static bool ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait)
Definition: parallel.c:1411
#define Assert(condition)
Definition: c.h:739
static bool do_wait
Definition: pg_ctl.c:79