Age Owner Branch data TLA 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
5596 bruce@momjian.us 89 :CBC 21 : main(int argc, char **argv)
90 : : {
91 : 21 : 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 : : */
2350 peter@eisentraut.org 98 : 21 : pg_logging_init(argv[0]);
3249 peter_e@gmx.net 99 : 21 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
100 : :
101 : : /* Set default restrictive mask until new cluster permissions are read */
2709 sfrost@snowman.net 102 : 21 : umask(PG_MODE_MASK_OWNER);
103 : :
5436 bruce@momjian.us 104 : 21 : parseCommandLine(argc, argv);
105 : :
2350 peter@eisentraut.org 106 : 18 : get_restricted_token();
107 : :
5083 bruce@momjian.us 108 : 18 : adjust_data_dir(&old_cluster);
109 : 18 : 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 : : */
1308 michael@paquier.xyz 115 [ - + ]: 18 : if (!GetDataDirectoryCreatePerm(new_cluster.pgdata))
543 michael@paquier.xyz 116 :UBC 0 : pg_fatal("could not read permissions of directory \"%s\": %m",
117 : : new_cluster.pgdata);
118 : :
1308 michael@paquier.xyz 119 :CBC 18 : 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 : 18 : make_outputdirs(new_cluster.pgdata);
126 : :
407 nathan@postgresql.or 127 : 18 : setup(argv[0]);
128 : :
129 : 17 : output_check_banner();
130 : :
5436 bruce@momjian.us 131 : 17 : check_cluster_versions();
132 : :
407 nathan@postgresql.or 133 : 16 : get_sock_dir(&old_cluster);
134 : 16 : get_sock_dir(&new_cluster);
135 : :
136 : 16 : check_cluster_compatibility();
137 : :
138 : 16 : check_and_dump_old_cluster();
139 : :
140 : :
141 : : /* -- NEW -- */
4608 bruce@momjian.us 142 : 13 : start_postmaster(&new_cluster, true);
143 : :
5436 144 : 13 : check_new_cluster();
145 : 9 : report_clusters_compatible();
146 : :
2937 peter_e@gmx.net 147 : 8 : pg_log(PG_REPORT,
148 : : "\n"
149 : : "Performing Upgrade\n"
150 : : "------------------");
151 : :
912 jdavis@postgresql.or 152 : 8 : set_locale_and_encoding();
153 : :
5436 bruce@momjian.us 154 : 8 : prepare_new_cluster();
155 : :
5248 156 : 8 : stop_postmaster(false);
157 : :
158 : : /*
159 : : * Destructive Changes to New Cluster
160 : : */
161 : :
3095 rhaas@postgresql.org 162 : 8 : copy_xact_xlog_xid();
197 msawada@postgresql.o 163 : 8 : set_new_cluster_char_signedness();
164 : :
165 : : /* New now using xids of the old system */
166 : :
167 : : /* -- NEW -- */
4608 bruce@momjian.us 168 : 8 : start_postmaster(&new_cluster, true);
169 : :
2784 tgl@sss.pgh.pa.us 170 : 8 : prepare_new_globals();
171 : :
5436 bruce@momjian.us 172 : 8 : create_new_objects();
173 : :
5248 174 : 8 : 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 : : */
165 nathan@postgresql.or 183 [ + + ]: 8 : if (user_opts.transfer_mode == TRANSFER_MODE_LINK ||
184 [ + + ]: 7 : user_opts.transfer_mode == TRANSFER_MODE_SWAP)
185 : 2 : disable_old_cluster(user_opts.transfer_mode);
186 : :
4623 bruce@momjian.us 187 : 8 : 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 : : */
5170 peter_e@gmx.net 196 : 8 : prep_status("Setting next OID for new cluster");
2798 bruce@momjian.us 197 : 8 : 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);
5436 201 : 8 : check_ok();
202 : :
45 akapila@postgresql.o 203 :GNC 8 : 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 [ + + + + ]: 8 : if (migrate_logical_slots || old_cluster.sub_retain_dead_tuples)
227 : : {
681 akapila@postgresql.o 228 :CBC 2 : start_postmaster(&new_cluster, true);
229 : :
45 akapila@postgresql.o 230 [ + + ]:GNC 2 : if (migrate_logical_slots)
231 : 1 : create_logical_replication_slots();
232 : :
233 [ + + ]: 2 : if (old_cluster.sub_retain_dead_tuples)
234 : 1 : create_conflict_detection_slot();
235 : :
681 akapila@postgresql.o 236 :CBC 2 : stop_postmaster(false);
237 : : }
238 : :
1358 michael@paquier.xyz 239 [ - + ]: 8 : if (user_opts.do_sync)
240 : : {
1358 michael@paquier.xyz 241 :UBC 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,
165 nathan@postgresql.or 245 [ # # ]: 0 : (user_opts.transfer_mode == TRANSFER_MODE_SWAP) ?
246 : : "--no-sync-data-files" : "",
247 : : new_cluster.pgdata,
248 : : user_opts.sync_method);
1358 michael@paquier.xyz 249 : 0 : check_ok();
250 : : }
251 : :
5436 bruce@momjian.us 252 :CBC 8 : create_script_for_old_cluster_deletion(&deletion_script_file_name);
253 : :
3000 254 : 8 : issue_warnings_and_set_wal_level();
255 : :
2937 peter_e@gmx.net 256 : 8 : pg_log(PG_REPORT,
257 : : "\n"
258 : : "Upgrade Complete\n"
259 : : "----------------");
260 : :
1762 magnus@hagander.net 261 : 8 : output_completion_banner(deletion_script_file_name);
262 : :
5596 bruce@momjian.us 263 : 8 : pg_free(deletion_script_file_name);
264 : :
1186 michael@paquier.xyz 265 : 8 : cleanup_output_dirs();
266 : :
5596 bruce@momjian.us 267 : 8 : 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
1308 michael@paquier.xyz 276 : 18 : make_outputdirs(char *pgdata)
277 : : {
278 : : FILE *fp;
279 : : char **filename;
280 : 18 : 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 : :
1186 287 : 18 : log_opts.rootdir = (char *) pg_malloc0(MAXPGPATH);
288 : 18 : len = snprintf(log_opts.rootdir, MAXPGPATH, "%s/%s", pgdata, BASE_OUTPUTDIR);
289 [ - + ]: 18 : if (len >= MAXPGPATH)
1152 tgl@sss.pgh.pa.us 290 :UBC 0 : pg_fatal("directory path for new cluster is too long");
291 : :
292 : : /* BASE_OUTPUTDIR/$timestamp/ */
1186 michael@paquier.xyz 293 :CBC 18 : gettimeofday(&time, NULL);
294 : 18 : tt = (time_t) time.tv_sec;
295 : 18 : strftime(timebuf, sizeof(timebuf), "%Y%m%dT%H%M%S", localtime(&tt));
296 : : /* append milliseconds */
297 : 18 : snprintf(timebuf + strlen(timebuf), sizeof(timebuf) - strlen(timebuf),
298 : 18 : ".%03d", (int) (time.tv_usec / 1000));
299 : 18 : log_opts.basedir = (char *) pg_malloc0(MAXPGPATH);
300 : 18 : len = snprintf(log_opts.basedir, MAXPGPATH, "%s/%s", log_opts.rootdir,
301 : : timebuf);
302 [ - + ]: 18 : if (len >= MAXPGPATH)
1152 tgl@sss.pgh.pa.us 303 :UBC 0 : pg_fatal("directory path for new cluster is too long");
304 : :
305 : : /* BASE_OUTPUTDIR/$timestamp/dump/ */
1186 michael@paquier.xyz 306 :CBC 18 : log_opts.dumpdir = (char *) pg_malloc0(MAXPGPATH);
307 : 18 : len = snprintf(log_opts.dumpdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
308 : : timebuf, DUMP_OUTPUTDIR);
309 [ - + ]: 18 : if (len >= MAXPGPATH)
1152 tgl@sss.pgh.pa.us 310 :UBC 0 : pg_fatal("directory path for new cluster is too long");
311 : :
312 : : /* BASE_OUTPUTDIR/$timestamp/log/ */
1186 michael@paquier.xyz 313 :CBC 18 : log_opts.logdir = (char *) pg_malloc0(MAXPGPATH);
314 : 18 : len = snprintf(log_opts.logdir, MAXPGPATH, "%s/%s/%s", log_opts.rootdir,
315 : : timebuf, LOG_OUTPUTDIR);
316 [ - + ]: 18 : if (len >= MAXPGPATH)
1152 tgl@sss.pgh.pa.us 317 :UBC 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 : : */
1186 michael@paquier.xyz 323 [ + + - + ]:CBC 18 : if (mkdir(log_opts.rootdir, pg_dir_create_mode) < 0 && errno != EEXIST)
1152 tgl@sss.pgh.pa.us 324 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.rootdir);
1186 michael@paquier.xyz 325 [ - + ]:CBC 18 : if (mkdir(log_opts.basedir, pg_dir_create_mode) < 0)
1152 tgl@sss.pgh.pa.us 326 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.basedir);
1186 michael@paquier.xyz 327 [ - + ]:CBC 18 : if (mkdir(log_opts.dumpdir, pg_dir_create_mode) < 0)
1152 tgl@sss.pgh.pa.us 328 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.dumpdir);
1186 michael@paquier.xyz 329 [ - + ]:CBC 18 : if (mkdir(log_opts.logdir, pg_dir_create_mode) < 0)
1152 tgl@sss.pgh.pa.us 330 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", log_opts.logdir);
331 : :
1181 tgl@sss.pgh.pa.us 332 :CBC 18 : len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
333 : : log_opts.logdir, INTERNAL_LOG_FILE);
334 [ - + ]: 18 : if (len >= sizeof(filename_path))
1152 tgl@sss.pgh.pa.us 335 :UBC 0 : pg_fatal("directory path for new cluster is too long");
336 : :
1308 michael@paquier.xyz 337 [ - + ]:CBC 18 : if ((log_opts.internal = fopen_priv(filename_path, "a")) == NULL)
1152 tgl@sss.pgh.pa.us 338 :UBC 0 : pg_fatal("could not open log file \"%s\": %m", filename_path);
339 : :
340 : : /* label start of upgrade in logfiles */
1308 michael@paquier.xyz 341 [ + + ]:CBC 72 : for (filename = output_files; *filename != NULL; filename++)
342 : : {
1181 tgl@sss.pgh.pa.us 343 : 54 : len = snprintf(filename_path, sizeof(filename_path), "%s/%s",
344 : : log_opts.logdir, *filename);
345 [ - + ]: 54 : if (len >= sizeof(filename_path))
1152 tgl@sss.pgh.pa.us 346 :UBC 0 : pg_fatal("directory path for new cluster is too long");
1308 michael@paquier.xyz 347 [ - + ]:CBC 54 : if ((fp = fopen_priv(filename_path, "a")) == NULL)
1152 tgl@sss.pgh.pa.us 348 :UBC 0 : pg_fatal("could not write to log file \"%s\": %m", filename_path);
349 : :
1181 tgl@sss.pgh.pa.us 350 :CBC 54 : fprintf(fp,
351 : : "-----------------------------------------------------------------\n"
352 : : " pg_upgrade run on %s"
353 : : "-----------------------------------------------------------------\n\n",
354 : : ctime(&run_time));
1308 michael@paquier.xyz 355 : 54 : fclose(fp);
356 : : }
357 : 18 : }
358 : :
359 : :
360 : : static void
407 nathan@postgresql.or 361 : 18 : 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 : : */
5227 bruce@momjian.us 367 : 18 : 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 : : */
2233 peter@eisentraut.org 374 [ - + ]: 18 : if (!new_cluster.bindir)
375 : : {
376 : : char exec_path[MAXPGPATH];
377 : :
2233 peter@eisentraut.org 378 [ # # ]:UBC 0 : if (find_my_exec(argv0, exec_path) < 0)
1152 tgl@sss.pgh.pa.us 379 : 0 : pg_fatal("%s: could not find own program executable", argv0);
380 : : /* Trim off program name and keep just path */
2233 peter@eisentraut.org 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 : :
5436 bruce@momjian.us 386 :CBC 18 : verify_directories();
387 : :
388 : : /* no postmasters should be running, except for a live check */
4608 389 [ - + ]: 17 : 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 : : */
4608 bruce@momjian.us 400 [ # # ]:UBC 0 : if (start_postmaster(&old_cluster, false))
401 : 0 : stop_postmaster(false);
402 : : else
403 : : {
404 [ # # ]: 0 : if (!user_opts.check)
4358 peter_e@gmx.net 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
407 nathan@postgresql.or 408 : 0 : user_opts.live_check = true;
409 : : }
410 : : }
411 : :
412 : : /* same goes for the new postmaster */
4608 bruce@momjian.us 413 [ - + ]:CBC 17 : if (pid_lock_file_exists(new_cluster.pgdata))
414 : : {
4608 bruce@momjian.us 415 [ # # ]:UBC 0 : if (start_postmaster(&new_cluster, false))
416 : 0 : stop_postmaster(false);
417 : : else
4358 peter_e@gmx.net 418 : 0 : pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
419 : : "Please shutdown that postmaster and try again.");
420 : : }
5596 bruce@momjian.us 421 :CBC 17 : }
422 : :
423 : : /*
424 : : * Set the new cluster's default char signedness using the old cluster's
425 : : * value.
426 : : */
427 : : static void
197 msawada@postgresql.o 428 : 8 : 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 [ - + ]: 8 : if (user_opts.char_signedness != -1)
197 msawada@postgresql.o 437 :UBC 0 : new_char_signedness = (user_opts.char_signedness == 1);
438 : : else
197 msawada@postgresql.o 439 :CBC 8 : new_char_signedness = old_cluster.controldata.default_char_signedness;
440 : :
441 : : /* Change the char signedness of the new cluster, if necessary */
442 [ + + ]: 8 : if (new_cluster.controldata.default_char_signedness != new_char_signedness)
443 : : {
444 : 1 : prep_status("Setting the default char signedness for new cluster");
445 : :
446 [ - + ]: 1 : 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 : 1 : check_ok();
453 : : }
454 : 8 : }
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
912 jdavis@postgresql.or 464 : 8 : set_locale_and_encoding(void)
465 : : {
466 : : PGconn *conn_new_template1;
467 : : char *datcollate_literal;
468 : : char *datctype_literal;
546 469 : 8 : char *datlocale_literal = NULL;
912 470 : 8 : DbLocaleInfo *locale = old_cluster.template0;
471 : :
472 : 8 : prep_status("Setting locale and encoding for new cluster");
473 : :
474 : : /* escape literals with respect to new cluster */
475 : 8 : conn_new_template1 = connectToServer(&new_cluster, "template1");
476 : :
477 : 8 : datcollate_literal = PQescapeLiteral(conn_new_template1,
478 : 8 : locale->db_collate,
479 : 8 : strlen(locale->db_collate));
480 : 8 : datctype_literal = PQescapeLiteral(conn_new_template1,
481 : 8 : locale->db_ctype,
482 : 8 : strlen(locale->db_ctype));
483 : :
156 484 [ + + ]: 8 : if (locale->db_locale)
485 : 1 : datlocale_literal = PQescapeLiteral(conn_new_template1,
486 : 1 : locale->db_locale,
487 : 1 : strlen(locale->db_locale));
488 : : else
489 : 7 : datlocale_literal = "NULL";
490 : :
491 : : /* update template0 in new cluster */
546 492 [ + - ]: 8 : if (GET_MAJOR_VERSION(new_cluster.major_version) >= 1700)
493 : 8 : 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 : 8 : locale->db_collprovider,
503 : : datcollate_literal,
504 : : datctype_literal,
505 : : datlocale_literal));
546 jdavis@postgresql.or 506 [ # # ]:UBC 0 : else if (GET_MAJOR_VERSION(new_cluster.major_version) >= 1500)
912 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 : :
912 jdavis@postgresql.or 531 :CBC 8 : PQfreemem(datcollate_literal);
532 : 8 : PQfreemem(datctype_literal);
156 533 [ + + ]: 8 : if (locale->db_locale)
534 : 1 : PQfreemem(datlocale_literal);
535 : :
912 536 : 8 : PQfinish(conn_new_template1);
537 : :
538 : 8 : check_ok();
539 : 8 : }
540 : :
541 : :
542 : : static void
5436 bruce@momjian.us 543 : 8 : 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 : 8 : prep_status("Analyzing all rows in the new cluster");
2798 551 : 8 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
552 : : "\"%s/vacuumdb\" %s --all --analyze %s",
553 : : new_cluster.bindir, cluster_conn_opts(&new_cluster),
4758 alvherre@alvh.no-ip. 554 [ - + ]: 8 : log_opts.verbose ? "--verbose" : "");
5436 bruce@momjian.us 555 : 8 : 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 : : */
2919 peter_e@gmx.net 563 : 8 : prep_status("Freezing all rows in the new cluster");
2798 bruce@momjian.us 564 : 8 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
565 : : "\"%s/vacuumdb\" %s --all --freeze %s",
566 : : new_cluster.bindir, cluster_conn_opts(&new_cluster),
4758 alvherre@alvh.no-ip. 567 [ - + ]: 8 : log_opts.verbose ? "--verbose" : "");
5436 bruce@momjian.us 568 : 8 : check_ok();
5596 569 : 8 : }
570 : :
571 : :
572 : : static void
2784 tgl@sss.pgh.pa.us 573 : 8 : prepare_new_globals(void)
574 : : {
575 : : /*
576 : : * Before we restore anything, set frozenxids of initdb-created tables.
577 : : */
4084 bruce@momjian.us 578 : 8 : set_frozenxids(false);
579 : :
580 : : /*
581 : : * Now restore global objects (roles and tablespaces).
582 : : */
4663 583 : 8 : prep_status("Restoring global objects in the new cluster");
584 : :
2798 585 : 8 : 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);
5436 590 : 8 : check_ok();
5596 591 : 8 : }
592 : :
593 : :
594 : : static void
5436 595 : 8 : create_new_objects(void)
596 : : {
597 : : int dbnum;
598 : : PGconn *conn_new_template1;
599 : :
1293 andres@anarazel.de 600 : 8 : 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 : : */
425 nathan@postgresql.or 609 : 8 : conn_new_template1 = connectToServer(&new_cluster, "template1");
610 : 8 : PQclear(executeQueryOrDie(conn_new_template1, "CHECKPOINT"));
611 : 8 : 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 : : */
4663 bruce@momjian.us 618 [ + - ]: 8 : for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
619 : : {
620 : : char sql_file_name[MAXPGPATH],
621 : : log_file_name[MAXPGPATH];
4483 622 : 8 : DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
623 : : const char *create_opts;
624 : :
625 : : /* Process only template1 in this pass */
2750 tgl@sss.pgh.pa.us 626 [ - + ]: 8 : if (strcmp(old_db->db_name, "template1") != 0)
2750 tgl@sss.pgh.pa.us 627 :UBC 0 : continue;
628 : :
4656 bruce@momjian.us 629 :CBC 8 : pg_log(PG_STATUS, "%s", old_db->db_name);
4637 630 : 8 : snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
631 : 8 : 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 : : */
2750 tgl@sss.pgh.pa.us 638 : 8 : create_opts = "--clean --create";
639 : :
640 : 8 : 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 : 8 : break; /* done once we've processed template1 */
655 : : }
656 : :
657 [ + + ]: 36 : for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
658 : : {
659 : : char sql_file_name[MAXPGPATH],
660 : : log_file_name[MAXPGPATH];
661 : 28 : DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
662 : : const char *create_opts;
663 : : int txn_size;
664 : :
665 : : /* Skip template1 in this pass */
2784 666 [ + + ]: 28 : if (strcmp(old_db->db_name, "template1") == 0)
2750 667 : 8 : continue;
668 : :
669 : 20 : pg_log(PG_STATUS, "%s", old_db->db_name);
670 : 20 : snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
671 : 20 : 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 [ + + ]: 20 : if (strcmp(old_db->db_name, "postgres") == 0)
679 : 8 : create_opts = "--clean --create";
680 : : else
681 : 12 : 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 : : */
523 688 : 20 : txn_size = RESTORE_TRANSACTION_SIZE;
689 [ - + ]: 20 : if (user_opts.jobs > 1)
690 : : {
523 tgl@sss.pgh.pa.us 691 :UBC 0 : txn_size /= user_opts.jobs;
692 : : /* Keep some sanity if -j is huge */
693 : 0 : txn_size = Max(txn_size, 10);
694 : : }
695 : :
4480 sfrost@snowman.net 696 :CBC 20 : 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 */
4637 bruce@momjian.us 710 [ - + ]: 8 : while (reap_child(true) == true)
711 : : ;
712 : :
4663 713 : 8 : end_progress_output();
5436 714 : 8 : 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 : : */
1796 720 [ - + ]: 8 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 902)
4084 bruce@momjian.us 721 :UBC 0 : set_frozenxids(true);
722 : :
723 : : /* update new_cluster info now that we have objects in the databases */
407 nathan@postgresql.or 724 :CBC 8 : get_db_rel_and_slot_infos(&new_cluster);
5596 bruce@momjian.us 725 : 8 : }
726 : :
727 : : /*
728 : : * Delete the given subdirectory contents from the new cluster
729 : : */
730 : : static void
2867 peter_e@gmx.net 731 : 24 : remove_new_subdir(const char *subdir, bool rmtopdir)
732 : : {
733 : : char new_path[MAXPGPATH];
734 : :
4811 alvherre@alvh.no-ip. 735 : 24 : prep_status("Deleting files from new %s", subdir);
736 : :
737 : 24 : snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
4092 bruce@momjian.us 738 [ - + ]: 24 : if (!rmtree(new_path, rmtopdir))
1152 tgl@sss.pgh.pa.us 739 :UBC 0 : pg_fatal("could not delete directory \"%s\"", new_path);
740 : :
5436 bruce@momjian.us 741 :CBC 24 : check_ok();
4092 742 : 24 : }
743 : :
744 : : /*
745 : : * Copy the files from the old cluster into it
746 : : */
747 : : static void
2867 peter_e@gmx.net 748 : 24 : copy_subdir_files(const char *old_subdir, const char *new_subdir)
749 : : {
750 : : char old_path[MAXPGPATH];
751 : : char new_path[MAXPGPATH];
752 : :
3095 rhaas@postgresql.org 753 : 24 : remove_new_subdir(new_subdir, true);
754 : :
755 : 24 : snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
756 : 24 : snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
757 : :
758 : 24 : prep_status("Copying old %s to new server", old_subdir);
759 : :
2798 bruce@momjian.us 760 : 24 : 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 : :
5436 769 : 24 : check_ok();
4811 alvherre@alvh.no-ip. 770 : 24 : }
771 : :
772 : : static void
3095 rhaas@postgresql.org 773 : 8 : 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 : : */
1796 bruce@momjian.us 779 [ - + ]: 8 : copy_subdir_files(GET_MAJOR_VERSION(old_cluster.major_version) <= 906 ?
780 : : "pg_clog" : "pg_xact",
781 [ - + ]: 8 : GET_MAJOR_VERSION(new_cluster.major_version) <= 906 ?
782 : : "pg_clog" : "pg_xact");
783 : :
1503 784 : 8 : prep_status("Setting oldest XID for new cluster");
785 : 8 : 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 : 8 : check_ok();
790 : :
791 : : /* set the next transaction id and epoch of the new cluster */
4019 792 : 8 : prep_status("Setting next transaction ID and epoch for new cluster");
2798 793 : 8 : 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 : 8 : 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 : 8 : 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);
5436 808 : 8 : 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 : : */
4609 alvherre@alvh.no-ip. 816 [ + - ]: 8 : if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
817 [ + - ]: 8 : new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
818 : : {
3095 rhaas@postgresql.org 819 : 8 : copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
820 : 8 : copy_subdir_files("pg_multixact/members", "pg_multixact/members");
821 : :
4609 alvherre@alvh.no-ip. 822 : 8 : 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 : : */
2798 bruce@momjian.us 828 : 8 : 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);
4609 alvherre@alvh.no-ip. 835 : 8 : check_ok();
836 : : }
4609 alvherre@alvh.no-ip. 837 [ # # ]:UBC 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 : : */
4092 bruce@momjian.us 844 : 0 : remove_new_subdir("pg_multixact/offsets", false);
845 : :
2919 peter_e@gmx.net 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 : : */
2798 bruce@momjian.us 856 : 0 : exec_prog(UTILITY_LOG_FILE, NULL, true, true,
857 : : "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
858 : : new_cluster.bindir,
4609 alvherre@alvh.no-ip. 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 */
5436 bruce@momjian.us 866 :CBC 8 : prep_status("Resetting WAL archives");
2798 867 : 8 : 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);
5436 872 : 8 : check_ok();
5596 873 : 8 : }
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
4084 898 : 8 : 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 [ + - ]: 8 : if (!minmxid_only)
909 : 8 : prep_status("Setting frozenxid and minmxid counters in new cluster");
910 : : else
4084 bruce@momjian.us 911 :UBC 0 : prep_status("Setting minmxid counter in new cluster");
912 : :
5362 bruce@momjian.us 913 :CBC 8 : conn_template1 = connectToServer(&new_cluster, "template1");
914 : :
4084 915 [ + - ]: 8 : if (!minmxid_only)
916 : : /* set pg_database.datfrozenxid */
917 : 8 : 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 */
5436 923 : 8 : 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 : 8 : dbres = executeQueryOrDie(conn_template1,
930 : : "SELECT datname, datallowconn "
931 : : "FROM pg_catalog.pg_database");
932 : :
5589 933 : 8 : i_datname = PQfnumber(dbres, "datname");
934 : 8 : i_datallowconn = PQfnumber(dbres, "datallowconn");
935 : :
5596 936 : 8 : ntups = PQntuples(dbres);
937 [ + + ]: 32 : for (dbnum = 0; dbnum < ntups; dbnum++)
938 : : {
5541 939 : 24 : char *datname = PQgetvalue(dbres, dbnum, i_datname);
940 : 24 : 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 : : */
5589 949 [ + + ]: 24 : if (strcmp(datallowconn, "f") == 0)
5436 950 : 8 : PQclear(executeQueryOrDie(conn_template1,
951 : : "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
952 : : quote_identifier(datname)));
953 : :
5362 954 : 24 : conn = connectToServer(&new_cluster, datname);
955 : :
4084 956 [ + - ]: 24 : if (!minmxid_only)
957 : : /* set pg_class.relfrozenxid */
958 : 24 : 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 */
5436 969 : 24 : 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));
5596 978 : 24 : PQfinish(conn);
979 : :
980 : : /* Reset datallowconn flag */
5589 981 [ + + ]: 24 : if (strcmp(datallowconn, "f") == 0)
5436 982 : 8 : PQclear(executeQueryOrDie(conn_template1,
983 : : "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
984 : : quote_identifier(datname)));
985 : : }
986 : :
5596 987 : 8 : PQclear(dbres);
988 : :
5589 989 : 8 : PQfinish(conn_template1);
990 : :
5436 991 : 8 : check_ok();
5596 992 : 8 : }
993 : :
994 : : /*
995 : : * create_logical_replication_slots()
996 : : *
997 : : * Similar to create_new_objects() but only restores logical replication slots.
998 : : */
999 : : static void
681 akapila@postgresql.o 1000 : 1 : create_logical_replication_slots(void)
1001 : : {
1002 : 1 : prep_status_progress("Restoring logical replication slots in the new cluster");
1003 : :
1004 [ + + ]: 3 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1005 : : {
1006 : 2 : DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
1007 : 2 : LogicalSlotInfoArr *slot_arr = &old_db->slot_arr;
1008 : : PGconn *conn;
1009 : : PQExpBuffer query;
1010 : :
1011 : : /* Skip this database if there are no slots */
1012 [ + + ]: 2 : if (slot_arr->nslots == 0)
1013 : 1 : continue;
1014 : :
1015 : 1 : conn = connectToServer(&new_cluster, old_db->db_name);
1016 : 1 : query = createPQExpBuffer();
1017 : :
1018 : 1 : pg_log(PG_STATUS, "%s", old_db->db_name);
1019 : :
1020 [ + + ]: 2 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
1021 : : {
1022 : 1 : LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
1023 : :
1024 : : /* Constructs a query for creating logical replication slots */
141 drowley@postgresql.o 1025 : 1 : appendPQExpBufferStr(query,
1026 : : "SELECT * FROM "
1027 : : "pg_catalog.pg_create_logical_replication_slot(");
681 akapila@postgresql.o 1028 : 1 : appendStringLiteralConn(query, slot_info->slotname, conn);
141 drowley@postgresql.o 1029 : 1 : appendPQExpBufferStr(query, ", ");
681 akapila@postgresql.o 1030 : 1 : appendStringLiteralConn(query, slot_info->plugin, conn);
1031 : :
590 1032 : 2 : appendPQExpBuffer(query, ", false, %s, %s);",
1033 [ + - ]: 1 : slot_info->two_phase ? "true" : "false",
1034 [ + - ]: 1 : slot_info->failover ? "true" : "false");
1035 : :
681 1036 : 1 : PQclear(executeQueryOrDie(conn, "%s", query->data));
1037 : :
1038 : 1 : resetPQExpBuffer(query);
1039 : : }
1040 : :
1041 : 1 : PQfinish(conn);
1042 : :
1043 : 1 : destroyPQExpBuffer(query);
1044 : : }
1045 : :
1046 : 1 : end_progress_output();
1047 : 1 : check_ok();
1048 : :
1049 : 1 : 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
45 akapila@postgresql.o 1060 :GNC 1 : create_conflict_detection_slot(void)
1061 : : {
1062 : : PGconn *conn_new_template1;
1063 : :
1064 : 1 : prep_status("Creating the replication conflict detection slot");
1065 : :
1066 : 1 : conn_new_template1 = connectToServer(&new_cluster, "template1");
1067 : 1 : PQclear(executeQueryOrDie(conn_new_template1, "SELECT pg_catalog.binary_upgrade_create_conflict_detection_slot()"));
1068 : 1 : PQfinish(conn_new_template1);
1069 : :
1070 : 1 : check_ok();
1071 : 1 : }
|