Skip to content

Commit 6ab8f27

Browse files
pg_upgrade: Parallelize retrieving extension updates.
This commit makes use of the new task framework in pg_upgrade to parallelize retrieving the set of extensions that should be updated with the ALTER EXTENSION command after upgrade. This step will now process multiple databases concurrently when pg_upgrade's --jobs option is provided a value greater than 1. Reviewed-by: Daniel Gustafsson, Ilya Gladyshev Discussion: https://postgr.es/m/20240516211638.GA1688936%40nathanxps13
1 parent 46cad8b commit 6ab8f27

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

src/bin/pg_upgrade/version.c

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -139,72 +139,76 @@ old_9_6_invalidate_hash_indexes(ClusterInfo *cluster, bool check_mode)
139139
check_ok();
140140
}
141141

142+
/*
143+
* Callback function for processing results of query for
144+
* report_extension_updates()'s UpgradeTask. If the query returned any rows,
145+
* write the details to the report file.
146+
*/
147+
static void
148+
process_extension_updates(DbInfo *dbinfo, PGresult *res, void *arg)
149+
{
150+
bool db_used = false;
151+
int ntups = PQntuples(res);
152+
int i_name = PQfnumber(res, "name");
153+
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
154+
155+
AssertVariableIsOfType(&process_extension_updates, UpgradeTaskProcessCB);
156+
157+
for (int rowno = 0; rowno < ntups; rowno++)
158+
{
159+
if (report->file == NULL &&
160+
(report->file = fopen_priv(report->path, "w")) == NULL)
161+
pg_fatal("could not open file \"%s\": %m", report->path);
162+
if (!db_used)
163+
{
164+
PQExpBufferData connectbuf;
165+
166+
initPQExpBuffer(&connectbuf);
167+
appendPsqlMetaConnect(&connectbuf, dbinfo->db_name);
168+
fputs(connectbuf.data, report->file);
169+
termPQExpBuffer(&connectbuf);
170+
db_used = true;
171+
}
172+
fprintf(report->file, "ALTER EXTENSION %s UPDATE;\n",
173+
quote_identifier(PQgetvalue(res, rowno, i_name)));
174+
}
175+
}
176+
142177
/*
143178
* report_extension_updates()
144179
* Report extensions that should be updated.
145180
*/
146181
void
147182
report_extension_updates(ClusterInfo *cluster)
148183
{
149-
int dbnum;
150-
FILE *script = NULL;
151-
char *output_path = "update_extensions.sql";
184+
UpgradeTaskReport report;
185+
UpgradeTask *task = upgrade_task_create();
186+
const char *query = "SELECT name "
187+
"FROM pg_available_extensions "
188+
"WHERE installed_version != default_version";
152189

153190
prep_status("Checking for extension updates");
154191

155-
for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
156-
{
157-
PGresult *res;
158-
bool db_used = false;
159-
int ntups;
160-
int rowno;
161-
int i_name;
162-
DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
163-
PGconn *conn = connectToServer(cluster, active_db->db_name);
164-
165-
/* find extensions needing updates */
166-
res = executeQueryOrDie(conn,
167-
"SELECT name "
168-
"FROM pg_available_extensions "
169-
"WHERE installed_version != default_version"
170-
);
192+
report.file = NULL;
193+
strcpy(report.path, "update_extensions.sql");
171194

172-
ntups = PQntuples(res);
173-
i_name = PQfnumber(res, "name");
174-
for (rowno = 0; rowno < ntups; rowno++)
175-
{
176-
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
177-
pg_fatal("could not open file \"%s\": %m", output_path);
178-
if (!db_used)
179-
{
180-
PQExpBufferData connectbuf;
195+
upgrade_task_add_step(task, query, process_extension_updates,
196+
true, &report);
181197

182-
initPQExpBuffer(&connectbuf);
183-
appendPsqlMetaConnect(&connectbuf, active_db->db_name);
184-
fputs(connectbuf.data, script);
185-
termPQExpBuffer(&connectbuf);
186-
db_used = true;
187-
}
188-
fprintf(script, "ALTER EXTENSION %s UPDATE;\n",
189-
quote_identifier(PQgetvalue(res, rowno, i_name)));
190-
}
198+
upgrade_task_run(task, cluster);
199+
upgrade_task_free(task);
191200

192-
PQclear(res);
193-
194-
PQfinish(conn);
195-
}
196-
197-
if (script)
201+
if (report.file)
198202
{
199-
fclose(script);
203+
fclose(report.file);
200204
report_status(PG_REPORT, "notice");
201205
pg_log(PG_REPORT, "\n"
202206
"Your installation contains extensions that should be updated\n"
203207
"with the ALTER EXTENSION command. The file\n"
204208
" %s\n"
205209
"when executed by psql by the database superuser will update\n"
206210
"these extensions.",
207-
output_path);
211+
report.path);
208212
}
209213
else
210214
check_ok();

0 commit comments

Comments
 (0)