From c38137cd2cbc45c348e8e144f864a62c08bd7b7f Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 28 Aug 2024 10:45:59 -0500 Subject: [PATCH v10 01/11] Introduce framework for parallelizing various pg_upgrade tasks. A number of pg_upgrade steps require connecting to every database in the cluster and running the same query in each one. When there are many databases, these steps are particularly time-consuming, especially since these steps are performed sequentially in a single process. This commit introduces a new framework that makes it easy to parallelize most of these once-in-each-database tasks. Specifically, it manages a simple state machine of slots and uses libpq's asynchronous APIs to establish the connections and run the queries. The --jobs option is used to determine the number of slots to use. To use this new task framework, callers simply need to provide the query and a callback function to process its results, and the framework takes care of the rest. A more complete description is provided at the top of the new task.c file. None of the eligible once-in-each-database tasks are converted to use this new framework in this commit. That will be done via several follow-up commits. Reviewed-by: Jeff Davis, Robert Haas, Daniel Gustafsson, Ilya Gladyshev, Corey Huinker Discussion: https://postgr.es/m/20240516211638.GA1688936%40nathanxps13 --- src/bin/pg_upgrade/Makefile | 1 + src/bin/pg_upgrade/meson.build | 1 + src/bin/pg_upgrade/pg_upgrade.h | 21 ++ src/bin/pg_upgrade/task.c | 426 +++++++++++++++++++++++++++++++ src/tools/pgindent/typedefs.list | 5 + 5 files changed, 454 insertions(+) create mode 100644 src/bin/pg_upgrade/task.c diff --git a/src/bin/pg_upgrade/Makefile b/src/bin/pg_upgrade/Makefile index bde91e2beb..f83d2b5d30 100644 --- a/src/bin/pg_upgrade/Makefile +++ b/src/bin/pg_upgrade/Makefile @@ -25,6 +25,7 @@ OBJS = \ relfilenumber.o \ server.o \ tablespace.o \ + task.o \ util.o \ version.o diff --git a/src/bin/pg_upgrade/meson.build b/src/bin/pg_upgrade/meson.build index 9825fa3305..3d88419674 100644 --- a/src/bin/pg_upgrade/meson.build +++ b/src/bin/pg_upgrade/meson.build @@ -14,6 +14,7 @@ pg_upgrade_sources = files( 'relfilenumber.c', 'server.c', 'tablespace.c', + 'task.c', 'util.c', 'version.c', ) diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h index cdb6e2b759..53f693c2d4 100644 --- a/src/bin/pg_upgrade/pg_upgrade.h +++ b/src/bin/pg_upgrade/pg_upgrade.h @@ -494,3 +494,24 @@ void parallel_transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr char *old_pgdata, char *new_pgdata, char *old_tablespace); bool reap_child(bool wait_for_child); + +/* task.c */ + +typedef void (*UpgradeTaskProcessCB) (DbInfo *dbinfo, PGresult *res, void *arg); + +/* struct definition is private to task.c */ +typedef struct UpgradeTask UpgradeTask; + +UpgradeTask *upgrade_task_create(void); +void upgrade_task_add_step(UpgradeTask *task, const char *query, + UpgradeTaskProcessCB process_cb, bool free_result, + void *arg); +void upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster); +void upgrade_task_free(UpgradeTask *task); + +/* convenient type for common private data needed by several tasks */ +typedef struct +{ + FILE *file; + char path[MAXPGPATH]; +} UpgradeTaskReport; diff --git a/src/bin/pg_upgrade/task.c b/src/bin/pg_upgrade/task.c new file mode 100644 index 0000000000..b5e2455ae2 --- /dev/null +++ b/src/bin/pg_upgrade/task.c @@ -0,0 +1,426 @@ +/* + * task.c + * framework for parallelizing pg_upgrade's once-in-each-database tasks + * + * This framework provides an efficient way of running the various + * once-in-each-database tasks required by pg_upgrade. Specifically, it + * parallelizes these tasks by managing a simple state machine of + * user_opts.jobs slots and using libpq's asynchronous APIs to establish the + * connections and run the queries. Callers simply need to create a callback + * function and build/execute an UpgradeTask. A simple example follows: + * + * static void + * my_process_cb(DbInfo *dbinfo, PGresult *res, void *arg) + * { + * for (int i = 0; i < PQntuples(res); i++) + * { + * ... process results ... + * } + * } + * + * void + * my_task(ClusterInfo *cluster) + * { + * UpgradeTask *task = upgrade_task_create(); + * + * upgrade_task_add_step(task, + * "... query text ...", + * my_process_cb, + * true, // let the task free the PGresult + * NULL); // "arg" pointer for callback + * upgrade_task_run(task, cluster); + * upgrade_task_free(task); + * } + * + * Note that multiple steps can be added to a given task. When there are + * multiple steps, the task will run all of the steps consecutively in the same + * database connection before freeing the connection and moving on. In other + * words, it only ever initiates one connection to each database in the + * cluster for a given run. + * + * Copyright (c) 2024, PostgreSQL Global Development Group + * src/bin/pg_upgrade/task.c + */ + +#include "postgres_fe.h" + +#include "common/connect.h" +#include "fe_utils/string_utils.h" +#include "pg_upgrade.h" + +/* + * dbs_complete stores the number of databases that we have completed + * processing. When this value equals the number of databases in the cluster, + * the task is finished. + */ +static int dbs_complete; + +/* + * dbs_processing stores the index of the next database in the cluster's array + * of databases that will be picked up for processing. It will always be + * greater than or equal to dbs_complete. + */ +static int dbs_processing; + +/* + * This struct stores all the information for a single step of a task. All + * steps in a task are run in a single connection before moving on to the next + * database (which requires a new connection). + */ +typedef struct UpgradeTaskStep +{ + UpgradeTaskProcessCB process_cb; /* processes the results of the query */ + const char *query; /* query text */ + bool free_result; /* should we free the result? */ + void *arg; /* pointer passed to process_cb */ +} UpgradeTaskStep; + +/* + * This struct is a thin wrapper around an array of steps, i.e., + * UpgradeTaskStep. + */ +typedef struct UpgradeTask +{ + UpgradeTaskStep *steps; + int num_steps; +} UpgradeTask; + +/* + * The different states for a parallel slot. + */ +typedef enum +{ + FREE, /* slot available for use in a new database */ + CONNECTING, /* waiting for connection to be established */ + RUNNING_QUERIES, /* running/processing queries in the task */ +} UpgradeTaskSlotState; + +/* + * We maintain an array of user_opts.jobs slots to execute the task. + */ +typedef struct +{ + UpgradeTaskSlotState state; /* state of the slot */ + int db_idx; /* index of the database assigned to slot */ + int step_idx; /* index of the current step of task */ + PGconn *conn; /* current connection managed by slot */ +} UpgradeTaskSlot; + +/* + * Initializes an UpgradeTask. + */ +UpgradeTask * +upgrade_task_create(void) +{ + UpgradeTask *task = pg_malloc0(sizeof(UpgradeTask)); + + /* All tasks must first set a secure search_path. */ + upgrade_task_add_step(task, ALWAYS_SECURE_SEARCH_PATH_SQL, NULL, true, NULL); + return task; +} + +/* + * Frees all storage associated with an UpgradeTask. + */ +void +upgrade_task_free(UpgradeTask *task) +{ + if (task->steps) + pg_free(task->steps); + + pg_free(task); +} + +/* + * Adds a step to an UpgradeTask. The steps will be executed in each database + * in the order in which they are added. + * + * task: task object that must have been initialized via upgrade_task_create() + * query: the query text + * process_cb: function that processes the results of the query + * free_result: should we free the PGresult, or leave it to the caller? + * arg: pointer to task-specific data that is passed to each callback + */ +void +upgrade_task_add_step(UpgradeTask *task, const char *query, + UpgradeTaskProcessCB process_cb, bool free_result, + void *arg) +{ + UpgradeTaskStep *new_step; + + task->steps = pg_realloc(task->steps, + ++task->num_steps * sizeof(UpgradeTaskStep)); + + new_step = &task->steps[task->num_steps - 1]; + new_step->process_cb = process_cb; + new_step->query = query; + new_step->free_result = free_result; + new_step->arg = arg; +} + +/* + * Build a connection string for the slot's current database and asynchronously + * start a new connection, but do not wait for the connection to be + * established. + */ +static void +start_conn(const ClusterInfo *cluster, UpgradeTaskSlot *slot) +{ + PQExpBufferData conn_opts; + DbInfo *dbinfo = &cluster->dbarr.dbs[slot->db_idx]; + + /* Build connection string with proper quoting */ + initPQExpBuffer(&conn_opts); + appendPQExpBufferStr(&conn_opts, "dbname="); + appendConnStrVal(&conn_opts, dbinfo->db_name); + appendPQExpBufferStr(&conn_opts, " user="); + appendConnStrVal(&conn_opts, os_info.user); + appendPQExpBuffer(&conn_opts, " port=%d", cluster->port); + if (cluster->sockdir) + { + appendPQExpBufferStr(&conn_opts, " host="); + appendConnStrVal(&conn_opts, cluster->sockdir); + } + + slot->conn = PQconnectStart(conn_opts.data); + + if (!slot->conn) + pg_fatal("failed to create connection with connection string: \"%s\"", + conn_opts.data); + + termPQExpBuffer(&conn_opts); +} + +/* + * Run the process_cb callback function to process the result of a query, and + * free the result if the caller indicated we should do so. + */ +static void +process_query_result(const ClusterInfo *cluster, UpgradeTaskSlot *slot, + const UpgradeTask *task) +{ + UpgradeTaskStep *steps = &task->steps[slot->step_idx]; + UpgradeTaskProcessCB process_cb = steps->process_cb; + DbInfo *dbinfo = &cluster->dbarr.dbs[slot->db_idx]; + PGresult *res = PQgetResult(slot->conn); + + if (PQstatus(slot->conn) == CONNECTION_BAD || + (PQresultStatus(res) != PGRES_TUPLES_OK && + PQresultStatus(res) != PGRES_COMMAND_OK)) + pg_fatal("connection failure: %s", PQerrorMessage(slot->conn)); + + /* + * We assume that a NULL process_cb callback function means there's + * nothing to process. This is primarily intended for the inital step in + * every task that sets a safe search_path. + */ + if (process_cb) + (*process_cb) (dbinfo, res, steps->arg); + + if (steps->free_result) + PQclear(res); +} + +/* + * Advances the state machine for a given slot as necessary. + */ +static void +process_slot(const ClusterInfo *cluster, UpgradeTaskSlot *slot, const UpgradeTask *task) +{ + PQExpBufferData queries; + + switch (slot->state) + { + case FREE: + + /* + * If all of the databases in the cluster have been processed or + * are currently being processed by other slots, we are done. + */ + if (dbs_processing >= cluster->dbarr.ndbs) + return; + + /* + * Claim the next database in the cluster's array and initiate a + * new connection. + */ + slot->db_idx = dbs_processing++; + slot->state = CONNECTING; + start_conn(cluster, slot); + + return; + + case CONNECTING: + + /* Check for connection failure. */ + if (PQconnectPoll(slot->conn) == PGRES_POLLING_FAILED) + pg_fatal("connection failure: %s", PQerrorMessage(slot->conn)); + + /* Check whether the connection is still establishing. */ + if (PQconnectPoll(slot->conn) != PGRES_POLLING_OK) + return; + + /* + * Move on to running/processing the queries in the task. We + * combine all the queries and send them to the server together. + */ + slot->state = RUNNING_QUERIES; + initPQExpBuffer(&queries); + for (int i = 0; i < task->num_steps; i++) + appendPQExpBuffer(&queries, "%s;", task->steps[i].query); + if (!PQsendQuery(slot->conn, queries.data)) + pg_fatal("connection failure: %s", PQerrorMessage(slot->conn)); + termPQExpBuffer(&queries); + + return; + + case RUNNING_QUERIES: + + /* + * Consume any available data and clear the read-ready indicator + * for the connection. + */ + if (!PQconsumeInput(slot->conn)) + pg_fatal("connection failure: %s", PQerrorMessage(slot->conn)); + + /* + * Process any results that are ready so that we can free up this + * slot for another database as soon as possible. + */ + for (; slot->step_idx < task->num_steps; slot->step_idx++) + { + /* If no more results are available yet, move on. */ + if (PQisBusy(slot->conn)) + return; + + process_query_result(cluster, slot, task); + } + + /* + * If we just finished processing the result of the last step in + * the task, free the slot. We recursively call this function on + * the newly-freed slot so that we can start initiating the next + * connection immediately instead of waiting for the next loop + * through the slots. + */ + dbs_complete++; + (void) PQgetResult(slot->conn); + PQfinish(slot->conn); + memset(slot, 0, sizeof(UpgradeTaskSlot)); + + process_slot(cluster, slot, task); + + return; + } +} + +/* + * Wait on the slots to either finish connecting or to receive query results if + * possible. This avoids a tight loop in upgrade_task_run(). + */ +static void +wait_on_slots(UpgradeTaskSlot *slots, int numslots) +{ + fd_set input_mask; + fd_set output_mask; + fd_set except_mask; + int maxFd = 0; + + FD_ZERO(&input_mask); + FD_ZERO(&output_mask); + FD_ZERO(&except_mask); + + for (int i = 0; i < numslots; i++) + { + int sock; + bool read = false; + + switch (slots[i].state) + { + case FREE: + + /* + * This function should only ever see free slots as we are + * finishing processing the last few databases, at which point + * we don't have any databases left for them to process. We'll + * never use these slots again, so we can safely ignore them. + */ + continue; + + case CONNECTING: + + /* + * If we are waiting for the connection to establish, choose + * whether to wait for reading or for writing on the socket as + * appropriate. If neither apply, just return immediately so + * that we can handle the slot. + */ + { + PostgresPollingStatusType status; + + status = PQconnectPoll(slots[i].conn); + if (status == PGRES_POLLING_READING) + read = true; + else if (status != PGRES_POLLING_WRITING) + return; + } + break; + + case RUNNING_QUERIES: + + /* + * Once we've sent the queries, we must wait for the socket to + * be read-ready. Note that process_slot() handles calling + * PQconsumeInput() as required. + */ + read = true; + break; + } + + /* + * If there's some problem retrieving the socket, just pretend this + * slot doesn't exist. We don't expect this to happen regularly in + * practice, so it seems unlikely to cause too much harm. + */ + sock = PQsocket(slots[i].conn); + if (sock < 0) + continue; + + /* + * Add the socket to the set. + */ + FD_SET(sock, read ? &input_mask : &output_mask); + FD_SET(sock, &except_mask); + maxFd = Max(maxFd, sock); + } + + /* + * If we found socket(s) to wait on, wait. + */ + if (maxFd != 0) + (void) select(maxFd + 1, &input_mask, &output_mask, &except_mask, NULL); +} + +/* + * Runs all the steps of the task in every database in the cluster using + * user_opts.jobs parallel slots. + */ +void +upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster) +{ + int jobs = Max(1, user_opts.jobs); + UpgradeTaskSlot *slots = pg_malloc0(sizeof(UpgradeTaskSlot) * jobs); + + dbs_complete = 0; + dbs_processing = 0; + + while (dbs_complete < cluster->dbarr.ndbs) + { + for (int i = 0; i < jobs; i++) + process_slot(cluster, &slots[i], task); + + wait_on_slots(slots, jobs); + } + + pg_free(slots); +} diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 9e951a9e6f..43c0f9f85b 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -3039,6 +3039,11 @@ UnresolvedTup UnresolvedTupData UpdateContext UpdateStmt +UpgradeTask +UpgradeTaskReport +UpgradeTaskSlot +UpgradeTaskSlotState +UpgradeTaskStep UploadManifestCmd UpperRelationKind UpperUniquePath -- 2.39.3 (Apple Git-146)