Skip to content

Commit 9db3018

Browse files
pg_upgrade: Parallelize contrib/isn check.
This commit makes use of the new task framework in pg_upgrade to parallelize the check for contrib/isn functions that rely on the bigint data type. 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 bbf83ca commit 9db3018

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,39 @@ check_for_prepared_transactions(ClusterInfo *cluster)
12251225
check_ok();
12261226
}
12271227

1228+
/*
1229+
* Callback function for processing result of query for
1230+
* check_for_isn_and_int8_passing_mismatch()'s UpgradeTask. If the query
1231+
* returned any rows (i.e., the check failed), write the details to the report
1232+
* file.
1233+
*/
1234+
static void
1235+
process_isn_and_int8_passing_mismatch(DbInfo *dbinfo, PGresult *res, void *arg)
1236+
{
1237+
bool db_used = false;
1238+
int ntups = PQntuples(res);
1239+
int i_nspname = PQfnumber(res, "nspname");
1240+
int i_proname = PQfnumber(res, "proname");
1241+
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1242+
1243+
AssertVariableIsOfType(&process_isn_and_int8_passing_mismatch,
1244+
UpgradeTaskProcessCB);
1245+
1246+
for (int rowno = 0; rowno < ntups; rowno++)
1247+
{
1248+
if (report->file == NULL &&
1249+
(report->file = fopen_priv(report->path, "w")) == NULL)
1250+
pg_fatal("could not open file \"%s\": %m", report->path);
1251+
if (!db_used)
1252+
{
1253+
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1254+
db_used = true;
1255+
}
1256+
fprintf(report->file, " %s.%s\n",
1257+
PQgetvalue(res, rowno, i_nspname),
1258+
PQgetvalue(res, rowno, i_proname));
1259+
}
1260+
}
12281261

12291262
/*
12301263
* check_for_isn_and_int8_passing_mismatch()
@@ -1236,9 +1269,13 @@ check_for_prepared_transactions(ClusterInfo *cluster)
12361269
static void
12371270
check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
12381271
{
1239-
int dbnum;
1240-
FILE *script = NULL;
1241-
char output_path[MAXPGPATH];
1272+
UpgradeTask *task;
1273+
UpgradeTaskReport report;
1274+
const char *query = "SELECT n.nspname, p.proname "
1275+
"FROM pg_catalog.pg_proc p, "
1276+
" pg_catalog.pg_namespace n "
1277+
"WHERE p.pronamespace = n.oid AND "
1278+
" p.probin = '$libdir/isn'";
12421279

12431280
prep_status("Checking for contrib/isn with bigint-passing mismatch");
12441281

@@ -1250,62 +1287,28 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
12501287
return;
12511288
}
12521289

1253-
snprintf(output_path, sizeof(output_path), "%s/%s",
1290+
report.file = NULL;
1291+
snprintf(report.path, sizeof(report.path), "%s/%s",
12541292
log_opts.basedir,
12551293
"contrib_isn_and_int8_pass_by_value.txt");
12561294

1257-
for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1258-
{
1259-
PGresult *res;
1260-
bool db_used = false;
1261-
int ntups;
1262-
int rowno;
1263-
int i_nspname,
1264-
i_proname;
1265-
DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1266-
PGconn *conn = connectToServer(cluster, active_db->db_name);
1267-
1268-
/* Find any functions coming from contrib/isn */
1269-
res = executeQueryOrDie(conn,
1270-
"SELECT n.nspname, p.proname "
1271-
"FROM pg_catalog.pg_proc p, "
1272-
" pg_catalog.pg_namespace n "
1273-
"WHERE p.pronamespace = n.oid AND "
1274-
" p.probin = '$libdir/isn'");
1275-
1276-
ntups = PQntuples(res);
1277-
i_nspname = PQfnumber(res, "nspname");
1278-
i_proname = PQfnumber(res, "proname");
1279-
for (rowno = 0; rowno < ntups; rowno++)
1280-
{
1281-
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1282-
pg_fatal("could not open file \"%s\": %m", output_path);
1283-
if (!db_used)
1284-
{
1285-
fprintf(script, "In database: %s\n", active_db->db_name);
1286-
db_used = true;
1287-
}
1288-
fprintf(script, " %s.%s\n",
1289-
PQgetvalue(res, rowno, i_nspname),
1290-
PQgetvalue(res, rowno, i_proname));
1291-
}
1292-
1293-
PQclear(res);
1294-
1295-
PQfinish(conn);
1296-
}
1295+
task = upgrade_task_create();
1296+
upgrade_task_add_step(task, query, process_isn_and_int8_passing_mismatch,
1297+
true, &report);
1298+
upgrade_task_run(task, cluster);
1299+
upgrade_task_free(task);
12971300

1298-
if (script)
1301+
if (report.file)
12991302
{
1300-
fclose(script);
1303+
fclose(report.file);
13011304
pg_log(PG_REPORT, "fatal");
13021305
pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
13031306
"bigint data type. Your old and new clusters pass bigint values\n"
13041307
"differently so this cluster cannot currently be upgraded. You can\n"
13051308
"manually dump databases in the old cluster that use \"contrib/isn\"\n"
13061309
"facilities, drop them, perform the upgrade, and then restore them. A\n"
13071310
"list of the problem functions is in the file:\n"
1308-
" %s", output_path);
1311+
" %s", report.path);
13091312
}
13101313
else
13111314
check_ok();

0 commit comments

Comments
 (0)