PostgreSQL Source Code  git master
parallel_slot.h File Reference
#include "fe_utils/connect_utils.h"
#include "libpq-fe.h"
Include dependency graph for parallel_slot.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ParallelSlot
 
struct  ParallelSlotArray
 

Typedefs

typedef bool(* ParallelSlotResultHandler) (PGresult *res, PGconn *conn, void *context)
 
typedef struct ParallelSlot ParallelSlot
 
typedef struct ParallelSlotArray ParallelSlotArray
 

Functions

static void ParallelSlotSetHandler (ParallelSlot *slot, ParallelSlotResultHandler handler, void *context)
 
static void ParallelSlotClearHandler (ParallelSlot *slot)
 
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)
 

Typedef Documentation

◆ ParallelSlot

typedef struct ParallelSlot ParallelSlot

◆ ParallelSlotArray

◆ ParallelSlotResultHandler

typedef bool(* ParallelSlotResultHandler) (PGresult *res, PGconn *conn, void *context)

Definition at line 18 of file parallel_slot.h.

Function Documentation

◆ ParallelSlotClearHandler()

static void ParallelSlotClearHandler ( ParallelSlot slot)
inlinestatic

Definition at line 55 of file parallel_slot.h.

56 {
57  slot->handler = NULL;
58  slot->handler_context = NULL;
59 }
ParallelSlotResultHandler handler
Definition: parallel_slot.h:32
void * handler_context
Definition: parallel_slot.h:33

References ParallelSlot::handler, and ParallelSlot::handler_context.

Referenced by ParallelSlotsWaitCompletion(), and wait_on_slots().

◆ ParallelSlotsAdoptConn()

void ParallelSlotsAdoptConn ( ParallelSlotArray sa,
PGconn conn 
)

Definition at line 460 of file parallel_slot.c.

461 {
462  int offset;
463 
464  offset = find_unconnected_slot(sa);
465  if (offset >= 0)
466  sa->slots[offset].connection = conn;
467  else
469 }
void disconnectDatabase(PGconn *conn)
static int find_unconnected_slot(const ParallelSlotArray *sa)
PGconn * conn
Definition: streamutil.c:53

References conn, disconnectDatabase(), and find_unconnected_slot().

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

◆ ParallelSlotSetHandler()

static void ParallelSlotSetHandler ( ParallelSlot slot,
ParallelSlotResultHandler  handler,
void *  context 
)
inlinestatic

Definition at line 47 of file parallel_slot.h.

49 {
50  slot->handler = handler;
51  slot->handler_context = context;
52 }
tree context
Definition: radixtree.h:1835

References context, ParallelSlot::handler, and ParallelSlot::handler_context.

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

◆ ParallelSlotsGetIdle()

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

Definition at line 371 of file parallel_slot.c.

372 {
373  int offset;
374 
375  Assert(sa);
376  Assert(sa->numslots > 0);
377 
378  while (1)
379  {
380  /* First choice: a slot already connected to the desired database. */
381  offset = find_matching_idle_slot(sa, dbname);
382  if (offset >= 0)
383  {
384  sa->slots[offset].inUse = true;
385  return &sa->slots[offset];
386  }
387 
388  /* Second choice: a slot not connected to any database. */
389  offset = find_unconnected_slot(sa);
390  if (offset >= 0)
391  {
392  connect_slot(sa, offset, dbname);
393  sa->slots[offset].inUse = true;
394  return &sa->slots[offset];
395  }
396 
397  /* Third choice: a slot connected to the wrong database. */
398  offset = find_any_idle_slot(sa);
399  if (offset >= 0)
400  {
401  disconnectDatabase(sa->slots[offset].connection);
402  sa->slots[offset].connection = NULL;
403  connect_slot(sa, offset, dbname);
404  sa->slots[offset].inUse = true;
405  return &sa->slots[offset];
406  }
407 
408  /*
409  * Fourth choice: block until one or more slots become available. If
410  * any slots hit a fatal error, we'll find out about that here and
411  * return NULL.
412  */
413  if (!wait_on_slots(sa))
414  return NULL;
415  }
416 }
#define Assert(condition)
Definition: c.h:837
static bool wait_on_slots(ParallelSlotArray *sa)
static int find_matching_idle_slot(const ParallelSlotArray *sa, const char *dbname)
static void connect_slot(ParallelSlotArray *sa, int slotno, const char *dbname)
static int find_any_idle_slot(const ParallelSlotArray *sa)
char * dbname
Definition: streamutil.c:50

References Assert, connect_slot(), dbname, disconnectDatabase(), find_any_idle_slot(), find_matching_idle_slot(), find_unconnected_slot(), and wait_on_slots().

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

◆ ParallelSlotsSetup()

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

Definition at line 428 of file parallel_slot.c.

430 {
432 
433  Assert(numslots > 0);
434  Assert(cparams != NULL);
435  Assert(progname != NULL);
436 
437  sa = (ParallelSlotArray *) palloc0(offsetof(ParallelSlotArray, slots) +
438  numslots * sizeof(ParallelSlot));
439 
440  sa->numslots = numslots;
441  sa->cparams = cparams;
442  sa->progname = progname;
443  sa->echo = echo;
444  sa->initcmd = initcmd;
445 
446  return sa;
447 }
struct ParallelSlot ParallelSlot
Definition: parallel.h:52
const char * progname
Definition: main.c:43
void * palloc0(Size size)
Definition: mcxt.c:1347

References Assert, palloc0(), and progname.

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

◆ ParallelSlotsTerminate()

void ParallelSlotsTerminate ( ParallelSlotArray sa)

Definition at line 479 of file parallel_slot.c.

480 {
481  int i;
482 
483  for (i = 0; i < sa->numslots; i++)
484  {
485  PGconn *conn = sa->slots[i].connection;
486 
487  if (conn == NULL)
488  continue;
489 
491  }
492 }
int i
Definition: isn.c:72

References conn, disconnectDatabase(), and i.

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

◆ ParallelSlotsWaitCompletion()

bool ParallelSlotsWaitCompletion ( ParallelSlotArray sa)

Definition at line 501 of file parallel_slot.c.

502 {
503  int i;
504 
505  for (i = 0; i < sa->numslots; i++)
506  {
507  if (sa->slots[i].connection == NULL)
508  continue;
509  if (!consumeQueryResult(&sa->slots[i]))
510  return false;
511  /* Mark connection as idle */
512  sa->slots[i].inUse = false;
513  ParallelSlotClearHandler(&sa->slots[i]);
514  }
515 
516  return true;
517 }
static bool consumeQueryResult(ParallelSlot *slot)
Definition: parallel_slot.c:58
static void ParallelSlotClearHandler(ParallelSlot *slot)
Definition: parallel_slot.h:55

References consumeQueryResult(), i, and ParallelSlotClearHandler().

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

◆ TableCommandResultHandler()

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

Definition at line 540 of file parallel_slot.c.

541 {
542  Assert(res != NULL);
543  Assert(conn != NULL);
544 
545  /*
546  * If it's an error, report it. Errors about a missing table are harmless
547  * so we continue processing; but die for other errors.
548  */
550  {
551  char *sqlState = PQresultErrorField(res, PG_DIAG_SQLSTATE);
552 
553  pg_log_error("processing of database \"%s\" failed: %s",
555 
556  if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
557  {
558  PQclear(res);
559  return false;
560  }
561  }
562 
563  return true;
564 }
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7036
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7200
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3466
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
#define pg_log_error(...)
Definition: logging.h:106
#define ERRCODE_UNDEFINED_TABLE
Definition: parallel_slot.c:28
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:56

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

Referenced by reindex_one_database(), and vacuum_one_database().