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