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

Go to the source code of this file.

Functions

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 setup (char *argv0, bool *live_check)
 
static void cleanup (void)
 
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

◆ cleanup()

static void cleanup ( void  )
static

Definition at line 676 of file pg_upgrade.c.

References DB_DUMP_FILE_MASK, DB_DUMP_LOG_FILE_MASK, DbInfo::db_oid, ClusterInfo::dbarr, DbInfoArr::dbs, filename, GLOBALS_DUMP_FILE, LogOpts::internal, log_opts, MAXPGPATH, DbInfoArr::ndbs, output_files, LogOpts::retain, and snprintf.

Referenced by main().

677 {
678  fclose(log_opts.internal);
679 
680  /* Remove dump and log files? */
681  if (!log_opts.retain)
682  {
683  int dbnum;
684  char **filename;
685 
686  for (filename = output_files; *filename != NULL; filename++)
687  unlink(*filename);
688 
689  /* remove dump files */
690  unlink(GLOBALS_DUMP_FILE);
691 
692  if (old_cluster.dbarr.dbs)
693  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
694  {
695  char sql_file_name[MAXPGPATH],
696  log_file_name[MAXPGPATH];
697  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
698 
699  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
700  unlink(sql_file_name);
701 
702  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
703  unlink(log_file_name);
704  }
705  }
706 }
LogOpts log_opts
Definition: util.c:18
Oid db_oid
Definition: pg_upgrade.h:186
#define MAXPGPATH
char * output_files[]
Definition: pg_upgrade.c:62
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:33
DbInfoArr dbarr
Definition: pg_upgrade.h:266
FILE * internal
Definition: pg_upgrade.h:287
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:35
static char * filename
Definition: pg_dumpall.c:88
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:32
DbInfo * dbs
Definition: pg_upgrade.h:198
bool retain
Definition: pg_upgrade.h:289
#define snprintf
Definition: port.h:192

◆ copy_subdir_files()

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

Definition at line 435 of file pg_upgrade.c.

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

Referenced by copy_xact_xlog_xid().

436 {
437  char old_path[MAXPGPATH];
438  char new_path[MAXPGPATH];
439 
440  remove_new_subdir(new_subdir, true);
441 
442  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
443  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
444 
445  prep_status("Copying old %s to new server", old_subdir);
446 
447  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
448 #ifndef WIN32
449  "cp -Rf \"%s\" \"%s\"",
450 #else
451  /* flags: everything, no confirm, quiet, overwrite read-only */
452  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
453 #endif
454  old_path, new_path);
455 
456  check_ok();
457 }
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:418
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
static void check_ok(void)
Definition: initdb.c:2153
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
char * pgdata
Definition: pg_upgrade.h:267
bool exec_prog(const char *log_file, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:80
#define snprintf
Definition: port.h:192

◆ copy_xact_xlog_xid()

static void copy_xact_xlog_xid ( void  )
static

Definition at line 460 of file pg_upgrade.c.

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

Referenced by main().

461 {
462  /*
463  * Copy old commit logs to new data dir. pg_clog has been renamed to
464  * pg_xact in post-10 clusters.
465  */
467  "pg_clog" : "pg_xact",
469  "pg_clog" : "pg_xact");
470 
471  /* set the next transaction id and epoch of the new cluster */
472  prep_status("Setting next transaction ID and epoch for new cluster");
473  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
474  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
477  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
478  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
481  /* must reset commit timestamp limits also */
482  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
483  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
488  check_ok();
489 
490  /*
491  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
492  * (see pg_upgrade.h) and the new server is after, then we don't copy
493  * pg_multixact files, but we need to reset pg_control so that the new
494  * server doesn't attempt to read multis older than the cutoff value.
495  */
498  {
499  copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
500  copy_subdir_files("pg_multixact/members", "pg_multixact/members");
501 
502  prep_status("Setting next multixact ID and offset for new cluster");
503 
504  /*
505  * we preserve all files and contents, so we must preserve both "next"
506  * counters here and the oldest multi present on system.
507  */
508  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
509  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
515  check_ok();
516  }
518  {
519  /*
520  * Remove offsets/0000 file created by initdb that no longer matches
521  * the new multi-xid value. "members" starts at zero so no need to
522  * remove it.
523  */
524  remove_new_subdir("pg_multixact/offsets", false);
525 
526  prep_status("Setting oldest multixact ID in new cluster");
527 
528  /*
529  * We don't preserve files in this case, but it's important that the
530  * oldest multi is set to the latest value used by the old system, so
531  * that multixact.c returns the empty set for multis that might be
532  * present on disk. We set next multi to the value following that; it
533  * might end up wrapped around (i.e. 0) if the old cluster had
534  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
535  */
536  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
537  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
542  check_ok();
543  }
544 
545  /* now reset the wal archives in the new cluster */
546  prep_status("Resetting WAL archives");
547  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
548  /* use timeline 1 to match controldata and no WAL history file */
549  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
552  check_ok();
553 }
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:418
uint32 major_version
Definition: pg_upgrade.h:275
ControlData controldata
Definition: pg_upgrade.h:265
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
char nextxlogfile[25]
Definition: pg_upgrade.h:211
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:216
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:212
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:213
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
static void check_ok(void)
Definition: initdb.c:2153
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:217
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:435
char * bindir
Definition: pg_upgrade.h:270
uint32 cat_ver
Definition: pg_upgrade.h:210
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:215
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:121
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
char * pgdata
Definition: pg_upgrade.h:267
bool exec_prog(const char *log_file, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:80

◆ create_new_objects()

static void create_new_objects ( void  )
static

Definition at line 312 of file pg_upgrade.c.

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, end_progress_output(), exec_prog(), get_db_and_rel_infos(), GET_MAJOR_VERSION, ClusterInfo::major_version, MAXPGPATH, DbInfoArr::ndbs, parallel_exec_prog(), pg_log(), PG_STATUS, prep_status(), reap_child(), set_frozenxids(), and snprintf.

Referenced by main().

313 {
314  int dbnum;
315 
316  prep_status("Restoring database schemas in the new cluster\n");
317 
318  /*
319  * We cannot process the template1 database concurrently with others,
320  * because when it's transiently dropped, connection attempts would fail.
321  * So handle it in a separate non-parallelized pass.
322  */
323  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
324  {
325  char sql_file_name[MAXPGPATH],
326  log_file_name[MAXPGPATH];
327  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
328  const char *create_opts;
329 
330  /* Process only template1 in this pass */
331  if (strcmp(old_db->db_name, "template1") != 0)
332  continue;
333 
334  pg_log(PG_STATUS, "%s", old_db->db_name);
335  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
336  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
337 
338  /*
339  * template1 and postgres databases will already exist in the target
340  * installation, so tell pg_restore to drop and recreate them;
341  * otherwise we would fail to propagate their database-level
342  * properties.
343  */
344  create_opts = "--clean --create";
345 
346  exec_prog(log_file_name,
347  NULL,
348  true,
349  true,
350  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
351  "--dbname postgres \"%s\"",
354  create_opts,
355  sql_file_name);
356 
357  break; /* done once we've processed template1 */
358  }
359 
360  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
361  {
362  char sql_file_name[MAXPGPATH],
363  log_file_name[MAXPGPATH];
364  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
365  const char *create_opts;
366 
367  /* Skip template1 in this pass */
368  if (strcmp(old_db->db_name, "template1") == 0)
369  continue;
370 
371  pg_log(PG_STATUS, "%s", old_db->db_name);
372  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
373  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
374 
375  /*
376  * template1 and postgres databases will already exist in the target
377  * installation, so tell pg_restore to drop and recreate them;
378  * otherwise we would fail to propagate their database-level
379  * properties.
380  */
381  if (strcmp(old_db->db_name, "postgres") == 0)
382  create_opts = "--clean --create";
383  else
384  create_opts = "--create";
385 
386  parallel_exec_prog(log_file_name,
387  NULL,
388  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
389  "--dbname template1 \"%s\"",
392  create_opts,
393  sql_file_name);
394  }
395 
396  /* reap all children */
397  while (reap_child(true) == true)
398  ;
399 
401  check_ok();
402 
403  /*
404  * We don't have minmxids for databases or relations in pre-9.3 clusters,
405  * so set those after we have restored the schema.
406  */
408  set_frozenxids(true);
409 
410  /* update new_cluster info now that we have objects in the databases */
412 }
void parallel_exec_prog(const char *log_file, const char *opt_log_file, const char *fmt,...)
Definition: parallel.c:63
uint32 major_version
Definition: pg_upgrade.h:275
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
void end_progress_output(void)
Definition: util.c:44
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:91
Oid db_oid
Definition: pg_upgrade.h:186
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:67
static void check_ok(void)
Definition: initdb.c:2153
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:33
char * bindir
Definition: pg_upgrade.h:270
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:578
DbInfoArr dbarr
Definition: pg_upgrade.h:266
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:311
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:35
bool reap_child(bool wait_for_child)
Definition: parallel.c:289
char * db_name
Definition: pg_upgrade.h:187
bool exec_prog(const char *log_file, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:80
DbInfo * dbs
Definition: pg_upgrade.h:198
#define snprintf
Definition: port.h:192

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 75 of file pg_upgrade.c.

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(), ClusterInfo::controldata, copy_xact_xlog_xid(), create_new_objects(), create_script_for_cluster_analyze(), create_script_for_old_cluster_deletion(), ClusterInfo::dbarr, disable_old_cluster(), exec_prog(), get_restricted_token(), get_sock_dir(), GetDataDirectoryCreatePerm(), issue_warnings_and_set_wal_level(), output_check_banner(), output_completion_banner(), parseCommandLine(), PG_FATAL, pg_free(), pg_log(), pg_mode_mask, PG_MODE_MASK_OWNER, PG_REPORT, PG_TEXTDOMAIN, ClusterInfo::pgdata, prep_status(), prepare_new_cluster(), prepare_new_globals(), OSInfo::progname, report_clusters_compatible(), set_pglocale_pgservice(), setup(), start_postmaster(), stop_postmaster(), strerror, transfer_all_new_tablespaces(), UserOpts::transfer_mode, TRANSFER_MODE_LINK, user_opts, and UTILITY_LOG_FILE.

76 {
77  char *analyze_script_file_name = NULL;
78  char *deletion_script_file_name = NULL;
79  bool live_check = false;
80 
81  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
82 
83  /* Set default restrictive mask until new cluster permissions are read */
84  umask(PG_MODE_MASK_OWNER);
85 
86  parseCommandLine(argc, argv);
87 
89 
92 
93  setup(argv[0], &live_check);
94 
95  output_check_banner(live_check);
96 
98 
99  get_sock_dir(&old_cluster, live_check);
100  get_sock_dir(&new_cluster, false);
101 
102  check_cluster_compatibility(live_check);
103 
104  /* Set mask based on PGDATA permissions */
106  {
107  pg_log(PG_FATAL, "could not read permissions of directory \"%s\": %s\n",
108  new_cluster.pgdata, strerror(errno));
109  exit(1);
110  }
111 
112  umask(pg_mode_mask);
113 
114  check_and_dump_old_cluster(live_check);
115 
116 
117  /* -- NEW -- */
119 
122 
124  "\n"
125  "Performing Upgrade\n"
126  "------------------\n");
127 
129 
130  stop_postmaster(false);
131 
132  /*
133  * Destructive Changes to New Cluster
134  */
135 
137 
138  /* New now using xids of the old system */
139 
140  /* -- NEW -- */
142 
144 
146 
147  stop_postmaster(false);
148 
149  /*
150  * Most failures happen in create_new_objects(), which has completed at
151  * this point. We do this here because it is just before linking, which
152  * will link the old and new cluster data files, preventing the old
153  * cluster from being safely started once the new cluster is started.
154  */
157 
160 
161  /*
162  * Assuming OIDs are only used in system tables, there is no need to
163  * restore the OID counter because we have not transferred any OIDs from
164  * the old system, but we do it anyway just in case. We do it late here
165  * because there is no need to have the schema load use new oids.
166  */
167  prep_status("Setting next OID for new cluster");
168  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
169  "\"%s/pg_resetwal\" -o %u \"%s\"",
172  check_ok();
173 
174  prep_status("Sync data directory to disk");
175  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
176  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
178  check_ok();
179 
180  create_script_for_cluster_analyze(&analyze_script_file_name);
181  create_script_for_old_cluster_deletion(&deletion_script_file_name);
182 
184 
186  "\n"
187  "Upgrade Complete\n"
188  "----------------\n");
189 
190  output_completion_banner(analyze_script_file_name,
191  deletion_script_file_name);
192 
193  pg_free(analyze_script_file_name);
194  pg_free(deletion_script_file_name);
195 
196  cleanup();
197 
198  return 0;
199 }
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenode.c:30
void parseCommandLine(int argc, char *argv[])
Definition: option.c:39
static void cleanup(void)
Definition: pg_upgrade.c:676
ControlData controldata
Definition: pg_upgrade.h:265
void check_cluster_versions(void)
Definition: check.c:250
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:261
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:465
static void create_new_objects(void)
Definition: pg_upgrade.c:312
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:214
void check_cluster_compatibility(bool live_check)
Definition: check.c:292
static void adjust_data_dir(void)
Definition: pg_ctl.c:2121
void get_restricted_token(const char *progname)
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
static void prepare_new_globals(void)
Definition: pg_upgrade.c:291
void output_check_banner(bool live_check)
Definition: check.c:62
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:67
static void check_ok(void)
Definition: initdb.c:2153
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:536
transferMode transfer_mode
Definition: pg_upgrade.h:300
UserOpts user_opts
Definition: option.c:30
char * bindir
Definition: pg_upgrade.h:270
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1121
#define PG_MODE_MASK_OWNER
Definition: file_perm.h:24
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:203
void stop_postmaster(bool in_atexit)
Definition: server.c:328
DbInfoArr dbarr
Definition: pg_upgrade.h:266
const char * progname
Definition: pg_upgrade.h:316
#define strerror
Definition: port.h:205
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:447
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool GetDataDirectoryCreatePerm(const char *dataDir)
char * pgdata
Definition: pg_upgrade.h:267
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
void disable_old_cluster(void)
Definition: controldata.c:676
OSInfo os_info
Definition: pg_upgrade.c:60
void output_completion_banner(char *analyze_script_file_name, char *deletion_script_file_name)
Definition: check.c:219
void create_script_for_cluster_analyze(char **analyze_script_file_name)
Definition: check.c:446
void check_new_cluster(void)
Definition: check.c:151
bool exec_prog(const char *log_file, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:80
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:80
void report_clusters_compatible(void)
Definition: check.c:179
void issue_warnings_and_set_wal_level(void)
Definition: check.c:196
int pg_mode_mask
Definition: file_perm.c:25
static void copy_xact_xlog_xid(void)
Definition: pg_upgrade.c:460

◆ prepare_new_cluster()

static void prepare_new_cluster ( void  )
static

Definition at line 261 of file pg_upgrade.c.

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

Referenced by main().

262 {
263  /*
264  * It would make more sense to freeze after loading the schema, but that
265  * would cause us to lose the frozenids restored by the load. We use
266  * --analyze so autovacuum doesn't update statistics later
267  */
268  prep_status("Analyzing all rows in the new cluster");
269  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
270  "\"%s/vacuumdb\" %s --all --analyze %s",
272  log_opts.verbose ? "--verbose" : "");
273  check_ok();
274 
275  /*
276  * We do freeze after analyze so pg_statistic is also frozen. template0 is
277  * not frozen here, but data rows were frozen by initdb, and we set its
278  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
279  * counter later.
280  */
281  prep_status("Freezing all rows in the new cluster");
282  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
283  "\"%s/vacuumdb\" %s --all --freeze %s",
285  log_opts.verbose ? "--verbose" : "");
286  check_ok();
287 }
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:91
LogOpts log_opts
Definition: util.c:18
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:2153
bool verbose
Definition: pg_upgrade.h:288
char * bindir
Definition: pg_upgrade.h:270
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool exec_prog(const char *log_file, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:80

◆ prepare_new_globals()

static void prepare_new_globals ( void  )
static

Definition at line 291 of file pg_upgrade.c.

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

Referenced by main().

292 {
293  /*
294  * Before we restore anything, set frozenxids of initdb-created tables.
295  */
296  set_frozenxids(false);
297 
298  /*
299  * Now restore global objects (roles and tablespaces).
300  */
301  prep_status("Restoring global objects in the new cluster");
302 
303  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
304  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
307  check_ok();
308 }
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:91
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:2153
char * bindir
Definition: pg_upgrade.h:270
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:578
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:367
bool exec_prog(const char *log_file, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition: exec.c:80
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:32

◆ remove_new_subdir()

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

Definition at line 418 of file pg_upgrade.c.

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

Referenced by copy_subdir_files(), and copy_xact_xlog_xid().

419 {
420  char new_path[MAXPGPATH];
421 
422  prep_status("Deleting files from new %s", subdir);
423 
424  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
425  if (!rmtree(new_path, rmtopdir))
426  pg_fatal("could not delete directory \"%s\"\n", new_path);
427 
428  check_ok();
429 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:81
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:2153
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:36
char * pgdata
Definition: pg_upgrade.h:267
#define snprintf
Definition: port.h:192

◆ set_frozenxids()

static void set_frozenxids ( bool  minmxid_only)
static

Definition at line 578 of file pg_upgrade.c.

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

Referenced by create_new_objects(), and prepare_new_globals().

579 {
580  int dbnum;
581  PGconn *conn,
582  *conn_template1;
583  PGresult *dbres;
584  int ntups;
585  int i_datname;
586  int i_datallowconn;
587 
588  if (!minmxid_only)
589  prep_status("Setting frozenxid and minmxid counters in new cluster");
590  else
591  prep_status("Setting minmxid counter in new cluster");
592 
593  conn_template1 = connectToServer(&new_cluster, "template1");
594 
595  if (!minmxid_only)
596  /* set pg_database.datfrozenxid */
597  PQclear(executeQueryOrDie(conn_template1,
598  "UPDATE pg_catalog.pg_database "
599  "SET datfrozenxid = '%u'",
601 
602  /* set pg_database.datminmxid */
603  PQclear(executeQueryOrDie(conn_template1,
604  "UPDATE pg_catalog.pg_database "
605  "SET datminmxid = '%u'",
607 
608  /* get database names */
609  dbres = executeQueryOrDie(conn_template1,
610  "SELECT datname, datallowconn "
611  "FROM pg_catalog.pg_database");
612 
613  i_datname = PQfnumber(dbres, "datname");
614  i_datallowconn = PQfnumber(dbres, "datallowconn");
615 
616  ntups = PQntuples(dbres);
617  for (dbnum = 0; dbnum < ntups; dbnum++)
618  {
619  char *datname = PQgetvalue(dbres, dbnum, i_datname);
620  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
621 
622  /*
623  * We must update databases where datallowconn = false, e.g.
624  * template0, because autovacuum increments their datfrozenxids,
625  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
626  * even though all the data rows are already frozen. To enable this,
627  * we temporarily change datallowconn.
628  */
629  if (strcmp(datallowconn, "f") == 0)
630  PQclear(executeQueryOrDie(conn_template1,
631  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
632  quote_identifier(datname)));
633 
634  conn = connectToServer(&new_cluster, datname);
635 
636  if (!minmxid_only)
637  /* set pg_class.relfrozenxid */
639  "UPDATE pg_catalog.pg_class "
640  "SET relfrozenxid = '%u' "
641  /* only heap, materialized view, and TOAST are vacuumed */
642  "WHERE relkind IN ("
643  CppAsString2(RELKIND_RELATION) ", "
644  CppAsString2(RELKIND_MATVIEW) ", "
645  CppAsString2(RELKIND_TOASTVALUE) ")",
647 
648  /* set pg_class.relminmxid */
650  "UPDATE pg_catalog.pg_class "
651  "SET relminmxid = '%u' "
652  /* only heap, materialized view, and TOAST are vacuumed */
653  "WHERE relkind IN ("
654  CppAsString2(RELKIND_RELATION) ", "
655  CppAsString2(RELKIND_MATVIEW) ", "
656  CppAsString2(RELKIND_TOASTVALUE) ")",
658  PQfinish(conn);
659 
660  /* Reset datallowconn flag */
661  if (strcmp(datallowconn, "f") == 0)
662  PQclear(executeQueryOrDie(conn_template1,
663  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
664  quote_identifier(datname)));
665  }
666 
667  PQclear(dbres);
668 
669  PQfinish(conn_template1);
670 
671  check_ok();
672 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10554
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3147
ControlData controldata
Definition: pg_upgrade.h:265
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3808
NameData datname
Definition: pg_database.h:32
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2753
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
bool datallowconn
Definition: pg_database.h:38
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:212
PGconn * conn
Definition: streamutil.c:55
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
static void check_ok(void)
Definition: initdb.c:2153
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
#define CppAsString2(x)
Definition: c.h:224
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:215
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2861
void PQclear(PGresult *res)
Definition: fe-exec.c:695

◆ setup()

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

Definition at line 203 of file pg_upgrade.c.

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

Referenced by main().

204 {
205  char exec_path[MAXPGPATH]; /* full path to my executable */
206 
207  /*
208  * make sure the user has a clean environment, otherwise, we may confuse
209  * libpq when we connect to one (or both) of the servers.
210  */
212 
214 
215  /* no postmasters should be running, except for a live check */
217  {
218  /*
219  * If we have a postmaster.pid file, try to start the server. If it
220  * starts, the pid file was stale, so stop the server. If it doesn't
221  * start, assume the server is running. If the pid file is left over
222  * from a server crash, this also allows any committed transactions
223  * stored in the WAL to be replayed so they are not lost, because WAL
224  * files are not transferred from old to new servers. We later check
225  * for a clean shutdown.
226  */
227  if (start_postmaster(&old_cluster, false))
228  stop_postmaster(false);
229  else
230  {
231  if (!user_opts.check)
232  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
233  "Please shutdown that postmaster and try again.\n");
234  else
235  *live_check = true;
236  }
237  }
238 
239  /* same goes for the new postmaster */
241  {
242  if (start_postmaster(&new_cluster, false))
243  stop_postmaster(false);
244  else
245  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
246  "Please shutdown that postmaster and try again.\n");
247  }
248 
249  /* get path to pg_upgrade executable */
250  if (find_my_exec(argv0, exec_path) < 0)
251  pg_fatal("%s: could not find own program executable\n", argv0);
252 
253  /* Trim off program name and keep just path */
254  *last_dir_separator(exec_path) = '\0';
255  canonicalize_path(exec_path);
256  os_info.exec_path = pg_strdup(exec_path);
257 }
static char * argv0
Definition: pg_ctl.c:95
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:219
void canonicalize_path(char *path)
Definition: path.c:254
void pg_fatal(const char *fmt,...)
Definition: logging.c:81
ClusterInfo new_cluster
Definition: pg_upgrade.c:58
void check_pghost_envvar(void)
Definition: server.c:355
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:129
ClusterInfo old_cluster
Definition: pg_upgrade.c:58
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
UserOpts user_opts
Definition: option.c:30
void stop_postmaster(bool in_atexit)
Definition: server.c:328
char * last_dir_separator(const char *filename)
Definition: path.c:138
void verify_directories(void)
Definition: exec.c:250
static char * exec_path
Definition: pg_ctl.c:90
bool check
Definition: pg_upgrade.h:298
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:447
char * pgdata
Definition: pg_upgrade.h:267
OSInfo os_info
Definition: pg_upgrade.c:60
char * exec_path
Definition: pg_upgrade.h:317

Variable Documentation

◆ new_cluster

◆ old_cluster

◆ os_info

◆ output_files

char* output_files[]
Initial value:
= {
NULL
}
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:38
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:36
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37

Definition at line 62 of file pg_upgrade.c.

Referenced by cleanup(), and parseCommandLine().