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