6 * Copyright (c) 2010-2011, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/pg_upgrade.c
11 * To simplify the upgrade process, we force certain system items to be
12 * consistent between old and new clusters:
14 * We control all assignments of pg_class.relfilenode so we can keep the
15 * same relfilenodes for old and new files. The only exception is
16 * pg_largeobject, pg_largeobject_metadata, and its indexes, which can
17 * change due to a cluster, reindex, or vacuum full. (We don't create
18 * those so have no control over their oid/relfilenode values.)
20 * While pg_class.oid and pg_class.relfilenode are intially the same, they
21 * can diverge due to cluster, reindex, or vacuum full. The new cluster
22 * will again have matching pg_class.relfilenode and pg_class.oid values,
23 * but based on the new relfilenode value, so the old/new oids might
26 * We control all assignments of pg_type.oid because these are stored
32 #include "pg_upgrade.h"
34 #ifdef HAVE_LANGINFO_H
38 static void disable_old_cluster(void);
39 static void prepare_new_cluster(void);
40 static void prepare_new_databases(void);
41 static void create_new_objects(void);
42 static void copy_clog_xlog_xid(void);
43 static void set_frozenxids(void);
44 static void setup(char *argv0, bool live_check);
45 static void cleanup(void);
47 ClusterInfo old_cluster, new_cluster;
51 main(int argc, char **argv)
53 char *sequence_script_file_name = NULL;
54 char *deletion_script_file_name = NULL;
55 bool live_check = false;
57 parseCommandLine(argc, argv);
59 output_check_banner(&live_check);
61 setup(argv[0], live_check);
63 check_cluster_versions();
64 check_cluster_compatibility(live_check);
66 check_old_cluster(live_check, &sequence_script_file_name);
70 start_postmaster(&new_cluster, false);
73 report_clusters_compatible();
75 pg_log(PG_REPORT, "\nPerforming Upgrade\n");
76 pg_log(PG_REPORT, "------------------\n");
78 disable_old_cluster();
79 prepare_new_cluster();
81 stop_postmaster(false, false);
84 * Destructive Changes to New Cluster
89 /* New now using xids of the old system */
91 prepare_new_databases();
95 transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
96 old_cluster.pgdata, new_cluster.pgdata);
99 * Assuming OIDs are only used in system tables, there is no need to
100 * restore the OID counter because we have not transferred any OIDs from
101 * the old system, but we do it anyway just in case. We do it late here
102 * because there is no need to have the schema load use new oids.
104 prep_status("Setting next oid for new cluster");
105 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
107 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
110 create_script_for_old_cluster_deletion(&deletion_script_file_name);
112 issue_warnings(sequence_script_file_name);
114 pg_log(PG_REPORT, "\nUpgrade complete\n");
115 pg_log(PG_REPORT, "----------------\n");
117 output_completion_banner(deletion_script_file_name);
119 pg_free(deletion_script_file_name);
120 pg_free(sequence_script_file_name);
129 setup(char *argv0, bool live_check)
131 char exec_path[MAXPGPATH]; /* full path to my executable */
134 * make sure the user has a clean environment, otherwise, we may confuse
135 * libpq when we connect to one (or both) of the servers.
137 check_for_libpq_envvars();
139 verify_directories();
141 /* no postmasters should be running */
142 if (!live_check && is_server_running(old_cluster.pgdata))
144 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
145 "Please shutdown that postmaster and try again.\n");
148 /* same goes for the new postmaster */
149 if (is_server_running(new_cluster.pgdata))
151 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
152 "Please shutdown that postmaster and try again.\n");
155 /* get path to pg_upgrade executable */
156 if (find_my_exec(argv0, exec_path) < 0)
157 pg_log(PG_FATAL, "Could not get pathname to pg_upgrade: %s\n", getErrorText(errno));
159 /* Trim off program name and keep just path */
160 *last_dir_separator(exec_path) = '\0';
161 canonicalize_path(exec_path);
162 os_info.exec_path = pg_strdup(exec_path);
167 disable_old_cluster(void)
169 /* rename pg_control so old server cannot be accidentally started */
170 rename_old_pg_control();
175 prepare_new_cluster(void)
178 * It would make more sense to freeze after loading the schema, but that
179 * would cause us to lose the frozenids restored by the load. We use
180 * --analyze so autovacuum doesn't update statistics later
182 prep_status("Analyzing all rows in the new cluster");
184 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
185 "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
186 new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
190 * We do freeze after analyze so pg_statistic is also frozen. template0 is
191 * not frozen here, but data rows were frozen by initdb, and we set its
192 * datfrozenxid and relfrozenxids later to match the new xid counter
195 prep_status("Freezing all rows on the new cluster");
197 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
198 "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
199 new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
202 get_pg_database_relfilenode(&new_cluster);
207 prepare_new_databases(void)
210 start_postmaster(&new_cluster, false);
213 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
214 * does not launch here and delete clog files, before the frozen xids are
221 * We have to create the databases first so we can create the toast table
222 * placeholder relfiles.
224 prep_status("Creating databases in the new cluster");
226 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
227 /* --no-psqlrc prevents AUTOCOMMIT=off */
228 "--no-psqlrc --port %d --username \"%s\" "
229 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
230 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
231 GLOBALS_DUMP_FILE, log_opts.filename);
234 get_db_and_rel_infos(&new_cluster);
236 stop_postmaster(false, false);
241 create_new_objects(void)
244 start_postmaster(&new_cluster, false);
246 install_support_functions();
248 prep_status("Restoring database schema to new cluster");
250 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
251 "--no-psqlrc --port %d --username \"%s\" "
252 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
253 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
254 DB_DUMP_FILE, log_opts.filename);
257 /* regenerate now that we have db schemas */
258 dbarr_free(&new_cluster.dbarr);
259 get_db_and_rel_infos(&new_cluster);
261 uninstall_support_functions();
263 stop_postmaster(false, false);
268 copy_clog_xlog_xid(void)
270 char old_clog_path[MAXPGPATH];
271 char new_clog_path[MAXPGPATH];
273 /* copy old commit logs to new data dir */
274 prep_status("Deleting new commit clogs");
276 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
277 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
278 if (!rmtree(new_clog_path, true))
279 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
282 prep_status("Copying old commit clogs to new server");
284 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
287 /* flags: everything, no confirm, quiet, overwrite read-only */
288 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
291 old_clog_path, new_clog_path);
294 /* set the next transaction id of the new cluster */
295 prep_status("Setting next transaction id for new cluster");
296 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
297 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
300 /* now reset the wal archives in the new cluster */
301 prep_status("Resetting WAL archives");
302 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
303 new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
304 old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
305 new_cluster.pgdata, log_opts.filename);
313 * We have frozen all xids, so set relfrozenxid and datfrozenxid
314 * to be the old cluster's xid counter, which we just set in the new
315 * cluster. User-table frozenxid values will be set by pg_dumpall
316 * --binary-upgrade, but objects not set by the pg_dump must have
317 * proper frozen counters.
331 prep_status("Setting frozenxid counters in new cluster");
333 conn_template1 = connectToServer(&new_cluster, "template1");
335 /* set pg_database.datfrozenxid */
336 PQclear(executeQueryOrDie(conn_template1,
337 "UPDATE pg_catalog.pg_database "
338 "SET datfrozenxid = '%u'",
339 old_cluster.controldata.chkpnt_nxtxid));
341 /* get database names */
342 dbres = executeQueryOrDie(conn_template1,
343 "SELECT datname, datallowconn "
344 "FROM pg_catalog.pg_database");
346 i_datname = PQfnumber(dbres, "datname");
347 i_datallowconn = PQfnumber(dbres, "datallowconn");
349 ntups = PQntuples(dbres);
350 for (dbnum = 0; dbnum < ntups; dbnum++)
352 char *datname = PQgetvalue(dbres, dbnum, i_datname);
353 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
356 * We must update databases where datallowconn = false, e.g.
357 * template0, because autovacuum increments their datfrozenxids and
358 * relfrozenxids even if autovacuum is turned off, and even though all
359 * the data rows are already frozen To enable this, we temporarily
360 * change datallowconn.
362 if (strcmp(datallowconn, "f") == 0)
363 PQclear(executeQueryOrDie(conn_template1,
364 "UPDATE pg_catalog.pg_database "
365 "SET datallowconn = true "
366 "WHERE datname = '%s'", datname));
368 conn = connectToServer(&new_cluster, datname);
370 /* set pg_class.relfrozenxid */
371 PQclear(executeQueryOrDie(conn,
372 "UPDATE pg_catalog.pg_class "
373 "SET relfrozenxid = '%u' "
374 /* only heap and TOAST are vacuumed */
375 "WHERE relkind IN ('r', 't')",
376 old_cluster.controldata.chkpnt_nxtxid));
379 /* Reset datallowconn flag */
380 if (strcmp(datallowconn, "f") == 0)
381 PQclear(executeQueryOrDie(conn_template1,
382 "UPDATE pg_catalog.pg_database "
383 "SET datallowconn = false "
384 "WHERE datname = '%s'", datname));
389 PQfinish(conn_template1);
399 char filename[MAXPGPATH];
401 for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
402 pg_free(os_info.tablespaces[tblnum]);
403 pg_free(os_info.tablespaces);
405 dbarr_free(&old_cluster.dbarr);
406 dbarr_free(&new_cluster.dbarr);
407 pg_free(log_opts.filename);
408 pg_free(os_info.user);
409 pg_free(old_cluster.controldata.lc_collate);
410 pg_free(new_cluster.controldata.lc_collate);
411 pg_free(old_cluster.controldata.lc_ctype);
412 pg_free(new_cluster.controldata.lc_ctype);
413 pg_free(old_cluster.controldata.encoding);
414 pg_free(new_cluster.controldata.encoding);
415 pg_free(old_cluster.tablespace_suffix);
416 pg_free(new_cluster.tablespace_suffix);
418 if (log_opts.fd != NULL)
424 if (log_opts.debug_fd)
425 fclose(log_opts.debug_fd);
427 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
429 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
431 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);