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 1224 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().

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

◆ 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 pg_log_error.

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  pg_log_error("WSAStartup failed: %d", 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)
#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 1287 of file parallel.c.

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

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

1288 {
1289  int i;
1290 
1291  for (i = 0; i < pstate->numWorkers; i++)
1292  {
1293  if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE)
1294  return false;
1295  }
1296  return true;
1297 }
ParallelSlot * parallelSlot
Definition: parallel.h:45
T_WorkerStatus workerStatus
Definition: parallel.c:97
int i
int numWorkers
Definition: parallel.h:42

◆ ParallelBackupEnd()

void ParallelBackupEnd ( ArchiveHandle AH,
ParallelState pstate 
)

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

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

◆ ParallelBackupStart()

ParallelState* ParallelBackupStart ( ArchiveHandle AH)

Definition at line 916 of file parallel.c.

References ParallelSlot::AH, DumpSignalInformation::am_worker, Assert, 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().

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

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

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

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

◆ WaitForWorkers()

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

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

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