]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
Add copyrights to pg_upgrade and pg_upgrade_tools files, per Tom.
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
1 /*
2  *      pg_upgrade.c
3  *
4  *      main source file
5  *
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 $
8  */
9
10 #include "pg_upgrade.h"
11
12 #ifdef HAVE_LANGINFO_H
13 #include <langinfo.h>
14 #endif
15
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);
24
25
26 int
27 main(int argc, char **argv)
28 {
29         migratorContext ctx;
30         char       *sequence_script_file_name = NULL;
31         char       *deletion_script_file_name = NULL;
32         bool            live_check = false;
33
34         memset(&ctx, 0, sizeof(ctx));
35
36         parseCommandLine(&ctx, argc, argv);
37
38         output_check_banner(&ctx, &live_check);
39
40         setup(&ctx, argv[0], live_check);
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\" > "
85                   DEVNULL SYSTEMQUOTE,
86                   ctx.new.bindir, ctx.old.controldata.chkpnt_nxtoid, ctx.new.pgdata);
87         check_ok(&ctx);
88
89         create_script_for_old_cluster_deletion(&ctx, &deletion_script_file_name);
90
91         issue_warnings(&ctx, sequence_script_file_name);
92
93         pg_log(&ctx, PG_REPORT, "\nUpgrade complete\n");
94         pg_log(&ctx, PG_REPORT, "----------------\n");
95
96         output_completion_banner(&ctx, deletion_script_file_name);
97
98         pg_free(deletion_script_file_name);
99         pg_free(sequence_script_file_name);
100
101         cleanup(&ctx);
102
103         return 0;
104 }
105
106
107 static void
108 setup(migratorContext *ctx, char *argv0, bool live_check)
109 {
110         char            exec_path[MAXPGPATH];   /* full path to my executable */
111
112         /*
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.
115          */
116         check_for_libpq_envvars(ctx);
117
118         verify_directories(ctx);
119
120         /* no postmasters should be running */
121         if (!live_check && is_server_running(ctx, ctx->old.pgdata))
122         {
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");
125         }
126
127         /* same goes for the new postmaster */
128         if (is_server_running(ctx, ctx->new.pgdata))
129         {
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");
132         }
133
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));
137
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);
142 }
143
144
145 static void
146 disable_old_cluster(migratorContext *ctx)
147 {
148         /* rename pg_control so old server cannot be accidentally started */
149         rename_old_pg_control(ctx);
150 }
151
152
153 static void
154 prepare_new_cluster(migratorContext *ctx)
155 {
156         /*
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
160          */
161         prep_status(ctx, "Analyzing all rows in the new cluster");
162         exec_prog(ctx, true,
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);
166         check_ok(ctx);
167
168         /*
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.
173          */
174         prep_status(ctx, "Freezing all rows on the new cluster");
175         exec_prog(ctx, true,
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);
179         check_ok(ctx);
180
181         get_pg_database_relfilenode(ctx, CLUSTER_NEW);
182 }
183
184
185 static void
186 prepare_new_databases(migratorContext *ctx)
187 {
188         /* -- NEW -- */
189         start_postmaster(ctx, CLUSTER_NEW, false);
190
191         /*
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
194          * set.
195          */
196
197         set_frozenxids(ctx);
198
199         /*
200          * We have to create the databases first so we can create the toast table
201          * placeholder relfiles.
202          */
203         prep_status(ctx, "Creating databases in the new cluster");
204         exec_prog(ctx, true,
205                           SYSTEMQUOTE "\"%s/psql\" --port %d --username \"%s\" "
206                           "--set ON_ERROR_STOP=on -f \"%s/%s\" --dbname template1 >> \"%s\""
207                           SYSTEMQUOTE,
208                           ctx->new.bindir, ctx->new.port, ctx->user, ctx->cwd,
209                           GLOBALS_DUMP_FILE, ctx->logfile);
210         check_ok(ctx);
211
212         get_db_and_rel_infos(ctx, &ctx->new.dbarr, CLUSTER_NEW);
213
214         stop_postmaster(ctx, false, false);
215 }
216
217
218 static void
219 create_new_objects(migratorContext *ctx)
220 {
221         /* -- NEW -- */
222         start_postmaster(ctx, CLUSTER_NEW, false);
223
224         install_support_functions(ctx);
225
226         prep_status(ctx, "Restoring database schema to new cluster");
227         exec_prog(ctx, true,
228                           SYSTEMQUOTE "\"%s/psql\" --port %d --username \"%s\" "
229                           "--set ON_ERROR_STOP=on -f \"%s/%s\" --dbname template1 >> \"%s\""
230                           SYSTEMQUOTE,
231                           ctx->new.bindir, ctx->new.port,  ctx->user, ctx->cwd,
232                           DB_DUMP_FILE, ctx->logfile);
233         check_ok(ctx);
234
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);
238
239         uninstall_support_functions(ctx);
240
241         stop_postmaster(ctx, false, false);
242 }
243
244
245 static void
246 copy_clog_xlog_xid(migratorContext *ctx)
247 {
248         char            old_clog_path[MAXPGPATH];
249         char            new_clog_path[MAXPGPATH];
250
251         /* copy old commit logs to new data dir */
252         prep_status(ctx, "Deleting new commit clogs");
253
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);
258         check_ok(ctx);
259
260         prep_status(ctx, "Copying old commit clogs to new server");
261         /* libpgport's copydir() doesn't work in FRONTEND code */
262 #ifndef WIN32
263         exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
264                           "cp -Rf",
265 #else
266         /* flags: everything, no confirm, quiet, overwrite read-only */
267         exec_prog(ctx, true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
268                           "xcopy /e /y /q /r",
269 #endif
270                           old_clog_path, new_clog_path);
271         check_ok(ctx);
272
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);
277         check_ok(ctx);
278
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);
285         check_ok(ctx);
286 }
287
288
289 /*
290  *      set_frozenxids()
291  *
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.
297  */
298 static
299 void
300 set_frozenxids(migratorContext *ctx)
301 {
302         int                     dbnum;
303         PGconn     *conn, *conn_template1;
304         PGresult   *dbres;
305         int                     ntups;
306         int                     i_datname;
307         int                     i_datallowconn;
308
309         prep_status(ctx, "Setting frozenxid counters in new cluster");
310
311         conn_template1 = connectToServer(ctx, "template1", CLUSTER_NEW);
312
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));
318
319         /* get database names */
320         dbres = executeQueryOrDie(ctx, conn_template1,
321                                                           "SELECT       datname, datallowconn "
322                                                           "FROM pg_catalog.pg_database");
323
324         i_datname = PQfnumber(dbres, "datname");
325         i_datallowconn = PQfnumber(dbres, "datallowconn");
326
327         ntups = PQntuples(dbres);
328         for (dbnum = 0; dbnum < ntups; dbnum++)
329         {
330                 char *datname = PQgetvalue(dbres, dbnum, i_datname);
331                 char *datallowconn= PQgetvalue(dbres, dbnum, i_datallowconn);
332
333                 /*
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.
339                  */
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));
345
346                 conn = connectToServer(ctx, datname, CLUSTER_NEW);
347
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));
355                 PQfinish(conn);
356
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));
363         }
364
365         PQclear(dbres);
366
367         PQfinish(conn_template1);
368
369         check_ok(ctx);
370 }
371
372
373 static void
374 cleanup(migratorContext *ctx)
375 {
376         int                     tblnum;
377         char            filename[MAXPGPATH];
378
379         for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
380                 pg_free(ctx->tablespaces[tblnum]);
381         pg_free(ctx->tablespaces);
382
383         dbarr_free(&ctx->old.dbarr);
384         dbarr_free(&ctx->new.dbarr);
385         pg_free(ctx->logfile);
386         pg_free(ctx->user);
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);
397
398         if (ctx->log_fd != NULL)
399         {
400                 fclose(ctx->log_fd);
401                 ctx->log_fd = NULL;
402         }
403
404         if (ctx->debug_fd)
405                 fclose(ctx->debug_fd);
406
407         snprintf(filename, sizeof(filename), "%s/%s", ctx->cwd, ALL_DUMP_FILE);
408         unlink(filename);
409         snprintf(filename, sizeof(filename), "%s/%s", ctx->cwd, GLOBALS_DUMP_FILE);
410         unlink(filename);
411         snprintf(filename, sizeof(filename), "%s/%s", ctx->cwd, DB_DUMP_FILE);
412         unlink(filename);
413 }