LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - pg_upgrade.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 83.4 % 295 246 5 2 42 42 204 3 8
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 12 12 4 8
Baseline: 16@8cea358b128 Branches: 56.4 % 110 62 6 42 2 14 46
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 75.0 % 16 12 4 12
(60,120] days: 66.7 % 3 2 1 2
(120,180] days: 100.0 % 28 28 28
(240..) days: 82.3 % 248 204 2 42 204
Function coverage date bins:
(120,180] days: 100.0 % 1 1 1
(240..) days: 100.0 % 11 11 3 8
Branch coverage date bins:
[..60] days: 50.0 % 8 4 4 4
(60,120] days: 50.0 % 4 2 2 2
(120,180] days: 100.0 % 8 8 8
(240..) days: 53.3 % 90 48 42 2 46

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

Generated by: LCOV version 2.1-beta2-3-g6141622