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 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 intially 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_auth.oid because these oids are stored
33 * in pg_largeobject_metadata.
38 #include "pg_upgrade.h"
40 #ifdef HAVE_LANGINFO_H
44 static void disable_old_cluster(void);
45 static void prepare_new_cluster(void);
46 static void prepare_new_databases(void);
47 static void create_new_objects(void);
48 static void copy_clog_xlog_xid(void);
49 static void set_frozenxids(void);
50 static void setup(char *argv0, bool live_check);
51 static void cleanup(void);
53 /* This is the database used by pg_dumpall to restore global tables */
54 #define GLOBAL_DUMP_DB "postgres"
56 ClusterInfo old_cluster, new_cluster;
60 main(int argc, char **argv)
62 char *sequence_script_file_name = NULL;
63 char *deletion_script_file_name = NULL;
64 bool live_check = false;
66 parseCommandLine(argc, argv);
68 output_check_banner(&live_check);
70 setup(argv[0], live_check);
72 check_cluster_versions();
73 check_cluster_compatibility(live_check);
75 check_old_cluster(live_check, &sequence_script_file_name);
79 start_postmaster(&new_cluster, false);
82 report_clusters_compatible();
84 pg_log(PG_REPORT, "\nPerforming Upgrade\n");
85 pg_log(PG_REPORT, "------------------\n");
87 disable_old_cluster();
88 prepare_new_cluster();
90 stop_postmaster(false, false);
93 * Destructive Changes to New Cluster
98 /* New now using xids of the old system */
100 prepare_new_databases();
102 create_new_objects();
104 transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
105 old_cluster.pgdata, new_cluster.pgdata);
108 * Assuming OIDs are only used in system tables, there is no need to
109 * restore the OID counter because we have not transferred any OIDs from
110 * the old system, but we do it anyway just in case. We do it late here
111 * because there is no need to have the schema load use new oids.
113 prep_status("Setting next oid for new cluster");
114 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
116 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
119 create_script_for_old_cluster_deletion(&deletion_script_file_name);
121 issue_warnings(sequence_script_file_name);
123 pg_log(PG_REPORT, "\nUpgrade complete\n");
124 pg_log(PG_REPORT, "----------------\n");
126 output_completion_banner(deletion_script_file_name);
128 pg_free(deletion_script_file_name);
129 pg_free(sequence_script_file_name);
138 setup(char *argv0, bool live_check)
140 char exec_path[MAXPGPATH]; /* full path to my executable */
143 * make sure the user has a clean environment, otherwise, we may confuse
144 * libpq when we connect to one (or both) of the servers.
146 check_for_libpq_envvars();
148 verify_directories();
150 /* no postmasters should be running */
151 if (!live_check && is_server_running(old_cluster.pgdata))
153 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
154 "Please shutdown that postmaster and try again.\n");
157 /* same goes for the new postmaster */
158 if (is_server_running(new_cluster.pgdata))
160 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
161 "Please shutdown that postmaster and try again.\n");
164 /* get path to pg_upgrade executable */
165 if (find_my_exec(argv0, exec_path) < 0)
166 pg_log(PG_FATAL, "Could not get pathname to pg_upgrade: %s\n", getErrorText(errno));
168 /* Trim off program name and keep just path */
169 *last_dir_separator(exec_path) = '\0';
170 canonicalize_path(exec_path);
171 os_info.exec_path = pg_strdup(exec_path);
176 disable_old_cluster(void)
178 /* rename pg_control so old server cannot be accidentally started */
179 rename_old_pg_control();
184 prepare_new_cluster(void)
187 * It would make more sense to freeze after loading the schema, but that
188 * would cause us to lose the frozenids restored by the load. We use
189 * --analyze so autovacuum doesn't update statistics later
191 prep_status("Analyzing all rows in the new cluster");
193 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
194 "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
195 new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
199 * We do freeze after analyze so pg_statistic is also frozen. template0 is
200 * not frozen here, but data rows were frozen by initdb, and we set its
201 * datfrozenxid and relfrozenxids later to match the new xid counter
204 prep_status("Freezing all rows on the new cluster");
206 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
207 "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
208 new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
211 get_pg_database_relfilenode(&new_cluster);
216 prepare_new_databases(void)
219 start_postmaster(&new_cluster, false);
222 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
223 * does not launch here and delete clog files, before the frozen xids are
229 prep_status("Creating databases in the new cluster");
232 * Install support functions in the global-restore database
233 * to preserve pg_authid.oid.
235 install_support_functions_in_new_db(GLOBAL_DUMP_DB);
238 * We have to create the databases first so we can install support
239 * functions in all the other databases. Ideally we could create
240 * the support functions in template1 but pg_dumpall creates database
241 * using the template0 template.
244 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
245 /* --no-psqlrc prevents AUTOCOMMIT=off */
246 "--no-psqlrc --port %d --username \"%s\" "
247 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
248 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
249 GLOBALS_DUMP_FILE, log_opts.filename);
252 /* we load this to get a current list of databases */
253 get_db_and_rel_infos(&new_cluster);
255 stop_postmaster(false, false);
260 create_new_objects(void)
265 start_postmaster(&new_cluster, false);
267 prep_status("Adding support functions to new cluster");
269 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
271 DbInfo *new_db = &new_cluster.dbarr.dbs[dbnum];
273 /* skip db we already installed */
274 if (strcmp(new_db->db_name, GLOBAL_DUMP_DB) != 0)
275 install_support_functions_in_new_db(new_db->db_name);
279 prep_status("Restoring database schema to new cluster");
281 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
282 "--no-psqlrc --port %d --username \"%s\" "
283 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
284 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
285 DB_DUMP_FILE, log_opts.filename);
288 /* regenerate now that we have objects in the databases */
289 get_db_and_rel_infos(&new_cluster);
291 uninstall_support_functions_from_new_cluster();
293 stop_postmaster(false, false);
298 copy_clog_xlog_xid(void)
300 char old_clog_path[MAXPGPATH];
301 char new_clog_path[MAXPGPATH];
303 /* copy old commit logs to new data dir */
304 prep_status("Deleting new commit clogs");
306 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
307 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
308 if (!rmtree(new_clog_path, true))
309 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
312 prep_status("Copying old commit clogs to new server");
314 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
317 /* flags: everything, no confirm, quiet, overwrite read-only */
318 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
321 old_clog_path, new_clog_path);
324 /* set the next transaction id of the new cluster */
325 prep_status("Setting next transaction id for new cluster");
326 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
327 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
330 /* now reset the wal archives in the new cluster */
331 prep_status("Resetting WAL archives");
332 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
333 new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
334 old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
335 new_cluster.pgdata, log_opts.filename);
343 * We have frozen all xids, so set relfrozenxid and datfrozenxid
344 * to be the old cluster's xid counter, which we just set in the new
345 * cluster. User-table frozenxid values will be set by pg_dumpall
346 * --binary-upgrade, but objects not set by the pg_dump must have
347 * proper frozen counters.
361 prep_status("Setting frozenxid counters in new cluster");
363 conn_template1 = connectToServer(&new_cluster, "template1");
365 /* set pg_database.datfrozenxid */
366 PQclear(executeQueryOrDie(conn_template1,
367 "UPDATE pg_catalog.pg_database "
368 "SET datfrozenxid = '%u'",
369 old_cluster.controldata.chkpnt_nxtxid));
371 /* get database names */
372 dbres = executeQueryOrDie(conn_template1,
373 "SELECT datname, datallowconn "
374 "FROM pg_catalog.pg_database");
376 i_datname = PQfnumber(dbres, "datname");
377 i_datallowconn = PQfnumber(dbres, "datallowconn");
379 ntups = PQntuples(dbres);
380 for (dbnum = 0; dbnum < ntups; dbnum++)
382 char *datname = PQgetvalue(dbres, dbnum, i_datname);
383 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
386 * We must update databases where datallowconn = false, e.g.
387 * template0, because autovacuum increments their datfrozenxids and
388 * relfrozenxids even if autovacuum is turned off, and even though all
389 * the data rows are already frozen To enable this, we temporarily
390 * change datallowconn.
392 if (strcmp(datallowconn, "f") == 0)
393 PQclear(executeQueryOrDie(conn_template1,
394 "UPDATE pg_catalog.pg_database "
395 "SET datallowconn = true "
396 "WHERE datname = '%s'", datname));
398 conn = connectToServer(&new_cluster, datname);
400 /* set pg_class.relfrozenxid */
401 PQclear(executeQueryOrDie(conn,
402 "UPDATE pg_catalog.pg_class "
403 "SET relfrozenxid = '%u' "
404 /* only heap and TOAST are vacuumed */
405 "WHERE relkind IN ('r', 't')",
406 old_cluster.controldata.chkpnt_nxtxid));
409 /* Reset datallowconn flag */
410 if (strcmp(datallowconn, "f") == 0)
411 PQclear(executeQueryOrDie(conn_template1,
412 "UPDATE pg_catalog.pg_database "
413 "SET datallowconn = false "
414 "WHERE datname = '%s'", datname));
419 PQfinish(conn_template1);
428 char filename[MAXPGPATH];
433 if (log_opts.debug_fd)
434 fclose(log_opts.debug_fd);
436 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
438 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
440 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);