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

752 {
753  fclose(log_opts.internal);
754 
755  /* Remove dump and log files? */
756  if (!log_opts.retain)
757  (void) rmtree(log_opts.basedir, true);
758 }
LogOpts log_opts
Definition: util.c:17
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
bool retain
Definition: pg_upgrade.h:277
FILE * internal
Definition: pg_upgrade.h:275
char * basedir
Definition: pg_upgrade.h:279

References LogOpts::basedir, LogOpts::internal, log_opts, LogOpts::retain, and rmtree().

Referenced by main().

◆ copy_subdir_files()

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

Definition at line 503 of file pg_upgrade.c.

504 {
505  char old_path[MAXPGPATH];
506  char new_path[MAXPGPATH];
507 
508  remove_new_subdir(new_subdir, true);
509 
510  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
511  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
512 
513  prep_status("Copying old %s to new server", old_subdir);
514 
515  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
516 #ifndef WIN32
517  "cp -Rf \"%s\" \"%s\"",
518 #else
519  /* flags: everything, no confirm, quiet, overwrite read-only */
520  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
521 #endif
522  old_path, new_path);
523 
524  check_ok();
525 }
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:81
static void check_ok(void)
Definition: initdb.c:1908
#define MAXPGPATH
ClusterInfo new_cluster
Definition: pg_upgrade.c:64
static void remove_new_subdir(const char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:486
ClusterInfo old_cluster
Definition: pg_upgrade.c:63
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:42
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define snprintf
Definition: port.h:225
char * pgdata
Definition: pg_upgrade.h:255

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

529 {
530  /*
531  * Copy old commit logs to new data dir. pg_clog has been renamed to
532  * pg_xact in post-10 clusters.
533  */
535  "pg_clog" : "pg_xact",
537  "pg_clog" : "pg_xact");
538 
539  prep_status("Setting oldest XID for new cluster");
540  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
541  "\"%s/pg_resetwal\" -f -u %u \"%s\"",
544  check_ok();
545 
546  /* set the next transaction id and epoch of the new cluster */
547  prep_status("Setting next transaction ID and epoch for new cluster");
548  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
549  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
552  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
553  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
556  /* must reset commit timestamp limits also */
557  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
558  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
563  check_ok();
564 
565  /*
566  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
567  * (see pg_upgrade.h) and the new server is after, then we don't copy
568  * pg_multixact files, but we need to reset pg_control so that the new
569  * server doesn't attempt to read multis older than the cutoff value.
570  */
573  {
574  copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
575  copy_subdir_files("pg_multixact/members", "pg_multixact/members");
576 
577  prep_status("Setting next multixact ID and offset for new cluster");
578 
579  /*
580  * we preserve all files and contents, so we must preserve both "next"
581  * counters here and the oldest multi present on system.
582  */
583  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
584  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
590  check_ok();
591  }
593  {
594  /*
595  * Remove offsets/0000 file created by initdb that no longer matches
596  * the new multi-xid value. "members" starts at zero so no need to
597  * remove it.
598  */
599  remove_new_subdir("pg_multixact/offsets", false);
600 
601  prep_status("Setting oldest multixact ID in new cluster");
602 
603  /*
604  * We don't preserve files in this case, but it's important that the
605  * oldest multi is set to the latest value used by the old system, so
606  * that multixact.c returns the empty set for multis that might be
607  * present on disk. We set next multi to the value following that; it
608  * might end up wrapped around (i.e. 0) if the old cluster had
609  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
610  */
611  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
612  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
617  check_ok();
618  }
619 
620  /* now reset the wal archives in the new cluster */
621  prep_status("Resetting WAL archives");
622  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
623  /* use timeline 1 to match controldata and no WAL history file */
624  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
627  check_ok();
628 }
static void copy_subdir_files(const char *old_subdir, const char *new_subdir)
Definition: pg_upgrade.c:503
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:113
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:26
ControlData controldata
Definition: pg_upgrade.h:253
char * bindir
Definition: pg_upgrade.h:258
uint32 major_version
Definition: pg_upgrade.h:263
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:199
char nextxlogfile[25]
Definition: pg_upgrade.h:198
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:203
uint32 cat_ver
Definition: pg_upgrade.h:197
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:202
uint32 chkpnt_oldstxid
Definition: pg_upgrade.h:205
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:200
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:204

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

381 {
382  int dbnum;
383 
384  prep_status_progress("Restoring database schemas in the new cluster");
385 
386  /*
387  * We cannot process the template1 database concurrently with others,
388  * because when it's transiently dropped, connection attempts would fail.
389  * So handle it in a separate non-parallelized pass.
390  */
391  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
392  {
393  char sql_file_name[MAXPGPATH],
394  log_file_name[MAXPGPATH];
395  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
396  const char *create_opts;
397 
398  /* Process only template1 in this pass */
399  if (strcmp(old_db->db_name, "template1") != 0)
400  continue;
401 
402  pg_log(PG_STATUS, "%s", old_db->db_name);
403  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
404  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
405 
406  /*
407  * template1 database will already exist in the target installation,
408  * so tell pg_restore to drop and recreate it; otherwise we would fail
409  * to propagate its database-level properties.
410  */
411  create_opts = "--clean --create";
412 
413  exec_prog(log_file_name,
414  NULL,
415  true,
416  true,
417  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
418  "--dbname postgres \"%s/%s\"",
421  create_opts,
423  sql_file_name);
424 
425  break; /* done once we've processed template1 */
426  }
427 
428  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
429  {
430  char sql_file_name[MAXPGPATH],
431  log_file_name[MAXPGPATH];
432  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
433  const char *create_opts;
434 
435  /* Skip template1 in this pass */
436  if (strcmp(old_db->db_name, "template1") == 0)
437  continue;
438 
439  pg_log(PG_STATUS, "%s", old_db->db_name);
440  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
441  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
442 
443  /*
444  * postgres database will already exist in the target installation, so
445  * tell pg_restore to drop and recreate it; otherwise we would fail to
446  * propagate its database-level properties.
447  */
448  if (strcmp(old_db->db_name, "postgres") == 0)
449  create_opts = "--clean --create";
450  else
451  create_opts = "--create";
452 
453  parallel_exec_prog(log_file_name,
454  NULL,
455  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
456  "--dbname template1 \"%s/%s\"",
459  create_opts,
461  sql_file_name);
462  }
463 
464  /* reap all children */
465  while (reap_child(true) == true)
466  ;
467 
469  check_ok();
470 
471  /*
472  * We don't have minmxids for databases or relations in pre-9.3 clusters,
473  * so set those after we have restored the schema.
474  */
476  set_frozenxids(true);
477 
478  /* update new_cluster info now that we have objects in the databases */
480 }
bool reap_child(bool wait_for_child)
Definition: parallel.c:284
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:653
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:40
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
void void prep_status_progress(const char *fmt,...) pg_attribute_printf(1
@ PG_STATUS
Definition: pg_upgrade.h:236
void end_progress_output(void)
Definition: util.c:42
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:92
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:30
DbInfoArr dbarr
Definition: pg_upgrade.h:254
DbInfo * dbs
Definition: pg_upgrade.h:185
char * db_name
Definition: pg_upgrade.h:172
Oid db_oid
Definition: pg_upgrade.h:171
char * dumpdir
Definition: pg_upgrade.h:280

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

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 80 of file pg_upgrade.c.

81 {
82  char *deletion_script_file_name = NULL;
83  bool live_check = false;
84 
85  pg_logging_init(argv[0]);
86  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
87 
88  /* Set default restrictive mask until new cluster permissions are read */
89  umask(PG_MODE_MASK_OWNER);
90 
91  parseCommandLine(argc, argv);
92 
94 
97 
98  /*
99  * Set mask based on PGDATA permissions, needed for the creation of the
100  * output directories with correct permissions.
101  */
103  pg_fatal("could not read permissions of directory \"%s\": %s\n",
104  new_cluster.pgdata, strerror(errno));
105 
106  umask(pg_mode_mask);
107 
108  /*
109  * This needs to happen after adjusting the data directory of the new
110  * cluster in adjust_data_dir().
111  */
113 
114  setup(argv[0], &live_check);
115 
116  output_check_banner(live_check);
117 
119 
120  get_sock_dir(&old_cluster, live_check);
121  get_sock_dir(&new_cluster, false);
122 
123  check_cluster_compatibility(live_check);
124 
125  check_and_dump_old_cluster(live_check);
126 
127 
128  /* -- NEW -- */
130 
133 
135  "\n"
136  "Performing Upgrade\n"
137  "------------------\n");
138 
140 
141  stop_postmaster(false);
142 
143  /*
144  * Destructive Changes to New Cluster
145  */
146 
148 
149  /* New now using xids of the old system */
150 
151  /* -- NEW -- */
153 
155 
157 
158  stop_postmaster(false);
159 
160  /*
161  * Most failures happen in create_new_objects(), which has completed at
162  * this point. We do this here because it is just before linking, which
163  * will link the old and new cluster data files, preventing the old
164  * cluster from being safely started once the new cluster is started.
165  */
168 
171 
172  /*
173  * Assuming OIDs are only used in system tables, there is no need to
174  * restore the OID counter because we have not transferred any OIDs from
175  * the old system, but we do it anyway just in case. We do it late here
176  * because there is no need to have the schema load use new oids.
177  */
178  prep_status("Setting next OID for new cluster");
179  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
180  "\"%s/pg_resetwal\" -o %u \"%s\"",
183  check_ok();
184 
185  if (user_opts.do_sync)
186  {
187  prep_status("Sync data directory to disk");
188  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
189  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
191  check_ok();
192  }
193 
194  create_script_for_old_cluster_deletion(&deletion_script_file_name);
195 
197 
199  "\n"
200  "Upgrade Complete\n"
201  "----------------\n");
202 
203  output_completion_banner(deletion_script_file_name);
204 
205  pg_free(deletion_script_file_name);
206 
207  cleanup();
208 
209  return 0;
210 }
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1212
void output_check_banner(bool live_check)
Definition: check.c:66
void check_cluster_versions(void)
Definition: check.c:278
void check_cluster_compatibility(bool live_check)
Definition: check.c:321
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:84
void issue_warnings_and_set_wal_level(void)
Definition: check.c:223
void check_new_cluster(void)
Definition: check.c:176
void report_clusters_compatible(void)
Definition: check.c:206
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:513
void output_completion_banner(char *deletion_script_file_name)
Definition: check.c:244
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:446
void disable_old_cluster(void)
Definition: controldata.c:705
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:2211
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:445
static void make_outputdirs(char *pgdata)
Definition: pg_upgrade.c:218
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:328
static void cleanup(void)
Definition: pg_upgrade.c:751
static void create_new_objects(void)
Definition: pg_upgrade.c:380
static void copy_xact_xlog_xid(void)
Definition: pg_upgrade.c:528
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:264
static void prepare_new_globals(void)
Definition: pg_upgrade.c:358
void transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
Definition: relfilenode.c:29
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:227
@ PG_REPORT
Definition: pg_upgrade.h:237
void stop_postmaster(bool in_atexit)
Definition: server.c:323
#define strerror
Definition: port.h:238
void get_restricted_token(void)
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:445
UserOpts user_opts
Definition: option.c:29
void parseCommandLine(int argc, char *argv[])
Definition: option.c:38
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:201
bool do_sync
Definition: pg_upgrade.h:293
transferMode transfer_mode
Definition: pg_upgrade.h:294

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

219 {
220  FILE *fp;
221  char **filename;
222  time_t run_time = time(NULL);
223  char filename_path[MAXPGPATH];
224 
225  log_opts.basedir = (char *) pg_malloc(MAXPGPATH);
226  snprintf(log_opts.basedir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
227  log_opts.dumpdir = (char *) pg_malloc(MAXPGPATH);
228  snprintf(log_opts.dumpdir, MAXPGPATH, "%s/%s", pgdata, DUMP_OUTPUTDIR);
229  log_opts.logdir = (char *) pg_malloc(MAXPGPATH);
230  snprintf(log_opts.logdir, MAXPGPATH, "%s/%s", pgdata, LOG_OUTPUTDIR);
231 
233  pg_fatal("could not create directory \"%s\": %m\n", log_opts.basedir);
235  pg_fatal("could not create directory \"%s\": %m\n", log_opts.dumpdir);
237  pg_fatal("could not create directory \"%s\": %m\n", log_opts.logdir);
238 
239  snprintf(filename_path, sizeof(filename_path), "%s/%s", log_opts.logdir,
241  if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
242  pg_fatal("could not open log file \"%s\": %m\n", filename_path);
243 
244  /* label start of upgrade in logfiles */
245  for (filename = output_files; *filename != NULL; filename++)
246  {
247  snprintf(filename_path, sizeof(filename_path), "%s/%s",
249  if ((fp = fopen_priv(filename_path, "a")) == NULL)
250  pg_fatal("could not write to log file \"%s\": %m\n", filename_path);
251 
252  /* Start with newline because we might be appending to a file. */
253  fprintf(fp, "\n"
254  "-----------------------------------------------------------------\n"
255  " pg_upgrade run on %s"
256  "-----------------------------------------------------------------\n\n",
257  ctime(&run_time));
258  fclose(fp);
259  }
260 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int pg_dir_create_mode
Definition: file_perm.c:18
static char * filename
Definition: pg_dumpall.c:94
char * output_files[]
Definition: pg_upgrade.c:67
#define LOG_OUTPUTDIR
Definition: pg_upgrade.h:37
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:380
#define DUMP_OUTPUTDIR
Definition: pg_upgrade.h:38
#define BASE_OUTPUTDIR
Definition: pg_upgrade.h:36
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:43
#define fprintf
Definition: port.h:229
char * logdir
Definition: pg_upgrade.h:281
#define mkdir(a, b)
Definition: win32_port.h:71

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

Referenced by main().

◆ prepare_new_cluster()

static void prepare_new_cluster ( void  )
static

Definition at line 328 of file pg_upgrade.c.

329 {
330  /*
331  * It would make more sense to freeze after loading the schema, but that
332  * would cause us to lose the frozenxids restored by the load. We use
333  * --analyze so autovacuum doesn't update statistics later
334  */
335  prep_status("Analyzing all rows in the new cluster");
336  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
337  "\"%s/vacuumdb\" %s --all --analyze %s",
339  log_opts.verbose ? "--verbose" : "");
340  check_ok();
341 
342  /*
343  * We do freeze after analyze so pg_statistic is also frozen. template0 is
344  * not frozen here, but data rows were frozen by initdb, and we set its
345  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
346  * counter later.
347  */
348  prep_status("Freezing all rows in the new cluster");
349  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
350  "\"%s/vacuumdb\" %s --all --freeze %s",
352  log_opts.verbose ? "--verbose" : "");
353  check_ok();
354 }
bool verbose
Definition: pg_upgrade.h:276

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

359 {
360  /*
361  * Before we restore anything, set frozenxids of initdb-created tables.
362  */
363  set_frozenxids(false);
364 
365  /*
366  * Now restore global objects (roles and tablespaces).
367  */
368  prep_status("Restoring global objects in the new cluster");
369 
370  exec_prog(UTILITY_LOG_FILE, NULL, true, true,
371  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
375  check_ok();
376 }
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:29
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:358

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

487 {
488  char new_path[MAXPGPATH];
489 
490  prep_status("Deleting files from new %s", subdir);
491 
492  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
493  if (!rmtree(new_path, rmtopdir))
494  pg_fatal("could not delete directory \"%s\"\n", new_path);
495 
496  check_ok();
497 }

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

654 {
655  int dbnum;
656  PGconn *conn,
657  *conn_template1;
658  PGresult *dbres;
659  int ntups;
660  int i_datname;
661  int i_datallowconn;
662 
663  if (!minmxid_only)
664  prep_status("Setting frozenxid and minmxid counters in new cluster");
665  else
666  prep_status("Setting minmxid counter in new cluster");
667 
668  conn_template1 = connectToServer(&new_cluster, "template1");
669 
670  if (!minmxid_only)
671  /* set pg_database.datfrozenxid */
672  PQclear(executeQueryOrDie(conn_template1,
673  "UPDATE pg_catalog.pg_database "
674  "SET datfrozenxid = '%u'",
676 
677  /* set pg_database.datminmxid */
678  PQclear(executeQueryOrDie(conn_template1,
679  "UPDATE pg_catalog.pg_database "
680  "SET datminmxid = '%u'",
682 
683  /* get database names */
684  dbres = executeQueryOrDie(conn_template1,
685  "SELECT datname, datallowconn "
686  "FROM pg_catalog.pg_database");
687 
688  i_datname = PQfnumber(dbres, "datname");
689  i_datallowconn = PQfnumber(dbres, "datallowconn");
690 
691  ntups = PQntuples(dbres);
692  for (dbnum = 0; dbnum < ntups; dbnum++)
693  {
694  char *datname = PQgetvalue(dbres, dbnum, i_datname);
695  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
696 
697  /*
698  * We must update databases where datallowconn = false, e.g.
699  * template0, because autovacuum increments their datfrozenxids,
700  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
701  * even though all the data rows are already frozen. To enable this,
702  * we temporarily change datallowconn.
703  */
704  if (strcmp(datallowconn, "f") == 0)
705  PQclear(executeQueryOrDie(conn_template1,
706  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
708 
710 
711  if (!minmxid_only)
712  /* set pg_class.relfrozenxid */
714  "UPDATE pg_catalog.pg_class "
715  "SET relfrozenxid = '%u' "
716  /* only heap, materialized view, and TOAST are vacuumed */
717  "WHERE relkind IN ("
718  CppAsString2(RELKIND_RELATION) ", "
719  CppAsString2(RELKIND_MATVIEW) ", "
720  CppAsString2(RELKIND_TOASTVALUE) ")",
722 
723  /* set pg_class.relminmxid */
725  "UPDATE pg_catalog.pg_class "
726  "SET relminmxid = '%u' "
727  /* only heap, materialized view, and TOAST are vacuumed */
728  "WHERE relkind IN ("
729  CppAsString2(RELKIND_RELATION) ", "
730  CppAsString2(RELKIND_MATVIEW) ", "
731  CppAsString2(RELKIND_TOASTVALUE) ")",
733  PQfinish(conn);
734 
735  /* Reset datallowconn flag */
736  if (strcmp(datallowconn, "f") == 0)
737  PQclear(executeQueryOrDie(conn_template1,
738  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
740  }
741 
742  PQclear(dbres);
743 
744  PQfinish(conn_template1);
745 
746  check_ok();
747 }
#define CppAsString2(x)
Definition: c.h:289
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4261
void PQclear(PGresult *res)
Definition: fe-exec.c:718
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3340
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3735
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3448
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:12192
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 264 of file pg_upgrade.c.

265 {
266  /*
267  * make sure the user has a clean environment, otherwise, we may confuse
268  * libpq when we connect to one (or both) of the servers.
269  */
271 
272  /*
273  * In case the user hasn't specified the directory for the new binaries
274  * with -B, default to using the path of the currently executed pg_upgrade
275  * binary.
276  */
277  if (!new_cluster.bindir)
278  {
279  char exec_path[MAXPGPATH];
280 
281  if (find_my_exec(argv0, exec_path) < 0)
282  pg_fatal("%s: could not find own program executable\n", argv0);
283  /* Trim off program name and keep just path */
284  *last_dir_separator(exec_path) = '\0';
287  }
288 
290 
291  /* no postmasters should be running, except for a live check */
293  {
294  /*
295  * If we have a postmaster.pid file, try to start the server. If it
296  * starts, the pid file was stale, so stop the server. If it doesn't
297  * start, assume the server is running. If the pid file is left over
298  * from a server crash, this also allows any committed transactions
299  * stored in the WAL to be replayed so they are not lost, because WAL
300  * files are not transferred from old to new servers. We later check
301  * for a clean shutdown.
302  */
303  if (start_postmaster(&old_cluster, false))
304  stop_postmaster(false);
305  else
306  {
307  if (!user_opts.check)
308  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
309  "Please shutdown that postmaster and try again.\n");
310  else
311  *live_check = true;
312  }
313  }
314 
315  /* same goes for the new postmaster */
317  {
318  if (start_postmaster(&new_cluster, false))
319  stop_postmaster(false);
320  else
321  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
322  "Please shutdown that postmaster and try again.\n");
323  }
324 }
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:223
void verify_directories(void)
Definition: exec.c:254
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:140
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * argv0
Definition: pg_ctl.c:97
static char * exec_path
Definition: pg_ctl.c:92
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:291

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:41

Definition at line 67 of file pg_upgrade.c.

Referenced by make_outputdirs().