]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
Improve pg_upgrade's status display
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
1 /*
2  *      pg_upgrade.c
3  *
4  *      main source file
5  *
6  *      Copyright (c) 2010-2012, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/pg_upgrade.c
8  */
9
10 /*
11  *      To simplify the upgrade process, we force certain system values to be
12  *      identical between old and new clusters:
13  *
14  *      We control all assignments of pg_class.oid (and relfilenode) so toast
15  *      oids are the same between old and new clusters.  This is important
16  *      because toast oids are stored as toast pointers in user tables.
17  *
18  *      FYI, while pg_class.oid and pg_class.relfilenode are initially the same
19  *      in a cluster, but they can diverge due to CLUSTER, REINDEX, or VACUUM
20  *      FULL.  The new cluster will have matching pg_class.oid and
21  *      pg_class.relfilenode values and be based on the old oid value.  This can
22  *      cause the old and new pg_class.relfilenode values to differ.  In summary,
23  *      old and new pg_class.oid and new pg_class.relfilenode will have the
24  *      same value, and old pg_class.relfilenode might differ.
25  *
26  *      We control all assignments of pg_type.oid because these oids are stored
27  *      in user composite type values.
28  *
29  *      We control all assignments of pg_enum.oid because these oids are stored
30  *      in user tables as enum values.
31  *
32  *      We control all assignments of pg_authid.oid because these oids are stored
33  *      in pg_largeobject_metadata.
34  */
35
36
37
38 #include "postgres.h"
39
40 #include "pg_upgrade.h"
41
42 #ifdef HAVE_LANGINFO_H
43 #include <langinfo.h>
44 #endif
45
46 static void prepare_new_cluster(void);
47 static void prepare_new_databases(void);
48 static void create_new_objects(void);
49 static void copy_clog_xlog_xid(void);
50 static void set_frozenxids(void);
51 static void setup(char *argv0, bool live_check);
52 static void cleanup(void);
53
54 ClusterInfo old_cluster,
55                         new_cluster;
56 OSInfo          os_info;
57
58 char       *output_files[] = {
59         SERVER_LOG_FILE,
60 #ifdef WIN32
61         /* unique file for pg_ctl start */
62         SERVER_START_LOG_FILE,
63 #endif
64         RESTORE_LOG_FILE,
65         UTILITY_LOG_FILE,
66         INTERNAL_LOG_FILE,
67         NULL
68 };
69
70
71 int
72 main(int argc, char **argv)
73 {
74         char       *sequence_script_file_name = NULL;
75         char       *analyze_script_file_name = NULL;
76         char       *deletion_script_file_name = NULL;
77         bool            live_check = false;
78
79         parseCommandLine(argc, argv);
80
81         adjust_data_dir(&old_cluster);
82         adjust_data_dir(&new_cluster);
83
84         output_check_banner(&live_check);
85
86         setup(argv[0], live_check);
87
88         check_cluster_versions();
89
90         get_sock_dir(&old_cluster, live_check);
91         get_sock_dir(&new_cluster, false);
92
93         check_cluster_compatibility(live_check);
94
95         check_and_dump_old_cluster(live_check, &sequence_script_file_name);
96
97
98         /* -- NEW -- */
99         start_postmaster(&new_cluster);
100
101         check_new_cluster();
102         report_clusters_compatible();
103
104         pg_log(PG_REPORT, "\nPerforming Upgrade\n");
105         pg_log(PG_REPORT, "------------------\n");
106
107         prepare_new_cluster();
108
109         stop_postmaster(false);
110
111         /*
112          * Destructive Changes to New Cluster
113          */
114
115         copy_clog_xlog_xid();
116
117         /* New now using xids of the old system */
118
119         /* -- NEW -- */
120         start_postmaster(&new_cluster);
121
122         prepare_new_databases();
123
124         create_new_objects();
125
126         stop_postmaster(false);
127
128         /*
129          * Most failures happen in create_new_objects(), which has completed at
130          * this point.  We do this here because it is just before linking, which
131          * will link the old and new cluster data files, preventing the old
132          * cluster from being safely started once the new cluster is started.
133          */
134         if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
135                 disable_old_cluster();
136
137         transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
138                                                  old_cluster.pgdata, new_cluster.pgdata);
139
140         /*
141          * Assuming OIDs are only used in system tables, there is no need to
142          * restore the OID counter because we have not transferred any OIDs from
143          * the old system, but we do it anyway just in case.  We do it late here
144          * because there is no need to have the schema load use new oids.
145          */
146         prep_status("Setting next OID for new cluster");
147         exec_prog(UTILITY_LOG_FILE, NULL, true,
148                           "\"%s/pg_resetxlog\" -o %u \"%s\"",
149                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid,
150                           new_cluster.pgdata);
151         check_ok();
152
153         prep_status("Sync data directory to disk");
154         exec_prog(UTILITY_LOG_FILE, NULL, true,
155                           "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
156                           new_cluster.pgdata);
157         check_ok();
158
159         create_script_for_cluster_analyze(&analyze_script_file_name);
160         create_script_for_old_cluster_deletion(&deletion_script_file_name);
161
162         issue_warnings(sequence_script_file_name);
163
164         pg_log(PG_REPORT, "\nUpgrade Complete\n");
165         pg_log(PG_REPORT, "----------------\n");
166
167         output_completion_banner(analyze_script_file_name,
168                                                          deletion_script_file_name);
169
170         pg_free(analyze_script_file_name);
171         pg_free(deletion_script_file_name);
172         pg_free(sequence_script_file_name);
173
174         cleanup();
175
176         return 0;
177 }
178
179
180 static void
181 setup(char *argv0, bool live_check)
182 {
183         char            exec_path[MAXPGPATH];   /* full path to my executable */
184
185         /*
186          * make sure the user has a clean environment, otherwise, we may confuse
187          * libpq when we connect to one (or both) of the servers.
188          */
189         check_pghost_envvar();
190
191         verify_directories();
192
193         /* no postmasters should be running */
194         if (!live_check && is_server_running(old_cluster.pgdata))
195                 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
196                            "Please shutdown that postmaster and try again.\n");
197
198         /* same goes for the new postmaster */
199         if (is_server_running(new_cluster.pgdata))
200                 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
201                            "Please shutdown that postmaster and try again.\n");
202
203         /* get path to pg_upgrade executable */
204         if (find_my_exec(argv0, exec_path) < 0)
205                 pg_log(PG_FATAL, "Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
206
207         /* Trim off program name and keep just path */
208         *last_dir_separator(exec_path) = '\0';
209         canonicalize_path(exec_path);
210         os_info.exec_path = pg_strdup(exec_path);
211 }
212
213
214 static void
215 prepare_new_cluster(void)
216 {
217         /*
218          * It would make more sense to freeze after loading the schema, but that
219          * would cause us to lose the frozenids restored by the load. We use
220          * --analyze so autovacuum doesn't update statistics later
221          */
222         prep_status("Analyzing all rows in the new cluster");
223         exec_prog(UTILITY_LOG_FILE, NULL, true,
224                           "\"%s/vacuumdb\" %s --all --analyze %s",
225                           new_cluster.bindir, cluster_conn_opts(&new_cluster),
226                           log_opts.verbose ? "--verbose" : "");
227         check_ok();
228
229         /*
230          * We do freeze after analyze so pg_statistic is also frozen. template0 is
231          * not frozen here, but data rows were frozen by initdb, and we set its
232          * datfrozenxid and relfrozenxids later to match the new xid counter
233          * later.
234          */
235         prep_status("Freezing all rows on the new cluster");
236         exec_prog(UTILITY_LOG_FILE, NULL, true,
237                           "\"%s/vacuumdb\" %s --all --freeze %s",
238                           new_cluster.bindir, cluster_conn_opts(&new_cluster),
239                           log_opts.verbose ? "--verbose" : "");
240         check_ok();
241
242         get_pg_database_relfilenode(&new_cluster);
243 }
244
245
246 static void
247 prepare_new_databases(void)
248 {
249         /*
250          * We set autovacuum_freeze_max_age to its maximum value so autovacuum
251          * does not launch here and delete clog files, before the frozen xids are
252          * set.
253          */
254
255         set_frozenxids();
256
257         prep_status("Restoring global objects in the new cluster");
258
259         /*
260          * Install support functions in the global-object restore database to
261          * preserve pg_authid.oid.      pg_dumpall uses 'template0' as its template
262          * database so objects we add into 'template1' are not propogated.      They
263          * are removed on pg_upgrade exit.
264          */
265         install_support_functions_in_new_db("template1");
266
267         /*
268          * We have to create the databases first so we can install support
269          * functions in all the other databases.  Ideally we could create the
270          * support functions in template1 but pg_dumpall creates database using
271          * the template0 template.
272          */
273         exec_prog(RESTORE_LOG_FILE, NULL, true,
274                           "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
275                           new_cluster.bindir, cluster_conn_opts(&new_cluster),
276                           GLOBALS_DUMP_FILE);
277         check_ok();
278
279         /* we load this to get a current list of databases */
280         get_db_and_rel_infos(&new_cluster);
281 }
282
283
284 static void
285 create_new_objects(void)
286 {
287         int                     dbnum;
288
289         prep_status("Adding support functions to new cluster");
290
291         /*
292          *      Technically, we only need to install these support functions in new
293          *      databases that also exist in the old cluster, but for completeness
294          *      we process all new databases.
295          */
296         for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
297         {
298                 DbInfo     *new_db = &new_cluster.dbarr.dbs[dbnum];
299
300                 /* skip db we already installed */
301                 if (strcmp(new_db->db_name, "template1") != 0)
302                         install_support_functions_in_new_db(new_db->db_name);
303         }
304         check_ok();
305
306         prep_status("Restoring database schemas in the new cluster\n");
307
308         for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
309         {
310                 char file_name[MAXPGPATH];
311                 DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
312
313                 pg_log(PG_STATUS, "%s", old_db->db_name);
314                 snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
315
316                 /*
317                  *      Using pg_restore --single-transaction is faster than other
318                  *      methods, like --jobs.  pg_dump only produces its output at the
319                  *      end, so there is little parallelism using the pipe.
320                  */
321                 exec_prog(RESTORE_LOG_FILE, NULL, true,
322                                   "\"%s/pg_restore\" %s --exit-on-error --single-transaction --verbose --dbname \"%s\" \"%s\"",
323                                   new_cluster.bindir, cluster_conn_opts(&new_cluster),
324                                   old_db->db_name, file_name);
325         }
326         end_progress_output();
327         check_ok();
328
329         /* regenerate now that we have objects in the databases */
330         get_db_and_rel_infos(&new_cluster);
331
332         uninstall_support_functions_from_new_cluster();
333 }
334
335 /*
336  * Delete the given subdirectory contents from the new cluster, and copy the
337  * files from the old cluster into it.
338  */
339 static void
340 copy_subdir_files(char *subdir)
341 {
342         char            old_path[MAXPGPATH];
343         char            new_path[MAXPGPATH];
344
345         prep_status("Deleting files from new %s", subdir);
346
347         snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
348         snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
349         if (!rmtree(new_path, true))
350                 pg_log(PG_FATAL, "could not delete directory \"%s\"\n", new_path);
351         check_ok();
352
353         prep_status("Copying old %s to new server", subdir);
354
355         exec_prog(UTILITY_LOG_FILE, NULL, true,
356 #ifndef WIN32
357                           "cp -Rf \"%s\" \"%s\"",
358 #else
359         /* flags: everything, no confirm, quiet, overwrite read-only */
360                           "xcopy /e /y /q /r \"%s\" \"%s\\\"",
361 #endif
362                           old_path, new_path);
363
364         check_ok();
365 }
366
367 static void
368 copy_clog_xlog_xid(void)
369 {
370         /* copy old commit logs to new data dir */
371         copy_subdir_files("pg_clog");
372
373         /* set the next transaction id of the new cluster */
374         prep_status("Setting next transaction ID for new cluster");
375         exec_prog(UTILITY_LOG_FILE, NULL, true,
376                           "\"%s/pg_resetxlog\" -f -x %u \"%s\"",
377                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid,
378                           new_cluster.pgdata);
379         check_ok();
380
381         /* now reset the wal archives in the new cluster */
382         prep_status("Resetting WAL archives");
383         exec_prog(UTILITY_LOG_FILE, NULL, true,
384                           "\"%s/pg_resetxlog\" -l %s \"%s\"", new_cluster.bindir,
385                           old_cluster.controldata.nextxlogfile,
386                           new_cluster.pgdata);
387         check_ok();
388 }
389
390
391 /*
392  *      set_frozenxids()
393  *
394  *      We have frozen all xids, so set relfrozenxid and datfrozenxid
395  *      to be the old cluster's xid counter, which we just set in the new
396  *      cluster.  User-table frozenxid values will be set by pg_dumpall
397  *      --binary-upgrade, but objects not set by the pg_dump must have
398  *      proper frozen counters.
399  */
400 static
401 void
402 set_frozenxids(void)
403 {
404         int                     dbnum;
405         PGconn     *conn,
406                            *conn_template1;
407         PGresult   *dbres;
408         int                     ntups;
409         int                     i_datname;
410         int                     i_datallowconn;
411
412         prep_status("Setting frozenxid counters in new cluster");
413
414         conn_template1 = connectToServer(&new_cluster, "template1");
415
416         /* set pg_database.datfrozenxid */
417         PQclear(executeQueryOrDie(conn_template1,
418                                                           "UPDATE pg_catalog.pg_database "
419                                                           "SET  datfrozenxid = '%u'",
420                                                           old_cluster.controldata.chkpnt_nxtxid));
421
422         /* get database names */
423         dbres = executeQueryOrDie(conn_template1,
424                                                           "SELECT       datname, datallowconn "
425                                                           "FROM pg_catalog.pg_database");
426
427         i_datname = PQfnumber(dbres, "datname");
428         i_datallowconn = PQfnumber(dbres, "datallowconn");
429
430         ntups = PQntuples(dbres);
431         for (dbnum = 0; dbnum < ntups; dbnum++)
432         {
433                 char       *datname = PQgetvalue(dbres, dbnum, i_datname);
434                 char       *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
435
436                 /*
437                  * We must update databases where datallowconn = false, e.g.
438                  * template0, because autovacuum increments their datfrozenxids and
439                  * relfrozenxids even if autovacuum is turned off, and even though all
440                  * the data rows are already frozen  To enable this, we temporarily
441                  * change datallowconn.
442                  */
443                 if (strcmp(datallowconn, "f") == 0)
444                         PQclear(executeQueryOrDie(conn_template1,
445                                                                           "UPDATE pg_catalog.pg_database "
446                                                                           "SET  datallowconn = true "
447                                                                           "WHERE datname = '%s'", datname));
448
449                 conn = connectToServer(&new_cluster, datname);
450
451                 /* set pg_class.relfrozenxid */
452                 PQclear(executeQueryOrDie(conn,
453                                                                   "UPDATE       pg_catalog.pg_class "
454                                                                   "SET  relfrozenxid = '%u' "
455                 /* only heap and TOAST are vacuumed */
456                                                                   "WHERE        relkind IN ('r', 't')",
457                                                                   old_cluster.controldata.chkpnt_nxtxid));
458                 PQfinish(conn);
459
460                 /* Reset datallowconn flag */
461                 if (strcmp(datallowconn, "f") == 0)
462                         PQclear(executeQueryOrDie(conn_template1,
463                                                                           "UPDATE pg_catalog.pg_database "
464                                                                           "SET  datallowconn = false "
465                                                                           "WHERE datname = '%s'", datname));
466         }
467
468         PQclear(dbres);
469
470         PQfinish(conn_template1);
471
472         check_ok();
473 }
474
475
476 static void
477 cleanup(void)
478 {
479
480         fclose(log_opts.internal);
481
482         /* Remove dump and log files? */
483         if (!log_opts.retain)
484         {
485                 int                     dbnum;
486                 char      **filename;
487
488                 for (filename = output_files; *filename != NULL; filename++)
489                         unlink(*filename);
490
491                 /* remove dump files */
492                 unlink(GLOBALS_DUMP_FILE);
493
494                 if (old_cluster.dbarr.dbs)
495                         for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
496                         {
497                                 char file_name[MAXPGPATH];
498                                 DbInfo     *old_db = &old_cluster.dbarr.dbs[dbnum];
499
500                                 snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
501                                 unlink(file_name);
502                         }
503         }
504 }