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