Skip to content

Commit fb694d9

Browse files
committed
pg_upgrade: only allow template0 to be non-connectable
This patch causes pg_upgrade to error out during its check phase if: (1) template0 is marked connectable or (2) any other database is marked non-connectable This is done because, in the first case, pg_upgrade would fail because the pg_dumpall --globals restore would fail, and in the second case, the database would not be restored, leading to data loss. Report by Matt Landry (1), Stephen Frost (2) Backpatch through 9.0
1 parent 12cc299 commit fb694d9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static void check_databases_are_compatible(void);
1919
static void check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb);
2020
static bool equivalent_locale(int category, const char *loca, const char *locb);
2121
static void check_is_install_user(ClusterInfo *cluster);
22+
static void check_proper_datallowconn(ClusterInfo *cluster);
2223
static void check_for_prepared_transactions(ClusterInfo *cluster);
2324
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
2425
static void check_for_reg_data_type_usage(ClusterInfo *cluster);
@@ -93,6 +94,7 @@ check_and_dump_old_cluster(bool live_check)
9394
* Check for various failure cases
9495
*/
9596
check_is_install_user(&old_cluster);
97+
check_proper_datallowconn(&old_cluster);
9698
check_for_prepared_transactions(&old_cluster);
9799
check_for_reg_data_type_usage(&old_cluster);
98100
check_for_isn_and_int8_passing_mismatch(&old_cluster);
@@ -642,6 +644,58 @@ check_is_install_user(ClusterInfo *cluster)
642644
}
643645

644646

647+
static void
648+
check_proper_datallowconn(ClusterInfo *cluster)
649+
{
650+
int dbnum;
651+
PGconn *conn_template1;
652+
PGresult *dbres;
653+
int ntups;
654+
int i_datname;
655+
int i_datallowconn;
656+
657+
prep_status("Checking database connection settings");
658+
659+
conn_template1 = connectToServer(cluster, "template1");
660+
661+
/* get database names */
662+
dbres = executeQueryOrDie(conn_template1,
663+
"SELECT datname, datallowconn "
664+
"FROM pg_catalog.pg_database");
665+
666+
i_datname = PQfnumber(dbres, "datname");
667+
i_datallowconn = PQfnumber(dbres, "datallowconn");
668+
669+
ntups = PQntuples(dbres);
670+
for (dbnum = 0; dbnum < ntups; dbnum++)
671+
{
672+
char *datname = PQgetvalue(dbres, dbnum, i_datname);
673+
char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
674+
675+
if (strcmp(datname, "template0") == 0)
676+
{
677+
/* avoid restore failure when pg_dumpall tries to create template0 */
678+
if (strcmp(datallowconn, "t") == 0)
679+
pg_fatal("template0 must not allow connections, "
680+
"i.e. its pg_database.datallowconn must be false\n");
681+
}
682+
else
683+
{
684+
/* avoid datallowconn == false databases from being skipped on restore */
685+
if (strcmp(datallowconn, "f") == 0)
686+
pg_fatal("All non-template0 databases must allow connections, "
687+
"i.e. their pg_database.datallowconn must be true\n");
688+
}
689+
}
690+
691+
PQclear(dbres);
692+
693+
PQfinish(conn_template1);
694+
695+
check_ok();
696+
}
697+
698+
645699
/*
646700
* check_for_prepared_transactions()
647701
*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy