12 #include "catalog/pg_authid_d.h"
52 for (
c = result; *
c !=
'\0';
c++)
69 "Performing Consistency Checks on Old Live Server\n"
70 "------------------------------------------------");
75 "Performing Consistency Checks\n"
76 "-----------------------------");
230 "If pg_upgrade fails after this point, you must re-initdb the\n"
231 "new cluster before continuing.");
270 "Optimizer statistics are not transferred by pg_upgrade.\n"
271 "Once you start the new server, consider running:\n"
274 if (deletion_script_file_name)
276 "Running this script will delete the old cluster's data files:\n"
278 deletion_script_file_name);
281 "Could not create a script to delete the old cluster's data files\n"
282 "because user-defined tablespaces or the new cluster's data directory\n"
283 "exist in the old cluster directory. The old cluster's contents must\n"
284 "be deleted manually.");
305 pg_fatal(
"This utility can only upgrade from PostgreSQL version %s and later.",
310 pg_fatal(
"This utility can only upgrade to PostgreSQL version %s.",
319 pg_fatal(
"This utility cannot be used to downgrade to older major PostgreSQL versions.");
324 pg_fatal(
"Old cluster data and binary directories are from different major versions.");
327 pg_fatal(
"New cluster data and binary directories are from different major versions.");
342 pg_fatal(
"When checking a live server, "
343 "the old and new port numbers must be different.");
357 for (relnum = 0; relnum < rel_arr->
nrels;
361 if (strcmp(rel_arr->
rels[relnum].
nspname,
"pg_catalog") != 0)
362 pg_fatal(
"New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
385 prep_status(
"Checking for new cluster tablespace directories");
395 if (
stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
396 pg_fatal(
"new cluster tablespace directory already exists: \"%s\"",
416 *deletion_script_file_name =
psprintf(
"%sdelete_old_cluster.%s",
429 "\nWARNING: new data directory should not be inside the old data directory, i.e. %s", old_cluster_pgdata);
432 unlink(*deletion_script_file_name);
433 pg_free(*deletion_script_file_name);
434 *deletion_script_file_name = NULL;
453 "\nWARNING: user-defined tablespace locations should not be inside the data directory, i.e. %s", old_tablespace_dir);
456 unlink(*deletion_script_file_name);
457 pg_free(*deletion_script_file_name);
458 *deletion_script_file_name = NULL;
463 prep_status(
"Creating script to delete old cluster");
465 if ((script =
fopen_priv(*deletion_script_file_name,
"w")) == NULL)
466 pg_fatal(
"could not open file \"%s\": %s",
467 *deletion_script_file_name,
strerror(errno));
471 fprintf(script,
"#!/bin/sh\n\n");
516 if (chmod(*deletion_script_file_name,
S_IRWXU) != 0)
517 pg_fatal(
"could not add execute permission to file \"%s\": %s",
518 *deletion_script_file_name,
strerror(errno));
537 prep_status(
"Checking database user is the install user");
541 "SELECT rolsuper, oid "
542 "FROM pg_catalog.pg_roles "
543 "WHERE rolname = current_user "
544 "AND rolname !~ '^pg_'");
553 pg_fatal(
"database user \"%s\" is not the install user",
560 "FROM pg_catalog.pg_roles "
561 "WHERE rolname !~ '^pg_'");
564 pg_fatal(
"could not determine the number of users");
572 pg_fatal(
"Only the install user can be defined in the new cluster.");
601 prep_status(
"Checking database connection settings");
603 snprintf(output_path,
sizeof(output_path),
"%s/%s",
605 "databases_with_datallowconn_false.txt");
611 "SELECT datname, datallowconn "
612 "FROM pg_catalog.pg_database");
615 i_datallowconn =
PQfnumber(dbres,
"datallowconn");
618 for (dbnum = 0; dbnum < ntups; dbnum++)
623 if (strcmp(
datname,
"template0") == 0)
627 pg_fatal(
"template0 must not allow connections, "
628 "i.e. its pg_database.datallowconn must be false");
638 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
639 pg_fatal(
"could not open file \"%s\": %s",
655 pg_fatal(
"All non-template0 databases must allow connections, i.e. their\n"
656 "pg_database.datallowconn must be true. Your installation contains\n"
657 "non-template0 databases with their pg_database.datallowconn set to\n"
658 "false. Consider allowing connection for all non-template0 databases\n"
659 "or drop the databases which do not allow connections. A list of\n"
660 "databases with the problem is in the file:\n"
684 "FROM pg_catalog.pg_prepared_xacts");
689 pg_fatal(
"The source cluster contains prepared transactions");
691 pg_fatal(
"The target cluster contains prepared transactions");
716 prep_status(
"Checking for contrib/isn with bigint-passing mismatch");
726 snprintf(output_path,
sizeof(output_path),
"%s/%s",
728 "contrib_isn_and_int8_pass_by_value.txt");
730 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
733 bool db_used =
false;
743 "SELECT n.nspname, p.proname "
744 "FROM pg_catalog.pg_proc p, "
745 " pg_catalog.pg_namespace n "
746 "WHERE p.pronamespace = n.oid AND "
747 " p.probin = '$libdir/isn'");
752 for (rowno = 0; rowno < ntups; rowno++)
754 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
755 pg_fatal(
"could not open file \"%s\": %s",
776 pg_fatal(
"Your installation contains \"contrib/isn\" functions which rely on the\n"
777 "bigint data type. Your old and new clusters pass bigint values\n"
778 "differently so this cluster cannot currently be upgraded. You can\n"
779 "manually dump databases in the old cluster that use \"contrib/isn\"\n"
780 "facilities, drop them, perform the upgrade, and then restore them. A\n"
781 "list of the problem functions is in the file:\n"
798 prep_status(
"Checking for user-defined postfix operators");
800 snprintf(output_path,
sizeof(output_path),
"%s/%s",
805 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
808 bool db_used =
false;
826 "SELECT o.oid AS oproid, "
827 " n.nspname AS oprnsp, "
829 " tn.nspname AS typnsp, "
831 "FROM pg_catalog.pg_operator o, "
832 " pg_catalog.pg_namespace n, "
833 " pg_catalog.pg_type t, "
834 " pg_catalog.pg_namespace tn "
835 "WHERE o.oprnamespace = n.oid AND "
836 " o.oprleft = t.oid AND "
837 " t.typnamespace = tn.oid AND "
838 " o.oprright = 0 AND "
846 for (rowno = 0; rowno < ntups; rowno++)
848 if (script == NULL &&
849 (script =
fopen_priv(output_path,
"w")) == NULL)
850 pg_fatal(
"could not open file \"%s\": %s",
857 fprintf(script,
" (oid=%s) %s.%s (%s.%s, NONE)\n",
874 pg_fatal(
"Your installation contains user-defined postfix operators, which are not\n"
875 "supported anymore. Consider dropping the postfix operators and replacing\n"
876 "them with prefix operators or function calls.\n"
877 "A list of user-defined postfix operators is in the file:\n"
898 prep_status(
"Checking for incompatible polymorphic functions");
900 snprintf(output_path,
sizeof(output_path),
"%s/%s",
902 "incompatible_polymorphics.txt");
908 "'array_append(anyarray,anyelement)'"
909 ", 'array_cat(anyarray,anyarray)'"
910 ", 'array_prepend(anyelement,anyarray)'");
914 ", 'array_remove(anyarray,anyelement)'"
915 ", 'array_replace(anyarray,anyelement,anyelement)'");
919 ", 'array_position(anyarray,anyelement)'"
920 ", 'array_position(anyarray,anyelement,integer)'"
921 ", 'array_positions(anyarray,anyelement)'"
922 ", 'width_bucket(anyelement,anyarray)'");
924 for (
int dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
926 bool db_used =
false;
941 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
943 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
944 "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
945 "WHERE p.oid >= 16384 "
946 "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
947 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
951 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
953 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
954 "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
955 "WHERE p.oid >= 16384 "
956 "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
957 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
961 "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
962 "FROM pg_operator AS op "
963 "WHERE op.oid >= 16384 "
964 "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
965 "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[]);",
966 old_polymorphics.
data,
967 old_polymorphics.
data,
968 old_polymorphics.
data);
975 for (
int rowno = 0; rowno < ntups; rowno++)
977 if (script == NULL &&
978 (script =
fopen_priv(output_path,
"w")) == NULL)
979 pg_fatal(
"could not open file \"%s\": %s",
1000 pg_fatal(
"Your installation contains user-defined objects that refer to internal\n"
1001 "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
1002 "These user-defined objects must be dropped before upgrading and restored\n"
1003 "afterwards, changing them to refer to the new corresponding functions with\n"
1004 "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
1005 "A list of the problematic objects is in the file:\n"
1006 " %s", output_path);
1021 FILE *script = NULL;
1026 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1028 "tables_with_oids.txt");
1031 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
1034 bool db_used =
false;
1043 "SELECT n.nspname, c.relname "
1044 "FROM pg_catalog.pg_class c, "
1045 " pg_catalog.pg_namespace n "
1046 "WHERE c.relnamespace = n.oid AND "
1048 " n.nspname NOT IN ('pg_catalog')");
1053 for (rowno = 0; rowno < ntups; rowno++)
1055 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
1056 pg_fatal(
"could not open file \"%s\": %s",
1077 pg_fatal(
"Your installation contains tables declared WITH OIDS, which is not\n"
1078 "supported anymore. Consider removing the oid column using\n"
1079 " ALTER TABLE ... SET WITHOUT OIDS;\n"
1080 "A list of tables with the problem is in the file:\n"
1081 " %s", output_path);
1105 prep_status(
"Checking for system-defined composite types in user tables");
1107 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1109 "tables_using_composite.txt");
1122 firstUserOid = 16384;
1124 base_query =
psprintf(
"SELECT t.oid FROM pg_catalog.pg_type t "
1125 "LEFT JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid "
1126 " WHERE typtype = 'c' AND (t.oid < %u OR nspname = 'information_schema')",
1136 pg_fatal(
"Your installation contains system-defined composite type(s) in user tables.\n"
1137 "These type OIDs are not stable across PostgreSQL versions,\n"
1138 "so this cluster cannot currently be upgraded. You can\n"
1139 "drop the problem columns and restart the upgrade.\n"
1140 "A list of the problem columns is in the file:\n"
1141 " %s", output_path);
1164 prep_status(
"Checking for reg* data types in user tables");
1166 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1168 "tables_using_reg.txt");
1175 "SELECT oid FROM pg_catalog.pg_type t "
1176 "WHERE t.typnamespace = "
1177 " (SELECT oid FROM pg_catalog.pg_namespace "
1178 " WHERE nspname = 'pg_catalog') "
1179 " AND t.typname IN ( "
1183 " 'regdictionary', "
1197 pg_fatal(
"Your installation contains one of the reg* data types in user tables.\n"
1198 "These data types reference system OIDs that are not preserved by\n"
1199 "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
1200 "drop the problem columns and restart the upgrade.\n"
1201 "A list of the problem columns is in the file:\n"
1202 " %s", output_path);
1218 prep_status(
"Checking for incompatible aclitem data type in user tables");
1220 snprintf(output_path,
sizeof(output_path),
"tables_using_aclitem.txt");
1225 pg_fatal(
"Your installation contains the \"aclitem\" data type in user tables.\n"
1226 "The internal format of \"aclitem\" changed in PostgreSQL version 16\n"
1227 "so this cluster cannot currently be upgraded. You can drop the\n"
1228 "problem columns and restart the upgrade. A list of the problem\n"
1229 "columns is in the file:\n"
1230 " %s", output_path);
1246 prep_status(
"Checking for incompatible \"jsonb\" data type");
1248 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1250 "tables_using_jsonb.txt");
1255 pg_fatal(
"Your installation contains the \"jsonb\" data type in user tables.\n"
1256 "The internal format of \"jsonb\" changed during 9.4 beta so this\n"
1257 "cluster cannot currently be upgraded. You can\n"
1258 "drop the problem columns and restart the upgrade.\n"
1259 "A list of the problem columns is in the file:\n"
1260 " %s", output_path);
1279 FILE *script = NULL;
1282 prep_status(
"Checking for roles starting with \"pg_\"");
1284 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1286 "pg_role_prefix.txt");
1289 "SELECT oid AS roloid, rolname "
1290 "FROM pg_catalog.pg_roles "
1291 "WHERE rolname ~ '^pg_'");
1296 for (
int rowno = 0; rowno < ntups; rowno++)
1298 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
1299 pg_fatal(
"could not open file \"%s\": %s",
1301 fprintf(script,
"%s (oid=%s)\n",
1314 pg_fatal(
"Your installation contains roles starting with \"pg_\".\n"
1315 "\"pg_\" is a reserved prefix for system roles, the cluster\n"
1316 "cannot be upgraded until these roles are renamed.\n"
1317 "A list of roles starting with \"pg_\" is in the file:\n"
1318 " %s", output_path);
1331 FILE *script = NULL;
1334 prep_status(
"Checking for user-defined encoding conversions");
1336 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1338 "encoding_conversions.txt");
1341 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
1344 bool db_used =
false;
1360 "SELECT c.oid as conoid, c.conname, n.nspname "
1361 "FROM pg_catalog.pg_conversion c, "
1362 " pg_catalog.pg_namespace n "
1363 "WHERE c.connamespace = n.oid AND "
1369 for (rowno = 0; rowno < ntups; rowno++)
1371 if (script == NULL &&
1372 (script =
fopen_priv(output_path,
"w")) == NULL)
1373 pg_fatal(
"could not open file \"%s\": %s",
1380 fprintf(script,
" (oid=%s) %s.%s\n",
1395 pg_fatal(
"Your installation contains user-defined encoding conversions.\n"
1396 "The conversion function parameters changed in PostgreSQL version 14\n"
1397 "so this cluster cannot currently be upgraded. You can remove the\n"
1398 "encoding conversions in the old cluster and restart the upgrade.\n"
1399 "A list of user-defined encoding conversions is in the file:\n"
1400 " %s", output_path);
void output_check_banner(bool live_check)
void check_cluster_versions(void)
static void check_for_tables_with_oids(ClusterInfo *cluster)
static void check_for_pg_role_prefix(ClusterInfo *cluster)
void check_cluster_compatibility(bool live_check)
static void check_for_composite_data_type_usage(ClusterInfo *cluster)
void check_and_dump_old_cluster(bool live_check)
void issue_warnings_and_set_wal_level(void)
static char * fix_path_separator(char *path)
void check_new_cluster(void)
void report_clusters_compatible(void)
static void check_is_install_user(ClusterInfo *cluster)
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
static void check_new_cluster_is_empty(void)
static void check_for_user_defined_postfix_ops(ClusterInfo *cluster)
static void check_for_new_tablespace_dir(ClusterInfo *new_cluster)
void output_completion_banner(char *deletion_script_file_name)
static void check_for_prepared_transactions(ClusterInfo *cluster)
static void check_proper_datallowconn(ClusterInfo *cluster)
static void check_for_aclitem_data_type_usage(ClusterInfo *cluster)
static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster)
static void check_for_incompatible_polymorphics(ClusterInfo *cluster)
static void check_for_reg_data_type_usage(ClusterInfo *cluster)
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
void get_control_data(ClusterInfo *cluster, bool live_check)
void check_control_data(ControlData *oldctrl, ControlData *newctrl)
static void PGresult * res
void generate_old_dump(void)
void PQfinish(PGconn *conn)
int PQntuples(const PGresult *res)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
int PQfnumber(const PGresult *res, const char *field_name)
char * pg_strdup(const char *in)
void check_file_clone(void)
void check_hard_link(void)
void check_loadable_libraries(void)
void get_loadable_libraries(void)
void get_db_and_rel_infos(ClusterInfo *cluster)
static void check_ok(void)
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
static pid_t start_postmaster(void)
void init_tablespaces(void)
bool check_for_data_types_usage(ClusterInfo *cluster, const char *base_query, const char *output_path)
void old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster)
bool check_for_data_type_usage(ClusterInfo *cluster, const char *type_name, const char *output_path)
void cleanup_output_dirs(void)
void report_extension_updates(ClusterInfo *cluster)
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
void old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster)
void old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster)
#define fopen_priv(path, mode)
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
#define GET_MAJOR_VERSION(v)
void stop_postmaster(bool in_atexit)
void prep_status(const char *fmt,...) pg_attribute_printf(1
void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster, bool check_mode)
#define JSONB_FORMAT_CHANGE_CAT_VER
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
bool path_is_prefix_of_path(const char *path1, const char *path2)
void canonicalize_path(char *path)
size_t strlcpy(char *dst, const char *src, size_t siz)
void initPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
char * psprintf(const char *fmt,...)
void appendShellString(PQExpBuffer buf, const char *str)
const char * tablespace_suffix
bool float8_pass_by_value
transferMode transfer_mode