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

Commit f638aaf

Browse files
Find invalid databases during upgrade check stage
Before continuing with the check start by checking that all databases allow connections to avoid a hard fail without proper error reporting. Inspired by a larger patch by Thomas Krennwallner. Discussion: https://postgr.es/m/f9315bf0-e03e-4490-9f0d-5b6f7a6d9908@postsubmeta.net
1 parent 1e37cc6 commit f638aaf

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
static void check_new_cluster_is_empty(void);
1818
static void check_is_install_user(ClusterInfo *cluster);
19-
static void check_proper_datallowconn(ClusterInfo *cluster);
19+
static void check_for_connection_status(ClusterInfo *cluster);
2020
static void check_for_prepared_transactions(ClusterInfo *cluster);
2121
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2222
static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
@@ -590,6 +590,12 @@ check_and_dump_old_cluster(void)
590590
if (!user_opts.live_check)
591591
start_postmaster(&old_cluster, true);
592592

593+
/*
594+
* First check that all databases allow connections since we'll otherwise
595+
* fail in later stages.
596+
*/
597+
check_for_connection_status(&old_cluster);
598+
593599
/*
594600
* Extract a list of databases, tables, and logical replication slots from
595601
* the old cluster.
@@ -605,7 +611,6 @@ check_and_dump_old_cluster(void)
605611
* Check for various failure cases
606612
*/
607613
check_is_install_user(&old_cluster);
608-
check_proper_datallowconn(&old_cluster);
609614
check_for_prepared_transactions(&old_cluster);
610615
check_for_isn_and_int8_passing_mismatch(&old_cluster);
611616

@@ -1087,45 +1092,48 @@ check_is_install_user(ClusterInfo *cluster)
10871092

10881093

10891094
/*
1090-
* check_proper_datallowconn
1095+
* check_for_connection_status
10911096
*
10921097
* Ensure that all non-template0 databases allow connections since they
10931098
* otherwise won't be restored; and that template0 explicitly doesn't allow
10941099
* connections since it would make pg_dumpall --globals restore fail.
10951100
*/
10961101
static void
1097-
check_proper_datallowconn(ClusterInfo *cluster)
1102+
check_for_connection_status(ClusterInfo *cluster)
10981103
{
10991104
int dbnum;
11001105
PGconn *conn_template1;
11011106
PGresult *dbres;
11021107
int ntups;
11031108
int i_datname;
11041109
int i_datallowconn;
1110+
int i_datconnlimit;
11051111
FILE *script = NULL;
11061112
char output_path[MAXPGPATH];
11071113

11081114
prep_status("Checking database connection settings");
11091115

11101116
snprintf(output_path, sizeof(output_path), "%s/%s",
11111117
log_opts.basedir,
1112-
"databases_with_datallowconn_false.txt");
1118+
"databases_cannot_connect_to.txt");
11131119

11141120
conn_template1 = connectToServer(cluster, "template1");
11151121

11161122
/* get database names */
11171123
dbres = executeQueryOrDie(conn_template1,
1118-
"SELECT datname, datallowconn "
1124+
"SELECT datname, datallowconn, datconnlimit "
11191125
"FROM pg_catalog.pg_database");
11201126

11211127
i_datname = PQfnumber(dbres, "datname");
11221128
i_datallowconn = PQfnumber(dbres, "datallowconn");
1129+
i_datconnlimit = PQfnumber(dbres, "datconnlimit");
11231130

11241131
ntups = PQntuples(dbres);
11251132
for (dbnum = 0; dbnum < ntups; dbnum++)
11261133
{
11271134
char *datname = PQgetvalue(dbres, dbnum, i_datname);
11281135
char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
1136+
char *datconnlimit = PQgetvalue(dbres, dbnum, i_datconnlimit);
11291137

11301138
if (strcmp(datname, "template0") == 0)
11311139
{
@@ -1137,10 +1145,11 @@ check_proper_datallowconn(ClusterInfo *cluster)
11371145
else
11381146
{
11391147
/*
1140-
* avoid datallowconn == false databases from being skipped on
1141-
* restore
1148+
* Avoid datallowconn == false databases from being skipped on
1149+
* restore, and ensure that no databases are marked invalid with
1150+
* datconnlimit == -2.
11421151
*/
1143-
if (strcmp(datallowconn, "f") == 0)
1152+
if ((strcmp(datallowconn, "f") == 0) || strcmp(datconnlimit, "-2") == 0)
11441153
{
11451154
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
11461155
pg_fatal("could not open file \"%s\": %m", output_path);
@@ -1159,11 +1168,11 @@ check_proper_datallowconn(ClusterInfo *cluster)
11591168
fclose(script);
11601169
pg_log(PG_REPORT, "fatal");
11611170
pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
1162-
"pg_database.datallowconn must be true. Your installation contains\n"
1163-
"non-template0 databases with their pg_database.datallowconn set to\n"
1164-
"false. Consider allowing connection for all non-template0 databases\n"
1165-
"or drop the databases which do not allow connections. A list of\n"
1166-
"databases with the problem is in the file:\n"
1171+
"pg_database.datallowconn must be true and pg_database.datconnlimit\n"
1172+
"must not be -2. Your installation contains non-template0 databases\n"
1173+
"which cannot be connected to. Consider allowing connection for all\n"
1174+
"non-template0 databases or drop the databases which do not allow\n"
1175+
"connections. A list of databases with the problem is in the file:\n"
11671176
" %s", output_path);
11681177
}
11691178
else

src/bin/pg_upgrade/t/002_pg_upgrade.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ sub filter_dump
424424
$mode, '--check',
425425
],
426426
1,
427-
[qr/invalid/], # pg_upgrade prints errors on stdout :(
427+
[qr/datconnlimit/],
428428
[qr/^$/],
429429
'invalid database causes failure');
430430
rmtree($newnode->data_dir . "/pg_upgrade_output.d");

0 commit comments

Comments
 (0)