]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
4067f4bd268e3e5b12c46168470c94ed7acc5ba8
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
1 /*
2  *      pg_upgrade.c
3  *
4  *      main source file
5  */
6
7 #include "pg_upgrade.h"
8
9 #ifdef HAVE_LANGINFO_H
10 #include <langinfo.h>
11 #endif
12
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);
22
23
24 int
25 main(int argc, char **argv)
26 {
27         migratorContext ctx;
28         char       *sequence_script_file_name = NULL;
29         char       *deletion_script_file_name = NULL;
30         bool            live_check = false;
31
32         memset(&ctx, 0, sizeof(ctx));
33
34         parseCommandLine(&ctx, argc, argv);
35
36         output_check_banner(&ctx, &live_check);
37
38         setup(&ctx, argv[0], live_check);
39
40         create_empty_output_directory(&ctx);
41
42         check_cluster_versions(&ctx);
43         check_cluster_compatibility(&ctx, live_check);
44
45         check_old_cluster(&ctx, live_check, &sequence_script_file_name);
46
47
48         /* -- NEW -- */
49         start_postmaster(&ctx, CLUSTER_NEW, false);
50
51         check_new_cluster(&ctx);
52         report_clusters_compatible(&ctx);
53
54         pg_log(&ctx, PG_REPORT, "\nPerforming Migration\n");
55         pg_log(&ctx, PG_REPORT, "--------------------\n");
56
57         disable_old_cluster(&ctx);
58         prepare_new_cluster(&ctx);
59
60         stop_postmaster(&ctx, false, false);
61
62         /*
63          * Destructive Changes to New Cluster
64          */
65
66         copy_clog_xlog_xid(&ctx);
67
68         /* New now using xids of the old system */
69
70         prepare_new_databases(&ctx);
71
72         create_new_objects(&ctx);
73
74         transfer_all_new_dbs(&ctx, &ctx.old.dbarr, &ctx.new.dbarr,
75                                                  ctx.old.pgdata, ctx.new.pgdata);
76
77         /*
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.
82          */
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);
86         check_ok(&ctx);
87
88         create_script_for_old_cluster_deletion(&ctx, &deletion_script_file_name);
89
90         issue_warnings(&ctx, sequence_script_file_name);
91
92         pg_log(&ctx, PG_REPORT, "\nUpgrade complete\n");
93         pg_log(&ctx, PG_REPORT, "----------------\n");
94
95         output_completion_banner(&ctx, deletion_script_file_name);
96
97         pg_free(deletion_script_file_name);
98         pg_free(sequence_script_file_name);
99
100         cleanup(&ctx);
101
102         return 0;
103 }
104
105
106 static void
107 setup(migratorContext *ctx, char *argv0, bool live_check)
108 {
109         char            exec_path[MAXPGPATH];   /* full path to my executable */
110
111         /*
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.
114          */
115         check_for_libpq_envvars(ctx);
116
117         verify_directories(ctx);
118
119         /* no postmasters should be running */
120         if (!live_check && is_server_running(ctx, ctx->old.pgdata))
121         {
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");
124         }
125
126         /* same goes for the new postmaster */
127         if (is_server_running(ctx, ctx->new.pgdata))
128         {
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");
131         }
132
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));
136
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);
141 }
142
143
144 static void
145 disable_old_cluster(migratorContext *ctx)
146 {
147         /* rename pg_control so old server cannot be accidentally started */
148         rename_old_pg_control(ctx);
149 }
150
151
152 static void
153 prepare_new_cluster(migratorContext *ctx)
154 {
155         /*
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
159          */
160         prep_status(ctx, "Analyzing all rows in the new cluster");
161         exec_prog(ctx, true,
162                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --all --analyze >> %s 2>&1" SYSTEMQUOTE,
163                           ctx->new.bindir, ctx->new.port, ctx->logfile);
164         check_ok(ctx);
165
166         /*
167          * We do freeze after analyze so pg_statistic is also frozen
168          */
169         prep_status(ctx, "Freezing all rows on the new cluster");
170         exec_prog(ctx, true,
171                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --all --freeze >> %s 2>&1" SYSTEMQUOTE,
172                           ctx->new.bindir, ctx->new.port, ctx->logfile);
173         check_ok(ctx);
174
175         get_pg_database_relfilenode(ctx, CLUSTER_NEW);
176 }
177
178
179 static void
180 prepare_new_databases(migratorContext *ctx)
181 {
182         /* -- NEW -- */
183         start_postmaster(ctx, CLUSTER_NEW, false);
184
185         /*
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
188          * set.
189          */
190
191         set_frozenxids(ctx);
192
193         /*
194          * We have to create the databases first so we can create the toast table
195          * placeholder relfiles.
196          */
197         prep_status(ctx, "Creating databases in the new cluster");
198         exec_prog(ctx, true,
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);
203         check_ok(ctx);
204
205         get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
206
207         stop_postmaster(ctx, false, false);
208 }
209
210
211 static void
212 create_new_objects(migratorContext *ctx)
213 {
214         /* -- NEW -- */
215         start_postmaster(ctx, CLUSTER_NEW, false);
216
217         install_support_functions(ctx);
218
219         prep_status(ctx, "Restoring database schema to new cluster");
220         exec_prog(ctx, true,
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);
225         check_ok(ctx);
226
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);
230
231         uninstall_support_functions(ctx);
232
233         stop_postmaster(ctx, false, false);
234 }
235
236
237 static void
238 copy_clog_xlog_xid(migratorContext *ctx)
239 {
240         char            old_clog_path[MAXPGPATH];
241         char            new_clog_path[MAXPGPATH];
242
243         /* copy old commit logs to new data dir */
244         prep_status(ctx, "Deleting new commit clogs");
245
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);
250         check_ok(ctx);
251
252         prep_status(ctx, "Copying old commit clogs to new server");
253         /* libpgport's copydir() doesn't work in FRONTEND code */
254 #ifndef WIN32
255         exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
256                           "cp -Rf",
257 #else
258         /* flags: everything, no confirm, quiet, overwrite read-only */
259         exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
260                           "xcopy /e /y /q /r",
261 #endif
262                           old_clog_path, new_clog_path);
263         check_ok(ctx);
264
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);
269         check_ok(ctx);
270
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);
277         check_ok(ctx);
278 }
279
280
281 /*
282  *      set_frozenxids()
283  *
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.
289  */
290 static
291 void
292 set_frozenxids(migratorContext *ctx)
293 {
294         int                     dbnum;
295         PGconn     *conn;
296         PGresult   *dbres;
297         int                     ntups;
298
299         prep_status(ctx, "Setting frozenxid counters in new cluster");
300
301         conn = connectToServer(ctx, "template1", CLUSTER_NEW);
302
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));
310
311         /* get database names */
312         dbres = executeQueryOrDie(ctx, conn,
313                                                           "SELECT       datname "
314                                                           "FROM pg_catalog.pg_database "
315                                                           "WHERE        datname != 'template0'");
316
317         /* free dbres below */
318         PQfinish(conn);
319
320         ntups = PQntuples(dbres);
321         for (dbnum = 0; dbnum < ntups; dbnum++)
322         {
323                 conn = connectToServer(ctx, PQgetvalue(dbres, dbnum, 0), CLUSTER_NEW);
324
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 "
331                                                                   "             relkind = 't'",
332                                                                   ctx->old.controldata.chkpnt_nxtxid));
333                 PQfinish(conn);
334         }
335
336         PQclear(dbres);
337
338         check_ok(ctx);
339 }
340
341
342 static void
343 cleanup(migratorContext *ctx)
344 {
345         int                     tblnum;
346         char            filename[MAXPGPATH];
347
348         for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
349                 pg_free(ctx->tablespaces[tblnum]);
350         pg_free(ctx->tablespaces);
351
352         dbarr_free(&ctx->old.dbarr);
353         dbarr_free(&ctx->new.dbarr);
354         pg_free(ctx->logfile);
355         pg_free(ctx->user);
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);
366
367         if (ctx->log_fd != NULL)
368         {
369                 fclose(ctx->log_fd);
370                 ctx->log_fd = NULL;
371         }
372
373         if (ctx->debug_fd)
374                 fclose(ctx->debug_fd);
375
376         snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, ALL_DUMP_FILE);
377         unlink(filename);
378         snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, GLOBALS_DUMP_FILE);
379         unlink(filename);
380         snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, DB_DUMP_FILE);
381         unlink(filename);
382 }
383
384
385 /*
386  * create_empty_output_directory
387  *
388  *      Create empty directory for output files
389  */
390 static void
391 create_empty_output_directory(migratorContext *ctx)
392 {
393         /*
394          *      rmtree() outputs a warning if the directory does not exist,
395          *      so we try to create the directory first.
396          */
397         if (mkdir(ctx->output_dir, S_IRWXU) != 0)
398         {
399                 if (errno == EEXIST)
400                         rmtree(ctx->output_dir, false);
401                 else
402                         pg_log(ctx, PG_FATAL, "Cannot create subdirectory %s: %s\n",
403                            ctx->output_dir, getErrorText(errno));
404         }
405 }