List offending databases in pg_upgrade datallowconn check
authorDaniel Gustafsson
Thu, 24 Mar 2022 21:41:40 +0000 (22:41 +0100)
committerDaniel Gustafsson
Thu, 24 Mar 2022 21:41:40 +0000 (22:41 +0100)
The check for datallowconn being properly set on all databases in the
old cluster errored out on the first instance, rather than report the
set of problematic databases.  This adds reporting to a textfile like
how many other checks are performed.  While there, also add a comment
to the function as per how other checks are commented.

This check won't catch if template1 isn't allowing connections, since
that's used for connecting in the first place.  That error remains as
it is today:

connection to server on socket ".." failed: FATAL:  database "template1" is not currently accepting connections

Author: Jeevan Ladhe 
Reviewed-by: Suraj Kharage
Reviewed-by: Justin Pryzby
Discussion: https://postgr.es/m/CAOgcT0McABqF_iFFQuuRf9is+gmYMsmUu_SWNikyr=2VGyP9Jw@mail.gmail.com

src/bin/pg_upgrade/check.c

index cf3b398d9e056c0613bfa400fdb9ab97a63acc80..dd3972bb6cfd5a6a73785955fc5ac7f918b2fed7 100644 (file)
@@ -682,6 +682,13 @@ check_is_install_user(ClusterInfo *cluster)
 }
 
 
+/*
+ * check_proper_datallowconn
+ *
+ * Ensure that all non-template0 databases allow connections since they
+ * otherwise won't be restored; and that template0 explicitly doesn't allow
+ * connections since it would make pg_dumpall --globals restore fail.
+ */
 static void
 check_proper_datallowconn(ClusterInfo *cluster)
 {
@@ -691,9 +698,16 @@ check_proper_datallowconn(ClusterInfo *cluster)
    int         ntups;
    int         i_datname;
    int         i_datallowconn;
+   FILE       *script = NULL;
+   char        output_path[MAXPGPATH];
+   bool        found = false;
 
    prep_status("Checking database connection settings");
 
+   snprintf(output_path, sizeof(output_path), "%s/%s",
+            log_opts.basedir,
+            "databases_with_datallowconn_false.txt");
+
    conn_template1 = connectToServer(cluster, "template1");
 
    /* get database names */
@@ -724,8 +738,14 @@ check_proper_datallowconn(ClusterInfo *cluster)
             * restore
             */
            if (strcmp(datallowconn, "f") == 0)
-               pg_fatal("All non-template0 databases must allow connections, "
-                        "i.e. their pg_database.datallowconn must be true\n");
+           {
+               found = true;
+               if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
+                   pg_fatal("could not open file \"%s\": %s\n",
+                            output_path, strerror(errno));
+
+               fprintf(script, "%s\n", datname);
+           }
        }
    }
 
@@ -733,7 +753,22 @@ check_proper_datallowconn(ClusterInfo *cluster)
 
    PQfinish(conn_template1);
 
-   check_ok();
+   if (script)
+       fclose(script);
+
+   if (found)
+   {
+       pg_log(PG_REPORT, "fatal\n");
+       pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
+                "pg_database.datallowconn must be true.  Your installation contains\n"
+                "non-template0 databases with their pg_database.datallowconn set to\n"
+                "false.  Consider allowing connection for all non-template0 databases\n"
+                "or drop the databases which do not allow connections.  A list of\n"
+                "databases with the problem is in the file:\n"
+                "    %s\n\n", output_path);
+   }
+   else
+       check_ok();
 }