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.
168 * template0 is not frozen here, but data rows were frozen by initdb,
169 * and we set its datfrozenxid and relfrozenxids later to match the
170 * new xid counter later.
172 prep_status(ctx, "Freezing all rows on the new cluster");
174 SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --all --freeze >> %s 2>&1" SYSTEMQUOTE,
175 ctx->new.bindir, ctx->new.port, ctx->logfile);
178 get_pg_database_relfilenode(ctx, CLUSTER_NEW);
183 prepare_new_databases(migratorContext *ctx)
186 start_postmaster(ctx, CLUSTER_NEW, false);
189 * We set autovacuum_freeze_max_age to its maximum value so autovacuum
190 * does not launch here and delete clog files, before the frozen xids are
197 * We have to create the databases first so we can create the toast table
198 * placeholder relfiles.
200 prep_status(ctx, "Creating databases in the new cluster");
202 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on --port %d "
203 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
204 ctx->new.bindir, ctx->new.port, ctx->output_dir,
205 GLOBALS_DUMP_FILE, ctx->logfile);
208 get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
210 stop_postmaster(ctx, false, false);
215 create_new_objects(migratorContext *ctx)
218 start_postmaster(ctx, CLUSTER_NEW, false);
220 install_support_functions(ctx);
222 prep_status(ctx, "Restoring database schema to new cluster");
224 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on --port %d "
225 "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
226 ctx->new.bindir, ctx->new.port, ctx->output_dir,
227 DB_DUMP_FILE, ctx->logfile);
230 /* regenerate now that we have db schemas */
231 dbarr_free(&ctx->new.dbarr);
232 get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
234 uninstall_support_functions(ctx);
236 stop_postmaster(ctx, false, false);
241 copy_clog_xlog_xid(migratorContext *ctx)
243 char old_clog_path[MAXPGPATH];
244 char new_clog_path[MAXPGPATH];
246 /* copy old commit logs to new data dir */
247 prep_status(ctx, "Deleting new commit clogs");
249 snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", ctx->old.pgdata);
250 snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", ctx->new.pgdata);
251 if (rmtree(new_clog_path, true) != true)
252 pg_log(ctx, PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
255 prep_status(ctx, "Copying old commit clogs to new server");
256 /* libpgport's copydir() doesn't work in FRONTEND code */
258 exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
261 /* flags: everything, no confirm, quiet, overwrite read-only */
262 exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
265 old_clog_path, new_clog_path);
268 /* set the next transaction id of the new cluster */
269 prep_status(ctx, "Setting next transaction id for new cluster");
270 exec_prog(ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
271 ctx->new.bindir, ctx->old.controldata.chkpnt_nxtxid, ctx->new.pgdata);
274 /* now reset the wal archives in the new cluster */
275 prep_status(ctx, "Resetting WAL archives");
276 exec_prog(ctx, true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
277 ctx->new.bindir, ctx->old.controldata.chkpnt_tli,
278 ctx->old.controldata.logid, ctx->old.controldata.nxtlogseg,
279 ctx->new.pgdata, ctx->logfile);
287 * We have frozen all xids, so set relfrozenxid and datfrozenxid
288 * to be the old cluster's xid counter, which we just set in the new
289 * cluster. User-table frozenxid values will be set by pg_dumpall
290 * --binary-upgrade, but objects not set by the pg_dump must have
291 * proper frozen counters.
295 set_frozenxids(migratorContext *ctx)
298 PGconn *conn, *conn_template1;
304 prep_status(ctx, "Setting frozenxid counters in new cluster");
306 conn_template1 = connectToServer(ctx, "template1", CLUSTER_NEW);
308 /* set pg_database.datfrozenxid */
309 PQclear(executeQueryOrDie(ctx, conn_template1,
310 "UPDATE pg_catalog.pg_database "
311 "SET datfrozenxid = '%u'",
312 ctx->old.controldata.chkpnt_nxtxid));
314 /* get database names */
315 dbres = executeQueryOrDie(ctx, conn_template1,
316 "SELECT datname, datallowconn "
317 "FROM pg_catalog.pg_database");
319 i_datname = PQfnumber(dbres, "datname");
320 i_datallowconn = PQfnumber(dbres, "datallowconn");
322 ntups = PQntuples(dbres);
323 for (dbnum = 0; dbnum < ntups; dbnum++)
325 char *datname = PQgetvalue(dbres, dbnum, i_datname);
326 char *datallowconn= PQgetvalue(dbres, dbnum, i_datallowconn);
329 * We must update databases where datallowconn = false, e.g.
330 * template0, because autovacuum increments their datfrozenxids and
331 * relfrozenxids even if autovacuum is turned off, and even though
332 * all the data rows are already frozen To enable this, we
333 * temporarily change datallowconn.
335 if (strcmp(datallowconn, "f") == 0)
336 PQclear(executeQueryOrDie(ctx, conn_template1,
337 "UPDATE pg_catalog.pg_database "
338 "SET datallowconn = true "
339 "WHERE datname = '%s'", datname));
341 conn = connectToServer(ctx, datname, CLUSTER_NEW);
343 /* set pg_class.relfrozenxid */
344 PQclear(executeQueryOrDie(ctx, conn,
345 "UPDATE pg_catalog.pg_class "
346 "SET relfrozenxid = '%u' "
347 /* only heap and TOAST are vacuumed */
348 "WHERE relkind IN ('r', 't')",
349 ctx->old.controldata.chkpnt_nxtxid));
352 /* Reset datallowconn flag */
353 if (strcmp(datallowconn, "f") == 0)
354 PQclear(executeQueryOrDie(ctx, conn_template1,
355 "UPDATE pg_catalog.pg_database "
356 "SET datallowconn = false "
357 "WHERE datname = '%s'", datname));
362 PQfinish(conn_template1);
369 cleanup(migratorContext *ctx)
372 char filename[MAXPGPATH];
374 for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
375 pg_free(ctx->tablespaces[tblnum]);
376 pg_free(ctx->tablespaces);
378 dbarr_free(&ctx->old.dbarr);
379 dbarr_free(&ctx->new.dbarr);
380 pg_free(ctx->logfile);
382 pg_free(ctx->old.major_version_str);
383 pg_free(ctx->new.major_version_str);
384 pg_free(ctx->old.controldata.lc_collate);
385 pg_free(ctx->new.controldata.lc_collate);
386 pg_free(ctx->old.controldata.lc_ctype);
387 pg_free(ctx->new.controldata.lc_ctype);
388 pg_free(ctx->old.controldata.encoding);
389 pg_free(ctx->new.controldata.encoding);
390 pg_free(ctx->old.tablespace_suffix);
391 pg_free(ctx->new.tablespace_suffix);
393 if (ctx->log_fd != NULL)
400 fclose(ctx->debug_fd);
402 snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, ALL_DUMP_FILE);
404 snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, GLOBALS_DUMP_FILE);
406 snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, DB_DUMP_FILE);
412 * create_empty_output_directory
414 * Create empty directory for output files
417 create_empty_output_directory(migratorContext *ctx)
420 * rmtree() outputs a warning if the directory does not exist,
421 * so we try to create the directory first.
423 if (mkdir(ctx->output_dir, S_IRWXU) != 0)
426 rmtree(ctx->output_dir, false);
428 pg_log(ctx, PG_FATAL, "Cannot create subdirectory %s: %s\n",
429 ctx->output_dir, getErrorText(errno));