]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
c140f6521e9527624f793bf0429712c3195605d0
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
1 /*
2  *      pg_upgrade.c
3  *
4  *      main source file
5  *
6  *      Copyright (c) 2010, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/pg_upgrade.c
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(void);
17 static void prepare_new_cluster(void);
18 static void prepare_new_databases(void);
19 static void create_new_objects(void);
20 static void copy_clog_xlog_xid(void);
21 static void set_frozenxids(void);
22 static void setup(char *argv0, bool live_check);
23 static void cleanup(void);
24
25 ClusterInfo old_cluster,
26                         new_cluster;
27 OSInfo          os_info;
28
29 int
30 main(int argc, char **argv)
31 {
32         char       *sequence_script_file_name = NULL;
33         char       *deletion_script_file_name = NULL;
34         bool            live_check = false;
35
36         parseCommandLine(argc, argv);
37
38         output_check_banner(&live_check);
39
40         setup(argv[0], live_check);
41
42         check_cluster_versions();
43         check_cluster_compatibility(live_check);
44
45         check_old_cluster(live_check, &sequence_script_file_name);
46
47
48         /* -- NEW -- */
49         start_postmaster(CLUSTER_NEW, false);
50
51         check_new_cluster();
52         report_clusters_compatible();
53
54         pg_log(PG_REPORT, "\nPerforming Migration\n");
55         pg_log(PG_REPORT, "--------------------\n");
56
57         disable_old_cluster();
58         prepare_new_cluster();
59
60         stop_postmaster(false, false);
61
62         /*
63          * Destructive Changes to New Cluster
64          */
65
66         copy_clog_xlog_xid();
67
68         /* New now using xids of the old system */
69
70         prepare_new_databases();
71
72         create_new_objects();
73
74         transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
75                                                  old_cluster.pgdata, new_cluster.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("Setting next oid for new cluster");
84         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
85                           DEVNULL SYSTEMQUOTE,
86                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
87         check_ok();
88
89         create_script_for_old_cluster_deletion(&deletion_script_file_name);
90
91         issue_warnings(sequence_script_file_name);
92
93         pg_log(PG_REPORT, "\nUpgrade complete\n");
94         pg_log(PG_REPORT, "----------------\n");
95
96         output_completion_banner(deletion_script_file_name);
97
98         pg_free(deletion_script_file_name);
99         pg_free(sequence_script_file_name);
100
101         cleanup();
102
103         return 0;
104 }
105
106
107 static void
108 setup(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();
117
118         verify_directories();
119
120         /* no postmasters should be running */
121         if (!live_check && is_server_running(old_cluster.pgdata))
122         {
123                 pg_log(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(new_cluster.pgdata))
129         {
130                 pg_log(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(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         os_info.exec_path = pg_strdup(exec_path);
142 }
143
144
145 static void
146 disable_old_cluster(void)
147 {
148         /* rename pg_control so old server cannot be accidentally started */
149         rename_old_pg_control();
150 }
151
152
153 static void
154 prepare_new_cluster(void)
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("Analyzing all rows in the new cluster");
162         exec_prog(true,
163                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
164                           "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
165                    new_cluster.bindir, new_cluster.port, os_info.user, log.filename);
166         check_ok();
167
168         /*
169          * We do freeze after analyze so pg_statistic is also frozen. template0 is
170          * not frozen here, but data rows were frozen by initdb, and we set its
171          * datfrozenxid and relfrozenxids later to match the new xid counter
172          * later.
173          */
174         prep_status("Freezing all rows on the new cluster");
175         exec_prog(true,
176                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
177                           "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
178                    new_cluster.bindir, new_cluster.port, os_info.user, log.filename);
179         check_ok();
180
181         get_pg_database_relfilenode(CLUSTER_NEW);
182 }
183
184
185 static void
186 prepare_new_databases(void)
187 {
188         /* -- NEW -- */
189         start_postmaster(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();
198
199         /*
200          * We have to create the databases first so we can create the toast table
201          * placeholder relfiles.
202          */
203         prep_status("Creating databases in the new cluster");
204         exec_prog(true,
205                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
206         /* --no-psqlrc prevents AUTOCOMMIT=off */
207                           "--no-psqlrc --port %d --username \"%s\" "
208                           "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
209                           new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
210                           GLOBALS_DUMP_FILE, log.filename);
211         check_ok();
212
213         get_db_and_rel_infos(&new_cluster.dbarr, CLUSTER_NEW);
214
215         stop_postmaster(false, false);
216 }
217
218
219 static void
220 create_new_objects(void)
221 {
222         /* -- NEW -- */
223         start_postmaster(CLUSTER_NEW, false);
224
225         install_support_functions();
226
227         prep_status("Restoring database schema to new cluster");
228         exec_prog(true,
229                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
230                           "--no-psqlrc --port %d --username \"%s\" "
231                           "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
232                           new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
233                           DB_DUMP_FILE, log.filename);
234         check_ok();
235
236         /* regenerate now that we have db schemas */
237         dbarr_free(&new_cluster.dbarr);
238         get_db_and_rel_infos(&new_cluster.dbarr, CLUSTER_NEW);
239
240         uninstall_support_functions();
241
242         stop_postmaster(false, false);
243 }
244
245
246 static void
247 copy_clog_xlog_xid(void)
248 {
249         char            old_clog_path[MAXPGPATH];
250         char            new_clog_path[MAXPGPATH];
251
252         /* copy old commit logs to new data dir */
253         prep_status("Deleting new commit clogs");
254
255         snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
256         snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
257         if (rmtree(new_clog_path, true) != true)
258                 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
259         check_ok();
260
261         prep_status("Copying old commit clogs to new server");
262         /* libpgport's copydir() doesn't work in FRONTEND code */
263 #ifndef WIN32
264         exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
265                           "cp -Rf",
266 #else
267         /* flags: everything, no confirm, quiet, overwrite read-only */
268         exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
269                           "xcopy /e /y /q /r",
270 #endif
271                           old_clog_path, new_clog_path);
272         check_ok();
273
274         /* set the next transaction id of the new cluster */
275         prep_status("Setting next transaction id for new cluster");
276         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
277                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
278         check_ok();
279
280         /* now reset the wal archives in the new cluster */
281         prep_status("Resetting WAL archives");
282         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
283                           new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
284                         old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
285                           new_cluster.pgdata, log.filename);
286         check_ok();
287 }
288
289
290 /*
291  *      set_frozenxids()
292  *
293  *      We have frozen all xids, so set relfrozenxid and datfrozenxid
294  *      to be the old cluster's xid counter, which we just set in the new
295  *      cluster.  User-table frozenxid values will be set by pg_dumpall
296  *      --binary-upgrade, but objects not set by the pg_dump must have
297  *      proper frozen counters.
298  */
299 static
300 void
301 set_frozenxids(void)
302 {
303         int                     dbnum;
304         PGconn     *conn,
305                            *conn_template1;
306         PGresult   *dbres;
307         int                     ntups;
308         int                     i_datname;
309         int                     i_datallowconn;
310
311         prep_status("Setting frozenxid counters in new cluster");
312
313         conn_template1 = connectToServer("template1", CLUSTER_NEW);
314
315         /* set pg_database.datfrozenxid */
316         PQclear(executeQueryOrDie(conn_template1,
317                                                           "UPDATE pg_catalog.pg_database "
318                                                           "SET  datfrozenxid = '%u'",
319                                                           old_cluster.controldata.chkpnt_nxtxid));
320
321         /* get database names */
322         dbres = executeQueryOrDie(conn_template1,
323                                                           "SELECT       datname, datallowconn "
324                                                           "FROM pg_catalog.pg_database");
325
326         i_datname = PQfnumber(dbres, "datname");
327         i_datallowconn = PQfnumber(dbres, "datallowconn");
328
329         ntups = PQntuples(dbres);
330         for (dbnum = 0; dbnum < ntups; dbnum++)
331         {
332                 char       *datname = PQgetvalue(dbres, dbnum, i_datname);
333                 char       *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
334
335                 /*
336                  * We must update databases where datallowconn = false, e.g.
337                  * template0, because autovacuum increments their datfrozenxids and
338                  * relfrozenxids even if autovacuum is turned off, and even though all
339                  * the data rows are already frozen  To enable this, we temporarily
340                  * change datallowconn.
341                  */
342                 if (strcmp(datallowconn, "f") == 0)
343                         PQclear(executeQueryOrDie(conn_template1,
344                                                                           "UPDATE pg_catalog.pg_database "
345                                                                           "SET  datallowconn = true "
346                                                                           "WHERE datname = '%s'", datname));
347
348                 conn = connectToServer(datname, CLUSTER_NEW);
349
350                 /* set pg_class.relfrozenxid */
351                 PQclear(executeQueryOrDie(conn,
352                                                                   "UPDATE       pg_catalog.pg_class "
353                                                                   "SET  relfrozenxid = '%u' "
354                 /* only heap and TOAST are vacuumed */
355                                                                   "WHERE        relkind IN ('r', 't')",
356                                                                   old_cluster.controldata.chkpnt_nxtxid));
357                 PQfinish(conn);
358
359                 /* Reset datallowconn flag */
360                 if (strcmp(datallowconn, "f") == 0)
361                         PQclear(executeQueryOrDie(conn_template1,
362                                                                           "UPDATE pg_catalog.pg_database "
363                                                                           "SET  datallowconn = false "
364                                                                           "WHERE datname = '%s'", datname));
365         }
366
367         PQclear(dbres);
368
369         PQfinish(conn_template1);
370
371         check_ok();
372 }
373
374
375 static void
376 cleanup(void)
377 {
378         int                     tblnum;
379         char            filename[MAXPGPATH];
380
381         for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
382                 pg_free(os_info.tablespaces[tblnum]);
383         pg_free(os_info.tablespaces);
384
385         dbarr_free(&old_cluster.dbarr);
386         dbarr_free(&new_cluster.dbarr);
387         pg_free(log.filename);
388         pg_free(os_info.user);
389         pg_free(old_cluster.major_version_str);
390         pg_free(new_cluster.major_version_str);
391         pg_free(old_cluster.controldata.lc_collate);
392         pg_free(new_cluster.controldata.lc_collate);
393         pg_free(old_cluster.controldata.lc_ctype);
394         pg_free(new_cluster.controldata.lc_ctype);
395         pg_free(old_cluster.controldata.encoding);
396         pg_free(new_cluster.controldata.encoding);
397         pg_free(old_cluster.tablespace_suffix);
398         pg_free(new_cluster.tablespace_suffix);
399
400         if (log.fd != NULL)
401         {
402                 fclose(log.fd);
403                 log.fd = NULL;
404         }
405
406         if (log.debug_fd)
407                 fclose(log.debug_fd);
408
409         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
410         unlink(filename);
411         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
412         unlink(filename);
413         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);
414         unlink(filename);
415 }