4 * server checks and output routines
6 * Copyright (c) 2010-2019, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/check.c
10 #include "postgres_fe.h"
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"
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_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)
66 "Performing Consistency Checks on Old Live Server\n"
67 "------------------------------------------------\n");
72 "Performing Consistency Checks\n"
73 "-----------------------------\n");
79 check_and_dump_old_cluster(bool live_check)
84 start_postmaster(&old_cluster, true);
86 /* Extract a list of databases and tables from the old cluster */
87 get_db_and_rel_infos(&old_cluster);
91 get_loadable_libraries();
95 * Check for various failure cases
97 check_is_install_user(&old_cluster);
98 check_proper_datallowconn(&old_cluster);
99 check_for_prepared_transactions(&old_cluster);
100 check_for_reg_data_type_usage(&old_cluster);
101 check_for_isn_and_int8_passing_mismatch(&old_cluster);
104 * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
105 * supported anymore. Verify there are none, iff applicable.
107 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
108 check_for_tables_with_oids(&old_cluster);
111 * PG 12 changed the 'sql_identifier' type storage to be based on name,
112 * not varchar, which breaks on-disk format for existing data. So we need
113 * to prevent upgrade when used in user objects (tables, indexes, ...).
115 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
116 old_11_check_for_sql_identifier_data_type_usage(&old_cluster);
119 * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
122 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
124 old_9_6_check_for_unknown_data_type_usage(&old_cluster);
126 old_9_6_invalidate_hash_indexes(&old_cluster, true);
129 /* 9.5 and below should not have roles starting with pg_ */
130 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
131 check_for_pg_role_prefix(&old_cluster);
133 if (GET_MAJOR_VERSION(old_cluster.major_version) == 904 &&
134 old_cluster.controldata.cat_ver < JSONB_FORMAT_CHANGE_CAT_VER)
135 check_for_jsonb_9_4_usage(&old_cluster);
137 /* Pre-PG 9.4 had a different 'line' data type internal format */
138 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 903)
139 old_9_3_check_for_line_data_type_usage(&old_cluster);
141 /* Pre-PG 9.0 had no large object permissions */
142 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
143 new_9_0_populate_pg_largeobject_metadata(&old_cluster, true);
146 * While not a check option, we do this now because this is the only time
147 * the old server is running.
149 if (!user_opts.check)
153 stop_postmaster(false);
158 check_new_cluster(void)
160 get_db_and_rel_infos(&new_cluster);
162 check_new_cluster_is_empty();
163 check_databases_are_compatible();
165 check_loadable_libraries();
167 switch (user_opts.transfer_mode)
169 case TRANSFER_MODE_CLONE:
172 case TRANSFER_MODE_COPY:
174 case TRANSFER_MODE_LINK:
179 check_is_install_user(&new_cluster);
181 check_for_prepared_transactions(&new_cluster);
186 report_clusters_compatible(void)
190 pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
191 /* stops new cluster */
192 stop_postmaster(false);
196 pg_log(PG_REPORT, "\n"
197 "If pg_upgrade fails after this point, you must re-initdb the\n"
198 "new cluster before continuing.\n");
203 issue_warnings_and_set_wal_level(void)
206 * We unconditionally start/stop the new server because pg_resetwal -o set
207 * wal_level to 'minimum'. If the user is upgrading standby servers using
208 * the rsync instructions, they will need pg_upgrade to write its final
209 * WAL record showing wal_level as 'replica'.
211 start_postmaster(&new_cluster, true);
213 /* Create dummy large object permissions for old < PG 9.0? */
214 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
215 new_9_0_populate_pg_largeobject_metadata(&new_cluster, false);
217 /* Reindex hash indexes for old < 10.0 */
218 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
219 old_9_6_invalidate_hash_indexes(&new_cluster, false);
221 stop_postmaster(false);
226 output_completion_banner(char *analyze_script_file_name,
227 char *deletion_script_file_name)
229 /* Did we copy the free space files? */
230 if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
232 "Optimizer statistics are not transferred by pg_upgrade so,\n"
233 "once you start the new server, consider running:\n"
234 " %s\n\n", analyze_script_file_name);
237 "Optimizer statistics and free space information are not transferred\n"
238 "by pg_upgrade so, once you start the new server, consider running:\n"
239 " %s\n\n", analyze_script_file_name);
242 if (deletion_script_file_name)
244 "Running this script will delete the old cluster's data files:\n"
246 deletion_script_file_name);
249 "Could not create a script to delete the old cluster's data files\n"
250 "because user-defined tablespaces or the new cluster's data directory\n"
251 "exist in the old cluster directory. The old cluster's contents must\n"
252 "be deleted manually.\n");
257 check_cluster_versions(void)
259 prep_status("Checking cluster versions");
261 /* cluster versions should already have been obtained */
262 Assert(old_cluster.major_version != 0);
263 Assert(new_cluster.major_version != 0);
266 * We allow upgrades from/to the same major version for alpha/beta
270 if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
271 pg_fatal("This utility can only upgrade from PostgreSQL version 8.4 and later.\n");
273 /* Only current PG version is supported as a target */
274 if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
275 pg_fatal("This utility can only upgrade to PostgreSQL version %s.\n",
279 * We can't allow downgrading because we use the target pg_dump, and
280 * pg_dump cannot operate on newer database versions, only current and
283 if (old_cluster.major_version > new_cluster.major_version)
284 pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
286 /* Ensure binaries match the designated data directories */
287 if (GET_MAJOR_VERSION(old_cluster.major_version) !=
288 GET_MAJOR_VERSION(old_cluster.bin_version))
289 pg_fatal("Old cluster data and binary directories are from different major versions.\n");
290 if (GET_MAJOR_VERSION(new_cluster.major_version) !=
291 GET_MAJOR_VERSION(new_cluster.bin_version))
292 pg_fatal("New cluster data and binary directories are from different major versions.\n");
299 check_cluster_compatibility(bool live_check)
301 /* get/check pg_control data of servers */
302 get_control_data(&old_cluster, live_check);
303 get_control_data(&new_cluster, false);
304 check_control_data(&old_cluster.controldata, &new_cluster.controldata);
306 /* We read the real port number for PG >= 9.1 */
307 if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) < 901 &&
308 old_cluster.port == DEF_PGUPORT)
309 pg_fatal("When checking a pre-PG 9.1 live old server, "
310 "you must specify the old server's port number.\n");
312 if (live_check && old_cluster.port == new_cluster.port)
313 pg_fatal("When checking a live server, "
314 "the old and new port numbers must be different.\n");
319 * check_locale_and_encoding()
321 * Check that locale and encoding of a database in the old and new clusters
325 check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb)
327 if (olddb->db_encoding != newdb->db_encoding)
328 pg_fatal("encodings for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
330 pg_encoding_to_char(olddb->db_encoding),
331 pg_encoding_to_char(newdb->db_encoding));
332 if (!equivalent_locale(LC_COLLATE, olddb->db_collate, newdb->db_collate))
333 pg_fatal("lc_collate values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
334 olddb->db_name, olddb->db_collate, newdb->db_collate);
335 if (!equivalent_locale(LC_CTYPE, olddb->db_ctype, newdb->db_ctype))
336 pg_fatal("lc_ctype values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
337 olddb->db_name, olddb->db_ctype, newdb->db_ctype);
341 * equivalent_locale()
343 * Best effort locale-name comparison. Return false if we are not 100% sure
344 * the locales are equivalent.
346 * Note: The encoding parts of the names are ignored. This function is
347 * currently used to compare locale names stored in pg_database, and
348 * pg_database contains a separate encoding field. That's compared directly
349 * in check_locale_and_encoding().
352 equivalent_locale(int category, const char *loca, const char *locb)
362 * If the names are equal, the locales are equivalent. Checking this first
363 * avoids calling setlocale() in the common case that the names are equal.
364 * That's a good thing, if setlocale() is buggy, for example.
366 if (pg_strcasecmp(loca, locb) == 0)
370 * Not identical. Canonicalize both names, remove the encoding parts, and
373 canona = get_canonical_locale_name(category, loca);
374 chara = strrchr(canona, '.');
375 lena = chara ? (chara - canona) : strlen(canona);
377 canonb = get_canonical_locale_name(category, locb);
378 charb = strrchr(canonb, '.');
379 lenb = charb ? (charb - canonb) : strlen(canonb);
381 if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0)
395 check_new_cluster_is_empty(void)
399 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
402 RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
404 for (relnum = 0; relnum < rel_arr->nrels;
407 /* pg_largeobject and its index should be skipped */
408 if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
409 pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"\n",
410 new_cluster.dbarr.dbs[dbnum].db_name,
411 rel_arr->rels[relnum].nspname,
412 rel_arr->rels[relnum].relname);
418 * Check that every database that already exists in the new cluster is
419 * compatible with the corresponding database in the old one.
422 check_databases_are_compatible(void)
429 for (newdbnum = 0; newdbnum < new_cluster.dbarr.ndbs; newdbnum++)
431 newdbinfo = &new_cluster.dbarr.dbs[newdbnum];
433 /* Find the corresponding database in the old cluster */
434 for (olddbnum = 0; olddbnum < old_cluster.dbarr.ndbs; olddbnum++)
436 olddbinfo = &old_cluster.dbarr.dbs[olddbnum];
437 if (strcmp(newdbinfo->db_name, olddbinfo->db_name) == 0)
439 check_locale_and_encoding(olddbinfo, newdbinfo);
448 * create_script_for_cluster_analyze()
450 * This incrementally generates better optimizer statistics
453 create_script_for_cluster_analyze(char **analyze_script_file_name)
456 PQExpBufferData user_specification;
458 prep_status("Creating script to analyze new cluster");
460 initPQExpBuffer(&user_specification);
461 if (os_info.user_specified)
463 appendPQExpBufferStr(&user_specification, "-U ");
464 appendShellString(&user_specification, os_info.user);
465 appendPQExpBufferChar(&user_specification, ' ');
468 *analyze_script_file_name = psprintf("%sanalyze_new_cluster.%s",
469 SCRIPT_PREFIX, SCRIPT_EXT);
471 if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
472 pg_fatal("could not open file \"%s\": %s\n",
473 *analyze_script_file_name, strerror(errno));
476 /* add shebang header */
477 fprintf(script, "#!/bin/sh\n\n");
479 /* suppress command echoing */
480 fprintf(script, "@echo off\n");
483 fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n",
484 ECHO_QUOTE, ECHO_QUOTE);
485 fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n",
486 ECHO_QUOTE, ECHO_QUOTE);
487 fprintf(script, "echo %swith increasing accuracy. When it is done, your system will%s\n",
488 ECHO_QUOTE, ECHO_QUOTE);
489 fprintf(script, "echo %shave the default level of optimizer statistics.%s\n",
490 ECHO_QUOTE, ECHO_QUOTE);
491 fprintf(script, "echo%s\n\n", ECHO_BLANK);
493 fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n",
494 ECHO_QUOTE, ECHO_QUOTE);
495 fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n",
496 ECHO_QUOTE, ECHO_QUOTE);
497 fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n",
498 ECHO_QUOTE, ECHO_QUOTE);
499 fprintf(script, "echo%s\n\n", ECHO_BLANK);
501 fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n",
502 ECHO_QUOTE, ECHO_QUOTE);
503 fprintf(script, "echo %sthis script and run:%s\n",
504 ECHO_QUOTE, ECHO_QUOTE);
505 fprintf(script, "echo %s \"%s/vacuumdb\" %s--all %s%s\n", ECHO_QUOTE,
506 new_cluster.bindir, user_specification.data,
507 /* Did we copy the free space files? */
508 (GET_MAJOR_VERSION(old_cluster.major_version) >= 804) ?
509 "--analyze-only" : "--analyze", ECHO_QUOTE);
510 fprintf(script, "echo%s\n\n", ECHO_BLANK);
512 fprintf(script, "\"%s/vacuumdb\" %s--all --analyze-in-stages\n",
513 new_cluster.bindir, user_specification.data);
514 /* Did we copy the free space files? */
515 if (GET_MAJOR_VERSION(old_cluster.major_version) < 804)
516 fprintf(script, "\"%s/vacuumdb\" %s--all\n", new_cluster.bindir,
517 user_specification.data);
519 fprintf(script, "echo%s\n\n", ECHO_BLANK);
520 fprintf(script, "echo %sDone%s\n",
521 ECHO_QUOTE, ECHO_QUOTE);
526 if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
527 pg_fatal("could not add execute permission to file \"%s\": %s\n",
528 *analyze_script_file_name, strerror(errno));
531 termPQExpBuffer(&user_specification);
538 * create_script_for_old_cluster_deletion()
540 * This is particularly useful for tablespace deletion.
543 create_script_for_old_cluster_deletion(char **deletion_script_file_name)
547 char old_cluster_pgdata[MAXPGPATH],
548 new_cluster_pgdata[MAXPGPATH];
550 *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
551 SCRIPT_PREFIX, SCRIPT_EXT);
553 strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
554 canonicalize_path(old_cluster_pgdata);
556 strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
557 canonicalize_path(new_cluster_pgdata);
559 /* Some people put the new data directory inside the old one. */
560 if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
563 "\nWARNING: new data directory should not be inside the old data directory, e.g. %s\n", old_cluster_pgdata);
565 /* Unlink file in case it is left over from a previous run. */
566 unlink(*deletion_script_file_name);
567 pg_free(*deletion_script_file_name);
568 *deletion_script_file_name = NULL;
573 * Some users (oddly) create tablespaces inside the cluster data
574 * directory. We can't create a proper old cluster delete script in that
577 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
579 char old_tablespace_dir[MAXPGPATH];
581 strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
582 canonicalize_path(old_tablespace_dir);
583 if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
585 /* reproduce warning from CREATE TABLESPACE that is in the log */
587 "\nWARNING: user-defined tablespace locations should not be inside the data directory, e.g. %s\n", old_tablespace_dir);
589 /* Unlink file in case it is left over from a previous run. */
590 unlink(*deletion_script_file_name);
591 pg_free(*deletion_script_file_name);
592 *deletion_script_file_name = NULL;
597 prep_status("Creating script to delete old cluster");
599 if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
600 pg_fatal("could not open file \"%s\": %s\n",
601 *deletion_script_file_name, strerror(errno));
604 /* add shebang header */
605 fprintf(script, "#!/bin/sh\n\n");
608 /* delete old cluster's default tablespace */
609 fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
610 fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
612 /* delete old cluster's alternate tablespaces */
613 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
616 * Do the old cluster's per-database directories share a directory
617 * with a new version-specific tablespace?
619 if (strlen(old_cluster.tablespace_suffix) == 0)
621 /* delete per-database directories */
624 fprintf(script, "\n");
625 /* remove PG_VERSION? */
626 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
627 fprintf(script, RM_CMD " %s%cPG_VERSION\n",
628 fix_path_separator(os_info.old_tablespaces[tblnum]),
631 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
632 fprintf(script, RMDIR_CMD " %c%s%c%d%c\n", PATH_QUOTE,
633 fix_path_separator(os_info.old_tablespaces[tblnum]),
634 PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid,
639 char *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
642 * Simply delete the tablespace directory, which might be ".old"
643 * or a version-specific subdirectory.
645 fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
646 fix_path_separator(os_info.old_tablespaces[tblnum]),
647 fix_path_separator(suffix_path), PATH_QUOTE);
655 if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
656 pg_fatal("could not add execute permission to file \"%s\": %s\n",
657 *deletion_script_file_name, strerror(errno));
665 * check_is_install_user()
667 * Check we are the install user, and that the new cluster
668 * has no other users.
671 check_is_install_user(ClusterInfo *cluster)
674 PGconn *conn = connectToServer(cluster, "template1");
676 prep_status("Checking database user is the install user");
678 /* Can't use pg_authid because only superusers can view it. */
679 res = executeQueryOrDie(conn,
680 "SELECT rolsuper, oid "
681 "FROM pg_catalog.pg_roles "
682 "WHERE rolname = current_user "
683 "AND rolname !~ '^pg_'");
686 * We only allow the install user in the new cluster (see comment below)
687 * and we preserve pg_authid.oid, so this must be the install user in the
690 if (PQntuples(res) != 1 ||
691 atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
692 pg_fatal("database user \"%s\" is not the install user\n",
697 res = executeQueryOrDie(conn,
699 "FROM pg_catalog.pg_roles "
700 "WHERE rolname !~ '^pg_'");
702 if (PQntuples(res) != 1)
703 pg_fatal("could not determine the number of users\n");
706 * We only allow the install user in the new cluster because other defined
707 * users might match users defined in the old cluster and generate an
708 * error during pg_dump restore.
710 if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1)
711 pg_fatal("Only the install user can be defined in the new cluster.\n");
722 check_proper_datallowconn(ClusterInfo *cluster)
725 PGconn *conn_template1;
731 prep_status("Checking database connection settings");
733 conn_template1 = connectToServer(cluster, "template1");
735 /* get database names */
736 dbres = executeQueryOrDie(conn_template1,
737 "SELECT datname, datallowconn "
738 "FROM pg_catalog.pg_database");
740 i_datname = PQfnumber(dbres, "datname");
741 i_datallowconn = PQfnumber(dbres, "datallowconn");
743 ntups = PQntuples(dbres);
744 for (dbnum = 0; dbnum < ntups; dbnum++)
746 char *datname = PQgetvalue(dbres, dbnum, i_datname);
747 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
749 if (strcmp(datname, "template0") == 0)
751 /* avoid restore failure when pg_dumpall tries to create template0 */
752 if (strcmp(datallowconn, "t") == 0)
753 pg_fatal("template0 must not allow connections, "
754 "i.e. its pg_database.datallowconn must be false\n");
759 * avoid datallowconn == false databases from being skipped on
762 if (strcmp(datallowconn, "f") == 0)
763 pg_fatal("All non-template0 databases must allow connections, "
764 "i.e. their pg_database.datallowconn must be true\n");
770 PQfinish(conn_template1);
777 * check_for_prepared_transactions()
779 * Make sure there are no prepared transactions because the storage format
780 * might have changed.
783 check_for_prepared_transactions(ClusterInfo *cluster)
786 PGconn *conn = connectToServer(cluster, "template1");
788 prep_status("Checking for prepared transactions");
790 res = executeQueryOrDie(conn,
792 "FROM pg_catalog.pg_prepared_xacts");
794 if (PQntuples(res) != 0)
796 if (cluster == &old_cluster)
797 pg_fatal("The source cluster contains prepared transactions\n");
799 pg_fatal("The target cluster contains prepared transactions\n");
811 * check_for_isn_and_int8_passing_mismatch()
813 * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
814 * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
815 * it must match for the old and new servers.
818 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
823 char output_path[MAXPGPATH];
825 prep_status("Checking for contrib/isn with bigint-passing mismatch");
827 if (old_cluster.controldata.float8_pass_by_value ==
828 new_cluster.controldata.float8_pass_by_value)
835 snprintf(output_path, sizeof(output_path),
836 "contrib_isn_and_int8_pass_by_value.txt");
838 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
841 bool db_used = false;
846 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
847 PGconn *conn = connectToServer(cluster, active_db->db_name);
849 /* Find any functions coming from contrib/isn */
850 res = executeQueryOrDie(conn,
851 "SELECT n.nspname, p.proname "
852 "FROM pg_catalog.pg_proc p, "
853 " pg_catalog.pg_namespace n "
854 "WHERE p.pronamespace = n.oid AND "
855 " p.probin = '$libdir/isn'");
857 ntups = PQntuples(res);
858 i_nspname = PQfnumber(res, "nspname");
859 i_proname = PQfnumber(res, "proname");
860 for (rowno = 0; rowno < ntups; rowno++)
863 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
864 pg_fatal("could not open file \"%s\": %s\n",
865 output_path, strerror(errno));
868 fprintf(script, "In database: %s\n", active_db->db_name);
871 fprintf(script, " %s.%s\n",
872 PQgetvalue(res, rowno, i_nspname),
873 PQgetvalue(res, rowno, i_proname));
886 pg_log(PG_REPORT, "fatal\n");
887 pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
888 "bigint data type. Your old and new clusters pass bigint values\n"
889 "differently so this cluster cannot currently be upgraded. You can\n"
890 "manually upgrade databases that use \"contrib/isn\" facilities and remove\n"
891 "\"contrib/isn\" from the old cluster and restart the upgrade. A list of\n"
892 "the problem functions is in the file:\n"
893 " %s\n\n", output_path);
901 * Verify that no tables are declared WITH OIDS.
904 check_for_tables_with_oids(ClusterInfo *cluster)
909 char output_path[MAXPGPATH];
911 prep_status("Checking for tables WITH OIDS");
913 snprintf(output_path, sizeof(output_path),
914 "tables_with_oids.txt");
916 /* Find any tables declared WITH OIDS */
917 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
920 bool db_used = false;
925 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
926 PGconn *conn = connectToServer(cluster, active_db->db_name);
928 res = executeQueryOrDie(conn,
929 "SELECT n.nspname, c.relname "
930 "FROM pg_catalog.pg_class c, "
931 " pg_catalog.pg_namespace n "
932 "WHERE c.relnamespace = n.oid AND "
934 " n.nspname NOT IN ('pg_catalog')");
936 ntups = PQntuples(res);
937 i_nspname = PQfnumber(res, "nspname");
938 i_relname = PQfnumber(res, "relname");
939 for (rowno = 0; rowno < ntups; rowno++)
942 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
943 pg_fatal("could not open file \"%s\": %s\n",
944 output_path, strerror(errno));
947 fprintf(script, "In database: %s\n", active_db->db_name);
950 fprintf(script, " %s.%s\n",
951 PQgetvalue(res, rowno, i_nspname),
952 PQgetvalue(res, rowno, i_relname));
965 pg_log(PG_REPORT, "fatal\n");
966 pg_fatal("Your installation contains tables declared WITH OIDS, which is not supported\n"
967 "anymore. Consider removing the oid column using\n"
968 " ALTER TABLE ... SET WITHOUT OIDS;\n"
969 "A list of tables with the problem is in the file:\n"
970 " %s\n\n", output_path);
978 * check_for_reg_data_type_usage()
979 * pg_upgrade only preserves these system values:
984 * Many of the reg* data types reference system catalog info that is
985 * not preserved, and hence these data types cannot be used in user
986 * tables upgraded by pg_upgrade.
989 check_for_reg_data_type_usage(ClusterInfo *cluster)
994 char output_path[MAXPGPATH];
996 prep_status("Checking for reg* data types in user tables");
998 snprintf(output_path, sizeof(output_path), "tables_using_reg.txt");
1000 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1003 bool db_used = false;
1009 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1010 PGconn *conn = connectToServer(cluster, active_db->db_name);
1013 * While several relkinds don't store any data, e.g. views, they can
1014 * be used to define data types of other columns, so we check all
1017 res = executeQueryOrDie(conn,
1018 "SELECT n.nspname, c.relname, a.attname "
1019 "FROM pg_catalog.pg_class c, "
1020 " pg_catalog.pg_namespace n, "
1021 " pg_catalog.pg_attribute a, "
1022 " pg_catalog.pg_type t "
1023 "WHERE c.oid = a.attrelid AND "
1024 " NOT a.attisdropped AND "
1025 " a.atttypid = t.oid AND "
1026 " t.typnamespace = "
1027 " (SELECT oid FROM pg_namespace "
1028 " WHERE nspname = 'pg_catalog') AND"
1030 /* regclass.oid is preserved, so 'regclass' is OK */
1032 " 'regdictionary', "
1038 /* regrole.oid is preserved, so 'regrole' is OK */
1039 /* regtype.oid is preserved, so 'regtype' is OK */
1041 " c.relnamespace = n.oid AND "
1042 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
1044 ntups = PQntuples(res);
1045 i_nspname = PQfnumber(res, "nspname");
1046 i_relname = PQfnumber(res, "relname");
1047 i_attname = PQfnumber(res, "attname");
1048 for (rowno = 0; rowno < ntups; rowno++)
1051 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1052 pg_fatal("could not open file \"%s\": %s\n",
1053 output_path, strerror(errno));
1056 fprintf(script, "In database: %s\n", active_db->db_name);
1059 fprintf(script, " %s.%s.%s\n",
1060 PQgetvalue(res, rowno, i_nspname),
1061 PQgetvalue(res, rowno, i_relname),
1062 PQgetvalue(res, rowno, i_attname));
1075 pg_log(PG_REPORT, "fatal\n");
1076 pg_fatal("Your installation contains one of the reg* data types in user tables.\n"
1077 "These data types reference system OIDs that are not preserved by\n"
1078 "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
1079 "remove the problem tables and restart the upgrade. A list of the problem\n"
1080 "columns is in the file:\n"
1081 " %s\n\n", output_path);
1089 * check_for_jsonb_9_4_usage()
1091 * JSONB changed its storage format during 9.4 beta, so check for it.
1094 check_for_jsonb_9_4_usage(ClusterInfo *cluster)
1097 FILE *script = NULL;
1099 char output_path[MAXPGPATH];
1101 prep_status("Checking for incompatible \"jsonb\" data type");
1103 snprintf(output_path, sizeof(output_path), "tables_using_jsonb.txt");
1105 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1108 bool db_used = false;
1114 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1115 PGconn *conn = connectToServer(cluster, active_db->db_name);
1118 * While several relkinds don't store any data, e.g. views, they can
1119 * be used to define data types of other columns, so we check all
1122 res = executeQueryOrDie(conn,
1123 "SELECT n.nspname, c.relname, a.attname "
1124 "FROM pg_catalog.pg_class c, "
1125 " pg_catalog.pg_namespace n, "
1126 " pg_catalog.pg_attribute a "
1127 "WHERE c.oid = a.attrelid AND "
1128 " NOT a.attisdropped AND "
1129 " a.atttypid = 'pg_catalog.jsonb'::pg_catalog.regtype AND "
1130 " c.relnamespace = n.oid AND "
1131 /* exclude possible orphaned temp tables */
1132 " n.nspname !~ '^pg_temp_' AND "
1133 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
1135 ntups = PQntuples(res);
1136 i_nspname = PQfnumber(res, "nspname");
1137 i_relname = PQfnumber(res, "relname");
1138 i_attname = PQfnumber(res, "attname");
1139 for (rowno = 0; rowno < ntups; rowno++)
1142 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1143 pg_fatal("could not open file \"%s\": %s\n",
1144 output_path, strerror(errno));
1147 fprintf(script, "In database: %s\n", active_db->db_name);
1150 fprintf(script, " %s.%s.%s\n",
1151 PQgetvalue(res, rowno, i_nspname),
1152 PQgetvalue(res, rowno, i_relname),
1153 PQgetvalue(res, rowno, i_attname));
1166 pg_log(PG_REPORT, "fatal\n");
1167 pg_fatal("Your installation contains the \"jsonb\" data type in user tables.\n"
1168 "The internal format of \"jsonb\" changed during 9.4 beta so this cluster cannot currently\n"
1169 "be upgraded. You can remove the problem tables and restart the upgrade. A list\n"
1170 "of the problem columns is in the file:\n"
1171 " %s\n\n", output_path);
1178 * check_for_pg_role_prefix()
1180 * Versions older than 9.6 should not have any pg_* roles
1183 check_for_pg_role_prefix(ClusterInfo *cluster)
1186 PGconn *conn = connectToServer(cluster, "template1");
1188 prep_status("Checking for roles starting with \"pg_\"");
1190 res = executeQueryOrDie(conn,
1192 "FROM pg_catalog.pg_roles "
1193 "WHERE rolname ~ '^pg_'");
1195 if (PQntuples(res) != 0)
1197 if (cluster == &old_cluster)
1198 pg_fatal("The source cluster contains roles starting with \"pg_\"\n");
1200 pg_fatal("The target cluster contains roles starting with \"pg_\"\n");
1212 * get_canonical_locale_name
1214 * Send the locale name to the system, and hope we get back a canonical
1215 * version. This should match the backend's check_locale() function.
1218 get_canonical_locale_name(int category, const char *locale)
1223 /* get the current setting, so we can restore it. */
1224 save = setlocale(category, NULL);
1226 pg_fatal("failed to get the current locale\n");
1228 /* 'save' may be pointing at a modifiable scratch variable, so copy it. */
1229 save = pg_strdup(save);
1231 /* set the locale with setlocale, to see if it accepts it. */
1232 res = setlocale(category, locale);
1235 pg_fatal("failed to get system locale name for \"%s\"\n", locale);
1237 res = pg_strdup(res);
1239 /* restore old value. */
1240 if (!setlocale(category, save))
1241 pg_fatal("failed to restore old locale \"%s\"\n", save);