1 /*
2  *	check.c
3  *
4  *	server checks and output routines
5  *
6  *	Copyright (c) 2010-2020, PostgreSQL Global Development Group
7  *	src/bin/pg_upgrade/check.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "catalog/pg_authid_d.h"
13 #include "fe_utils/string_utils.h"
14 #include "mb/pg_wchar.h"
15 #include "pg_upgrade.h"
16 
17 static void check_new_cluster_is_empty(void);
18 static void check_databases_are_compatible(void);
19 static void check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb);
20 static bool equivalent_locale(int category, const char *loca, const char *locb);
21 static void check_is_install_user(ClusterInfo *cluster);
22 static void check_proper_datallowconn(ClusterInfo *cluster);
23 static void check_for_prepared_transactions(ClusterInfo *cluster);
24 static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
25 static void check_for_tables_with_oids(ClusterInfo *cluster);
26 static void check_for_composite_data_type_usage(ClusterInfo *cluster);
27 static void check_for_reg_data_type_usage(ClusterInfo *cluster);
28 static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
29 static void check_for_pg_role_prefix(ClusterInfo *cluster);
30 static void check_for_new_tablespace_dir(ClusterInfo *new_cluster);
31 static char *get_canonical_locale_name(int category, const char *locale);
32 
33 
34 /*
35  * fix_path_separator
36  * For non-Windows, just return the argument.
37  * For Windows convert any forward slash to a backslash
38  * such as is suitable for arguments to builtin commands
39  * like RMDIR and DEL.
40  */
41 static char *
fix_path_separator(char * path)42 fix_path_separator(char *path)
43 {
44 #ifdef WIN32
45 
46 	char	   *result;
47 	char	   *c;
48 
49 	result = pg_strdup(path);
50 
51 	for (c = result; *c != '\0'; c++)
52 		if (*c == '/')
53 			*c = '\\';
54 
55 	return result;
56 #else
57 
58 	return path;
59 #endif
60 }
61 
62 void
output_check_banner(bool live_check)63 output_check_banner(bool live_check)
64 {
65 	if (user_opts.check && live_check)
66 	{
67 		pg_log(PG_REPORT,
68 			   "Performing Consistency Checks on Old Live Server\n"
69 			   "------------------------------------------------\n");
70 	}
71 	else
72 	{
73 		pg_log(PG_REPORT,
74 			   "Performing Consistency Checks\n"
75 			   "-----------------------------\n");
76 	}
77 }
78 
79 
80 void
check_and_dump_old_cluster(bool live_check)81 check_and_dump_old_cluster(bool live_check)
82 {
83 	/* -- OLD -- */
84 
85 	if (!live_check)
86 		start_postmaster(&old_cluster, true);
87 
88 	/* Extract a list of databases and tables from the old cluster */
89 	get_db_and_rel_infos(&old_cluster);
90 
91 	init_tablespaces();
92 
93 	get_loadable_libraries();
94 
95 
96 	/*
97 	 * Check for various failure cases
98 	 */
99 	check_is_install_user(&old_cluster);
100 	check_proper_datallowconn(&old_cluster);
101 	check_for_prepared_transactions(&old_cluster);
102 	check_for_composite_data_type_usage(&old_cluster);
103 	check_for_reg_data_type_usage(&old_cluster);
104 	check_for_isn_and_int8_passing_mismatch(&old_cluster);
105 
106 	/*
107 	 * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
108 	 * supported anymore. Verify there are none, iff applicable.
109 	 */
110 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
111 		check_for_tables_with_oids(&old_cluster);
112 
113 	/*
114 	 * PG 12 changed the 'sql_identifier' type storage to be based on name,
115 	 * not varchar, which breaks on-disk format for existing data. So we need
116 	 * to prevent upgrade when used in user objects (tables, indexes, ...).
117 	 */
118 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
119 		old_11_check_for_sql_identifier_data_type_usage(&old_cluster);
120 
121 	/*
122 	 * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
123 	 * hash indexes
124 	 */
125 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
126 	{
127 		old_9_6_check_for_unknown_data_type_usage(&old_cluster);
128 		if (user_opts.check)
129 			old_9_6_invalidate_hash_indexes(&old_cluster, true);
130 	}
131 
132 	/* 9.5 and below should not have roles starting with pg_ */
133 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
134 		check_for_pg_role_prefix(&old_cluster);
135 
136 	if (GET_MAJOR_VERSION(old_cluster.major_version) == 904 &&
137 		old_cluster.controldata.cat_ver < JSONB_FORMAT_CHANGE_CAT_VER)
138 		check_for_jsonb_9_4_usage(&old_cluster);
139 
140 	/* Pre-PG 9.4 had a different 'line' data type internal format */
141 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 903)
142 		old_9_3_check_for_line_data_type_usage(&old_cluster);
143 
144 	/* Pre-PG 9.0 had no large object permissions */
145 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
146 		new_9_0_populate_pg_largeobject_metadata(&old_cluster, true);
147 
148 	/*
149 	 * While not a check option, we do this now because this is the only time
150 	 * the old server is running.
151 	 */
152 	if (!user_opts.check)
153 		generate_old_dump();
154 
155 	if (!live_check)
156 		stop_postmaster(false);
157 }
158 
159 
160 void
check_new_cluster(void)161 check_new_cluster(void)
162 {
163 	get_db_and_rel_infos(&new_cluster);
164 
165 	check_new_cluster_is_empty();
166 	check_databases_are_compatible();
167 
168 	check_loadable_libraries();
169 
170 	switch (user_opts.transfer_mode)
171 	{
172 		case TRANSFER_MODE_CLONE:
173 			check_file_clone();
174 			break;
175 		case TRANSFER_MODE_COPY:
176 			break;
177 		case TRANSFER_MODE_LINK:
178 			check_hard_link();
179 			break;
180 	}
181 
182 	check_is_install_user(&new_cluster);
183 
184 	check_for_prepared_transactions(&new_cluster);
185 
186 	check_for_new_tablespace_dir(&new_cluster);
187 }
188 
189 
190 void
report_clusters_compatible(void)191 report_clusters_compatible(void)
192 {
193 	if (user_opts.check)
194 	{
195 		pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
196 		/* stops new cluster */
197 		stop_postmaster(false);
198 		exit(0);
199 	}
200 
201 	pg_log(PG_REPORT, "\n"
202 		   "If pg_upgrade fails after this point, you must re-initdb the\n"
203 		   "new cluster before continuing.\n");
204 }
205 
206 
207 void
issue_warnings_and_set_wal_level(void)208 issue_warnings_and_set_wal_level(void)
209 {
210 	/*
211 	 * We unconditionally start/stop the new server because pg_resetwal -o set
212 	 * wal_level to 'minimum'.  If the user is upgrading standby servers using
213 	 * the rsync instructions, they will need pg_upgrade to write its final
214 	 * WAL record showing wal_level as 'replica'.
215 	 */
216 	start_postmaster(&new_cluster, true);
217 
218 	/* Create dummy large object permissions for old < PG 9.0? */
219 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
220 		new_9_0_populate_pg_largeobject_metadata(&new_cluster, false);
221 
222 	/* Reindex hash indexes for old < 10.0 */
223 	if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
224 		old_9_6_invalidate_hash_indexes(&new_cluster, false);
225 
226 	report_extension_updates(&new_cluster);
227 
228 	stop_postmaster(false);
229 }
230 
231 
232 void
output_completion_banner(char * analyze_script_file_name,char * deletion_script_file_name)233 output_completion_banner(char *analyze_script_file_name,
234 						 char *deletion_script_file_name)
235 {
236 	pg_log(PG_REPORT,
237 		   "Optimizer statistics are not transferred by pg_upgrade so,\n"
238 		   "once you start the new server, consider running:\n"
239 		   "    %s\n\n", analyze_script_file_name);
240 
241 	if (deletion_script_file_name)
242 		pg_log(PG_REPORT,
243 			   "Running this script will delete the old cluster's data files:\n"
244 			   "    %s\n",
245 			   deletion_script_file_name);
246 	else
247 		pg_log(PG_REPORT,
248 			   "Could not create a script to delete the old cluster's data files\n"
249 			   "because user-defined tablespaces or the new cluster's data directory\n"
250 			   "exist in the old cluster directory.  The old cluster's contents must\n"
251 			   "be deleted manually.\n");
252 }
253 
254 
255 void
check_cluster_versions(void)256 check_cluster_versions(void)
257 {
258 	prep_status("Checking cluster versions");
259 
260 	/* cluster versions should already have been obtained */
261 	Assert(old_cluster.major_version != 0);
262 	Assert(new_cluster.major_version != 0);
263 
264 	/*
265 	 * We allow upgrades from/to the same major version for alpha/beta
266 	 * upgrades
267 	 */
268 
269 	if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
270 		pg_fatal("This utility can only upgrade from PostgreSQL version 8.4 and later.\n");
271 
272 	/* Only current PG version is supported as a target */
273 	if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
274 		pg_fatal("This utility can only upgrade to PostgreSQL version %s.\n",
275 				 PG_MAJORVERSION);
276 
277 	/*
278 	 * We can't allow downgrading because we use the target pg_dump, and
279 	 * pg_dump cannot operate on newer database versions, only current and
280 	 * older versions.
281 	 */
282 	if (old_cluster.major_version > new_cluster.major_version)
283 		pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
284 
285 	/* Ensure binaries match the designated data directories */
286 	if (GET_MAJOR_VERSION(old_cluster.major_version) !=
287 		GET_MAJOR_VERSION(old_cluster.bin_version))
288 		pg_fatal("Old cluster data and binary directories are from different major versions.\n");
289 	if (GET_MAJOR_VERSION(new_cluster.major_version) !=
290 		GET_MAJOR_VERSION(new_cluster.bin_version))
291 		pg_fatal("New cluster data and binary directories are from different major versions.\n");
292 
293 	check_ok();
294 }
295 
296 
297 void
check_cluster_compatibility(bool live_check)298 check_cluster_compatibility(bool live_check)
299 {
300 	/* get/check pg_control data of servers */
301 	get_control_data(&old_cluster, live_check);
302 	get_control_data(&new_cluster, false);
303 	check_control_data(&old_cluster.controldata, &new_cluster.controldata);
304 
305 	/* We read the real port number for PG >= 9.1 */
306 	if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) <= 900 &&
307 		old_cluster.port == DEF_PGUPORT)
308 		pg_fatal("When checking a pre-PG 9.1 live old server, "
309 				 "you must specify the old server's port number.\n");
310 
311 	if (live_check && old_cluster.port == new_cluster.port)
312 		pg_fatal("When checking a live server, "
313 				 "the old and new port numbers must be different.\n");
314 }
315 
316 
317 /*
318  * check_locale_and_encoding()
319  *
320  * Check that locale and encoding of a database in the old and new clusters
321  * are compatible.
322  */
323 static void
check_locale_and_encoding(DbInfo * olddb,DbInfo * newdb)324 check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb)
325 {
326 	if (olddb->db_encoding != newdb->db_encoding)
327 		pg_fatal("encodings for database \"%s\" do not match:  old \"%s\", new \"%s\"\n",
328 				 olddb->db_name,
329 				 pg_encoding_to_char(olddb->db_encoding),
330 				 pg_encoding_to_char(newdb->db_encoding));
331 	if (!equivalent_locale(LC_COLLATE, olddb->db_collate, newdb->db_collate))
332 		pg_fatal("lc_collate values for database \"%s\" do not match:  old \"%s\", new \"%s\"\n",
333 				 olddb->db_name, olddb->db_collate, newdb->db_collate);
334 	if (!equivalent_locale(LC_CTYPE, olddb->db_ctype, newdb->db_ctype))
335 		pg_fatal("lc_ctype values for database \"%s\" do not match:  old \"%s\", new \"%s\"\n",
336 				 olddb->db_name, olddb->db_ctype, newdb->db_ctype);
337 }
338 
339 /*
340  * equivalent_locale()
341  *
342  * Best effort locale-name comparison.  Return false if we are not 100% sure
343  * the locales are equivalent.
344  *
345  * Note: The encoding parts of the names are ignored. This function is
346  * currently used to compare locale names stored in pg_database, and
347  * pg_database contains a separate encoding field. That's compared directly
348  * in check_locale_and_encoding().
349  */
350 static bool
equivalent_locale(int category,const char * loca,const char * locb)351 equivalent_locale(int category, const char *loca, const char *locb)
352 {
353 	const char *chara;
354 	const char *charb;
355 	char	   *canona;
356 	char	   *canonb;
357 	int			lena;
358 	int			lenb;
359 
360 	/*
361 	 * If the names are equal, the locales are equivalent. Checking this first
362 	 * avoids calling setlocale() in the common case that the names are equal.
363 	 * That's a good thing, if setlocale() is buggy, for example.
364 	 */
365 	if (pg_strcasecmp(loca, locb) == 0)
366 		return true;
367 
368 	/*
369 	 * Not identical. Canonicalize both names, remove the encoding parts, and
370 	 * try again.
371 	 */
372 	canona = get_canonical_locale_name(category, loca);
373 	chara = strrchr(canona, '.');
374 	lena = chara ? (chara - canona) : strlen(canona);
375 
376 	canonb = get_canonical_locale_name(category, locb);
377 	charb = strrchr(canonb, '.');
378 	lenb = charb ? (charb - canonb) : strlen(canonb);
379 
380 	if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0)
381 	{
382 		pg_free(canona);
383 		pg_free(canonb);
384 		return true;
385 	}
386 
387 	pg_free(canona);
388 	pg_free(canonb);
389 	return false;
390 }
391 
392 
393 static void
check_new_cluster_is_empty(void)394 check_new_cluster_is_empty(void)
395 {
396 	int			dbnum;
397 
398 	for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
399 	{
400 		int			relnum;
401 		RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
402 
403 		for (relnum = 0; relnum < rel_arr->nrels;
404 			 relnum++)
405 		{
406 			/* pg_largeobject and its index should be skipped */
407 			if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
408 				pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"\n",
409 						 new_cluster.dbarr.dbs[dbnum].db_name,
410 						 rel_arr->rels[relnum].nspname,
411 						 rel_arr->rels[relnum].relname);
412 		}
413 	}
414 }
415 
416 /*
417  * Check that every database that already exists in the new cluster is
418  * compatible with the corresponding database in the old one.
419  */
420 static void
check_databases_are_compatible(void)421 check_databases_are_compatible(void)
422 {
423 	int			newdbnum;
424 	int			olddbnum;
425 	DbInfo	   *newdbinfo;
426 	DbInfo	   *olddbinfo;
427 
428 	for (newdbnum = 0; newdbnum < new_cluster.dbarr.ndbs; newdbnum++)
429 	{
430 		newdbinfo = &new_cluster.dbarr.dbs[newdbnum];
431 
432 		/* Find the corresponding database in the old cluster */
433 		for (olddbnum = 0; olddbnum < old_cluster.dbarr.ndbs; olddbnum++)
434 		{
435 			olddbinfo = &old_cluster.dbarr.dbs[olddbnum];
436 			if (strcmp(newdbinfo->db_name, olddbinfo->db_name) == 0)
437 			{
438 				check_locale_and_encoding(olddbinfo, newdbinfo);
439 				break;
440 			}
441 		}
442 	}
443 }
444 
445 
446 /*
447  * create_script_for_cluster_analyze()
448  *
449  *	This incrementally generates better optimizer statistics
450  */
451 void
create_script_for_cluster_analyze(char ** analyze_script_file_name)452 create_script_for_cluster_analyze(char **analyze_script_file_name)
453 {
454 	FILE	   *script = NULL;
455 	PQExpBufferData user_specification;
456 
457 	prep_status("Creating script to analyze new cluster");
458 
459 	initPQExpBuffer(&user_specification);
460 	if (os_info.user_specified)
461 	{
462 		appendPQExpBufferStr(&user_specification, "-U ");
463 		appendShellString(&user_specification, os_info.user);
464 		appendPQExpBufferChar(&user_specification, ' ');
465 	}
466 
467 	*analyze_script_file_name = psprintf("%sanalyze_new_cluster.%s",
468 										 SCRIPT_PREFIX, SCRIPT_EXT);
469 
470 	if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
471 		pg_fatal("could not open file \"%s\": %s\n",
472 				 *analyze_script_file_name, strerror(errno));
473 
474 #ifndef WIN32
475 	/* add shebang header */
476 	fprintf(script, "#!/bin/sh\n\n");
477 #else
478 	/* suppress command echoing */
479 	fprintf(script, "@echo off\n");
480 #endif
481 
482 	fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n",
483 			ECHO_QUOTE, ECHO_QUOTE);
484 	fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n",
485 			ECHO_QUOTE, ECHO_QUOTE);
486 	fprintf(script, "echo %swith increasing accuracy.  When it is done, your system will%s\n",
487 			ECHO_QUOTE, ECHO_QUOTE);
488 	fprintf(script, "echo %shave the default level of optimizer statistics.%s\n",
489 			ECHO_QUOTE, ECHO_QUOTE);
490 	fprintf(script, "echo%s\n\n", ECHO_BLANK);
491 
492 	fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n",
493 			ECHO_QUOTE, ECHO_QUOTE);
494 	fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n",
495 			ECHO_QUOTE, ECHO_QUOTE);
496 	fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n",
497 			ECHO_QUOTE, ECHO_QUOTE);
498 	fprintf(script, "echo%s\n\n", ECHO_BLANK);
499 
500 	fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n",
501 			ECHO_QUOTE, ECHO_QUOTE);
502 	fprintf(script, "echo %sthis script and run:%s\n",
503 			ECHO_QUOTE, ECHO_QUOTE);
504 	fprintf(script, "echo %s    \"%s/vacuumdb\" %s--all --analyze-only%s\n", ECHO_QUOTE,
505 			new_cluster.bindir, user_specification.data, ECHO_QUOTE);
506 	fprintf(script, "echo%s\n\n", ECHO_BLANK);
507 
508 	fprintf(script, "\"%s/vacuumdb\" %s--all --analyze-in-stages\n",
509 			new_cluster.bindir, user_specification.data);
510 
511 	fprintf(script, "echo%s\n\n", ECHO_BLANK);
512 	fprintf(script, "echo %sDone%s\n",
513 			ECHO_QUOTE, ECHO_QUOTE);
514 
515 	fclose(script);
516 
517 #ifndef WIN32
518 	if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
519 		pg_fatal("could not add execute permission to file \"%s\": %s\n",
520 				 *analyze_script_file_name, strerror(errno));
521 #endif
522 
523 	termPQExpBuffer(&user_specification);
524 
525 	check_ok();
526 }
527 
528 
529 /*
530  * A previous run of pg_upgrade might have failed and the new cluster
531  * directory recreated, but they might have forgotten to remove
532  * the new cluster's tablespace directories.  Therefore, check that
533  * new cluster tablespace directories do not already exist.  If
534  * they do, it would cause an error while restoring global objects.
535  * This allows the failure to be detected at check time, rather than
536  * during schema restore.
537  *
538  * Note, v8.4 has no tablespace_suffix, which is fine so long as the
539  * version being upgraded *to* has a suffix, since it's not allowed
540  * to pg_upgrade from a version to the same version if tablespaces are
541  * in use.
542  */
543 static void
check_for_new_tablespace_dir(ClusterInfo * new_cluster)544 check_for_new_tablespace_dir(ClusterInfo *new_cluster)
545 {
546 	int		tblnum;
547 	char	new_tablespace_dir[MAXPGPATH];
548 
549 	prep_status("Checking for new cluster tablespace directories");
550 
551 	for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
552 	{
553 		struct stat statbuf;
554 
555 		snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
556 				os_info.old_tablespaces[tblnum],
557 				new_cluster->tablespace_suffix);
558 
559 		if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
560 			pg_fatal("new cluster tablespace directory already exists: \"%s\"\n",
561 					 new_tablespace_dir);
562 	}
563 
564 	check_ok();
565 }
566 
567 /*
568  * create_script_for_old_cluster_deletion()
569  *
570  *	This is particularly useful for tablespace deletion.
571  */
572 void
create_script_for_old_cluster_deletion(char ** deletion_script_file_name)573 create_script_for_old_cluster_deletion(char **deletion_script_file_name)
574 {
575 	FILE	   *script = NULL;
576 	int			tblnum;
577 	char		old_cluster_pgdata[MAXPGPATH],
578 				new_cluster_pgdata[MAXPGPATH];
579 
580 	*deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
581 										  SCRIPT_PREFIX, SCRIPT_EXT);
582 
583 	strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
584 	canonicalize_path(old_cluster_pgdata);
585 
586 	strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
587 	canonicalize_path(new_cluster_pgdata);
588 
589 	/* Some people put the new data directory inside the old one. */
590 	if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
591 	{
592 		pg_log(PG_WARNING,
593 			   "\nWARNING:  new data directory should not be inside the old data directory, e.g. %s\n", old_cluster_pgdata);
594 
595 		/* Unlink file in case it is left over from a previous run. */
596 		unlink(*deletion_script_file_name);
597 		pg_free(*deletion_script_file_name);
598 		*deletion_script_file_name = NULL;
599 		return;
600 	}
601 
602 	/*
603 	 * Some users (oddly) create tablespaces inside the cluster data
604 	 * directory.  We can't create a proper old cluster delete script in that
605 	 * case.
606 	 */
607 	for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
608 	{
609 		char		old_tablespace_dir[MAXPGPATH];
610 
611 		strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
612 		canonicalize_path(old_tablespace_dir);
613 		if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
614 		{
615 			/* reproduce warning from CREATE TABLESPACE that is in the log */
616 			pg_log(PG_WARNING,
617 				   "\nWARNING:  user-defined tablespace locations should not be inside the data directory, e.g. %s\n", old_tablespace_dir);
618 
619 			/* Unlink file in case it is left over from a previous run. */
620 			unlink(*deletion_script_file_name);
621 			pg_free(*deletion_script_file_name);
622 			*deletion_script_file_name = NULL;
623 			return;
624 		}
625 	}
626 
627 	prep_status("Creating script to delete old cluster");
628 
629 	if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
630 		pg_fatal("could not open file \"%s\": %s\n",
631 				 *deletion_script_file_name, strerror(errno));
632 
633 #ifndef WIN32
634 	/* add shebang header */
635 	fprintf(script, "#!/bin/sh\n\n");
636 #endif
637 
638 	/* delete old cluster's default tablespace */
639 	fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
640 			fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
641 
642 	/* delete old cluster's alternate tablespaces */
643 	for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
644 	{
645 		/*
646 		 * Do the old cluster's per-database directories share a directory
647 		 * with a new version-specific tablespace?
648 		 */
649 		if (strlen(old_cluster.tablespace_suffix) == 0)
650 		{
651 			/* delete per-database directories */
652 			int			dbnum;
653 
654 			fprintf(script, "\n");
655 			/* remove PG_VERSION? */
656 			if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
657 				fprintf(script, RM_CMD " %s%cPG_VERSION\n",
658 						fix_path_separator(os_info.old_tablespaces[tblnum]),
659 						PATH_SEPARATOR);
660 
661 			for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
662 				fprintf(script, RMDIR_CMD " %c%s%c%d%c\n", PATH_QUOTE,
663 						fix_path_separator(os_info.old_tablespaces[tblnum]),
664 						PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid,
665 						PATH_QUOTE);
666 		}
667 		else
668 		{
669 			char	   *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
670 
671 			/*
672 			 * Simply delete the tablespace directory, which might be ".old"
673 			 * or a version-specific subdirectory.
674 			 */
675 			fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
676 					fix_path_separator(os_info.old_tablespaces[tblnum]),
677 					fix_path_separator(suffix_path), PATH_QUOTE);
678 			pfree(suffix_path);
679 		}
680 	}
681 
682 	fclose(script);
683 
684 #ifndef WIN32
685 	if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
686 		pg_fatal("could not add execute permission to file \"%s\": %s\n",
687 				 *deletion_script_file_name, strerror(errno));
688 #endif
689 
690 	check_ok();
691 }
692 
693 
694 /*
695  *	check_is_install_user()
696  *
697  *	Check we are the install user, and that the new cluster
698  *	has no other users.
699  */
700 static void
check_is_install_user(ClusterInfo * cluster)701 check_is_install_user(ClusterInfo *cluster)
702 {
703 	PGresult   *res;
704 	PGconn	   *conn = connectToServer(cluster, "template1");
705 
706 	prep_status("Checking database user is the install user");
707 
708 	/* Can't use pg_authid because only superusers can view it. */
709 	res = executeQueryOrDie(conn,
710 							"SELECT rolsuper, oid "
711 							"FROM pg_catalog.pg_roles "
712 							"WHERE rolname = current_user "
713 							"AND rolname !~ '^pg_'");
714 
715 	/*
716 	 * We only allow the install user in the new cluster (see comment below)
717 	 * and we preserve pg_authid.oid, so this must be the install user in the
718 	 * old cluster too.
719 	 */
720 	if (PQntuples(res) != 1 ||
721 		atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
722 		pg_fatal("database user \"%s\" is not the install user\n",
723 				 os_info.user);
724 
725 	PQclear(res);
726 
727 	res = executeQueryOrDie(conn,
728 							"SELECT COUNT(*) "
729 							"FROM pg_catalog.pg_roles "
730 							"WHERE rolname !~ '^pg_'");
731 
732 	if (PQntuples(res) != 1)
733 		pg_fatal("could not determine the number of users\n");
734 
735 	/*
736 	 * We only allow the install user in the new cluster because other defined
737 	 * users might match users defined in the old cluster and generate an
738 	 * error during pg_dump restore.
739 	 */
740 	if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1)
741 		pg_fatal("Only the install user can be defined in the new cluster.\n");
742 
743 	PQclear(res);
744 
745 	PQfinish(conn);
746 
747 	check_ok();
748 }
749 
750 
751 static void
check_proper_datallowconn(ClusterInfo * cluster)752 check_proper_datallowconn(ClusterInfo *cluster)
753 {
754 	int			dbnum;
755 	PGconn	   *conn_template1;
756 	PGresult   *dbres;
757 	int			ntups;
758 	int			i_datname;
759 	int			i_datallowconn;
760 
761 	prep_status("Checking database connection settings");
762 
763 	conn_template1 = connectToServer(cluster, "template1");
764 
765 	/* get database names */
766 	dbres = executeQueryOrDie(conn_template1,
767 							  "SELECT	datname, datallowconn "
768 							  "FROM	pg_catalog.pg_database");
769 
770 	i_datname = PQfnumber(dbres, "datname");
771 	i_datallowconn = PQfnumber(dbres, "datallowconn");
772 
773 	ntups = PQntuples(dbres);
774 	for (dbnum = 0; dbnum < ntups; dbnum++)
775 	{
776 		char	   *datname = PQgetvalue(dbres, dbnum, i_datname);
777 		char	   *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
778 
779 		if (strcmp(datname, "template0") == 0)
780 		{
781 			/* avoid restore failure when pg_dumpall tries to create template0 */
782 			if (strcmp(datallowconn, "t") == 0)
783 				pg_fatal("template0 must not allow connections, "
784 						 "i.e. its pg_database.datallowconn must be false\n");
785 		}
786 		else
787 		{
788 			/*
789 			 * avoid datallowconn == false databases from being skipped on
790 			 * restore
791 			 */
792 			if (strcmp(datallowconn, "f") == 0)
793 				pg_fatal("All non-template0 databases must allow connections, "
794 						 "i.e. their pg_database.datallowconn must be true\n");
795 		}
796 	}
797 
798 	PQclear(dbres);
799 
800 	PQfinish(conn_template1);
801 
802 	check_ok();
803 }
804 
805 
806 /*
807  *	check_for_prepared_transactions()
808  *
809  *	Make sure there are no prepared transactions because the storage format
810  *	might have changed.
811  */
812 static void
check_for_prepared_transactions(ClusterInfo * cluster)813 check_for_prepared_transactions(ClusterInfo *cluster)
814 {
815 	PGresult   *res;
816 	PGconn	   *conn = connectToServer(cluster, "template1");
817 
818 	prep_status("Checking for prepared transactions");
819 
820 	res = executeQueryOrDie(conn,
821 							"SELECT * "
822 							"FROM pg_catalog.pg_prepared_xacts");
823 
824 	if (PQntuples(res) != 0)
825 	{
826 		if (cluster == &old_cluster)
827 			pg_fatal("The source cluster contains prepared transactions\n");
828 		else
829 			pg_fatal("The target cluster contains prepared transactions\n");
830 	}
831 
832 	PQclear(res);
833 
834 	PQfinish(conn);
835 
836 	check_ok();
837 }
838 
839 
840 /*
841  *	check_for_isn_and_int8_passing_mismatch()
842  *
843  *	contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
844  *	by value.  The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
845  *	it must match for the old and new servers.
846  */
847 static void
check_for_isn_and_int8_passing_mismatch(ClusterInfo * cluster)848 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
849 {
850 	int			dbnum;
851 	FILE	   *script = NULL;
852 	bool		found = false;
853 	char		output_path[MAXPGPATH];
854 
855 	prep_status("Checking for contrib/isn with bigint-passing mismatch");
856 
857 	if (old_cluster.controldata.float8_pass_by_value ==
858 		new_cluster.controldata.float8_pass_by_value)
859 	{
860 		/* no mismatch */
861 		check_ok();
862 		return;
863 	}
864 
865 	snprintf(output_path, sizeof(output_path),
866 			 "contrib_isn_and_int8_pass_by_value.txt");
867 
868 	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
869 	{
870 		PGresult   *res;
871 		bool		db_used = false;
872 		int			ntups;
873 		int			rowno;
874 		int			i_nspname,
875 					i_proname;
876 		DbInfo	   *active_db = &cluster->dbarr.dbs[dbnum];
877 		PGconn	   *conn = connectToServer(cluster, active_db->db_name);
878 
879 		/* Find any functions coming from contrib/isn */
880 		res = executeQueryOrDie(conn,
881 								"SELECT n.nspname, p.proname "
882 								"FROM	pg_catalog.pg_proc p, "
883 								"		pg_catalog.pg_namespace n "
884 								"WHERE	p.pronamespace = n.oid AND "
885 								"		p.probin = '$libdir/isn'");
886 
887 		ntups = PQntuples(res);
888 		i_nspname = PQfnumber(res, "nspname");
889 		i_proname = PQfnumber(res, "proname");
890 		for (rowno = 0; rowno < ntups; rowno++)
891 		{
892 			found = true;
893 			if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
894 				pg_fatal("could not open file \"%s\": %s\n",
895 						 output_path, strerror(errno));
896 			if (!db_used)
897 			{
898 				fprintf(script, "In database: %s\n", active_db->db_name);
899 				db_used = true;
900 			}
901 			fprintf(script, "  %s.%s\n",
902 					PQgetvalue(res, rowno, i_nspname),
903 					PQgetvalue(res, rowno, i_proname));
904 		}
905 
906 		PQclear(res);
907 
908 		PQfinish(conn);
909 	}
910 
911 	if (script)
912 		fclose(script);
913 
914 	if (found)
915 	{
916 		pg_log(PG_REPORT, "fatal\n");
917 		pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
918 				 "bigint data type.  Your old and new clusters pass bigint values\n"
919 				 "differently so this cluster cannot currently be upgraded.  You can\n"
920 				 "manually dump databases in the old cluster that use \"contrib/isn\"\n"
921 				 "facilities, drop them, perform the upgrade, and then restore them.  A\n"
922 				 "list of the problem functions is in the file:\n"
923 				 "    %s\n\n", output_path);
924 	}
925 	else
926 		check_ok();
927 }
928 
929 
930 /*
931  * Verify that no tables are declared WITH OIDS.
932  */
933 static void
check_for_tables_with_oids(ClusterInfo * cluster)934 check_for_tables_with_oids(ClusterInfo *cluster)
935 {
936 	int			dbnum;
937 	FILE	   *script = NULL;
938 	bool		found = false;
939 	char		output_path[MAXPGPATH];
940 
941 	prep_status("Checking for tables WITH OIDS");
942 
943 	snprintf(output_path, sizeof(output_path),
944 			 "tables_with_oids.txt");
945 
946 	/* Find any tables declared WITH OIDS */
947 	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
948 	{
949 		PGresult   *res;
950 		bool		db_used = false;
951 		int			ntups;
952 		int			rowno;
953 		int			i_nspname,
954 					i_relname;
955 		DbInfo	   *active_db = &cluster->dbarr.dbs[dbnum];
956 		PGconn	   *conn = connectToServer(cluster, active_db->db_name);
957 
958 		res = executeQueryOrDie(conn,
959 								"SELECT n.nspname, c.relname "
960 								"FROM	pg_catalog.pg_class c, "
961 								"		pg_catalog.pg_namespace n "
962 								"WHERE	c.relnamespace = n.oid AND "
963 								"		c.relhasoids AND"
964 								"       n.nspname NOT IN ('pg_catalog')");
965 
966 		ntups = PQntuples(res);
967 		i_nspname = PQfnumber(res, "nspname");
968 		i_relname = PQfnumber(res, "relname");
969 		for (rowno = 0; rowno < ntups; rowno++)
970 		{
971 			found = true;
972 			if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
973 				pg_fatal("could not open file \"%s\": %s\n",
974 						 output_path, strerror(errno));
975 			if (!db_used)
976 			{
977 				fprintf(script, "In database: %s\n", active_db->db_name);
978 				db_used = true;
979 			}
980 			fprintf(script, "  %s.%s\n",
981 					PQgetvalue(res, rowno, i_nspname),
982 					PQgetvalue(res, rowno, i_relname));
983 		}
984 
985 		PQclear(res);
986 
987 		PQfinish(conn);
988 	}
989 
990 	if (script)
991 		fclose(script);
992 
993 	if (found)
994 	{
995 		pg_log(PG_REPORT, "fatal\n");
996 		pg_fatal("Your installation contains tables declared WITH OIDS, which is not\n"
997 				 "supported anymore.  Consider removing the oid column using\n"
998 				 "    ALTER TABLE ... SET WITHOUT OIDS;\n"
999 				 "A list of tables with the problem is in the file:\n"
1000 				 "    %s\n\n", output_path);
1001 	}
1002 	else
1003 		check_ok();
1004 }
1005 
1006 
1007 /*
1008  * check_for_composite_data_type_usage()
1009  *	Check for system-defined composite types used in user tables.
1010  *
1011  *	The OIDs of rowtypes of system catalogs and information_schema views
1012  *	can change across major versions; unlike user-defined types, we have
1013  *	no mechanism for forcing them to be the same in the new cluster.
1014  *	Hence, if any user table uses one, that's problematic for pg_upgrade.
1015  */
1016 static void
check_for_composite_data_type_usage(ClusterInfo * cluster)1017 check_for_composite_data_type_usage(ClusterInfo *cluster)
1018 {
1019 	bool		found;
1020 	Oid			firstUserOid;
1021 	char		output_path[MAXPGPATH];
1022 	char	   *base_query;
1023 
1024 	prep_status("Checking for system-defined composite types in user tables");
1025 
1026 	snprintf(output_path, sizeof(output_path), "tables_using_composite.txt");
1027 
1028 	/*
1029 	 * Look for composite types that were made during initdb *or* belong to
1030 	 * information_schema; that's important in case information_schema was
1031 	 * dropped and reloaded.
1032 	 *
1033 	 * The cutoff OID here should match the source cluster's value of
1034 	 * FirstNormalObjectId.  We hardcode it rather than using that C #define
1035 	 * because, if that #define is ever changed, our own version's value is
1036 	 * NOT what to use.  Eventually we may need a test on the source cluster's
1037 	 * version to select the correct value.
1038 	 */
1039 	firstUserOid = 16384;
1040 
1041 	base_query = psprintf("SELECT t.oid FROM pg_catalog.pg_type t "
1042 						  "LEFT JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid "
1043 						  " WHERE typtype = 'c' AND (t.oid < %u OR nspname = 'information_schema')",
1044 						  firstUserOid);
1045 
1046 	found = check_for_data_types_usage(cluster, base_query, output_path);
1047 
1048 	free(base_query);
1049 
1050 	if (found)
1051 	{
1052 		pg_log(PG_REPORT, "fatal\n");
1053 		pg_fatal("Your installation contains system-defined composite type(s) in user tables.\n"
1054 				 "These type OIDs are not stable across PostgreSQL versions,\n"
1055 				 "so this cluster cannot currently be upgraded.  You can\n"
1056 				 "drop the problem columns and restart the upgrade.\n"
1057 				 "A list of the problem columns is in the file:\n"
1058 				 "    %s\n\n", output_path);
1059 	}
1060 	else
1061 		check_ok();
1062 }
1063 
1064 /*
1065  * check_for_reg_data_type_usage()
1066  *	pg_upgrade only preserves these system values:
1067  *		pg_class.oid
1068  *		pg_type.oid
1069  *		pg_enum.oid
1070  *
1071  *	Many of the reg* data types reference system catalog info that is
1072  *	not preserved, and hence these data types cannot be used in user
1073  *	tables upgraded by pg_upgrade.
1074  */
1075 static void
check_for_reg_data_type_usage(ClusterInfo * cluster)1076 check_for_reg_data_type_usage(ClusterInfo *cluster)
1077 {
1078 	bool		found;
1079 	char		output_path[MAXPGPATH];
1080 
1081 	prep_status("Checking for reg* data types in user tables");
1082 
1083 	snprintf(output_path, sizeof(output_path), "tables_using_reg.txt");
1084 
1085 	/*
1086 	 * Note: older servers will not have all of these reg* types, so we have
1087 	 * to write the query like this rather than depending on casts to regtype.
1088 	 */
1089 	found = check_for_data_types_usage(cluster,
1090 									   "SELECT oid FROM pg_catalog.pg_type t "
1091 									   "WHERE t.typnamespace = "
1092 									   "        (SELECT oid FROM pg_catalog.pg_namespace "
1093 									   "         WHERE nspname = 'pg_catalog') "
1094 									   "  AND t.typname IN ( "
1095 	/* pg_class.oid is preserved, so 'regclass' is OK */
1096 									   "           'regcollation', "
1097 									   "           'regconfig', "
1098 									   "           'regdictionary', "
1099 									   "           'regnamespace', "
1100 									   "           'regoper', "
1101 									   "           'regoperator', "
1102 									   "           'regproc', "
1103 									   "           'regprocedure' "
1104 	/* pg_authid.oid is preserved, so 'regrole' is OK */
1105 	/* pg_type.oid is (mostly) preserved, so 'regtype' is OK */
1106 									   "         )",
1107 									   output_path);
1108 
1109 	if (found)
1110 	{
1111 		pg_log(PG_REPORT, "fatal\n");
1112 		pg_fatal("Your installation contains one of the reg* data types in user tables.\n"
1113 				 "These data types reference system OIDs that are not preserved by\n"
1114 				 "pg_upgrade, so this cluster cannot currently be upgraded.  You can\n"
1115 				 "remove the problem tables and restart the upgrade.  A list of the\n"
1116 				 "problem columns is in the file:\n"
1117 				 "    %s\n\n", output_path);
1118 	}
1119 	else
1120 		check_ok();
1121 }
1122 
1123 
1124 /*
1125  * check_for_jsonb_9_4_usage()
1126  *
1127  *	JSONB changed its storage format during 9.4 beta, so check for it.
1128  */
1129 static void
check_for_jsonb_9_4_usage(ClusterInfo * cluster)1130 check_for_jsonb_9_4_usage(ClusterInfo *cluster)
1131 {
1132 	char		output_path[MAXPGPATH];
1133 
1134 	prep_status("Checking for incompatible \"jsonb\" data type");
1135 
1136 	snprintf(output_path, sizeof(output_path), "tables_using_jsonb.txt");
1137 
1138 	if (check_for_data_type_usage(cluster, "pg_catalog.jsonb", output_path))
1139 	{
1140 		pg_log(PG_REPORT, "fatal\n");
1141 		pg_fatal("Your installation contains the \"jsonb\" data type in user tables.\n"
1142 				 "The internal format of \"jsonb\" changed during 9.4 beta so this\n"
1143 				 "cluster cannot currently be upgraded.  You can remove the problem\n"
1144 				 "tables and restart the upgrade.  A list of the problem columns is\n"
1145 				 "in the file:\n"
1146 				 "    %s\n\n", output_path);
1147 	}
1148 	else
1149 		check_ok();
1150 }
1151 
1152 /*
1153  * check_for_pg_role_prefix()
1154  *
1155  *	Versions older than 9.6 should not have any pg_* roles
1156  */
1157 static void
check_for_pg_role_prefix(ClusterInfo * cluster)1158 check_for_pg_role_prefix(ClusterInfo *cluster)
1159 {
1160 	PGresult   *res;
1161 	PGconn	   *conn = connectToServer(cluster, "template1");
1162 
1163 	prep_status("Checking for roles starting with \"pg_\"");
1164 
1165 	res = executeQueryOrDie(conn,
1166 							"SELECT * "
1167 							"FROM pg_catalog.pg_roles "
1168 							"WHERE rolname ~ '^pg_'");
1169 
1170 	if (PQntuples(res) != 0)
1171 	{
1172 		if (cluster == &old_cluster)
1173 			pg_fatal("The source cluster contains roles starting with \"pg_\"\n");
1174 		else
1175 			pg_fatal("The target cluster contains roles starting with \"pg_\"\n");
1176 	}
1177 
1178 	PQclear(res);
1179 
1180 	PQfinish(conn);
1181 
1182 	check_ok();
1183 }
1184 
1185 
1186 /*
1187  * get_canonical_locale_name
1188  *
1189  * Send the locale name to the system, and hope we get back a canonical
1190  * version.  This should match the backend's check_locale() function.
1191  */
1192 static char *
get_canonical_locale_name(int category,const char * locale)1193 get_canonical_locale_name(int category, const char *locale)
1194 {
1195 	char	   *save;
1196 	char	   *res;
1197 
1198 	/* get the current setting, so we can restore it. */
1199 	save = setlocale(category, NULL);
1200 	if (!save)
1201 		pg_fatal("failed to get the current locale\n");
1202 
1203 	/* 'save' may be pointing at a modifiable scratch variable, so copy it. */
1204 	save = pg_strdup(save);
1205 
1206 	/* set the locale with setlocale, to see if it accepts it. */
1207 	res = setlocale(category, locale);
1208 
1209 	if (!res)
1210 		pg_fatal("failed to get system locale name for \"%s\"\n", locale);
1211 
1212 	res = pg_strdup(res);
1213 
1214 	/* restore old value. */
1215 	if (!setlocale(category, save))
1216 		pg_fatal("failed to restore old locale \"%s\"\n", save);
1217 
1218 	pg_free(save);
1219 
1220 	return res;
1221 }
1222