7 #include "pg_upgrade.h"
13 static void disable_old_cluster(migratorContext *ctx);
14 static void prepare_new_cluster(migratorContext *ctx);
15 static void prepare_new_databases(migratorContext *ctx);
16 static void create_new_objects(migratorContext *ctx);
17 static void copy_clog_xlog_xid(migratorContext *ctx);
18 static void set_frozenxids(migratorContext *ctx);
19 static void setup(migratorContext *ctx, char *argv0, bool live_check);
20 static void cleanup(migratorContext *ctx);
21 static void create_empty_output_directory(migratorContext *ctx);
25 main(int argc, char **argv)
28 char *sequence_script_file_name = NULL;
29 char *deletion_script_file_name = NULL;
30 bool live_check = false;
32 memset(&ctx, 0, sizeof(ctx));
34 parseCommandLine(&ctx, argc, argv);
36 output_check_banner(&ctx, &live_check);
38 setup(&ctx, argv[0], live_check);
40 create_empty_output_directory(&ctx);
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\" > " DEVNULL SYSTEMQUOTE,
85 ctx.new.bindir, ctx.old.controldata.chkpnt_nxtoid, ctx.new.pgdata);
88 create_script_for_old_cluster_deletion(&ctx, &deletion_script_file_name);
90 issue_warnings(&ctx, sequence_script_file_name);
92 pg_log(&ctx, PG_REPORT, "\nUpgrade complete\n");
93 pg_log(&ctx, PG_REPORT, "----------------\n");
95 output_completion_banner(&ctx, deletion_script_file_name);
97 pg_free(deletion_script_file_name);
98 pg_free(sequence_script_file_name);
107 setup(migratorContext *ctx, 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(ctx);
117 verify_directories(ctx);
119 /* no postmasters should be running */
120 if (!live_check && is_server_running(ctx, ctx->old.pgdata))
122 pg_log(ctx, 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(ctx, ctx->new.pgdata))
129 pg_log(ctx, 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(ctx, 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 ctx->exec_path = pg_strdup(ctx, exec_path);
145 disable_old_cluster(migratorContext *ctx)
147 /* rename pg_control so old server cannot be accidentally started */
148 rename_old_pg_control(ctx);
153 prepare_new_cluster(migratorContext *ctx)
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(ctx, "Analyzing all rows in the new cluster");
162 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --all --analyze >> %s 2>&1" SYSTEMQUOTE,
163 ctx->new.bindir, ctx->new.port, ctx->logfile);
167 * We do freeze after analyze so pg_statistic is also frozen
169 prep_status(ctx, "Freezing all rows on the new cluster");
171 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --all --freeze >> %s 2>&1" SYSTEMQUOTE,
172 ctx->new.bindir, ctx->new.port, ctx->logfile);
175 get_pg_database_relfilenode(ctx, CLUSTER_NEW);
180 prepare_new_databases(migratorContext *ctx)
183 start_postmaster(ctx, CLUSTER_NEW, false);
186 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
187 * does not launch here and delete clog files, before the frozen xids are
194 * We have to create the databases first so we can create the toast table
195 * placeholder relfiles.
197 prep_status(ctx, "Creating databases in the new cluster");
199 SYSTEMQUOTE "\"%s/%s\" --set ON_ERROR_STOP=on --port %d "
200 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
201 ctx->new.bindir, ctx->new.psql_exe, ctx->new.port,
202 ctx->output_dir, GLOBALS_DUMP_FILE, ctx->logfile);
205 get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
207 stop_postmaster(ctx, false, false);
212 create_new_objects(migratorContext *ctx)
215 start_postmaster(ctx, CLUSTER_NEW, false);
217 install_support_functions(ctx);
219 prep_status(ctx, "Restoring database schema to new cluster");
221 SYSTEMQUOTE "\"%s/%s\" --set ON_ERROR_STOP=on --port %d "
222 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
223 ctx->new.bindir, ctx->new.psql_exe, ctx->new.port,
224 ctx->output_dir, DB_DUMP_FILE, ctx->logfile);
227 /* regenerate now that we have db schemas */
228 dbarr_free(&ctx->new.dbarr);
229 get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
231 uninstall_support_functions(ctx);
233 stop_postmaster(ctx, false, false);
238 copy_clog_xlog_xid(migratorContext *ctx)
240 char old_clog_path[MAXPGPATH];
241 char new_clog_path[MAXPGPATH];
243 /* copy old commit logs to new data dir */
244 prep_status(ctx, "Deleting new commit clogs");
246 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", ctx->old.pgdata);
247 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", ctx->new.pgdata);
248 if (rmtree(new_clog_path, true) != true)
249 pg_log(ctx, PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
252 prep_status(ctx, "Copying old commit clogs to new server");
253 /* libpgport's copydir() doesn't work in FRONTEND code */
255 exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
258 /* flags: everything, no confirm, quiet, overwrite read-only */
259 exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
262 old_clog_path, new_clog_path);
265 /* set the next transaction id of the new cluster */
266 prep_status(ctx, "Setting next transaction id for new cluster");
267 exec_prog(ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
268 ctx->new.bindir, ctx->old.controldata.chkpnt_nxtxid, ctx->new.pgdata);
271 /* now reset the wal archives in the new cluster */
272 prep_status(ctx, "Resetting WAL archives");
273 exec_prog(ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
274 ctx->new.bindir, ctx->old.controldata.chkpnt_tli,
275 ctx->old.controldata.logid, ctx->old.controldata.nxtlogseg,
276 ctx->new.pgdata, ctx->logfile);
284 * We have frozen all xids, so set relfrozenxid and datfrozenxid
285 * to be the old cluster's xid counter, which we just set in the new
286 * cluster. User-table frozenxid values will be set by pg_dumpall
287 * --binary-upgrade, but objects not set by the pg_dump must have
288 * proper frozen counters.
292 set_frozenxids(migratorContext *ctx)
299 prep_status(ctx, "Setting frozenxid counters in new cluster");
301 conn = connectToServer(ctx, "template1", CLUSTER_NEW);
303 /* set pg_database.datfrozenxid */
304 PQclear(executeQueryOrDie(ctx, conn,
305 "UPDATE pg_catalog.pg_database "
306 "SET datfrozenxid = '%u' "
307 /* cannot connect to 'template0', so ignore */
308 "WHERE datname != 'template0'",
309 ctx->old.controldata.chkpnt_nxtxid));
311 /* get database names */
312 dbres = executeQueryOrDie(ctx, conn,
314 "FROM pg_catalog.pg_database "
315 "WHERE datname != 'template0'");
317 /* free dbres below */
320 ntups = PQntuples(dbres);
321 for (dbnum = 0; dbnum < ntups; dbnum++)
323 conn = connectToServer(ctx, PQgetvalue(dbres, dbnum, 0), CLUSTER_NEW);
325 /* set pg_class.relfrozenxid */
326 PQclear(executeQueryOrDie(ctx, conn,
327 "UPDATE pg_catalog.pg_class "
328 "SET relfrozenxid = '%u' "
329 /* only heap and TOAST are vacuumed */
330 "WHERE relkind = 'r' OR "
332 ctx->old.controldata.chkpnt_nxtxid));
343 cleanup(migratorContext *ctx)
346 char filename[MAXPGPATH];
348 for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
349 pg_free(ctx->tablespaces[tblnum]);
350 pg_free(ctx->tablespaces);
352 dbarr_free(&ctx->old.dbarr);
353 dbarr_free(&ctx->new.dbarr);
354 pg_free(ctx->logfile);
356 pg_free(ctx->old.major_version_str);
357 pg_free(ctx->new.major_version_str);
358 pg_free(ctx->old.controldata.lc_collate);
359 pg_free(ctx->new.controldata.lc_collate);
360 pg_free(ctx->old.controldata.lc_ctype);
361 pg_free(ctx->new.controldata.lc_ctype);
362 pg_free(ctx->old.controldata.encoding);
363 pg_free(ctx->new.controldata.encoding);
364 pg_free(ctx->old.tablespace_suffix);
365 pg_free(ctx->new.tablespace_suffix);
367 if (ctx->log_fd != NULL)
374 fclose(ctx->debug_fd);
376 snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, ALL_DUMP_FILE);
378 snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, GLOBALS_DUMP_FILE);
380 snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, DB_DUMP_FILE);
386 * create_empty_output_directory
388 * Create empty directory for output files
391 create_empty_output_directory(migratorContext *ctx)
394 * rmtree() outputs a warning if the directory does not exist,
395 * so we try to create the directory first.
397 if (mkdir(ctx->output_dir, S_IRWXU) != 0)
400 rmtree(ctx->output_dir, false);
402 pg_log(ctx, PG_FATAL, "Cannot create subdirectory %s: %s\n",
403 ctx->output_dir, getErrorText(errno));