PostgreSQL Source Code  git master
pg_upgrade.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include "catalog/pg_class_d.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "common/restricted_token.h"
#include "fe_utils/string_utils.h"
#include "pg_upgrade.h"
Include dependency graph for pg_upgrade.c:

Go to the source code of this file.

Functions

static void set_locale_and_encoding (void)
 
static void prepare_new_cluster (void)
 
static void prepare_new_globals (void)
 
static void create_new_objects (void)
 
static void copy_xact_xlog_xid (void)
 
static void set_frozenxids (bool minmxid_only)
 
static void make_outputdirs (char *pgdata)
 
static void setup (char *argv0, bool *live_check)
 
int main (int argc, char **argv)
 
static void remove_new_subdir (const char *subdir, bool rmtopdir)
 
static void copy_subdir_files (const char *old_subdir, const char *new_subdir)
 

Variables

ClusterInfo old_cluster
 
ClusterInfo new_cluster
 
OSInfo os_info
 
char * output_files []
 

Function Documentation

◆ copy_subdir_files()

static void copy_subdir_files ( const char *  old_subdir,
const char *  new_subdir 
)
static

Definition at line 620 of file pg_upgrade.c.

621 {
622  char old_path[MAXPGPATH];
623  char new_path[MAXPGPATH];
624 
625  remove_new_subdir(new_subdir, true);
626 
627  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
628  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
629 
630  prep_status("Copying old %s to new server", old_subdir);
631 
632  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
633 #ifndef WIN32
634  "cp -Rf \"%s\" \"%s\"",
635 #else
636  /* flags: everything, no confirm, quiet, overwrite read-only */
637  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
638 #endif
639  old_path, new_path);
640 
641  check_ok();
642 }
bool exec_prog(const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:86
static void check_ok(void)
Definition: initdb.c:2036
#define MAXPGPATH
ClusterInfo new_cluster
Definition: pg_upgrade.c:64
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:603
ClusterInfo old_cluster
Definition: pg_upgrade.c:63
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:45
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define snprintf
Definition: port.h:238
char * pgdata
Definition: pg_upgrade.h:264

References check_ok(), exec_prog(), MAXPGPATH, new_cluster, old_cluster, ClusterInfo::pgdata, prep_status(), remove_new_subdir(), snprintf, and UTILITY_LOG_FILE.

Referenced by copy_xact_xlog_xid().

◆ copy_xact_xlog_xid()

static void copy_xact_xlog_xid ( void  )
static

Definition at line 645 of file pg_upgrade.c.

646 {
647  /*
648  * Copy old commit logs to new data dir. pg_clog has been renamed to
649  * pg_xact in post-10 clusters.
650  */
652  "pg_clog" : "pg_xact",
654  "pg_clog" : "pg_xact");
655 
656  prep_status("Setting oldest XID for new cluster");
657  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
658  "\"%s/pg_resetwal\" -f -u %u \"%s\"",
661  check_ok();
662 
663  /* set the next transaction id and epoch of the new cluster */
664  prep_status("Setting next transaction ID and epoch for new cluster");
665  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
666  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
669  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
670  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
673  /* must reset commit timestamp limits also */
674  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
675  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
680  check_ok();
681 
682  /*
683  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
684  * (see pg_upgrade.h) and the new server is after, then we don't copy
685  * pg_multixact files, but we need to reset pg_control so that the new
686  * server doesn't attempt to read multis older than the cutoff value.
687  */
690  {
691  copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
692  copy_subdir_files("pg_multixact/members", "pg_multixact/members");
693 
694  prep_status("Setting next multixact ID and offset for new cluster");
695 
696  /*
697  * we preserve all files and contents, so we must preserve both "next"
698  * counters here and the oldest multi present on system.
699  */
700  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
701  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
707  check_ok();
708  }
710  {
711  /*
712  * Remove offsets/0000 file created by initdb that no longer matches
713  * the new multi-xid value. "members" starts at zero so no need to
714  * remove it.
715  */
716  remove_new_subdir("pg_multixact/offsets", false);
717 
718  prep_status("Setting oldest multixact ID in new cluster");
719 
720  /*
721  * We don't preserve files in this case, but it's important that the
722  * oldest multi is set to the latest value used by the old system, so
723  * that multixact.c returns the empty set for multis that might be
724  * present on disk. We set next multi to the value following that; it
725  * might end up wrapped around (i.e. 0) if the old cluster had
726  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
727  */
728  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
729  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
734  check_ok();
735  }
736 
737  /* now reset the wal archives in the new cluster */
738  prep_status("Resetting WAL archives");
739  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
740  /* use timeline 1 to match controldata and no WAL history file */
741  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
744  check_ok();
745 }
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:620
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:116
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
ControlData controldata
Definition: pg_upgrade.h:261
char * bindir
Definition: pg_upgrade.h:267
uint32 major_version
Definition: pg_upgrade.h:272
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:209
char nextxlogfile[25]
Definition: pg_upgrade.h:208
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:213
uint32 cat_ver
Definition: pg_upgrade.h:207
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:212
uint32 chkpnt_oldstxid
Definition: pg_upgrade.h:215
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:210
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:214

References ClusterInfo::bindir, ControlData::cat_ver, check_ok(), ControlData::chkpnt_nxtepoch, ControlData::chkpnt_nxtmulti, ControlData::chkpnt_nxtmxoff, ControlData::chkpnt_nxtxid, ControlData::chkpnt_oldstMulti, ControlData::chkpnt_oldstxid, ClusterInfo::controldata, copy_subdir_files(), exec_prog(), GET_MAJOR_VERSION, ClusterInfo::major_version, MULTIXACT_FORMATCHANGE_CAT_VER, new_cluster, ControlData::nextxlogfile, old_cluster, ClusterInfo::pgdata, prep_status(), remove_new_subdir(), and UTILITY_LOG_FILE.

Referenced by main().

◆ create_new_objects()

static void create_new_objects ( void  )
static

Definition at line 497 of file pg_upgrade.c.

498 {
499  int dbnum;
500 
501  prep_status_progress("Restoring database schemas in the new cluster");
502 
503  /*
504  * We cannot process the template1 database concurrently with others,
505  * because when it's transiently dropped, connection attempts would fail.
506  * So handle it in a separate non-parallelized pass.
507  */
508  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
509  {
510  char sql_file_name[MAXPGPATH],
511  log_file_name[MAXPGPATH];
512  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
513  const char *create_opts;
514 
515  /* Process only template1 in this pass */
516  if (strcmp(old_db->db_name, "template1") != 0)
517  continue;
518 
519  pg_log(PG_STATUS, "%s", old_db->db_name);
520  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
521  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
522 
523  /*
524  * template1 database will already exist in the target installation,
525  * so tell pg_restore to drop and recreate it; otherwise we would fail
526  * to propagate its database-level properties.
527  */
528  create_opts = "--clean --create";
529 
530  exec_prog(log_file_name,
531  NULL,
532  true,
533  true,
534  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
535  "--dbname postgres \"%s/%s\"",
538  create_opts,
540  sql_file_name);
541 
542  break; /* done once we've processed template1 */
543  }
544 
545  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
546  {
547  char sql_file_name[MAXPGPATH],
548  log_file_name[MAXPGPATH];
549  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
550  const char *create_opts;
551 
552  /* Skip template1 in this pass */
553  if (strcmp(old_db->db_name, "template1") == 0)
554  continue;
555 
556  pg_log(PG_STATUS, "%s", old_db->db_name);
557  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
558  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
559 
560  /*
561  * postgres database will already exist in the target installation, so
562  * tell pg_restore to drop and recreate it; otherwise we would fail to
563  * propagate its database-level properties.
564  */
565  if (strcmp(old_db->db_name, "postgres") == 0)
566  create_opts = "--clean --create";
567  else
568  create_opts = "--create";
569 
570  parallel_exec_prog(log_file_name,
571  NULL,
572  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
573  "--dbname template1 \"%s/%s\"",
576  create_opts,
578  sql_file_name);
579  }
580 
581  /* reap all children */
582  while (reap_child(true) == true)
583  ;
584 
586  check_ok();
587 
588  /*
589  * We don't have minmxids for databases or relations in pre-9.3 clusters,
590  * so set those after we have restored the schema.
591  */
593  set_frozenxids(true);
594 
595  /* update new_cluster info now that we have objects in the databases */
597 }
bool reap_child(bool wait_for_child)
Definition: parallel.c:278
void parallel_exec_prog(const char *log_file, const char *opt_log_file, const char *fmt,...)
Definition: parallel.c:62
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:275
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:770
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:43
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
LogOpts log_opts
Definition: util.c:17
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
@ PG_STATUS
Definition: pg_upgrade.h:246
void end_progress_output(void)
Definition: util.c:43
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:92
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:31
DbInfoArr dbarr
Definition: pg_upgrade.h:263
DbInfo * dbs
Definition: pg_upgrade.h:195
char * db_name
Definition: pg_upgrade.h:175
Oid db_oid
Definition: pg_upgrade.h:174
char * dumpdir
Definition: pg_upgrade.h:290

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), DB_DUMP_FILE_MASK, DB_DUMP_LOG_FILE_MASK, DbInfo::db_name, DbInfo::db_oid, ClusterInfo::dbarr, DbInfoArr::dbs, LogOpts::dumpdir, end_progress_output(), exec_prog(), get_db_and_rel_infos(), GET_MAJOR_VERSION, log_opts, ClusterInfo::major_version, MAXPGPATH, DbInfoArr::ndbs, new_cluster, old_cluster, parallel_exec_prog(), pg_log(), PG_STATUS, prep_status_progress(), reap_child(), set_frozenxids(), and snprintf.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 80 of file pg_upgrade.c.

81 {
82  char *deletion_script_file_name = NULL;
83  bool live_check = false;
84 
85  /*
86  * pg_upgrade doesn't currently use common/logging.c, but initialize it
87  * anyway because we might call common code that does.
88  */
89  pg_logging_init(argv[0]);
90  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
91 
92  /* Set default restrictive mask until new cluster permissions are read */
93  umask(PG_MODE_MASK_OWNER);
94 
95  parseCommandLine(argc, argv);
96 
98 
101 
102  /*
103  * Set mask based on PGDATA permissions, needed for the creation of the
104  * output directories with correct permissions.
105  */
107  pg_fatal("could not read permissions of directory \"%s\": %s",
108  new_cluster.pgdata, strerror(errno));
109 
110  umask(pg_mode_mask);
111 
112  /*
113  * This needs to happen after adjusting the data directory of the new
114  * cluster in adjust_data_dir().
115  */
117 
118  setup(argv[0], &live_check);
119 
120  output_check_banner(live_check);
121 
123 
124  get_sock_dir(&old_cluster, live_check);
125  get_sock_dir(&new_cluster, false);
126 
127  check_cluster_compatibility(live_check);
128 
129  check_and_dump_old_cluster(live_check);
130 
131 
132  /* -- NEW -- */
134 
137 
139  "\n"
140  "Performing Upgrade\n"
141  "------------------");
142 
144 
146 
147  stop_postmaster(false);
148 
149  /*
150  * Destructive Changes to New Cluster
151  */
152 
154 
155  /* New now using xids of the old system */
156 
157  /* -- NEW -- */
159 
161 
163 
164  stop_postmaster(false);
165 
166  /*
167  * Most failures happen in create_new_objects(), which has completed at
168  * this point. We do this here because it is just before linking, which
169  * will link the old and new cluster data files, preventing the old
170  * cluster from being safely started once the new cluster is started.
171  */
174 
177 
178  /*
179  * Assuming OIDs are only used in system tables, there is no need to
180  * restore the OID counter because we have not transferred any OIDs from
181  * the old system, but we do it anyway just in case. We do it late here
182  * because there is no need to have the schema load use new oids.
183  */
184  prep_status("Setting next OID for new cluster");
185  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
186  "\"%s/pg_resetwal\" -o %u \"%s\"",
189  check_ok();
190 
191  if (user_opts.do_sync)
192  {
193  prep_status("Sync data directory to disk");
194  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
195  "\"%s/initdb\" --sync-only \"%s\" --sync-method %s",
199  check_ok();
200  }
201 
202  create_script_for_old_cluster_deletion(&deletion_script_file_name);
203 
205 
207  "\n"
208  "Upgrade Complete\n"
209  "----------------");
210 
211  output_completion_banner(deletion_script_file_name);
212 
213  pg_free(deletion_script_file_name);
214 
216 
217  return 0;
218 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1227
void output_check_banner(bool live_check)
Definition: check.c:67
void check_cluster_versions(void)
Definition: check.c:304
void check_cluster_compatibility(bool live_check)
Definition: check.c:347
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:85
void issue_warnings_and_set_wal_level(void)
Definition: check.c:249
void check_new_cluster(void)
Definition: check.c:201
void report_clusters_compatible(void)
Definition: check.c:230
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:422
void output_completion_banner(char *deletion_script_file_name)
Definition: check.c:270
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:436
void disable_old_cluster(void)
Definition: controldata.c:713
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int pg_mode_mask
Definition: file_perm.c:25
bool GetDataDirectoryCreatePerm(const char *dataDir)
#define PG_MODE_MASK_OWNER
Definition: file_perm.h:24
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_fatal(...)
static void adjust_data_dir(void)
Definition: pg_ctl.c:2116
static pid_t start_postmaster(void)
Definition: pg_ctl.c:440
static void make_outputdirs(char *pgdata)
Definition: pg_upgrade.c:226
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:445
static void create_new_objects(void)
Definition: pg_upgrade.c:497
static void copy_xact_xlog_xid(void)
Definition: pg_upgrade.c:645
static void set_locale_and_encoding(void)
Definition: pg_upgrade.c:382
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:311
static void prepare_new_globals(void)
Definition: pg_upgrade.c:475
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenumber.c:29
void cleanup_output_dirs(void)
Definition: util.c:63
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:237
@ PG_REPORT
Definition: pg_upgrade.h:248
void stop_postmaster(bool in_atexit)
Definition: server.c:320
#define strerror
Definition: port.h:251
void get_restricted_token(void)
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:469
UserOpts user_opts
Definition: option.c:30
void parseCommandLine(int argc, char *argv[])
Definition: option.c:39
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:211
char * sync_method
Definition: pg_upgrade.h:307
bool do_sync
Definition: pg_upgrade.h:303
transferMode transfer_mode
Definition: pg_upgrade.h:304

References adjust_data_dir(), ClusterInfo::bindir, check_and_dump_old_cluster(), check_cluster_compatibility(), check_cluster_versions(), check_new_cluster(), check_ok(), ControlData::chkpnt_nxtoid, cleanup_output_dirs(), ClusterInfo::controldata, copy_xact_xlog_xid(), create_new_objects(), create_script_for_old_cluster_deletion(), ClusterInfo::dbarr, disable_old_cluster(), UserOpts::do_sync, exec_prog(), get_restricted_token(), get_sock_dir(), GetDataDirectoryCreatePerm(), issue_warnings_and_set_wal_level(), make_outputdirs(), new_cluster, old_cluster, output_check_banner(), output_completion_banner(), parseCommandLine(), pg_fatal, pg_free(), pg_log(), pg_logging_init(), pg_mode_mask, PG_MODE_MASK_OWNER, PG_REPORT, PG_TEXTDOMAIN, ClusterInfo::pgdata, prep_status(), prepare_new_cluster(), prepare_new_globals(), report_clusters_compatible(), set_locale_and_encoding(), set_pglocale_pgservice(), setup(), start_postmaster(), stop_postmaster(), strerror, UserOpts::sync_method, transfer_all_new_tablespaces(), UserOpts::transfer_mode, TRANSFER_MODE_LINK, user_opts, and UTILITY_LOG_FILE.

◆ make_outputdirs()

static void make_outputdirs ( char *  pgdata)
static

Definition at line 226 of file pg_upgrade.c.

227 {
228  FILE *fp;
229  char **filename;
230  time_t run_time = time(NULL);
231  char filename_path[MAXPGPATH];
232  char timebuf[128];
233  struct timeval time;
234  time_t tt;
235  int len;
236 
237  log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
238  len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
239  if (len >= MAXPGPATH)
240  pg_fatal("directory path for new cluster is too long");
241 
242  /* BASE_OUTPUTDIR/$timestamp/ */
243  gettimeofday(&time, NULL);
244  tt = (time_t) time.tv_sec;
245  strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
246  /* append milliseconds */
247  snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
248  ".%03d", (int) (time.tv_usec / 1000));
249  log_opts.basedir = (char *) pg_malloc0(MAXPGPATH);
251  timebuf);
252  if (len >= MAXPGPATH)
253  pg_fatal("directory path for new cluster is too long");
254 
255  /* BASE_OUTPUTDIR/$timestamp/dump/ */
256  log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
258  timebuf, DUMP_OUTPUTDIR);
259  if (len >= MAXPGPATH)
260  pg_fatal("directory path for new cluster is too long");
261 
262  /* BASE_OUTPUTDIR/$timestamp/log/ */
263  log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
265  timebuf, LOG_OUTPUTDIR);
266  if (len >= MAXPGPATH)
267  pg_fatal("directory path for new cluster is too long");
268 
269  /*
270  * Ignore the error case where the root path exists, as it is kept the
271  * same across runs.
272  */
273  if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
274  pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
276  pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
278  pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
280  pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
281 
282  len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
284  if (len >= sizeof(filename_path))
285  pg_fatal("directory path for new cluster is too long");
286 
287  if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
288  pg_fatal("could not open log file \"%s\": %m", filename_path);
289 
290  /* label start of upgrade in logfiles */
291  for (filename = output_files; *filename != NULL; filename++)
292  {
293  len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
295  if (len >= sizeof(filename_path))
296  pg_fatal("directory path for new cluster is too long");
297  if ((fp = fopen_priv(filename_path, "a")) == NULL)
298  pg_fatal("could not write to log file \"%s\": %m", filename_path);
299 
300  fprintf(fp,
301  "-----------------------------------------------------------------\n"
302  " pg_upgrade run on %s"
303  "-----------------------------------------------------------------\n\n",
304  ctime(&run_time));
305  fclose(fp);
306  }
307 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
int pg_dir_create_mode
Definition: file_perm.c:18
const void size_t len
static char * filename
Definition: pg_dumpall.c:119
char * output_files[]
Definition: pg_upgrade.c:67
#define LOG_OUTPUTDIR
Definition: pg_upgrade.h:40
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:391
#define DUMP_OUTPUTDIR
Definition: pg_upgrade.h:41
#define BASE_OUTPUTDIR
Definition: pg_upgrade.h:39
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:46
#define fprintf
Definition: port.h:242
char * rootdir
Definition: pg_upgrade.h:288
FILE * internal
Definition: pg_upgrade.h:284
char * basedir
Definition: pg_upgrade.h:289
char * logdir
Definition: pg_upgrade.h:291
#define mkdir(a, b)
Definition: win32_port.h:80
int gettimeofday(struct timeval *tp, void *tzp)

References BASE_OUTPUTDIR, LogOpts::basedir, DUMP_OUTPUTDIR, LogOpts::dumpdir, filename, fopen_priv, fprintf, gettimeofday(), LogOpts::internal, INTERNAL_LOG_FILE, len, log_opts, LOG_OUTPUTDIR, LogOpts::logdir, MAXPGPATH, mkdir, output_files, pg_dir_create_mode, pg_fatal, pg_malloc0(), LogOpts::rootdir, and snprintf.

Referenced by main().

◆ prepare_new_cluster()

static void prepare_new_cluster ( void  )
static

Definition at line 445 of file pg_upgrade.c.

446 {
447  /*
448  * It would make more sense to freeze after loading the schema, but that
449  * would cause us to lose the frozenxids restored by the load. We use
450  * --analyze so autovacuum doesn't update statistics later
451  */
452  prep_status("Analyzing all rows in the new cluster");
453  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
454  "\"%s/vacuumdb\" %s --all --analyze %s",
456  log_opts.verbose ? "--verbose" : "");
457  check_ok();
458 
459  /*
460  * We do freeze after analyze so pg_statistic is also frozen. template0 is
461  * not frozen here, but data rows were frozen by initdb, and we set its
462  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
463  * counter later.
464  */
465  prep_status("Freezing all rows in the new cluster");
466  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
467  "\"%s/vacuumdb\" %s --all --freeze %s",
469  log_opts.verbose ? "--verbose" : "");
470  check_ok();
471 }
bool verbose
Definition: pg_upgrade.h:285

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), exec_prog(), log_opts, new_cluster, prep_status(), UTILITY_LOG_FILE, and LogOpts::verbose.

Referenced by main().

◆ prepare_new_globals()

static void prepare_new_globals ( void  )
static

Definition at line 475 of file pg_upgrade.c.

476 {
477  /*
478  * Before we restore anything, set frozenxids of initdb-created tables.
479  */
480  set_frozenxids(false);
481 
482  /*
483  * Now restore global objects (roles and tablespaces).
484  */
485  prep_status("Restoring global objects in the new cluster");
486 
487  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
488  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
492  check_ok();
493 }
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:30
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:369

References ClusterInfo::bindir, check_ok(), cluster_conn_opts(), LogOpts::dumpdir, exec_prog(), EXEC_PSQL_ARGS, GLOBALS_DUMP_FILE, log_opts, new_cluster, prep_status(), set_frozenxids(), and UTILITY_LOG_FILE.

Referenced by main().

◆ remove_new_subdir()

static void remove_new_subdir ( const char *  subdir,
bool  rmtopdir 
)
static

Definition at line 603 of file pg_upgrade.c.

604 {
605  char new_path[MAXPGPATH];
606 
607  prep_status("Deleting files from new %s", subdir);
608 
609  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
610  if (!rmtree(new_path, rmtopdir))
611  pg_fatal("could not delete directory \"%s\"", new_path);
612 
613  check_ok();
614 }
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50

References check_ok(), MAXPGPATH, new_cluster, pg_fatal, ClusterInfo::pgdata, prep_status(), rmtree(), and snprintf.

Referenced by copy_subdir_files(), and copy_xact_xlog_xid().

◆ set_frozenxids()

static void set_frozenxids ( bool  minmxid_only)
static

Definition at line 770 of file pg_upgrade.c.

771 {
772  int dbnum;
773  PGconn *conn,
774  *conn_template1;
775  PGresult *dbres;
776  int ntups;
777  int i_datname;
778  int i_datallowconn;
779 
780  if (!minmxid_only)
781  prep_status("Setting frozenxid and minmxid counters in new cluster");
782  else
783  prep_status("Setting minmxid counter in new cluster");
784 
785  conn_template1 = connectToServer(&new_cluster, "template1");
786 
787  if (!minmxid_only)
788  /* set pg_database.datfrozenxid */
789  PQclear(executeQueryOrDie(conn_template1,
790  "UPDATE pg_catalog.pg_database "
791  "SET datfrozenxid = '%u'",
793 
794  /* set pg_database.datminmxid */
795  PQclear(executeQueryOrDie(conn_template1,
796  "UPDATE pg_catalog.pg_database "
797  "SET datminmxid = '%u'",
799 
800  /* get database names */
801  dbres = executeQueryOrDie(conn_template1,
802  "SELECT datname, datallowconn "
803  "FROM pg_catalog.pg_database");
804 
805  i_datname = PQfnumber(dbres, "datname");
806  i_datallowconn = PQfnumber(dbres, "datallowconn");
807 
808  ntups = PQntuples(dbres);
809  for (dbnum = 0; dbnum < ntups; dbnum++)
810  {
811  char *datname = PQgetvalue(dbres, dbnum, i_datname);
812  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
813 
814  /*
815  * We must update databases where datallowconn = false, e.g.
816  * template0, because autovacuum increments their datfrozenxids,
817  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
818  * even though all the data rows are already frozen. To enable this,
819  * we temporarily change datallowconn.
820  */
821  if (strcmp(datallowconn, "f") == 0)
822  PQclear(executeQueryOrDie(conn_template1,
823  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
825 
827 
828  if (!minmxid_only)
829  /* set pg_class.relfrozenxid */
831  "UPDATE pg_catalog.pg_class "
832  "SET relfrozenxid = '%u' "
833  /* only heap, materialized view, and TOAST are vacuumed */
834  "WHERE relkind IN ("
835  CppAsString2(RELKIND_RELATION) ", "
836  CppAsString2(RELKIND_MATVIEW) ", "
837  CppAsString2(RELKIND_TOASTVALUE) ")",
839 
840  /* set pg_class.relminmxid */
842  "UPDATE pg_catalog.pg_class "
843  "SET relminmxid = '%u' "
844  /* only heap, materialized view, and TOAST are vacuumed */
845  "WHERE relkind IN ("
846  CppAsString2(RELKIND_RELATION) ", "
847  CppAsString2(RELKIND_MATVIEW) ", "
848  CppAsString2(RELKIND_TOASTVALUE) ")",
850  PQfinish(conn);
851 
852  /* Reset datallowconn flag */
853  if (strcmp(datallowconn, "f") == 0)
854  PQclear(executeQueryOrDie(conn_template1,
855  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
857  }
858 
859  PQclear(dbres);
860 
861  PQfinish(conn_template1);
862 
863  check_ok();
864 }
#define CppAsString2(x)
Definition: c.h:316
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4602
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3395
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3790
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3503
NameData datname
Definition: pg_database.h:35
bool datallowconn
Definition: pg_database.h:50
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11965
PGconn * conn
Definition: streamutil.c:54

References check_ok(), ControlData::chkpnt_nxtmulti, ControlData::chkpnt_nxtxid, conn, connectToServer(), ClusterInfo::controldata, CppAsString2, datallowconn, datname, executeQueryOrDie(), new_cluster, old_cluster, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), prep_status(), and quote_identifier().

Referenced by create_new_objects(), and prepare_new_globals().

◆ set_locale_and_encoding()

static void set_locale_and_encoding ( void  )
static

Definition at line 382 of file pg_upgrade.c.

383 {
384  PGconn *conn_new_template1;
385  char *datcollate_literal;
386  char *datctype_literal;
387  char *daticulocale_literal = NULL;
389 
390  prep_status("Setting locale and encoding for new cluster");
391 
392  /* escape literals with respect to new cluster */
393  conn_new_template1 = connectToServer(&new_cluster, "template1");
394 
395  datcollate_literal = PQescapeLiteral(conn_new_template1,
396  locale->db_collate,
397  strlen(locale->db_collate));
398  datctype_literal = PQescapeLiteral(conn_new_template1,
399  locale->db_ctype,
400  strlen(locale->db_ctype));
401  if (locale->db_iculocale)
402  daticulocale_literal = PQescapeLiteral(conn_new_template1,
403  locale->db_iculocale,
404  strlen(locale->db_iculocale));
405  else
406  daticulocale_literal = pg_strdup("NULL");
407 
408  /* update template0 in new cluster */
410  PQclear(executeQueryOrDie(conn_new_template1,
411  "UPDATE pg_catalog.pg_database "
412  " SET encoding = %d, "
413  " datlocprovider = '%c', "
414  " datcollate = %s, "
415  " datctype = %s, "
416  " daticulocale = %s "
417  " WHERE datname = 'template0' ",
418  locale->db_encoding,
419  locale->db_collprovider,
420  datcollate_literal,
421  datctype_literal,
422  daticulocale_literal));
423  else
424  PQclear(executeQueryOrDie(conn_new_template1,
425  "UPDATE pg_catalog.pg_database "
426  " SET encoding = %d, "
427  " datcollate = %s, "
428  " datctype = %s "
429  " WHERE datname = 'template0' ",
430  locale->db_encoding,
431  datcollate_literal,
432  datctype_literal));
433 
434  PQfreemem(datcollate_literal);
435  PQfreemem(datctype_literal);
436  PQfreemem(daticulocale_literal);
437 
438  PQfinish(conn_new_template1);
439 
440  check_ok();
441 }
void PQfreemem(void *ptr)
Definition: fe-exec.c:3946
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4218
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * locale
Definition: initdb.c:140
DbLocaleInfo * template0
Definition: pg_upgrade.h:262

References check_ok(), connectToServer(), executeQueryOrDie(), GET_MAJOR_VERSION, locale, ClusterInfo::major_version, new_cluster, old_cluster, pg_strdup(), PQclear(), PQescapeLiteral(), PQfinish(), PQfreemem(), prep_status(), and ClusterInfo::template0.

Referenced by main().

◆ setup()

static void setup ( char *  argv0,
bool live_check 
)
static

Definition at line 311 of file pg_upgrade.c.

312 {
313  /*
314  * make sure the user has a clean environment, otherwise, we may confuse
315  * libpq when we connect to one (or both) of the servers.
316  */
318 
319  /*
320  * In case the user hasn't specified the directory for the new binaries
321  * with -B, default to using the path of the currently executed pg_upgrade
322  * binary.
323  */
324  if (!new_cluster.bindir)
325  {
326  char exec_path[MAXPGPATH];
327 
328  if (find_my_exec(argv0, exec_path) < 0)
329  pg_fatal("%s: could not find own program executable", argv0);
330  /* Trim off program name and keep just path */
331  *last_dir_separator(exec_path) = '\0';
334  }
335 
337 
338  /* no postmasters should be running, except for a live check */
340  {
341  /*
342  * If we have a postmaster.pid file, try to start the server. If it
343  * starts, the pid file was stale, so stop the server. If it doesn't
344  * start, assume the server is running. If the pid file is left over
345  * from a server crash, this also allows any committed transactions
346  * stored in the WAL to be replayed so they are not lost, because WAL
347  * files are not transferred from old to new servers. We later check
348  * for a clean shutdown.
349  */
350  if (start_postmaster(&old_cluster, false))
351  stop_postmaster(false);
352  else
353  {
354  if (!user_opts.check)
355  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
356  "Please shutdown that postmaster and try again.");
357  else
358  *live_check = true;
359  }
360  }
361 
362  /* same goes for the new postmaster */
364  {
365  if (start_postmaster(&new_cluster, false))
366  stop_postmaster(false);
367  else
368  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
369  "Please shutdown that postmaster and try again.");
370  }
371 }
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:234
void verify_directories(void)
Definition: exec.c:265
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:158
static char * argv0
Definition: pg_ctl.c:92
static char * exec_path
Definition: pg_ctl.c:87
void check_pghost_envvar(void)
Definition: server.c:347
char * last_dir_separator(const char *filename)
Definition: path.c:139
void canonicalize_path(char *path)
Definition: path.c:264
bool check
Definition: pg_upgrade.h:301

References argv0, ClusterInfo::bindir, canonicalize_path(), UserOpts::check, check_pghost_envvar(), exec_path, find_my_exec(), last_dir_separator(), MAXPGPATH, new_cluster, old_cluster, pg_fatal, pg_strdup(), ClusterInfo::pgdata, pid_lock_file_exists(), start_postmaster(), stop_postmaster(), user_opts, and verify_directories().

Referenced by main().

Variable Documentation

◆ new_cluster

◆ old_cluster

◆ os_info

◆ output_files

char* output_files[]
Initial value:
= {
NULL
}
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:44

Definition at line 67 of file pg_upgrade.c.

Referenced by make_outputdirs().