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 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 545 of file pg_upgrade.c.

546 {
547  char old_path[MAXPGPATH];
548  char new_path[MAXPGPATH];
549 
550  remove_new_subdir(new_subdir, true);
551 
552  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
553  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
554 
555  prep_status("Copying old %s to new server", old_subdir);
556 
557  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
558 #ifndef WIN32
559  "cp -Rf \"%s\" \"%s\"",
560 #else
561  /* flags: everything, no confirm, quiet, overwrite read-only */
562  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
563 #endif
564  old_path, new_path);
565 
566  check_ok();
567 }
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:1823
#define MAXPGPATH
ClusterInfo new_cluster
Definition: pg_upgrade.c:63
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:528
ClusterInfo old_cluster
Definition: pg_upgrade.c:62
#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:256

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 570 of file pg_upgrade.c.

571 {
572  /*
573  * Copy old commit logs to new data dir. pg_clog has been renamed to
574  * pg_xact in post-10 clusters.
575  */
577  "pg_clog" : "pg_xact",
579  "pg_clog" : "pg_xact");
580 
581  prep_status("Setting oldest XID for new cluster");
582  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
583  "\"%s/pg_resetwal\" -f -u %u \"%s\"",
586  check_ok();
587 
588  /* set the next transaction id and epoch of the new cluster */
589  prep_status("Setting next transaction ID and epoch for new cluster");
590  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
591  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
594  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
595  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
598  /* must reset commit timestamp limits also */
599  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
600  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
605  check_ok();
606 
607  /*
608  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
609  * (see pg_upgrade.h) and the new server is after, then we don't copy
610  * pg_multixact files, but we need to reset pg_control so that the new
611  * server doesn't attempt to read multis older than the cutoff value.
612  */
615  {
616  copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
617  copy_subdir_files("pg_multixact/members", "pg_multixact/members");
618 
619  prep_status("Setting next multixact ID and offset for new cluster");
620 
621  /*
622  * we preserve all files and contents, so we must preserve both "next"
623  * counters here and the oldest multi present on system.
624  */
625  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
626  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
632  check_ok();
633  }
635  {
636  /*
637  * Remove offsets/0000 file created by initdb that no longer matches
638  * the new multi-xid value. "members" starts at zero so no need to
639  * remove it.
640  */
641  remove_new_subdir("pg_multixact/offsets", false);
642 
643  prep_status("Setting oldest multixact ID in new cluster");
644 
645  /*
646  * We don't preserve files in this case, but it's important that the
647  * oldest multi is set to the latest value used by the old system, so
648  * that multixact.c returns the empty set for multis that might be
649  * present on disk. We set next multi to the value following that; it
650  * might end up wrapped around (i.e. 0) if the old cluster had
651  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
652  */
653  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
654  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
659  check_ok();
660  }
661 
662  /* now reset the wal archives in the new cluster */
663  prep_status("Resetting WAL archives");
664  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
665  /* use timeline 1 to match controldata and no WAL history file */
666  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
669  check_ok();
670 }
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:545
#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:254
char * bindir
Definition: pg_upgrade.h:259
uint32 major_version
Definition: pg_upgrade.h:264
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:202
char nextxlogfile[25]
Definition: pg_upgrade.h:201
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:206
uint32 cat_ver
Definition: pg_upgrade.h:200
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:205
uint32 chkpnt_oldstxid
Definition: pg_upgrade.h:208
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:203
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:207

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 422 of file pg_upgrade.c.

423 {
424  int dbnum;
425 
426  prep_status_progress("Restoring database schemas in the new cluster");
427 
428  /*
429  * We cannot process the template1 database concurrently with others,
430  * because when it's transiently dropped, connection attempts would fail.
431  * So handle it in a separate non-parallelized pass.
432  */
433  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
434  {
435  char sql_file_name[MAXPGPATH],
436  log_file_name[MAXPGPATH];
437  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
438  const char *create_opts;
439 
440  /* Process only template1 in this pass */
441  if (strcmp(old_db->db_name, "template1") != 0)
442  continue;
443 
444  pg_log(PG_STATUS, "%s", old_db->db_name);
445  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
446  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
447 
448  /*
449  * template1 database will already exist in the target installation,
450  * so tell pg_restore to drop and recreate it; otherwise we would fail
451  * to propagate its database-level properties.
452  */
453  create_opts = "--clean --create";
454 
455  exec_prog(log_file_name,
456  NULL,
457  true,
458  true,
459  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
460  "--dbname postgres \"%s/%s\"",
463  create_opts,
465  sql_file_name);
466 
467  break; /* done once we've processed template1 */
468  }
469 
470  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
471  {
472  char sql_file_name[MAXPGPATH],
473  log_file_name[MAXPGPATH];
474  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
475  const char *create_opts;
476 
477  /* Skip template1 in this pass */
478  if (strcmp(old_db->db_name, "template1") == 0)
479  continue;
480 
481  pg_log(PG_STATUS, "%s", old_db->db_name);
482  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
483  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
484 
485  /*
486  * postgres database will already exist in the target installation, so
487  * tell pg_restore to drop and recreate it; otherwise we would fail to
488  * propagate its database-level properties.
489  */
490  if (strcmp(old_db->db_name, "postgres") == 0)
491  create_opts = "--clean --create";
492  else
493  create_opts = "--create";
494 
495  parallel_exec_prog(log_file_name,
496  NULL,
497  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
498  "--dbname template1 \"%s/%s\"",
501  create_opts,
503  sql_file_name);
504  }
505 
506  /* reap all children */
507  while (reap_child(true) == true)
508  ;
509 
511  check_ok();
512 
513  /*
514  * We don't have minmxids for databases or relations in pre-9.3 clusters,
515  * so set those after we have restored the schema.
516  */
518  set_frozenxids(true);
519 
520  /* update new_cluster info now that we have objects in the databases */
522 }
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:274
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:695
#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:239
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:255
DbInfo * dbs
Definition: pg_upgrade.h:188
char * db_name
Definition: pg_upgrade.h:175
Oid db_oid
Definition: pg_upgrade.h:174
char * dumpdir
Definition: pg_upgrade.h:282

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 79 of file pg_upgrade.c.

80 {
81  char *deletion_script_file_name = NULL;
82  bool live_check = false;
83 
84  /*
85  * pg_upgrade doesn't currently use common/logging.c, but initialize it
86  * anyway because we might call common code that does.
87  */
88  pg_logging_init(argv[0]);
89  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
90 
91  /* Set default restrictive mask until new cluster permissions are read */
92  umask(PG_MODE_MASK_OWNER);
93 
94  parseCommandLine(argc, argv);
95 
97 
100 
101  /*
102  * Set mask based on PGDATA permissions, needed for the creation of the
103  * output directories with correct permissions.
104  */
106  pg_fatal("could not read permissions of directory \"%s\": %s",
107  new_cluster.pgdata, strerror(errno));
108 
109  umask(pg_mode_mask);
110 
111  /*
112  * This needs to happen after adjusting the data directory of the new
113  * cluster in adjust_data_dir().
114  */
116 
117  setup(argv[0], &live_check);
118 
119  output_check_banner(live_check);
120 
122 
123  get_sock_dir(&old_cluster, live_check);
124  get_sock_dir(&new_cluster, false);
125 
126  check_cluster_compatibility(live_check);
127 
128  check_and_dump_old_cluster(live_check);
129 
130 
131  /* -- NEW -- */
133 
136 
138  "\n"
139  "Performing Upgrade\n"
140  "------------------");
141 
143 
144  stop_postmaster(false);
145 
146  /*
147  * Destructive Changes to New Cluster
148  */
149 
151 
152  /* New now using xids of the old system */
153 
154  /* -- NEW -- */
156 
158 
160 
161  stop_postmaster(false);
162 
163  /*
164  * Most failures happen in create_new_objects(), which has completed at
165  * this point. We do this here because it is just before linking, which
166  * will link the old and new cluster data files, preventing the old
167  * cluster from being safely started once the new cluster is started.
168  */
171 
174 
175  /*
176  * Assuming OIDs are only used in system tables, there is no need to
177  * restore the OID counter because we have not transferred any OIDs from
178  * the old system, but we do it anyway just in case. We do it late here
179  * because there is no need to have the schema load use new oids.
180  */
181  prep_status("Setting next OID for new cluster");
182  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
183  "\"%s/pg_resetwal\" -o %u \"%s\"",
186  check_ok();
187 
188  if (user_opts.do_sync)
189  {
190  prep_status("Sync data directory to disk");
191  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
192  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
194  check_ok();
195  }
196 
197  create_script_for_old_cluster_deletion(&deletion_script_file_name);
198 
200 
202  "\n"
203  "Upgrade Complete\n"
204  "----------------");
205 
206  output_completion_banner(deletion_script_file_name);
207 
208  pg_free(deletion_script_file_name);
209 
211 
212  return 0;
213 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1204
void output_check_banner(bool live_check)
Definition: check.c:68
void check_cluster_versions(void)
Definition: check.c:296
void check_cluster_compatibility(bool live_check)
Definition: check.c:339
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:86
void issue_warnings_and_set_wal_level(void)
Definition: check.c:241
void check_new_cluster(void)
Definition: check.c:192
void report_clusters_compatible(void)
Definition: check.c:222
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:531
void output_completion_banner(char *deletion_script_file_name)
Definition: check.c:262
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:460
void disable_old_cluster(void)
Definition: controldata.c:712
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:221
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:370
static void create_new_objects(void)
Definition: pg_upgrade.c:422
static void copy_xact_xlog_xid(void)
Definition: pg_upgrade.c:570
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:306
static void prepare_new_globals(void)
Definition: pg_upgrade.c:400
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:230
@ PG_REPORT
Definition: pg_upgrade.h:241
void stop_postmaster(bool in_atexit)
Definition: server.c:323
#define strerror
Definition: port.h:251
void get_restricted_token(void)
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:456
UserOpts user_opts
Definition: option.c:29
void parseCommandLine(int argc, char *argv[])
Definition: option.c:38
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:204
bool do_sync
Definition: pg_upgrade.h:295
transferMode transfer_mode
Definition: pg_upgrade.h:296

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_pglocale_pgservice(), setup(), start_postmaster(), stop_postmaster(), strerror, 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 221 of file pg_upgrade.c.

222 {
223  FILE *fp;
224  char **filename;
225  time_t run_time = time(NULL);
226  char filename_path[MAXPGPATH];
227  char timebuf[128];
228  struct timeval time;
229  time_t tt;
230  int len;
231 
232  log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
233  len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
234  if (len >= MAXPGPATH)
235  pg_fatal("directory path for new cluster is too long");
236 
237  /* BASE_OUTPUTDIR/$timestamp/ */
238  gettimeofday(&time, NULL);
239  tt = (time_t) time.tv_sec;
240  strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
241  /* append milliseconds */
242  snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
243  ".%03d", (int) (time.tv_usec / 1000));
244  log_opts.basedir = (char *) pg_malloc0(MAXPGPATH);
246  timebuf);
247  if (len >= MAXPGPATH)
248  pg_fatal("directory path for new cluster is too long");
249 
250  /* BASE_OUTPUTDIR/$timestamp/dump/ */
251  log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
253  timebuf, DUMP_OUTPUTDIR);
254  if (len >= MAXPGPATH)
255  pg_fatal("directory path for new cluster is too long");
256 
257  /* BASE_OUTPUTDIR/$timestamp/log/ */
258  log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
260  timebuf, LOG_OUTPUTDIR);
261  if (len >= MAXPGPATH)
262  pg_fatal("directory path for new cluster is too long");
263 
264  /*
265  * Ignore the error case where the root path exists, as it is kept the
266  * same across runs.
267  */
268  if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
269  pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
271  pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
273  pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
275  pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
276 
277  len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
279  if (len >= sizeof(filename_path))
280  pg_fatal("directory path for new cluster is too long");
281 
282  if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
283  pg_fatal("could not open log file \"%s\": %m", filename_path);
284 
285  /* label start of upgrade in logfiles */
286  for (filename = output_files; *filename != NULL; filename++)
287  {
288  len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
290  if (len >= sizeof(filename_path))
291  pg_fatal("directory path for new cluster is too long");
292  if ((fp = fopen_priv(filename_path, "a")) == NULL)
293  pg_fatal("could not write to log file \"%s\": %m", filename_path);
294 
295  fprintf(fp,
296  "-----------------------------------------------------------------\n"
297  " pg_upgrade run on %s"
298  "-----------------------------------------------------------------\n\n",
299  ctime(&run_time));
300  fclose(fp);
301  }
302 }
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:66
#define LOG_OUTPUTDIR
Definition: pg_upgrade.h:40
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:382
#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:280
FILE * internal
Definition: pg_upgrade.h:276
char * basedir
Definition: pg_upgrade.h:281
char * logdir
Definition: pg_upgrade.h:283
#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 370 of file pg_upgrade.c.

371 {
372  /*
373  * It would make more sense to freeze after loading the schema, but that
374  * would cause us to lose the frozenxids restored by the load. We use
375  * --analyze so autovacuum doesn't update statistics later
376  */
377  prep_status("Analyzing all rows in the new cluster");
378  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
379  "\"%s/vacuumdb\" %s --all --analyze %s",
381  log_opts.verbose ? "--verbose" : "");
382  check_ok();
383 
384  /*
385  * We do freeze after analyze so pg_statistic is also frozen. template0 is
386  * not frozen here, but data rows were frozen by initdb, and we set its
387  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
388  * counter later.
389  */
390  prep_status("Freezing all rows in the new cluster");
391  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
392  "\"%s/vacuumdb\" %s --all --freeze %s",
394  log_opts.verbose ? "--verbose" : "");
395  check_ok();
396 }
bool verbose
Definition: pg_upgrade.h:277

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 400 of file pg_upgrade.c.

401 {
402  /*
403  * Before we restore anything, set frozenxids of initdb-created tables.
404  */
405  set_frozenxids(false);
406 
407  /*
408  * Now restore global objects (roles and tablespaces).
409  */
410  prep_status("Restoring global objects in the new cluster");
411 
412  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
413  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
417  check_ok();
418 }
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:30
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:360

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 528 of file pg_upgrade.c.

529 {
530  char new_path[MAXPGPATH];
531 
532  prep_status("Deleting files from new %s", subdir);
533 
534  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
535  if (!rmtree(new_path, rmtopdir))
536  pg_fatal("could not delete directory \"%s\"", new_path);
537 
538  check_ok();
539 }
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42

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 695 of file pg_upgrade.c.

696 {
697  int dbnum;
698  PGconn *conn,
699  *conn_template1;
700  PGresult *dbres;
701  int ntups;
702  int i_datname;
703  int i_datallowconn;
704 
705  if (!minmxid_only)
706  prep_status("Setting frozenxid and minmxid counters in new cluster");
707  else
708  prep_status("Setting minmxid counter in new cluster");
709 
710  conn_template1 = connectToServer(&new_cluster, "template1");
711 
712  if (!minmxid_only)
713  /* set pg_database.datfrozenxid */
714  PQclear(executeQueryOrDie(conn_template1,
715  "UPDATE pg_catalog.pg_database "
716  "SET datfrozenxid = '%u'",
718 
719  /* set pg_database.datminmxid */
720  PQclear(executeQueryOrDie(conn_template1,
721  "UPDATE pg_catalog.pg_database "
722  "SET datminmxid = '%u'",
724 
725  /* get database names */
726  dbres = executeQueryOrDie(conn_template1,
727  "SELECT datname, datallowconn "
728  "FROM pg_catalog.pg_database");
729 
730  i_datname = PQfnumber(dbres, "datname");
731  i_datallowconn = PQfnumber(dbres, "datallowconn");
732 
733  ntups = PQntuples(dbres);
734  for (dbnum = 0; dbnum < ntups; dbnum++)
735  {
736  char *datname = PQgetvalue(dbres, dbnum, i_datname);
737  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
738 
739  /*
740  * We must update databases where datallowconn = false, e.g.
741  * template0, because autovacuum increments their datfrozenxids,
742  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
743  * even though all the data rows are already frozen. To enable this,
744  * we temporarily change datallowconn.
745  */
746  if (strcmp(datallowconn, "f") == 0)
747  PQclear(executeQueryOrDie(conn_template1,
748  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
750 
752 
753  if (!minmxid_only)
754  /* set pg_class.relfrozenxid */
756  "UPDATE pg_catalog.pg_class "
757  "SET relfrozenxid = '%u' "
758  /* only heap, materialized view, and TOAST are vacuumed */
759  "WHERE relkind IN ("
760  CppAsString2(RELKIND_RELATION) ", "
761  CppAsString2(RELKIND_MATVIEW) ", "
762  CppAsString2(RELKIND_TOASTVALUE) ")",
764 
765  /* set pg_class.relminmxid */
767  "UPDATE pg_catalog.pg_class "
768  "SET relminmxid = '%u' "
769  /* only heap, materialized view, and TOAST are vacuumed */
770  "WHERE relkind IN ("
771  CppAsString2(RELKIND_RELATION) ", "
772  CppAsString2(RELKIND_MATVIEW) ", "
773  CppAsString2(RELKIND_TOASTVALUE) ")",
775  PQfinish(conn);
776 
777  /* Reset datallowconn flag */
778  if (strcmp(datallowconn, "f") == 0)
779  PQclear(executeQueryOrDie(conn_template1,
780  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
782  }
783 
784  PQclear(dbres);
785 
786  PQfinish(conn_template1);
787 
788  check_ok();
789 }
#define CppAsString2(x)
Definition: c.h:311
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4130
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3418
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:11529
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().

◆ setup()

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

Definition at line 306 of file pg_upgrade.c.

307 {
308  /*
309  * make sure the user has a clean environment, otherwise, we may confuse
310  * libpq when we connect to one (or both) of the servers.
311  */
313 
314  /*
315  * In case the user hasn't specified the directory for the new binaries
316  * with -B, default to using the path of the currently executed pg_upgrade
317  * binary.
318  */
319  if (!new_cluster.bindir)
320  {
321  char exec_path[MAXPGPATH];
322 
323  if (find_my_exec(argv0, exec_path) < 0)
324  pg_fatal("%s: could not find own program executable", argv0);
325  /* Trim off program name and keep just path */
326  *last_dir_separator(exec_path) = '\0';
329  }
330 
332 
333  /* no postmasters should be running, except for a live check */
335  {
336  /*
337  * If we have a postmaster.pid file, try to start the server. If it
338  * starts, the pid file was stale, so stop the server. If it doesn't
339  * start, assume the server is running. If the pid file is left over
340  * from a server crash, this also allows any committed transactions
341  * stored in the WAL to be replayed so they are not lost, because WAL
342  * files are not transferred from old to new servers. We later check
343  * for a clean shutdown.
344  */
345  if (start_postmaster(&old_cluster, false))
346  stop_postmaster(false);
347  else
348  {
349  if (!user_opts.check)
350  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
351  "Please shutdown that postmaster and try again.");
352  else
353  *live_check = true;
354  }
355  }
356 
357  /* same goes for the new postmaster */
359  {
360  if (start_postmaster(&new_cluster, false))
361  stop_postmaster(false);
362  else
363  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
364  "Please shutdown that postmaster and try again.");
365  }
366 }
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:152
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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:350
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:293

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 66 of file pg_upgrade.c.

Referenced by make_outputdirs().