]> granicus.if.org Git - postgresql/blobdiff - contrib/pg_upgrade/pg_upgrade.c
Update copyright for 2014
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
index c12f15b875bc7dad49ab6e2baa30f57f1b0e1c58..b992cadbc50c1ef904a32df419396a0fe8e4d850 100644 (file)
@@ -3,7 +3,7 @@
  *
  *     main source file
  *
- *     Copyright (c) 2010-2012, PostgreSQL Global Development Group
+ *     Copyright (c) 2010-2014, PostgreSQL Global Development Group
  *     contrib/pg_upgrade/pg_upgrade.c
  */
 
@@ -35,7 +35,7 @@
 
 
 
-#include "postgres.h"
+#include "postgres_fe.h"
 
 #include "pg_upgrade.h"
 
@@ -48,7 +48,7 @@ static void prepare_new_databases(void);
 static void create_new_objects(void);
 static void copy_clog_xlog_xid(void);
 static void set_frozenxids(void);
-static void setup(char *argv0, bool live_check);
+static void setup(char *argv0, bool *live_check);
 static void cleanup(void);
 
 ClusterInfo old_cluster,
@@ -61,7 +61,6 @@ char     *output_files[] = {
        /* unique file for pg_ctl start */
        SERVER_START_LOG_FILE,
 #endif
-       RESTORE_LOG_FILE,
        UTILITY_LOG_FILE,
        INTERNAL_LOG_FILE,
        NULL
@@ -81,9 +80,9 @@ main(int argc, char **argv)
        adjust_data_dir(&old_cluster);
        adjust_data_dir(&new_cluster);
 
-       output_check_banner(&live_check);
+       setup(argv[0], &live_check);
 
-       setup(argv[0], live_check);
+       output_check_banner(live_check);
 
        check_cluster_versions();
 
@@ -96,7 +95,7 @@ main(int argc, char **argv)
 
 
        /* -- NEW -- */
-       start_postmaster(&new_cluster);
+       start_postmaster(&new_cluster, true);
 
        check_new_cluster();
        report_clusters_compatible();
@@ -117,7 +116,7 @@ main(int argc, char **argv)
        /* New now using xids of the old system */
 
        /* -- NEW -- */
-       start_postmaster(&new_cluster);
+       start_postmaster(&new_cluster, true);
 
        prepare_new_databases();
 
@@ -134,8 +133,8 @@ main(int argc, char **argv)
        if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
                disable_old_cluster();
 
-       transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
-                                                old_cluster.pgdata, new_cluster.pgdata);
+       transfer_all_new_tablespaces(&old_cluster.dbarr, &new_cluster.dbarr,
+                                                                old_cluster.pgdata, new_cluster.pgdata);
 
        /*
         * Assuming OIDs are only used in system tables, there is no need to
@@ -150,6 +149,12 @@ main(int argc, char **argv)
                          new_cluster.pgdata);
        check_ok();
 
+       prep_status("Sync data directory to disk");
+       exec_prog(UTILITY_LOG_FILE, NULL, true,
+                         "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
+                         new_cluster.pgdata);
+       check_ok();
+
        create_script_for_cluster_analyze(&analyze_script_file_name);
        create_script_for_old_cluster_deletion(&deletion_script_file_name);
 
@@ -172,7 +177,7 @@ main(int argc, char **argv)
 
 
 static void
-setup(char *argv0, bool live_check)
+setup(char *argv0, bool *live_check)
 {
        char            exec_path[MAXPGPATH];   /* full path to my executable */
 
@@ -184,19 +189,42 @@ setup(char *argv0, bool live_check)
 
        verify_directories();
 
-       /* no postmasters should be running */
-       if (!live_check && is_server_running(old_cluster.pgdata))
-               pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
-                          "Please shutdown that postmaster and try again.\n");
+       /* no postmasters should be running, except for a 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 (start_postmaster(&old_cluster, false))
+                       stop_postmaster(false);
+               else
+               {
+                       if (!user_opts.check)
+                               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;
+               }
+       }
 
        /* same goes for the new postmaster */
-       if (is_server_running(new_cluster.pgdata))
-               pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
-                          "Please shutdown that postmaster and try again.\n");
+       if (pid_lock_file_exists(new_cluster.pgdata))
+       {
+               if (start_postmaster(&new_cluster, false))
+                       stop_postmaster(false);
+               else
+                       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';
@@ -264,7 +292,7 @@ prepare_new_databases(void)
         * support functions in template1 but pg_dumpall creates database using
         * the template0 template.
         */
-       exec_prog(RESTORE_LOG_FILE, NULL, true,
+       exec_prog(UTILITY_LOG_FILE, NULL, true,
                          "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
                          new_cluster.bindir, cluster_conn_opts(&new_cluster),
                          GLOBALS_DUMP_FILE);
@@ -283,9 +311,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++)
        {
@@ -301,22 +329,31 @@ create_new_objects(void)
 
        for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
        {
-               char 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_REPORT, OVERWRITE_MESSAGE, old_db->db_name);
-               snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
+               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);
 
                /*
-                *      Using pg_restore --single-transaction is faster than other
-                *      methods, like --jobs.  pg_dump only produces its output at the
-                *      end, so there is little parallelism using the pipe.
+                * pg_dump only produces its output at the end, so there is little
+                * parallelism if using the pipe.
                 */
-               exec_prog(RESTORE_LOG_FILE, NULL, true,
-                                 "\"%s/pg_restore\" %s --exit-on-error --single-transaction --verbose --dbname \"%s\" \"%s\"",
-                                 new_cluster.bindir, cluster_conn_opts(&new_cluster),
-                                 old_db->db_name, 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 */
+       while (reap_child(true) == true)
+               ;
+
        end_progress_output();
        check_ok();
 
@@ -341,7 +378,7 @@ copy_subdir_files(char *subdir)
        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);
+               pg_fatal("could not delete directory \"%s\"\n", new_path);
        check_ok();
 
        prep_status("Copying old %s to new server", subdir);
@@ -372,6 +409,53 @@ copy_clog_xlog_xid(void)
                          new_cluster.pgdata);
        check_ok();
 
+       /*
+        * 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.
+                */
+               exec_prog(UTILITY_LOG_FILE, NULL, true,
+                                 "\"%s/pg_resetxlog\" -O %u -m %u,%u \"%s\"",
+                                 new_cluster.bindir,
+                                 old_cluster.controldata.chkpnt_nxtmxoff,
+                                 old_cluster.controldata.chkpnt_nxtmulti,
+                                 old_cluster.controldata.chkpnt_oldstMulti,
+                                 new_cluster.pgdata);
+               check_ok();
+       }
+       else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
+       {
+               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
+                * that multixact.c returns the empty set for multis that might be
+                * present on disk.  We set next multi to the value following that; it
+                * might end up wrapped around (i.e. 0) if the old cluster had
+                * next=MaxMultiXactId, but multixact.c can cope with that just fine.
+                */
+               exec_prog(UTILITY_LOG_FILE, NULL, true,
+                                 "\"%s/pg_resetxlog\" -m %u,%u \"%s\"",
+                                 new_cluster.bindir,
+                                 old_cluster.controldata.chkpnt_nxtmulti + 1,
+                                 old_cluster.controldata.chkpnt_nxtmulti,
+                                 new_cluster.pgdata);
+               check_ok();
+       }
+
        /* now reset the wal archives in the new cluster */
        prep_status("Resetting WAL archives");
        exec_prog(UTILITY_LOG_FILE, NULL, true,
@@ -446,8 +530,8 @@ 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);
 
@@ -470,7 +554,6 @@ set_frozenxids(void)
 static void
 cleanup(void)
 {
-
        fclose(log_opts.internal);
 
        /* Remove dump and log files? */
@@ -488,11 +571,15 @@ cleanup(void)
                if (old_cluster.dbarr.dbs)
                        for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
                        {
-                               char 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(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
-                               unlink(file_name);
+                               snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
+                               unlink(log_file_name);
                        }
        }
 }