]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/pg_upgrade.c
Improve C comments about backend variables set by pg_upgrade_support
[postgresql] / contrib / pg_upgrade / pg_upgrade.c
1 /*
2  *      pg_upgrade.c
3  *
4  *      main source file
5  *
6  *      Copyright (c) 2010-2011, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/pg_upgrade.c
8  */
9
10 /*
11  *      To simplify the upgrade process, we force certain system items to be
12  *      consistent between old and new clusters:
13  *
14  *      We control all assignments of pg_class.relfilenode so we can keep the
15  *      same relfilenodes for old and new files.  The only exception is
16  *      pg_largeobject, pg_largeobject_metadata, and its indexes, which can
17  *      change due to a cluster, reindex, or vacuum full.  (We don't create
18  *      those so have no control over their oid/relfilenode values.)
19  *
20  *      While pg_class.oid and pg_class.relfilenode are intially the same, they
21  *      can diverge due to cluster, reindex, or vacuum full.  The new cluster
22  *      will again have matching pg_class.relfilenode and pg_class.oid values,
23  *      but based on the new relfilenode value, so the old/new oids might
24  *      differ.
25  *
26  *      We control all assignments of pg_type.oid because these are stored
27  *      in composite types.
28  */
29
30
31  
32 #include "pg_upgrade.h"
33
34 #ifdef HAVE_LANGINFO_H
35 #include <langinfo.h>
36 #endif
37
38 static void disable_old_cluster(void);
39 static void prepare_new_cluster(void);
40 static void prepare_new_databases(void);
41 static void create_new_objects(void);
42 static void copy_clog_xlog_xid(void);
43 static void set_frozenxids(void);
44 static void setup(char *argv0, bool live_check);
45 static void cleanup(void);
46
47 ClusterInfo old_cluster, new_cluster;
48 OSInfo          os_info;
49
50 int
51 main(int argc, char **argv)
52 {
53         char       *sequence_script_file_name = NULL;
54         char       *deletion_script_file_name = NULL;
55         bool            live_check = false;
56
57         parseCommandLine(argc, argv);
58
59         output_check_banner(&live_check);
60
61         setup(argv[0], live_check);
62
63         check_cluster_versions();
64         check_cluster_compatibility(live_check);
65
66         check_old_cluster(live_check, &sequence_script_file_name);
67
68
69         /* -- NEW -- */
70         start_postmaster(&new_cluster, false);
71
72         check_new_cluster();
73         report_clusters_compatible();
74
75         pg_log(PG_REPORT, "\nPerforming Upgrade\n");
76         pg_log(PG_REPORT, "------------------\n");
77
78         disable_old_cluster();
79         prepare_new_cluster();
80
81         stop_postmaster(false, false);
82
83         /*
84          * Destructive Changes to New Cluster
85          */
86
87         copy_clog_xlog_xid();
88
89         /* New now using xids of the old system */
90
91         prepare_new_databases();
92
93         create_new_objects();
94
95         transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
96                                                  old_cluster.pgdata, new_cluster.pgdata);
97
98         /*
99          * Assuming OIDs are only used in system tables, there is no need to
100          * restore the OID counter because we have not transferred any OIDs from
101          * the old system, but we do it anyway just in case.  We do it late here
102          * because there is no need to have the schema load use new oids.
103          */
104         prep_status("Setting next oid for new cluster");
105         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -o %u \"%s\" > "
106                           DEVNULL SYSTEMQUOTE,
107                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid, new_cluster.pgdata);
108         check_ok();
109
110         create_script_for_old_cluster_deletion(&deletion_script_file_name);
111
112         issue_warnings(sequence_script_file_name);
113
114         pg_log(PG_REPORT, "\nUpgrade complete\n");
115         pg_log(PG_REPORT, "----------------\n");
116
117         output_completion_banner(deletion_script_file_name);
118
119         pg_free(deletion_script_file_name);
120         pg_free(sequence_script_file_name);
121
122         cleanup();
123
124         return 0;
125 }
126
127
128 static void
129 setup(char *argv0, bool live_check)
130 {
131         char            exec_path[MAXPGPATH];   /* full path to my executable */
132
133         /*
134          * make sure the user has a clean environment, otherwise, we may confuse
135          * libpq when we connect to one (or both) of the servers.
136          */
137         check_for_libpq_envvars();
138
139         verify_directories();
140
141         /* no postmasters should be running */
142         if (!live_check && is_server_running(old_cluster.pgdata))
143         {
144                 pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
145                            "Please shutdown that postmaster and try again.\n");
146         }
147
148         /* same goes for the new postmaster */
149         if (is_server_running(new_cluster.pgdata))
150         {
151                 pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
152                            "Please shutdown that postmaster and try again.\n");
153         }
154
155         /* get path to pg_upgrade executable */
156         if (find_my_exec(argv0, exec_path) < 0)
157                 pg_log(PG_FATAL, "Could not get pathname to pg_upgrade: %s\n", getErrorText(errno));
158
159         /* Trim off program name and keep just path */
160         *last_dir_separator(exec_path) = '\0';
161         canonicalize_path(exec_path);
162         os_info.exec_path = pg_strdup(exec_path);
163 }
164
165
166 static void
167 disable_old_cluster(void)
168 {
169         /* rename pg_control so old server cannot be accidentally started */
170         rename_old_pg_control();
171 }
172
173
174 static void
175 prepare_new_cluster(void)
176 {
177         /*
178          * It would make more sense to freeze after loading the schema, but that
179          * would cause us to lose the frozenids restored by the load. We use
180          * --analyze so autovacuum doesn't update statistics later
181          */
182         prep_status("Analyzing all rows in the new cluster");
183         exec_prog(true,
184                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
185                           "--all --analyze >> %s 2>&1" SYSTEMQUOTE,
186                    new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
187         check_ok();
188
189         /*
190          * We do freeze after analyze so pg_statistic is also frozen. template0 is
191          * not frozen here, but data rows were frozen by initdb, and we set its
192          * datfrozenxid and relfrozenxids later to match the new xid counter
193          * later.
194          */
195         prep_status("Freezing all rows on the new cluster");
196         exec_prog(true,
197                           SYSTEMQUOTE "\"%s/vacuumdb\" --port %d --username \"%s\" "
198                           "--all --freeze >> %s 2>&1" SYSTEMQUOTE,
199                    new_cluster.bindir, new_cluster.port, os_info.user, log_opts.filename);
200         check_ok();
201
202         get_pg_database_relfilenode(&new_cluster);
203 }
204
205
206 static void
207 prepare_new_databases(void)
208 {
209         /* -- NEW -- */
210         start_postmaster(&new_cluster, false);
211
212         /*
213          * We set autovacuum_freeze_max_age to its maximum value so autovacuum
214          * does not launch here and delete clog files, before the frozen xids are
215          * set.
216          */
217
218         set_frozenxids();
219
220         /*
221          * We have to create the databases first so we can create the toast table
222          * placeholder relfiles.
223          */
224         prep_status("Creating databases in the new cluster");
225         exec_prog(true,
226                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
227         /* --no-psqlrc prevents AUTOCOMMIT=off */
228                           "--no-psqlrc --port %d --username \"%s\" "
229                           "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
230                           new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
231                           GLOBALS_DUMP_FILE, log_opts.filename);
232         check_ok();
233
234         get_db_and_rel_infos(&new_cluster);
235
236         stop_postmaster(false, false);
237 }
238
239
240 static void
241 create_new_objects(void)
242 {
243         /* -- NEW -- */
244         start_postmaster(&new_cluster, false);
245
246         install_support_functions();
247
248         prep_status("Restoring database schema to new cluster");
249         exec_prog(true,
250                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
251                           "--no-psqlrc --port %d --username \"%s\" "
252                           "-f \"%s/%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
253                           new_cluster.bindir, new_cluster.port, os_info.user, os_info.cwd,
254                           DB_DUMP_FILE, log_opts.filename);
255         check_ok();
256
257         /* regenerate now that we have db schemas */
258         dbarr_free(&new_cluster.dbarr);
259         get_db_and_rel_infos(&new_cluster);
260
261         uninstall_support_functions();
262
263         stop_postmaster(false, false);
264 }
265
266
267 static void
268 copy_clog_xlog_xid(void)
269 {
270         char            old_clog_path[MAXPGPATH];
271         char            new_clog_path[MAXPGPATH];
272
273         /* copy old commit logs to new data dir */
274         prep_status("Deleting new commit clogs");
275
276         snprintf(old_clog_path, sizeof(old_clog_path), "%s/pg_clog", old_cluster.pgdata);
277         snprintf(new_clog_path, sizeof(new_clog_path), "%s/pg_clog", new_cluster.pgdata);
278         if (!rmtree(new_clog_path, true))
279                 pg_log(PG_FATAL, "Unable to delete directory %s\n", new_clog_path);
280         check_ok();
281
282         prep_status("Copying old commit clogs to new server");
283 #ifndef WIN32
284         exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\"" SYSTEMQUOTE,
285                           "cp -Rf",
286 #else
287         /* flags: everything, no confirm, quiet, overwrite read-only */
288         exec_prog(true, SYSTEMQUOTE "%s \"%s\" \"%s\\\"" SYSTEMQUOTE,
289                           "xcopy /e /y /q /r",
290 #endif
291                           old_clog_path, new_clog_path);
292         check_ok();
293
294         /* set the next transaction id of the new cluster */
295         prep_status("Setting next transaction id for new cluster");
296         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -f -x %u \"%s\" > " DEVNULL SYSTEMQUOTE,
297                           new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid, new_cluster.pgdata);
298         check_ok();
299
300         /* now reset the wal archives in the new cluster */
301         prep_status("Resetting WAL archives");
302         exec_prog(true, SYSTEMQUOTE "\"%s/pg_resetxlog\" -l %u,%u,%u \"%s\" >> \"%s\" 2>&1" SYSTEMQUOTE,
303                           new_cluster.bindir, old_cluster.controldata.chkpnt_tli,
304                         old_cluster.controldata.logid, old_cluster.controldata.nxtlogseg,
305                           new_cluster.pgdata, log_opts.filename);
306         check_ok();
307 }
308
309
310 /*
311  *      set_frozenxids()
312  *
313  *      We have frozen all xids, so set relfrozenxid and datfrozenxid
314  *      to be the old cluster's xid counter, which we just set in the new
315  *      cluster.  User-table frozenxid values will be set by pg_dumpall
316  *      --binary-upgrade, but objects not set by the pg_dump must have
317  *      proper frozen counters.
318  */
319 static
320 void
321 set_frozenxids(void)
322 {
323         int                     dbnum;
324         PGconn     *conn,
325                            *conn_template1;
326         PGresult   *dbres;
327         int                     ntups;
328         int                     i_datname;
329         int                     i_datallowconn;
330
331         prep_status("Setting frozenxid counters in new cluster");
332
333         conn_template1 = connectToServer(&new_cluster, "template1");
334
335         /* set pg_database.datfrozenxid */
336         PQclear(executeQueryOrDie(conn_template1,
337                                                           "UPDATE pg_catalog.pg_database "
338                                                           "SET  datfrozenxid = '%u'",
339                                                           old_cluster.controldata.chkpnt_nxtxid));
340
341         /* get database names */
342         dbres = executeQueryOrDie(conn_template1,
343                                                           "SELECT       datname, datallowconn "
344                                                           "FROM pg_catalog.pg_database");
345
346         i_datname = PQfnumber(dbres, "datname");
347         i_datallowconn = PQfnumber(dbres, "datallowconn");
348
349         ntups = PQntuples(dbres);
350         for (dbnum = 0; dbnum < ntups; dbnum++)
351         {
352                 char       *datname = PQgetvalue(dbres, dbnum, i_datname);
353                 char       *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
354
355                 /*
356                  * We must update databases where datallowconn = false, e.g.
357                  * template0, because autovacuum increments their datfrozenxids and
358                  * relfrozenxids even if autovacuum is turned off, and even though all
359                  * the data rows are already frozen  To enable this, we temporarily
360                  * change datallowconn.
361                  */
362                 if (strcmp(datallowconn, "f") == 0)
363                         PQclear(executeQueryOrDie(conn_template1,
364                                                                           "UPDATE pg_catalog.pg_database "
365                                                                           "SET  datallowconn = true "
366                                                                           "WHERE datname = '%s'", datname));
367
368                 conn = connectToServer(&new_cluster, datname);
369
370                 /* set pg_class.relfrozenxid */
371                 PQclear(executeQueryOrDie(conn,
372                                                                   "UPDATE       pg_catalog.pg_class "
373                                                                   "SET  relfrozenxid = '%u' "
374                 /* only heap and TOAST are vacuumed */
375                                                                   "WHERE        relkind IN ('r', 't')",
376                                                                   old_cluster.controldata.chkpnt_nxtxid));
377                 PQfinish(conn);
378
379                 /* Reset datallowconn flag */
380                 if (strcmp(datallowconn, "f") == 0)
381                         PQclear(executeQueryOrDie(conn_template1,
382                                                                           "UPDATE pg_catalog.pg_database "
383                                                                           "SET  datallowconn = false "
384                                                                           "WHERE datname = '%s'", datname));
385         }
386
387         PQclear(dbres);
388
389         PQfinish(conn_template1);
390
391         check_ok();
392 }
393
394
395 static void
396 cleanup(void)
397 {
398         int                     tblnum;
399         char            filename[MAXPGPATH];
400
401         for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
402                 pg_free(os_info.tablespaces[tblnum]);
403         pg_free(os_info.tablespaces);
404
405         dbarr_free(&old_cluster.dbarr);
406         dbarr_free(&new_cluster.dbarr);
407         pg_free(log_opts.filename);
408         pg_free(os_info.user);
409         pg_free(old_cluster.controldata.lc_collate);
410         pg_free(new_cluster.controldata.lc_collate);
411         pg_free(old_cluster.controldata.lc_ctype);
412         pg_free(new_cluster.controldata.lc_ctype);
413         pg_free(old_cluster.controldata.encoding);
414         pg_free(new_cluster.controldata.encoding);
415         pg_free(old_cluster.tablespace_suffix);
416         pg_free(new_cluster.tablespace_suffix);
417
418         if (log_opts.fd != NULL)
419         {
420                 fclose(log_opts.fd);
421                 log_opts.fd = NULL;
422         }
423
424         if (log_opts.debug_fd)
425                 fclose(log_opts.debug_fd);
426
427         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, ALL_DUMP_FILE);
428         unlink(filename);
429         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, GLOBALS_DUMP_FILE);
430         unlink(filename);
431         snprintf(filename, sizeof(filename), "%s/%s", os_info.cwd, DB_DUMP_FILE);
432         unlink(filename);
433 }