LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - pg_upgrade.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 83.4 % 259 216 17 7 17 2 15 120 25 56 22 149 4 4
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 11 11 10 1 11
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a