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

Definition at line 37 of file parallel.h.

◆ 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 1229 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().

1235 {
1236  int worker;
1237  char buf[256];
1238 
1239  /* Get a worker, waiting if none are idle */
1240  while ((worker = GetIdleWorker(pstate)) == NO_SLOT)
1241  WaitForWorkers(AH, pstate, WFW_ONE_IDLE);
1242 
1243  /* Construct and send command string */
1244  buildWorkerCommand(AH, te, act, buf, sizeof(buf));
1245 
1246  sendMessageToWorker(pstate, worker, buf);
1247 
1248  /* Remember worker is busy, and which TocEntry it's working on */
1249  pstate->parallelSlot[worker].workerStatus = WRKR_WORKING;
1250  pstate->parallelSlot[worker].callback = callback;
1251  pstate->parallelSlot[worker].callback_data = callback_data;
1252  pstate->te[worker] = te;
1253 }
static void buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act, char *buf, int buflen)
Definition: parallel.c:1130
void * callback_data
Definition: parallel.c:97
static int GetIdleWorker(ParallelState *pstate)
Definition: parallel.c:1260
#define NO_SLOT
Definition: parallel.c:74
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:1670
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition: parallel.c:1477
TocEntry ** te
Definition: parallel.h:44
ParallelCompletionPtr callback
Definition: parallel.c:96
T_WorkerStatus workerStatus
Definition: parallel.c:93

◆ init_parallel_dump_utils()

void init_parallel_dump_utils ( void  )

Definition at line 251 of file parallel.c.

References _, exit_nicely, on_exit_nicely(), and progname.

Referenced by main().

252 {
253 #ifdef WIN32
254  if (!parallel_init_done)
255  {
256  WSADATA wsaData;
257  int err;
258 
259  /* Prepare for threaded operation */
260  tls_index = TlsAlloc();
261  mainThreadId = GetCurrentThreadId();
262 
263  /* Initialize socket access */
264  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
265  if (err != 0)
266  {
267  fprintf(stderr, _("%s: WSAStartup failed: %d\n"), progname, err);
268  exit_nicely(1);
269  }
270  /* ... and arrange to shut it down at exit */
271  on_exit_nicely(shutdown_parallel_dump_utils, NULL);
272  parallel_init_done = true;
273  }
274 #endif
275 }
void on_exit_nicely(on_exit_nicely_callback function, void *arg)
const char * progname
Definition: pg_standby.c:37
#define exit_nicely(code)
Definition: pg_dumpall.c:92
#define _(x)
Definition: elog.c:84

◆ IsEveryWorkerIdle()

bool IsEveryWorkerIdle ( ParallelState pstate)

Definition at line 1292 of file parallel.c.

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

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

1293 {
1294  int i;
1295 
1296  for (i = 0; i < pstate->numWorkers; i++)
1297  {
1298  if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE)
1299  return false;
1300  }
1301  return true;
1302 }
ParallelSlot * parallelSlot
Definition: parallel.h:45
T_WorkerStatus workerStatus
Definition: parallel.c:93
int i
int numWorkers
Definition: parallel.h:42

◆ ParallelBackupEnd()

void ParallelBackupEnd ( ArchiveHandle AH,
ParallelState pstate 
)

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

1082 {
1083  int i;
1084 
1085  /* No work if non-parallel */
1086  if (pstate->numWorkers == 1)
1087  return;
1088 
1089  /* There should not be any unfinished jobs */
1090  Assert(IsEveryWorkerIdle(pstate));
1091 
1092  /* Close the sockets so that the workers know they can exit */
1093  for (i = 0; i < pstate->numWorkers; i++)
1094  {
1095  closesocket(pstate->parallelSlot[i].pipeRead);
1096  closesocket(pstate->parallelSlot[i].pipeWrite);
1097  }
1098 
1099  /* Wait for them to exit */
1100  WaitForTerminatingWorkers(pstate);
1101 
1102  /*
1103  * Unlink pstate from shutdown_info, so the exit handler will not try to
1104  * use it; and likewise unlink from signal_info.
1105  */
1106  shutdown_info.pstate = NULL;
1107  set_cancel_pstate(NULL);
1108 
1109  /* Release state (mere neatnik-ism, since we're about to terminate) */
1110  free(pstate->te);
1111  free(pstate->parallelSlot);
1112  free(pstate);
1113 }
int pipeRead
Definition: parallel.c:101
static void set_cancel_pstate(ParallelState *pstate)
Definition: parallel.c:803
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition: parallel.c:1292
#define closesocket
Definition: port.h:292
ParallelSlot * parallelSlot
Definition: parallel.h:45
ParallelState * pstate
Definition: parallel.c:146
#define free(a)
Definition: header.h:65
static ShutdownInformation shutdown_info
Definition: parallel.c:150
#define Assert(condition)
Definition: c.h:670
TocEntry ** te
Definition: parallel.h:44
int pipeWrite
Definition: parallel.c:102
int i
static void WaitForTerminatingWorkers(ParallelState *pstate)
Definition: parallel.c:463
int numWorkers
Definition: parallel.h:42

◆ ParallelBackupStart()

ParallelState* ParallelBackupStart ( ArchiveHandle AH)

Definition at line 911 of file parallel.c.

References ParallelSlot::AH, DumpSignalInformation::am_worker, Assert, ParallelSlot::callback, ParallelSlot::callback_data, closesocket, _archiveHandle::connection, exit_horribly(), getLocalPQExpBuffer, i, modulename, 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, strerror(), ParallelState::te, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by _CloseArchive(), and RestoreArchive().

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

◆ set_archive_cancel_info()

void set_archive_cancel_info ( ArchiveHandle AH,
PGconn conn 
)

Definition at line 744 of file parallel.c.

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

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

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

◆ WaitForWorkers()

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

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

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