12 #include "catalog/pg_authid_d.h"
31 const char *datatype);
55 for (
c = result; *
c !=
'\0';
c++)
72 "Performing Consistency Checks on Old Live Server\n"
73 "------------------------------------------------");
78 "Performing Consistency Checks\n"
79 "-----------------------------");
243 "If pg_upgrade fails after this point, you must re-initdb the\n"
244 "new cluster before continuing.");
283 "Optimizer statistics are not transferred by pg_upgrade.\n"
284 "Once you start the new server, consider running:\n"
287 if (deletion_script_file_name)
289 "Running this script will delete the old cluster's data files:\n"
291 deletion_script_file_name);
294 "Could not create a script to delete the old cluster's data files\n"
295 "because user-defined tablespaces or the new cluster's data directory\n"
296 "exist in the old cluster directory. The old cluster's contents must\n"
297 "be deleted manually.");
318 pg_fatal(
"This utility can only upgrade from PostgreSQL version %s and later.",
323 pg_fatal(
"This utility can only upgrade to PostgreSQL version %s.",
332 pg_fatal(
"This utility cannot be used to downgrade to older major PostgreSQL versions.");
337 pg_fatal(
"Old cluster data and binary directories are from different major versions.");
340 pg_fatal(
"New cluster data and binary directories are from different major versions.");
355 pg_fatal(
"When checking a live server, "
356 "the old and new port numbers must be different.");
370 for (relnum = 0; relnum < rel_arr->
nrels;
374 if (strcmp(rel_arr->
rels[relnum].
nspname,
"pg_catalog") != 0)
375 pg_fatal(
"New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
398 prep_status(
"Checking for new cluster tablespace directories");
408 if (
stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
409 pg_fatal(
"new cluster tablespace directory already exists: \"%s\"",
429 *deletion_script_file_name =
psprintf(
"%sdelete_old_cluster.%s",
442 "\nWARNING: new data directory should not be inside the old data directory, i.e. %s", old_cluster_pgdata);
445 unlink(*deletion_script_file_name);
446 pg_free(*deletion_script_file_name);
447 *deletion_script_file_name = NULL;
466 "\nWARNING: user-defined tablespace locations should not be inside the data directory, i.e. %s", old_tablespace_dir);
469 unlink(*deletion_script_file_name);
470 pg_free(*deletion_script_file_name);
471 *deletion_script_file_name = NULL;
476 prep_status(
"Creating script to delete old cluster");
478 if ((script =
fopen_priv(*deletion_script_file_name,
"w")) == NULL)
479 pg_fatal(
"could not open file \"%s\": %s",
480 *deletion_script_file_name,
strerror(errno));
484 fprintf(script,
"#!/bin/sh\n\n");
529 if (chmod(*deletion_script_file_name,
S_IRWXU) != 0)
530 pg_fatal(
"could not add execute permission to file \"%s\": %s",
531 *deletion_script_file_name,
strerror(errno));
550 prep_status(
"Checking database user is the install user");
554 "SELECT rolsuper, oid "
555 "FROM pg_catalog.pg_roles "
556 "WHERE rolname = current_user "
557 "AND rolname !~ '^pg_'");
566 pg_fatal(
"database user \"%s\" is not the install user",
573 "FROM pg_catalog.pg_roles "
574 "WHERE rolname !~ '^pg_'");
577 pg_fatal(
"could not determine the number of users");
585 pg_fatal(
"Only the install user can be defined in the new cluster.");
614 prep_status(
"Checking database connection settings");
616 snprintf(output_path,
sizeof(output_path),
"%s/%s",
618 "databases_with_datallowconn_false.txt");
624 "SELECT datname, datallowconn "
625 "FROM pg_catalog.pg_database");
628 i_datallowconn =
PQfnumber(dbres,
"datallowconn");
631 for (dbnum = 0; dbnum < ntups; dbnum++)
636 if (strcmp(
datname,
"template0") == 0)
640 pg_fatal(
"template0 must not allow connections, "
641 "i.e. its pg_database.datallowconn must be false");
651 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
652 pg_fatal(
"could not open file \"%s\": %s",
668 pg_fatal(
"All non-template0 databases must allow connections, i.e. their\n"
669 "pg_database.datallowconn must be true. Your installation contains\n"
670 "non-template0 databases with their pg_database.datallowconn set to\n"
671 "false. Consider allowing connection for all non-template0 databases\n"
672 "or drop the databases which do not allow connections. A list of\n"
673 "databases with the problem is in the file:\n"
697 "FROM pg_catalog.pg_prepared_xacts");
702 pg_fatal(
"The source cluster contains prepared transactions");
704 pg_fatal(
"The target cluster contains prepared transactions");
729 prep_status(
"Checking for contrib/isn with bigint-passing mismatch");
739 snprintf(output_path,
sizeof(output_path),
"%s/%s",
741 "contrib_isn_and_int8_pass_by_value.txt");
743 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
746 bool db_used =
false;
756 "SELECT n.nspname, p.proname "
757 "FROM pg_catalog.pg_proc p, "
758 " pg_catalog.pg_namespace n "
759 "WHERE p.pronamespace = n.oid AND "
760 " p.probin = '$libdir/isn'");
765 for (rowno = 0; rowno < ntups; rowno++)
767 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
768 pg_fatal(
"could not open file \"%s\": %s",
789 pg_fatal(
"Your installation contains \"contrib/isn\" functions which rely on the\n"
790 "bigint data type. Your old and new clusters pass bigint values\n"
791 "differently so this cluster cannot currently be upgraded. You can\n"
792 "manually dump databases in the old cluster that use \"contrib/isn\"\n"
793 "facilities, drop them, perform the upgrade, and then restore them. A\n"
794 "list of the problem functions is in the file:\n"
811 prep_status(
"Checking for user-defined postfix operators");
813 snprintf(output_path,
sizeof(output_path),
"%s/%s",
818 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
821 bool db_used =
false;
839 "SELECT o.oid AS oproid, "
840 " n.nspname AS oprnsp, "
842 " tn.nspname AS typnsp, "
844 "FROM pg_catalog.pg_operator o, "
845 " pg_catalog.pg_namespace n, "
846 " pg_catalog.pg_type t, "
847 " pg_catalog.pg_namespace tn "
848 "WHERE o.oprnamespace = n.oid AND "
849 " o.oprleft = t.oid AND "
850 " t.typnamespace = tn.oid AND "
851 " o.oprright = 0 AND "
859 for (rowno = 0; rowno < ntups; rowno++)
861 if (script == NULL &&
862 (script =
fopen_priv(output_path,
"w")) == NULL)
863 pg_fatal(
"could not open file \"%s\": %s",
870 fprintf(script,
" (oid=%s) %s.%s (%s.%s, NONE)\n",
887 pg_fatal(
"Your installation contains user-defined postfix operators, which are not\n"
888 "supported anymore. Consider dropping the postfix operators and replacing\n"
889 "them with prefix operators or function calls.\n"
890 "A list of user-defined postfix operators is in the file:\n"
911 prep_status(
"Checking for incompatible polymorphic functions");
913 snprintf(output_path,
sizeof(output_path),
"%s/%s",
915 "incompatible_polymorphics.txt");
921 "'array_append(anyarray,anyelement)'"
922 ", 'array_cat(anyarray,anyarray)'"
923 ", 'array_prepend(anyelement,anyarray)'");
927 ", 'array_remove(anyarray,anyelement)'"
928 ", 'array_replace(anyarray,anyelement,anyelement)'");
932 ", 'array_position(anyarray,anyelement)'"
933 ", 'array_position(anyarray,anyelement,integer)'"
934 ", 'array_positions(anyarray,anyelement)'"
935 ", 'width_bucket(anyelement,anyarray)'");
937 for (
int dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
939 bool db_used =
false;
954 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
956 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
957 "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
958 "WHERE p.oid >= 16384 "
959 "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
960 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
964 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
966 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
967 "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
968 "WHERE p.oid >= 16384 "
969 "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
970 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
974 "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
975 "FROM pg_operator AS op "
976 "WHERE op.oid >= 16384 "
977 "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
978 "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[]);",
979 old_polymorphics.
data,
980 old_polymorphics.
data,
981 old_polymorphics.
data);
988 for (
int rowno = 0; rowno < ntups; rowno++)
990 if (script == NULL &&
991 (script =
fopen_priv(output_path,
"w")) == NULL)
992 pg_fatal(
"could not open file \"%s\": %s",
1013 pg_fatal(
"Your installation contains user-defined objects that refer to internal\n"
1014 "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
1015 "These user-defined objects must be dropped before upgrading and restored\n"
1016 "afterwards, changing them to refer to the new corresponding functions with\n"
1017 "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
1018 "A list of the problematic objects is in the file:\n"
1019 " %s", output_path);
1034 FILE *script = NULL;
1039 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1041 "tables_with_oids.txt");
1044 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
1047 bool db_used =
false;
1056 "SELECT n.nspname, c.relname "
1057 "FROM pg_catalog.pg_class c, "
1058 " pg_catalog.pg_namespace n "
1059 "WHERE c.relnamespace = n.oid AND "
1061 " n.nspname NOT IN ('pg_catalog')");
1066 for (rowno = 0; rowno < ntups; rowno++)
1068 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
1069 pg_fatal(
"could not open file \"%s\": %s",
1090 pg_fatal(
"Your installation contains tables declared WITH OIDS, which is not\n"
1091 "supported anymore. Consider removing the oid column using\n"
1092 " ALTER TABLE ... SET WITHOUT OIDS;\n"
1093 "A list of tables with the problem is in the file:\n"
1094 " %s", output_path);
1118 prep_status(
"Checking for system-defined composite types in user tables");
1120 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1122 "tables_using_composite.txt");
1135 firstUserOid = 16384;
1137 base_query =
psprintf(
"SELECT t.oid FROM pg_catalog.pg_type t "
1138 "LEFT JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid "
1139 " WHERE typtype = 'c' AND (t.oid < %u OR nspname = 'information_schema')",
1149 pg_fatal(
"Your installation contains system-defined composite types in user tables.\n"
1150 "These type OIDs are not stable across PostgreSQL versions,\n"
1151 "so this cluster cannot currently be upgraded. You can\n"
1152 "drop the problem columns and restart the upgrade.\n"
1153 "A list of the problem columns is in the file:\n"
1154 " %s", output_path);
1177 prep_status(
"Checking for reg* data types in user tables");
1179 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1181 "tables_using_reg.txt");
1188 "SELECT oid FROM pg_catalog.pg_type t "
1189 "WHERE t.typnamespace = "
1190 " (SELECT oid FROM pg_catalog.pg_namespace "
1191 " WHERE nspname = 'pg_catalog') "
1192 " AND t.typname IN ( "
1196 " 'regdictionary', "
1210 pg_fatal(
"Your installation contains one of the reg* data types in user tables.\n"
1211 "These data types reference system OIDs that are not preserved by\n"
1212 "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
1213 "drop the problem columns and restart the upgrade.\n"
1214 "A list of the problem columns is in the file:\n"
1215 " %s", output_path);
1231 prep_status(
"Checking for incompatible \"%s\" data type in user tables",
1234 snprintf(output_path,
sizeof(output_path),
"tables_using_aclitem.txt");
1239 pg_fatal(
"Your installation contains the \"aclitem\" data type in user tables.\n"
1240 "The internal format of \"aclitem\" changed in PostgreSQL version 16\n"
1241 "so this cluster cannot currently be upgraded. You can drop the\n"
1242 "problem columns and restart the upgrade. A list of the problem\n"
1243 "columns is in the file:\n"
1244 " %s", output_path);
1258 const char *datatype)
1263 prep_status(
"Checking for removed \"%s\" data type in user tables",
1266 snprintf(output_path,
sizeof(output_path),
"tables_using_%s.txt",
1268 snprintf(
typename,
sizeof(
typename),
"pg_catalog.%s", datatype);
1273 pg_fatal(
"Your installation contains the \"%s\" data type in user tables.\n"
1274 "The \"%s\" type has been removed in PostgreSQL version %s,\n"
1275 "so this cluster cannot currently be upgraded. You can drop the\n"
1276 "problem columns, or change them to another data type, and restart\n"
1277 "the upgrade. A list of the problem columns is in the file:\n"
1278 " %s", datatype, datatype, version, output_path);
1295 prep_status(
"Checking for incompatible \"jsonb\" data type");
1297 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1299 "tables_using_jsonb.txt");
1304 pg_fatal(
"Your installation contains the \"jsonb\" data type in user tables.\n"
1305 "The internal format of \"jsonb\" changed during 9.4 beta so this\n"
1306 "cluster cannot currently be upgraded. You can\n"
1307 "drop the problem columns and restart the upgrade.\n"
1308 "A list of the problem columns is in the file:\n"
1309 " %s", output_path);
1328 FILE *script = NULL;
1331 prep_status(
"Checking for roles starting with \"pg_\"");
1333 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1335 "pg_role_prefix.txt");
1338 "SELECT oid AS roloid, rolname "
1339 "FROM pg_catalog.pg_roles "
1340 "WHERE rolname ~ '^pg_'");
1345 for (
int rowno = 0; rowno < ntups; rowno++)
1347 if (script == NULL && (script =
fopen_priv(output_path,
"w")) == NULL)
1348 pg_fatal(
"could not open file \"%s\": %s",
1350 fprintf(script,
"%s (oid=%s)\n",
1363 pg_fatal(
"Your installation contains roles starting with \"pg_\".\n"
1364 "\"pg_\" is a reserved prefix for system roles. The cluster\n"
1365 "cannot be upgraded until these roles are renamed.\n"
1366 "A list of roles starting with \"pg_\" is in the file:\n"
1367 " %s", output_path);
1380 FILE *script = NULL;
1383 prep_status(
"Checking for user-defined encoding conversions");
1385 snprintf(output_path,
sizeof(output_path),
"%s/%s",
1387 "encoding_conversions.txt");
1390 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
1393 bool db_used =
false;
1409 "SELECT c.oid as conoid, c.conname, n.nspname "
1410 "FROM pg_catalog.pg_conversion c, "
1411 " pg_catalog.pg_namespace n "
1412 "WHERE c.connamespace = n.oid AND "
1418 for (rowno = 0; rowno < ntups; rowno++)
1420 if (script == NULL &&
1421 (script =
fopen_priv(output_path,
"w")) == NULL)
1422 pg_fatal(
"could not open file \"%s\": %s",
1429 fprintf(script,
" (oid=%s) %s.%s\n",
1444 pg_fatal(
"Your installation contains user-defined encoding conversions.\n"
1445 "The conversion function parameters changed in PostgreSQL version 14\n"
1446 "so this cluster cannot currently be upgraded. You can remove the\n"
1447 "encoding conversions in the old cluster and restart the upgrade.\n"
1448 "A list of user-defined encoding conversions is in the file:\n"
1449 " %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)
static void check_for_removed_data_type_usage(ClusterInfo *cluster, const char *version, const char *datatype)
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)
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_new_tablespace_dir(void)
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