pg_upgrade: run all data type checks per connection
[pgsql.git] / src / bin / pg_upgrade / check.c
blobc198896c9f0fba7df38285e16edabd80de8f1a00
1 /*
2 * check.c
4 * server checks and output routines
6 * Copyright (c) 2010-2024, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/check.c
8 */
10 #include "postgres_fe.h"
12 #include "catalog/pg_authid_d.h"
13 #include "catalog/pg_class_d.h"
14 #include "catalog/pg_collation.h"
15 #include "fe_utils/string_utils.h"
16 #include "mb/pg_wchar.h"
17 #include "pg_upgrade.h"
19 static void check_new_cluster_is_empty(void);
20 static void check_is_install_user(ClusterInfo *cluster);
21 static void check_proper_datallowconn(ClusterInfo *cluster);
22 static void check_for_prepared_transactions(ClusterInfo *cluster);
23 static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
24 static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
25 static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
26 static void check_for_tables_with_oids(ClusterInfo *cluster);
27 static void check_for_pg_role_prefix(ClusterInfo *cluster);
28 static void check_for_new_tablespace_dir(void);
29 static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster);
30 static void check_new_cluster_logical_replication_slots(void);
31 static void check_new_cluster_subscription_configuration(void);
32 static void check_old_cluster_for_valid_slots(bool live_check);
33 static void check_old_cluster_subscription_state(void);
36 * DataTypesUsageChecks - definitions of data type checks for the old cluster
37 * in order to determine if an upgrade can be performed. See the comment on
38 * data_types_usage_checks below for a more detailed description.
40 typedef struct
42 /* Status line to print to the user */
43 const char *status;
44 /* Filename to store report to */
45 const char *report_filename;
46 /* Query to extract the oid of the datatype */
47 const char *base_query;
48 /* Text to store to report in case of error */
49 const char *report_text;
50 /* The latest version where the check applies */
51 int threshold_version;
52 /* A function pointer for determining if the check applies */
53 DataTypesUsageVersionCheck version_hook;
54 } DataTypesUsageChecks;
57 * Special values for threshold_version for indicating that a check applies to
58 * all versions, or that a custom function needs to be invoked to determine
59 * if the check applies.
61 #define MANUAL_CHECK 1
62 #define ALL_VERSIONS -1
64 /*--
65 * Data type usage checks. Each check for problematic data type usage is
66 * defined in this array with metadata, SQL query for finding the data type
67 * and functionality for deciding if the check is applicable to the version
68 * of the old cluster. The struct members are described in detail below:
70 * status A oneline string which can be printed to the user to
71 * inform about progress. Should not end with newline.
72 * report_filename The filename in which the list of problems detected by
73 * the check will be printed.
74 * base_query A query which extracts the Oid of the datatype checked
75 * for.
76 * report_text The text which will be printed to the user to explain
77 * what the check did, and why it failed. The text should
78 * end with a newline, and does not need to refer to the
79 * report_filename as that is automatically appended to
80 * the report with the path to the log folder.
81 * threshold_version The major version of PostgreSQL for which to run the
82 * check. Iff the old cluster is less than, or equal to,
83 * the threshold version then the check will be executed.
84 * If the old version is greater than the threshold then
85 * the check is skipped. If the threshold_version is set
86 * to ALL_VERSIONS then it will be run unconditionally,
87 * if set to MANUAL_CHECK then the version_hook function
88 * will be executed in order to determine whether or not
89 * to run.
90 * version_hook A function pointer to a version check function of type
91 * DataTypesUsageVersionCheck which is used to determine
92 * if the check is applicable to the old cluster. If the
93 * version_hook returns true then the check will be run,
94 * else it will be skipped. The function will only be
95 * executed iff threshold_version is set to MANUAL_CHECK.
97 static DataTypesUsageChecks data_types_usage_checks[] =
100 * Look for composite types that were made during initdb *or* belong to
101 * information_schema; that's important in case information_schema was
102 * dropped and reloaded.
104 * The cutoff OID here should match the source cluster's value of
105 * FirstNormalObjectId. We hardcode it rather than using that C #define
106 * because, if that #define is ever changed, our own version's value is
107 * NOT what to use. Eventually we may need a test on the source cluster's
108 * version to select the correct value.
111 .status = gettext_noop("Checking for system-defined composite types in user tables"),
112 .report_filename = "tables_using_composite.txt",
113 .base_query =
114 "SELECT t.oid FROM pg_catalog.pg_type t "
115 "LEFT JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid "
116 " WHERE typtype = 'c' AND (t.oid < 16384 OR nspname = 'information_schema')",
117 .report_text =
118 gettext_noop("Your installation contains system-defined composite types in user tables.\n"
119 "These type OIDs are not stable across PostgreSQL versions,\n"
120 "so this cluster cannot currently be upgraded. You can drop the\n"
121 "problem columns and restart the upgrade.\n"),
122 .threshold_version = ALL_VERSIONS
126 * 9.3 -> 9.4 Fully implement the 'line' data type in 9.4, which
127 * previously returned "not enabled" by default and was only functionally
128 * enabled with a compile-time switch; as of 9.4 "line" has a different
129 * on-disk representation format.
132 .status = gettext_noop("Checking for incompatible \"line\" data type"),
133 .report_filename = "tables_using_line.txt",
134 .base_query =
135 "SELECT 'pg_catalog.line'::pg_catalog.regtype AS oid",
136 .report_text =
137 gettext_noop("Your installation contains the \"line\" data type in user tables.\n"
138 "this data type changed its internal and input/output format\n"
139 "between your old and new versions so this\n"
140 "cluster cannot currently be upgraded. You can\n"
141 "drop the problem columns and restart the upgrade.\n"),
142 .threshold_version = 903
146 * pg_upgrade only preserves these system values: pg_class.oid pg_type.oid
147 * pg_enum.oid
149 * Many of the reg* data types reference system catalog info that is not
150 * preserved, and hence these data types cannot be used in user tables
151 * upgraded by pg_upgrade.
154 .status = gettext_noop("Checking for reg* data types in user tables"),
155 .report_filename = "tables_using_reg.txt",
158 * Note: older servers will not have all of these reg* types, so we
159 * have to write the query like this rather than depending on casts to
160 * regtype.
162 .base_query =
163 "SELECT oid FROM pg_catalog.pg_type t "
164 "WHERE t.typnamespace = "
165 " (SELECT oid FROM pg_catalog.pg_namespace "
166 " WHERE nspname = 'pg_catalog') "
167 " AND t.typname IN ( "
168 /* pg_class.oid is preserved, so 'regclass' is OK */
169 " 'regcollation', "
170 " 'regconfig', "
171 " 'regdictionary', "
172 " 'regnamespace', "
173 " 'regoper', "
174 " 'regoperator', "
175 " 'regproc', "
176 " 'regprocedure' "
177 /* pg_authid.oid is preserved, so 'regrole' is OK */
178 /* pg_type.oid is (mostly) preserved, so 'regtype' is OK */
179 " )",
180 .report_text =
181 gettext_noop("Your installation contains one of the reg* data types in user tables.\n"
182 "These data types reference system OIDs that are not preserved by\n"
183 "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
184 "drop the problem columns and restart the upgrade.\n"),
185 .threshold_version = ALL_VERSIONS
189 * PG 16 increased the size of the 'aclitem' type, which breaks the
190 * on-disk format for existing data.
193 .status = gettext_noop("Checking for incompatible \"aclitem\" data type"),
194 .report_filename = "tables_using_aclitem.txt",
195 .base_query =
196 "SELECT 'pg_catalog.aclitem'::pg_catalog.regtype AS oid",
197 .report_text =
198 gettext_noop("Your installation contains the \"aclitem\" data type in user tables.\n"
199 "The internal format of \"aclitem\" changed in PostgreSQL version 16\n"
200 "so this cluster cannot currently be upgraded. You can drop the\n"
201 "problem columns and restart the upgrade.\n"),
202 .threshold_version = 1500
206 * It's no longer allowed to create tables or views with "unknown"-type
207 * columns. We do not complain about views with such columns, because
208 * they should get silently converted to "text" columns during the DDL
209 * dump and reload; it seems unlikely to be worth making users do that by
210 * hand. However, if there's a table with such a column, the DDL reload
211 * will fail, so we should pre-detect that rather than failing
212 * mid-upgrade. Worse, if there's a matview with such a column, the DDL
213 * reload will silently change it to "text" which won't match the on-disk
214 * storage (which is like "cstring"). So we *must* reject that.
217 .status = gettext_noop("Checking for invalid \"unknown\" user columns"),
218 .report_filename = "tables_using_unknown.txt",
219 .base_query =
220 "SELECT 'pg_catalog.unknown'::pg_catalog.regtype AS oid",
221 .report_text =
222 gettext_noop("Your installation contains the \"unknown\" data type in user tables.\n"
223 "This data type is no longer allowed in tables, so this cluster\n"
224 "cannot currently be upgraded. You can drop the problem columns\n"
225 "and restart the upgrade.\n"),
226 .threshold_version = 906
230 * PG 12 changed the 'sql_identifier' type storage to be based on name,
231 * not varchar, which breaks on-disk format for existing data. So we need
232 * to prevent upgrade when used in user objects (tables, indexes, ...). In
233 * 12, the sql_identifier data type was switched from name to varchar,
234 * which does affect the storage (name is by-ref, but not varlena). This
235 * means user tables using sql_identifier for columns are broken because
236 * the on-disk format is different.
239 .status = gettext_noop("Checking for invalid \"sql_identifier\" user columns"),
240 .report_filename = "tables_using_sql_identifier.txt",
241 .base_query =
242 "SELECT 'information_schema.sql_identifier'::pg_catalog.regtype AS oid",
243 .report_text =
244 gettext_noop("Your installation contains the \"sql_identifier\" data type in user tables.\n"
245 "The on-disk format for this data type has changed, so this\n"
246 "cluster cannot currently be upgraded. You can drop the problem\n"
247 "columns and restart the upgrade.\n"),
248 .threshold_version = 1100
252 * JSONB changed its storage format during 9.4 beta, so check for it.
255 .status = gettext_noop("Checking for incompatible \"jsonb\" data type in user tables"),
256 .report_filename = "tables_using_jsonb.txt",
257 .base_query =
258 "SELECT 'pg_catalog.jsonb'::pg_catalog.regtype AS oid",
259 .report_text =
260 gettext_noop("Your installation contains the \"jsonb\" data type in user tables.\n"
261 "The internal format of \"jsonb\" changed during 9.4 beta so this\n"
262 "cluster cannot currently be upgraded. You can drop the problem \n"
263 "columns and restart the upgrade.\n"),
264 .threshold_version = MANUAL_CHECK,
265 .version_hook = jsonb_9_4_check_applicable
269 * PG 12 removed types abstime, reltime, tinterval.
272 .status = gettext_noop("Checking for removed \"abstime\" data type in user tables"),
273 .report_filename = "tables_using_abstime.txt",
274 .base_query =
275 "SELECT 'pg_catalog.abstime'::pg_catalog.regtype AS oid",
276 .report_text =
277 gettext_noop("Your installation contains the \"abstime\" data type in user tables.\n"
278 "The \"abstime\" type has been removed in PostgreSQL version 12,\n"
279 "so this cluster cannot currently be upgraded. You can drop the\n"
280 "problem columns, or change them to another data type, and restart\n"
281 "the upgrade.\n"),
282 .threshold_version = 1100
285 .status = gettext_noop("Checking for removed \"reltime\" data type in user tables"),
286 .report_filename = "tables_using_reltime.txt",
287 .base_query =
288 "SELECT 'pg_catalog.reltime'::pg_catalog.regtype AS oid",
289 .report_text =
290 gettext_noop("Your installation contains the \"reltime\" data type in user tables.\n"
291 "The \"reltime\" type has been removed in PostgreSQL version 12,\n"
292 "so this cluster cannot currently be upgraded. You can drop the\n"
293 "problem columns, or change them to another data type, and restart\n"
294 "the upgrade.\n"),
295 .threshold_version = 1100
298 .status = gettext_noop("Checking for removed \"tinterval\" data type in user tables"),
299 .report_filename = "tables_using_tinterval.txt",
300 .base_query =
301 "SELECT 'pg_catalog.tinterval'::pg_catalog.regtype AS oid",
302 .report_text =
303 gettext_noop("Your installation contains the \"tinterval\" data type in user tables.\n"
304 "The \"tinterval\" type has been removed in PostgreSQL version 12,\n"
305 "so this cluster cannot currently be upgraded. You can drop the\n"
306 "problem columns, or change them to another data type, and restart\n"
307 "the upgrade.\n"),
308 .threshold_version = 1100
311 /* End of checks marker, must remain last */
313 NULL, NULL, NULL, NULL, 0, NULL
318 * check_for_data_types_usage()
319 * Detect whether there are any stored columns depending on given type(s)
321 * If so, write a report to the given file name and signal a failure to the
322 * user.
324 * The checks to run are defined in a DataTypesUsageChecks structure where
325 * each check has a metadata for explaining errors to the user, a base_query,
326 * a report filename and a function pointer hook for validating if the check
327 * should be executed given the cluster at hand.
329 * base_query should be a SELECT yielding a single column named "oid",
330 * containing the pg_type OIDs of one or more types that are known to have
331 * inconsistent on-disk representations across server versions.
333 * We check for the type(s) in tables, matviews, and indexes, but not views;
334 * there's no storage involved in a view.
336 static void
337 check_for_data_types_usage(ClusterInfo *cluster, DataTypesUsageChecks * checks)
339 bool found = false;
340 bool *results;
341 PQExpBufferData report;
342 DataTypesUsageChecks *tmp = checks;
343 int n_data_types_usage_checks = 0;
345 prep_status("Checking for data type usage");
347 /* Gather number of checks to perform */
348 while (tmp->status != NULL)
350 n_data_types_usage_checks++;
351 tmp++;
354 /* Prepare an array to store the results of checks in */
355 results = pg_malloc0(sizeof(bool) * n_data_types_usage_checks);
358 * Connect to each database in the cluster and run all defined checks
359 * against that database before trying the next one.
361 for (int dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
363 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
364 PGconn *conn = connectToServer(cluster, active_db->db_name);
366 for (int checknum = 0; checknum < n_data_types_usage_checks; checknum++)
368 PGresult *res;
369 int ntups;
370 int i_nspname;
371 int i_relname;
372 int i_attname;
373 FILE *script = NULL;
374 bool db_used = false;
375 char output_path[MAXPGPATH];
376 DataTypesUsageChecks *cur_check = &checks[checknum];
378 if (cur_check->threshold_version == MANUAL_CHECK)
380 Assert(cur_check->version_hook);
383 * Make sure that the check applies to the current cluster
384 * version and skip if not. If no check hook has been defined
385 * we run the check for all versions.
387 if (!cur_check->version_hook(cluster))
388 continue;
390 else if (cur_check->threshold_version != ALL_VERSIONS)
392 if (GET_MAJOR_VERSION(cluster->major_version) > cur_check->threshold_version)
393 continue;
395 else
396 Assert(cur_check->threshold_version == ALL_VERSIONS);
398 snprintf(output_path, sizeof(output_path), "%s/%s",
399 log_opts.basedir,
400 cur_check->report_filename);
403 * The type(s) of interest might be wrapped in a domain, array,
404 * composite, or range, and these container types can be nested
405 * (to varying extents depending on server version, but that's not
406 * of concern here). To handle all these cases we need a
407 * recursive CTE.
409 res = executeQueryOrDie(conn,
410 "WITH RECURSIVE oids AS ( "
411 /* start with the type(s) returned by base_query */
412 " %s "
413 " UNION ALL "
414 " SELECT * FROM ( "
415 /* inner WITH because we can only reference the CTE once */
416 " WITH x AS (SELECT oid FROM oids) "
417 /* domains on any type selected so far */
418 " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
419 " UNION ALL "
420 /* arrays over any type selected so far */
421 " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typelem = x.oid AND typtype = 'b' "
422 " UNION ALL "
423 /* composite types containing any type selected so far */
424 " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
425 " WHERE t.typtype = 'c' AND "
426 " t.oid = c.reltype AND "
427 " c.oid = a.attrelid AND "
428 " NOT a.attisdropped AND "
429 " a.atttypid = x.oid "
430 " UNION ALL "
431 /* ranges containing any type selected so far */
432 " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_range r, x "
433 " WHERE t.typtype = 'r' AND r.rngtypid = t.oid AND r.rngsubtype = x.oid"
434 " ) foo "
435 ") "
436 /* now look for stored columns of any such type */
437 "SELECT n.nspname, c.relname, a.attname "
438 "FROM pg_catalog.pg_class c, "
439 " pg_catalog.pg_namespace n, "
440 " pg_catalog.pg_attribute a "
441 "WHERE c.oid = a.attrelid AND "
442 " NOT a.attisdropped AND "
443 " a.atttypid IN (SELECT oid FROM oids) AND "
444 " c.relkind IN ("
445 CppAsString2(RELKIND_RELATION) ", "
446 CppAsString2(RELKIND_MATVIEW) ", "
447 CppAsString2(RELKIND_INDEX) ") AND "
448 " c.relnamespace = n.oid AND "
449 /* exclude possible orphaned temp tables */
450 " n.nspname !~ '^pg_temp_' AND "
451 " n.nspname !~ '^pg_toast_temp_' AND "
452 /* exclude system catalogs, too */
453 " n.nspname NOT IN ('pg_catalog', 'information_schema')",
454 cur_check->base_query);
456 ntups = PQntuples(res);
459 * The datatype was found, so extract the data and log to the
460 * requested filename. We need to open the file for appending
461 * since the check might have already found the type in another
462 * database earlier in the loop.
464 if (ntups)
467 * Make sure we have a buffer to save reports to now that we
468 * found a first failing check.
470 if (!found)
471 initPQExpBuffer(&report);
472 found = true;
475 * If this is the first time we see an error for the check in
476 * question then print a status message of the failure.
478 if (!results[checknum])
480 pg_log(PG_REPORT, " failed check: %s", _(cur_check->status));
481 appendPQExpBuffer(&report, "\n%s\n%s %s\n",
482 _(cur_check->report_text),
483 _("A list of the problem columns is in the file:"),
484 output_path);
486 results[checknum] = true;
488 i_nspname = PQfnumber(res, "nspname");
489 i_relname = PQfnumber(res, "relname");
490 i_attname = PQfnumber(res, "attname");
492 for (int rowno = 0; rowno < ntups; rowno++)
494 if (script == NULL && (script = fopen_priv(output_path, "a")) == NULL)
495 pg_fatal("could not open file \"%s\": %m", output_path);
497 if (!db_used)
499 fprintf(script, "In database: %s\n", active_db->db_name);
500 db_used = true;
502 fprintf(script, " %s.%s.%s\n",
503 PQgetvalue(res, rowno, i_nspname),
504 PQgetvalue(res, rowno, i_relname),
505 PQgetvalue(res, rowno, i_attname));
508 if (script)
510 fclose(script);
511 script = NULL;
515 PQclear(res);
518 PQfinish(conn);
521 if (found)
522 pg_fatal("Data type checks failed: %s", report.data);
524 check_ok();
528 * fix_path_separator
529 * For non-Windows, just return the argument.
530 * For Windows convert any forward slash to a backslash
531 * such as is suitable for arguments to builtin commands
532 * like RMDIR and DEL.
534 static char *
535 fix_path_separator(char *path)
537 #ifdef WIN32
539 char *result;
540 char *c;
542 result = pg_strdup(path);
544 for (c = result; *c != '\0'; c++)
545 if (*c == '/')
546 *c = '\\';
548 return result;
549 #else
551 return path;
552 #endif
555 void
556 output_check_banner(bool live_check)
558 if (user_opts.check && live_check)
560 pg_log(PG_REPORT,
561 "Performing Consistency Checks on Old Live Server\n"
562 "------------------------------------------------");
564 else
566 pg_log(PG_REPORT,
567 "Performing Consistency Checks\n"
568 "-----------------------------");
573 void
574 check_and_dump_old_cluster(bool live_check)
576 /* -- OLD -- */
578 if (!live_check)
579 start_postmaster(&old_cluster, true);
582 * Extract a list of databases, tables, and logical replication slots from
583 * the old cluster.
585 get_db_rel_and_slot_infos(&old_cluster, live_check);
587 init_tablespaces();
589 get_loadable_libraries();
593 * Check for various failure cases
595 check_is_install_user(&old_cluster);
596 check_proper_datallowconn(&old_cluster);
597 check_for_prepared_transactions(&old_cluster);
598 check_for_isn_and_int8_passing_mismatch(&old_cluster);
600 if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1700)
603 * Logical replication slots can be migrated since PG17. See comments
604 * atop get_old_cluster_logical_slot_infos().
606 check_old_cluster_for_valid_slots(live_check);
609 * Subscriptions and their dependencies can be migrated since PG17.
610 * See comments atop get_db_subscription_count().
612 check_old_cluster_subscription_state();
615 check_for_data_types_usage(&old_cluster, data_types_usage_checks);
618 * PG 14 changed the function signature of encoding conversion functions.
619 * Conversions from older versions cannot be upgraded automatically
620 * because the user-defined functions used by the encoding conversions
621 * need to be changed to match the new signature.
623 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
624 check_for_user_defined_encoding_conversions(&old_cluster);
627 * Pre-PG 14 allowed user defined postfix operators, which are not
628 * supported anymore. Verify there are none, iff applicable.
630 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
631 check_for_user_defined_postfix_ops(&old_cluster);
634 * PG 14 changed polymorphic functions from anyarray to
635 * anycompatiblearray.
637 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
638 check_for_incompatible_polymorphics(&old_cluster);
641 * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
642 * supported anymore. Verify there are none, iff applicable.
644 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
645 check_for_tables_with_oids(&old_cluster);
648 * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
649 * hash indexes
651 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
653 if (user_opts.check)
654 old_9_6_invalidate_hash_indexes(&old_cluster, true);
657 /* 9.5 and below should not have roles starting with pg_ */
658 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
659 check_for_pg_role_prefix(&old_cluster);
662 * While not a check option, we do this now because this is the only time
663 * the old server is running.
665 if (!user_opts.check)
666 generate_old_dump();
668 if (!live_check)
669 stop_postmaster(false);
673 void
674 check_new_cluster(void)
676 get_db_rel_and_slot_infos(&new_cluster, false);
678 check_new_cluster_is_empty();
680 check_loadable_libraries();
682 switch (user_opts.transfer_mode)
684 case TRANSFER_MODE_CLONE:
685 check_file_clone();
686 break;
687 case TRANSFER_MODE_COPY:
688 break;
689 case TRANSFER_MODE_COPY_FILE_RANGE:
690 check_copy_file_range();
691 break;
692 case TRANSFER_MODE_LINK:
693 check_hard_link();
694 break;
697 check_is_install_user(&new_cluster);
699 check_for_prepared_transactions(&new_cluster);
701 check_for_new_tablespace_dir();
703 check_new_cluster_logical_replication_slots();
705 check_new_cluster_subscription_configuration();
709 void
710 report_clusters_compatible(void)
712 if (user_opts.check)
714 pg_log(PG_REPORT, "\n*Clusters are compatible*");
715 /* stops new cluster */
716 stop_postmaster(false);
718 cleanup_output_dirs();
719 exit(0);
722 pg_log(PG_REPORT, "\n"
723 "If pg_upgrade fails after this point, you must re-initdb the\n"
724 "new cluster before continuing.");
728 void
729 issue_warnings_and_set_wal_level(void)
732 * We unconditionally start/stop the new server because pg_resetwal -o set
733 * wal_level to 'minimum'. If the user is upgrading standby servers using
734 * the rsync instructions, they will need pg_upgrade to write its final
735 * WAL record showing wal_level as 'replica'.
737 start_postmaster(&new_cluster, true);
739 /* Reindex hash indexes for old < 10.0 */
740 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
741 old_9_6_invalidate_hash_indexes(&new_cluster, false);
743 report_extension_updates(&new_cluster);
745 stop_postmaster(false);
749 void
750 output_completion_banner(char *deletion_script_file_name)
752 PQExpBufferData user_specification;
754 initPQExpBuffer(&user_specification);
755 if (os_info.user_specified)
757 appendPQExpBufferStr(&user_specification, "-U ");
758 appendShellString(&user_specification, os_info.user);
759 appendPQExpBufferChar(&user_specification, ' ');
762 pg_log(PG_REPORT,
763 "Optimizer statistics are not transferred by pg_upgrade.\n"
764 "Once you start the new server, consider running:\n"
765 " %s/vacuumdb %s--all --analyze-in-stages", new_cluster.bindir, user_specification.data);
767 if (deletion_script_file_name)
768 pg_log(PG_REPORT,
769 "Running this script will delete the old cluster's data files:\n"
770 " %s",
771 deletion_script_file_name);
772 else
773 pg_log(PG_REPORT,
774 "Could not create a script to delete the old cluster's data files\n"
775 "because user-defined tablespaces or the new cluster's data directory\n"
776 "exist in the old cluster directory. The old cluster's contents must\n"
777 "be deleted manually.");
779 termPQExpBuffer(&user_specification);
783 void
784 check_cluster_versions(void)
786 prep_status("Checking cluster versions");
788 /* cluster versions should already have been obtained */
789 Assert(old_cluster.major_version != 0);
790 Assert(new_cluster.major_version != 0);
793 * We allow upgrades from/to the same major version for alpha/beta
794 * upgrades
797 if (GET_MAJOR_VERSION(old_cluster.major_version) < 902)
798 pg_fatal("This utility can only upgrade from PostgreSQL version %s and later.",
799 "9.2");
801 /* Only current PG version is supported as a target */
802 if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
803 pg_fatal("This utility can only upgrade to PostgreSQL version %s.",
804 PG_MAJORVERSION);
807 * We can't allow downgrading because we use the target pg_dump, and
808 * pg_dump cannot operate on newer database versions, only current and
809 * older versions.
811 if (old_cluster.major_version > new_cluster.major_version)
812 pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.");
814 /* Ensure binaries match the designated data directories */
815 if (GET_MAJOR_VERSION(old_cluster.major_version) !=
816 GET_MAJOR_VERSION(old_cluster.bin_version))
817 pg_fatal("Old cluster data and binary directories are from different major versions.");
818 if (GET_MAJOR_VERSION(new_cluster.major_version) !=
819 GET_MAJOR_VERSION(new_cluster.bin_version))
820 pg_fatal("New cluster data and binary directories are from different major versions.");
822 check_ok();
826 void
827 check_cluster_compatibility(bool live_check)
829 /* get/check pg_control data of servers */
830 get_control_data(&old_cluster, live_check);
831 get_control_data(&new_cluster, false);
832 check_control_data(&old_cluster.controldata, &new_cluster.controldata);
834 if (live_check && old_cluster.port == new_cluster.port)
835 pg_fatal("When checking a live server, "
836 "the old and new port numbers must be different.");
840 static void
841 check_new_cluster_is_empty(void)
843 int dbnum;
845 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
847 int relnum;
848 RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
850 for (relnum = 0; relnum < rel_arr->nrels;
851 relnum++)
853 /* pg_largeobject and its index should be skipped */
854 if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
855 pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
856 new_cluster.dbarr.dbs[dbnum].db_name,
857 rel_arr->rels[relnum].nspname,
858 rel_arr->rels[relnum].relname);
864 * A previous run of pg_upgrade might have failed and the new cluster
865 * directory recreated, but they might have forgotten to remove
866 * the new cluster's tablespace directories. Therefore, check that
867 * new cluster tablespace directories do not already exist. If
868 * they do, it would cause an error while restoring global objects.
869 * This allows the failure to be detected at check time, rather than
870 * during schema restore.
872 static void
873 check_for_new_tablespace_dir(void)
875 int tblnum;
876 char new_tablespace_dir[MAXPGPATH];
878 prep_status("Checking for new cluster tablespace directories");
880 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
882 struct stat statbuf;
884 snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
885 os_info.old_tablespaces[tblnum],
886 new_cluster.tablespace_suffix);
888 if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
889 pg_fatal("new cluster tablespace directory already exists: \"%s\"",
890 new_tablespace_dir);
893 check_ok();
897 * create_script_for_old_cluster_deletion()
899 * This is particularly useful for tablespace deletion.
901 void
902 create_script_for_old_cluster_deletion(char **deletion_script_file_name)
904 FILE *script = NULL;
905 int tblnum;
906 char old_cluster_pgdata[MAXPGPATH],
907 new_cluster_pgdata[MAXPGPATH];
909 *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
910 SCRIPT_PREFIX, SCRIPT_EXT);
912 strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
913 canonicalize_path(old_cluster_pgdata);
915 strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
916 canonicalize_path(new_cluster_pgdata);
918 /* Some people put the new data directory inside the old one. */
919 if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
921 pg_log(PG_WARNING,
922 "\nWARNING: new data directory should not be inside the old data directory, i.e. %s", old_cluster_pgdata);
924 /* Unlink file in case it is left over from a previous run. */
925 unlink(*deletion_script_file_name);
926 pg_free(*deletion_script_file_name);
927 *deletion_script_file_name = NULL;
928 return;
932 * Some users (oddly) create tablespaces inside the cluster data
933 * directory. We can't create a proper old cluster delete script in that
934 * case.
936 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
938 char old_tablespace_dir[MAXPGPATH];
940 strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
941 canonicalize_path(old_tablespace_dir);
942 if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
944 /* reproduce warning from CREATE TABLESPACE that is in the log */
945 pg_log(PG_WARNING,
946 "\nWARNING: user-defined tablespace locations should not be inside the data directory, i.e. %s", old_tablespace_dir);
948 /* Unlink file in case it is left over from a previous run. */
949 unlink(*deletion_script_file_name);
950 pg_free(*deletion_script_file_name);
951 *deletion_script_file_name = NULL;
952 return;
956 prep_status("Creating script to delete old cluster");
958 if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
959 pg_fatal("could not open file \"%s\": %m",
960 *deletion_script_file_name);
962 #ifndef WIN32
963 /* add shebang header */
964 fprintf(script, "#!/bin/sh\n\n");
965 #endif
967 /* delete old cluster's default tablespace */
968 fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
969 fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
971 /* delete old cluster's alternate tablespaces */
972 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
975 * Do the old cluster's per-database directories share a directory
976 * with a new version-specific tablespace?
978 if (strlen(old_cluster.tablespace_suffix) == 0)
980 /* delete per-database directories */
981 int dbnum;
983 fprintf(script, "\n");
985 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
986 fprintf(script, RMDIR_CMD " %c%s%c%u%c\n", PATH_QUOTE,
987 fix_path_separator(os_info.old_tablespaces[tblnum]),
988 PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid,
989 PATH_QUOTE);
991 else
993 char *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
996 * Simply delete the tablespace directory, which might be ".old"
997 * or a version-specific subdirectory.
999 fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
1000 fix_path_separator(os_info.old_tablespaces[tblnum]),
1001 fix_path_separator(suffix_path), PATH_QUOTE);
1002 pfree(suffix_path);
1006 fclose(script);
1008 #ifndef WIN32
1009 if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
1010 pg_fatal("could not add execute permission to file \"%s\": %m",
1011 *deletion_script_file_name);
1012 #endif
1014 check_ok();
1019 * check_is_install_user()
1021 * Check we are the install user, and that the new cluster
1022 * has no other users.
1024 static void
1025 check_is_install_user(ClusterInfo *cluster)
1027 PGresult *res;
1028 PGconn *conn = connectToServer(cluster, "template1");
1030 prep_status("Checking database user is the install user");
1032 /* Can't use pg_authid because only superusers can view it. */
1033 res = executeQueryOrDie(conn,
1034 "SELECT rolsuper, oid "
1035 "FROM pg_catalog.pg_roles "
1036 "WHERE rolname = current_user "
1037 "AND rolname !~ '^pg_'");
1040 * We only allow the install user in the new cluster (see comment below)
1041 * and we preserve pg_authid.oid, so this must be the install user in the
1042 * old cluster too.
1044 if (PQntuples(res) != 1 ||
1045 atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
1046 pg_fatal("database user \"%s\" is not the install user",
1047 os_info.user);
1049 PQclear(res);
1051 res = executeQueryOrDie(conn,
1052 "SELECT COUNT(*) "
1053 "FROM pg_catalog.pg_roles "
1054 "WHERE rolname !~ '^pg_'");
1056 if (PQntuples(res) != 1)
1057 pg_fatal("could not determine the number of users");
1060 * We only allow the install user in the new cluster because other defined
1061 * users might match users defined in the old cluster and generate an
1062 * error during pg_dump restore.
1064 if (cluster == &new_cluster && strcmp(PQgetvalue(res, 0, 0), "1") != 0)
1065 pg_fatal("Only the install user can be defined in the new cluster.");
1067 PQclear(res);
1069 PQfinish(conn);
1071 check_ok();
1076 * check_proper_datallowconn
1078 * Ensure that all non-template0 databases allow connections since they
1079 * otherwise won't be restored; and that template0 explicitly doesn't allow
1080 * connections since it would make pg_dumpall --globals restore fail.
1082 static void
1083 check_proper_datallowconn(ClusterInfo *cluster)
1085 int dbnum;
1086 PGconn *conn_template1;
1087 PGresult *dbres;
1088 int ntups;
1089 int i_datname;
1090 int i_datallowconn;
1091 FILE *script = NULL;
1092 char output_path[MAXPGPATH];
1094 prep_status("Checking database connection settings");
1096 snprintf(output_path, sizeof(output_path), "%s/%s",
1097 log_opts.basedir,
1098 "databases_with_datallowconn_false.txt");
1100 conn_template1 = connectToServer(cluster, "template1");
1102 /* get database names */
1103 dbres = executeQueryOrDie(conn_template1,
1104 "SELECT datname, datallowconn "
1105 "FROM pg_catalog.pg_database");
1107 i_datname = PQfnumber(dbres, "datname");
1108 i_datallowconn = PQfnumber(dbres, "datallowconn");
1110 ntups = PQntuples(dbres);
1111 for (dbnum = 0; dbnum < ntups; dbnum++)
1113 char *datname = PQgetvalue(dbres, dbnum, i_datname);
1114 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
1116 if (strcmp(datname, "template0") == 0)
1118 /* avoid restore failure when pg_dumpall tries to create template0 */
1119 if (strcmp(datallowconn, "t") == 0)
1120 pg_fatal("template0 must not allow connections, "
1121 "i.e. its pg_database.datallowconn must be false");
1123 else
1126 * avoid datallowconn == false databases from being skipped on
1127 * restore
1129 if (strcmp(datallowconn, "f") == 0)
1131 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1132 pg_fatal("could not open file \"%s\": %m", output_path);
1134 fprintf(script, "%s\n", datname);
1139 PQclear(dbres);
1141 PQfinish(conn_template1);
1143 if (script)
1145 fclose(script);
1146 pg_log(PG_REPORT, "fatal");
1147 pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
1148 "pg_database.datallowconn must be true. Your installation contains\n"
1149 "non-template0 databases with their pg_database.datallowconn set to\n"
1150 "false. Consider allowing connection for all non-template0 databases\n"
1151 "or drop the databases which do not allow connections. A list of\n"
1152 "databases with the problem is in the file:\n"
1153 " %s", output_path);
1155 else
1156 check_ok();
1161 * check_for_prepared_transactions()
1163 * Make sure there are no prepared transactions because the storage format
1164 * might have changed.
1166 static void
1167 check_for_prepared_transactions(ClusterInfo *cluster)
1169 PGresult *res;
1170 PGconn *conn = connectToServer(cluster, "template1");
1172 prep_status("Checking for prepared transactions");
1174 res = executeQueryOrDie(conn,
1175 "SELECT * "
1176 "FROM pg_catalog.pg_prepared_xacts");
1178 if (PQntuples(res) != 0)
1180 if (cluster == &old_cluster)
1181 pg_fatal("The source cluster contains prepared transactions");
1182 else
1183 pg_fatal("The target cluster contains prepared transactions");
1186 PQclear(res);
1188 PQfinish(conn);
1190 check_ok();
1195 * check_for_isn_and_int8_passing_mismatch()
1197 * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
1198 * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
1199 * it must match for the old and new servers.
1201 static void
1202 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
1204 int dbnum;
1205 FILE *script = NULL;
1206 char output_path[MAXPGPATH];
1208 prep_status("Checking for contrib/isn with bigint-passing mismatch");
1210 if (old_cluster.controldata.float8_pass_by_value ==
1211 new_cluster.controldata.float8_pass_by_value)
1213 /* no mismatch */
1214 check_ok();
1215 return;
1218 snprintf(output_path, sizeof(output_path), "%s/%s",
1219 log_opts.basedir,
1220 "contrib_isn_and_int8_pass_by_value.txt");
1222 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1224 PGresult *res;
1225 bool db_used = false;
1226 int ntups;
1227 int rowno;
1228 int i_nspname,
1229 i_proname;
1230 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1231 PGconn *conn = connectToServer(cluster, active_db->db_name);
1233 /* Find any functions coming from contrib/isn */
1234 res = executeQueryOrDie(conn,
1235 "SELECT n.nspname, p.proname "
1236 "FROM pg_catalog.pg_proc p, "
1237 " pg_catalog.pg_namespace n "
1238 "WHERE p.pronamespace = n.oid AND "
1239 " p.probin = '$libdir/isn'");
1241 ntups = PQntuples(res);
1242 i_nspname = PQfnumber(res, "nspname");
1243 i_proname = PQfnumber(res, "proname");
1244 for (rowno = 0; rowno < ntups; rowno++)
1246 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1247 pg_fatal("could not open file \"%s\": %m", output_path);
1248 if (!db_used)
1250 fprintf(script, "In database: %s\n", active_db->db_name);
1251 db_used = true;
1253 fprintf(script, " %s.%s\n",
1254 PQgetvalue(res, rowno, i_nspname),
1255 PQgetvalue(res, rowno, i_proname));
1258 PQclear(res);
1260 PQfinish(conn);
1263 if (script)
1265 fclose(script);
1266 pg_log(PG_REPORT, "fatal");
1267 pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
1268 "bigint data type. Your old and new clusters pass bigint values\n"
1269 "differently so this cluster cannot currently be upgraded. You can\n"
1270 "manually dump databases in the old cluster that use \"contrib/isn\"\n"
1271 "facilities, drop them, perform the upgrade, and then restore them. A\n"
1272 "list of the problem functions is in the file:\n"
1273 " %s", output_path);
1275 else
1276 check_ok();
1280 * Verify that no user defined postfix operators exist.
1282 static void
1283 check_for_user_defined_postfix_ops(ClusterInfo *cluster)
1285 int dbnum;
1286 FILE *script = NULL;
1287 char output_path[MAXPGPATH];
1289 prep_status("Checking for user-defined postfix operators");
1291 snprintf(output_path, sizeof(output_path), "%s/%s",
1292 log_opts.basedir,
1293 "postfix_ops.txt");
1295 /* Find any user defined postfix operators */
1296 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1298 PGresult *res;
1299 bool db_used = false;
1300 int ntups;
1301 int rowno;
1302 int i_oproid,
1303 i_oprnsp,
1304 i_oprname,
1305 i_typnsp,
1306 i_typname;
1307 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1308 PGconn *conn = connectToServer(cluster, active_db->db_name);
1311 * The query below hardcodes FirstNormalObjectId as 16384 rather than
1312 * interpolating that C #define into the query because, if that
1313 * #define is ever changed, the cutoff we want to use is the value
1314 * used by pre-version 14 servers, not that of some future version.
1316 res = executeQueryOrDie(conn,
1317 "SELECT o.oid AS oproid, "
1318 " n.nspname AS oprnsp, "
1319 " o.oprname, "
1320 " tn.nspname AS typnsp, "
1321 " t.typname "
1322 "FROM pg_catalog.pg_operator o, "
1323 " pg_catalog.pg_namespace n, "
1324 " pg_catalog.pg_type t, "
1325 " pg_catalog.pg_namespace tn "
1326 "WHERE o.oprnamespace = n.oid AND "
1327 " o.oprleft = t.oid AND "
1328 " t.typnamespace = tn.oid AND "
1329 " o.oprright = 0 AND "
1330 " o.oid >= 16384");
1331 ntups = PQntuples(res);
1332 i_oproid = PQfnumber(res, "oproid");
1333 i_oprnsp = PQfnumber(res, "oprnsp");
1334 i_oprname = PQfnumber(res, "oprname");
1335 i_typnsp = PQfnumber(res, "typnsp");
1336 i_typname = PQfnumber(res, "typname");
1337 for (rowno = 0; rowno < ntups; rowno++)
1339 if (script == NULL &&
1340 (script = fopen_priv(output_path, "w")) == NULL)
1341 pg_fatal("could not open file \"%s\": %m", output_path);
1342 if (!db_used)
1344 fprintf(script, "In database: %s\n", active_db->db_name);
1345 db_used = true;
1347 fprintf(script, " (oid=%s) %s.%s (%s.%s, NONE)\n",
1348 PQgetvalue(res, rowno, i_oproid),
1349 PQgetvalue(res, rowno, i_oprnsp),
1350 PQgetvalue(res, rowno, i_oprname),
1351 PQgetvalue(res, rowno, i_typnsp),
1352 PQgetvalue(res, rowno, i_typname));
1355 PQclear(res);
1357 PQfinish(conn);
1360 if (script)
1362 fclose(script);
1363 pg_log(PG_REPORT, "fatal");
1364 pg_fatal("Your installation contains user-defined postfix operators, which are not\n"
1365 "supported anymore. Consider dropping the postfix operators and replacing\n"
1366 "them with prefix operators or function calls.\n"
1367 "A list of user-defined postfix operators is in the file:\n"
1368 " %s", output_path);
1370 else
1371 check_ok();
1375 * check_for_incompatible_polymorphics()
1377 * Make sure nothing is using old polymorphic functions with
1378 * anyarray/anyelement rather than the new anycompatible variants.
1380 static void
1381 check_for_incompatible_polymorphics(ClusterInfo *cluster)
1383 PGresult *res;
1384 FILE *script = NULL;
1385 char output_path[MAXPGPATH];
1386 PQExpBufferData old_polymorphics;
1388 prep_status("Checking for incompatible polymorphic functions");
1390 snprintf(output_path, sizeof(output_path), "%s/%s",
1391 log_opts.basedir,
1392 "incompatible_polymorphics.txt");
1394 /* The set of problematic functions varies a bit in different versions */
1395 initPQExpBuffer(&old_polymorphics);
1397 appendPQExpBufferStr(&old_polymorphics,
1398 "'array_append(anyarray,anyelement)'"
1399 ", 'array_cat(anyarray,anyarray)'"
1400 ", 'array_prepend(anyelement,anyarray)'");
1402 if (GET_MAJOR_VERSION(cluster->major_version) >= 903)
1403 appendPQExpBufferStr(&old_polymorphics,
1404 ", 'array_remove(anyarray,anyelement)'"
1405 ", 'array_replace(anyarray,anyelement,anyelement)'");
1407 if (GET_MAJOR_VERSION(cluster->major_version) >= 905)
1408 appendPQExpBufferStr(&old_polymorphics,
1409 ", 'array_position(anyarray,anyelement)'"
1410 ", 'array_position(anyarray,anyelement,integer)'"
1411 ", 'array_positions(anyarray,anyelement)'"
1412 ", 'width_bucket(anyelement,anyarray)'");
1414 for (int dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1416 bool db_used = false;
1417 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1418 PGconn *conn = connectToServer(cluster, active_db->db_name);
1419 int ntups;
1420 int i_objkind,
1421 i_objname;
1424 * The query below hardcodes FirstNormalObjectId as 16384 rather than
1425 * interpolating that C #define into the query because, if that
1426 * #define is ever changed, the cutoff we want to use is the value
1427 * used by pre-version 14 servers, not that of some future version.
1429 res = executeQueryOrDie(conn,
1430 /* Aggregate transition functions */
1431 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1432 "FROM pg_proc AS p "
1433 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1434 "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
1435 "WHERE p.oid >= 16384 "
1436 "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
1437 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1439 /* Aggregate final functions */
1440 "UNION ALL "
1441 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1442 "FROM pg_proc AS p "
1443 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1444 "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
1445 "WHERE p.oid >= 16384 "
1446 "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
1447 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1449 /* Operators */
1450 "UNION ALL "
1451 "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
1452 "FROM pg_operator AS op "
1453 "WHERE op.oid >= 16384 "
1454 "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
1455 "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[]);",
1456 old_polymorphics.data,
1457 old_polymorphics.data,
1458 old_polymorphics.data);
1460 ntups = PQntuples(res);
1462 i_objkind = PQfnumber(res, "objkind");
1463 i_objname = PQfnumber(res, "objname");
1465 for (int rowno = 0; rowno < ntups; rowno++)
1467 if (script == NULL &&
1468 (script = fopen_priv(output_path, "w")) == NULL)
1469 pg_fatal("could not open file \"%s\": %m", output_path);
1470 if (!db_used)
1472 fprintf(script, "In database: %s\n", active_db->db_name);
1473 db_used = true;
1476 fprintf(script, " %s: %s\n",
1477 PQgetvalue(res, rowno, i_objkind),
1478 PQgetvalue(res, rowno, i_objname));
1481 PQclear(res);
1482 PQfinish(conn);
1485 if (script)
1487 fclose(script);
1488 pg_log(PG_REPORT, "fatal");
1489 pg_fatal("Your installation contains user-defined objects that refer to internal\n"
1490 "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
1491 "These user-defined objects must be dropped before upgrading and restored\n"
1492 "afterwards, changing them to refer to the new corresponding functions with\n"
1493 "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
1494 "A list of the problematic objects is in the file:\n"
1495 " %s", output_path);
1497 else
1498 check_ok();
1500 termPQExpBuffer(&old_polymorphics);
1504 * Verify that no tables are declared WITH OIDS.
1506 static void
1507 check_for_tables_with_oids(ClusterInfo *cluster)
1509 int dbnum;
1510 FILE *script = NULL;
1511 char output_path[MAXPGPATH];
1513 prep_status("Checking for tables WITH OIDS");
1515 snprintf(output_path, sizeof(output_path), "%s/%s",
1516 log_opts.basedir,
1517 "tables_with_oids.txt");
1519 /* Find any tables declared WITH OIDS */
1520 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1522 PGresult *res;
1523 bool db_used = false;
1524 int ntups;
1525 int rowno;
1526 int i_nspname,
1527 i_relname;
1528 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1529 PGconn *conn = connectToServer(cluster, active_db->db_name);
1531 res = executeQueryOrDie(conn,
1532 "SELECT n.nspname, c.relname "
1533 "FROM pg_catalog.pg_class c, "
1534 " pg_catalog.pg_namespace n "
1535 "WHERE c.relnamespace = n.oid AND "
1536 " c.relhasoids AND"
1537 " n.nspname NOT IN ('pg_catalog')");
1539 ntups = PQntuples(res);
1540 i_nspname = PQfnumber(res, "nspname");
1541 i_relname = PQfnumber(res, "relname");
1542 for (rowno = 0; rowno < ntups; rowno++)
1544 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1545 pg_fatal("could not open file \"%s\": %m", output_path);
1546 if (!db_used)
1548 fprintf(script, "In database: %s\n", active_db->db_name);
1549 db_used = true;
1551 fprintf(script, " %s.%s\n",
1552 PQgetvalue(res, rowno, i_nspname),
1553 PQgetvalue(res, rowno, i_relname));
1556 PQclear(res);
1558 PQfinish(conn);
1561 if (script)
1563 fclose(script);
1564 pg_log(PG_REPORT, "fatal");
1565 pg_fatal("Your installation contains tables declared WITH OIDS, which is not\n"
1566 "supported anymore. Consider removing the oid column using\n"
1567 " ALTER TABLE ... SET WITHOUT OIDS;\n"
1568 "A list of tables with the problem is in the file:\n"
1569 " %s", output_path);
1571 else
1572 check_ok();
1577 * check_for_pg_role_prefix()
1579 * Versions older than 9.6 should not have any pg_* roles
1581 static void
1582 check_for_pg_role_prefix(ClusterInfo *cluster)
1584 PGresult *res;
1585 PGconn *conn = connectToServer(cluster, "template1");
1586 int ntups;
1587 int i_roloid;
1588 int i_rolname;
1589 FILE *script = NULL;
1590 char output_path[MAXPGPATH];
1592 prep_status("Checking for roles starting with \"pg_\"");
1594 snprintf(output_path, sizeof(output_path), "%s/%s",
1595 log_opts.basedir,
1596 "pg_role_prefix.txt");
1598 res = executeQueryOrDie(conn,
1599 "SELECT oid AS roloid, rolname "
1600 "FROM pg_catalog.pg_roles "
1601 "WHERE rolname ~ '^pg_'");
1603 ntups = PQntuples(res);
1604 i_roloid = PQfnumber(res, "roloid");
1605 i_rolname = PQfnumber(res, "rolname");
1606 for (int rowno = 0; rowno < ntups; rowno++)
1608 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1609 pg_fatal("could not open file \"%s\": %m", output_path);
1610 fprintf(script, "%s (oid=%s)\n",
1611 PQgetvalue(res, rowno, i_rolname),
1612 PQgetvalue(res, rowno, i_roloid));
1615 PQclear(res);
1617 PQfinish(conn);
1619 if (script)
1621 fclose(script);
1622 pg_log(PG_REPORT, "fatal");
1623 pg_fatal("Your installation contains roles starting with \"pg_\".\n"
1624 "\"pg_\" is a reserved prefix for system roles. The cluster\n"
1625 "cannot be upgraded until these roles are renamed.\n"
1626 "A list of roles starting with \"pg_\" is in the file:\n"
1627 " %s", output_path);
1629 else
1630 check_ok();
1634 * Verify that no user-defined encoding conversions exist.
1636 static void
1637 check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
1639 int dbnum;
1640 FILE *script = NULL;
1641 char output_path[MAXPGPATH];
1643 prep_status("Checking for user-defined encoding conversions");
1645 snprintf(output_path, sizeof(output_path), "%s/%s",
1646 log_opts.basedir,
1647 "encoding_conversions.txt");
1649 /* Find any user defined encoding conversions */
1650 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1652 PGresult *res;
1653 bool db_used = false;
1654 int ntups;
1655 int rowno;
1656 int i_conoid,
1657 i_conname,
1658 i_nspname;
1659 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1660 PGconn *conn = connectToServer(cluster, active_db->db_name);
1663 * The query below hardcodes FirstNormalObjectId as 16384 rather than
1664 * interpolating that C #define into the query because, if that
1665 * #define is ever changed, the cutoff we want to use is the value
1666 * used by pre-version 14 servers, not that of some future version.
1668 res = executeQueryOrDie(conn,
1669 "SELECT c.oid as conoid, c.conname, n.nspname "
1670 "FROM pg_catalog.pg_conversion c, "
1671 " pg_catalog.pg_namespace n "
1672 "WHERE c.connamespace = n.oid AND "
1673 " c.oid >= 16384");
1674 ntups = PQntuples(res);
1675 i_conoid = PQfnumber(res, "conoid");
1676 i_conname = PQfnumber(res, "conname");
1677 i_nspname = PQfnumber(res, "nspname");
1678 for (rowno = 0; rowno < ntups; rowno++)
1680 if (script == NULL &&
1681 (script = fopen_priv(output_path, "w")) == NULL)
1682 pg_fatal("could not open file \"%s\": %m", output_path);
1683 if (!db_used)
1685 fprintf(script, "In database: %s\n", active_db->db_name);
1686 db_used = true;
1688 fprintf(script, " (oid=%s) %s.%s\n",
1689 PQgetvalue(res, rowno, i_conoid),
1690 PQgetvalue(res, rowno, i_nspname),
1691 PQgetvalue(res, rowno, i_conname));
1694 PQclear(res);
1696 PQfinish(conn);
1699 if (script)
1701 fclose(script);
1702 pg_log(PG_REPORT, "fatal");
1703 pg_fatal("Your installation contains user-defined encoding conversions.\n"
1704 "The conversion function parameters changed in PostgreSQL version 14\n"
1705 "so this cluster cannot currently be upgraded. You can remove the\n"
1706 "encoding conversions in the old cluster and restart the upgrade.\n"
1707 "A list of user-defined encoding conversions is in the file:\n"
1708 " %s", output_path);
1710 else
1711 check_ok();
1715 * check_new_cluster_logical_replication_slots()
1717 * Verify that there are no logical replication slots on the new cluster and
1718 * that the parameter settings necessary for creating slots are sufficient.
1720 static void
1721 check_new_cluster_logical_replication_slots(void)
1723 PGresult *res;
1724 PGconn *conn;
1725 int nslots_on_old;
1726 int nslots_on_new;
1727 int max_replication_slots;
1728 char *wal_level;
1730 /* Logical slots can be migrated since PG17. */
1731 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1600)
1732 return;
1734 nslots_on_old = count_old_cluster_logical_slots();
1736 /* Quick return if there are no logical slots to be migrated. */
1737 if (nslots_on_old == 0)
1738 return;
1740 conn = connectToServer(&new_cluster, "template1");
1742 prep_status("Checking for new cluster logical replication slots");
1744 res = executeQueryOrDie(conn, "SELECT count(*) "
1745 "FROM pg_catalog.pg_replication_slots "
1746 "WHERE slot_type = 'logical' AND "
1747 "temporary IS FALSE;");
1749 if (PQntuples(res) != 1)
1750 pg_fatal("could not count the number of logical replication slots");
1752 nslots_on_new = atoi(PQgetvalue(res, 0, 0));
1754 if (nslots_on_new)
1755 pg_fatal("Expected 0 logical replication slots but found %d.",
1756 nslots_on_new);
1758 PQclear(res);
1760 res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
1761 "WHERE name IN ('wal_level', 'max_replication_slots') "
1762 "ORDER BY name DESC;");
1764 if (PQntuples(res) != 2)
1765 pg_fatal("could not determine parameter settings on new cluster");
1767 wal_level = PQgetvalue(res, 0, 0);
1769 if (strcmp(wal_level, "logical") != 0)
1770 pg_fatal("wal_level must be \"logical\", but is set to \"%s\"",
1771 wal_level);
1773 max_replication_slots = atoi(PQgetvalue(res, 1, 0));
1775 if (nslots_on_old > max_replication_slots)
1776 pg_fatal("max_replication_slots (%d) must be greater than or equal to the number of "
1777 "logical replication slots (%d) on the old cluster",
1778 max_replication_slots, nslots_on_old);
1780 PQclear(res);
1781 PQfinish(conn);
1783 check_ok();
1787 * check_new_cluster_subscription_configuration()
1789 * Verify that the max_replication_slots configuration specified is enough for
1790 * creating the subscriptions. This is required to create the replication
1791 * origin for each subscription.
1793 static void
1794 check_new_cluster_subscription_configuration(void)
1796 PGresult *res;
1797 PGconn *conn;
1798 int nsubs_on_old;
1799 int max_replication_slots;
1801 /* Subscriptions and their dependencies can be migrated since PG17. */
1802 if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
1803 return;
1805 nsubs_on_old = count_old_cluster_subscriptions();
1807 /* Quick return if there are no subscriptions to be migrated. */
1808 if (nsubs_on_old == 0)
1809 return;
1811 prep_status("Checking for new cluster configuration for subscriptions");
1813 conn = connectToServer(&new_cluster, "template1");
1815 res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
1816 "WHERE name = 'max_replication_slots';");
1818 if (PQntuples(res) != 1)
1819 pg_fatal("could not determine parameter settings on new cluster");
1821 max_replication_slots = atoi(PQgetvalue(res, 0, 0));
1822 if (nsubs_on_old > max_replication_slots)
1823 pg_fatal("max_replication_slots (%d) must be greater than or equal to the number of "
1824 "subscriptions (%d) on the old cluster",
1825 max_replication_slots, nsubs_on_old);
1827 PQclear(res);
1828 PQfinish(conn);
1830 check_ok();
1834 * check_old_cluster_for_valid_slots()
1836 * Verify that all the logical slots are valid and have consumed all the WAL
1837 * before shutdown.
1839 static void
1840 check_old_cluster_for_valid_slots(bool live_check)
1842 char output_path[MAXPGPATH];
1843 FILE *script = NULL;
1845 prep_status("Checking for valid logical replication slots");
1847 snprintf(output_path, sizeof(output_path), "%s/%s",
1848 log_opts.basedir,
1849 "invalid_logical_slots.txt");
1851 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1853 LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
1855 for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
1857 LogicalSlotInfo *slot = &slot_arr->slots[slotnum];
1859 /* Is the slot usable? */
1860 if (slot->invalid)
1862 if (script == NULL &&
1863 (script = fopen_priv(output_path, "w")) == NULL)
1864 pg_fatal("could not open file \"%s\": %m", output_path);
1866 fprintf(script, "The slot \"%s\" is invalid\n",
1867 slot->slotname);
1869 continue;
1873 * Do additional check to ensure that all logical replication
1874 * slots have consumed all the WAL before shutdown.
1876 * Note: This can be satisfied only when the old cluster has been
1877 * shut down, so we skip this for live checks.
1879 if (!live_check && !slot->caught_up)
1881 if (script == NULL &&
1882 (script = fopen_priv(output_path, "w")) == NULL)
1883 pg_fatal("could not open file \"%s\": %m", output_path);
1885 fprintf(script,
1886 "The slot \"%s\" has not consumed the WAL yet\n",
1887 slot->slotname);
1892 if (script)
1894 fclose(script);
1896 pg_log(PG_REPORT, "fatal");
1897 pg_fatal("Your installation contains logical replication slots that can't be upgraded.\n"
1898 "You can remove invalid slots and/or consume the pending WAL for other slots,\n"
1899 "and then restart the upgrade.\n"
1900 "A list of the problematic slots is in the file:\n"
1901 " %s", output_path);
1904 check_ok();
1908 * check_old_cluster_subscription_state()
1910 * Verify that the replication origin corresponding to each of the
1911 * subscriptions are present and each of the subscribed tables is in
1912 * 'i' (initialize) or 'r' (ready) state.
1914 static void
1915 check_old_cluster_subscription_state(void)
1917 FILE *script = NULL;
1918 char output_path[MAXPGPATH];
1919 int ntup;
1921 prep_status("Checking for subscription state");
1923 snprintf(output_path, sizeof(output_path), "%s/%s",
1924 log_opts.basedir,
1925 "subs_invalid.txt");
1926 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1928 PGresult *res;
1929 DbInfo *active_db = &old_cluster.dbarr.dbs[dbnum];
1930 PGconn *conn = connectToServer(&old_cluster, active_db->db_name);
1932 /* We need to check for pg_replication_origin only once. */
1933 if (dbnum == 0)
1936 * Check that all the subscriptions have their respective
1937 * replication origin.
1939 res = executeQueryOrDie(conn,
1940 "SELECT d.datname, s.subname "
1941 "FROM pg_catalog.pg_subscription s "
1942 "LEFT OUTER JOIN pg_catalog.pg_replication_origin o "
1943 " ON o.roname = 'pg_' || s.oid "
1944 "INNER JOIN pg_catalog.pg_database d "
1945 " ON d.oid = s.subdbid "
1946 "WHERE o.roname iS NULL;");
1948 ntup = PQntuples(res);
1949 for (int i = 0; i < ntup; i++)
1951 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1952 pg_fatal("could not open file \"%s\": %m", output_path);
1953 fprintf(script, "The replication origin is missing for database:\"%s\" subscription:\"%s\"\n",
1954 PQgetvalue(res, i, 0),
1955 PQgetvalue(res, i, 1));
1957 PQclear(res);
1961 * We don't allow upgrade if there is a risk of dangling slot or
1962 * origin corresponding to initial sync after upgrade.
1964 * A slot/origin not created yet refers to the 'i' (initialize) state,
1965 * while 'r' (ready) state refers to a slot/origin created previously
1966 * but already dropped. These states are supported for pg_upgrade. The
1967 * other states listed below are not supported:
1969 * a) SUBREL_STATE_DATASYNC: A relation upgraded while in this state
1970 * would retain a replication slot, which could not be dropped by the
1971 * sync worker spawned after the upgrade because the subscription ID
1972 * used for the slot name won't match anymore.
1974 * b) SUBREL_STATE_SYNCDONE: A relation upgraded while in this state
1975 * would retain the replication origin when there is a failure in
1976 * tablesync worker immediately after dropping the replication slot in
1977 * the publisher.
1979 * c) SUBREL_STATE_FINISHEDCOPY: A tablesync worker spawned to work on
1980 * a relation upgraded while in this state would expect an origin ID
1981 * with the OID of the subscription used before the upgrade, causing
1982 * it to fail.
1984 * d) SUBREL_STATE_SYNCWAIT, SUBREL_STATE_CATCHUP and
1985 * SUBREL_STATE_UNKNOWN: These states are not stored in the catalog,
1986 * so we need not allow these states.
1988 res = executeQueryOrDie(conn,
1989 "SELECT r.srsubstate, s.subname, n.nspname, c.relname "
1990 "FROM pg_catalog.pg_subscription_rel r "
1991 "LEFT JOIN pg_catalog.pg_subscription s"
1992 " ON r.srsubid = s.oid "
1993 "LEFT JOIN pg_catalog.pg_class c"
1994 " ON r.srrelid = c.oid "
1995 "LEFT JOIN pg_catalog.pg_namespace n"
1996 " ON c.relnamespace = n.oid "
1997 "WHERE r.srsubstate NOT IN ('i', 'r') "
1998 "ORDER BY s.subname");
2000 ntup = PQntuples(res);
2001 for (int i = 0; i < ntup; i++)
2003 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
2004 pg_fatal("could not open file \"%s\": %m", output_path);
2006 fprintf(script, "The table sync state \"%s\" is not allowed for database:\"%s\" subscription:\"%s\" schema:\"%s\" relation:\"%s\"\n",
2007 PQgetvalue(res, i, 0),
2008 active_db->db_name,
2009 PQgetvalue(res, i, 1),
2010 PQgetvalue(res, i, 2),
2011 PQgetvalue(res, i, 3));
2014 PQclear(res);
2015 PQfinish(conn);
2018 if (script)
2020 fclose(script);
2021 pg_log(PG_REPORT, "fatal");
2022 pg_fatal("Your installation contains subscriptions without origin or having relations not in i (initialize) or r (ready) state.\n"
2023 "You can allow the initial sync to finish for all relations and then restart the upgrade.\n"
2024 "A list of the problematic subscriptions is in the file:\n"
2025 " %s", output_path);
2027 else
2028 check_ok();