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/logging.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 684 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().

685 {
686  fclose(log_opts.internal);
687 
688  /* Remove dump and log files? */
689  if (!log_opts.retain)
690  {
691  int dbnum;
692  char **filename;
693 
694  for (filename = output_files; *filename != NULL; filename++)
695  unlink(*filename);
696 
697  /* remove dump files */
698  unlink(GLOBALS_DUMP_FILE);
699 
700  if (old_cluster.dbarr.dbs)
701  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
702  {
703  char sql_file_name[MAXPGPATH],
704  log_file_name[MAXPGPATH];
705  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
706 
707  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
708  unlink(sql_file_name);
709 
710  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
711  unlink(log_file_name);
712  }
713  }
714 }
LogOpts log_opts
Definition: util.c:18
Oid db_oid
Definition: pg_upgrade.h:183
#define MAXPGPATH
char * output_files[]
Definition: pg_upgrade.c:63
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:30
DbInfoArr dbarr
Definition: pg_upgrade.h:263
FILE * internal
Definition: pg_upgrade.h:284
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:32
static char * filename
Definition: pg_dumpall.c:91
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:29
DbInfo * dbs
Definition: pg_upgrade.h:195
bool retain
Definition: pg_upgrade.h:286
#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 443 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().

444 {
445  char old_path[MAXPGPATH];
446  char new_path[MAXPGPATH];
447 
448  remove_new_subdir(new_subdir, true);
449 
450  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
451  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
452 
453  prep_status("Copying old %s to new server", old_subdir);
454 
455  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
456 #ifndef WIN32
457  "cp -Rf \"%s\" \"%s\"",
458 #else
459  /* flags: everything, no confirm, quiet, overwrite read-only */
460  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
461 #endif
462  old_path, new_path);
463 
464  check_ok();
465 }
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:426
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void check_ok(void)
Definition: initdb.c:2079
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:34
char * pgdata
Definition: pg_upgrade.h:264
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 468 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().

469 {
470  /*
471  * Copy old commit logs to new data dir. pg_clog has been renamed to
472  * pg_xact in post-10 clusters.
473  */
475  "pg_clog" : "pg_xact",
477  "pg_clog" : "pg_xact");
478 
479  /* set the next transaction id and epoch of the new cluster */
480  prep_status("Setting next transaction ID and epoch for new cluster");
481  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
482  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
485  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
486  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
489  /* must reset commit timestamp limits also */
490  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
491  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
496  check_ok();
497 
498  /*
499  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
500  * (see pg_upgrade.h) and the new server is after, then we don't copy
501  * pg_multixact files, but we need to reset pg_control so that the new
502  * server doesn't attempt to read multis older than the cutoff value.
503  */
506  {
507  copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
508  copy_subdir_files("pg_multixact/members", "pg_multixact/members");
509 
510  prep_status("Setting next multixact ID and offset for new cluster");
511 
512  /*
513  * we preserve all files and contents, so we must preserve both "next"
514  * counters here and the oldest multi present on system.
515  */
516  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
517  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
523  check_ok();
524  }
526  {
527  /*
528  * Remove offsets/0000 file created by initdb that no longer matches
529  * the new multi-xid value. "members" starts at zero so no need to
530  * remove it.
531  */
532  remove_new_subdir("pg_multixact/offsets", false);
533 
534  prep_status("Setting oldest multixact ID in new cluster");
535 
536  /*
537  * We don't preserve files in this case, but it's important that the
538  * oldest multi is set to the latest value used by the old system, so
539  * that multixact.c returns the empty set for multis that might be
540  * present on disk. We set next multi to the value following that; it
541  * might end up wrapped around (i.e. 0) if the old cluster had
542  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
543  */
544  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
545  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
550  check_ok();
551  }
552 
553  /* now reset the wal archives in the new cluster */
554  prep_status("Resetting WAL archives");
555  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
556  /* use timeline 1 to match controldata and no WAL history file */
557  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
560  check_ok();
561 }
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:426
uint32 major_version
Definition: pg_upgrade.h:272
ControlData controldata
Definition: pg_upgrade.h:262
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:26
char nextxlogfile[25]
Definition: pg_upgrade.h:208
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:213
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:209
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:210
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void check_ok(void)
Definition: initdb.c:2079
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:214
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:443
char * bindir
Definition: pg_upgrade.h:267
uint32 cat_ver
Definition: pg_upgrade.h:207
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:212
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:118
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:34
char * pgdata
Definition: pg_upgrade.h:264
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 320 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().

321 {
322  int dbnum;
323 
324  prep_status("Restoring database schemas in the new cluster\n");
325 
326  /*
327  * We cannot process the template1 database concurrently with others,
328  * because when it's transiently dropped, connection attempts would fail.
329  * So handle it in a separate non-parallelized pass.
330  */
331  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
332  {
333  char sql_file_name[MAXPGPATH],
334  log_file_name[MAXPGPATH];
335  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
336  const char *create_opts;
337 
338  /* Process only template1 in this pass */
339  if (strcmp(old_db->db_name, "template1") != 0)
340  continue;
341 
342  pg_log(PG_STATUS, "%s", old_db->db_name);
343  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
344  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
345 
346  /*
347  * template1 and postgres databases will already exist in the target
348  * installation, so tell pg_restore to drop and recreate them;
349  * otherwise we would fail to propagate their database-level
350  * properties.
351  */
352  create_opts = "--clean --create";
353 
354  exec_prog(log_file_name,
355  NULL,
356  true,
357  true,
358  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
359  "--dbname postgres \"%s\"",
362  create_opts,
363  sql_file_name);
364 
365  break; /* done once we've processed template1 */
366  }
367 
368  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
369  {
370  char sql_file_name[MAXPGPATH],
371  log_file_name[MAXPGPATH];
372  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
373  const char *create_opts;
374 
375  /* Skip template1 in this pass */
376  if (strcmp(old_db->db_name, "template1") == 0)
377  continue;
378 
379  pg_log(PG_STATUS, "%s", old_db->db_name);
380  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
381  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
382 
383  /*
384  * template1 and postgres databases will already exist in the target
385  * installation, so tell pg_restore to drop and recreate them;
386  * otherwise we would fail to propagate their database-level
387  * properties.
388  */
389  if (strcmp(old_db->db_name, "postgres") == 0)
390  create_opts = "--clean --create";
391  else
392  create_opts = "--create";
393 
394  parallel_exec_prog(log_file_name,
395  NULL,
396  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
397  "--dbname template1 \"%s\"",
400  create_opts,
401  sql_file_name);
402  }
403 
404  /* reap all children */
405  while (reap_child(true) == true)
406  ;
407 
409  check_ok();
410 
411  /*
412  * We don't have minmxids for databases or relations in pre-9.3 clusters,
413  * so set those after we have restored the schema.
414  */
416  set_frozenxids(true);
417 
418  /* update new_cluster info now that we have objects in the databases */
420 }
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:272
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:26
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:183
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void check_ok(void)
Definition: initdb.c:2079
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:30
char * bindir
Definition: pg_upgrade.h:267
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:586
DbInfoArr dbarr
Definition: pg_upgrade.h:263
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:311
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:32
bool reap_child(bool wait_for_child)
Definition: parallel.c:289
char * db_name
Definition: pg_upgrade.h:184
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:195
#define snprintf
Definition: port.h:192

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 76 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_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_pglocale_pgservice(), setup(), start_postmaster(), stop_postmaster(), strerror, transfer_all_new_tablespaces(), UserOpts::transfer_mode, TRANSFER_MODE_LINK, user_opts, and UTILITY_LOG_FILE.

77 {
78  char *analyze_script_file_name = NULL;
79  char *deletion_script_file_name = NULL;
80  bool live_check = false;
81 
82  pg_logging_init(argv[0]);
83  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
84 
85  /* Set default restrictive mask until new cluster permissions are read */
86  umask(PG_MODE_MASK_OWNER);
87 
88  parseCommandLine(argc, argv);
89 
91 
94 
95  setup(argv[0], &live_check);
96 
97  output_check_banner(live_check);
98 
100 
101  get_sock_dir(&old_cluster, live_check);
102  get_sock_dir(&new_cluster, false);
103 
104  check_cluster_compatibility(live_check);
105 
106  /* Set mask based on PGDATA permissions */
108  {
109  pg_log(PG_FATAL, "could not read permissions of directory \"%s\": %s\n",
110  new_cluster.pgdata, strerror(errno));
111  exit(1);
112  }
113 
114  umask(pg_mode_mask);
115 
116  check_and_dump_old_cluster(live_check);
117 
118 
119  /* -- NEW -- */
121 
124 
126  "\n"
127  "Performing Upgrade\n"
128  "------------------\n");
129 
131 
132  stop_postmaster(false);
133 
134  /*
135  * Destructive Changes to New Cluster
136  */
137 
139 
140  /* New now using xids of the old system */
141 
142  /* -- NEW -- */
144 
146 
148 
149  stop_postmaster(false);
150 
151  /*
152  * Most failures happen in create_new_objects(), which has completed at
153  * this point. We do this here because it is just before linking, which
154  * will link the old and new cluster data files, preventing the old
155  * cluster from being safely started once the new cluster is started.
156  */
159 
162 
163  /*
164  * Assuming OIDs are only used in system tables, there is no need to
165  * restore the OID counter because we have not transferred any OIDs from
166  * the old system, but we do it anyway just in case. We do it late here
167  * because there is no need to have the schema load use new oids.
168  */
169  prep_status("Setting next OID for new cluster");
170  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
171  "\"%s/pg_resetwal\" -o %u \"%s\"",
174  check_ok();
175 
176  prep_status("Sync data directory to disk");
177  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
178  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
180  check_ok();
181 
182  create_script_for_cluster_analyze(&analyze_script_file_name);
183  create_script_for_old_cluster_deletion(&deletion_script_file_name);
184 
186 
188  "\n"
189  "Upgrade Complete\n"
190  "----------------\n");
191 
192  output_completion_banner(analyze_script_file_name,
193  deletion_script_file_name);
194 
195  pg_free(analyze_script_file_name);
196  pg_free(deletion_script_file_name);
197 
198  cleanup();
199 
200  return 0;
201 }
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:41
static void cleanup(void)
Definition: pg_upgrade.c:684
ControlData controldata
Definition: pg_upgrade.h:262
void check_cluster_versions(void)
Definition: check.c:250
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:269
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:473
static void create_new_objects(void)
Definition: pg_upgrade.c:320
void get_restricted_token(void)
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:211
void pg_logging_init(const char *argv0)
Definition: logging.c:39
void check_cluster_compatibility(bool live_check)
Definition: check.c:292
static void adjust_data_dir(void)
Definition: pg_ctl.c:2174
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
static void prepare_new_globals(void)
Definition: pg_upgrade.c:299
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:59
static void check_ok(void)
Definition: initdb.c:2079
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:536
transferMode transfer_mode
Definition: pg_upgrade.h:297
UserOpts user_opts
Definition: option.c:32
char * bindir
Definition: pg_upgrade.h:267
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1135
#define PG_MODE_MASK_OWNER
Definition: file_perm.h:24
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:205
void stop_postmaster(bool in_atexit)
Definition: server.c:328
DbInfoArr dbarr
Definition: pg_upgrade.h:263
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
#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:450
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:34
bool GetDataDirectoryCreatePerm(const char *dataDir)
char * pgdata
Definition: pg_upgrade.h:264
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:565
void disable_old_cluster(void)
Definition: controldata.c:676
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:468

◆ prepare_new_cluster()

static void prepare_new_cluster ( void  )
static

Definition at line 269 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().

270 {
271  /*
272  * It would make more sense to freeze after loading the schema, but that
273  * would cause us to lose the frozenxids restored by the load. We use
274  * --analyze so autovacuum doesn't update statistics later
275  */
276  prep_status("Analyzing all rows in the new cluster");
277  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
278  "\"%s/vacuumdb\" %s --all --analyze %s",
280  log_opts.verbose ? "--verbose" : "");
281  check_ok();
282 
283  /*
284  * We do freeze after analyze so pg_statistic is also frozen. template0 is
285  * not frozen here, but data rows were frozen by initdb, and we set its
286  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
287  * counter later.
288  */
289  prep_status("Freezing all rows in the new cluster");
290  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
291  "\"%s/vacuumdb\" %s --all --freeze %s",
293  log_opts.verbose ? "--verbose" : "");
294  check_ok();
295 }
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:59
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:2079
bool verbose
Definition: pg_upgrade.h:285
char * bindir
Definition: pg_upgrade.h:267
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:34
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 299 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().

300 {
301  /*
302  * Before we restore anything, set frozenxids of initdb-created tables.
303  */
304  set_frozenxids(false);
305 
306  /*
307  * Now restore global objects (roles and tablespaces).
308  */
309  prep_status("Restoring global objects in the new cluster");
310 
311  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
312  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
315  check_ok();
316 }
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:91
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:2079
char * bindir
Definition: pg_upgrade.h:267
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:586
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:34
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:363
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:29

◆ remove_new_subdir()

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

Definition at line 426 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().

427 {
428  char new_path[MAXPGPATH];
429 
430  prep_status("Deleting files from new %s", subdir);
431 
432  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
433  if (!rmtree(new_path, rmtopdir))
434  pg_fatal("could not delete directory \"%s\"\n", new_path);
435 
436  check_ok();
437 }
#define pg_fatal(...)
Definition: pg_rewind.h:39
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:2079
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
char * pgdata
Definition: pg_upgrade.h:264
#define snprintf
Definition: port.h:192

◆ set_frozenxids()

static void set_frozenxids ( bool  minmxid_only)
static

Definition at line 586 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().

587 {
588  int dbnum;
589  PGconn *conn,
590  *conn_template1;
591  PGresult *dbres;
592  int ntups;
593  int i_datname;
594  int i_datallowconn;
595 
596  if (!minmxid_only)
597  prep_status("Setting frozenxid and minmxid counters in new cluster");
598  else
599  prep_status("Setting minmxid counter in new cluster");
600 
601  conn_template1 = connectToServer(&new_cluster, "template1");
602 
603  if (!minmxid_only)
604  /* set pg_database.datfrozenxid */
605  PQclear(executeQueryOrDie(conn_template1,
606  "UPDATE pg_catalog.pg_database "
607  "SET datfrozenxid = '%u'",
609 
610  /* set pg_database.datminmxid */
611  PQclear(executeQueryOrDie(conn_template1,
612  "UPDATE pg_catalog.pg_database "
613  "SET datminmxid = '%u'",
615 
616  /* get database names */
617  dbres = executeQueryOrDie(conn_template1,
618  "SELECT datname, datallowconn "
619  "FROM pg_catalog.pg_database");
620 
621  i_datname = PQfnumber(dbres, "datname");
622  i_datallowconn = PQfnumber(dbres, "datallowconn");
623 
624  ntups = PQntuples(dbres);
625  for (dbnum = 0; dbnum < ntups; dbnum++)
626  {
627  char *datname = PQgetvalue(dbres, dbnum, i_datname);
628  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
629 
630  /*
631  * We must update databases where datallowconn = false, e.g.
632  * template0, because autovacuum increments their datfrozenxids,
633  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
634  * even though all the data rows are already frozen. To enable this,
635  * we temporarily change datallowconn.
636  */
637  if (strcmp(datallowconn, "f") == 0)
638  PQclear(executeQueryOrDie(conn_template1,
639  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
640  quote_identifier(datname)));
641 
642  conn = connectToServer(&new_cluster, datname);
643 
644  if (!minmxid_only)
645  /* set pg_class.relfrozenxid */
647  "UPDATE pg_catalog.pg_class "
648  "SET relfrozenxid = '%u' "
649  /* only heap, materialized view, and TOAST are vacuumed */
650  "WHERE relkind IN ("
651  CppAsString2(RELKIND_RELATION) ", "
652  CppAsString2(RELKIND_MATVIEW) ", "
653  CppAsString2(RELKIND_TOASTVALUE) ")",
655 
656  /* set pg_class.relminmxid */
658  "UPDATE pg_catalog.pg_class "
659  "SET relminmxid = '%u' "
660  /* only heap, materialized view, and TOAST are vacuumed */
661  "WHERE relkind IN ("
662  CppAsString2(RELKIND_RELATION) ", "
663  CppAsString2(RELKIND_MATVIEW) ", "
664  CppAsString2(RELKIND_TOASTVALUE) ")",
666  PQfinish(conn);
667 
668  /* Reset datallowconn flag */
669  if (strcmp(datallowconn, "f") == 0)
670  PQclear(executeQueryOrDie(conn_template1,
671  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
672  quote_identifier(datname)));
673  }
674 
675  PQclear(dbres);
676 
677  PQfinish(conn_template1);
678 
679  check_ok();
680 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:10628
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
ControlData controldata
Definition: pg_upgrade.h:262
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4080
NameData datname
Definition: pg_database.h:35
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
bool datallowconn
Definition: pg_database.h:53
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:209
PGconn * conn
Definition: streamutil.c:56
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void check_ok(void)
Definition: initdb.c:2079
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:212
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
void PQclear(PGresult *res)
Definition: fe-exec.c:695

◆ setup()

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

Definition at line 205 of file pg_upgrade.c.

References ClusterInfo::bindir, canonicalize_path(), UserOpts::check, check_pghost_envvar(), 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().

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 
213  /*
214  * In case the user hasn't specified the directory for the new binaries
215  * with -B, default to using the path of the currently executed pg_upgrade
216  * binary.
217  */
218  if (!new_cluster.bindir)
219  {
220  char exec_path[MAXPGPATH];
221 
222  if (find_my_exec(argv0, exec_path) < 0)
223  pg_fatal("%s: could not find own program executable\n", argv0);
224  /* Trim off program name and keep just path */
225  *last_dir_separator(exec_path) = '\0';
226  canonicalize_path(exec_path);
227  new_cluster.bindir = pg_strdup(exec_path);
228  }
229 
231 
232  /* no postmasters should be running, except for a live check */
234  {
235  /*
236  * If we have a postmaster.pid file, try to start the server. If it
237  * starts, the pid file was stale, so stop the server. If it doesn't
238  * start, assume the server is running. If the pid file is left over
239  * from a server crash, this also allows any committed transactions
240  * stored in the WAL to be replayed so they are not lost, because WAL
241  * files are not transferred from old to new servers. We later check
242  * for a clean shutdown.
243  */
244  if (start_postmaster(&old_cluster, false))
245  stop_postmaster(false);
246  else
247  {
248  if (!user_opts.check)
249  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
250  "Please shutdown that postmaster and try again.\n");
251  else
252  *live_check = true;
253  }
254  }
255 
256  /* same goes for the new postmaster */
258  {
259  if (start_postmaster(&new_cluster, false))
260  stop_postmaster(false);
261  else
262  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
263  "Please shutdown that postmaster and try again.\n");
264  }
265 }
static char * argv0
Definition: pg_ctl.c:97
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:219
void canonicalize_path(char *path)
Definition: path.c:254
#define pg_fatal(...)
Definition: pg_rewind.h:39
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
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:59
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
UserOpts user_opts
Definition: option.c:32
char * bindir
Definition: pg_upgrade.h:267
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:92
bool check
Definition: pg_upgrade.h:295
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:450
char * pgdata
Definition: pg_upgrade.h:264

Variable Documentation

◆ new_cluster

◆ old_cluster

◆ os_info

◆ output_files

char* output_files[]
Initial value:
= {
NULL
}
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:35
#define SERVER_LOG_FILE
Definition: pg_upgrade.h:33
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:34

Definition at line 63 of file pg_upgrade.c.

Referenced by cleanup(), and parseCommandLine().