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)
 
static void ParallelSlotSetIdle (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 ParallelSlotSetIdle().

◆ ParallelSlotsAdoptConn()

void ParallelSlotsAdoptConn ( ParallelSlotArray sa,
PGconn conn 
)

Definition at line 459 of file parallel_slot.c.

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

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

◆ ParallelSlotSetIdle()

static void ParallelSlotSetIdle ( ParallelSlot slot)
inlinestatic

Definition at line 62 of file parallel_slot.h.

63{
64 slot->inUse = false;
66}
static void ParallelSlotClearHandler(ParallelSlot *slot)
Definition: parallel_slot.h:55

References ParallelSlot::inUse, and ParallelSlotClearHandler().

Referenced by ParallelSlotsWaitCompletion(), run_vacuum_command(), and wait_on_slots().

◆ ParallelSlotsGetIdle()

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

Definition at line 370 of file parallel_slot.c.

371{
372 int offset;
373
374 Assert(sa);
375 Assert(sa->numslots > 0);
376
377 while (1)
378 {
379 /* First choice: a slot already connected to the desired database. */
381 if (offset >= 0)
382 {
383 sa->slots[offset].inUse = true;
384 return &sa->slots[offset];
385 }
386
387 /* Second choice: a slot not connected to any database. */
388 offset = find_unconnected_slot(sa);
389 if (offset >= 0)
390 {
391 connect_slot(sa, offset, dbname);
392 sa->slots[offset].inUse = true;
393 return &sa->slots[offset];
394 }
395
396 /* Third choice: a slot connected to the wrong database. */
397 offset = find_any_idle_slot(sa);
398 if (offset >= 0)
399 {
400 disconnectDatabase(sa->slots[offset].connection);
401 sa->slots[offset].connection = NULL;
402 connect_slot(sa, offset, dbname);
403 sa->slots[offset].inUse = true;
404 return &sa->slots[offset];
405 }
406
407 /*
408 * Fourth choice: block until one or more slots become available. If
409 * any slots hit a fatal error, we'll find out about that here and
410 * return NULL.
411 */
412 if (!wait_on_slots(sa))
413 return NULL;
414 }
415}
Assert(PointerIsAligned(start, uint64))
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:49

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

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

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

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

◆ ParallelSlotsTerminate()

void ParallelSlotsTerminate ( ParallelSlotArray sa)

Definition at line 478 of file parallel_slot.c.

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

References conn, disconnectDatabase(), and i.

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

◆ ParallelSlotsWaitCompletion()

bool ParallelSlotsWaitCompletion ( ParallelSlotArray sa)

Definition at line 500 of file parallel_slot.c.

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

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

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

◆ TableCommandResultHandler()

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

Definition at line 538 of file parallel_slot.c.

539{
540 Assert(res != NULL);
541 Assert(conn != NULL);
542
543 /*
544 * If it's an error, report it. Errors about a missing table are harmless
545 * so we continue processing; but die for other errors.
546 */
548 {
549 char *sqlState = PQresultErrorField(res, PG_DIAG_SQLSTATE);
550
551 pg_log_error("processing of database \"%s\" failed: %s",
553
554 if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
555 {
556 PQclear(res);
557 return false;
558 }
559 }
560
561 return true;
562}
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7538
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7704
#define PQclear
Definition: libpq-be-fe.h:245
#define PQresultErrorField
Definition: libpq-be-fe.h:249
#define PQresultStatus
Definition: libpq-be-fe.h:247
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
#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:57

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

Referenced by reindex_one_database(), and vacuum_one_database().