6 * Copyright (c) 2010-2010, PostgreSQL Global Development Group
7 * $PostgreSQL: pgsql/contrib/pg_upgrade/pg_upgrade.c,v 1.8 2010/07/03 16:25:01 momjian Exp $
10 #include "pg_upgrade.h"
12 #ifdef HAVE_LANGINFO_H
16 static void disable_old_cluster(migratorContext *ctx);
17 static void prepare_new_cluster(migratorContext *ctx);
18 static void prepare_new_databases(migratorContext *ctx);
19 static void create_new_objects(migratorContext *ctx);
20 static void copy_clog_xlog_xid(migratorContext *ctx);
21 static void set_frozenxids(migratorContext *ctx);
22 static void setup(migratorContext *ctx, char *argv0, bool live_check);
23 static void cleanup(migratorContext *ctx);
27 main(int argc, char **argv)
30 char *sequence_script_file_name = NULL;
31 char *deletion_script_file_name = NULL;
32 bool live_check = false;
34 memset(&ctx, 0, sizeof(ctx));
36 parseCommandLine(&ctx, argc, argv);
38 output_check_banner(&ctx, &live_check);
40 setup(&ctx, argv[0], live_check);
42 check_cluster_versions(&ctx);
43 check_cluster_compatibility(&ctx, live_check);
45 check_old_cluster(&ctx, live_check, &sequence_script_file_name);
49 start_postmaster(&ctx, CLUSTER_NEW, false);
51 check_new_cluster(&ctx);
52 report_clusters_compatible(&ctx);
54 pg_log(&ctx, PG_REPORT, "\nPerforming Migration\n");
55 pg_log(&ctx, PG_REPORT, "--------------------\n");
57 disable_old_cluster(&ctx);
58 prepare_new_cluster(&ctx);
60 stop_postmaster(&ctx, false, false);
63 * Destructive Changes to New Cluster
66 copy_clog_xlog_xid(&ctx);
68 /* New now using xids of the old system */
70 prepare_new_databases(&ctx);
72 create_new_objects(&ctx);
74 transfer_all_new_dbs(&ctx, &ctx.old.dbarr, &ctx.new.dbarr,
75 ctx.old.pgdata, ctx.new.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(&ctx, "Setting next oid for new cluster");
84 exec_prog(&ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
86 ctx.new.bindir, ctx.old.controldata.chkpnt_nxtoid, ctx.new.pgdata);
89 create_script_for_old_cluster_deletion(&ctx, &deletion_script_file_name);
91 issue_warnings(&ctx, sequence_script_file_name);
93 pg_log(&ctx, PG_REPORT, "\nUpgrade complete\n");
94 pg_log(&ctx, PG_REPORT, "----------------\n");
96 output_completion_banner(&ctx, deletion_script_file_name);
98 pg_free(deletion_script_file_name);
99 pg_free(sequence_script_file_name);
108 setup(migratorContext *ctx, 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(ctx);
118 verify_directories(ctx);
120 /* no postmasters should be running */
121 if (!live_check && is_server_running(ctx, ctx->old.pgdata))
123 pg_log(ctx, 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(ctx, ctx->new.pgdata))
130 pg_log(ctx, 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(ctx, 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 ctx->exec_path = pg_strdup(ctx, exec_path);
146 disable_old_cluster(migratorContext *ctx)
148 /* rename pg_control so old server cannot be accidentally started */
149 rename_old_pg_control(ctx);
154 prepare_new_cluster(migratorContext *ctx)
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(ctx, "Analyzing all rows in the new cluster");
163 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
164 "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
165 ctx->new.bindir, ctx->new.port, ctx->user, ctx->logfile);
169 * We do freeze after analyze so pg_statistic is also frozen.
170 * template0 is not frozen here, but data rows were frozen by initdb,
171 * and we set its datfrozenxid and relfrozenxids later to match the
172 * new xid counter later.
174 prep_status(ctx, "Freezing all rows on the new cluster");
176 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
177 "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
178 ctx->new.bindir, ctx->new.port, ctx->user, ctx->logfile);
181 get_pg_database_relfilenode(ctx, CLUSTER_NEW);
186 prepare_new_databases(migratorContext *ctx)
189 start_postmaster(ctx, 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(ctx, "Creating databases in the new cluster");
205 SYSTEMQUOTE "\"%s/psql\" --port %d --username \"%s\" "
206 "--set ON_ERROR_STOP=on -f \"%s/%s\" --dbname template1 >> \"%s\""
208 ctx->new.bindir, ctx->new.port, ctx->user, ctx->cwd,
209 GLOBALS_DUMP_FILE, ctx->logfile);
212 get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
214 stop_postmaster(ctx, false, false);
219 create_new_objects(migratorContext *ctx)
222 start_postmaster(ctx, CLUSTER_NEW, false);
224 install_support_functions(ctx);
226 prep_status(ctx, "Restoring database schema to new cluster");
228 SYSTEMQUOTE "\"%s/psql\" --port %d --username \"%s\" "
229 "--set ON_ERROR_STOP=on -f \"%s/%s\" --dbname template1 >> \"%s\""
231 ctx->new.bindir, ctx->new.port, ctx->user, ctx->cwd,
232 DB_DUMP_FILE, ctx->logfile);
235 /* regenerate now that we have db schemas */
236 dbarr_free(&ctx->new.dbarr);
237 get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
239 uninstall_support_functions(ctx);
241 stop_postmaster(ctx, false, false);
246 copy_clog_xlog_xid(migratorContext *ctx)
248 char old_clog_path[MAXPGPATH];
249 char new_clog_path[MAXPGPATH];
251 /* copy old commit logs to new data dir */
252 prep_status(ctx, "Deleting new commit clogs");
254 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", ctx->old.pgdata);
255 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", ctx->new.pgdata);
256 if (rmtree(new_clog_path, true) != true)
257 pg_log(ctx, PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
260 prep_status(ctx, "Copying old commit clogs to new server");
261 /* libpgport's copydir() doesn't work in FRONTEND code */
263 exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
266 /* flags: everything, no confirm, quiet, overwrite read-only */
267 exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
270 old_clog_path, new_clog_path);
273 /* set the next transaction id of the new cluster */
274 prep_status(ctx, "Setting next transaction id for new cluster");
275 exec_prog(ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
276 ctx->new.bindir, ctx->old.controldata.chkpnt_nxtxid, ctx->new.pgdata);
279 /* now reset the wal archives in the new cluster */
280 prep_status(ctx, "Resetting WAL archives");
281 exec_prog(ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
282 ctx->new.bindir, ctx->old.controldata.chkpnt_tli,
283 ctx->old.controldata.logid, ctx->old.controldata.nxtlogseg,
284 ctx->new.pgdata, ctx->logfile);
292 * We have frozen all xids, so set relfrozenxid and datfrozenxid
293 * to be the old cluster's xid counter, which we just set in the new
294 * cluster. User-table frozenxid values will be set by pg_dumpall
295 * --binary-upgrade, but objects not set by the pg_dump must have
296 * proper frozen counters.
300 set_frozenxids(migratorContext *ctx)
303 PGconn *conn, *conn_template1;
309 prep_status(ctx, "Setting frozenxid counters in new cluster");
311 conn_template1 = connectToServer(ctx, "template1", CLUSTER_NEW);
313 /* set pg_database.datfrozenxid */
314 PQclear(executeQueryOrDie(ctx, conn_template1,
315 "UPDATE pg_catalog.pg_database "
316 "SET datfrozenxid = '%u'",
317 ctx->old.controldata.chkpnt_nxtxid));
319 /* get database names */
320 dbres = executeQueryOrDie(ctx, 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
337 * all the data rows are already frozen To enable this, we
338 * temporarily change datallowconn.
340 if (strcmp(datallowconn, "f") == 0)
341 PQclear(executeQueryOrDie(ctx, conn_template1,
342 "UPDATE pg_catalog.pg_database "
343 "SET datallowconn = true "
344 "WHERE datname = '%s'", datname));
346 conn = connectToServer(ctx, datname, CLUSTER_NEW);
348 /* set pg_class.relfrozenxid */
349 PQclear(executeQueryOrDie(ctx, 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 ctx->old.controldata.chkpnt_nxtxid));
357 /* Reset datallowconn flag */
358 if (strcmp(datallowconn, "f") == 0)
359 PQclear(executeQueryOrDie(ctx, conn_template1,
360 "UPDATE pg_catalog.pg_database "
361 "SET datallowconn = false "
362 "WHERE datname = '%s'", datname));
367 PQfinish(conn_template1);
374 cleanup(migratorContext *ctx)
377 char filename[MAXPGPATH];
379 for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
380 pg_free(ctx->tablespaces[tblnum]);
381 pg_free(ctx->tablespaces);
383 dbarr_free(&ctx->old.dbarr);
384 dbarr_free(&ctx->new.dbarr);
385 pg_free(ctx->logfile);
387 pg_free(ctx->old.major_version_str);
388 pg_free(ctx->new.major_version_str);
389 pg_free(ctx->old.controldata.lc_collate);
390 pg_free(ctx->new.controldata.lc_collate);
391 pg_free(ctx->old.controldata.lc_ctype);
392 pg_free(ctx->new.controldata.lc_ctype);
393 pg_free(ctx->old.controldata.encoding);
394 pg_free(ctx->new.controldata.encoding);
395 pg_free(ctx->old.tablespace_suffix);
396 pg_free(ctx->new.tablespace_suffix);
398 if (ctx->log_fd != NULL)
405 fclose(ctx->debug_fd);
407 snprintf(filename, sizeof(filename), "%s/%s", ctx->cwd, ALL_DUMP_FILE);
409 snprintf(filename, sizeof(filename), "%s/%s", ctx->cwd, GLOBALS_DUMP_FILE);
411 snprintf(filename, sizeof(filename), "%s/%s", ctx->cwd, DB_DUMP_FILE);