]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
For pg_upgrade, update template0's datfrozenxid and its relfrozenxids to
[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          * 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.
171          */
172         prep_status(ctx, "Freezing all rows on the new cluster");
173         exec_prog(ctx, true,
174                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --all --freeze >> %s 2>&1" SYSTEMQUOTE,
175                           ctx->new.bindir, ctx->new.port, ctx->logfile);
176         check_ok(ctx);
177
178         get_pg_database_relfilenode(ctx, CLUSTER_NEW);
179 }
180
181
182 static void
183 prepare_new_databases(migratorContext *ctx)
184 {
185         /* -- NEW -- */
186         start_postmaster(ctx, CLUSTER_NEW, false);
187
188         /*
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
191          * set.
192          */
193
194         set_frozenxids(ctx);
195
196         /*
197          * We have to create the databases first so we can create the toast table
198          * placeholder relfiles.
199          */
200         prep_status(ctx, "Creating databases in the new cluster");
201         exec_prog(ctx, true,
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);
206         check_ok(ctx);
207
208         get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
209
210         stop_postmaster(ctx, false, false);
211 }
212
213
214 static void
215 create_new_objects(migratorContext *ctx)
216 {
217         /* -- NEW -- */
218         start_postmaster(ctx, CLUSTER_NEW, false);
219
220         install_support_functions(ctx);
221
222         prep_status(ctx, "Restoring database schema to new cluster");
223         exec_prog(ctx, true,
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);
228         check_ok(ctx);
229
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);
233
234         uninstall_support_functions(ctx);
235
236         stop_postmaster(ctx, false, false);
237 }
238
239
240 static void
241 copy_clog_xlog_xid(migratorContext *ctx)
242 {
243         char            old_clog_path[MAXPGPATH];
244         char            new_clog_path[MAXPGPATH];
245
246         /* copy old commit logs to new data dir */
247         prep_status(ctx, "Deleting new commit clogs");
248
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);
253         check_ok(ctx);
254
255         prep_status(ctx, "Copying old commit clogs to new server");
256         /* libpgport's copydir() doesn't work in FRONTEND code */
257 #ifndef WIN32
258         exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
259                           "cp -Rf",
260 #else
261         /* flags: everything, no confirm, quiet, overwrite read-only */
262         exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
263                           "xcopy /e /y /q /r",
264 #endif
265                           old_clog_path, new_clog_path);
266         check_ok(ctx);
267
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);
272         check_ok(ctx);
273
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);
280         check_ok(ctx);
281 }
282
283
284 /*
285  *      set_frozenxids()
286  *
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.
292  */
293 static
294 void
295 set_frozenxids(migratorContext *ctx)
296 {
297         int                     dbnum;
298         PGconn     *conn, *conn_template1;
299         PGresult   *dbres;
300         int                     ntups;
301         int                     i_datname;
302         int                     i_datallowconn;
303
304         prep_status(ctx, "Setting frozenxid counters in new cluster");
305
306         conn_template1 = connectToServer(ctx, "template1", CLUSTER_NEW);
307
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));
313
314         /* get database names */
315         dbres = executeQueryOrDie(ctx, conn_template1,
316                                                           "SELECT       datname, datallowconn "
317                                                           "FROM pg_catalog.pg_database");
318
319         i_datname = PQfnumber(dbres, "datname");
320         i_datallowconn = PQfnumber(dbres, "datallowconn");
321
322         ntups = PQntuples(dbres);
323         for (dbnum = 0; dbnum < ntups; dbnum++)
324         {
325                 char *datname = PQgetvalue(dbres, dbnum, i_datname);
326                 char *datallowconn= PQgetvalue(dbres, dbnum, i_datallowconn);
327
328                 /*
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.
334                  */
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));
340
341                 conn = connectToServer(ctx, datname, CLUSTER_NEW);
342
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));
350                 PQfinish(conn);
351
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));
358         }
359
360         PQclear(dbres);
361
362         PQfinish(conn_template1);
363
364         check_ok(ctx);
365 }
366
367
368 static void
369 cleanup(migratorContext *ctx)
370 {
371         int                     tblnum;
372         char            filename[MAXPGPATH];
373
374         for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
375                 pg_free(ctx->tablespaces[tblnum]);
376         pg_free(ctx->tablespaces);
377
378         dbarr_free(&ctx->old.dbarr);
379         dbarr_free(&ctx->new.dbarr);
380         pg_free(ctx->logfile);
381         pg_free(ctx->user);
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);
392
393         if (ctx->log_fd != NULL)
394         {
395                 fclose(ctx->log_fd);
396                 ctx->log_fd = NULL;
397         }
398
399         if (ctx->debug_fd)
400                 fclose(ctx->debug_fd);
401
402         snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, ALL_DUMP_FILE);
403         unlink(filename);
404         snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, GLOBALS_DUMP_FILE);
405         unlink(filename);
406         snprintf(filename, sizeof(filename), "%s/%s", ctx->output_dir, DB_DUMP_FILE);
407         unlink(filename);
408 }
409
410
411 /*
412  * create_empty_output_directory
413  *
414  *      Create empty directory for output files
415  */
416 static void
417 create_empty_output_directory(migratorContext *ctx)
418 {
419         /*
420          *      rmtree() outputs a warning if the directory does not exist,
421          *      so we try to create the directory first.
422          */
423         if (mkdir(ctx->output_dir, S_IRWXU) != 0)
424         {
425                 if (errno == EEXIST)
426                         rmtree(ctx->output_dir, false);
427                 else
428                         pg_log(ctx, PG_FATAL, "Cannot create subdirectory %s: %s\n",
429                            ctx->output_dir, getErrorText(errno));
430         }
431 }