LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - pg_upgrade.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: c38de3ae6098ccf1904109f2e550eb4777f66024 vs e352ac8c5c90ed6ce792f044c22145578542a330 Lines: 84.9 % 331 281 1 49 3 17 261 5 16
Current Date: 2026-11-05 14:14:01 +0100 Functions: 100.0 % 15 15 7 8 2
Baseline: lcov-20261105-baseline Branches: 62.7 % 118 74 44 3 71
Baseline Date: 2026-11-05 14:14:01 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 11 11 11
(30,360] days: 45.5 % 22 10 1 11 6 4
(360..) days: 87.2 % 298 260 38 3 257
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 12 12 4 8
Branch coverage date bins:
(30,360] days: 25.0 % 8 2 6 2
(360..) days: 65.5 % 110 72 38 3 69

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

Generated by: LCOV version 2.0-1