6 * Copyright (c) 2010-2013, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/pg_upgrade.c
11 * To simplify the upgrade process, we force certain system values to be
12 * identical between old and new clusters:
14 * We control all assignments of pg_class.oid (and relfilenode) so toast
15 * oids are the same between old and new clusters. This is important
16 * because toast oids are stored as toast pointers in user tables.
18 * FYI, while pg_class.oid and pg_class.relfilenode are initially the same
19 * in a cluster, but they can diverge due to CLUSTER, REINDEX, or VACUUM
20 * FULL. The new cluster will have matching pg_class.oid and
21 * pg_class.relfilenode values and be based on the old oid value. This can
22 * cause the old and new pg_class.relfilenode values to differ. In summary,
23 * old and new pg_class.oid and new pg_class.relfilenode will have the
24 * same value, and old pg_class.relfilenode might differ.
26 * We control all assignments of pg_type.oid because these oids are stored
27 * in user composite type values.
29 * We control all assignments of pg_enum.oid because these oids are stored
30 * in user tables as enum values.
32 * We control all assignments of pg_authid.oid because these oids are stored
33 * in pg_largeobject_metadata.
38 #include "postgres_fe.h"
40 #include "pg_upgrade.h"
42 #ifdef HAVE_LANGINFO_H
46 static void prepare_new_cluster(void);
47 static void prepare_new_databases(void);
48 static void create_new_objects(void);
49 static void copy_clog_xlog_xid(void);
50 static void set_frozenxids(void);
51 static void setup(char *argv0, bool *live_check);
52 static void cleanup(void);
54 ClusterInfo old_cluster,
58 char *output_files[] = {
61 /* unique file for pg_ctl start */
62 SERVER_START_LOG_FILE,
71 main(int argc, char **argv)
73 char *sequence_script_file_name = NULL;
74 char *analyze_script_file_name = NULL;
75 char *deletion_script_file_name = NULL;
76 bool live_check = false;
78 parseCommandLine(argc, argv);
80 adjust_data_dir(&old_cluster);
81 adjust_data_dir(&new_cluster);
83 setup(argv[0], &live_check);
85 output_check_banner(live_check);
87 check_cluster_versions();
89 get_sock_dir(&old_cluster, live_check);
90 get_sock_dir(&new_cluster, false);
92 check_cluster_compatibility(live_check);
94 check_and_dump_old_cluster(live_check, &sequence_script_file_name);
98 start_postmaster(&new_cluster, true);
101 report_clusters_compatible();
103 pg_log(PG_REPORT, "\nPerforming Upgrade\n");
104 pg_log(PG_REPORT, "------------------\n");
106 prepare_new_cluster();
108 stop_postmaster(false);
111 * Destructive Changes to New Cluster
114 copy_clog_xlog_xid();
116 /* New now using xids of the old system */
119 start_postmaster(&new_cluster, true);
121 prepare_new_databases();
123 create_new_objects();
125 stop_postmaster(false);
128 * Most failures happen in create_new_objects(), which has completed at
129 * this point. We do this here because it is just before linking, which
130 * will link the old and new cluster data files, preventing the old
131 * cluster from being safely started once the new cluster is started.
133 if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
134 disable_old_cluster();
136 transfer_all_new_tablespaces(&old_cluster.dbarr, &new_cluster.dbarr,
137 old_cluster.pgdata, new_cluster.pgdata);
140 * Assuming OIDs are only used in system tables, there is no need to
141 * restore the OID counter because we have not transferred any OIDs from
142 * the old system, but we do it anyway just in case. We do it late here
143 * because there is no need to have the schema load use new oids.
145 prep_status("Setting next OID for new cluster");
146 exec_prog(UTILITY_LOG_FILE, NULL, true,
147 "\"%s/pg_resetxlog\" -o %u \"%s\"",
148 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid,
152 prep_status("Sync data directory to disk");
153 exec_prog(UTILITY_LOG_FILE, NULL, true,
154 "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
158 create_script_for_cluster_analyze(&analyze_script_file_name);
159 create_script_for_old_cluster_deletion(&deletion_script_file_name);
161 issue_warnings(sequence_script_file_name);
163 pg_log(PG_REPORT, "\nUpgrade Complete\n");
164 pg_log(PG_REPORT, "----------------\n");
166 output_completion_banner(analyze_script_file_name,
167 deletion_script_file_name);
169 pg_free(analyze_script_file_name);
170 pg_free(deletion_script_file_name);
171 pg_free(sequence_script_file_name);
180 setup(char *argv0, bool *live_check)
182 char exec_path[MAXPGPATH]; /* full path to my executable */
185 * make sure the user has a clean environment, otherwise, we may confuse
186 * libpq when we connect to one (or both) of the servers.
188 check_pghost_envvar();
190 verify_directories();
192 /* no postmasters should be running, except for a live check */
193 if (pid_lock_file_exists(old_cluster.pgdata))
196 * If we have a postmaster.pid file, try to start the server. If
197 * it starts, the pid file was stale, so stop the server. If it
198 * doesn't start, assume the server is running. If the pid file
199 * is left over from a server crash, this also allows any committed
200 * transactions stored in the WAL to be replayed so they are not
201 * lost, because WAL files are not transfered from old to new
204 if (start_postmaster(&old_cluster, false))
205 stop_postmaster(false);
208 if (!user_opts.check)
209 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
210 "Please shutdown that postmaster and try again.\n");
216 /* same goes for the new postmaster */
217 if (pid_lock_file_exists(new_cluster.pgdata))
219 if (start_postmaster(&new_cluster, false))
220 stop_postmaster(false);
222 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
223 "Please shutdown that postmaster and try again.\n");
226 /* get path to pg_upgrade executable */
227 if (find_my_exec(argv0, exec_path) < 0)
228 pg_log(PG_FATAL, "Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
230 /* Trim off program name and keep just path */
231 *last_dir_separator(exec_path) = '\0';
232 canonicalize_path(exec_path);
233 os_info.exec_path = pg_strdup(exec_path);
238 prepare_new_cluster(void)
241 * It would make more sense to freeze after loading the schema, but that
242 * would cause us to lose the frozenids restored by the load. We use
243 * --analyze so autovacuum doesn't update statistics later
245 prep_status("Analyzing all rows in the new cluster");
246 exec_prog(UTILITY_LOG_FILE, NULL, true,
247 "\"%s/vacuumdb\" %s --all --analyze %s",
248 new_cluster.bindir, cluster_conn_opts(&new_cluster),
249 log_opts.verbose ? "--verbose" : "");
253 * We do freeze after analyze so pg_statistic is also frozen. template0 is
254 * not frozen here, but data rows were frozen by initdb, and we set its
255 * datfrozenxid and relfrozenxids later to match the new xid counter
258 prep_status("Freezing all rows on the new cluster");
259 exec_prog(UTILITY_LOG_FILE, NULL, true,
260 "\"%s/vacuumdb\" %s --all --freeze %s",
261 new_cluster.bindir, cluster_conn_opts(&new_cluster),
262 log_opts.verbose ? "--verbose" : "");
265 get_pg_database_relfilenode(&new_cluster);
270 prepare_new_databases(void)
273 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
274 * does not launch here and delete clog files, before the frozen xids are
280 prep_status("Restoring global objects in the new cluster");
283 * Install support functions in the global-object restore database to
284 * preserve pg_authid.oid. pg_dumpall uses 'template0' as its template
285 * database so objects we add into 'template1' are not propogated. They
286 * are removed on pg_upgrade exit.
288 install_support_functions_in_new_db("template1");
291 * We have to create the databases first so we can install support
292 * functions in all the other databases. Ideally we could create the
293 * support functions in template1 but pg_dumpall creates database using
294 * the template0 template.
296 exec_prog(UTILITY_LOG_FILE, NULL, true,
297 "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
298 new_cluster.bindir, cluster_conn_opts(&new_cluster),
302 /* we load this to get a current list of databases */
303 get_db_and_rel_infos(&new_cluster);
308 create_new_objects(void)
312 prep_status("Adding support functions to new cluster");
315 * Technically, we only need to install these support functions in new
316 * databases that also exist in the old cluster, but for completeness
317 * we process all new databases.
319 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
321 DbInfo *new_db = &new_cluster.dbarr.dbs[dbnum];
323 /* skip db we already installed */
324 if (strcmp(new_db->db_name, "template1") != 0)
325 install_support_functions_in_new_db(new_db->db_name);
329 prep_status("Restoring database schemas in the new cluster\n");
331 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
333 char sql_file_name[MAXPGPATH], log_file_name[MAXPGPATH];
334 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
336 pg_log(PG_STATUS, "%s", old_db->db_name);
337 snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
338 snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
341 * pg_dump only produces its output at the end, so there is little
342 * parallelism if using the pipe.
344 parallel_exec_prog(log_file_name, NULL,
345 "\"%s/pg_restore\" %s --exit-on-error --verbose --dbname \"%s\" \"%s\"",
346 new_cluster.bindir, cluster_conn_opts(&new_cluster),
347 old_db->db_name, sql_file_name);
350 /* reap all children */
351 while (reap_child(true) == true)
354 end_progress_output();
357 /* regenerate now that we have objects in the databases */
358 get_db_and_rel_infos(&new_cluster);
360 uninstall_support_functions_from_new_cluster();
364 * Delete the given subdirectory contents from the new cluster, and copy the
365 * files from the old cluster into it.
368 copy_subdir_files(char *subdir)
370 char old_path[MAXPGPATH];
371 char new_path[MAXPGPATH];
373 prep_status("Deleting files from new %s", subdir);
375 snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
376 snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
377 if (!rmtree(new_path, true))
378 pg_log(PG_FATAL, "could not delete directory \"%s\"\n", new_path);
381 prep_status("Copying old %s to new server", subdir);
383 exec_prog(UTILITY_LOG_FILE, NULL, true,
385 "cp -Rf \"%s\" \"%s\"",
387 /* flags: everything, no confirm, quiet, overwrite read-only */
388 "xcopy /e /y /q /r \"%s\" \"%s\\\"",
396 copy_clog_xlog_xid(void)
398 /* copy old commit logs to new data dir */
399 copy_subdir_files("pg_clog");
401 /* set the next transaction id of the new cluster */
402 prep_status("Setting next transaction ID for new cluster");
403 exec_prog(UTILITY_LOG_FILE, NULL, true,
404 "\"%s/pg_resetxlog\" -f -x %u \"%s\"",
405 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid,
410 * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
411 * (see pg_upgrade.h) and the new server is after, then we don't copy
412 * pg_multixact files, but we need to reset pg_control so that the new
413 * server doesn't attempt to read multis older than the cutoff value.
415 if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
416 new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
418 copy_subdir_files("pg_multixact/offsets");
419 copy_subdir_files("pg_multixact/members");
420 prep_status("Setting next multixact ID and offset for new cluster");
422 * we preserve all files and contents, so we must preserve both "next"
423 * counters here and the oldest multi present on system.
425 exec_prog(UTILITY_LOG_FILE, NULL, true,
426 "\"%s/pg_resetxlog\" -O %u -m %u,%u \"%s\"",
428 old_cluster.controldata.chkpnt_nxtmxoff,
429 old_cluster.controldata.chkpnt_nxtmulti,
430 old_cluster.controldata.chkpnt_oldstMulti,
434 else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
436 prep_status("Setting oldest multixact ID on new cluster");
438 * We don't preserve files in this case, but it's important that the
439 * oldest multi is set to the latest value used by the old system, so
440 * that multixact.c returns the empty set for multis that might be
441 * present on disk. We set next multi to the value following that; it
442 * might end up wrapped around (i.e. 0) if the old cluster had
443 * next=MaxMultiXactId, but multixact.c can cope with that just fine.
445 exec_prog(UTILITY_LOG_FILE, NULL, true,
446 "\"%s/pg_resetxlog\" -m %u,%u \"%s\"",
448 old_cluster.controldata.chkpnt_nxtmulti + 1,
449 old_cluster.controldata.chkpnt_nxtmulti,
454 /* now reset the wal archives in the new cluster */
455 prep_status("Resetting WAL archives");
456 exec_prog(UTILITY_LOG_FILE, NULL, true,
457 "\"%s/pg_resetxlog\" -l %s \"%s\"", new_cluster.bindir,
458 old_cluster.controldata.nextxlogfile,
467 * We have frozen all xids, so set relfrozenxid and datfrozenxid
468 * to be the old cluster's xid counter, which we just set in the new
469 * cluster. User-table frozenxid values will be set by pg_dumpall
470 * --binary-upgrade, but objects not set by the pg_dump must have
471 * proper frozen counters.
485 prep_status("Setting frozenxid counters in new cluster");
487 conn_template1 = connectToServer(&new_cluster, "template1");
489 /* set pg_database.datfrozenxid */
490 PQclear(executeQueryOrDie(conn_template1,
491 "UPDATE pg_catalog.pg_database "
492 "SET datfrozenxid = '%u'",
493 old_cluster.controldata.chkpnt_nxtxid));
495 /* get database names */
496 dbres = executeQueryOrDie(conn_template1,
497 "SELECT datname, datallowconn "
498 "FROM pg_catalog.pg_database");
500 i_datname = PQfnumber(dbres, "datname");
501 i_datallowconn = PQfnumber(dbres, "datallowconn");
503 ntups = PQntuples(dbres);
504 for (dbnum = 0; dbnum < ntups; dbnum++)
506 char *datname = PQgetvalue(dbres, dbnum, i_datname);
507 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
510 * We must update databases where datallowconn = false, e.g.
511 * template0, because autovacuum increments their datfrozenxids and
512 * relfrozenxids even if autovacuum is turned off, and even though all
513 * the data rows are already frozen To enable this, we temporarily
514 * change datallowconn.
516 if (strcmp(datallowconn, "f") == 0)
517 PQclear(executeQueryOrDie(conn_template1,
518 "UPDATE pg_catalog.pg_database "
519 "SET datallowconn = true "
520 "WHERE datname = '%s'", datname));
522 conn = connectToServer(&new_cluster, datname);
524 /* set pg_class.relfrozenxid */
525 PQclear(executeQueryOrDie(conn,
526 "UPDATE pg_catalog.pg_class "
527 "SET relfrozenxid = '%u' "
528 /* only heap, materialized view, and TOAST are vacuumed */
529 "WHERE relkind IN ('r', 'm', 't')",
530 old_cluster.controldata.chkpnt_nxtxid));
533 /* Reset datallowconn flag */
534 if (strcmp(datallowconn, "f") == 0)
535 PQclear(executeQueryOrDie(conn_template1,
536 "UPDATE pg_catalog.pg_database "
537 "SET datallowconn = false "
538 "WHERE datname = '%s'", datname));
543 PQfinish(conn_template1);
553 fclose(log_opts.internal);
555 /* Remove dump and log files? */
556 if (!log_opts.retain)
561 for (filename = output_files; *filename != NULL; filename++)
564 /* remove dump files */
565 unlink(GLOBALS_DUMP_FILE);
567 if (old_cluster.dbarr.dbs)
568 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
570 char sql_file_name[MAXPGPATH], log_file_name[MAXPGPATH];
571 DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
573 snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
574 unlink(sql_file_name);
576 snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
577 unlink(log_file_name);