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