Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f71519e

Browse files
committed
Refactor and generalize the ParallelSlot machinery.
Create a wrapper object, ParallelSlotArray, to encapsulate the number of slots and the slot array itself, plus some other relevant bits of information. This reduces the number of parameters we have to pass around all over the place. Allow for a ParallelSlotArray to contain slots connected to different databases within a single cluster. The current clients of this mechanism don't need this, but it is expected to be used by future patches. Defer connecting to databases until we actually need the connection for something. This is a slight behavior change for vacuumdb and reindexdb. If you specify a number of jobs that is larger than the number of objects, the extra connections will now not be used. But, on the other hand, if you specify a number of jobs that is so large that it's going to fail, the failure would previously have happened before any operations were actually started, and now it won't. Mark Dilger, reviewed by me. Discussion: http://postgr.es/m/[email protected] Discussion: http://postgr.es/m/[email protected]
1 parent 2c0cefc commit f71519e

File tree

5 files changed

+338
-161
lines changed

5 files changed

+338
-161
lines changed

src/bin/scripts/reindexdb.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static SimpleStringList *get_parallel_object_list(PGconn *conn,
3636
ReindexType type,
3737
SimpleStringList *user_list,
3838
bool echo);
39-
static void reindex_one_database(const ConnParams *cparams, ReindexType type,
39+
static void reindex_one_database(ConnParams *cparams, ReindexType type,
4040
SimpleStringList *user_list,
4141
const char *progname,
4242
bool echo, bool verbose, bool concurrently,
@@ -330,7 +330,7 @@ main(int argc, char *argv[])
330330
}
331331

332332
static void
333-
reindex_one_database(const ConnParams *cparams, ReindexType type,
333+
reindex_one_database(ConnParams *cparams, ReindexType type,
334334
SimpleStringList *user_list,
335335
const char *progname, bool echo,
336336
bool verbose, bool concurrently, int concurrentCons,
@@ -341,7 +341,7 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
341341
bool parallel = concurrentCons > 1;
342342
SimpleStringList *process_list = user_list;
343343
ReindexType process_type = type;
344-
ParallelSlot *slots;
344+
ParallelSlotArray *sa;
345345
bool failed = false;
346346
int items_count = 0;
347347

@@ -461,7 +461,8 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
461461

462462
Assert(process_list != NULL);
463463

464-
slots = ParallelSlotsSetup(cparams, progname, echo, conn, concurrentCons);
464+
sa = ParallelSlotsSetup(concurrentCons, cparams, progname, echo, NULL);
465+
ParallelSlotsAdoptConn(sa, conn);
465466

466467
cell = process_list->head;
467468
do
@@ -475,7 +476,7 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
475476
goto finish;
476477
}
477478

478-
free_slot = ParallelSlotsGetIdle(slots, concurrentCons);
479+
free_slot = ParallelSlotsGetIdle(sa, NULL);
479480
if (!free_slot)
480481
{
481482
failed = true;
@@ -489,7 +490,7 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
489490
cell = cell->next;
490491
} while (cell != NULL);
491492

492-
if (!ParallelSlotsWaitCompletion(slots, concurrentCons))
493+
if (!ParallelSlotsWaitCompletion(sa))
493494
failed = true;
494495

495496
finish:
@@ -499,8 +500,8 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
499500
pg_free(process_list);
500501
}
501502

502-
ParallelSlotsTerminate(slots, concurrentCons);
503-
pfree(slots);
503+
ParallelSlotsTerminate(sa);
504+
pfree(sa);
504505

505506
if (failed)
506507
exit(1);

src/bin/scripts/vacuumdb.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct vacuumingOptions
4545
} vacuumingOptions;
4646

4747

48-
static void vacuum_one_database(const ConnParams *cparams,
48+
static void vacuum_one_database(ConnParams *cparams,
4949
vacuumingOptions *vacopts,
5050
int stage,
5151
SimpleStringList *tables,
@@ -408,7 +408,7 @@ main(int argc, char *argv[])
408408
* a list of tables from the database.
409409
*/
410410
static void
411-
vacuum_one_database(const ConnParams *cparams,
411+
vacuum_one_database(ConnParams *cparams,
412412
vacuumingOptions *vacopts,
413413
int stage,
414414
SimpleStringList *tables,
@@ -421,13 +421,14 @@ vacuum_one_database(const ConnParams *cparams,
421421
PGresult *res;
422422
PGconn *conn;
423423
SimpleStringListCell *cell;
424-
ParallelSlot *slots;
424+
ParallelSlotArray *sa;
425425
SimpleStringList dbtables = {NULL, NULL};
426426
int i;
427427
int ntups;
428428
bool failed = false;
429429
bool tables_listed = false;
430430
bool has_where = false;
431+
const char *initcmd;
431432
const char *stage_commands[] = {
432433
"SET default_statistics_target=1; SET vacuum_cost_delay=0;",
433434
"SET default_statistics_target=10; RESET vacuum_cost_delay;",
@@ -684,26 +685,25 @@ vacuum_one_database(const ConnParams *cparams,
684685
concurrentCons = 1;
685686

686687
/*
687-
* Setup the database connections. We reuse the connection we already have
688-
* for the first slot. If not in parallel mode, the first slot in the
689-
* array contains the connection.
688+
* All slots need to be prepared to run the appropriate analyze stage, if
689+
* caller requested that mode. We have to prepare the initial connection
690+
* ourselves before setting up the slots.
690691
*/
691-
slots = ParallelSlotsSetup(cparams, progname, echo, conn, concurrentCons);
692+
if (stage == ANALYZE_NO_STAGE)
693+
initcmd = NULL;
694+
else
695+
{
696+
initcmd = stage_commands[stage];
697+
executeCommand(conn, initcmd, echo);
698+
}
692699

693700
/*
694-
* Prepare all the connections to run the appropriate analyze stage, if
695-
* caller requested that mode.
701+
* Setup the database connections. We reuse the connection we already have
702+
* for the first slot. If not in parallel mode, the first slot in the
703+
* array contains the connection.
696704
*/
697-
if (stage != ANALYZE_NO_STAGE)
698-
{
699-
int j;
700-
701-
/* We already emitted the message above */
702-
703-
for (j = 0; j < concurrentCons; j++)
704-
executeCommand((slots + j)->connection,
705-
stage_commands[stage], echo);
706-
}
705+
sa = ParallelSlotsSetup(concurrentCons, cparams, progname, echo, initcmd);
706+
ParallelSlotsAdoptConn(sa, conn);
707707

708708
initPQExpBuffer(&sql);
709709

@@ -719,7 +719,7 @@ vacuum_one_database(const ConnParams *cparams,
719719
goto finish;
720720
}
721721

722-
free_slot = ParallelSlotsGetIdle(slots, concurrentCons);
722+
free_slot = ParallelSlotsGetIdle(sa, NULL);
723723
if (!free_slot)
724724
{
725725
failed = true;
@@ -740,12 +740,12 @@ vacuum_one_database(const ConnParams *cparams,
740740
cell = cell->next;
741741
} while (cell != NULL);
742742

743-
if (!ParallelSlotsWaitCompletion(slots, concurrentCons))
743+
if (!ParallelSlotsWaitCompletion(sa))
744744
failed = true;
745745

746746
finish:
747-
ParallelSlotsTerminate(slots, concurrentCons);
748-
pg_free(slots);
747+
ParallelSlotsTerminate(sa);
748+
pg_free(sa);
749749

750750
termPQExpBuffer(&sql);
751751

0 commit comments

Comments
 (0)