LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - pg_upgrade.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 191 232 82.3 %
Date: 2022-12-05 11:11:09 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  pg_upgrade.c
       3             :  *
       4             :  *  main source file
       5             :  *
       6             :  *  Copyright (c) 2010-2022, PostgreSQL Global Development Group
       7             :  *  src/bin/pg_upgrade/pg_upgrade.c
       8             :  */
       9             : 
      10             : /*
      11             :  *  To simplify the upgrade process, we force certain system values to be
      12             :  *  identical between old and new clusters:
      13             :  *
      14             :  *  We control all assignments of pg_class.oid (and relfilenode) so toast
      15             :  *  oids are the same between old and new clusters.  This is important
      16             :  *  because toast oids are stored as toast pointers in user tables.
      17             :  *
      18             :  *  While pg_class.oid and pg_class.relfilenode are initially the same in a
      19             :  *  cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
      20             :  *  control assignments of pg_class.relfilenode because we want the filenames
      21             :  *  to match between the old and new cluster.
      22             :  *
      23             :  *  We control assignment of pg_tablespace.oid because we want the oid to match
      24             :  *  between the old and new cluster.
      25             :  *
      26             :  *  We control all assignments of pg_type.oid because these oids are stored
      27             :  *  in user composite type values.
      28             :  *
      29             :  *  We control all assignments of pg_enum.oid because these oids are stored
      30             :  *  in user tables as enum values.
      31             :  *
      32             :  *  We control all assignments of pg_authid.oid for historical reasons (the
      33             :  *  oids used to be stored in pg_largeobject_metadata, which is now copied via
      34             :  *  SQL commands), that might change at some point in the future.
      35             :  */
      36             : 
      37             : 
      38             : 
      39             : #include "postgres_fe.h"
      40             : 
      41             : #include <time.h>
      42             : 
      43             : #ifdef HAVE_LANGINFO_H
      44             : #include <langinfo.h>
      45             : #endif
      46             : 
      47             : #include "catalog/pg_class_d.h"
      48             : #include "common/file_perm.h"
      49             : #include "common/logging.h"
      50             : #include "common/restricted_token.h"
      51             : #include "fe_utils/string_utils.h"
      52             : #include "pg_upgrade.h"
      53             : 
      54             : static void prepare_new_cluster(void);
      55             : static void prepare_new_globals(void);
      56             : static void create_new_objects(void);
      57             : static void copy_xact_xlog_xid(void);
      58             : static void set_frozenxids(bool minmxid_only);
      59             : static void make_outputdirs(char *pgdata);
      60             : static void setup(char *argv0, bool *live_check);
      61             : 
      62             : ClusterInfo old_cluster,
      63             :             new_cluster;
      64             : OSInfo      os_info;
      65             : 
      66             : char       *output_files[] = {
      67             :     SERVER_LOG_FILE,
      68             : #ifdef WIN32
      69             :     /* unique file for pg_ctl start */
      70             :     SERVER_START_LOG_FILE,
      71             : #endif
      72             :     UTILITY_LOG_FILE,
      73             :     INTERNAL_LOG_FILE,
      74             :     NULL
      75             : };
      76             : 
      77             : 
      78             : int
      79          12 : main(int argc, char **argv)
      80             : {
      81          12 :     char       *deletion_script_file_name = NULL;
      82          12 :     bool        live_check = false;
      83             : 
      84             :     /*
      85             :      * pg_upgrade doesn't currently use common/logging.c, but initialize it
      86             :      * anyway because we might call common code that does.
      87             :      */
      88          12 :     pg_logging_init(argv[0]);
      89          12 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
      90             : 
      91             :     /* Set default restrictive mask until new cluster permissions are read */
      92          12 :     umask(PG_MODE_MASK_OWNER);
      93             : 
      94          12 :     parseCommandLine(argc, argv);
      95             : 
      96           6 :     get_restricted_token();
      97             : 
      98           6 :     adjust_data_dir(&old_cluster);
      99           6 :     adjust_data_dir(&new_cluster);
     100             : 
     101             :     /*
     102             :      * Set mask based on PGDATA permissions, needed for the creation of the
     103             :      * output directories with correct permissions.
     104             :      */
     105           6 :     if (!GetDataDirectoryCreatePerm(new_cluster.pgdata))
     106           0 :         pg_fatal("could not read permissions of directory \"%s\": %s",
     107           0 :                  new_cluster.pgdata, strerror(errno));
     108             : 
     109           6 :     umask(pg_mode_mask);
     110             : 
     111             :     /*
     112             :      * This needs to happen after adjusting the data directory of the new
     113             :      * cluster in adjust_data_dir().
     114             :      */
     115           6 :     make_outputdirs(new_cluster.pgdata);
     116             : 
     117           6 :     setup(argv[0], &live_check);
     118             : 
     119           4 :     output_check_banner(live_check);
     120             : 
     121           4 :     check_cluster_versions();
     122             : 
     123           4 :     get_sock_dir(&old_cluster, live_check);
     124           4 :     get_sock_dir(&new_cluster, false);
     125             : 
     126           4 :     check_cluster_compatibility(live_check);
     127             : 
     128           4 :     check_and_dump_old_cluster(live_check);
     129             : 
     130             : 
     131             :     /* -- NEW -- */
     132           4 :     start_postmaster(&new_cluster, true);
     133             : 
     134           4 :     check_new_cluster();
     135           4 :     report_clusters_compatible();
     136             : 
     137           2 :     pg_log(PG_REPORT,
     138             :            "\n"
     139             :            "Performing Upgrade\n"
     140             :            "------------------");
     141             : 
     142           2 :     prepare_new_cluster();
     143             : 
     144           2 :     stop_postmaster(false);
     145             : 
     146             :     /*
     147             :      * Destructive Changes to New Cluster
     148             :      */
     149             : 
     150           2 :     copy_xact_xlog_xid();
     151             : 
     152             :     /* New now using xids of the old system */
     153             : 
     154             :     /* -- NEW -- */
     155           2 :     start_postmaster(&new_cluster, true);
     156             : 
     157           2 :     prepare_new_globals();
     158             : 
     159           2 :     create_new_objects();
     160             : 
     161           2 :     stop_postmaster(false);
     162             : 
     163             :     /*
     164             :      * Most failures happen in create_new_objects(), which has completed at
     165             :      * this point.  We do this here because it is just before linking, which
     166             :      * will link the old and new cluster data files, preventing the old
     167             :      * cluster from being safely started once the new cluster is started.
     168             :      */
     169           2 :     if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
     170           0 :         disable_old_cluster();
     171             : 
     172           2 :     transfer_all_new_tablespaces(&old_cluster.dbarr, &new_cluster.dbarr,
     173             :                                  old_cluster.pgdata, new_cluster.pgdata);
     174             : 
     175             :     /*
     176             :      * Assuming OIDs are only used in system tables, there is no need to
     177             :      * restore the OID counter because we have not transferred any OIDs from
     178             :      * the old system, but we do it anyway just in case.  We do it late here
     179             :      * because there is no need to have the schema load use new oids.
     180             :      */
     181           2 :     prep_status("Setting next OID for new cluster");
     182           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     183             :               "\"%s/pg_resetwal\" -o %u \"%s\"",
     184             :               new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid,
     185             :               new_cluster.pgdata);
     186           2 :     check_ok();
     187             : 
     188           2 :     if (user_opts.do_sync)
     189             :     {
     190           0 :         prep_status("Sync data directory to disk");
     191           0 :         exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     192             :                   "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
     193             :                   new_cluster.pgdata);
     194           0 :         check_ok();
     195             :     }
     196             : 
     197           2 :     create_script_for_old_cluster_deletion(&deletion_script_file_name);
     198             : 
     199           2 :     issue_warnings_and_set_wal_level();
     200             : 
     201           2 :     pg_log(PG_REPORT,
     202             :            "\n"
     203             :            "Upgrade Complete\n"
     204             :            "----------------");
     205             : 
     206           2 :     output_completion_banner(deletion_script_file_name);
     207             : 
     208           2 :     pg_free(deletion_script_file_name);
     209             : 
     210           2 :     cleanup_output_dirs();
     211             : 
     212           2 :     return 0;
     213             : }
     214             : 
     215             : /*
     216             :  * Create and assign proper permissions to the set of output directories
     217             :  * used to store any data generated internally, filling in log_opts in
     218             :  * the process.
     219             :  */
     220             : static void
     221           6 : make_outputdirs(char *pgdata)
     222             : {
     223             :     FILE       *fp;
     224             :     char      **filename;
     225           6 :     time_t      run_time = time(NULL);
     226             :     char        filename_path[MAXPGPATH];
     227             :     char        timebuf[128];
     228             :     struct timeval time;
     229             :     time_t      tt;
     230             :     int         len;
     231             : 
     232           6 :     log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
     233           6 :     len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
     234           6 :     if (len >= MAXPGPATH)
     235           0 :         pg_fatal("directory path for new cluster is too long");
     236             : 
     237             :     /* BASE_OUTPUTDIR/$timestamp/ */
     238           6 :     gettimeofday(&time, NULL);
     239           6 :     tt = (time_t) time.tv_sec;
     240           6 :     strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
     241             :     /* append milliseconds */
     242           6 :     snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
     243           6 :              ".%03d", (int) (time.tv_usec / 1000));
     244           6 :     log_opts.basedir = (char *) pg_malloc0(MAXPGPATH);
     245           6 :     len = snprintf(log_opts.basedir, MAXPGPATH, "%s/%s", log_opts.rootdir,
     246             :                    timebuf);
     247           6 :     if (len >= MAXPGPATH)
     248           0 :         pg_fatal("directory path for new cluster is too long");
     249             : 
     250             :     /* BASE_OUTPUTDIR/$timestamp/dump/ */
     251           6 :     log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
     252           6 :     len = snprintf(log_opts.dumpdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
     253             :                    timebuf, DUMP_OUTPUTDIR);
     254           6 :     if (len >= MAXPGPATH)
     255           0 :         pg_fatal("directory path for new cluster is too long");
     256             : 
     257             :     /* BASE_OUTPUTDIR/$timestamp/log/ */
     258           6 :     log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
     259           6 :     len = snprintf(log_opts.logdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
     260             :                    timebuf, LOG_OUTPUTDIR);
     261           6 :     if (len >= MAXPGPATH)
     262           0 :         pg_fatal("directory path for new cluster is too long");
     263             : 
     264             :     /*
     265             :      * Ignore the error case where the root path exists, as it is kept the
     266             :      * same across runs.
     267             :      */
     268           6 :     if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
     269           0 :         pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
     270           6 :     if (mkdir(log_opts.basedir, pg_dir_create_mode) < 0)
     271           0 :         pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
     272           6 :     if (mkdir(log_opts.dumpdir, pg_dir_create_mode) < 0)
     273           0 :         pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
     274           6 :     if (mkdir(log_opts.logdir, pg_dir_create_mode) < 0)
     275           0 :         pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
     276             : 
     277           6 :     len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
     278             :                    log_opts.logdir, INTERNAL_LOG_FILE);
     279           6 :     if (len >= sizeof(filename_path))
     280           0 :         pg_fatal("directory path for new cluster is too long");
     281             : 
     282           6 :     if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
     283           0 :         pg_fatal("could not open log file \"%s\": %m", filename_path);
     284             : 
     285             :     /* label start of upgrade in logfiles */
     286          24 :     for (filename = output_files; *filename != NULL; filename++)
     287             :     {
     288          18 :         len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
     289             :                        log_opts.logdir, *filename);
     290          18 :         if (len >= sizeof(filename_path))
     291           0 :             pg_fatal("directory path for new cluster is too long");
     292          18 :         if ((fp = fopen_priv(filename_path, "a")) == NULL)
     293           0 :             pg_fatal("could not write to log file \"%s\": %m", filename_path);
     294             : 
     295          18 :         fprintf(fp,
     296             :                 "-----------------------------------------------------------------\n"
     297             :                 "  pg_upgrade run on %s"
     298             :                 "-----------------------------------------------------------------\n\n",
     299             :                 ctime(&run_time));
     300          18 :         fclose(fp);
     301             :     }
     302           6 : }
     303             : 
     304             : 
     305             : static void
     306           6 : setup(char *argv0, bool *live_check)
     307             : {
     308             :     /*
     309             :      * make sure the user has a clean environment, otherwise, we may confuse
     310             :      * libpq when we connect to one (or both) of the servers.
     311             :      */
     312           6 :     check_pghost_envvar();
     313             : 
     314             :     /*
     315             :      * In case the user hasn't specified the directory for the new binaries
     316             :      * with -B, default to using the path of the currently executed pg_upgrade
     317             :      * binary.
     318             :      */
     319           6 :     if (!new_cluster.bindir)
     320             :     {
     321             :         char        exec_path[MAXPGPATH];
     322             : 
     323           0 :         if (find_my_exec(argv0, exec_path) < 0)
     324           0 :             pg_fatal("%s: could not find own program executable", argv0);
     325             :         /* Trim off program name and keep just path */
     326           0 :         *last_dir_separator(exec_path) = '\0';
     327           0 :         canonicalize_path(exec_path);
     328           0 :         new_cluster.bindir = pg_strdup(exec_path);
     329             :     }
     330             : 
     331           6 :     verify_directories();
     332             : 
     333             :     /* no postmasters should be running, except for a live check */
     334           4 :     if (pid_lock_file_exists(old_cluster.pgdata))
     335             :     {
     336             :         /*
     337             :          * If we have a postmaster.pid file, try to start the server.  If it
     338             :          * starts, the pid file was stale, so stop the server.  If it doesn't
     339             :          * start, assume the server is running.  If the pid file is left over
     340             :          * from a server crash, this also allows any committed transactions
     341             :          * stored in the WAL to be replayed so they are not lost, because WAL
     342             :          * files are not transferred from old to new servers.  We later check
     343             :          * for a clean shutdown.
     344             :          */
     345           0 :         if (start_postmaster(&old_cluster, false))
     346           0 :             stop_postmaster(false);
     347             :         else
     348             :         {
     349           0 :             if (!user_opts.check)
     350           0 :                 pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
     351             :                          "Please shutdown that postmaster and try again.");
     352             :             else
     353           0 :                 *live_check = true;
     354             :         }
     355             :     }
     356             : 
     357             :     /* same goes for the new postmaster */
     358           4 :     if (pid_lock_file_exists(new_cluster.pgdata))
     359             :     {
     360           0 :         if (start_postmaster(&new_cluster, false))
     361           0 :             stop_postmaster(false);
     362             :         else
     363           0 :             pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
     364             :                      "Please shutdown that postmaster and try again.");
     365             :     }
     366           4 : }
     367             : 
     368             : 
     369             : static void
     370           2 : prepare_new_cluster(void)
     371             : {
     372             :     /*
     373             :      * It would make more sense to freeze after loading the schema, but that
     374             :      * would cause us to lose the frozenxids restored by the load. We use
     375             :      * --analyze so autovacuum doesn't update statistics later
     376             :      */
     377           2 :     prep_status("Analyzing all rows in the new cluster");
     378           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     379             :               "\"%s/vacuumdb\" %s --all --analyze %s",
     380             :               new_cluster.bindir, cluster_conn_opts(&new_cluster),
     381           2 :               log_opts.verbose ? "--verbose" : "");
     382           2 :     check_ok();
     383             : 
     384             :     /*
     385             :      * We do freeze after analyze so pg_statistic is also frozen. template0 is
     386             :      * not frozen here, but data rows were frozen by initdb, and we set its
     387             :      * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
     388             :      * counter later.
     389             :      */
     390           2 :     prep_status("Freezing all rows in the new cluster");
     391           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     392             :               "\"%s/vacuumdb\" %s --all --freeze %s",
     393             :               new_cluster.bindir, cluster_conn_opts(&new_cluster),
     394           2 :               log_opts.verbose ? "--verbose" : "");
     395           2 :     check_ok();
     396           2 : }
     397             : 
     398             : 
     399             : static void
     400           2 : prepare_new_globals(void)
     401             : {
     402             :     /*
     403             :      * Before we restore anything, set frozenxids of initdb-created tables.
     404             :      */
     405           2 :     set_frozenxids(false);
     406             : 
     407             :     /*
     408             :      * Now restore global objects (roles and tablespaces).
     409             :      */
     410           2 :     prep_status("Restoring global objects in the new cluster");
     411             : 
     412           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     413             :               "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
     414             :               new_cluster.bindir, cluster_conn_opts(&new_cluster),
     415             :               log_opts.dumpdir,
     416             :               GLOBALS_DUMP_FILE);
     417           2 :     check_ok();
     418           2 : }
     419             : 
     420             : 
     421             : static void
     422           2 : create_new_objects(void)
     423             : {
     424             :     int         dbnum;
     425             : 
     426           2 :     prep_status_progress("Restoring database schemas in the new cluster");
     427             : 
     428             :     /*
     429             :      * We cannot process the template1 database concurrently with others,
     430             :      * because when it's transiently dropped, connection attempts would fail.
     431             :      * So handle it in a separate non-parallelized pass.
     432             :      */
     433           2 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
     434             :     {
     435             :         char        sql_file_name[MAXPGPATH],
     436             :                     log_file_name[MAXPGPATH];
     437           2 :         DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
     438             :         const char *create_opts;
     439             : 
     440             :         /* Process only template1 in this pass */
     441           2 :         if (strcmp(old_db->db_name, "template1") != 0)
     442           0 :             continue;
     443             : 
     444           2 :         pg_log(PG_STATUS, "%s", old_db->db_name);
     445           2 :         snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
     446           2 :         snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
     447             : 
     448             :         /*
     449             :          * template1 database will already exist in the target installation,
     450             :          * so tell pg_restore to drop and recreate it; otherwise we would fail
     451             :          * to propagate its database-level properties.
     452             :          */
     453           2 :         create_opts = "--clean --create";
     454             : 
     455           2 :         exec_prog(log_file_name,
     456             :                   NULL,
     457             :                   true,
     458             :                   true,
     459             :                   "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
     460             :                   "--dbname postgres \"%s/%s\"",
     461             :                   new_cluster.bindir,
     462             :                   cluster_conn_opts(&new_cluster),
     463             :                   create_opts,
     464             :                   log_opts.dumpdir,
     465             :                   sql_file_name);
     466             : 
     467           2 :         break;                  /* done once we've processed template1 */
     468             :     }
     469             : 
     470          14 :     for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
     471             :     {
     472             :         char        sql_file_name[MAXPGPATH],
     473             :                     log_file_name[MAXPGPATH];
     474          12 :         DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
     475             :         const char *create_opts;
     476             : 
     477             :         /* Skip template1 in this pass */
     478          12 :         if (strcmp(old_db->db_name, "template1") == 0)
     479           2 :             continue;
     480             : 
     481          10 :         pg_log(PG_STATUS, "%s", old_db->db_name);
     482          10 :         snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
     483          10 :         snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
     484             : 
     485             :         /*
     486             :          * postgres database will already exist in the target installation, so
     487             :          * tell pg_restore to drop and recreate it; otherwise we would fail to
     488             :          * propagate its database-level properties.
     489             :          */
     490          10 :         if (strcmp(old_db->db_name, "postgres") == 0)
     491           2 :             create_opts = "--clean --create";
     492             :         else
     493           8 :             create_opts = "--create";
     494             : 
     495          10 :         parallel_exec_prog(log_file_name,
     496             :                            NULL,
     497             :                            "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
     498             :                            "--dbname template1 \"%s/%s\"",
     499             :                            new_cluster.bindir,
     500             :                            cluster_conn_opts(&new_cluster),
     501             :                            create_opts,
     502             :                            log_opts.dumpdir,
     503             :                            sql_file_name);
     504             :     }
     505             : 
     506             :     /* reap all children */
     507           2 :     while (reap_child(true) == true)
     508             :         ;
     509             : 
     510           2 :     end_progress_output();
     511           2 :     check_ok();
     512             : 
     513             :     /*
     514             :      * We don't have minmxids for databases or relations in pre-9.3 clusters,
     515             :      * so set those after we have restored the schema.
     516             :      */
     517           2 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 902)
     518           0 :         set_frozenxids(true);
     519             : 
     520             :     /* update new_cluster info now that we have objects in the databases */
     521           2 :     get_db_and_rel_infos(&new_cluster);
     522           2 : }
     523             : 
     524             : /*
     525             :  * Delete the given subdirectory contents from the new cluster
     526             :  */
     527             : static void
     528           6 : remove_new_subdir(const char *subdir, bool rmtopdir)
     529             : {
     530             :     char        new_path[MAXPGPATH];
     531             : 
     532           6 :     prep_status("Deleting files from new %s", subdir);
     533             : 
     534           6 :     snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
     535           6 :     if (!rmtree(new_path, rmtopdir))
     536           0 :         pg_fatal("could not delete directory \"%s\"", new_path);
     537             : 
     538           6 :     check_ok();
     539           6 : }
     540             : 
     541             : /*
     542             :  * Copy the files from the old cluster into it
     543             :  */
     544             : static void
     545           6 : copy_subdir_files(const char *old_subdir, const char *new_subdir)
     546             : {
     547             :     char        old_path[MAXPGPATH];
     548             :     char        new_path[MAXPGPATH];
     549             : 
     550           6 :     remove_new_subdir(new_subdir, true);
     551             : 
     552           6 :     snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
     553           6 :     snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
     554             : 
     555           6 :     prep_status("Copying old %s to new server", old_subdir);
     556             : 
     557           6 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     558             : #ifndef WIN32
     559             :               "cp -Rf \"%s\" \"%s\"",
     560             : #else
     561             :     /* flags: everything, no confirm, quiet, overwrite read-only */
     562             :               "xcopy /e /y /q /r \"%s\" \"%s\\\"",
     563             : #endif
     564             :               old_path, new_path);
     565             : 
     566           6 :     check_ok();
     567           6 : }
     568             : 
     569             : static void
     570           2 : copy_xact_xlog_xid(void)
     571             : {
     572             :     /*
     573             :      * Copy old commit logs to new data dir. pg_clog has been renamed to
     574             :      * pg_xact in post-10 clusters.
     575             :      */
     576           2 :     copy_subdir_files(GET_MAJOR_VERSION(old_cluster.major_version) <= 906 ?
     577             :                       "pg_clog" : "pg_xact",
     578           2 :                       GET_MAJOR_VERSION(new_cluster.major_version) <= 906 ?
     579             :                       "pg_clog" : "pg_xact");
     580             : 
     581           2 :     prep_status("Setting oldest XID for new cluster");
     582           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     583             :               "\"%s/pg_resetwal\" -f -u %u \"%s\"",
     584             :               new_cluster.bindir, old_cluster.controldata.chkpnt_oldstxid,
     585             :               new_cluster.pgdata);
     586           2 :     check_ok();
     587             : 
     588             :     /* set the next transaction id and epoch of the new cluster */
     589           2 :     prep_status("Setting next transaction ID and epoch for new cluster");
     590           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     591             :               "\"%s/pg_resetwal\" -f -x %u \"%s\"",
     592             :               new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid,
     593             :               new_cluster.pgdata);
     594           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     595             :               "\"%s/pg_resetwal\" -f -e %u \"%s\"",
     596             :               new_cluster.bindir, old_cluster.controldata.chkpnt_nxtepoch,
     597             :               new_cluster.pgdata);
     598             :     /* must reset commit timestamp limits also */
     599           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     600             :               "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
     601             :               new_cluster.bindir,
     602             :               old_cluster.controldata.chkpnt_nxtxid,
     603             :               old_cluster.controldata.chkpnt_nxtxid,
     604             :               new_cluster.pgdata);
     605           2 :     check_ok();
     606             : 
     607             :     /*
     608             :      * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
     609             :      * (see pg_upgrade.h) and the new server is after, then we don't copy
     610             :      * pg_multixact files, but we need to reset pg_control so that the new
     611             :      * server doesn't attempt to read multis older than the cutoff value.
     612             :      */
     613           2 :     if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
     614           2 :         new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
     615             :     {
     616           2 :         copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
     617           2 :         copy_subdir_files("pg_multixact/members", "pg_multixact/members");
     618             : 
     619           2 :         prep_status("Setting next multixact ID and offset for new cluster");
     620             : 
     621             :         /*
     622             :          * we preserve all files and contents, so we must preserve both "next"
     623             :          * counters here and the oldest multi present on system.
     624             :          */
     625           2 :         exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     626             :                   "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
     627             :                   new_cluster.bindir,
     628             :                   old_cluster.controldata.chkpnt_nxtmxoff,
     629             :                   old_cluster.controldata.chkpnt_nxtmulti,
     630             :                   old_cluster.controldata.chkpnt_oldstMulti,
     631             :                   new_cluster.pgdata);
     632           2 :         check_ok();
     633             :     }
     634           0 :     else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
     635             :     {
     636             :         /*
     637             :          * Remove offsets/0000 file created by initdb that no longer matches
     638             :          * the new multi-xid value.  "members" starts at zero so no need to
     639             :          * remove it.
     640             :          */
     641           0 :         remove_new_subdir("pg_multixact/offsets", false);
     642             : 
     643           0 :         prep_status("Setting oldest multixact ID in new cluster");
     644             : 
     645             :         /*
     646             :          * We don't preserve files in this case, but it's important that the
     647             :          * oldest multi is set to the latest value used by the old system, so
     648             :          * that multixact.c returns the empty set for multis that might be
     649             :          * present on disk.  We set next multi to the value following that; it
     650             :          * might end up wrapped around (i.e. 0) if the old cluster had
     651             :          * next=MaxMultiXactId, but multixact.c can cope with that just fine.
     652             :          */
     653           0 :         exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     654             :                   "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
     655             :                   new_cluster.bindir,
     656           0 :                   old_cluster.controldata.chkpnt_nxtmulti + 1,
     657             :                   old_cluster.controldata.chkpnt_nxtmulti,
     658             :                   new_cluster.pgdata);
     659           0 :         check_ok();
     660             :     }
     661             : 
     662             :     /* now reset the wal archives in the new cluster */
     663           2 :     prep_status("Resetting WAL archives");
     664           2 :     exec_prog(UTILITY_LOG_FILE, NULL, true, true,
     665             :     /* use timeline 1 to match controldata and no WAL history file */
     666             :               "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
     667             :               old_cluster.controldata.nextxlogfile + 8,
     668             :               new_cluster.pgdata);
     669           2 :     check_ok();
     670           2 : }
     671             : 
     672             : 
     673             : /*
     674             :  *  set_frozenxids()
     675             :  *
     676             :  * This is called on the new cluster before we restore anything, with
     677             :  * minmxid_only = false.  Its purpose is to ensure that all initdb-created
     678             :  * vacuumable tables have relfrozenxid/relminmxid matching the old cluster's
     679             :  * xid/mxid counters.  We also initialize the datfrozenxid/datminmxid of the
     680             :  * built-in databases to match.
     681             :  *
     682             :  * As we create user tables later, their relfrozenxid/relminmxid fields will
     683             :  * be restored properly by the binary-upgrade restore script.  Likewise for
     684             :  * user-database datfrozenxid/datminmxid.  However, if we're upgrading from a
     685             :  * pre-9.3 database, which does not store per-table or per-DB minmxid, then
     686             :  * the relminmxid/datminmxid values filled in by the restore script will just
     687             :  * be zeroes.
     688             :  *
     689             :  * Hence, with a pre-9.3 source database, a second call occurs after
     690             :  * everything is restored, with minmxid_only = true.  This pass will
     691             :  * initialize all tables and databases, both those made by initdb and user
     692             :  * objects, with the desired minmxid value.  frozenxid values are left alone.
     693             :  */
     694             : static void
     695           2 : set_frozenxids(bool minmxid_only)
     696             : {
     697             :     int         dbnum;
     698             :     PGconn     *conn,
     699             :                *conn_template1;
     700             :     PGresult   *dbres;
     701             :     int         ntups;
     702             :     int         i_datname;
     703             :     int         i_datallowconn;
     704             : 
     705           2 :     if (!minmxid_only)
     706           2 :         prep_status("Setting frozenxid and minmxid counters in new cluster");
     707             :     else
     708           0 :         prep_status("Setting minmxid counter in new cluster");
     709             : 
     710           2 :     conn_template1 = connectToServer(&new_cluster, "template1");
     711             : 
     712           2 :     if (!minmxid_only)
     713             :         /* set pg_database.datfrozenxid */
     714           2 :         PQclear(executeQueryOrDie(conn_template1,
     715             :                                   "UPDATE pg_catalog.pg_database "
     716             :                                   "SET datfrozenxid = '%u'",
     717             :                                   old_cluster.controldata.chkpnt_nxtxid));
     718             : 
     719             :     /* set pg_database.datminmxid */
     720           2 :     PQclear(executeQueryOrDie(conn_template1,
     721             :                               "UPDATE pg_catalog.pg_database "
     722             :                               "SET datminmxid = '%u'",
     723             :                               old_cluster.controldata.chkpnt_nxtmulti));
     724             : 
     725             :     /* get database names */
     726           2 :     dbres = executeQueryOrDie(conn_template1,
     727             :                               "SELECT  datname, datallowconn "
     728             :                               "FROM    pg_catalog.pg_database");
     729             : 
     730           2 :     i_datname = PQfnumber(dbres, "datname");
     731           2 :     i_datallowconn = PQfnumber(dbres, "datallowconn");
     732             : 
     733           2 :     ntups = PQntuples(dbres);
     734           8 :     for (dbnum = 0; dbnum < ntups; dbnum++)
     735             :     {
     736           6 :         char       *datname = PQgetvalue(dbres, dbnum, i_datname);
     737           6 :         char       *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
     738             : 
     739             :         /*
     740             :          * We must update databases where datallowconn = false, e.g.
     741             :          * template0, because autovacuum increments their datfrozenxids,
     742             :          * relfrozenxids, and relminmxid even if autovacuum is turned off, and
     743             :          * even though all the data rows are already frozen.  To enable this,
     744             :          * we temporarily change datallowconn.
     745             :          */
     746           6 :         if (strcmp(datallowconn, "f") == 0)
     747           2 :             PQclear(executeQueryOrDie(conn_template1,
     748             :                                       "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
     749             :                                       quote_identifier(datname)));
     750             : 
     751           6 :         conn = connectToServer(&new_cluster, datname);
     752             : 
     753           6 :         if (!minmxid_only)
     754             :             /* set pg_class.relfrozenxid */
     755           6 :             PQclear(executeQueryOrDie(conn,
     756             :                                       "UPDATE  pg_catalog.pg_class "
     757             :                                       "SET relfrozenxid = '%u' "
     758             :             /* only heap, materialized view, and TOAST are vacuumed */
     759             :                                       "WHERE   relkind IN ("
     760             :                                       CppAsString2(RELKIND_RELATION) ", "
     761             :                                       CppAsString2(RELKIND_MATVIEW) ", "
     762             :                                       CppAsString2(RELKIND_TOASTVALUE) ")",
     763             :                                       old_cluster.controldata.chkpnt_nxtxid));
     764             : 
     765             :         /* set pg_class.relminmxid */
     766           6 :         PQclear(executeQueryOrDie(conn,
     767             :                                   "UPDATE  pg_catalog.pg_class "
     768             :                                   "SET relminmxid = '%u' "
     769             :         /* only heap, materialized view, and TOAST are vacuumed */
     770             :                                   "WHERE   relkind IN ("
     771             :                                   CppAsString2(RELKIND_RELATION) ", "
     772             :                                   CppAsString2(RELKIND_MATVIEW) ", "
     773             :                                   CppAsString2(RELKIND_TOASTVALUE) ")",
     774             :                                   old_cluster.controldata.chkpnt_nxtmulti));
     775           6 :         PQfinish(conn);
     776             : 
     777             :         /* Reset datallowconn flag */
     778           6 :         if (strcmp(datallowconn, "f") == 0)
     779           2 :             PQclear(executeQueryOrDie(conn_template1,
     780             :                                       "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
     781             :                                       quote_identifier(datname)));
     782             :     }
     783             : 
     784           2 :     PQclear(dbres);
     785             : 
     786           2 :     PQfinish(conn_template1);
     787             : 
     788           2 :     check_ok();
     789           2 : }

Generated by: LCOV version 1.14