]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
Furter pg_upgrade optimizations to reduce function call argument count.
[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, new_cluster;
26 OSInfo          os_info;
27
28 int
29 main(int argc, char **argv)
30 {
31         char       *sequence_script_file_name = NULL;
32         char       *deletion_script_file_name = NULL;
33         bool            live_check = false;
34
35         parseCommandLine(argc, argv);
36
37         output_check_banner(&live_check);
38
39         setup(argv[0], live_check);
40
41         check_cluster_versions();
42         check_cluster_compatibility(live_check);
43
44         check_old_cluster(live_check, &sequence_script_file_name);
45
46
47         /* -- NEW -- */
48         start_postmaster(&new_cluster, false);
49
50         check_new_cluster();
51         report_clusters_compatible();
52
53         pg_log(PG_REPORT, "\nPerforming Upgrade\n");
54         pg_log(PG_REPORT, "------------------\n");
55
56         disable_old_cluster();
57         prepare_new_cluster();
58
59         stop_postmaster(false, false);
60
61         /*
62          * Destructive Changes to New Cluster
63          */
64
65         copy_clog_xlog_xid();
66
67         /* New now using xids of the old system */
68
69         prepare_new_databases();
70
71         create_new_objects();
72
73         transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
74                                                  old_cluster.pgdata, new_cluster.pgdata);
75
76         /*
77          * Assuming OIDs are only used in system tables, there is no need to
78          * restore the OID counter because we have not transferred any OIDs from
79          * the old system, but we do it anyway just in case.  We do it late here
80          * because there is no need to have the schema load use new oids.
81          */
82         prep_status("Setting next oid for new cluster");
83         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
84                           DEVNULL SYSTEMQUOTE,
85                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
86         check_ok();
87
88         create_script_for_old_cluster_deletion(&deletion_script_file_name);
89
90         issue_warnings(sequence_script_file_name);
91
92         pg_log(PG_REPORT, "\nUpgrade complete\n");
93         pg_log(PG_REPORT, "----------------\n");
94
95         output_completion_banner(deletion_script_file_name);
96
97         pg_free(deletion_script_file_name);
98         pg_free(sequence_script_file_name);
99
100         cleanup();
101
102         return 0;
103 }
104
105
106 static void
107 setup(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();
116
117         verify_directories();
118
119         /* no postmasters should be running */
120         if (!live_check && is_server_running(old_cluster.pgdata))
121         {
122                 pg_log(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(new_cluster.pgdata))
128         {
129                 pg_log(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(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         os_info.exec_path = pg_strdup(exec_path);
141 }
142
143
144 static void
145 disable_old_cluster(void)
146 {
147         /* rename pg_control so old server cannot be accidentally started */
148         rename_old_pg_control();
149 }
150
151
152 static void
153 prepare_new_cluster(void)
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("Analyzing all rows in the new cluster");
161         exec_prog(true,
162                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
163                           "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
164                    new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
165         check_ok();
166
167         /*
168          * We do freeze after analyze so pg_statistic is also frozen. template0 is
169          * not frozen here, but data rows were frozen by initdb, and we set its
170          * datfrozenxid and relfrozenxids later to match the new xid counter
171          * later.
172          */
173         prep_status("Freezing all rows on the new cluster");
174         exec_prog(true,
175                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
176                           "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
177                    new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
178         check_ok();
179
180         get_pg_database_relfilenode(&new_cluster);
181 }
182
183
184 static void
185 prepare_new_databases(void)
186 {
187         /* -- NEW -- */
188         start_postmaster(&new_cluster, false);
189
190         /*
191          * We set autovacuum_freeze_max_age to its maximum value so autovacuum
192          * does not launch here and delete clog files, before the frozen xids are
193          * set.
194          */
195
196         set_frozenxids();
197
198         /*
199          * We have to create the databases first so we can create the toast table
200          * placeholder relfiles.
201          */
202         prep_status("Creating databases in the new cluster");
203         exec_prog(true,
204                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
205         /* --no-psqlrc prevents AUTOCOMMIT=off */
206                           "--no-psqlrc --port %d --username \"%s\" "
207                           "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
208                           new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
209                           GLOBALS_DUMP_FILE, log_opts.filename);
210         check_ok();
211
212         get_db_and_rel_infos(&new_cluster);
213
214         stop_postmaster(false, false);
215 }
216
217
218 static void
219 create_new_objects(void)
220 {
221         /* -- NEW -- */
222         start_postmaster(&new_cluster, false);
223
224         install_support_functions();
225
226         prep_status("Restoring database schema to new cluster");
227         exec_prog(true,
228                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
229                           "--no-psqlrc --port %d --username \"%s\" "
230                           "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
231                           new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
232                           DB_DUMP_FILE, log_opts.filename);
233         check_ok();
234
235         /* regenerate now that we have db schemas */
236         dbarr_free(&new_cluster.dbarr);
237         get_db_and_rel_infos(&new_cluster);
238
239         uninstall_support_functions();
240
241         stop_postmaster(false, false);
242 }
243
244
245 static void
246 copy_clog_xlog_xid(void)
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("Deleting new commit clogs");
253
254         snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
255         snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
256         if (!rmtree(new_clog_path, true))
257                 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
258         check_ok();
259
260         prep_status("Copying old commit clogs to new server");
261 #ifndef WIN32
262         exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
263                           "cp -Rf",
264 #else
265         /* flags: everything, no confirm, quiet, overwrite read-only */
266         exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
267                           "xcopy /e /y /q /r",
268 #endif
269                           old_clog_path, new_clog_path);
270         check_ok();
271
272         /* set the next transaction id of the new cluster */
273         prep_status("Setting next transaction id for new cluster");
274         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
275                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
276         check_ok();
277
278         /* now reset the wal archives in the new cluster */
279         prep_status("Resetting WAL archives");
280         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
281                           new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
282                         old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
283                           new_cluster.pgdata, log_opts.filename);
284         check_ok();
285 }
286
287
288 /*
289  *      set_frozenxids()
290  *
291  *      We have frozen all xids, so set relfrozenxid and datfrozenxid
292  *      to be the old cluster's xid counter, which we just set in the new
293  *      cluster.  User-table frozenxid values will be set by pg_dumpall
294  *      --binary-upgrade, but objects not set by the pg_dump must have
295  *      proper frozen counters.
296  */
297 static
298 void
299 set_frozenxids(void)
300 {
301         int                     dbnum;
302         PGconn     *conn,
303                            *conn_template1;
304         PGresult   *dbres;
305         int                     ntups;
306         int                     i_datname;
307         int                     i_datallowconn;
308
309         prep_status("Setting frozenxid counters in new cluster");
310
311         conn_template1 = connectToServer(&new_cluster, "template1");
312
313         /* set pg_database.datfrozenxid */
314         PQclear(executeQueryOrDie(conn_template1,
315                                                           "UPDATE pg_catalog.pg_database "
316                                                           "SET  datfrozenxid = '%u'",
317                                                           old_cluster.controldata.chkpnt_nxtxid));
318
319         /* get database names */
320         dbres = executeQueryOrDie(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 all
337                  * the data rows are already frozen  To enable this, we temporarily
338                  * change datallowconn.
339                  */
340                 if (strcmp(datallowconn, "f") == 0)
341                         PQclear(executeQueryOrDie(conn_template1,
342                                                                           "UPDATE pg_catalog.pg_database "
343                                                                           "SET  datallowconn = true "
344                                                                           "WHERE datname = '%s'", datname));
345
346                 conn = connectToServer(&new_cluster, datname);
347
348                 /* set pg_class.relfrozenxid */
349                 PQclear(executeQueryOrDie(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                                                                   old_cluster.controldata.chkpnt_nxtxid));
355                 PQfinish(conn);
356
357                 /* Reset datallowconn flag */
358                 if (strcmp(datallowconn, "f") == 0)
359                         PQclear(executeQueryOrDie(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();
370 }
371
372
373 static void
374 cleanup(void)
375 {
376         int                     tblnum;
377         char            filename[MAXPGPATH];
378
379         for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
380                 pg_free(os_info.tablespaces[tblnum]);
381         pg_free(os_info.tablespaces);
382
383         dbarr_free(&old_cluster.dbarr);
384         dbarr_free(&new_cluster.dbarr);
385         pg_free(log_opts.filename);
386         pg_free(os_info.user);
387         pg_free(old_cluster.controldata.lc_collate);
388         pg_free(new_cluster.controldata.lc_collate);
389         pg_free(old_cluster.controldata.lc_ctype);
390         pg_free(new_cluster.controldata.lc_ctype);
391         pg_free(old_cluster.controldata.encoding);
392         pg_free(new_cluster.controldata.encoding);
393         pg_free(old_cluster.tablespace_suffix);
394         pg_free(new_cluster.tablespace_suffix);
395
396         if (log_opts.fd != NULL)
397         {
398                 fclose(log_opts.fd);
399                 log_opts.fd = NULL;
400         }
401
402         if (log_opts.debug_fd)
403                 fclose(log_opts.debug_fd);
404
405         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
406         unlink(filename);
407         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
408         unlink(filename);
409         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);
410         unlink(filename);
411 }