Age Owner TLA Line data Source code
1 : /*
2 : * pg_upgrade.c
3 : *
4 : * main source file
5 : *
6 : * Copyright (c) 2010-2023, PostgreSQL Global Development Group
7 : * src/bin/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 : * While pg_class.oid and pg_class.relfilenode are initially the same in a
19 : * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
20 : * control assignments of pg_class.relfilenode because we want the filenames
21 : * to match between the old and new cluster.
22 : *
23 : * We control assignment of pg_tablespace.oid because we want the oid to match
24 : * between the old and new cluster.
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 for historical reasons (the
33 : * oids used to be stored in pg_largeobject_metadata, which is now copied via
34 : * SQL commands), that might change at some point in the future.
35 : */
36 :
37 :
38 :
39 : #include "postgres_fe.h"
40 :
41 : #include <time.h>
42 :
43 : #ifdef HAVE_LANGINFO_H
44 : #include <langinfo.h>
45 : #endif
46 :
47 : #include "catalog/pg_class_d.h"
48 : #include "common/file_perm.h"
49 : #include "common/logging.h"
50 : #include "common/restricted_token.h"
51 : #include "fe_utils/string_utils.h"
52 : #include "pg_upgrade.h"
53 :
54 : static void set_locale_and_encoding(void);
55 : static void prepare_new_cluster(void);
56 : static void prepare_new_globals(void);
57 : static void create_new_objects(void);
58 : static void copy_xact_xlog_xid(void);
59 : static void set_frozenxids(bool minmxid_only);
60 : static void make_outputdirs(char *pgdata);
61 : static void setup(char *argv0, bool *live_check);
62 :
63 : ClusterInfo old_cluster,
64 : new_cluster;
65 : OSInfo os_info;
66 :
67 : char *output_files[] = {
68 : SERVER_LOG_FILE,
69 : #ifdef WIN32
70 : /* unique file for pg_ctl start */
71 : SERVER_START_LOG_FILE,
72 : #endif
73 : UTILITY_LOG_FILE,
74 : INTERNAL_LOG_FILE,
75 : NULL
76 : };
77 :
78 :
79 : int
4715 bruce 80 GIC 6 : main(int argc, char **argv)
4715 bruce 81 ECB : {
4715 bruce 82 GIC 6 : char *deletion_script_file_name = NULL;
4715 bruce 83 CBC 6 : bool live_check = false;
4715 bruce 84 ECB :
85 : /*
86 : * pg_upgrade doesn't currently use common/logging.c, but initialize it
87 : * anyway because we might call common code that does.
88 : */
1469 peter 89 GIC 6 : pg_logging_init(argv[0]);
2368 peter_e 90 6 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
91 :
92 : /* Set default restrictive mask until new cluster permissions are read */
1828 sfrost 93 6 : umask(PG_MODE_MASK_OWNER);
1889 tgl 94 ECB :
4555 bruce 95 CBC 6 : parseCommandLine(argc, argv);
96 :
1469 peter 97 GIC 3 : get_restricted_token();
2932 andrew 98 ECB :
4202 bruce 99 GIC 3 : adjust_data_dir(&old_cluster);
4202 bruce 100 CBC 3 : adjust_data_dir(&new_cluster);
101 :
427 michael 102 ECB : /*
103 : * Set mask based on PGDATA permissions, needed for the creation of the
104 : * output directories with correct permissions.
105 : */
427 michael 106 GIC 3 : if (!GetDataDirectoryCreatePerm(new_cluster.pgdata))
271 tgl 107 UNC 0 : pg_fatal("could not read permissions of directory \"%s\": %s",
427 michael 108 UIC 0 : new_cluster.pgdata, strerror(errno));
109 :
427 michael 110 GIC 3 : umask(pg_mode_mask);
427 michael 111 ECB :
427 michael 112 EUB : /*
113 : * This needs to happen after adjusting the data directory of the new
114 : * cluster in adjust_data_dir().
427 michael 115 ECB : */
427 michael 116 GIC 3 : make_outputdirs(new_cluster.pgdata);
117 :
3727 bruce 118 3 : setup(argv[0], &live_check);
119 :
120 2 : output_check_banner(live_check);
4715 bruce 121 ECB :
4555 bruce 122 GIC 2 : check_cluster_versions();
4715 bruce 123 ECB :
3870 tgl 124 GIC 2 : get_sock_dir(&old_cluster, live_check);
3870 tgl 125 CBC 2 : get_sock_dir(&new_cluster, false);
126 :
bruce 127 2 : check_cluster_compatibility(live_check);
128 :
3149 129 2 : check_and_dump_old_cluster(live_check);
4715 bruce 130 ECB :
131 :
132 : /* -- NEW -- */
3727 bruce 133 GIC 2 : start_postmaster(&new_cluster, true);
4715 bruce 134 ECB :
4555 bruce 135 GIC 2 : check_new_cluster();
136 2 : report_clusters_compatible();
137 :
2056 peter_e 138 CBC 1 : pg_log(PG_REPORT,
139 : "\n"
2056 peter_e 140 ECB : "Performing Upgrade\n"
141 : "------------------");
142 :
31 jdavis 143 GNC 1 : set_locale_and_encoding();
144 :
4555 bruce 145 CBC 1 : prepare_new_cluster();
146 :
4367 bruce 147 GIC 1 : stop_postmaster(false);
148 :
149 : /*
4715 bruce 150 ECB : * Destructive Changes to New Cluster
151 : */
152 :
2214 rhaas 153 GIC 1 : copy_xact_xlog_xid();
4715 bruce 154 ECB :
155 : /* New now using xids of the old system */
156 :
157 : /* -- NEW -- */
3727 bruce 158 GIC 1 : start_postmaster(&new_cluster, true);
159 :
1903 tgl 160 CBC 1 : prepare_new_globals();
161 :
4555 bruce 162 GIC 1 : create_new_objects();
163 :
4367 164 1 : stop_postmaster(false);
4367 bruce 165 ECB :
166 : /*
3955 167 : * Most failures happen in create_new_objects(), which has completed at
168 : * this point. We do this here because it is just before linking, which
169 : * will link the old and new cluster data files, preventing the old
170 : * cluster from being safely started once the new cluster is started.
4052 171 : */
4052 bruce 172 GIC 1 : if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
4052 bruce 173 UIC 0 : disable_old_cluster();
174 :
3742 bruce 175 GIC 1 : transfer_all_new_tablespaces(&old_cluster.dbarr, &new_cluster.dbarr,
176 : old_cluster.pgdata, new_cluster.pgdata);
177 :
178 : /*
4715 bruce 179 ECB : * Assuming OIDs are only used in system tables, there is no need to
4715 bruce 180 EUB : * restore the OID counter because we have not transferred any OIDs from
181 : * the old system, but we do it anyway just in case. We do it late here
4715 bruce 182 ECB : * because there is no need to have the schema load use new oids.
183 : */
4289 peter_e 184 GIC 1 : prep_status("Setting next OID for new cluster");
1917 bruce 185 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
186 : "\"%s/pg_resetwal\" -o %u \"%s\"",
187 : new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid,
188 : new_cluster.pgdata);
4555 189 1 : check_ok();
190 :
477 michael 191 CBC 1 : if (user_opts.do_sync)
477 michael 192 ECB : {
477 michael 193 UIC 0 : prep_status("Sync data directory to disk");
194 0 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
195 : "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
477 michael 196 ECB : new_cluster.pgdata);
477 michael 197 UIC 0 : check_ok();
477 michael 198 ECB : }
199 :
4555 bruce 200 GBC 1 : create_script_for_old_cluster_deletion(&deletion_script_file_name);
4715 bruce 201 EUB :
2119 bruce 202 GIC 1 : issue_warnings_and_set_wal_level();
203 :
2056 peter_e 204 GBC 1 : pg_log(PG_REPORT,
205 : "\n"
206 : "Upgrade Complete\n"
207 : "----------------");
208 :
881 magnus 209 CBC 1 : output_completion_banner(deletion_script_file_name);
210 :
4715 bruce 211 1 : pg_free(deletion_script_file_name);
212 :
305 michael 213 GIC 1 : cleanup_output_dirs();
214 :
4715 bruce 215 1 : return 0;
4715 bruce 216 ECB : }
217 :
427 michael 218 : /*
219 : * Create and assign proper permissions to the set of output directories
220 : * used to store any data generated internally, filling in log_opts in
221 : * the process.
222 : */
223 : static void
427 michael 224 GIC 3 : make_outputdirs(char *pgdata)
225 : {
226 : FILE *fp;
227 : char **filename;
228 3 : time_t run_time = time(NULL);
229 : char filename_path[MAXPGPATH];
230 : char timebuf[128];
305 michael 231 ECB : struct timeval time;
232 : time_t tt;
233 : int len;
234 :
305 michael 235 CBC 3 : log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
305 michael 236 GIC 3 : len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
237 3 : if (len >= MAXPGPATH)
271 tgl 238 UNC 0 : pg_fatal("directory path for new cluster is too long");
239 :
240 : /* BASE_OUTPUTDIR/$timestamp/ */
305 michael 241 GIC 3 : gettimeofday(&time, NULL);
305 michael 242 CBC 3 : tt = (time_t) time.tv_sec;
243 3 : strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
305 michael 244 ECB : /* append milliseconds */
305 michael 245 GBC 3 : snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
305 michael 246 GIC 3 : ".%03d", (int) (time.tv_usec / 1000));
247 3 : log_opts.basedir = (char *) pg_malloc0(MAXPGPATH);
305 michael 248 CBC 3 : len = snprintf(log_opts.basedir, MAXPGPATH, "%s/%s", log_opts.rootdir,
305 michael 249 ECB : timebuf);
305 michael 250 CBC 3 : if (len >= MAXPGPATH)
271 tgl 251 UNC 0 : pg_fatal("directory path for new cluster is too long");
305 michael 252 ECB :
253 : /* BASE_OUTPUTDIR/$timestamp/dump/ */
305 michael 254 CBC 3 : log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
255 3 : len = snprintf(log_opts.dumpdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
256 : timebuf, DUMP_OUTPUTDIR);
257 3 : if (len >= MAXPGPATH)
271 tgl 258 UNC 0 : pg_fatal("directory path for new cluster is too long");
259 :
260 : /* BASE_OUTPUTDIR/$timestamp/log/ */
305 michael 261 CBC 3 : log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
262 3 : len = snprintf(log_opts.logdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
263 : timebuf, LOG_OUTPUTDIR);
264 3 : if (len >= MAXPGPATH)
271 tgl 265 UNC 0 : pg_fatal("directory path for new cluster is too long");
266 :
267 : /*
305 michael 268 ECB : * Ignore the error case where the root path exists, as it is kept the
269 : * same across runs.
270 : */
305 michael 271 CBC 3 : if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
271 tgl 272 UNC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
305 michael 273 GIC 3 : if (mkdir(log_opts.basedir, pg_dir_create_mode) < 0)
271 tgl 274 UNC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
305 michael 275 GIC 3 : if (mkdir(log_opts.dumpdir, pg_dir_create_mode) < 0)
271 tgl 276 UNC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
305 michael 277 GIC 3 : if (mkdir(log_opts.logdir, pg_dir_create_mode) < 0)
271 tgl 278 UNC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
427 michael 279 EUB :
300 tgl 280 CBC 3 : len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
300 tgl 281 EUB : log_opts.logdir, INTERNAL_LOG_FILE);
300 tgl 282 CBC 3 : if (len >= sizeof(filename_path))
271 tgl 283 UNC 0 : pg_fatal("directory path for new cluster is too long");
300 tgl 284 ECB :
427 michael 285 GBC 3 : if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
271 tgl 286 UNC 0 : pg_fatal("could not open log file \"%s\": %m", filename_path);
427 michael 287 ECB :
288 : /* label start of upgrade in logfiles */
427 michael 289 CBC 12 : for (filename = output_files; *filename != NULL; filename++)
427 michael 290 EUB : {
300 tgl 291 GIC 9 : len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
300 tgl 292 ECB : log_opts.logdir, *filename);
300 tgl 293 GBC 9 : if (len >= sizeof(filename_path))
271 tgl 294 UNC 0 : pg_fatal("directory path for new cluster is too long");
427 michael 295 GIC 9 : if ((fp = fopen_priv(filename_path, "a")) == NULL)
271 tgl 296 UNC 0 : pg_fatal("could not write to log file \"%s\": %m", filename_path);
297 :
300 tgl 298 CBC 9 : fprintf(fp,
299 : "-----------------------------------------------------------------\n"
427 michael 300 ECB : " pg_upgrade run on %s"
427 michael 301 EUB : "-----------------------------------------------------------------\n\n",
427 michael 302 ECB : ctime(&run_time));
427 michael 303 GBC 9 : fclose(fp);
304 : }
427 michael 305 CBC 3 : }
306 :
307 :
308 : static void
3727 bruce 309 GIC 3 : setup(char *argv0, bool *live_check)
4715 bruce 310 ECB : {
311 : /*
312 : * make sure the user has a clean environment, otherwise, we may confuse
313 : * libpq when we connect to one (or both) of the servers.
314 : */
4346 bruce 315 GIC 3 : check_pghost_envvar();
4715 bruce 316 ECB :
317 : /*
318 : * In case the user hasn't specified the directory for the new binaries
319 : * with -B, default to using the path of the currently executed pg_upgrade
320 : * binary.
321 : */
1352 peter 322 CBC 3 : if (!new_cluster.bindir)
323 : {
324 : char exec_path[MAXPGPATH];
325 :
1352 peter 326 UIC 0 : if (find_my_exec(argv0, exec_path) < 0)
271 tgl 327 UNC 0 : pg_fatal("%s: could not find own program executable", argv0);
328 : /* Trim off program name and keep just path */
1352 peter 329 LBC 0 : *last_dir_separator(exec_path) = '\0';
1352 peter 330 UIC 0 : canonicalize_path(exec_path);
331 0 : new_cluster.bindir = pg_strdup(exec_path);
332 : }
1352 peter 333 EUB :
4555 bruce 334 GBC 3 : verify_directories();
335 :
3727 bruce 336 EUB : /* no postmasters should be running, except for a live check */
3727 bruce 337 GBC 2 : if (pid_lock_file_exists(old_cluster.pgdata))
3727 bruce 338 EUB : {
339 : /*
340 : * If we have a postmaster.pid file, try to start the server. If it
3260 bruce 341 ECB : * starts, the pid file was stale, so stop the server. If it doesn't
342 : * start, assume the server is running. If the pid file is left over
343 : * from a server crash, this also allows any committed transactions
3602 344 : * stored in the WAL to be replayed so they are not lost, because WAL
345 : * files are not transferred from old to new servers. We later check
346 : * for a clean shutdown.
347 : */
3727 bruce 348 UIC 0 : if (start_postmaster(&old_cluster, false))
349 0 : stop_postmaster(false);
350 : else
351 : {
352 0 : if (!user_opts.check)
3477 peter_e 353 0 : pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
354 : "Please shutdown that postmaster and try again.");
3727 bruce 355 EUB : else
3727 bruce 356 UBC 0 : *live_check = true;
357 : }
358 : }
4715 bruce 359 EUB :
360 : /* same goes for the new postmaster */
3727 bruce 361 GIC 2 : if (pid_lock_file_exists(new_cluster.pgdata))
362 : {
3727 bruce 363 UBC 0 : if (start_postmaster(&new_cluster, false))
3727 bruce 364 UIC 0 : stop_postmaster(false);
365 : else
3477 peter_e 366 0 : pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
367 : "Please shutdown that postmaster and try again.");
3727 bruce 368 ECB : }
4715 bruce 369 GIC 2 : }
4715 bruce 370 EUB :
371 :
372 : /*
373 : * Copy locale and encoding information into the new cluster's template0.
374 : *
375 : * We need to copy the encoding, datlocprovider, datcollate, datctype, and
376 : * daticulocale. We don't need datcollversion because that's never set for
377 : * template0.
378 : */
379 : static void
31 jdavis 380 GNC 1 : set_locale_and_encoding(void)
381 : {
382 : PGconn *conn_new_template1;
383 : char *datcollate_literal;
384 : char *datctype_literal;
385 1 : char *daticulocale_literal = NULL;
386 1 : DbLocaleInfo *locale = old_cluster.template0;
387 :
388 1 : prep_status("Setting locale and encoding for new cluster");
389 :
390 : /* escape literals with respect to new cluster */
391 1 : conn_new_template1 = connectToServer(&new_cluster, "template1");
392 :
393 1 : datcollate_literal = PQescapeLiteral(conn_new_template1,
394 1 : locale->db_collate,
395 1 : strlen(locale->db_collate));
396 1 : datctype_literal = PQescapeLiteral(conn_new_template1,
397 1 : locale->db_ctype,
398 1 : strlen(locale->db_ctype));
399 1 : if (locale->db_iculocale)
400 1 : daticulocale_literal = PQescapeLiteral(conn_new_template1,
401 1 : locale->db_iculocale,
402 1 : strlen(locale->db_iculocale));
403 : else
31 jdavis 404 UNC 0 : daticulocale_literal = pg_strdup("NULL");
405 :
406 : /* update template0 in new cluster */
31 jdavis 407 GNC 1 : if (GET_MAJOR_VERSION(new_cluster.major_version) >= 1500)
408 1 : PQclear(executeQueryOrDie(conn_new_template1,
409 : "UPDATE pg_catalog.pg_database "
410 : " SET encoding = %d, "
411 : " datlocprovider = '%c', "
412 : " datcollate = %s, "
413 : " datctype = %s, "
414 : " daticulocale = %s "
415 : " WHERE datname = 'template0' ",
416 : locale->db_encoding,
417 1 : locale->db_collprovider,
418 : datcollate_literal,
419 : datctype_literal,
420 : daticulocale_literal));
421 : else
31 jdavis 422 UNC 0 : PQclear(executeQueryOrDie(conn_new_template1,
423 : "UPDATE pg_catalog.pg_database "
424 : " SET encoding = %d, "
425 : " datcollate = %s, "
426 : " datctype = %s "
427 : " WHERE datname = 'template0' ",
428 : locale->db_encoding,
429 : datcollate_literal,
430 : datctype_literal));
431 :
31 jdavis 432 GNC 1 : PQfreemem(datcollate_literal);
433 1 : PQfreemem(datctype_literal);
434 1 : PQfreemem(daticulocale_literal);
435 :
436 1 : PQfinish(conn_new_template1);
437 :
438 1 : check_ok();
439 1 : }
440 :
441 :
442 : static void
4555 bruce 443 GBC 1 : prepare_new_cluster(void)
444 : {
445 : /*
4715 bruce 446 ECB : * It would make more sense to freeze after loading the schema, but that
447 : * would cause us to lose the frozenxids restored by the load. We use
448 : * --analyze so autovacuum doesn't update statistics later
449 : */
4555 bruce 450 GIC 1 : prep_status("Analyzing all rows in the new cluster");
1917 451 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
452 : "\"%s/vacuumdb\" %s --all --analyze %s",
453 : new_cluster.bindir, cluster_conn_opts(&new_cluster),
3877 alvherre 454 1 : log_opts.verbose ? "--verbose" : "");
4555 bruce 455 1 : check_ok();
456 :
4715 bruce 457 ECB : /*
458 : * We do freeze after analyze so pg_statistic is also frozen. template0 is
459 : * not frozen here, but data rows were frozen by initdb, and we set its
460 : * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
461 : * counter later.
462 : */
2038 peter_e 463 CBC 1 : prep_status("Freezing all rows in the new cluster");
1917 bruce 464 GIC 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
3870 tgl 465 ECB : "\"%s/vacuumdb\" %s --all --freeze %s",
466 : new_cluster.bindir, cluster_conn_opts(&new_cluster),
3877 alvherre 467 GIC 1 : log_opts.verbose ? "--verbose" : "");
4555 bruce 468 CBC 1 : check_ok();
4715 bruce 469 GIC 1 : }
4715 bruce 470 ECB :
471 :
472 : static void
1903 tgl 473 CBC 1 : prepare_new_globals(void)
4715 bruce 474 ECB : {
475 : /*
1873 tgl 476 : * Before we restore anything, set frozenxids of initdb-created tables.
4715 bruce 477 : */
3203 bruce 478 CBC 1 : set_frozenxids(false);
4715 bruce 479 ECB :
480 : /*
1873 tgl 481 EUB : * Now restore global objects (roles and tablespaces).
482 : */
3782 bruce 483 GIC 1 : prep_status("Restoring global objects in the new cluster");
4475 bruce 484 ECB :
1917 bruce 485 CBC 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
486 : "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s/%s\"",
487 : new_cluster.bindir, cluster_conn_opts(&new_cluster),
488 : log_opts.dumpdir,
489 : GLOBALS_DUMP_FILE);
4555 bruce 490 GIC 1 : check_ok();
4715 491 1 : }
492 :
493 :
4715 bruce 494 ECB : static void
4555 bruce 495 GIC 1 : create_new_objects(void)
496 : {
497 : int dbnum;
498 :
412 andres 499 GBC 1 : prep_status_progress("Restoring database schemas in the new cluster");
500 :
501 : /*
502 : * We cannot process the template1 database concurrently with others,
503 : * because when it's transiently dropped, connection attempts would fail.
504 : * So handle it in a separate non-parallelized pass.
505 : */
3782 bruce 506 GIC 1 : for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
507 : {
508 : char sql_file_name[MAXPGPATH],
3602 bruce 509 ECB : log_file_name[MAXPGPATH];
3602 bruce 510 CBC 1 : DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
1903 tgl 511 ECB : const char *create_opts;
512 :
1869 513 : /* Process only template1 in this pass */
1869 tgl 514 GIC 1 : if (strcmp(old_db->db_name, "template1") != 0)
1869 tgl 515 LBC 0 : continue;
3782 bruce 516 ECB :
3775 bruce 517 GIC 1 : pg_log(PG_STATUS, "%s", old_db->db_name);
3756 518 1 : snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
519 1 : snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
3782 bruce 520 ECB :
521 : /*
522 : * template1 database will already exist in the target installation,
523 : * so tell pg_restore to drop and recreate it; otherwise we would fail
524 : * to propagate its database-level properties.
525 : */
1869 tgl 526 GIC 1 : create_opts = "--clean --create";
1869 tgl 527 ECB :
1869 tgl 528 CBC 1 : exec_prog(log_file_name,
529 : NULL,
530 : true,
1869 tgl 531 ECB : true,
532 : "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
533 : "--dbname postgres \"%s/%s\"",
534 : new_cluster.bindir,
535 : cluster_conn_opts(&new_cluster),
536 : create_opts,
537 : log_opts.dumpdir,
538 : sql_file_name);
539 :
1869 tgl 540 CBC 1 : break; /* done once we've processed template1 */
1869 tgl 541 ECB : }
542 :
1869 tgl 543 GIC 7 : for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1869 tgl 544 ECB : {
545 : char sql_file_name[MAXPGPATH],
546 : log_file_name[MAXPGPATH];
1869 tgl 547 GIC 6 : DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
548 : const char *create_opts;
549 :
1869 tgl 550 ECB : /* Skip template1 in this pass */
1903 tgl 551 GIC 6 : if (strcmp(old_db->db_name, "template1") == 0)
1869 552 1 : continue;
553 :
554 5 : pg_log(PG_STATUS, "%s", old_db->db_name);
1869 tgl 555 CBC 5 : snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
1869 tgl 556 GIC 5 : snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
557 :
558 : /*
559 : * postgres database will already exist in the target installation, so
1220 michael 560 ECB : * tell pg_restore to drop and recreate it; otherwise we would fail to
561 : * propagate its database-level properties.
1869 tgl 562 : */
1869 tgl 563 GIC 5 : if (strcmp(old_db->db_name, "postgres") == 0)
564 1 : create_opts = "--clean --create";
565 : else
566 4 : create_opts = "--create";
1903 tgl 567 ECB :
3599 sfrost 568 CBC 5 : parallel_exec_prog(log_file_name,
569 : NULL,
570 : "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
571 : "--dbname template1 \"%s/%s\"",
3599 sfrost 572 ECB : new_cluster.bindir,
573 : cluster_conn_opts(&new_cluster),
574 : create_opts,
575 : log_opts.dumpdir,
576 : sql_file_name);
577 : }
578 :
579 : /* reap all children */
3756 bruce 580 GIC 1 : while (reap_child(true) == true)
581 : ;
582 :
3782 bruce 583 CBC 1 : end_progress_output();
4555 bruce 584 GIC 1 : check_ok();
585 :
586 : /*
2878 bruce 587 ECB : * We don't have minmxids for databases or relations in pre-9.3 clusters,
588 : * so set those after we have restored the schema.
589 : */
915 bruce 590 GIC 1 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 902)
3203 bruce 591 LBC 0 : set_frozenxids(true);
3203 bruce 592 EUB :
593 : /* update new_cluster info now that we have objects in the databases */
4481 bruce 594 CBC 1 : get_db_and_rel_infos(&new_cluster);
4715 595 1 : }
4715 bruce 596 ECB :
597 : /*
598 : * Delete the given subdirectory contents from the new cluster
599 : */
600 : static void
1986 peter_e 601 GIC 3 : remove_new_subdir(const char *subdir, bool rmtopdir)
602 : {
3930 alvherre 603 ECB : char new_path[MAXPGPATH];
604 :
3930 alvherre 605 CBC 3 : prep_status("Deleting files from new %s", subdir);
606 :
3930 alvherre 607 GIC 3 : snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
3211 bruce 608 3 : if (!rmtree(new_path, rmtopdir))
271 tgl 609 UNC 0 : pg_fatal("could not delete directory \"%s\"", new_path);
610 :
4555 bruce 611 GIC 3 : check_ok();
3211 612 3 : }
613 :
614 : /*
615 : * Copy the files from the old cluster into it
616 : */
3211 bruce 617 ECB : static void
1986 peter_e 618 GIC 3 : copy_subdir_files(const char *old_subdir, const char *new_subdir)
619 : {
3211 bruce 620 ECB : char old_path[MAXPGPATH];
621 : char new_path[MAXPGPATH];
622 :
2214 rhaas 623 GIC 3 : remove_new_subdir(new_subdir, true);
3211 bruce 624 ECB :
2214 rhaas 625 GIC 3 : snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
626 3 : snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
627 :
2214 rhaas 628 CBC 3 : prep_status("Copying old %s to new server", old_subdir);
3930 alvherre 629 ECB :
1917 bruce 630 GIC 3 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
4715 bruce 631 ECB : #ifndef WIN32
3877 alvherre 632 : "cp -Rf \"%s\" \"%s\"",
4715 bruce 633 : #else
634 : /* flags: everything, no confirm, quiet, overwrite read-only */
635 : "xcopy /e /y /q /r \"%s\" \"%s\\\"",
636 : #endif
637 : old_path, new_path);
638 :
4555 bruce 639 GIC 3 : check_ok();
3930 alvherre 640 CBC 3 : }
3930 alvherre 641 ECB :
642 : static void
2214 rhaas 643 CBC 1 : copy_xact_xlog_xid(void)
644 : {
2214 rhaas 645 ECB : /*
646 : * Copy old commit logs to new data dir. pg_clog has been renamed to
647 : * pg_xact in post-10 clusters.
648 : */
915 bruce 649 GIC 1 : copy_subdir_files(GET_MAJOR_VERSION(old_cluster.major_version) <= 906 ?
650 : "pg_clog" : "pg_xact",
651 1 : GET_MAJOR_VERSION(new_cluster.major_version) <= 906 ?
652 : "pg_clog" : "pg_xact");
653 :
622 654 1 : prep_status("Setting oldest XID for new cluster");
655 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
656 : "\"%s/pg_resetwal\" -f -u %u \"%s\"",
622 bruce 657 ECB : new_cluster.bindir, old_cluster.controldata.chkpnt_oldstxid,
658 : new_cluster.pgdata);
622 bruce 659 GIC 1 : check_ok();
622 bruce 660 ECB :
3138 661 : /* set the next transaction id and epoch of the new cluster */
3138 bruce 662 GIC 1 : prep_status("Setting next transaction ID and epoch for new cluster");
1917 663 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
664 : "\"%s/pg_resetwal\" -f -x %u \"%s\"",
665 : new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid,
666 : new_cluster.pgdata);
1917 bruce 667 CBC 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
2250 rhaas 668 EUB : "\"%s/pg_resetwal\" -f -e %u \"%s\"",
669 : new_cluster.bindir, old_cluster.controldata.chkpnt_nxtepoch,
670 : new_cluster.pgdata);
3049 alvherre 671 ECB : /* must reset commit timestamp limits also */
1917 bruce 672 CBC 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
673 : "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
674 : new_cluster.bindir,
675 : old_cluster.controldata.chkpnt_nxtxid,
676 : old_cluster.controldata.chkpnt_nxtxid,
677 : new_cluster.pgdata);
4555 678 1 : check_ok();
679 :
680 : /*
681 : * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
3692 alvherre 682 ECB : * (see pg_upgrade.h) and the new server is after, then we don't copy
683 : * pg_multixact files, but we need to reset pg_control so that the new
684 : * server doesn't attempt to read multis older than the cutoff value.
3728 685 : */
3728 alvherre 686 GBC 1 : if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
3728 alvherre 687 GIC 1 : new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
3728 alvherre 688 ECB : {
2214 rhaas 689 CBC 1 : copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
2214 rhaas 690 GIC 1 : copy_subdir_files("pg_multixact/members", "pg_multixact/members");
691 :
3728 alvherre 692 1 : prep_status("Setting next multixact ID and offset for new cluster");
693 :
694 : /*
3728 alvherre 695 ECB : * we preserve all files and contents, so we must preserve both "next"
696 : * counters here and the oldest multi present on system.
697 : */
1917 bruce 698 GIC 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
699 : "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
3728 alvherre 700 ECB : new_cluster.bindir,
701 : old_cluster.controldata.chkpnt_nxtmxoff,
702 : old_cluster.controldata.chkpnt_nxtmulti,
703 : old_cluster.controldata.chkpnt_oldstMulti,
704 : new_cluster.pgdata);
3728 alvherre 705 CBC 1 : check_ok();
706 : }
3728 alvherre 707 LBC 0 : else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
708 : {
709 : /*
710 : * Remove offsets/0000 file created by initdb that no longer matches
711 : * the new multi-xid value. "members" starts at zero so no need to
712 : * remove it.
713 : */
3211 bruce 714 UIC 0 : remove_new_subdir("pg_multixact/offsets", false);
715 :
2038 peter_e 716 LBC 0 : prep_status("Setting oldest multixact ID in new cluster");
3602 bruce 717 ECB :
718 : /*
719 : * We don't preserve files in this case, but it's important that the
3728 alvherre 720 : * oldest multi is set to the latest value used by the old system, so
721 : * that multixact.c returns the empty set for multis that might be
722 : * present on disk. We set next multi to the value following that; it
723 : * might end up wrapped around (i.e. 0) if the old cluster had
724 : * next=MaxMultiXactId, but multixact.c can cope with that just fine.
725 : */
1917 bruce 726 LBC 0 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
727 : "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
3728 alvherre 728 ECB : new_cluster.bindir,
3728 alvherre 729 UIC 0 : old_cluster.controldata.chkpnt_nxtmulti + 1,
730 : old_cluster.controldata.chkpnt_nxtmulti,
3728 alvherre 731 ECB : new_cluster.pgdata);
3728 alvherre 732 LBC 0 : check_ok();
733 : }
734 :
735 : /* now reset the wal archives in the new cluster */
4555 bruce 736 CBC 1 : prep_status("Resetting WAL archives");
1917 bruce 737 GIC 1 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
738 : /* use timeline 1 to match controldata and no WAL history file */
2250 rhaas 739 ECB : "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
2885 bruce 740 : old_cluster.controldata.nextxlogfile + 8,
741 : new_cluster.pgdata);
4555 bruce 742 GIC 1 : check_ok();
4715 743 1 : }
4715 bruce 744 ECB :
745 :
746 : /*
747 : * set_frozenxids()
748 : *
1873 tgl 749 : * This is called on the new cluster before we restore anything, with
750 : * minmxid_only = false. Its purpose is to ensure that all initdb-created
751 : * vacuumable tables have relfrozenxid/relminmxid matching the old cluster's
752 : * xid/mxid counters. We also initialize the datfrozenxid/datminmxid of the
753 : * built-in databases to match.
754 : *
755 : * As we create user tables later, their relfrozenxid/relminmxid fields will
756 : * be restored properly by the binary-upgrade restore script. Likewise for
757 : * user-database datfrozenxid/datminmxid. However, if we're upgrading from a
758 : * pre-9.3 database, which does not store per-table or per-DB minmxid, then
759 : * the relminmxid/datminmxid values filled in by the restore script will just
760 : * be zeroes.
761 : *
762 : * Hence, with a pre-9.3 source database, a second call occurs after
763 : * everything is restored, with minmxid_only = true. This pass will
764 : * initialize all tables and databases, both those made by initdb and user
765 : * objects, with the desired minmxid value. frozenxid values are left alone.
4715 bruce 766 : */
1873 tgl 767 : static void
3203 bruce 768 GIC 1 : set_frozenxids(bool minmxid_only)
4715 bruce 769 ECB : {
770 : int dbnum;
771 : PGconn *conn,
772 : *conn_template1;
773 : PGresult *dbres;
774 : int ntups;
4708 775 : int i_datname;
776 : int i_datallowconn;
777 :
3203 bruce 778 GIC 1 : if (!minmxid_only)
779 1 : prep_status("Setting frozenxid and minmxid counters in new cluster");
780 : else
3203 bruce 781 UIC 0 : prep_status("Setting minmxid counter in new cluster");
4715 bruce 782 ECB :
4481 bruce 783 GIC 1 : conn_template1 = connectToServer(&new_cluster, "template1");
4715 bruce 784 EUB :
3203 bruce 785 GIC 1 : if (!minmxid_only)
786 : /* set pg_database.datfrozenxid */
787 1 : PQclear(executeQueryOrDie(conn_template1,
788 : "UPDATE pg_catalog.pg_database "
789 : "SET datfrozenxid = '%u'",
790 : old_cluster.controldata.chkpnt_nxtxid));
3203 bruce 791 EUB :
792 : /* set pg_database.datminmxid */
4555 bruce 793 GBC 1 : PQclear(executeQueryOrDie(conn_template1,
794 : "UPDATE pg_catalog.pg_database "
795 : "SET datminmxid = '%u'",
796 : old_cluster.controldata.chkpnt_nxtmulti));
797 :
798 : /* get database names */
4555 bruce 799 GIC 1 : dbres = executeQueryOrDie(conn_template1,
800 : "SELECT datname, datallowconn "
801 : "FROM pg_catalog.pg_database");
802 :
4708 bruce 803 GBC 1 : i_datname = PQfnumber(dbres, "datname");
4708 bruce 804 GIC 1 : i_datallowconn = PQfnumber(dbres, "datallowconn");
805 :
4715 bruce 806 GBC 1 : ntups = PQntuples(dbres);
4715 bruce 807 GIC 4 : for (dbnum = 0; dbnum < ntups; dbnum++)
808 : {
4660 bruce 809 GBC 3 : char *datname = PQgetvalue(dbres, dbnum, i_datname);
4660 bruce 810 GIC 3 : char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
811 :
812 : /*
4660 bruce 813 ECB : * We must update databases where datallowconn = false, e.g.
3203 814 : * template0, because autovacuum increments their datfrozenxids,
815 : * relfrozenxids, and relminmxid even if autovacuum is turned off, and
816 : * even though all the data rows are already frozen. To enable this,
817 : * we temporarily change datallowconn.
818 : */
4708 bruce 819 CBC 3 : if (strcmp(datallowconn, "f") == 0)
4555 820 1 : PQclear(executeQueryOrDie(conn_template1,
821 : "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
822 : quote_identifier(datname)));
823 :
4481 bruce 824 GIC 3 : conn = connectToServer(&new_cluster, datname);
825 :
3203 826 3 : if (!minmxid_only)
827 : /* set pg_class.relfrozenxid */
828 3 : PQclear(executeQueryOrDie(conn,
829 : "UPDATE pg_catalog.pg_class "
830 : "SET relfrozenxid = '%u' "
831 : /* only heap, materialized view, and TOAST are vacuumed */
832 : "WHERE relkind IN ("
833 : CppAsString2(RELKIND_RELATION) ", "
834 : CppAsString2(RELKIND_MATVIEW) ", "
835 : CppAsString2(RELKIND_TOASTVALUE) ")",
836 : old_cluster.controldata.chkpnt_nxtxid));
837 :
838 : /* set pg_class.relminmxid */
4555 839 3 : PQclear(executeQueryOrDie(conn,
840 : "UPDATE pg_catalog.pg_class "
841 : "SET relminmxid = '%u' "
842 : /* only heap, materialized view, and TOAST are vacuumed */
843 : "WHERE relkind IN ("
844 : CppAsString2(RELKIND_RELATION) ", "
2222 tgl 845 ECB : CppAsString2(RELKIND_MATVIEW) ", "
846 : CppAsString2(RELKIND_TOASTVALUE) ")",
847 : old_cluster.controldata.chkpnt_nxtmulti));
4715 bruce 848 GIC 3 : PQfinish(conn);
849 :
850 : /* Reset datallowconn flag */
4708 851 3 : if (strcmp(datallowconn, "f") == 0)
4555 852 1 : PQclear(executeQueryOrDie(conn_template1,
853 : "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
854 : quote_identifier(datname)));
4715 bruce 855 ECB : }
856 :
4715 bruce 857 GIC 1 : PQclear(dbres);
4715 bruce 858 EUB :
4708 bruce 859 GIC 1 : PQfinish(conn_template1);
4708 bruce 860 ECB :
4555 bruce 861 GIC 1 : check_ok();
4715 bruce 862 CBC 1 : }
|