6 * Copyright (c) 2010, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/pg_upgrade.c
10 #include "pg_upgrade.h"
12 #ifdef HAVE_LANGINFO_H
16 static void disable_old_cluster(void);
17 static void prepare_new_cluster(void);
18 static void prepare_new_databases(void);
19 static void create_new_objects(void);
20 static void copy_clog_xlog_xid(void);
21 static void set_frozenxids(void);
22 static void setup(char *argv0, bool live_check);
23 static void cleanup(void);
25 ClusterInfo old_cluster,
30 main(int argc, char **argv)
32 char *sequence_script_file_name = NULL;
33 char *deletion_script_file_name = NULL;
34 bool live_check = false;
36 parseCommandLine(argc, argv);
38 output_check_banner(&live_check);
40 setup(argv[0], live_check);
42 check_cluster_versions();
43 check_cluster_compatibility(live_check);
45 check_old_cluster(live_check, &sequence_script_file_name);
49 start_postmaster(CLUSTER_NEW, false);
52 report_clusters_compatible();
54 pg_log(PG_REPORT, "\nPerforming Migration\n");
55 pg_log(PG_REPORT, "--------------------\n");
57 disable_old_cluster();
58 prepare_new_cluster();
60 stop_postmaster(false, false);
63 * Destructive Changes to New Cluster
68 /* New now using xids of the old system */
70 prepare_new_databases();
74 transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
75 old_cluster.pgdata, new_cluster.pgdata);
78 * Assuming OIDs are only used in system tables, there is no need to
79 * restore the OID counter because we have not transferred any OIDs from
80 * the old system, but we do it anyway just in case. We do it late here
81 * because there is no need to have the schema load use new oids.
83 prep_status("Setting next oid for new cluster");
84 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
86 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
89 create_script_for_old_cluster_deletion(&deletion_script_file_name);
91 issue_warnings(sequence_script_file_name);
93 pg_log(PG_REPORT, "\nUpgrade complete\n");
94 pg_log(PG_REPORT, "----------------\n");
96 output_completion_banner(deletion_script_file_name);
98 pg_free(deletion_script_file_name);
99 pg_free(sequence_script_file_name);
108 setup(char *argv0, bool live_check)
110 char exec_path[MAXPGPATH]; /* full path to my executable */
113 * make sure the user has a clean environment, otherwise, we may confuse
114 * libpq when we connect to one (or both) of the servers.
116 check_for_libpq_envvars();
118 verify_directories();
120 /* no postmasters should be running */
121 if (!live_check && is_server_running(old_cluster.pgdata))
123 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
124 "Please shutdown that postmaster and try again.\n");
127 /* same goes for the new postmaster */
128 if (is_server_running(new_cluster.pgdata))
130 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
131 "Please shutdown that postmaster and try again.\n");
134 /* get path to pg_upgrade executable */
135 if (find_my_exec(argv0, exec_path) < 0)
136 pg_log(PG_FATAL, "Could not get pathname to pg_upgrade: %s\n", getErrorText(errno));
138 /* Trim off program name and keep just path */
139 *last_dir_separator(exec_path) = '\0';
140 canonicalize_path(exec_path);
141 os_info.exec_path = pg_strdup(exec_path);
146 disable_old_cluster(void)
148 /* rename pg_control so old server cannot be accidentally started */
149 rename_old_pg_control();
154 prepare_new_cluster(void)
157 * It would make more sense to freeze after loading the schema, but that
158 * would cause us to lose the frozenids restored by the load. We use
159 * --analyze so autovacuum doesn't update statistics later
161 prep_status("Analyzing all rows in the new cluster");
163 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
164 "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
165 new_cluster.bindir, new_cluster.port, os_info.user, log.filename);
169 * We do freeze after analyze so pg_statistic is also frozen. template0 is
170 * not frozen here, but data rows were frozen by initdb, and we set its
171 * datfrozenxid and relfrozenxids later to match the new xid counter
174 prep_status("Freezing all rows on the new cluster");
176 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
177 "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
178 new_cluster.bindir, new_cluster.port, os_info.user, log.filename);
181 get_pg_database_relfilenode(CLUSTER_NEW);
186 prepare_new_databases(void)
189 start_postmaster(CLUSTER_NEW, false);
192 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
193 * does not launch here and delete clog files, before the frozen xids are
200 * We have to create the databases first so we can create the toast table
201 * placeholder relfiles.
203 prep_status("Creating databases in the new cluster");
205 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
206 /* --no-psqlrc prevents AUTOCOMMIT=off */
207 "--no-psqlrc --port %d --username \"%s\" "
208 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
209 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
210 GLOBALS_DUMP_FILE, log.filename);
213 get_db_and_rel_infos(&new_cluster.dbarr, CLUSTER_NEW);
215 stop_postmaster(false, false);
220 create_new_objects(void)
223 start_postmaster(CLUSTER_NEW, false);
225 install_support_functions();
227 prep_status("Restoring database schema to new cluster");
229 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
230 "--no-psqlrc --port %d --username \"%s\" "
231 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
232 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
233 DB_DUMP_FILE, log.filename);
236 /* regenerate now that we have db schemas */
237 dbarr_free(&new_cluster.dbarr);
238 get_db_and_rel_infos(&new_cluster.dbarr, CLUSTER_NEW);
240 uninstall_support_functions();
242 stop_postmaster(false, false);
247 copy_clog_xlog_xid(void)
249 char old_clog_path[MAXPGPATH];
250 char new_clog_path[MAXPGPATH];
252 /* copy old commit logs to new data dir */
253 prep_status("Deleting new commit clogs");
255 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
256 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
257 if (rmtree(new_clog_path, true) != true)
258 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
261 prep_status("Copying old commit clogs to new server");
262 /* libpgport's copydir() doesn't work in FRONTEND code */
264 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
267 /* flags: everything, no confirm, quiet, overwrite read-only */
268 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
271 old_clog_path, new_clog_path);
274 /* set the next transaction id of the new cluster */
275 prep_status("Setting next transaction id for new cluster");
276 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
277 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
280 /* now reset the wal archives in the new cluster */
281 prep_status("Resetting WAL archives");
282 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
283 new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
284 old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
285 new_cluster.pgdata, log.filename);
293 * We have frozen all xids, so set relfrozenxid and datfrozenxid
294 * to be the old cluster's xid counter, which we just set in the new
295 * cluster. User-table frozenxid values will be set by pg_dumpall
296 * --binary-upgrade, but objects not set by the pg_dump must have
297 * proper frozen counters.
311 prep_status("Setting frozenxid counters in new cluster");
313 conn_template1 = connectToServer("template1", CLUSTER_NEW);
315 /* set pg_database.datfrozenxid */
316 PQclear(executeQueryOrDie(conn_template1,
317 "UPDATE pg_catalog.pg_database "
318 "SET datfrozenxid = '%u'",
319 old_cluster.controldata.chkpnt_nxtxid));
321 /* get database names */
322 dbres = executeQueryOrDie(conn_template1,
323 "SELECT datname, datallowconn "
324 "FROM pg_catalog.pg_database");
326 i_datname = PQfnumber(dbres, "datname");
327 i_datallowconn = PQfnumber(dbres, "datallowconn");
329 ntups = PQntuples(dbres);
330 for (dbnum = 0; dbnum < ntups; dbnum++)
332 char *datname = PQgetvalue(dbres, dbnum, i_datname);
333 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
336 * We must update databases where datallowconn = false, e.g.
337 * template0, because autovacuum increments their datfrozenxids and
338 * relfrozenxids even if autovacuum is turned off, and even though all
339 * the data rows are already frozen To enable this, we temporarily
340 * change datallowconn.
342 if (strcmp(datallowconn, "f") == 0)
343 PQclear(executeQueryOrDie(conn_template1,
344 "UPDATE pg_catalog.pg_database "
345 "SET datallowconn = true "
346 "WHERE datname = '%s'", datname));
348 conn = connectToServer(datname, CLUSTER_NEW);
350 /* set pg_class.relfrozenxid */
351 PQclear(executeQueryOrDie(conn,
352 "UPDATE pg_catalog.pg_class "
353 "SET relfrozenxid = '%u' "
354 /* only heap and TOAST are vacuumed */
355 "WHERE relkind IN ('r', 't')",
356 old_cluster.controldata.chkpnt_nxtxid));
359 /* Reset datallowconn flag */
360 if (strcmp(datallowconn, "f") == 0)
361 PQclear(executeQueryOrDie(conn_template1,
362 "UPDATE pg_catalog.pg_database "
363 "SET datallowconn = false "
364 "WHERE datname = '%s'", datname));
369 PQfinish(conn_template1);
379 char filename[MAXPGPATH];
381 for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
382 pg_free(os_info.tablespaces[tblnum]);
383 pg_free(os_info.tablespaces);
385 dbarr_free(&old_cluster.dbarr);
386 dbarr_free(&new_cluster.dbarr);
387 pg_free(log.filename);
388 pg_free(os_info.user);
389 pg_free(old_cluster.major_version_str);
390 pg_free(new_cluster.major_version_str);
391 pg_free(old_cluster.controldata.lc_collate);
392 pg_free(new_cluster.controldata.lc_collate);
393 pg_free(old_cluster.controldata.lc_ctype);
394 pg_free(new_cluster.controldata.lc_ctype);
395 pg_free(old_cluster.controldata.encoding);
396 pg_free(new_cluster.controldata.encoding);
397 pg_free(old_cluster.tablespace_suffix);
398 pg_free(new_cluster.tablespace_suffix);
407 fclose(log.debug_fd);
409 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
411 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
413 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);