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, new_cluster;
29 main(int argc, char **argv)
31 char *sequence_script_file_name = NULL;
32 char *deletion_script_file_name = NULL;
33 bool live_check = false;
35 parseCommandLine(argc, argv);
37 output_check_banner(&live_check);
39 setup(argv[0], live_check);
41 check_cluster_versions();
42 check_cluster_compatibility(live_check);
44 check_old_cluster(live_check, &sequence_script_file_name);
48 start_postmaster(&new_cluster, false);
51 report_clusters_compatible();
53 pg_log(PG_REPORT, "\nPerforming Upgrade\n");
54 pg_log(PG_REPORT, "------------------\n");
56 disable_old_cluster();
57 prepare_new_cluster();
59 stop_postmaster(false, false);
62 * Destructive Changes to New Cluster
67 /* New now using xids of the old system */
69 prepare_new_databases();
73 transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
74 old_cluster.pgdata, new_cluster.pgdata);
77 * Assuming OIDs are only used in system tables, there is no need to
78 * restore the OID counter because we have not transferred any OIDs from
79 * the old system, but we do it anyway just in case. We do it late here
80 * because there is no need to have the schema load use new oids.
82 prep_status("Setting next oid for new cluster");
83 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
85 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
88 create_script_for_old_cluster_deletion(&deletion_script_file_name);
90 issue_warnings(sequence_script_file_name);
92 pg_log(PG_REPORT, "\nUpgrade complete\n");
93 pg_log(PG_REPORT, "----------------\n");
95 output_completion_banner(deletion_script_file_name);
97 pg_free(deletion_script_file_name);
98 pg_free(sequence_script_file_name);
107 setup(char *argv0, bool live_check)
109 char exec_path[MAXPGPATH]; /* full path to my executable */
112 * make sure the user has a clean environment, otherwise, we may confuse
113 * libpq when we connect to one (or both) of the servers.
115 check_for_libpq_envvars();
117 verify_directories();
119 /* no postmasters should be running */
120 if (!live_check && is_server_running(old_cluster.pgdata))
122 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
123 "Please shutdown that postmaster and try again.\n");
126 /* same goes for the new postmaster */
127 if (is_server_running(new_cluster.pgdata))
129 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
130 "Please shutdown that postmaster and try again.\n");
133 /* get path to pg_upgrade executable */
134 if (find_my_exec(argv0, exec_path) < 0)
135 pg_log(PG_FATAL, "Could not get pathname to pg_upgrade: %s\n", getErrorText(errno));
137 /* Trim off program name and keep just path */
138 *last_dir_separator(exec_path) = '\0';
139 canonicalize_path(exec_path);
140 os_info.exec_path = pg_strdup(exec_path);
145 disable_old_cluster(void)
147 /* rename pg_control so old server cannot be accidentally started */
148 rename_old_pg_control();
153 prepare_new_cluster(void)
156 * It would make more sense to freeze after loading the schema, but that
157 * would cause us to lose the frozenids restored by the load. We use
158 * --analyze so autovacuum doesn't update statistics later
160 prep_status("Analyzing all rows in the new cluster");
162 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
163 "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
164 new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
168 * We do freeze after analyze so pg_statistic is also frozen. template0 is
169 * not frozen here, but data rows were frozen by initdb, and we set its
170 * datfrozenxid and relfrozenxids later to match the new xid counter
173 prep_status("Freezing all rows on the new cluster");
175 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
176 "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
177 new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
180 get_pg_database_relfilenode(&new_cluster);
185 prepare_new_databases(void)
188 start_postmaster(&new_cluster, false);
191 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
192 * does not launch here and delete clog files, before the frozen xids are
199 * We have to create the databases first so we can create the toast table
200 * placeholder relfiles.
202 prep_status("Creating databases in the new cluster");
204 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
205 /* --no-psqlrc prevents AUTOCOMMIT=off */
206 "--no-psqlrc --port %d --username \"%s\" "
207 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
208 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
209 GLOBALS_DUMP_FILE, log_opts.filename);
212 get_db_and_rel_infos(&new_cluster);
214 stop_postmaster(false, false);
219 create_new_objects(void)
222 start_postmaster(&new_cluster, false);
224 install_support_functions();
226 prep_status("Restoring database schema to new cluster");
228 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
229 "--no-psqlrc --port %d --username \"%s\" "
230 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
231 new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
232 DB_DUMP_FILE, log_opts.filename);
235 /* regenerate now that we have db schemas */
236 dbarr_free(&new_cluster.dbarr);
237 get_db_and_rel_infos(&new_cluster);
239 uninstall_support_functions();
241 stop_postmaster(false, false);
246 copy_clog_xlog_xid(void)
248 char old_clog_path[MAXPGPATH];
249 char new_clog_path[MAXPGPATH];
251 /* copy old commit logs to new data dir */
252 prep_status("Deleting new commit clogs");
254 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
255 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
256 if (!rmtree(new_clog_path, true))
257 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
260 prep_status("Copying old commit clogs to new server");
262 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
265 /* flags: everything, no confirm, quiet, overwrite read-only */
266 exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
269 old_clog_path, new_clog_path);
272 /* set the next transaction id of the new cluster */
273 prep_status("Setting next transaction id for new cluster");
274 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
275 new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
278 /* now reset the wal archives in the new cluster */
279 prep_status("Resetting WAL archives");
280 exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
281 new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
282 old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
283 new_cluster.pgdata, log_opts.filename);
291 * We have frozen all xids, so set relfrozenxid and datfrozenxid
292 * to be the old cluster's xid counter, which we just set in the new
293 * cluster. User-table frozenxid values will be set by pg_dumpall
294 * --binary-upgrade, but objects not set by the pg_dump must have
295 * proper frozen counters.
309 prep_status("Setting frozenxid counters in new cluster");
311 conn_template1 = connectToServer(&new_cluster, "template1");
313 /* set pg_database.datfrozenxid */
314 PQclear(executeQueryOrDie(conn_template1,
315 "UPDATE pg_catalog.pg_database "
316 "SET datfrozenxid = '%u'",
317 old_cluster.controldata.chkpnt_nxtxid));
319 /* get database names */
320 dbres = executeQueryOrDie(conn_template1,
321 "SELECT datname, datallowconn "
322 "FROM pg_catalog.pg_database");
324 i_datname = PQfnumber(dbres, "datname");
325 i_datallowconn = PQfnumber(dbres, "datallowconn");
327 ntups = PQntuples(dbres);
328 for (dbnum = 0; dbnum < ntups; dbnum++)
330 char *datname = PQgetvalue(dbres, dbnum, i_datname);
331 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
334 * We must update databases where datallowconn = false, e.g.
335 * template0, because autovacuum increments their datfrozenxids and
336 * relfrozenxids even if autovacuum is turned off, and even though all
337 * the data rows are already frozen To enable this, we temporarily
338 * change datallowconn.
340 if (strcmp(datallowconn, "f") == 0)
341 PQclear(executeQueryOrDie(conn_template1,
342 "UPDATE pg_catalog.pg_database "
343 "SET datallowconn = true "
344 "WHERE datname = '%s'", datname));
346 conn = connectToServer(&new_cluster, datname);
348 /* set pg_class.relfrozenxid */
349 PQclear(executeQueryOrDie(conn,
350 "UPDATE pg_catalog.pg_class "
351 "SET relfrozenxid = '%u' "
352 /* only heap and TOAST are vacuumed */
353 "WHERE relkind IN ('r', 't')",
354 old_cluster.controldata.chkpnt_nxtxid));
357 /* Reset datallowconn flag */
358 if (strcmp(datallowconn, "f") == 0)
359 PQclear(executeQueryOrDie(conn_template1,
360 "UPDATE pg_catalog.pg_database "
361 "SET datallowconn = false "
362 "WHERE datname = '%s'", datname));
367 PQfinish(conn_template1);
377 char filename[MAXPGPATH];
379 for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
380 pg_free(os_info.tablespaces[tblnum]);
381 pg_free(os_info.tablespaces);
383 dbarr_free(&old_cluster.dbarr);
384 dbarr_free(&new_cluster.dbarr);
385 pg_free(log_opts.filename);
386 pg_free(os_info.user);
387 pg_free(old_cluster.controldata.lc_collate);
388 pg_free(new_cluster.controldata.lc_collate);
389 pg_free(old_cluster.controldata.lc_ctype);
390 pg_free(new_cluster.controldata.lc_ctype);
391 pg_free(old_cluster.controldata.encoding);
392 pg_free(new_cluster.controldata.encoding);
393 pg_free(old_cluster.tablespace_suffix);
394 pg_free(new_cluster.tablespace_suffix);
396 if (log_opts.fd != NULL)
402 if (log_opts.debug_fd)
403 fclose(log_opts.debug_fd);
405 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
407 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
409 snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);