4 * server checks and output routines
6 * Copyright (c) 2010-2017, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/check.c
10 #include "postgres_fe.h"
12 #include "catalog/pg_authid.h"
13 #include "fe_utils/string_utils.h"
14 #include "mb/pg_wchar.h"
15 #include "pg_upgrade.h"
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_reg_data_type_usage(ClusterInfo *cluster);
27 static void check_for_jsonb_9_4_usage(ClusterInfo *cluster);
28 static void check_for_pg_role_prefix(ClusterInfo *cluster);
29 static char *get_canonical_locale_name(int category, const char *locale);
34 * For non-Windows, just return the argument.
35 * For Windows convert any forward slash to a backslash
36 * such as is suitable for arguments to builtin commands
40 fix_path_separator(char *path)
47 result = pg_strdup(path);
49 for (c = result; *c != '\0'; c++)
61 output_check_banner(bool live_check)
63 if (user_opts.check && live_check)
65 pg_log(PG_REPORT, "Performing Consistency Checks on Old Live Server\n");
66 pg_log(PG_REPORT, "------------------------------------------------\n");
70 pg_log(PG_REPORT, "Performing Consistency Checks\n");
71 pg_log(PG_REPORT, "-----------------------------\n");
77 check_and_dump_old_cluster(bool live_check)
82 start_postmaster(&old_cluster, true);
84 /* Extract a list of databases and tables from the old cluster */
85 get_db_and_rel_infos(&old_cluster);
89 get_loadable_libraries();
93 * Check for various failure cases
95 check_is_install_user(&old_cluster);
96 check_proper_datallowconn(&old_cluster);
97 check_for_prepared_transactions(&old_cluster);
98 check_for_reg_data_type_usage(&old_cluster);
99 check_for_isn_and_int8_passing_mismatch(&old_cluster);
101 /* Pre-PG 10 allowed tables with 'unknown' type columns */
102 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
103 old_9_6_check_for_unknown_data_type_usage(&old_cluster);
105 /* 9.5 and below should not have roles starting with pg_ */
106 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
107 check_for_pg_role_prefix(&old_cluster);
109 if (GET_MAJOR_VERSION(old_cluster.major_version) == 904 &&
110 old_cluster.controldata.cat_ver < JSONB_FORMAT_CHANGE_CAT_VER)
111 check_for_jsonb_9_4_usage(&old_cluster);
113 /* Pre-PG 9.4 had a different 'line' data type internal format */
114 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 903)
115 old_9_3_check_for_line_data_type_usage(&old_cluster);
117 /* Pre-PG 9.0 had no large object permissions */
118 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
119 new_9_0_populate_pg_largeobject_metadata(&old_cluster, true);
122 * While not a check option, we do this now because this is the only time
123 * the old server is running.
125 if (!user_opts.check)
129 stop_postmaster(false);
134 check_new_cluster(void)
136 get_db_and_rel_infos(&new_cluster);
138 check_new_cluster_is_empty();
139 check_databases_are_compatible();
141 check_loadable_libraries();
143 if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
146 check_is_install_user(&new_cluster);
148 check_for_prepared_transactions(&new_cluster);
153 report_clusters_compatible(void)
157 pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
158 /* stops new cluster */
159 stop_postmaster(false);
163 pg_log(PG_REPORT, "\n"
164 "If pg_upgrade fails after this point, you must re-initdb the\n"
165 "new cluster before continuing.\n");
172 /* Create dummy large object permissions for old < PG 9.0? */
173 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
175 start_postmaster(&new_cluster, true);
176 new_9_0_populate_pg_largeobject_metadata(&new_cluster, false);
177 stop_postmaster(false);
183 output_completion_banner(char *analyze_script_file_name,
184 char *deletion_script_file_name)
186 /* Did we copy the free space files? */
187 if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
189 "Optimizer statistics are not transferred by pg_upgrade so,\n"
190 "once you start the new server, consider running:\n"
191 " %s\n\n", analyze_script_file_name);
194 "Optimizer statistics and free space information are not transferred\n"
195 "by pg_upgrade so, once you start the new server, consider running:\n"
196 " %s\n\n", analyze_script_file_name);
199 if (deletion_script_file_name)
201 "Running this script will delete the old cluster's data files:\n"
203 deletion_script_file_name);
206 "Could not create a script to delete the old cluster's data files\n"
207 "because user-defined tablespaces or the new cluster's data directory\n"
208 "exist in the old cluster directory. The old cluster's contents must\n"
209 "be deleted manually.\n");
214 check_cluster_versions(void)
216 prep_status("Checking cluster versions");
218 /* get old and new cluster versions */
219 old_cluster.major_version = get_major_server_version(&old_cluster);
220 new_cluster.major_version = get_major_server_version(&new_cluster);
223 * We allow upgrades from/to the same major version for alpha/beta
227 if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
228 pg_fatal("This utility can only upgrade from PostgreSQL version 8.4 and later.\n");
230 /* Only current PG version is supported as a target */
231 if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
232 pg_fatal("This utility can only upgrade to PostgreSQL version %s.\n",
236 * We can't allow downgrading because we use the target pg_dump, and
237 * pg_dump cannot operate on newer database versions, only current and
240 if (old_cluster.major_version > new_cluster.major_version)
241 pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
243 /* Ensure binaries match the designated data directories */
244 if (GET_MAJOR_VERSION(old_cluster.major_version) !=
245 GET_MAJOR_VERSION(old_cluster.bin_version))
246 pg_fatal("Old cluster data and binary directories are from different major versions.\n");
247 if (GET_MAJOR_VERSION(new_cluster.major_version) !=
248 GET_MAJOR_VERSION(new_cluster.bin_version))
249 pg_fatal("New cluster data and binary directories are from different major versions.\n");
256 check_cluster_compatibility(bool live_check)
258 /* get/check pg_control data of servers */
259 get_control_data(&old_cluster, live_check);
260 get_control_data(&new_cluster, false);
261 check_control_data(&old_cluster.controldata, &new_cluster.controldata);
263 /* Is it 9.0 but without tablespace directories? */
264 if (GET_MAJOR_VERSION(new_cluster.major_version) == 900 &&
265 new_cluster.controldata.cat_ver < TABLE_SPACE_SUBDIRS_CAT_VER)
266 pg_fatal("This utility can only upgrade to PostgreSQL version 9.0 after 2010-01-11\n"
267 "because of backend API changes made during development.\n");
269 /* We read the real port number for PG >= 9.1 */
270 if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) < 901 &&
271 old_cluster.port == DEF_PGUPORT)
272 pg_fatal("When checking a pre-PG 9.1 live old server, "
273 "you must specify the old server's port number.\n");
275 if (live_check && old_cluster.port == new_cluster.port)
276 pg_fatal("When checking a live server, "
277 "the old and new port numbers must be different.\n");
282 * check_locale_and_encoding()
284 * Check that locale and encoding of a database in the old and new clusters
288 check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb)
290 if (olddb->db_encoding != newdb->db_encoding)
291 pg_fatal("encodings for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
293 pg_encoding_to_char(olddb->db_encoding),
294 pg_encoding_to_char(newdb->db_encoding));
295 if (!equivalent_locale(LC_COLLATE, olddb->db_collate, newdb->db_collate))
296 pg_fatal("lc_collate values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
297 olddb->db_name, olddb->db_collate, newdb->db_collate);
298 if (!equivalent_locale(LC_CTYPE, olddb->db_ctype, newdb->db_ctype))
299 pg_fatal("lc_ctype values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
300 olddb->db_name, olddb->db_ctype, newdb->db_ctype);
304 * equivalent_locale()
306 * Best effort locale-name comparison. Return false if we are not 100% sure
307 * the locales are equivalent.
309 * Note: The encoding parts of the names are ignored. This function is
310 * currently used to compare locale names stored in pg_database, and
311 * pg_database contains a separate encoding field. That's compared directly
312 * in check_locale_and_encoding().
315 equivalent_locale(int category, const char *loca, const char *locb)
325 * If the names are equal, the locales are equivalent. Checking this first
326 * avoids calling setlocale() in the common case that the names are equal.
327 * That's a good thing, if setlocale() is buggy, for example.
329 if (pg_strcasecmp(loca, locb) == 0)
333 * Not identical. Canonicalize both names, remove the encoding parts, and
336 canona = get_canonical_locale_name(category, loca);
337 chara = strrchr(canona, '.');
338 lena = chara ? (chara - canona) : strlen(canona);
340 canonb = get_canonical_locale_name(category, locb);
341 charb = strrchr(canonb, '.');
342 lenb = charb ? (charb - canonb) : strlen(canonb);
344 if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0)
358 check_new_cluster_is_empty(void)
362 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
365 RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
367 for (relnum = 0; relnum < rel_arr->nrels;
370 /* pg_largeobject and its index should be skipped */
371 if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
372 pg_fatal("New cluster database \"%s\" is not empty\n",
373 new_cluster.dbarr.dbs[dbnum].db_name);
379 * Check that every database that already exists in the new cluster is
380 * compatible with the corresponding database in the old one.
383 check_databases_are_compatible(void)
390 for (newdbnum = 0; newdbnum < new_cluster.dbarr.ndbs; newdbnum++)
392 newdbinfo = &new_cluster.dbarr.dbs[newdbnum];
394 /* Find the corresponding database in the old cluster */
395 for (olddbnum = 0; olddbnum < old_cluster.dbarr.ndbs; olddbnum++)
397 olddbinfo = &old_cluster.dbarr.dbs[olddbnum];
398 if (strcmp(newdbinfo->db_name, olddbinfo->db_name) == 0)
400 check_locale_and_encoding(olddbinfo, newdbinfo);
409 * create_script_for_cluster_analyze()
411 * This incrementally generates better optimizer statistics
414 create_script_for_cluster_analyze(char **analyze_script_file_name)
417 PQExpBufferData user_specification;
419 prep_status("Creating script to analyze new cluster");
421 initPQExpBuffer(&user_specification);
422 if (os_info.user_specified)
424 appendPQExpBufferStr(&user_specification, "-U ");
425 appendShellString(&user_specification, os_info.user);
426 appendPQExpBufferChar(&user_specification, ' ');
429 *analyze_script_file_name = psprintf("%sanalyze_new_cluster.%s",
430 SCRIPT_PREFIX, SCRIPT_EXT);
432 if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
433 pg_fatal("could not open file \"%s\": %s\n",
434 *analyze_script_file_name, strerror(errno));
437 /* add shebang header */
438 fprintf(script, "#!/bin/sh\n\n");
440 /* suppress command echoing */
441 fprintf(script, "@echo off\n");
444 fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n",
445 ECHO_QUOTE, ECHO_QUOTE);
446 fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n",
447 ECHO_QUOTE, ECHO_QUOTE);
448 fprintf(script, "echo %swith increasing accuracy. When it is done, your system will%s\n",
449 ECHO_QUOTE, ECHO_QUOTE);
450 fprintf(script, "echo %shave the default level of optimizer statistics.%s\n",
451 ECHO_QUOTE, ECHO_QUOTE);
452 fprintf(script, "echo%s\n\n", ECHO_BLANK);
454 fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n",
455 ECHO_QUOTE, ECHO_QUOTE);
456 fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n",
457 ECHO_QUOTE, ECHO_QUOTE);
458 fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n",
459 ECHO_QUOTE, ECHO_QUOTE);
460 fprintf(script, "echo%s\n\n", ECHO_BLANK);
462 fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n",
463 ECHO_QUOTE, ECHO_QUOTE);
464 fprintf(script, "echo %sthis script and run:%s\n",
465 ECHO_QUOTE, ECHO_QUOTE);
466 fprintf(script, "echo %s \"%s/vacuumdb\" %s--all %s%s\n", ECHO_QUOTE,
467 new_cluster.bindir, user_specification.data,
468 /* Did we copy the free space files? */
469 (GET_MAJOR_VERSION(old_cluster.major_version) >= 804) ?
470 "--analyze-only" : "--analyze", ECHO_QUOTE);
471 fprintf(script, "echo%s\n\n", ECHO_BLANK);
473 fprintf(script, "\"%s/vacuumdb\" %s--all --analyze-in-stages\n",
474 new_cluster.bindir, user_specification.data);
475 /* Did we copy the free space files? */
476 if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
477 fprintf(script, "\"%s/vacuumdb\" %s--all\n", new_cluster.bindir,
478 user_specification.data);
480 fprintf(script, "echo%s\n\n", ECHO_BLANK);
481 fprintf(script, "echo %sDone%s\n",
482 ECHO_QUOTE, ECHO_QUOTE);
487 if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
488 pg_fatal("could not add execute permission to file \"%s\": %s\n",
489 *analyze_script_file_name, strerror(errno));
492 termPQExpBuffer(&user_specification);
499 * create_script_for_old_cluster_deletion()
501 * This is particularly useful for tablespace deletion.
504 create_script_for_old_cluster_deletion(char **deletion_script_file_name)
508 char old_cluster_pgdata[MAXPGPATH],
509 new_cluster_pgdata[MAXPGPATH];
511 *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
512 SCRIPT_PREFIX, SCRIPT_EXT);
514 strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
515 canonicalize_path(old_cluster_pgdata);
517 strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
518 canonicalize_path(new_cluster_pgdata);
520 /* Some people put the new data directory inside the old one. */
521 if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
524 "\nWARNING: new data directory should not be inside the old data directory, e.g. %s\n", old_cluster_pgdata);
526 /* Unlink file in case it is left over from a previous run. */
527 unlink(*deletion_script_file_name);
528 pg_free(*deletion_script_file_name);
529 *deletion_script_file_name = NULL;
534 * Some users (oddly) create tablespaces inside the cluster data
535 * directory. We can't create a proper old cluster delete script in that
538 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
540 char old_tablespace_dir[MAXPGPATH];
542 strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
543 canonicalize_path(old_tablespace_dir);
544 if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
546 /* reproduce warning from CREATE TABLESPACE that is in the log */
548 "\nWARNING: user-defined tablespace locations should not be inside the data directory, e.g. %s\n", old_tablespace_dir);
550 /* Unlink file in case it is left over from a previous run. */
551 unlink(*deletion_script_file_name);
552 pg_free(*deletion_script_file_name);
553 *deletion_script_file_name = NULL;
558 prep_status("Creating script to delete old cluster");
560 if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
561 pg_fatal("could not open file \"%s\": %s\n",
562 *deletion_script_file_name, strerror(errno));
565 /* add shebang header */
566 fprintf(script, "#!/bin/sh\n\n");
569 /* delete old cluster's default tablespace */
570 fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
571 fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
573 /* delete old cluster's alternate tablespaces */
574 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
577 * Do the old cluster's per-database directories share a directory
578 * with a new version-specific tablespace?
580 if (strlen(old_cluster.tablespace_suffix) == 0)
582 /* delete per-database directories */
585 fprintf(script, "\n");
586 /* remove PG_VERSION? */
587 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
588 fprintf(script, RM_CMD " %s%cPG_VERSION\n",
589 fix_path_separator(os_info.old_tablespaces[tblnum]),
592 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
593 fprintf(script, RMDIR_CMD " %c%s%c%d%c\n", PATH_QUOTE,
594 fix_path_separator(os_info.old_tablespaces[tblnum]),
595 PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid,
600 char *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
603 * Simply delete the tablespace directory, which might be ".old"
604 * or a version-specific subdirectory.
606 fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
607 fix_path_separator(os_info.old_tablespaces[tblnum]),
608 fix_path_separator(suffix_path), PATH_QUOTE);
616 if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
617 pg_fatal("could not add execute permission to file \"%s\": %s\n",
618 *deletion_script_file_name, strerror(errno));
626 * check_is_install_user()
628 * Check we are the install user, and that the new cluster
629 * has no other users.
632 check_is_install_user(ClusterInfo *cluster)
635 PGconn *conn = connectToServer(cluster, "template1");
637 prep_status("Checking database user is the install user");
639 /* Can't use pg_authid because only superusers can view it. */
640 res = executeQueryOrDie(conn,
641 "SELECT rolsuper, oid "
642 "FROM pg_catalog.pg_roles "
643 "WHERE rolname = current_user "
644 "AND rolname !~ '^pg_'");
647 * We only allow the install user in the new cluster (see comment below)
648 * and we preserve pg_authid.oid, so this must be the install user in the
651 if (PQntuples(res) != 1 ||
652 atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
653 pg_fatal("database user \"%s\" is not the install user\n",
658 res = executeQueryOrDie(conn,
660 "FROM pg_catalog.pg_roles "
661 "WHERE rolname !~ '^pg_'");
663 if (PQntuples(res) != 1)
664 pg_fatal("could not determine the number of users\n");
667 * We only allow the install user in the new cluster because other defined
668 * users might match users defined in the old cluster and generate an
669 * error during pg_dump restore.
671 if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1)
672 pg_fatal("Only the install user can be defined in the new cluster.\n");
683 check_proper_datallowconn(ClusterInfo *cluster)
686 PGconn *conn_template1;
692 prep_status("Checking database connection settings");
694 conn_template1 = connectToServer(cluster, "template1");
696 /* get database names */
697 dbres = executeQueryOrDie(conn_template1,
698 "SELECT datname, datallowconn "
699 "FROM pg_catalog.pg_database");
701 i_datname = PQfnumber(dbres, "datname");
702 i_datallowconn = PQfnumber(dbres, "datallowconn");
704 ntups = PQntuples(dbres);
705 for (dbnum = 0; dbnum < ntups; dbnum++)
707 char *datname = PQgetvalue(dbres, dbnum, i_datname);
708 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
710 if (strcmp(datname, "template0") == 0)
712 /* avoid restore failure when pg_dumpall tries to create template0 */
713 if (strcmp(datallowconn, "t") == 0)
714 pg_fatal("template0 must not allow connections, "
715 "i.e. its pg_database.datallowconn must be false\n");
720 * avoid datallowconn == false databases from being skipped on
723 if (strcmp(datallowconn, "f") == 0)
724 pg_fatal("All non-template0 databases must allow connections, "
725 "i.e. their pg_database.datallowconn must be true\n");
731 PQfinish(conn_template1);
738 * check_for_prepared_transactions()
740 * Make sure there are no prepared transactions because the storage format
741 * might have changed.
744 check_for_prepared_transactions(ClusterInfo *cluster)
747 PGconn *conn = connectToServer(cluster, "template1");
749 prep_status("Checking for prepared transactions");
751 res = executeQueryOrDie(conn,
753 "FROM pg_catalog.pg_prepared_xacts");
755 if (PQntuples(res) != 0)
756 pg_fatal("The %s cluster contains prepared transactions\n",
757 CLUSTER_NAME(cluster));
768 * check_for_isn_and_int8_passing_mismatch()
770 * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
771 * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
772 * it must match for the old and new servers.
775 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
780 char output_path[MAXPGPATH];
782 prep_status("Checking for contrib/isn with bigint-passing mismatch");
784 if (old_cluster.controldata.float8_pass_by_value ==
785 new_cluster.controldata.float8_pass_by_value)
792 snprintf(output_path, sizeof(output_path),
793 "contrib_isn_and_int8_pass_by_value.txt");
795 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
798 bool db_used = false;
803 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
804 PGconn *conn = connectToServer(cluster, active_db->db_name);
806 /* Find any functions coming from contrib/isn */
807 res = executeQueryOrDie(conn,
808 "SELECT n.nspname, p.proname "
809 "FROM pg_catalog.pg_proc p, "
810 " pg_catalog.pg_namespace n "
811 "WHERE p.pronamespace = n.oid AND "
812 " p.probin = '$libdir/isn'");
814 ntups = PQntuples(res);
815 i_nspname = PQfnumber(res, "nspname");
816 i_proname = PQfnumber(res, "proname");
817 for (rowno = 0; rowno < ntups; rowno++)
820 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
821 pg_fatal("could not open file \"%s\": %s\n",
822 output_path, strerror(errno));
825 fprintf(script, "Database: %s\n", active_db->db_name);
828 fprintf(script, " %s.%s\n",
829 PQgetvalue(res, rowno, i_nspname),
830 PQgetvalue(res, rowno, i_proname));
843 pg_log(PG_REPORT, "fatal\n");
844 pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
845 "bigint data type. Your old and new clusters pass bigint values\n"
846 "differently so this cluster cannot currently be upgraded. You can\n"
847 "manually upgrade databases that use \"contrib/isn\" facilities and remove\n"
848 "\"contrib/isn\" from the old cluster and restart the upgrade. A list of\n"
849 "the problem functions is in the file:\n"
850 " %s\n\n", output_path);
858 * check_for_reg_data_type_usage()
859 * pg_upgrade only preserves these system values:
864 * Many of the reg* data types reference system catalog info that is
865 * not preserved, and hence these data types cannot be used in user
866 * tables upgraded by pg_upgrade.
869 check_for_reg_data_type_usage(ClusterInfo *cluster)
874 char output_path[MAXPGPATH];
876 prep_status("Checking for reg* system OID user data types");
878 snprintf(output_path, sizeof(output_path), "tables_using_reg.txt");
880 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
883 bool db_used = false;
889 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
890 PGconn *conn = connectToServer(cluster, active_db->db_name);
893 * While several relkinds don't store any data, e.g. views, they can
894 * be used to define data types of other columns, so we check all
897 res = executeQueryOrDie(conn,
898 "SELECT n.nspname, c.relname, a.attname "
899 "FROM pg_catalog.pg_class c, "
900 " pg_catalog.pg_namespace n, "
901 " pg_catalog.pg_attribute a "
902 "WHERE c.oid = a.attrelid AND "
903 " NOT a.attisdropped AND "
905 " 'pg_catalog.regproc'::pg_catalog.regtype, "
906 " 'pg_catalog.regprocedure'::pg_catalog.regtype, "
907 " 'pg_catalog.regoper'::pg_catalog.regtype, "
908 " 'pg_catalog.regoperator'::pg_catalog.regtype, "
909 /* regclass.oid is preserved, so 'regclass' is OK */
910 /* regtype.oid is preserved, so 'regtype' is OK */
911 " 'pg_catalog.regconfig'::pg_catalog.regtype, "
912 " 'pg_catalog.regdictionary'::pg_catalog.regtype) AND "
913 " c.relnamespace = n.oid AND "
914 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
916 ntups = PQntuples(res);
917 i_nspname = PQfnumber(res, "nspname");
918 i_relname = PQfnumber(res, "relname");
919 i_attname = PQfnumber(res, "attname");
920 for (rowno = 0; rowno < ntups; rowno++)
923 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
924 pg_fatal("could not open file \"%s\": %s\n",
925 output_path, strerror(errno));
928 fprintf(script, "Database: %s\n", active_db->db_name);
931 fprintf(script, " %s.%s.%s\n",
932 PQgetvalue(res, rowno, i_nspname),
933 PQgetvalue(res, rowno, i_relname),
934 PQgetvalue(res, rowno, i_attname));
947 pg_log(PG_REPORT, "fatal\n");
948 pg_fatal("Your installation contains one of the reg* data types in user tables.\n"
949 "These data types reference system OIDs that are not preserved by\n"
950 "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
951 "remove the problem tables and restart the upgrade. A list of the problem\n"
952 "columns is in the file:\n"
953 " %s\n\n", output_path);
961 * check_for_jsonb_9_4_usage()
963 * JSONB changed its storage format during 9.4 beta, so check for it.
966 check_for_jsonb_9_4_usage(ClusterInfo *cluster)
971 char output_path[MAXPGPATH];
973 prep_status("Checking for JSONB user data types");
975 snprintf(output_path, sizeof(output_path), "tables_using_jsonb.txt");
977 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
980 bool db_used = false;
986 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
987 PGconn *conn = connectToServer(cluster, active_db->db_name);
990 * While several relkinds don't store any data, e.g. views, they can
991 * be used to define data types of other columns, so we check all
994 res = executeQueryOrDie(conn,
995 "SELECT n.nspname, c.relname, a.attname "
996 "FROM pg_catalog.pg_class c, "
997 " pg_catalog.pg_namespace n, "
998 " pg_catalog.pg_attribute a "
999 "WHERE c.oid = a.attrelid AND "
1000 " NOT a.attisdropped AND "
1001 " a.atttypid = 'pg_catalog.jsonb'::pg_catalog.regtype AND "
1002 " c.relnamespace = n.oid AND "
1003 /* exclude possible orphaned temp tables */
1004 " n.nspname !~ '^pg_temp_' AND "
1005 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
1007 ntups = PQntuples(res);
1008 i_nspname = PQfnumber(res, "nspname");
1009 i_relname = PQfnumber(res, "relname");
1010 i_attname = PQfnumber(res, "attname");
1011 for (rowno = 0; rowno < ntups; rowno++)
1014 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1015 pg_fatal("could not open file \"%s\": %s\n",
1016 output_path, strerror(errno));
1019 fprintf(script, "Database: %s\n", active_db->db_name);
1022 fprintf(script, " %s.%s.%s\n",
1023 PQgetvalue(res, rowno, i_nspname),
1024 PQgetvalue(res, rowno, i_relname),
1025 PQgetvalue(res, rowno, i_attname));
1038 pg_log(PG_REPORT, "fatal\n");
1039 pg_fatal("Your installation contains one of the JSONB data types in user tables.\n"
1040 "The internal format of JSONB changed during 9.4 beta so this cluster cannot currently\n"
1041 "be upgraded. You can remove the problem tables and restart the upgrade. A list\n"
1042 "of the problem columns is in the file:\n"
1043 " %s\n\n", output_path);
1050 * check_for_pg_role_prefix()
1052 * Versions older than 9.6 should not have any pg_* roles
1055 check_for_pg_role_prefix(ClusterInfo *cluster)
1058 PGconn *conn = connectToServer(cluster, "template1");
1060 prep_status("Checking for roles starting with 'pg_'");
1062 res = executeQueryOrDie(conn,
1064 "FROM pg_catalog.pg_roles "
1065 "WHERE rolname ~ '^pg_'");
1067 if (PQntuples(res) != 0)
1068 pg_fatal("The %s cluster contains roles starting with 'pg_'\n",
1069 CLUSTER_NAME(cluster));
1080 * get_canonical_locale_name
1082 * Send the locale name to the system, and hope we get back a canonical
1083 * version. This should match the backend's check_locale() function.
1086 get_canonical_locale_name(int category, const char *locale)
1091 /* get the current setting, so we can restore it. */
1092 save = setlocale(category, NULL);
1094 pg_fatal("failed to get the current locale\n");
1096 /* 'save' may be pointing at a modifiable scratch variable, so copy it. */
1097 save = pg_strdup(save);
1099 /* set the locale with setlocale, to see if it accepts it. */
1100 res = setlocale(category, locale);
1103 pg_fatal("failed to get system locale name for \"%s\"\n", locale);
1105 res = pg_strdup(res);
1107 /* restore old value. */
1108 if (!setlocale(category, save))
1109 pg_fatal("failed to restore old locale \"%s\"\n", save);