PostgreSQL Source Code  git master
parallel_slot.c File Reference
Include dependency graph for parallel_slot.c:

Go to the source code of this file.

Macros

#define ERRCODE_UNDEFINED_TABLE   "42P01"
 

Functions

static int select_loop (int maxFd, fd_set *workerset)
 
static bool processQueryResult (ParallelSlot *slot, PGresult *result)
 
static bool consumeQueryResult (ParallelSlot *slot)
 
static int find_matching_idle_slot (const ParallelSlotArray *sa, const char *dbname)
 
static int find_unconnected_slot (const ParallelSlotArray *sa)
 
static int find_any_idle_slot (const ParallelSlotArray *sa)
 
static bool wait_on_slots (ParallelSlotArray *sa)
 
static void connect_slot (ParallelSlotArray *sa, int slotno, const char *dbname)
 
ParallelSlotParallelSlotsGetIdle (ParallelSlotArray *sa, const char *dbname)
 
ParallelSlotArrayParallelSlotsSetup (int numslots, ConnParams *cparams, const char *progname, bool echo, const char *initcmd)
 
void ParallelSlotsAdoptConn (ParallelSlotArray *sa, PGconn *conn)
 
void ParallelSlotsTerminate (ParallelSlotArray *sa)
 
bool ParallelSlotsWaitCompletion (ParallelSlotArray *sa)
 
bool TableCommandResultHandler (PGresult *res, PGconn *conn, void *context)
 

Macro Definition Documentation

◆ ERRCODE_UNDEFINED_TABLE

#define ERRCODE_UNDEFINED_TABLE   "42P01"

Definition at line 30 of file parallel_slot.c.

Referenced by TableCommandResultHandler().

Function Documentation

◆ connect_slot()

static void connect_slot ( ParallelSlotArray sa,
int  slotno,
const char *  dbname 
)
static

Definition at line 289 of file parallel_slot.c.

References connectDatabase(), ParallelSlot::connection, ParallelSlotArray::cparams, dbname, ParallelSlotArray::echo, executeCommand(), ParallelSlotArray::initcmd, _connParams::override_dbname, pg_log_fatal, PQsocket(), ParallelSlotArray::progname, and ParallelSlotArray::slots.

Referenced by ParallelSlotsGetIdle().

290 {
291  const char *old_override;
292  ParallelSlot *slot = &sa->slots[slotno];
293 
294  old_override = sa->cparams->override_dbname;
295  if (dbname)
297  slot->connection = connectDatabase(sa->cparams, sa->progname, sa->echo, false, true);
298  sa->cparams->override_dbname = old_override;
299 
300  if (PQsocket(slot->connection) >= FD_SETSIZE)
301  {
302  pg_log_fatal("too many jobs for this platform");
303  exit(1);
304  }
305 
306  /* Setup the connection using the supplied command, if any. */
307  if (sa->initcmd)
308  executeCommand(slot->connection, sa->initcmd, sa->echo);
309 }
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1897
const char * initcmd
Definition: parallel_slot.h:42
PGconn * connection
Definition: parallel_slot.h:23
char * override_dbname
Definition: pg_backup.h:72
static PGconn * connectDatabase(const char *dbname, const char *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
Definition: pg_dumpall.c:1636
char * dbname
Definition: streamutil.c:51
const char * progname
Definition: parallel_slot.h:40
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43
ConnParams * cparams
Definition: parallel_slot.h:39
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6753
#define pg_log_fatal(...)
Definition: logging.h:76

◆ consumeQueryResult()

static bool consumeQueryResult ( ParallelSlot slot)
static

Definition at line 60 of file parallel_slot.c.

References ParallelSlot::connection, PQgetResult(), processQueryResult(), ResetCancelConn(), and SetCancelConn().

Referenced by ParallelSlotsWaitCompletion().

61 {
62  bool ok = true;
63  PGresult *result;
64 
66  while ((result = PQgetResult(slot->connection)) != NULL)
67  {
68  if (!processQueryResult(slot, result))
69  ok = false;
70  }
72  return ok;
73 }
void ResetCancelConn(void)
Definition: cancel.c:100
void SetCancelConn(PGconn *conn)
Definition: cancel.c:70
PGconn * connection
Definition: parallel_slot.h:23
static bool processQueryResult(ParallelSlot *slot, PGresult *result)
Definition: parallel_slot.c:41
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1927

◆ find_any_idle_slot()

static int find_any_idle_slot ( const ParallelSlotArray sa)
static

Definition at line 181 of file parallel_slot.c.

References i, ParallelSlot::inUse, ParallelSlotArray::numslots, and ParallelSlotArray::slots.

Referenced by ParallelSlotsGetIdle().

182 {
183  int i;
184 
185  for (i = 0; i < sa->numslots; i++)
186  if (!sa->slots[i].inUse)
187  return i;
188 
189  return -1;
190 }
int i
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43

◆ find_matching_idle_slot()

static int find_matching_idle_slot ( const ParallelSlotArray sa,
const char *  dbname 
)
static

Definition at line 137 of file parallel_slot.c.

References ParallelSlot::connection, i, ParallelSlot::inUse, ParallelSlotArray::numslots, PQdb(), and ParallelSlotArray::slots.

Referenced by ParallelSlotsGetIdle().

138 {
139  int i;
140 
141  for (i = 0; i < sa->numslots; i++)
142  {
143  if (sa->slots[i].inUse)
144  continue;
145 
146  if (sa->slots[i].connection == NULL)
147  continue;
148 
149  if (dbname == NULL ||
150  strcmp(PQdb(sa->slots[i].connection), dbname) == 0)
151  return i;
152  }
153  return -1;
154 }
PGconn * connection
Definition: parallel_slot.h:23
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6581
char * dbname
Definition: streamutil.c:51
int i
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43

◆ find_unconnected_slot()

static int find_unconnected_slot ( const ParallelSlotArray sa)
static

Definition at line 161 of file parallel_slot.c.

References ParallelSlot::connection, i, ParallelSlot::inUse, ParallelSlotArray::numslots, and ParallelSlotArray::slots.

Referenced by ParallelSlotsAdoptConn(), and ParallelSlotsGetIdle().

162 {
163  int i;
164 
165  for (i = 0; i < sa->numslots; i++)
166  {
167  if (sa->slots[i].inUse)
168  continue;
169 
170  if (sa->slots[i].connection == NULL)
171  return i;
172  }
173 
174  return -1;
175 }
PGconn * connection
Definition: parallel_slot.h:23
int i
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43

◆ ParallelSlotsAdoptConn()

void ParallelSlotsAdoptConn ( ParallelSlotArray sa,
PGconn conn 
)

Definition at line 432 of file parallel_slot.c.

References conn, ParallelSlot::connection, disconnectDatabase(), find_unconnected_slot(), and ParallelSlotArray::slots.

Referenced by main(), ParallelSlotClearHandler(), reindex_one_database(), and vacuum_one_database().

433 {
434  int offset;
435 
436  offset = find_unconnected_slot(sa);
437  if (offset >= 0)
438  sa->slots[offset].connection = conn;
439  else
440  disconnectDatabase(conn);
441 }
static int find_unconnected_slot(const ParallelSlotArray *sa)
void disconnectDatabase(PGconn *conn)
PGconn * conn
Definition: streamutil.c:54
PGconn * connection
Definition: parallel_slot.h:23
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43

◆ ParallelSlotsGetIdle()

ParallelSlot* ParallelSlotsGetIdle ( ParallelSlotArray sa,
const char *  dbname 
)

Definition at line 343 of file parallel_slot.c.

References Assert, connect_slot(), ParallelSlot::connection, disconnectDatabase(), find_any_idle_slot(), find_matching_idle_slot(), find_unconnected_slot(), ParallelSlot::inUse, ParallelSlotArray::numslots, ParallelSlotArray::slots, and wait_on_slots().

Referenced by main(), ParallelSlotClearHandler(), reindex_one_database(), and vacuum_one_database().

344 {
345  int offset;
346 
347  Assert(sa);
348  Assert(sa->numslots > 0);
349 
350  while (1)
351  {
352  /* First choice: a slot already connected to the desired database. */
353  offset = find_matching_idle_slot(sa, dbname);
354  if (offset >= 0)
355  {
356  sa->slots[offset].inUse = true;
357  return &sa->slots[offset];
358  }
359 
360  /* Second choice: a slot not connected to any database. */
361  offset = find_unconnected_slot(sa);
362  if (offset >= 0)
363  {
364  connect_slot(sa, offset, dbname);
365  sa->slots[offset].inUse = true;
366  return &sa->slots[offset];
367  }
368 
369  /* Third choice: a slot connected to the wrong database. */
370  offset = find_any_idle_slot(sa);
371  if (offset >= 0)
372  {
373  disconnectDatabase(sa->slots[offset].connection);
374  sa->slots[offset].connection = NULL;
375  connect_slot(sa, offset, dbname);
376  sa->slots[offset].inUse = true;
377  return &sa->slots[offset];
378  }
379 
380  /*
381  * Fourth choice: block until one or more slots become available. If
382  * any slots hit a fatal error, we'll find out about that here and
383  * return NULL.
384  */
385  if (!wait_on_slots(sa))
386  return NULL;
387  }
388 }
static void connect_slot(ParallelSlotArray *sa, int slotno, const char *dbname)
static int find_unconnected_slot(const ParallelSlotArray *sa)
void disconnectDatabase(PGconn *conn)
PGconn * connection
Definition: parallel_slot.h:23
static int find_matching_idle_slot(const ParallelSlotArray *sa, const char *dbname)
#define Assert(condition)
Definition: c.h:804
char * dbname
Definition: streamutil.c:51
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43
static int find_any_idle_slot(const ParallelSlotArray *sa)
static bool wait_on_slots(ParallelSlotArray *sa)

◆ ParallelSlotsSetup()

ParallelSlotArray* ParallelSlotsSetup ( int  numslots,
ConnParams cparams,
const char *  progname,
bool  echo,
const char *  initcmd 
)

Definition at line 400 of file parallel_slot.c.

References Assert, ParallelSlotArray::cparams, ParallelSlotArray::echo, ParallelSlotArray::initcmd, ParallelSlotArray::numslots, offsetof, palloc0(), ParallelSlotArray::progname, and progname.

Referenced by main(), ParallelSlotClearHandler(), reindex_one_database(), and vacuum_one_database().

402 {
404 
405  Assert(numslots > 0);
406  Assert(cparams != NULL);
407  Assert(progname != NULL);
408 
410  numslots * sizeof(ParallelSlot));
411 
412  sa->numslots = numslots;
413  sa->cparams = cparams;
414  sa->progname = progname;
415  sa->echo = echo;
416  sa->initcmd = initcmd;
417 
418  return sa;
419 }
const char * progname
Definition: main.c:46
struct ParallelSlot ParallelSlot
Definition: parallel.h:37
const char * initcmd
Definition: parallel_slot.h:42
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Assert(condition)
Definition: c.h:804
const char * progname
Definition: parallel_slot.h:40
ConnParams * cparams
Definition: parallel_slot.h:39
#define offsetof(type, field)
Definition: c.h:727

◆ ParallelSlotsTerminate()

void ParallelSlotsTerminate ( ParallelSlotArray sa)

Definition at line 451 of file parallel_slot.c.

References conn, ParallelSlot::connection, disconnectDatabase(), i, ParallelSlotArray::numslots, and ParallelSlotArray::slots.

Referenced by main(), ParallelSlotClearHandler(), reindex_one_database(), and vacuum_one_database().

452 {
453  int i;
454 
455  for (i = 0; i < sa->numslots; i++)
456  {
457  PGconn *conn = sa->slots[i].connection;
458 
459  if (conn == NULL)
460  continue;
461 
462  disconnectDatabase(conn);
463  }
464 }
void disconnectDatabase(PGconn *conn)
PGconn * conn
Definition: streamutil.c:54
PGconn * connection
Definition: parallel_slot.h:23
int i
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43

◆ ParallelSlotsWaitCompletion()

bool ParallelSlotsWaitCompletion ( ParallelSlotArray sa)

Definition at line 473 of file parallel_slot.c.

References ParallelSlot::connection, consumeQueryResult(), i, ParallelSlotArray::numslots, and ParallelSlotArray::slots.

Referenced by main(), ParallelSlotClearHandler(), reindex_one_database(), and vacuum_one_database().

474 {
475  int i;
476 
477  for (i = 0; i < sa->numslots; i++)
478  {
479  if (sa->slots[i].connection == NULL)
480  continue;
481  if (!consumeQueryResult(&sa->slots[i]))
482  return false;
483  }
484 
485  return true;
486 }
PGconn * connection
Definition: parallel_slot.h:23
static bool consumeQueryResult(ParallelSlot *slot)
Definition: parallel_slot.c:60
int i
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43

◆ processQueryResult()

static bool processQueryResult ( ParallelSlot slot,
PGresult result 
)
static

Definition at line 41 of file parallel_slot.c.

References Assert, ParallelSlot::connection, ParallelSlot::handler, ParallelSlot::handler_context, and PQclear().

Referenced by consumeQueryResult(), and wait_on_slots().

42 {
43  Assert(slot->handler != NULL);
44 
45  /* On failure, the handler should return NULL after freeing the result */
46  if (!slot->handler(result, slot->connection, slot->handler_context))
47  return false;
48 
49  /* Ok, we have to free it ourself */
50  PQclear(result);
51  return true;
52 }
ParallelSlotResultHandler handler
Definition: parallel_slot.h:32
PGconn * connection
Definition: parallel_slot.h:23
void * handler_context
Definition: parallel_slot.h:33
void PQclear(PGresult *res)
Definition: fe-exec.c:680
#define Assert(condition)
Definition: c.h:804

◆ select_loop()

static int select_loop ( int  maxFd,
fd_set *  workerset 
)
static

Definition at line 82 of file parallel_slot.c.

References CancelRequested, EINTR, i, and select.

Referenced by wait_on_slots().

83 {
84  int i;
85  fd_set saveSet = *workerset;
86 
87  if (CancelRequested)
88  return -1;
89 
90  for (;;)
91  {
92  /*
93  * On Windows, we need to check once in a while for cancel requests;
94  * on other platforms we rely on select() returning when interrupted.
95  */
96  struct timeval *tvp;
97 #ifdef WIN32
98  struct timeval tv = {0, 1000000};
99 
100  tvp = &tv;
101 #else
102  tvp = NULL;
103 #endif
104 
105  *workerset = saveSet;
106  i = select(maxFd + 1, workerset, NULL, NULL, tvp);
107 
108 #ifdef WIN32
109  if (i == SOCKET_ERROR)
110  {
111  i = -1;
112 
113  if (WSAGetLastError() == WSAEINTR)
114  errno = EINTR;
115  }
116 #endif
117 
118  if (i < 0 && errno == EINTR)
119  continue; /* ignore this */
120  if (i < 0 || CancelRequested)
121  return -1; /* but not this */
122  if (i == 0)
123  continue; /* timeout (Win32 only) */
124  break;
125  }
126 
127  return i;
128 }
#define select(n, r, w, e, timeout)
Definition: win32_port.h:464
volatile sig_atomic_t CancelRequested
Definition: cancel.c:52
int i
#define EINTR
Definition: win32_port.h:343

◆ TableCommandResultHandler()

bool TableCommandResultHandler ( PGresult res,
PGconn conn,
void *  context 
)

Definition at line 509 of file parallel_slot.c.

References Assert, ERRCODE_UNDEFINED_TABLE, PG_DIAG_SQLSTATE, pg_log_error, PGRES_COMMAND_OK, PQclear(), PQdb(), PQerrorMessage(), PQresultErrorField(), and PQresultStatus().

Referenced by ParallelSlotClearHandler(), reindex_one_database(), and vacuum_one_database().

510 {
511  Assert(res != NULL);
512  Assert(conn != NULL);
513 
514  /*
515  * If it's an error, report it. Errors about a missing table are harmless
516  * so we continue processing; but die for other errors.
517  */
518  if (PQresultStatus(res) != PGRES_COMMAND_OK)
519  {
520  char *sqlState = PQresultErrorField(res, PG_DIAG_SQLSTATE);
521 
522  pg_log_error("processing of database \"%s\" failed: %s",
523  PQdb(conn), PQerrorMessage(conn));
524 
525  if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
526  {
527  PQclear(res);
528  return false;
529  }
530  }
531 
532  return true;
533 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6735
#define pg_log_error(...)
Definition: logging.h:80
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3097
#define ERRCODE_UNDEFINED_TABLE
Definition: parallel_slot.c:30
void PQclear(PGresult *res)
Definition: fe-exec.c:680
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6581
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3152
#define Assert(condition)
Definition: c.h:804

◆ wait_on_slots()

static bool wait_on_slots ( ParallelSlotArray sa)
static

Definition at line 198 of file parallel_slot.c.

References Assert, ParallelSlot::connection, i, ParallelSlot::inUse, ParallelSlotArray::numslots, ParallelSlotClearHandler(), PQconsumeInput(), PQgetResult(), PQisBusy(), PQsocket(), processQueryResult(), ResetCancelConn(), select_loop(), SetCancelConn(), and ParallelSlotArray::slots.

Referenced by ParallelSlotsGetIdle().

199 {
200  int i;
201  fd_set slotset;
202  int maxFd = 0;
203  PGconn *cancelconn = NULL;
204 
205  /* We must reconstruct the fd_set for each call to select_loop */
206  FD_ZERO(&slotset);
207 
208  for (i = 0; i < sa->numslots; i++)
209  {
210  int sock;
211 
212  /* We shouldn't get here if we still have slots without connections */
213  Assert(sa->slots[i].connection != NULL);
214 
215  sock = PQsocket(sa->slots[i].connection);
216 
217  /*
218  * We don't really expect any connections to lose their sockets after
219  * startup, but just in case, cope by ignoring them.
220  */
221  if (sock < 0)
222  continue;
223 
224  /* Keep track of the first valid connection we see. */
225  if (cancelconn == NULL)
226  cancelconn = sa->slots[i].connection;
227 
228  FD_SET(sock, &slotset);
229  if (sock > maxFd)
230  maxFd = sock;
231  }
232 
233  /*
234  * If we get this far with no valid connections, processing cannot
235  * continue.
236  */
237  if (cancelconn == NULL)
238  return false;
239 
241  i = select_loop(maxFd, &slotset);
242  ResetCancelConn();
243 
244  /* failure? */
245  if (i < 0)
246  return false;
247 
248  for (i = 0; i < sa->numslots; i++)
249  {
250  int sock;
251 
252  sock = PQsocket(sa->slots[i].connection);
253 
254  if (sock >= 0 && FD_ISSET(sock, &slotset))
255  {
256  /* select() says input is available, so consume it */
258  }
259 
260  /* Collect result(s) as long as any are available */
261  while (!PQisBusy(sa->slots[i].connection))
262  {
263  PGresult *result = PQgetResult(sa->slots[i].connection);
264 
265  if (result != NULL)
266  {
267  /* Handle and discard the command result */
268  if (!processQueryResult(&sa->slots[i], result))
269  return false;
270  }
271  else
272  {
273  /* This connection has become idle */
274  sa->slots[i].inUse = false;
276  break;
277  }
278  }
279  }
280  return true;
281 }
void ResetCancelConn(void)
Definition: cancel.c:100
static int select_loop(int maxFd, fd_set *workerset)
Definition: parallel_slot.c:82
void SetCancelConn(PGconn *conn)
Definition: cancel.c:70
PGconn * connection
Definition: parallel_slot.h:23
static bool processQueryResult(ParallelSlot *slot, PGresult *result)
Definition: parallel_slot.c:41
static void ParallelSlotClearHandler(ParallelSlot *slot)
Definition: parallel_slot.h:55
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1853
#define Assert(condition)
Definition: c.h:804
int PQisBusy(PGconn *conn)
Definition: fe-exec.c:1900
int i
ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER]
Definition: parallel_slot.h:43
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:6753
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1927