Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9726653

Browse files
Improve style of pg_upgrade task callback functions.
I wanted to avoid adjusting this code too much when converting these tasks to use the new parallelization framework (see commit 40e2e5e), which is why this is being done as a follow-up commit. These stylistic adjustments result in fewer lines of code and fewer levels of indentation in some places. While at it, add names to the UpgradeTaskSlotState enum and the UpgradeTaskSlot struct. I'm not aware of any established project policy in this area, but let's at least be consistent within the same file. Reviewed-by: Daniel Gustafsson Discussion: https://postgr.es/m/ZunW7XHLd2uTts4f%40nathan
1 parent 147bbc9 commit 9726653

File tree

3 files changed

+102
-143
lines changed

3 files changed

+102
-143
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 87 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static DataTypesUsageChecks data_types_usage_checks[] =
320320
struct data_type_check_state
321321
{
322322
DataTypesUsageChecks *check; /* the check for this step */
323-
bool *result; /* true if check failed for any database */
323+
bool result; /* true if check failed for any database */
324324
PQExpBuffer *report; /* buffer for report on failed checks */
325325
};
326326

@@ -390,69 +390,54 @@ process_data_type_check(DbInfo *dbinfo, PGresult *res, void *arg)
390390
{
391391
struct data_type_check_state *state = (struct data_type_check_state *) arg;
392392
int ntups = PQntuples(res);
393+
char output_path[MAXPGPATH];
394+
int i_nspname = PQfnumber(res, "nspname");
395+
int i_relname = PQfnumber(res, "relname");
396+
int i_attname = PQfnumber(res, "attname");
397+
FILE *script = NULL;
393398

394399
AssertVariableIsOfType(&process_data_type_check, UpgradeTaskProcessCB);
395400

396-
if (ntups)
397-
{
398-
char output_path[MAXPGPATH];
399-
int i_nspname;
400-
int i_relname;
401-
int i_attname;
402-
FILE *script = NULL;
403-
bool db_used = false;
401+
if (ntups == 0)
402+
return;
404403

405-
snprintf(output_path, sizeof(output_path), "%s/%s",
406-
log_opts.basedir,
407-
state->check->report_filename);
404+
snprintf(output_path, sizeof(output_path), "%s/%s",
405+
log_opts.basedir,
406+
state->check->report_filename);
408407

409-
/*
410-
* Make sure we have a buffer to save reports to now that we found a
411-
* first failing check.
412-
*/
413-
if (*state->report == NULL)
414-
*state->report = createPQExpBuffer();
408+
/*
409+
* Make sure we have a buffer to save reports to now that we found a first
410+
* failing check.
411+
*/
412+
if (*state->report == NULL)
413+
*state->report = createPQExpBuffer();
415414

416-
/*
417-
* If this is the first time we see an error for the check in question
418-
* then print a status message of the failure.
419-
*/
420-
if (!(*state->result))
421-
{
422-
pg_log(PG_REPORT, "failed check: %s", _(state->check->status));
423-
appendPQExpBuffer(*state->report, "\n%s\n%s %s\n",
424-
_(state->check->report_text),
425-
_("A list of the problem columns is in the file:"),
426-
output_path);
427-
}
428-
*state->result = true;
415+
/*
416+
* If this is the first time we see an error for the check in question
417+
* then print a status message of the failure.
418+
*/
419+
if (!state->result)
420+
{
421+
pg_log(PG_REPORT, "failed check: %s", _(state->check->status));
422+
appendPQExpBuffer(*state->report, "\n%s\n%s %s\n",
423+
_(state->check->report_text),
424+
_("A list of the problem columns is in the file:"),
425+
output_path);
426+
}
427+
state->result = true;
429428

430-
i_nspname = PQfnumber(res, "nspname");
431-
i_relname = PQfnumber(res, "relname");
432-
i_attname = PQfnumber(res, "attname");
429+
if ((script = fopen_priv(output_path, "a")) == NULL)
430+
pg_fatal("could not open file \"%s\": %m", output_path);
433431

434-
for (int rowno = 0; rowno < ntups; rowno++)
435-
{
436-
if (script == NULL && (script = fopen_priv(output_path, "a")) == NULL)
437-
pg_fatal("could not open file \"%s\": %m", output_path);
432+
fprintf(script, "In database: %s\n", dbinfo->db_name);
438433

439-
if (!db_used)
440-
{
441-
fprintf(script, "In database: %s\n", dbinfo->db_name);
442-
db_used = true;
443-
}
444-
fprintf(script, " %s.%s.%s\n",
445-
PQgetvalue(res, rowno, i_nspname),
446-
PQgetvalue(res, rowno, i_relname),
447-
PQgetvalue(res, rowno, i_attname));
448-
}
434+
for (int rowno = 0; rowno < ntups; rowno++)
435+
fprintf(script, " %s.%s.%s\n",
436+
PQgetvalue(res, rowno, i_nspname),
437+
PQgetvalue(res, rowno, i_relname),
438+
PQgetvalue(res, rowno, i_attname));
449439

450-
if (script)
451-
{
452-
fclose(script);
453-
script = NULL;
454-
}
455-
}
440+
fclose(script);
456441
}
457442

458443
/*
@@ -477,7 +462,6 @@ process_data_type_check(DbInfo *dbinfo, PGresult *res, void *arg)
477462
static void
478463
check_for_data_types_usage(ClusterInfo *cluster)
479464
{
480-
bool *results;
481465
PQExpBuffer report = NULL;
482466
DataTypesUsageChecks *tmp = data_types_usage_checks;
483467
int n_data_types_usage_checks = 0;
@@ -494,8 +478,7 @@ check_for_data_types_usage(ClusterInfo *cluster)
494478
tmp++;
495479
}
496480

497-
/* Prepare an array to store the results of checks in */
498-
results = pg_malloc0(sizeof(bool) * n_data_types_usage_checks);
481+
/* Allocate memory for queries and for task states */
499482
queries = pg_malloc0(sizeof(char *) * n_data_types_usage_checks);
500483
states = pg_malloc0(sizeof(struct data_type_check_state) * n_data_types_usage_checks);
501484

@@ -525,7 +508,6 @@ check_for_data_types_usage(ClusterInfo *cluster)
525508
queries[i] = data_type_check_query(i);
526509

527510
states[i].check = check;
528-
states[i].result = &results[i];
529511
states[i].report = &report;
530512

531513
upgrade_task_add_step(task, queries[i], process_data_type_check,
@@ -545,7 +527,6 @@ check_for_data_types_usage(ClusterInfo *cluster)
545527
destroyPQExpBuffer(report);
546528
}
547529

548-
pg_free(results);
549530
for (int i = 0; i < n_data_types_usage_checks; i++)
550531
{
551532
if (queries[i])
@@ -1234,7 +1215,6 @@ check_for_prepared_transactions(ClusterInfo *cluster)
12341215
static void
12351216
process_isn_and_int8_passing_mismatch(DbInfo *dbinfo, PGresult *res, void *arg)
12361217
{
1237-
bool db_used = false;
12381218
int ntups = PQntuples(res);
12391219
int i_nspname = PQfnumber(res, "nspname");
12401220
int i_proname = PQfnumber(res, "proname");
@@ -1243,20 +1223,19 @@ process_isn_and_int8_passing_mismatch(DbInfo *dbinfo, PGresult *res, void *arg)
12431223
AssertVariableIsOfType(&process_isn_and_int8_passing_mismatch,
12441224
UpgradeTaskProcessCB);
12451225

1226+
if (ntups == 0)
1227+
return;
1228+
1229+
if (report->file == NULL &&
1230+
(report->file = fopen_priv(report->path, "w")) == NULL)
1231+
pg_fatal("could not open file \"%s\": %m", report->path);
1232+
1233+
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1234+
12461235
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-
}
12561236
fprintf(report->file, " %s.%s\n",
12571237
PQgetvalue(res, rowno, i_nspname),
12581238
PQgetvalue(res, rowno, i_proname));
1259-
}
12601239
}
12611240

12621241
/*
@@ -1324,7 +1303,6 @@ process_user_defined_postfix_ops(DbInfo *dbinfo, PGresult *res, void *arg)
13241303
{
13251304
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
13261305
int ntups = PQntuples(res);
1327-
bool db_used = false;
13281306
int i_oproid = PQfnumber(res, "oproid");
13291307
int i_oprnsp = PQfnumber(res, "oprnsp");
13301308
int i_oprname = PQfnumber(res, "oprname");
@@ -1334,26 +1312,22 @@ process_user_defined_postfix_ops(DbInfo *dbinfo, PGresult *res, void *arg)
13341312
AssertVariableIsOfType(&process_user_defined_postfix_ops,
13351313
UpgradeTaskProcessCB);
13361314

1337-
if (!ntups)
1315+
if (ntups == 0)
13381316
return;
13391317

1318+
if (report->file == NULL &&
1319+
(report->file = fopen_priv(report->path, "w")) == NULL)
1320+
pg_fatal("could not open file \"%s\": %m", report->path);
1321+
1322+
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1323+
13401324
for (int rowno = 0; rowno < ntups; rowno++)
1341-
{
1342-
if (report->file == NULL &&
1343-
(report->file = fopen_priv(report->path, "w")) == NULL)
1344-
pg_fatal("could not open file \"%s\": %m", report->path);
1345-
if (!db_used)
1346-
{
1347-
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1348-
db_used = true;
1349-
}
13501325
fprintf(report->file, " (oid=%s) %s.%s (%s.%s, NONE)\n",
13511326
PQgetvalue(res, rowno, i_oproid),
13521327
PQgetvalue(res, rowno, i_oprnsp),
13531328
PQgetvalue(res, rowno, i_oprname),
13541329
PQgetvalue(res, rowno, i_typnsp),
13551330
PQgetvalue(res, rowno, i_typname));
1356-
}
13571331
}
13581332

13591333
/*
@@ -1422,29 +1396,26 @@ static void
14221396
process_incompat_polymorphics(DbInfo *dbinfo, PGresult *res, void *arg)
14231397
{
14241398
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1425-
bool db_used = false;
14261399
int ntups = PQntuples(res);
14271400
int i_objkind = PQfnumber(res, "objkind");
14281401
int i_objname = PQfnumber(res, "objname");
14291402

14301403
AssertVariableIsOfType(&process_incompat_polymorphics,
14311404
UpgradeTaskProcessCB);
14321405

1433-
for (int rowno = 0; rowno < ntups; rowno++)
1434-
{
1435-
if (report->file == NULL &&
1436-
(report->file = fopen_priv(report->path, "w")) == NULL)
1437-
pg_fatal("could not open file \"%s\": %m", report->path);
1438-
if (!db_used)
1439-
{
1440-
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1441-
db_used = true;
1442-
}
1406+
if (ntups == 0)
1407+
return;
1408+
1409+
if (report->file == NULL &&
1410+
(report->file = fopen_priv(report->path, "w")) == NULL)
1411+
pg_fatal("could not open file \"%s\": %m", report->path);
14431412

1413+
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1414+
1415+
for (int rowno = 0; rowno < ntups; rowno++)
14441416
fprintf(report->file, " %s: %s\n",
14451417
PQgetvalue(res, rowno, i_objkind),
14461418
PQgetvalue(res, rowno, i_objname));
1447-
}
14481419
}
14491420

14501421
/*
@@ -1558,30 +1529,25 @@ static void
15581529
process_with_oids_check(DbInfo *dbinfo, PGresult *res, void *arg)
15591530
{
15601531
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1561-
bool db_used = false;
15621532
int ntups = PQntuples(res);
15631533
int i_nspname = PQfnumber(res, "nspname");
15641534
int i_relname = PQfnumber(res, "relname");
15651535

15661536
AssertVariableIsOfType(&process_with_oids_check, UpgradeTaskProcessCB);
15671537

1568-
if (!ntups)
1538+
if (ntups == 0)
15691539
return;
15701540

1541+
if (report->file == NULL &&
1542+
(report->file = fopen_priv(report->path, "w")) == NULL)
1543+
pg_fatal("could not open file \"%s\": %m", report->path);
1544+
1545+
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1546+
15711547
for (int rowno = 0; rowno < ntups; rowno++)
1572-
{
1573-
if (report->file == NULL &&
1574-
(report->file = fopen_priv(report->path, "w")) == NULL)
1575-
pg_fatal("could not open file \"%s\": %m", report->path);
1576-
if (!db_used)
1577-
{
1578-
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1579-
db_used = true;
1580-
}
15811548
fprintf(report->file, " %s.%s\n",
15821549
PQgetvalue(res, rowno, i_nspname),
15831550
PQgetvalue(res, rowno, i_relname));
1584-
}
15851551
}
15861552

15871553
/*
@@ -1693,7 +1659,6 @@ static void
16931659
process_user_defined_encoding_conversions(DbInfo *dbinfo, PGresult *res, void *arg)
16941660
{
16951661
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1696-
bool db_used = false;
16971662
int ntups = PQntuples(res);
16981663
int i_conoid = PQfnumber(res, "conoid");
16991664
int i_conname = PQfnumber(res, "conname");
@@ -1702,24 +1667,20 @@ process_user_defined_encoding_conversions(DbInfo *dbinfo, PGresult *res, void *a
17021667
AssertVariableIsOfType(&process_user_defined_encoding_conversions,
17031668
UpgradeTaskProcessCB);
17041669

1705-
if (!ntups)
1670+
if (ntups == 0)
17061671
return;
17071672

1673+
if (report->file == NULL &&
1674+
(report->file = fopen_priv(report->path, "w")) == NULL)
1675+
pg_fatal("could not open file \"%s\": %m", report->path);
1676+
1677+
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1678+
17081679
for (int rowno = 0; rowno < ntups; rowno++)
1709-
{
1710-
if (report->file == NULL &&
1711-
(report->file = fopen_priv(report->path, "w")) == NULL)
1712-
pg_fatal("could not open file \"%s\": %m", report->path);
1713-
if (!db_used)
1714-
{
1715-
fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1716-
db_used = true;
1717-
}
17181680
fprintf(report->file, " (oid=%s) %s.%s\n",
17191681
PQgetvalue(res, rowno, i_conoid),
17201682
PQgetvalue(res, rowno, i_nspname),
17211683
PQgetvalue(res, rowno, i_conname));
1722-
}
17231684
}
17241685

17251686
/*
@@ -1971,27 +1932,28 @@ static void
19711932
process_old_sub_state_check(DbInfo *dbinfo, PGresult *res, void *arg)
19721933
{
19731934
UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1974-
int ntup = PQntuples(res);
1935+
int ntups = PQntuples(res);
19751936
int i_srsubstate = PQfnumber(res, "srsubstate");
19761937
int i_subname = PQfnumber(res, "subname");
19771938
int i_nspname = PQfnumber(res, "nspname");
19781939
int i_relname = PQfnumber(res, "relname");
19791940

19801941
AssertVariableIsOfType(&process_old_sub_state_check, UpgradeTaskProcessCB);
19811942

1982-
for (int i = 0; i < ntup; i++)
1983-
{
1984-
if (report->file == NULL &&
1985-
(report->file = fopen_priv(report->path, "w")) == NULL)
1986-
pg_fatal("could not open file \"%s\": %m", report->path);
1943+
if (ntups == 0)
1944+
return;
19871945

1946+
if (report->file == NULL &&
1947+
(report->file = fopen_priv(report->path, "w")) == NULL)
1948+
pg_fatal("could not open file \"%s\": %m", report->path);
1949+
1950+
for (int i = 0; i < ntups; i++)
19881951
fprintf(report->file, "The table sync state \"%s\" is not allowed for database:\"%s\" subscription:\"%s\" schema:\"%s\" relation:\"%s\"\n",
19891952
PQgetvalue(res, i, i_srsubstate),
19901953
dbinfo->db_name,
19911954
PQgetvalue(res, i, i_subname),
19921955
PQgetvalue(res, i, i_nspname),
19931956
PQgetvalue(res, i, i_relname));
1994-
}
19951957
}
19961958

19971959
/*

0 commit comments

Comments
 (0)