]> granicus.if.org Git - postgresql/blobdiff - contrib/pg_upgrade/pg_upgrade.c
Allow CREATE/ALTER DATABASE to manipulate datistemplate and datallowconn.
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
index 55155d22e287ed2c79b544feaae6b3993e8b71d8..b32d81efe711535699d66f7a621e5a07de313b50 100644 (file)
@@ -3,7 +3,7 @@
  *
  *     main source file
  *
- *     Copyright (c) 2010-2013, PostgreSQL Global Development Group
+ *     Copyright (c) 2010-2014, PostgreSQL Global Development Group
  *     contrib/pg_upgrade/pg_upgrade.c
  */
 
  *     oids are the same between old and new clusters.  This is important
  *     because toast oids are stored as toast pointers in user tables.
  *
- *     FYI, while pg_class.oid and pg_class.relfilenode are initially the same
- *     in a cluster, but they can diverge due to CLUSTER, REINDEX, or VACUUM
- *     FULL.  The new cluster will have matching pg_class.oid and
- *     pg_class.relfilenode values and be based on the old oid value.  This can
- *     cause the old and new pg_class.relfilenode values to differ.  In summary,
- *     old and new pg_class.oid and new pg_class.relfilenode will have the
- *     same value, and old pg_class.relfilenode might differ.
+ *     While pg_class.oid and pg_class.relfilenode are initially the same
+ *     in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
+ *     FULL.  In the new cluster, pg_class.oid and pg_class.relfilenode will
+ *     be the same and will match the old pg_class.oid value.  Because of
+ *     this, old/new pg_class.relfilenode values will not match if CLUSTER,
+ *     REINDEX, or VACUUM FULL have been performed in the old cluster.
  *
  *     We control all assignments of pg_type.oid because these oids are stored
  *     in user composite type values.
@@ -35,7 +34,7 @@
 
 
 
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include "pg_upgrade.h"
 
@@ -126,7 +125,7 @@ main(int argc, char **argv)
 
        /*
         * Most failures happen in create_new_objects(), which has completed at
-        * this point.  We do this here because it is just before linking, which
+        * this point.  We do this here because it is just before linking, which
         * will link the old and new cluster data files, preventing the old
         * cluster from being safely started once the new cluster is started.
         */
@@ -134,7 +133,7 @@ main(int argc, char **argv)
                disable_old_cluster();
 
        transfer_all_new_tablespaces(&old_cluster.dbarr, &new_cluster.dbarr,
-                                                old_cluster.pgdata, new_cluster.pgdata);
+                                                                old_cluster.pgdata, new_cluster.pgdata);
 
        /*
         * Assuming OIDs are only used in system tables, there is no need to
@@ -193,21 +192,20 @@ setup(char *argv0, bool *live_check)
        if (pid_lock_file_exists(old_cluster.pgdata))
        {
                /*
-                *      If we have a postmaster.pid file, try to start the server.  If
-                *      it starts, the pid file was stale, so stop the server.  If it
-                *      doesn't start, assume the server is running.  If the pid file
-                *      is left over from a server crash, this also allows any committed
-                *      transactions stored in the WAL to be replayed so they are not
-                *      lost, because WAL files are not transfered from old to new
-                *      servers.
-                */             
+                * If we have a postmaster.pid file, try to start the server.  If it
+                * starts, the pid file was stale, so stop the server.  If it doesn't
+                * start, assume the server is running.  If the pid file is left over
+                * from a server crash, this also allows any committed transactions
+                * stored in the WAL to be replayed so they are not lost, because WAL
+                * files are not transfered from old to new servers.
+                */
                if (start_postmaster(&old_cluster, false))
                        stop_postmaster(false);
                else
                {
                        if (!user_opts.check)
-                               pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
-                                          "Please shutdown that postmaster and try again.\n");
+                               pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
+                                                "Please shutdown that postmaster and try again.\n");
                        else
                                *live_check = true;
                }
@@ -219,13 +217,13 @@ setup(char *argv0, bool *live_check)
                if (start_postmaster(&new_cluster, false))
                        stop_postmaster(false);
                else
-                       pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
-                          "Please shutdown that postmaster and try again.\n");
+                       pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
+                                        "Please shutdown that postmaster and try again.\n");
        }
 
        /* get path to pg_upgrade executable */
        if (find_my_exec(argv0, exec_path) < 0)
-               pg_log(PG_FATAL, "Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
+               pg_fatal("Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
 
        /* Trim off program name and keep just path */
        *last_dir_separator(exec_path) = '\0';
@@ -281,8 +279,8 @@ prepare_new_databases(void)
 
        /*
         * Install support functions in the global-object restore database to
-        * preserve pg_authid.oid.      pg_dumpall uses 'template0' as its template
-        * database so objects we add into 'template1' are not propogated.      They
+        * preserve pg_authid.oid.  pg_dumpall uses 'template0' as its template
+        * database so objects we add into 'template1' are not propogated.  They
         * are removed on pg_upgrade exit.
         */
        install_support_functions_in_new_db("template1");
@@ -312,9 +310,9 @@ create_new_objects(void)
        prep_status("Adding support functions to new cluster");
 
        /*
-        *      Technically, we only need to install these support functions in new
-        *      databases that also exist in the old cluster, but for completeness
-        *      we process all new databases.
+        * Technically, we only need to install these support functions in new
+        * databases that also exist in the old cluster, but for completeness we
+        * process all new databases.
         */
        for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
        {
@@ -330,21 +328,25 @@ create_new_objects(void)
 
        for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
        {
-               char sql_file_name[MAXPGPATH], log_file_name[MAXPGPATH];
-               DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
+               char            sql_file_name[MAXPGPATH],
+                                       log_file_name[MAXPGPATH];
+               DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
 
                pg_log(PG_STATUS, "%s", old_db->db_name);
                snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
                snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
 
                /*
-                *      pg_dump only produces its output at the end, so there is little
-                *      parallelism if using the pipe.
+                * pg_dump only produces its output at the end, so there is little
+                * parallelism if using the pipe.
                 */
-               parallel_exec_prog(log_file_name, NULL,
-                                 "\"%s/pg_restore\" %s --exit-on-error --verbose --dbname \"%s\" \"%s\"",
-                                 new_cluster.bindir, cluster_conn_opts(&new_cluster),
-                                 old_db->db_name, sql_file_name);
+               parallel_exec_prog(log_file_name,
+                                                  NULL,
+                                                  "\"%s/pg_restore\" %s --exit-on-error --verbose --dbname \"%s\" \"%s\"",
+                                                  new_cluster.bindir,
+                                                  cluster_conn_opts(&new_cluster),
+                                                  old_db->db_name,
+                                                  sql_file_name);
        }
 
        /* reap all children */
@@ -361,8 +363,24 @@ create_new_objects(void)
 }
 
 /*
- * Delete the given subdirectory contents from the new cluster, and copy the
- * files from the old cluster into it.
+ * Delete the given subdirectory contents from the new cluster
+ */
+static void
+remove_new_subdir(char *subdir, bool rmtopdir)
+{
+       char            new_path[MAXPGPATH];
+
+       prep_status("Deleting files from new %s", subdir);
+
+       snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
+       if (!rmtree(new_path, rmtopdir))
+               pg_fatal("could not delete directory \"%s\"\n", new_path);
+
+       check_ok();
+}
+
+/*
+ * Copy the files from the old cluster into it
  */
 static void
 copy_subdir_files(char *subdir)
@@ -370,13 +388,10 @@ copy_subdir_files(char *subdir)
        char            old_path[MAXPGPATH];
        char            new_path[MAXPGPATH];
 
-       prep_status("Deleting files from new %s", subdir);
+       remove_new_subdir(subdir, true);
 
        snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
        snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
-       if (!rmtree(new_path, true))
-               pg_log(PG_FATAL, "could not delete directory \"%s\"\n", new_path);
-       check_ok();
 
        prep_status("Copying old %s to new server", subdir);
 
@@ -407,18 +422,19 @@ copy_clog_xlog_xid(void)
        check_ok();
 
        /*
-        * If both new and old are after the pg_multixact change commit, copy those
-        * files too.  If the old server is before that change and the new server
-        * is after, then we don't copy anything but we need to reset pg_control so
-        * that the new server doesn't attempt to read multis older than the cutoff
-        * value.
+        * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
+        * (see pg_upgrade.h) and the new server is after, then we don't copy
+        * pg_multixact files, but we need to reset pg_control so that the new
+        * server doesn't attempt to read multis older than the cutoff value.
         */
        if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
                new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
        {
                copy_subdir_files("pg_multixact/offsets");
                copy_subdir_files("pg_multixact/members");
+
                prep_status("Setting next multixact ID and offset for new cluster");
+
                /*
                 * we preserve all files and contents, so we must preserve both "next"
                 * counters here and the oldest multi present on system.
@@ -434,7 +450,15 @@ copy_clog_xlog_xid(void)
        }
        else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
        {
+               /*
+                * Remove files created by initdb that no longer match the
+                * new multi-xid value.
+                */
+               remove_new_subdir("pg_multixact/offsets", false);
+               remove_new_subdir("pg_multixact/members", false);
+
                prep_status("Setting oldest multixact ID on new cluster");
+
                /*
                 * We don't preserve files in this case, but it's important that the
                 * oldest multi is set to the latest value used by the old system, so
@@ -467,7 +491,7 @@ copy_clog_xlog_xid(void)
  *
  *     We have frozen all xids, so set relfrozenxid and datfrozenxid
  *     to be the old cluster's xid counter, which we just set in the new
- *     cluster.  User-table frozenxid values will be set by pg_dumpall
+ *     cluster.  User-table frozenxid values will be set by pg_dump
  *     --binary-upgrade, but objects not set by the pg_dump must have
  *     proper frozen counters.
  */
@@ -516,9 +540,8 @@ set_frozenxids(void)
                 */
                if (strcmp(datallowconn, "f") == 0)
                        PQclear(executeQueryOrDie(conn_template1,
-                                                                         "UPDATE pg_catalog.pg_database "
-                                                                         "SET  datallowconn = true "
-                                                                         "WHERE datname = '%s'", datname));
+                                                               "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
+                                                                         quote_identifier(datname)));
 
                conn = connectToServer(&new_cluster, datname);
 
@@ -526,17 +549,16 @@ set_frozenxids(void)
                PQclear(executeQueryOrDie(conn,
                                                                  "UPDATE       pg_catalog.pg_class "
                                                                  "SET  relfrozenxid = '%u' "
-               /* only heap and TOAST are vacuumed */
-                                                                 "WHERE        relkind IN ('r', 't')",
+               /* only heap, materialized view, and TOAST are vacuumed */
+                                                                 "WHERE        relkind IN ('r', 'm', 't')",
                                                                  old_cluster.controldata.chkpnt_nxtxid));
                PQfinish(conn);
 
                /* Reset datallowconn flag */
                if (strcmp(datallowconn, "f") == 0)
                        PQclear(executeQueryOrDie(conn_template1,
-                                                                         "UPDATE pg_catalog.pg_database "
-                                                                         "SET  datallowconn = false "
-                                                                         "WHERE datname = '%s'", datname));
+                                                          "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
+                                                                         quote_identifier(datname)));
        }
 
        PQclear(dbres);
@@ -550,7 +572,6 @@ set_frozenxids(void)
 static void
 cleanup(void)
 {
-
        fclose(log_opts.internal);
 
        /* Remove dump and log files? */
@@ -568,13 +589,14 @@ cleanup(void)
                if (old_cluster.dbarr.dbs)
                        for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
                        {
-                               char sql_file_name[MAXPGPATH], log_file_name[MAXPGPATH];
-                               DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
+                               char            sql_file_name[MAXPGPATH],
+                                                       log_file_name[MAXPGPATH];
+                               DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
 
                                snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
                                unlink(sql_file_name);
 
-                               snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
+                               snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
                                unlink(log_file_name);
                        }
        }