1 /*
2  *	pg_upgrade.c
3  *
4  *	main source file
5  *
6  *	Copyright (c) 2010-2018, 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
19  *	in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
20  *	FULL.  In the new cluster, pg_class.oid and pg_class.relfilenode will
21  *	be the same and will match the old pg_class.oid value.  Because of
22  *	this, old/new pg_class.relfilenode values will not match if CLUSTER,
23  *	REINDEX, or VACUUM FULL have been performed in the old cluster.
24  *
25  *	We control all assignments of pg_type.oid because these oids are stored
26  *	in user composite type values.
27  *
28  *	We control all assignments of pg_enum.oid because these oids are stored
29  *	in user tables as enum values.
30  *
31  *	We control all assignments of pg_authid.oid because these oids are stored
32  *	in pg_largeobject_metadata.
33  */
34 
35 
36 
37 #include "postgres_fe.h"
38 
39 #include "pg_upgrade.h"
40 #include "catalog/pg_class_d.h"
41 #include "common/file_perm.h"
42 #include "common/restricted_token.h"
43 #include "fe_utils/string_utils.h"
44 
45 #ifdef HAVE_LANGINFO_H
46 #include <langinfo.h>
47 #endif
48 
49 static void prepare_new_cluster(void);
50 static void prepare_new_globals(void);
51 static void create_new_objects(void);
52 static void copy_xact_xlog_xid(void);
53 static void set_frozenxids(bool minmxid_only);
54 static void setup(char *argv0, bool *live_check);
55 static void cleanup(void);
56 
57 ClusterInfo old_cluster,
58 			new_cluster;
59 OSInfo		os_info;
60 
61 char	   *output_files[] = {
62 	SERVER_LOG_FILE,
63 #ifdef WIN32
64 	/* unique file for pg_ctl start */
65 	SERVER_START_LOG_FILE,
66 #endif
67 	UTILITY_LOG_FILE,
68 	INTERNAL_LOG_FILE,
69 	NULL
70 };
71 
72 
73 int
main(int argc,char ** argv)74 main(int argc, char **argv)
75 {
76 	char	   *analyze_script_file_name = NULL;
77 	char	   *deletion_script_file_name = NULL;
78 	bool		live_check = false;
79 
80 	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_upgrade"));
81 
82 	/* Set default restrictive mask until new cluster permissions are read */
83 	umask(PG_MODE_MASK_OWNER);
84 
85 	parseCommandLine(argc, argv);
86 
87 	get_restricted_token(os_info.progname);
88 
89 	adjust_data_dir(&old_cluster);
90 	adjust_data_dir(&new_cluster);
91 
92 	setup(argv[0], &live_check);
93 
94 	output_check_banner(live_check);
95 
96 	check_cluster_versions();
97 
98 	get_sock_dir(&old_cluster, live_check);
99 	get_sock_dir(&new_cluster, false);
100 
101 	check_cluster_compatibility(live_check);
102 
103 	/* Set mask based on PGDATA permissions */
104 	if (!GetDataDirectoryCreatePerm(new_cluster.pgdata))
105 	{
106 		pg_log(PG_FATAL, "could not read permissions of directory \"%s\": %s\n",
107 			   new_cluster.pgdata, strerror(errno));
108 		exit(1);
109 	}
110 
111 	umask(pg_mode_mask);
112 
113 	check_and_dump_old_cluster(live_check);
114 
115 
116 	/* -- NEW -- */
117 	start_postmaster(&new_cluster, true);
118 
119 	check_new_cluster();
120 	report_clusters_compatible();
121 
122 	pg_log(PG_REPORT,
123 		   "\n"
124 		   "Performing Upgrade\n"
125 		   "------------------\n");
126 
127 	prepare_new_cluster();
128 
129 	stop_postmaster(false);
130 
131 	/*
132 	 * Destructive Changes to New Cluster
133 	 */
134 
135 	copy_xact_xlog_xid();
136 
137 	/* New now using xids of the old system */
138 
139 	/* -- NEW -- */
140 	start_postmaster(&new_cluster, true);
141 
142 	prepare_new_globals();
143 
144 	create_new_objects();
145 
146 	stop_postmaster(false);
147 
148 	/*
149 	 * Most failures happen in create_new_objects(), which has completed at
150 	 * this point.  We do this here because it is just before linking, which
151 	 * will link the old and new cluster data files, preventing the old
152 	 * cluster from being safely started once the new cluster is started.
153 	 */
154 	if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
155 		disable_old_cluster();
156 
157 	transfer_all_new_tablespaces(&old_cluster.dbarr, &new_cluster.dbarr,
158 								 old_cluster.pgdata, new_cluster.pgdata);
159 
160 	/*
161 	 * Assuming OIDs are only used in system tables, there is no need to
162 	 * restore the OID counter because we have not transferred any OIDs from
163 	 * the old system, but we do it anyway just in case.  We do it late here
164 	 * because there is no need to have the schema load use new oids.
165 	 */
166 	prep_status("Setting next OID for new cluster");
167 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
168 			  "\"%s/pg_resetwal\" -o %u \"%s\"",
169 			  new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid,
170 			  new_cluster.pgdata);
171 	check_ok();
172 
173 	prep_status("Sync data directory to disk");
174 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
175 			  "\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
176 			  new_cluster.pgdata);
177 	check_ok();
178 
179 	create_script_for_cluster_analyze(&analyze_script_file_name);
180 	create_script_for_old_cluster_deletion(&deletion_script_file_name);
181 
182 	issue_warnings_and_set_wal_level();
183 
184 	pg_log(PG_REPORT,
185 		   "\n"
186 		   "Upgrade Complete\n"
187 		   "----------------\n");
188 
189 	output_completion_banner(analyze_script_file_name,
190 							 deletion_script_file_name);
191 
192 	pg_free(analyze_script_file_name);
193 	pg_free(deletion_script_file_name);
194 
195 	cleanup();
196 
197 	return 0;
198 }
199 
200 
201 static void
setup(char * argv0,bool * live_check)202 setup(char *argv0, bool *live_check)
203 {
204 	char		exec_path[MAXPGPATH];	/* full path to my executable */
205 
206 	/*
207 	 * make sure the user has a clean environment, otherwise, we may confuse
208 	 * libpq when we connect to one (or both) of the servers.
209 	 */
210 	check_pghost_envvar();
211 
212 	verify_directories();
213 
214 	/* no postmasters should be running, except for a live check */
215 	if (pid_lock_file_exists(old_cluster.pgdata))
216 	{
217 		/*
218 		 * If we have a postmaster.pid file, try to start the server.  If it
219 		 * starts, the pid file was stale, so stop the server.  If it doesn't
220 		 * start, assume the server is running.  If the pid file is left over
221 		 * from a server crash, this also allows any committed transactions
222 		 * stored in the WAL to be replayed so they are not lost, because WAL
223 		 * files are not transferred from old to new servers.  We later check
224 		 * for a clean shutdown.
225 		 */
226 		if (start_postmaster(&old_cluster, false))
227 			stop_postmaster(false);
228 		else
229 		{
230 			if (!user_opts.check)
231 				pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
232 						 "Please shutdown that postmaster and try again.\n");
233 			else
234 				*live_check = true;
235 		}
236 	}
237 
238 	/* same goes for the new postmaster */
239 	if (pid_lock_file_exists(new_cluster.pgdata))
240 	{
241 		if (start_postmaster(&new_cluster, false))
242 			stop_postmaster(false);
243 		else
244 			pg_fatal("There seems to be a postmaster servicing the new cluster.\n"
245 					 "Please shutdown that postmaster and try again.\n");
246 	}
247 
248 	/* get path to pg_upgrade executable */
249 	if (find_my_exec(argv0, exec_path) < 0)
250 		pg_fatal("%s: could not find own program executable\n", argv0);
251 
252 	/* Trim off program name and keep just path */
253 	*last_dir_separator(exec_path) = '\0';
254 	canonicalize_path(exec_path);
255 	os_info.exec_path = pg_strdup(exec_path);
256 }
257 
258 
259 static void
prepare_new_cluster(void)260 prepare_new_cluster(void)
261 {
262 	/*
263 	 * It would make more sense to freeze after loading the schema, but that
264 	 * would cause us to lose the frozenids restored by the load. We use
265 	 * --analyze so autovacuum doesn't update statistics later
266 	 */
267 	prep_status("Analyzing all rows in the new cluster");
268 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
269 			  "\"%s/vacuumdb\" %s --all --analyze %s",
270 			  new_cluster.bindir, cluster_conn_opts(&new_cluster),
271 			  log_opts.verbose ? "--verbose" : "");
272 	check_ok();
273 
274 	/*
275 	 * We do freeze after analyze so pg_statistic is also frozen. template0 is
276 	 * not frozen here, but data rows were frozen by initdb, and we set its
277 	 * datfrozenxid, relfrozenxids, and relminmxid later to match the new xid
278 	 * counter later.
279 	 */
280 	prep_status("Freezing all rows in the new cluster");
281 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
282 			  "\"%s/vacuumdb\" %s --all --freeze %s",
283 			  new_cluster.bindir, cluster_conn_opts(&new_cluster),
284 			  log_opts.verbose ? "--verbose" : "");
285 	check_ok();
286 }
287 
288 
289 static void
prepare_new_globals(void)290 prepare_new_globals(void)
291 {
292 	/*
293 	 * Before we restore anything, set frozenxids of initdb-created tables.
294 	 */
295 	set_frozenxids(false);
296 
297 	/*
298 	 * Now restore global objects (roles and tablespaces).
299 	 */
300 	prep_status("Restoring global objects in the new cluster");
301 
302 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
303 			  "\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
304 			  new_cluster.bindir, cluster_conn_opts(&new_cluster),
305 			  GLOBALS_DUMP_FILE);
306 	check_ok();
307 }
308 
309 
310 static void
create_new_objects(void)311 create_new_objects(void)
312 {
313 	int			dbnum;
314 
315 	prep_status("Restoring database schemas in the new cluster\n");
316 
317 	/*
318 	 * We cannot process the template1 database concurrently with others,
319 	 * because when it's transiently dropped, connection attempts would fail.
320 	 * So handle it in a separate non-parallelized pass.
321 	 */
322 	for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
323 	{
324 		char		sql_file_name[MAXPGPATH],
325 					log_file_name[MAXPGPATH];
326 		DbInfo	   *old_db = &old_cluster.dbarr.dbs[dbnum];
327 		const char *create_opts;
328 
329 		/* Process only template1 in this pass */
330 		if (strcmp(old_db->db_name, "template1") != 0)
331 			continue;
332 
333 		pg_log(PG_STATUS, "%s", old_db->db_name);
334 		snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
335 		snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
336 
337 		/*
338 		 * template1 and postgres databases will already exist in the target
339 		 * installation, so tell pg_restore to drop and recreate them;
340 		 * otherwise we would fail to propagate their database-level
341 		 * properties.
342 		 */
343 		create_opts = "--clean --create";
344 
345 		exec_prog(log_file_name,
346 				  NULL,
347 				  true,
348 				  true,
349 				  "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
350 				  "--dbname postgres \"%s\"",
351 				  new_cluster.bindir,
352 				  cluster_conn_opts(&new_cluster),
353 				  create_opts,
354 				  sql_file_name);
355 
356 		break;					/* done once we've processed template1 */
357 	}
358 
359 	for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
360 	{
361 		char		sql_file_name[MAXPGPATH],
362 					log_file_name[MAXPGPATH];
363 		DbInfo	   *old_db = &old_cluster.dbarr.dbs[dbnum];
364 		const char *create_opts;
365 
366 		/* Skip template1 in this pass */
367 		if (strcmp(old_db->db_name, "template1") == 0)
368 			continue;
369 
370 		pg_log(PG_STATUS, "%s", old_db->db_name);
371 		snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
372 		snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
373 
374 		/*
375 		 * template1 and postgres databases will already exist in the target
376 		 * installation, so tell pg_restore to drop and recreate them;
377 		 * otherwise we would fail to propagate their database-level
378 		 * properties.
379 		 */
380 		if (strcmp(old_db->db_name, "postgres") == 0)
381 			create_opts = "--clean --create";
382 		else
383 			create_opts = "--create";
384 
385 		parallel_exec_prog(log_file_name,
386 						   NULL,
387 						   "\"%s/pg_restore\" %s %s --exit-on-error --verbose "
388 						   "--dbname template1 \"%s\"",
389 						   new_cluster.bindir,
390 						   cluster_conn_opts(&new_cluster),
391 						   create_opts,
392 						   sql_file_name);
393 	}
394 
395 	/* reap all children */
396 	while (reap_child(true) == true)
397 		;
398 
399 	end_progress_output();
400 	check_ok();
401 
402 	/*
403 	 * We don't have minmxids for databases or relations in pre-9.3 clusters,
404 	 * so set those after we have restored the schema.
405 	 */
406 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 902)
407 		set_frozenxids(true);
408 
409 	/* update new_cluster info now that we have objects in the databases */
410 	get_db_and_rel_infos(&new_cluster);
411 }
412 
413 /*
414  * Delete the given subdirectory contents from the new cluster
415  */
416 static void
remove_new_subdir(const char * subdir,bool rmtopdir)417 remove_new_subdir(const char *subdir, bool rmtopdir)
418 {
419 	char		new_path[MAXPGPATH];
420 
421 	prep_status("Deleting files from new %s", subdir);
422 
423 	snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
424 	if (!rmtree(new_path, rmtopdir))
425 		pg_fatal("could not delete directory \"%s\"\n", new_path);
426 
427 	check_ok();
428 }
429 
430 /*
431  * Copy the files from the old cluster into it
432  */
433 static void
copy_subdir_files(const char * old_subdir,const char * new_subdir)434 copy_subdir_files(const char *old_subdir, const char *new_subdir)
435 {
436 	char		old_path[MAXPGPATH];
437 	char		new_path[MAXPGPATH];
438 
439 	remove_new_subdir(new_subdir, true);
440 
441 	snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, old_subdir);
442 	snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, new_subdir);
443 
444 	prep_status("Copying old %s to new server", old_subdir);
445 
446 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
447 #ifndef WIN32
448 			  "cp -Rf \"%s\" \"%s\"",
449 #else
450 	/* flags: everything, no confirm, quiet, overwrite read-only */
451 			  "xcopy /e /y /q /r \"%s\" \"%s\\\"",
452 #endif
453 			  old_path, new_path);
454 
455 	check_ok();
456 }
457 
458 static void
copy_xact_xlog_xid(void)459 copy_xact_xlog_xid(void)
460 {
461 	/*
462 	 * Copy old commit logs to new data dir. pg_clog has been renamed to
463 	 * pg_xact in post-10 clusters.
464 	 */
465 	copy_subdir_files(GET_MAJOR_VERSION(old_cluster.major_version) <= 906 ?
466 					  "pg_clog" : "pg_xact",
467 					  GET_MAJOR_VERSION(new_cluster.major_version) <= 906 ?
468 					  "pg_clog" : "pg_xact");
469 
470 	prep_status("Setting oldest XID for new cluster");
471 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
472 			  "\"%s/pg_resetwal\" -f -u %u \"%s\"",
473 			  new_cluster.bindir, old_cluster.controldata.chkpnt_oldstxid,
474 			  new_cluster.pgdata);
475 	check_ok();
476 
477 	/* set the next transaction id and epoch of the new cluster */
478 	prep_status("Setting next transaction ID and epoch for new cluster");
479 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
480 			  "\"%s/pg_resetwal\" -f -x %u \"%s\"",
481 			  new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid,
482 			  new_cluster.pgdata);
483 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
484 			  "\"%s/pg_resetwal\" -f -e %u \"%s\"",
485 			  new_cluster.bindir, old_cluster.controldata.chkpnt_nxtepoch,
486 			  new_cluster.pgdata);
487 	/* must reset commit timestamp limits also */
488 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
489 			  "\"%s/pg_resetwal\" -f -c %u,%u \"%s\"",
490 			  new_cluster.bindir,
491 			  old_cluster.controldata.chkpnt_nxtxid,
492 			  old_cluster.controldata.chkpnt_nxtxid,
493 			  new_cluster.pgdata);
494 	check_ok();
495 
496 	/*
497 	 * If the old server is before the MULTIXACT_FORMATCHANGE_CAT_VER change
498 	 * (see pg_upgrade.h) and the new server is after, then we don't copy
499 	 * pg_multixact files, but we need to reset pg_control so that the new
500 	 * server doesn't attempt to read multis older than the cutoff value.
501 	 */
502 	if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
503 		new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
504 	{
505 		copy_subdir_files("pg_multixact/offsets", "pg_multixact/offsets");
506 		copy_subdir_files("pg_multixact/members", "pg_multixact/members");
507 
508 		prep_status("Setting next multixact ID and offset for new cluster");
509 
510 		/*
511 		 * we preserve all files and contents, so we must preserve both "next"
512 		 * counters here and the oldest multi present on system.
513 		 */
514 		exec_prog(UTILITY_LOG_FILE, NULL, true, true,
515 				  "\"%s/pg_resetwal\" -O %u -m %u,%u \"%s\"",
516 				  new_cluster.bindir,
517 				  old_cluster.controldata.chkpnt_nxtmxoff,
518 				  old_cluster.controldata.chkpnt_nxtmulti,
519 				  old_cluster.controldata.chkpnt_oldstMulti,
520 				  new_cluster.pgdata);
521 		check_ok();
522 	}
523 	else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
524 	{
525 		/*
526 		 * Remove offsets/0000 file created by initdb that no longer matches
527 		 * the new multi-xid value.  "members" starts at zero so no need to
528 		 * remove it.
529 		 */
530 		remove_new_subdir("pg_multixact/offsets", false);
531 
532 		prep_status("Setting oldest multixact ID in new cluster");
533 
534 		/*
535 		 * We don't preserve files in this case, but it's important that the
536 		 * oldest multi is set to the latest value used by the old system, so
537 		 * that multixact.c returns the empty set for multis that might be
538 		 * present on disk.  We set next multi to the value following that; it
539 		 * might end up wrapped around (i.e. 0) if the old cluster had
540 		 * next=MaxMultiXactId, but multixact.c can cope with that just fine.
541 		 */
542 		exec_prog(UTILITY_LOG_FILE, NULL, true, true,
543 				  "\"%s/pg_resetwal\" -m %u,%u \"%s\"",
544 				  new_cluster.bindir,
545 				  old_cluster.controldata.chkpnt_nxtmulti + 1,
546 				  old_cluster.controldata.chkpnt_nxtmulti,
547 				  new_cluster.pgdata);
548 		check_ok();
549 	}
550 
551 	/* now reset the wal archives in the new cluster */
552 	prep_status("Resetting WAL archives");
553 	exec_prog(UTILITY_LOG_FILE, NULL, true, true,
554 	/* use timeline 1 to match controldata and no WAL history file */
555 			  "\"%s/pg_resetwal\" -l 00000001%s \"%s\"", new_cluster.bindir,
556 			  old_cluster.controldata.nextxlogfile + 8,
557 			  new_cluster.pgdata);
558 	check_ok();
559 }
560 
561 
562 /*
563  *	set_frozenxids()
564  *
565  * This is called on the new cluster before we restore anything, with
566  * minmxid_only = false.  Its purpose is to ensure that all initdb-created
567  * vacuumable tables have relfrozenxid/relminmxid matching the old cluster's
568  * xid/mxid counters.  We also initialize the datfrozenxid/datminmxid of the
569  * built-in databases to match.
570  *
571  * As we create user tables later, their relfrozenxid/relminmxid fields will
572  * be restored properly by the binary-upgrade restore script.  Likewise for
573  * user-database datfrozenxid/datminmxid.  However, if we're upgrading from a
574  * pre-9.3 database, which does not store per-table or per-DB minmxid, then
575  * the relminmxid/datminmxid values filled in by the restore script will just
576  * be zeroes.
577  *
578  * Hence, with a pre-9.3 source database, a second call occurs after
579  * everything is restored, with minmxid_only = true.  This pass will
580  * initialize all tables and databases, both those made by initdb and user
581  * objects, with the desired minmxid value.  frozenxid values are left alone.
582  */
583 static void
set_frozenxids(bool minmxid_only)584 set_frozenxids(bool minmxid_only)
585 {
586 	int			dbnum;
587 	PGconn	   *conn,
588 			   *conn_template1;
589 	PGresult   *dbres;
590 	int			ntups;
591 	int			i_datname;
592 	int			i_datallowconn;
593 
594 	if (!minmxid_only)
595 		prep_status("Setting frozenxid and minmxid counters in new cluster");
596 	else
597 		prep_status("Setting minmxid counter in new cluster");
598 
599 	conn_template1 = connectToServer(&new_cluster, "template1");
600 
601 	if (!minmxid_only)
602 		/* set pg_database.datfrozenxid */
603 		PQclear(executeQueryOrDie(conn_template1,
604 								  "UPDATE pg_catalog.pg_database "
605 								  "SET	datfrozenxid = '%u'",
606 								  old_cluster.controldata.chkpnt_nxtxid));
607 
608 	/* set pg_database.datminmxid */
609 	PQclear(executeQueryOrDie(conn_template1,
610 							  "UPDATE pg_catalog.pg_database "
611 							  "SET	datminmxid = '%u'",
612 							  old_cluster.controldata.chkpnt_nxtmulti));
613 
614 	/* get database names */
615 	dbres = executeQueryOrDie(conn_template1,
616 							  "SELECT	datname, datallowconn "
617 							  "FROM	pg_catalog.pg_database");
618 
619 	i_datname = PQfnumber(dbres, "datname");
620 	i_datallowconn = PQfnumber(dbres, "datallowconn");
621 
622 	ntups = PQntuples(dbres);
623 	for (dbnum = 0; dbnum < ntups; dbnum++)
624 	{
625 		char	   *datname = PQgetvalue(dbres, dbnum, i_datname);
626 		char	   *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
627 
628 		/*
629 		 * We must update databases where datallowconn = false, e.g.
630 		 * template0, because autovacuum increments their datfrozenxids,
631 		 * relfrozenxids, and relminmxid even if autovacuum is turned off, and
632 		 * even though all the data rows are already frozen.  To enable this,
633 		 * we temporarily change datallowconn.
634 		 */
635 		if (strcmp(datallowconn, "f") == 0)
636 			PQclear(executeQueryOrDie(conn_template1,
637 									  "ALTER DATABASE %s ALLOW_CONNECTIONS = true",
638 									  quote_identifier(datname)));
639 
640 		conn = connectToServer(&new_cluster, datname);
641 
642 		if (!minmxid_only)
643 			/* set pg_class.relfrozenxid */
644 			PQclear(executeQueryOrDie(conn,
645 									  "UPDATE	pg_catalog.pg_class "
646 									  "SET	relfrozenxid = '%u' "
647 			/* only heap, materialized view, and TOAST are vacuumed */
648 									  "WHERE	relkind IN ("
649 									  CppAsString2(RELKIND_RELATION) ", "
650 									  CppAsString2(RELKIND_MATVIEW) ", "
651 									  CppAsString2(RELKIND_TOASTVALUE) ")",
652 									  old_cluster.controldata.chkpnt_nxtxid));
653 
654 		/* set pg_class.relminmxid */
655 		PQclear(executeQueryOrDie(conn,
656 								  "UPDATE	pg_catalog.pg_class "
657 								  "SET	relminmxid = '%u' "
658 		/* only heap, materialized view, and TOAST are vacuumed */
659 								  "WHERE	relkind IN ("
660 								  CppAsString2(RELKIND_RELATION) ", "
661 								  CppAsString2(RELKIND_MATVIEW) ", "
662 								  CppAsString2(RELKIND_TOASTVALUE) ")",
663 								  old_cluster.controldata.chkpnt_nxtmulti));
664 		PQfinish(conn);
665 
666 		/* Reset datallowconn flag */
667 		if (strcmp(datallowconn, "f") == 0)
668 			PQclear(executeQueryOrDie(conn_template1,
669 									  "ALTER DATABASE %s ALLOW_CONNECTIONS = false",
670 									  quote_identifier(datname)));
671 	}
672 
673 	PQclear(dbres);
674 
675 	PQfinish(conn_template1);
676 
677 	check_ok();
678 }
679 
680 
681 static void
cleanup(void)682 cleanup(void)
683 {
684 	fclose(log_opts.internal);
685 
686 	/* Remove dump and log files? */
687 	if (!log_opts.retain)
688 	{
689 		int			dbnum;
690 		char	  **filename;
691 
692 		for (filename = output_files; *filename != NULL; filename++)
693 			unlink(*filename);
694 
695 		/* remove dump files */
696 		unlink(GLOBALS_DUMP_FILE);
697 
698 		if (old_cluster.dbarr.dbs)
699 			for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
700 			{
701 				char		sql_file_name[MAXPGPATH],
702 							log_file_name[MAXPGPATH];
703 				DbInfo	   *old_db = &old_cluster.dbarr.dbs[dbnum];
704 
705 				snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
706 				unlink(sql_file_name);
707 
708 				snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
709 				unlink(log_file_name);
710 			}
711 	}
712 }
713