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 427 of file parallel_slot.c.

428 {
429  int offset;
430 
431  offset = find_unconnected_slot(sa);
432  if (offset >= 0)
433  sa->slots[offset].connection = conn;
434  else
436 }
void disconnectDatabase(PGconn *conn)
static int find_unconnected_slot(const ParallelSlotArray *sa)
PGconn * conn
Definition: streamutil.c:54

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 }

References 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 338 of file parallel_slot.c.

339 {
340  int offset;
341 
342  Assert(sa);
343  Assert(sa->numslots > 0);
344 
345  while (1)
346  {
347  /* First choice: a slot already connected to the desired database. */
348  offset = find_matching_idle_slot(sa, dbname);
349  if (offset >= 0)
350  {
351  sa->slots[offset].inUse = true;
352  return &sa->slots[offset];
353  }
354 
355  /* Second choice: a slot not connected to any database. */
356  offset = find_unconnected_slot(sa);
357  if (offset >= 0)
358  {
359  connect_slot(sa, offset, dbname);
360  sa->slots[offset].inUse = true;
361  return &sa->slots[offset];
362  }
363 
364  /* Third choice: a slot connected to the wrong database. */
365  offset = find_any_idle_slot(sa);
366  if (offset >= 0)
367  {
368  disconnectDatabase(sa->slots[offset].connection);
369  sa->slots[offset].connection = NULL;
370  connect_slot(sa, offset, dbname);
371  sa->slots[offset].inUse = true;
372  return &sa->slots[offset];
373  }
374 
375  /*
376  * Fourth choice: block until one or more slots become available. If
377  * any slots hit a fatal error, we'll find out about that here and
378  * return NULL.
379  */
380  if (!wait_on_slots(sa))
381  return NULL;
382  }
383 }
Assert(fmt[strlen(fmt) - 1] !='\n')
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:51

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 395 of file parallel_slot.c.

397 {
399 
400  Assert(numslots > 0);
401  Assert(cparams != NULL);
402  Assert(progname != NULL);
403 
404  sa = (ParallelSlotArray *) palloc0(offsetof(ParallelSlotArray, slots) +
405  numslots * sizeof(ParallelSlot));
406 
407  sa->numslots = numslots;
408  sa->cparams = cparams;
409  sa->progname = progname;
410  sa->echo = echo;
411  sa->initcmd = initcmd;
412 
413  return sa;
414 }
struct ParallelSlot ParallelSlot
Definition: parallel.h:52
const char * progname
Definition: main.c:45
void * palloc0(Size size)
Definition: mcxt.c:1241

References Assert(), palloc0(), and progname.

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

◆ ParallelSlotsTerminate()

void ParallelSlotsTerminate ( ParallelSlotArray sa)

Definition at line 446 of file parallel_slot.c.

447 {
448  int i;
449 
450  for (i = 0; i < sa->numslots; i++)
451  {
452  PGconn *conn = sa->slots[i].connection;
453 
454  if (conn == NULL)
455  continue;
456 
458  }
459 }
int i
Definition: isn.c:73

References conn, disconnectDatabase(), and i.

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

◆ ParallelSlotsWaitCompletion()

bool ParallelSlotsWaitCompletion ( ParallelSlotArray sa)

Definition at line 468 of file parallel_slot.c.

469 {
470  int i;
471 
472  for (i = 0; i < sa->numslots; i++)
473  {
474  if (sa->slots[i].connection == NULL)
475  continue;
476  if (!consumeQueryResult(&sa->slots[i]))
477  return false;
478  /* Mark connection as idle */
479  sa->slots[i].inUse = false;
480  ParallelSlotClearHandler(&sa->slots[i]);
481  }
482 
483  return true;
484 }
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 507 of file parallel_slot.c.

508 {
509  Assert(res != NULL);
510  Assert(conn != NULL);
511 
512  /*
513  * If it's an error, report it. Errors about a missing table are harmless
514  * so we continue processing; but die for other errors.
515  */
517  {
518  char *sqlState = PQresultErrorField(res, PG_DIAG_SQLSTATE);
519 
520  pg_log_error("processing of database \"%s\" failed: %s",
522 
523  if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
524  {
525  PQclear(res);
526  return false;
527  }
528  }
529 
530  return true;
531 }
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6589
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3240
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3295
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
#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().