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