PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_upgrade.c File Reference
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_databases (void)
 
static void create_new_objects (void)
 
static void copy_clog_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 (char *subdir, bool rmtopdir)
 
static void copy_subdir_files (char *subdir)
 

Variables

ClusterInfo old_cluster
 
ClusterInfo new_cluster
 
OSInfo os_info
 
char * output_files []
 

Function Documentation

static void cleanup ( void  )
static

Definition at line 596 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, NULL, output_files, LogOpts::retain, snprintf(), and unlink().

Referenced by main().

597 {
598  fclose(log_opts.internal);
599 
600  /* Remove dump and log files? */
601  if (!log_opts.retain)
602  {
603  int dbnum;
604  char **filename;
605 
606  for (filename = output_files; *filename != NULL; filename++)
607  unlink(*filename);
608 
609  /* remove dump files */
611 
612  if (old_cluster.dbarr.dbs)
613  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
614  {
615  char sql_file_name[MAXPGPATH],
616  log_file_name[MAXPGPATH];
617  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
618 
619  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
620  unlink(sql_file_name);
621 
622  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
623  unlink(log_file_name);
624  }
625  }
626 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
LogOpts log_opts
Definition: util.c:18
Oid db_oid
Definition: pg_upgrade.h:186
#define MAXPGPATH
char * output_files[]
Definition: pg_upgrade.c:59
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
int unlink(const char *filename)
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:33
DbInfoArr dbarr
Definition: pg_upgrade.h:265
#define NULL
Definition: c.h:226
FILE * internal
Definition: pg_upgrade.h:286
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:35
static char * filename
Definition: pg_dumpall.c:80
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:32
DbInfo * dbs
Definition: pg_upgrade.h:198
bool retain
Definition: pg_upgrade.h:288
static void copy_clog_xlog_xid ( void  )
static

Definition at line 403 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(), MULTIXACT_FORMATCHANGE_CAT_VER, ControlData::nextxlogfile, NULL, ClusterInfo::pgdata, prep_status(), remove_new_subdir(), and UTILITY_LOG_FILE.

Referenced by main().

404 {
405  /* copy old commit logs to new data dir */
406  copy_subdir_files("pg_clog");
407 
408  /* set the next transaction id and epoch of the new cluster */
409  prep_status("Setting next transaction ID and epoch for new cluster");
411  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
415  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
418  /* must reset commit timestamp limits also */
420  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
425  check_ok();
426 
427  /*
428  * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
429  * (see pg_upgrade.h) and the new server is after, then we don't copy
430  * pg_multixact files, but we need to reset pg_control so that the new
431  * server doesn't attempt to read multis older than the cutoff value.
432  */
435  {
436  copy_subdir_files("pg_multixact/offsets");
437  copy_subdir_files("pg_multixact/members");
438 
439  prep_status("Setting next multixact ID and offset for new cluster");
440 
441  /*
442  * we preserve all files and contents, so we must preserve both "next"
443  * counters here and the oldest multi present on system.
444  */
446  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
452  check_ok();
453  }
455  {
456  /*
457  * Remove offsets/0000 file created by initdb that no longer matches
458  * the new multi-xid value. "members" starts at zero so no need to
459  * remove it.
460  */
461  remove_new_subdir("pg_multixact/offsets", false);
462 
463  prep_status("Setting oldest multixact ID on new cluster");
464 
465  /*
466  * We don't preserve files in this case, but it's important that the
467  * oldest multi is set to the latest value used by the old system, so
468  * that multixact.c returns the empty set for multis that might be
469  * present on disk. We set next multi to the value following that; it
470  * might end up wrapped around (i.e. 0) if the old cluster had
471  * next=MaxMultiXactId, but multixact.c can cope with that just fine.
472  */
474  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
479  check_ok();
480  }
481 
482  /* now reset the wal archives in the new cluster */
483  prep_status("Resetting WAL archives");
485  /* use timeline 1 to match controldata and no WAL history file */
486  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
489  check_ok();
490 }
ControlData controldata
Definition: pg_upgrade.h:264
char nextxlogfile[25]
Definition: pg_upgrade.h:211
static void copy_subdir_files(char *subdir)
Definition: pg_upgrade.c:378
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
uint32 chkpnt_nxtmxoff
Definition: pg_upgrade.h:216
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:212
uint32 chkpnt_nxtepoch
Definition: pg_upgrade.h:213
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
static void check_ok(void)
Definition: initdb.c:1987
static void remove_new_subdir(char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:361
uint32 chkpnt_oldstMulti
Definition: pg_upgrade.h:217
char * bindir
Definition: pg_upgrade.h:269
uint32 cat_ver
Definition: pg_upgrade.h:210
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:215
#define NULL
Definition: c.h:226
#define MULTIXACT_FORMATCHANGE_CAT_VER
Definition: pg_upgrade.h:122
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
char * pgdata
Definition: pg_upgrade.h:266
static void copy_subdir_files ( char *  subdir)
static

Definition at line 378 of file pg_upgrade.c.

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

Referenced by copy_clog_xlog_xid().

379 {
380  char old_path[MAXPGPATH];
381  char new_path[MAXPGPATH];
382 
383  remove_new_subdir(subdir, true);
384 
385  snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
386  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
387 
388  prep_status("Copying old %s to new server", subdir);
389 
391 #ifndef WIN32
392  "cp -Rf \"%s\" \"%s\"",
393 #else
394  /* flags: everything, no confirm, quiet, overwrite read-only */
395  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
396 #endif
397  old_path, new_path);
398 
399  check_ok();
400 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
static void check_ok(void)
Definition: initdb.c:1987
static void remove_new_subdir(char *subdir, bool rmtopdir)
Definition: pg_upgrade.c:361
#define NULL
Definition: c.h:226
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
char * pgdata
Definition: pg_upgrade.h:266
static void create_new_objects ( void  )
static

Definition at line 299 of file pg_upgrade.c.

References appendConnStrVal(), appendPQExpBuffer(), appendShellString(), ClusterInfo::bindir, check_ok(), cluster_conn_opts(), connstr, PQExpBufferData::data, DB_DUMP_FILE_MASK, DB_DUMP_LOG_FILE_MASK, DbInfo::db_name, DbInfo::db_oid, ClusterInfo::dbarr, DbInfoArr::dbs, end_progress_output(), get_db_and_rel_infos(), GET_MAJOR_VERSION, initPQExpBuffer(), ClusterInfo::major_version, MAXPGPATH, DbInfoArr::ndbs, NULL, parallel_exec_prog(), pg_log(), PG_STATUS, prep_status(), reap_child(), set_frozenxids(), snprintf(), and termPQExpBuffer().

Referenced by main().

300 {
301  int dbnum;
302 
303  prep_status("Restoring database schemas in the new cluster\n");
304 
305  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
306  {
307  char sql_file_name[MAXPGPATH],
308  log_file_name[MAXPGPATH];
309  DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
311  escaped_connstr;
312 
313  initPQExpBuffer(&connstr);
314  appendPQExpBuffer(&connstr, "dbname=");
315  appendConnStrVal(&connstr, old_db->db_name);
316  initPQExpBuffer(&escaped_connstr);
317  appendShellString(&escaped_connstr, connstr.data);
318  termPQExpBuffer(&connstr);
319 
320  pg_log(PG_STATUS, "%s", old_db->db_name);
321  snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
322  snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
323 
324  /*
325  * pg_dump only produces its output at the end, so there is little
326  * parallelism if using the pipe.
327  */
328  parallel_exec_prog(log_file_name,
329  NULL,
330  "\"%s/pg_restore\" %s --exit-on-error --verbose --dbname %s \"%s\"",
333  escaped_connstr.data,
334  sql_file_name);
335 
336  termPQExpBuffer(&escaped_connstr);
337  }
338 
339  /* reap all children */
340  while (reap_child(true) == true)
341  ;
342 
344  check_ok();
345 
346  /*
347  * We don't have minmxids for databases or relations in pre-9.3 clusters,
348  * so set those after we have restored the schema.
349  */
351  set_frozenxids(true);
352 
353  /* regenerate now that we have objects in the databases */
355 }
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:274
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
void end_progress_output(void)
Definition: util.c:44
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:536
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:88
Oid db_oid
Definition: pg_upgrade.h:186
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
#define MAXPGPATH
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
static void check_ok(void)
Definition: initdb.c:1987
#define DB_DUMP_FILE_MASK
Definition: pg_upgrade.h:33
char * bindir
Definition: pg_upgrade.h:269
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:504
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:430
DbInfoArr dbarr
Definition: pg_upgrade.h:265
#define NULL
Definition: c.h:226
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:310
#define DB_DUMP_LOG_FILE_MASK
Definition: pg_upgrade.h:35
bool reap_child(bool wait_for_child)
Definition: parallel.c:290
char * db_name
Definition: pg_upgrade.h:187
DbInfo * dbs
Definition: pg_upgrade.h:198
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:63
int main ( int  argc,
char **  argv 
)

Definition at line 72 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_clog_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(), issue_warnings(), NULL, output_check_banner(), output_completion_banner(), parseCommandLine(), pg_free(), pg_log(), PG_REPORT, PG_TEXTDOMAIN, ClusterInfo::pgdata, prep_status(), prepare_new_cluster(), prepare_new_databases(), OSInfo::progname, report_clusters_compatible(), set_pglocale_pgservice(), setup(), start_postmaster(), stop_postmaster(), transfer_all_new_tablespaces(), UserOpts::transfer_mode, TRANSFER_MODE_LINK, user_opts, and UTILITY_LOG_FILE.

73 {
74  char *analyze_script_file_name = NULL;
75  char *deletion_script_file_name = NULL;
76  bool live_check = false;
77 
78  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
79  parseCommandLine(argc, argv);
80 
82 
85 
86  setup(argv[0], &live_check);
87 
88  output_check_banner(live_check);
89 
91 
92  get_sock_dir(&old_cluster, live_check);
93  get_sock_dir(&new_cluster, false);
94 
95  check_cluster_compatibility(live_check);
96 
97  check_and_dump_old_cluster(live_check);
98 
99 
100  /* -- NEW -- */
102 
105 
106  pg_log(PG_REPORT, "\nPerforming Upgrade\n");
107  pg_log(PG_REPORT, "------------------\n");
108 
110 
111  stop_postmaster(false);
112 
113  /*
114  * Destructive Changes to New Cluster
115  */
116 
118 
119  /* New now using xids of the old system */
120 
121  /* -- NEW -- */
123 
125 
127 
128  stop_postmaster(false);
129 
130  /*
131  * Most failures happen in create_new_objects(), which has completed at
132  * this point. We do this here because it is just before linking, which
133  * will link the old and new cluster data files, preventing the old
134  * cluster from being safely started once the new cluster is started.
135  */
138 
141 
142  /*
143  * Assuming OIDs are only used in system tables, there is no need to
144  * restore the OID counter because we have not transferred any OIDs from
145  * the old system, but we do it anyway just in case. We do it late here
146  * because there is no need to have the schema load use new oids.
147  */
148  prep_status("Setting next OID for new cluster");
150  "\"%s/pg_resetwal\" -o %u \"%s\"",
153  check_ok();
154 
155  prep_status("Sync data directory to disk");
157  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
159  check_ok();
160 
161  create_script_for_cluster_analyze(&analyze_script_file_name);
162  create_script_for_old_cluster_deletion(&deletion_script_file_name);
163 
164  issue_warnings();
165 
166  pg_log(PG_REPORT, "\nUpgrade Complete\n");
167  pg_log(PG_REPORT, "----------------\n");
168 
169  output_completion_banner(analyze_script_file_name,
170  deletion_script_file_name);
171 
172  pg_free(analyze_script_file_name);
173  pg_free(deletion_script_file_name);
174 
175  cleanup();
176 
177  return 0;
178 }
static void copy_clog_xlog_xid(void)
Definition: pg_upgrade.c:403
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:38
static void cleanup(void)
Definition: pg_upgrade.c:596
ControlData controldata
Definition: pg_upgrade.h:264
void check_cluster_versions(void)
Definition: check.c:214
static void prepare_new_cluster(void)
Definition: pg_upgrade.c:239
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:445
static void create_new_objects(void)
Definition: pg_upgrade.c:299
uint32 chkpnt_nxtoid
Definition: pg_upgrade.h:214
void check_cluster_compatibility(bool live_check)
Definition: check.c:256
void issue_warnings(void)
Definition: check.c:170
static void adjust_data_dir(void)
Definition: pg_ctl.c:2084
void stop_postmaster(bool fast)
Definition: server.c:308
void get_restricted_token(const char *progname)
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
void output_check_banner(bool live_check)
Definition: check.c:61
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
static void check_ok(void)
Definition: initdb.c:1987
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:504
transferMode transfer_mode
Definition: pg_upgrade.h:299
UserOpts user_opts
Definition: option.c:29
char * bindir
Definition: pg_upgrade.h:269
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
static void prepare_new_databases(void)
Definition: pg_upgrade.c:269
static void setup(char *argv0, bool *live_check)
Definition: pg_upgrade.c:182
DbInfoArr dbarr
Definition: pg_upgrade.h:265
const char * progname
Definition: pg_upgrade.h:309
#define NULL
Definition: c.h:226
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:428
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
char * pgdata
Definition: pg_upgrade.h:266
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
void disable_old_cluster(void)
Definition: controldata.c:606
OSInfo os_info
Definition: pg_upgrade.c:57
void output_completion_banner(char *analyze_script_file_name, char *deletion_script_file_name)
Definition: check.c:183
void create_script_for_cluster_analyze(char **analyze_script_file_name)
Definition: check.c:414
void check_new_cluster(void)
Definition: check.c:134
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:77
void report_clusters_compatible(void)
Definition: check.c:153
static void prepare_new_cluster ( void  )
static

Definition at line 239 of file pg_upgrade.c.

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

Referenced by main().

240 {
241  /*
242  * It would make more sense to freeze after loading the schema, but that
243  * would cause us to lose the frozenids restored by the load. We use
244  * --analyze so autovacuum doesn't update statistics later
245  */
246  prep_status("Analyzing all rows in the new cluster");
248  "\"%s/vacuumdb\" %s --all --analyze %s",
250  log_opts.verbose ? "--verbose" : "");
251  check_ok();
252 
253  /*
254  * We do freeze after analyze so pg_statistic is also frozen. template0 is
255  * not frozen here, but data rows were frozen by initdb, and we set its
256  * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
257  * counter later.
258  */
259  prep_status("Freezing all rows on the new cluster");
261  "\"%s/vacuumdb\" %s --all --freeze %s",
263  log_opts.verbose ? "--verbose" : "");
264  check_ok();
265 }
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:88
LogOpts log_opts
Definition: util.c:18
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:1987
bool verbose
Definition: pg_upgrade.h:287
char * bindir
Definition: pg_upgrade.h:269
#define NULL
Definition: c.h:226
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
static void prepare_new_databases ( void  )
static

Definition at line 269 of file pg_upgrade.c.

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

Referenced by main().

270 {
271  /*
272  * We set autovacuum_freeze_max_age to its maximum value so autovacuum
273  * does not launch here and delete clog files, before the frozen xids are
274  * set.
275  */
276 
277  set_frozenxids(false);
278 
279  prep_status("Restoring global objects in the new cluster");
280 
281  /*
282  * We have to create the databases first so we can install support
283  * functions in all the other databases. Ideally we could create the
284  * support functions in template1 but pg_dumpall creates database using
285  * the template0 template.
286  */
288  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
291  check_ok();
292 
293  /* we load this to get a current list of databases */
295 }
PGresult char * cluster_conn_opts(ClusterInfo *cluster)
Definition: server.c:88
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:1987
char * bindir
Definition: pg_upgrade.h:269
static void set_frozenxids(bool minmxid_only)
Definition: pg_upgrade.c:504
#define NULL
Definition: c.h:226
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:310
#define UTILITY_LOG_FILE
Definition: pg_upgrade.h:37
bool exec_prog(const char *log_file, const char *opt_log_file, bool throw_error, const char *fmt,...)
Definition: exec.c:77
#define EXEC_PSQL_ARGS
Definition: pg_upgrade.h:360
#define GLOBALS_DUMP_FILE
Definition: pg_upgrade.h:32
static void remove_new_subdir ( char *  subdir,
bool  rmtopdir 
)
static

Definition at line 361 of file pg_upgrade.c.

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

Referenced by copy_clog_xlog_xid(), and copy_subdir_files().

362 {
363  char new_path[MAXPGPATH];
364 
365  prep_status("Deleting files from new %s", subdir);
366 
367  snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
368  if (!rmtree(new_path, rmtopdir))
369  pg_fatal("could not delete directory \"%s\"\n", new_path);
370 
371  check_ok();
372 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
#define MAXPGPATH
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_ok(void)
Definition: initdb.c:1987
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:36
char * pgdata
Definition: pg_upgrade.h:266
static void set_frozenxids ( bool  minmxid_only)
static

Definition at line 504 of file pg_upgrade.c.

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

Referenced by create_new_objects(), and prepare_new_databases().

505 {
506  int dbnum;
507  PGconn *conn,
508  *conn_template1;
509  PGresult *dbres;
510  int ntups;
511  int i_datname;
512  int i_datallowconn;
513 
514  if (!minmxid_only)
515  prep_status("Setting frozenxid and minmxid counters in new cluster");
516  else
517  prep_status("Setting minmxid counter in new cluster");
518 
519  conn_template1 = connectToServer(&new_cluster, "template1");
520 
521  if (!minmxid_only)
522  /* set pg_database.datfrozenxid */
523  PQclear(executeQueryOrDie(conn_template1,
524  "UPDATE pg_catalog.pg_database "
525  "SET datfrozenxid = '%u'",
527 
528  /* set pg_database.datminmxid */
529  PQclear(executeQueryOrDie(conn_template1,
530  "UPDATE pg_catalog.pg_database "
531  "SET datminmxid = '%u'",
533 
534  /* get database names */
535  dbres = executeQueryOrDie(conn_template1,
536  "SELECT datname, datallowconn "
537  "FROM pg_catalog.pg_database");
538 
539  i_datname = PQfnumber(dbres, "datname");
540  i_datallowconn = PQfnumber(dbres, "datallowconn");
541 
542  ntups = PQntuples(dbres);
543  for (dbnum = 0; dbnum < ntups; dbnum++)
544  {
545  char *datname = PQgetvalue(dbres, dbnum, i_datname);
546  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
547 
548  /*
549  * We must update databases where datallowconn = false, e.g.
550  * template0, because autovacuum increments their datfrozenxids,
551  * relfrozenxids, and relminmxid even if autovacuum is turned off, and
552  * even though all the data rows are already frozen. To enable this,
553  * we temporarily change datallowconn.
554  */
555  if (strcmp(datallowconn, "f") == 0)
556  PQclear(executeQueryOrDie(conn_template1,
557  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
558  quote_identifier(datname)));
559 
560  conn = connectToServer(&new_cluster, datname);
561 
562  if (!minmxid_only)
563  /* set pg_class.relfrozenxid */
565  "UPDATE pg_catalog.pg_class "
566  "SET relfrozenxid = '%u' "
567  /* only heap, materialized view, and TOAST are vacuumed */
568  "WHERE relkind IN ('r', 'm', 't')",
570 
571  /* set pg_class.relminmxid */
573  "UPDATE pg_catalog.pg_class "
574  "SET relminmxid = '%u' "
575  /* only heap, materialized view, and TOAST are vacuumed */
576  "WHERE relkind IN ('r', 'm', 't')",
578  PQfinish(conn);
579 
580  /* Reset datallowconn flag */
581  if (strcmp(datallowconn, "f") == 0)
582  PQclear(executeQueryOrDie(conn_template1,
583  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
584  quote_identifier(datname)));
585  }
586 
587  PQclear(dbres);
588 
589  PQfinish(conn_template1);
590 
591  check_ok();
592 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:9968
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
ControlData controldata
Definition: pg_upgrade.h:264
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
uint32 chkpnt_nxtxid
Definition: pg_upgrade.h:212
PGconn * conn
Definition: streamutil.c:42
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
static void check_ok(void)
Definition: initdb.c:1987
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
uint32 chkpnt_nxtmulti
Definition: pg_upgrade.h:215
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
static void setup ( char *  argv0,
bool live_check 
)
static

Definition at line 182 of file pg_upgrade.c.

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

Referenced by main().

183 {
184  char exec_path[MAXPGPATH]; /* full path to my executable */
185 
186  /*
187  * make sure the user has a clean environment, otherwise, we may confuse
188  * libpq when we connect to one (or both) of the servers.
189  */
191 
193 
194  /* no postmasters should be running, except for a live check */
196  {
197  /*
198  * If we have a postmaster.pid file, try to start the server. If it
199  * starts, the pid file was stale, so stop the server. If it doesn't
200  * start, assume the server is running. If the pid file is left over
201  * from a server crash, this also allows any committed transactions
202  * stored in the WAL to be replayed so they are not lost, because WAL
203  * files are not transferred from old to new servers.
204  */
205  if (start_postmaster(&old_cluster, false))
206  stop_postmaster(false);
207  else
208  {
209  if (!user_opts.check)
210  pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
211  "Please shutdown that postmaster and try again.\n");
212  else
213  *live_check = true;
214  }
215  }
216 
217  /* same goes for the new postmaster */
219  {
220  if (start_postmaster(&new_cluster, false))
221  stop_postmaster(false);
222  else
223  pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
224  "Please shutdown that postmaster and try again.\n");
225  }
226 
227  /* get path to pg_upgrade executable */
228  if (find_my_exec(argv0, exec_path) < 0)
229  pg_fatal("%s: could not find own program executable\n", argv0);
230 
231  /* Trim off program name and keep just path */
232  *last_dir_separator(exec_path) = '\0';
233  canonicalize_path(exec_path);
234  os_info.exec_path = pg_strdup(exec_path);
235 }
static char * argv0
Definition: pg_ctl.c:89
bool pid_lock_file_exists(const char *datadir)
Definition: exec.c:216
void canonicalize_path(char *path)
Definition: path.c:254
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
void stop_postmaster(bool fast)
Definition: server.c:308
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
void check_pghost_envvar(void)
Definition: server.c:335
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:119
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
UserOpts user_opts
Definition: option.c:29
char * last_dir_separator(const char *filename)
Definition: path.c:138
void verify_directories(void)
Definition: exec.c:247
static char * exec_path
Definition: pg_ctl.c:84
bool check
Definition: pg_upgrade.h:297
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:428
char * pgdata
Definition: pg_upgrade.h:266
OSInfo os_info
Definition: pg_upgrade.c:57
char * exec_path
Definition: pg_upgrade.h:310

Variable Documentation

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

Definition at line 59 of file pg_upgrade.c.

Referenced by cleanup(), and parseCommandLine().