Release temporary array in check_for_data_types_usage().
[pgsql.git] / src / bin / pg_upgrade / check.c
blobfe73ec4f00b38f113d8abf0060c1998bc28d92e0
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 pg_free(results);
526 check_ok();
530 * fix_path_separator
531 * For non-Windows, just return the argument.
532 * For Windows convert any forward slash to a backslash
533 * such as is suitable for arguments to builtin commands
534 * like RMDIR and DEL.
536 static char *
537 fix_path_separator(char *path)
539 #ifdef WIN32
541 char *result;
542 char *c;
544 result = pg_strdup(path);
546 for (c = result; *c != '\0'; c++)
547 if (*c == '/')
548 *c = '\\';
550 return result;
551 #else
553 return path;
554 #endif
557 void
558 output_check_banner(bool live_check)
560 if (user_opts.check && live_check)
562 pg_log(PG_REPORT,
563 "Performing Consistency Checks on Old Live Server\n"
564 "------------------------------------------------");
566 else
568 pg_log(PG_REPORT,
569 "Performing Consistency Checks\n"
570 "-----------------------------");
575 void
576 check_and_dump_old_cluster(bool live_check)
578 /* -- OLD -- */
580 if (!live_check)
581 start_postmaster(&old_cluster, true);
584 * Extract a list of databases, tables, and logical replication slots from
585 * the old cluster.
587 get_db_rel_and_slot_infos(&old_cluster, live_check);
589 init_tablespaces();
591 get_loadable_libraries();
595 * Check for various failure cases
597 check_is_install_user(&old_cluster);
598 check_proper_datallowconn(&old_cluster);
599 check_for_prepared_transactions(&old_cluster);
600 check_for_isn_and_int8_passing_mismatch(&old_cluster);
602 if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1700)
605 * Logical replication slots can be migrated since PG17. See comments
606 * atop get_old_cluster_logical_slot_infos().
608 check_old_cluster_for_valid_slots(live_check);
611 * Subscriptions and their dependencies can be migrated since PG17.
612 * See comments atop get_db_subscription_count().
614 check_old_cluster_subscription_state();
617 check_for_data_types_usage(&old_cluster, data_types_usage_checks);
620 * PG 14 changed the function signature of encoding conversion functions.
621 * Conversions from older versions cannot be upgraded automatically
622 * because the user-defined functions used by the encoding conversions
623 * need to be changed to match the new signature.
625 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
626 check_for_user_defined_encoding_conversions(&old_cluster);
629 * Pre-PG 14 allowed user defined postfix operators, which are not
630 * supported anymore. Verify there are none, iff applicable.
632 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
633 check_for_user_defined_postfix_ops(&old_cluster);
636 * PG 14 changed polymorphic functions from anyarray to
637 * anycompatiblearray.
639 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
640 check_for_incompatible_polymorphics(&old_cluster);
643 * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
644 * supported anymore. Verify there are none, iff applicable.
646 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
647 check_for_tables_with_oids(&old_cluster);
650 * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
651 * hash indexes
653 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
655 if (user_opts.check)
656 old_9_6_invalidate_hash_indexes(&old_cluster, true);
659 /* 9.5 and below should not have roles starting with pg_ */
660 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
661 check_for_pg_role_prefix(&old_cluster);
664 * While not a check option, we do this now because this is the only time
665 * the old server is running.
667 if (!user_opts.check)
668 generate_old_dump();
670 if (!live_check)
671 stop_postmaster(false);
675 void
676 check_new_cluster(void)
678 get_db_rel_and_slot_infos(&new_cluster, false);
680 check_new_cluster_is_empty();
682 check_loadable_libraries();
684 switch (user_opts.transfer_mode)
686 case TRANSFER_MODE_CLONE:
687 check_file_clone();
688 break;
689 case TRANSFER_MODE_COPY:
690 break;
691 case TRANSFER_MODE_COPY_FILE_RANGE:
692 check_copy_file_range();
693 break;
694 case TRANSFER_MODE_LINK:
695 check_hard_link();
696 break;
699 check_is_install_user(&new_cluster);
701 check_for_prepared_transactions(&new_cluster);
703 check_for_new_tablespace_dir();
705 check_new_cluster_logical_replication_slots();
707 check_new_cluster_subscription_configuration();
711 void
712 report_clusters_compatible(void)
714 if (user_opts.check)
716 pg_log(PG_REPORT, "\n*Clusters are compatible*");
717 /* stops new cluster */
718 stop_postmaster(false);
720 cleanup_output_dirs();
721 exit(0);
724 pg_log(PG_REPORT, "\n"
725 "If pg_upgrade fails after this point, you must re-initdb the\n"
726 "new cluster before continuing.");
730 void
731 issue_warnings_and_set_wal_level(void)
734 * We unconditionally start/stop the new server because pg_resetwal -o set
735 * wal_level to 'minimum'. If the user is upgrading standby servers using
736 * the rsync instructions, they will need pg_upgrade to write its final
737 * WAL record showing wal_level as 'replica'.
739 start_postmaster(&new_cluster, true);
741 /* Reindex hash indexes for old < 10.0 */
742 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
743 old_9_6_invalidate_hash_indexes(&new_cluster, false);
745 report_extension_updates(&new_cluster);
747 stop_postmaster(false);
751 void
752 output_completion_banner(char *deletion_script_file_name)
754 PQExpBufferData user_specification;
756 initPQExpBuffer(&user_specification);
757 if (os_info.user_specified)
759 appendPQExpBufferStr(&user_specification, "-U ");
760 appendShellString(&user_specification, os_info.user);
761 appendPQExpBufferChar(&user_specification, ' ');
764 pg_log(PG_REPORT,
765 "Optimizer statistics are not transferred by pg_upgrade.\n"
766 "Once you start the new server, consider running:\n"
767 " %s/vacuumdb %s--all --analyze-in-stages", new_cluster.bindir, user_specification.data);
769 if (deletion_script_file_name)
770 pg_log(PG_REPORT,
771 "Running this script will delete the old cluster's data files:\n"
772 " %s",
773 deletion_script_file_name);
774 else
775 pg_log(PG_REPORT,
776 "Could not create a script to delete the old cluster's data files\n"
777 "because user-defined tablespaces or the new cluster's data directory\n"
778 "exist in the old cluster directory. The old cluster's contents must\n"
779 "be deleted manually.");
781 termPQExpBuffer(&user_specification);
785 void
786 check_cluster_versions(void)
788 prep_status("Checking cluster versions");
790 /* cluster versions should already have been obtained */
791 Assert(old_cluster.major_version != 0);
792 Assert(new_cluster.major_version != 0);
795 * We allow upgrades from/to the same major version for alpha/beta
796 * upgrades
799 if (GET_MAJOR_VERSION(old_cluster.major_version) < 902)
800 pg_fatal("This utility can only upgrade from PostgreSQL version %s and later.",
801 "9.2");
803 /* Only current PG version is supported as a target */
804 if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
805 pg_fatal("This utility can only upgrade to PostgreSQL version %s.",
806 PG_MAJORVERSION);
809 * We can't allow downgrading because we use the target pg_dump, and
810 * pg_dump cannot operate on newer database versions, only current and
811 * older versions.
813 if (old_cluster.major_version > new_cluster.major_version)
814 pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.");
816 /* Ensure binaries match the designated data directories */
817 if (GET_MAJOR_VERSION(old_cluster.major_version) !=
818 GET_MAJOR_VERSION(old_cluster.bin_version))
819 pg_fatal("Old cluster data and binary directories are from different major versions.");
820 if (GET_MAJOR_VERSION(new_cluster.major_version) !=
821 GET_MAJOR_VERSION(new_cluster.bin_version))
822 pg_fatal("New cluster data and binary directories are from different major versions.");
824 check_ok();
828 void
829 check_cluster_compatibility(bool live_check)
831 /* get/check pg_control data of servers */
832 get_control_data(&old_cluster, live_check);
833 get_control_data(&new_cluster, false);
834 check_control_data(&old_cluster.controldata, &new_cluster.controldata);
836 if (live_check && old_cluster.port == new_cluster.port)
837 pg_fatal("When checking a live server, "
838 "the old and new port numbers must be different.");
842 static void
843 check_new_cluster_is_empty(void)
845 int dbnum;
847 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
849 int relnum;
850 RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
852 for (relnum = 0; relnum < rel_arr->nrels;
853 relnum++)
855 /* pg_largeobject and its index should be skipped */
856 if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
857 pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
858 new_cluster.dbarr.dbs[dbnum].db_name,
859 rel_arr->rels[relnum].nspname,
860 rel_arr->rels[relnum].relname);
866 * A previous run of pg_upgrade might have failed and the new cluster
867 * directory recreated, but they might have forgotten to remove
868 * the new cluster's tablespace directories. Therefore, check that
869 * new cluster tablespace directories do not already exist. If
870 * they do, it would cause an error while restoring global objects.
871 * This allows the failure to be detected at check time, rather than
872 * during schema restore.
874 static void
875 check_for_new_tablespace_dir(void)
877 int tblnum;
878 char new_tablespace_dir[MAXPGPATH];
880 prep_status("Checking for new cluster tablespace directories");
882 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
884 struct stat statbuf;
886 snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
887 os_info.old_tablespaces[tblnum],
888 new_cluster.tablespace_suffix);
890 if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
891 pg_fatal("new cluster tablespace directory already exists: \"%s\"",
892 new_tablespace_dir);
895 check_ok();
899 * create_script_for_old_cluster_deletion()
901 * This is particularly useful for tablespace deletion.
903 void
904 create_script_for_old_cluster_deletion(char **deletion_script_file_name)
906 FILE *script = NULL;
907 int tblnum;
908 char old_cluster_pgdata[MAXPGPATH],
909 new_cluster_pgdata[MAXPGPATH];
911 *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
912 SCRIPT_PREFIX, SCRIPT_EXT);
914 strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
915 canonicalize_path(old_cluster_pgdata);
917 strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
918 canonicalize_path(new_cluster_pgdata);
920 /* Some people put the new data directory inside the old one. */
921 if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
923 pg_log(PG_WARNING,
924 "\nWARNING: new data directory should not be inside the old data directory, i.e. %s", old_cluster_pgdata);
926 /* Unlink file in case it is left over from a previous run. */
927 unlink(*deletion_script_file_name);
928 pg_free(*deletion_script_file_name);
929 *deletion_script_file_name = NULL;
930 return;
934 * Some users (oddly) create tablespaces inside the cluster data
935 * directory. We can't create a proper old cluster delete script in that
936 * case.
938 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
940 char old_tablespace_dir[MAXPGPATH];
942 strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
943 canonicalize_path(old_tablespace_dir);
944 if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
946 /* reproduce warning from CREATE TABLESPACE that is in the log */
947 pg_log(PG_WARNING,
948 "\nWARNING: user-defined tablespace locations should not be inside the data directory, i.e. %s", old_tablespace_dir);
950 /* Unlink file in case it is left over from a previous run. */
951 unlink(*deletion_script_file_name);
952 pg_free(*deletion_script_file_name);
953 *deletion_script_file_name = NULL;
954 return;
958 prep_status("Creating script to delete old cluster");
960 if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
961 pg_fatal("could not open file \"%s\": %m",
962 *deletion_script_file_name);
964 #ifndef WIN32
965 /* add shebang header */
966 fprintf(script, "#!/bin/sh\n\n");
967 #endif
969 /* delete old cluster's default tablespace */
970 fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
971 fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
973 /* delete old cluster's alternate tablespaces */
974 for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
977 * Do the old cluster's per-database directories share a directory
978 * with a new version-specific tablespace?
980 if (strlen(old_cluster.tablespace_suffix) == 0)
982 /* delete per-database directories */
983 int dbnum;
985 fprintf(script, "\n");
987 for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
988 fprintf(script, RMDIR_CMD " %c%s%c%u%c\n", PATH_QUOTE,
989 fix_path_separator(os_info.old_tablespaces[tblnum]),
990 PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid,
991 PATH_QUOTE);
993 else
995 char *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
998 * Simply delete the tablespace directory, which might be ".old"
999 * or a version-specific subdirectory.
1001 fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
1002 fix_path_separator(os_info.old_tablespaces[tblnum]),
1003 fix_path_separator(suffix_path), PATH_QUOTE);
1004 pfree(suffix_path);
1008 fclose(script);
1010 #ifndef WIN32
1011 if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
1012 pg_fatal("could not add execute permission to file \"%s\": %m",
1013 *deletion_script_file_name);
1014 #endif
1016 check_ok();
1021 * check_is_install_user()
1023 * Check we are the install user, and that the new cluster
1024 * has no other users.
1026 static void
1027 check_is_install_user(ClusterInfo *cluster)
1029 PGresult *res;
1030 PGconn *conn = connectToServer(cluster, "template1");
1032 prep_status("Checking database user is the install user");
1034 /* Can't use pg_authid because only superusers can view it. */
1035 res = executeQueryOrDie(conn,
1036 "SELECT rolsuper, oid "
1037 "FROM pg_catalog.pg_roles "
1038 "WHERE rolname = current_user "
1039 "AND rolname !~ '^pg_'");
1042 * We only allow the install user in the new cluster (see comment below)
1043 * and we preserve pg_authid.oid, so this must be the install user in the
1044 * old cluster too.
1046 if (PQntuples(res) != 1 ||
1047 atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
1048 pg_fatal("database user \"%s\" is not the install user",
1049 os_info.user);
1051 PQclear(res);
1053 res = executeQueryOrDie(conn,
1054 "SELECT COUNT(*) "
1055 "FROM pg_catalog.pg_roles "
1056 "WHERE rolname !~ '^pg_'");
1058 if (PQntuples(res) != 1)
1059 pg_fatal("could not determine the number of users");
1062 * We only allow the install user in the new cluster because other defined
1063 * users might match users defined in the old cluster and generate an
1064 * error during pg_dump restore.
1066 if (cluster == &new_cluster && strcmp(PQgetvalue(res, 0, 0), "1") != 0)
1067 pg_fatal("Only the install user can be defined in the new cluster.");
1069 PQclear(res);
1071 PQfinish(conn);
1073 check_ok();
1078 * check_proper_datallowconn
1080 * Ensure that all non-template0 databases allow connections since they
1081 * otherwise won't be restored; and that template0 explicitly doesn't allow
1082 * connections since it would make pg_dumpall --globals restore fail.
1084 static void
1085 check_proper_datallowconn(ClusterInfo *cluster)
1087 int dbnum;
1088 PGconn *conn_template1;
1089 PGresult *dbres;
1090 int ntups;
1091 int i_datname;
1092 int i_datallowconn;
1093 FILE *script = NULL;
1094 char output_path[MAXPGPATH];
1096 prep_status("Checking database connection settings");
1098 snprintf(output_path, sizeof(output_path), "%s/%s",
1099 log_opts.basedir,
1100 "databases_with_datallowconn_false.txt");
1102 conn_template1 = connectToServer(cluster, "template1");
1104 /* get database names */
1105 dbres = executeQueryOrDie(conn_template1,
1106 "SELECT datname, datallowconn "
1107 "FROM pg_catalog.pg_database");
1109 i_datname = PQfnumber(dbres, "datname");
1110 i_datallowconn = PQfnumber(dbres, "datallowconn");
1112 ntups = PQntuples(dbres);
1113 for (dbnum = 0; dbnum < ntups; dbnum++)
1115 char *datname = PQgetvalue(dbres, dbnum, i_datname);
1116 char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
1118 if (strcmp(datname, "template0") == 0)
1120 /* avoid restore failure when pg_dumpall tries to create template0 */
1121 if (strcmp(datallowconn, "t") == 0)
1122 pg_fatal("template0 must not allow connections, "
1123 "i.e. its pg_database.datallowconn must be false");
1125 else
1128 * avoid datallowconn == false databases from being skipped on
1129 * restore
1131 if (strcmp(datallowconn, "f") == 0)
1133 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1134 pg_fatal("could not open file \"%s\": %m", output_path);
1136 fprintf(script, "%s\n", datname);
1141 PQclear(dbres);
1143 PQfinish(conn_template1);
1145 if (script)
1147 fclose(script);
1148 pg_log(PG_REPORT, "fatal");
1149 pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
1150 "pg_database.datallowconn must be true. Your installation contains\n"
1151 "non-template0 databases with their pg_database.datallowconn set to\n"
1152 "false. Consider allowing connection for all non-template0 databases\n"
1153 "or drop the databases which do not allow connections. A list of\n"
1154 "databases with the problem is in the file:\n"
1155 " %s", output_path);
1157 else
1158 check_ok();
1163 * check_for_prepared_transactions()
1165 * Make sure there are no prepared transactions because the storage format
1166 * might have changed.
1168 static void
1169 check_for_prepared_transactions(ClusterInfo *cluster)
1171 PGresult *res;
1172 PGconn *conn = connectToServer(cluster, "template1");
1174 prep_status("Checking for prepared transactions");
1176 res = executeQueryOrDie(conn,
1177 "SELECT * "
1178 "FROM pg_catalog.pg_prepared_xacts");
1180 if (PQntuples(res) != 0)
1182 if (cluster == &old_cluster)
1183 pg_fatal("The source cluster contains prepared transactions");
1184 else
1185 pg_fatal("The target cluster contains prepared transactions");
1188 PQclear(res);
1190 PQfinish(conn);
1192 check_ok();
1197 * check_for_isn_and_int8_passing_mismatch()
1199 * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
1200 * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
1201 * it must match for the old and new servers.
1203 static void
1204 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
1206 int dbnum;
1207 FILE *script = NULL;
1208 char output_path[MAXPGPATH];
1210 prep_status("Checking for contrib/isn with bigint-passing mismatch");
1212 if (old_cluster.controldata.float8_pass_by_value ==
1213 new_cluster.controldata.float8_pass_by_value)
1215 /* no mismatch */
1216 check_ok();
1217 return;
1220 snprintf(output_path, sizeof(output_path), "%s/%s",
1221 log_opts.basedir,
1222 "contrib_isn_and_int8_pass_by_value.txt");
1224 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1226 PGresult *res;
1227 bool db_used = false;
1228 int ntups;
1229 int rowno;
1230 int i_nspname,
1231 i_proname;
1232 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1233 PGconn *conn = connectToServer(cluster, active_db->db_name);
1235 /* Find any functions coming from contrib/isn */
1236 res = executeQueryOrDie(conn,
1237 "SELECT n.nspname, p.proname "
1238 "FROM pg_catalog.pg_proc p, "
1239 " pg_catalog.pg_namespace n "
1240 "WHERE p.pronamespace = n.oid AND "
1241 " p.probin = '$libdir/isn'");
1243 ntups = PQntuples(res);
1244 i_nspname = PQfnumber(res, "nspname");
1245 i_proname = PQfnumber(res, "proname");
1246 for (rowno = 0; rowno < ntups; rowno++)
1248 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1249 pg_fatal("could not open file \"%s\": %m", output_path);
1250 if (!db_used)
1252 fprintf(script, "In database: %s\n", active_db->db_name);
1253 db_used = true;
1255 fprintf(script, " %s.%s\n",
1256 PQgetvalue(res, rowno, i_nspname),
1257 PQgetvalue(res, rowno, i_proname));
1260 PQclear(res);
1262 PQfinish(conn);
1265 if (script)
1267 fclose(script);
1268 pg_log(PG_REPORT, "fatal");
1269 pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
1270 "bigint data type. Your old and new clusters pass bigint values\n"
1271 "differently so this cluster cannot currently be upgraded. You can\n"
1272 "manually dump databases in the old cluster that use \"contrib/isn\"\n"
1273 "facilities, drop them, perform the upgrade, and then restore them. A\n"
1274 "list of the problem functions is in the file:\n"
1275 " %s", output_path);
1277 else
1278 check_ok();
1282 * Verify that no user defined postfix operators exist.
1284 static void
1285 check_for_user_defined_postfix_ops(ClusterInfo *cluster)
1287 int dbnum;
1288 FILE *script = NULL;
1289 char output_path[MAXPGPATH];
1291 prep_status("Checking for user-defined postfix operators");
1293 snprintf(output_path, sizeof(output_path), "%s/%s",
1294 log_opts.basedir,
1295 "postfix_ops.txt");
1297 /* Find any user defined postfix operators */
1298 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1300 PGresult *res;
1301 bool db_used = false;
1302 int ntups;
1303 int rowno;
1304 int i_oproid,
1305 i_oprnsp,
1306 i_oprname,
1307 i_typnsp,
1308 i_typname;
1309 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1310 PGconn *conn = connectToServer(cluster, active_db->db_name);
1313 * The query below hardcodes FirstNormalObjectId as 16384 rather than
1314 * interpolating that C #define into the query because, if that
1315 * #define is ever changed, the cutoff we want to use is the value
1316 * used by pre-version 14 servers, not that of some future version.
1318 res = executeQueryOrDie(conn,
1319 "SELECT o.oid AS oproid, "
1320 " n.nspname AS oprnsp, "
1321 " o.oprname, "
1322 " tn.nspname AS typnsp, "
1323 " t.typname "
1324 "FROM pg_catalog.pg_operator o, "
1325 " pg_catalog.pg_namespace n, "
1326 " pg_catalog.pg_type t, "
1327 " pg_catalog.pg_namespace tn "
1328 "WHERE o.oprnamespace = n.oid AND "
1329 " o.oprleft = t.oid AND "
1330 " t.typnamespace = tn.oid AND "
1331 " o.oprright = 0 AND "
1332 " o.oid >= 16384");
1333 ntups = PQntuples(res);
1334 i_oproid = PQfnumber(res, "oproid");
1335 i_oprnsp = PQfnumber(res, "oprnsp");
1336 i_oprname = PQfnumber(res, "oprname");
1337 i_typnsp = PQfnumber(res, "typnsp");
1338 i_typname = PQfnumber(res, "typname");
1339 for (rowno = 0; rowno < ntups; rowno++)
1341 if (script == NULL &&
1342 (script = fopen_priv(output_path, "w")) == NULL)
1343 pg_fatal("could not open file \"%s\": %m", output_path);
1344 if (!db_used)
1346 fprintf(script, "In database: %s\n", active_db->db_name);
1347 db_used = true;
1349 fprintf(script, " (oid=%s) %s.%s (%s.%s, NONE)\n",
1350 PQgetvalue(res, rowno, i_oproid),
1351 PQgetvalue(res, rowno, i_oprnsp),
1352 PQgetvalue(res, rowno, i_oprname),
1353 PQgetvalue(res, rowno, i_typnsp),
1354 PQgetvalue(res, rowno, i_typname));
1357 PQclear(res);
1359 PQfinish(conn);
1362 if (script)
1364 fclose(script);
1365 pg_log(PG_REPORT, "fatal");
1366 pg_fatal("Your installation contains user-defined postfix operators, which are not\n"
1367 "supported anymore. Consider dropping the postfix operators and replacing\n"
1368 "them with prefix operators or function calls.\n"
1369 "A list of user-defined postfix operators is in the file:\n"
1370 " %s", output_path);
1372 else
1373 check_ok();
1377 * check_for_incompatible_polymorphics()
1379 * Make sure nothing is using old polymorphic functions with
1380 * anyarray/anyelement rather than the new anycompatible variants.
1382 static void
1383 check_for_incompatible_polymorphics(ClusterInfo *cluster)
1385 PGresult *res;
1386 FILE *script = NULL;
1387 char output_path[MAXPGPATH];
1388 PQExpBufferData old_polymorphics;
1390 prep_status("Checking for incompatible polymorphic functions");
1392 snprintf(output_path, sizeof(output_path), "%s/%s",
1393 log_opts.basedir,
1394 "incompatible_polymorphics.txt");
1396 /* The set of problematic functions varies a bit in different versions */
1397 initPQExpBuffer(&old_polymorphics);
1399 appendPQExpBufferStr(&old_polymorphics,
1400 "'array_append(anyarray,anyelement)'"
1401 ", 'array_cat(anyarray,anyarray)'"
1402 ", 'array_prepend(anyelement,anyarray)'");
1404 if (GET_MAJOR_VERSION(cluster->major_version) >= 903)
1405 appendPQExpBufferStr(&old_polymorphics,
1406 ", 'array_remove(anyarray,anyelement)'"
1407 ", 'array_replace(anyarray,anyelement,anyelement)'");
1409 if (GET_MAJOR_VERSION(cluster->major_version) >= 905)
1410 appendPQExpBufferStr(&old_polymorphics,
1411 ", 'array_position(anyarray,anyelement)'"
1412 ", 'array_position(anyarray,anyelement,integer)'"
1413 ", 'array_positions(anyarray,anyelement)'"
1414 ", 'width_bucket(anyelement,anyarray)'");
1416 for (int dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1418 bool db_used = false;
1419 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1420 PGconn *conn = connectToServer(cluster, active_db->db_name);
1421 int ntups;
1422 int i_objkind,
1423 i_objname;
1426 * The query below hardcodes FirstNormalObjectId as 16384 rather than
1427 * interpolating that C #define into the query because, if that
1428 * #define is ever changed, the cutoff we want to use is the value
1429 * used by pre-version 14 servers, not that of some future version.
1431 res = executeQueryOrDie(conn,
1432 /* Aggregate transition functions */
1433 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1434 "FROM pg_proc AS p "
1435 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1436 "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
1437 "WHERE p.oid >= 16384 "
1438 "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
1439 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1441 /* Aggregate final functions */
1442 "UNION ALL "
1443 "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1444 "FROM pg_proc AS p "
1445 "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1446 "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
1447 "WHERE p.oid >= 16384 "
1448 "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
1449 "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1451 /* Operators */
1452 "UNION ALL "
1453 "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
1454 "FROM pg_operator AS op "
1455 "WHERE op.oid >= 16384 "
1456 "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
1457 "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[]);",
1458 old_polymorphics.data,
1459 old_polymorphics.data,
1460 old_polymorphics.data);
1462 ntups = PQntuples(res);
1464 i_objkind = PQfnumber(res, "objkind");
1465 i_objname = PQfnumber(res, "objname");
1467 for (int rowno = 0; rowno < ntups; rowno++)
1469 if (script == NULL &&
1470 (script = fopen_priv(output_path, "w")) == NULL)
1471 pg_fatal("could not open file \"%s\": %m", output_path);
1472 if (!db_used)
1474 fprintf(script, "In database: %s\n", active_db->db_name);
1475 db_used = true;
1478 fprintf(script, " %s: %s\n",
1479 PQgetvalue(res, rowno, i_objkind),
1480 PQgetvalue(res, rowno, i_objname));
1483 PQclear(res);
1484 PQfinish(conn);
1487 if (script)
1489 fclose(script);
1490 pg_log(PG_REPORT, "fatal");
1491 pg_fatal("Your installation contains user-defined objects that refer to internal\n"
1492 "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
1493 "These user-defined objects must be dropped before upgrading and restored\n"
1494 "afterwards, changing them to refer to the new corresponding functions with\n"
1495 "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
1496 "A list of the problematic objects is in the file:\n"
1497 " %s", output_path);
1499 else
1500 check_ok();
1502 termPQExpBuffer(&old_polymorphics);
1506 * Verify that no tables are declared WITH OIDS.
1508 static void
1509 check_for_tables_with_oids(ClusterInfo *cluster)
1511 int dbnum;
1512 FILE *script = NULL;
1513 char output_path[MAXPGPATH];
1515 prep_status("Checking for tables WITH OIDS");
1517 snprintf(output_path, sizeof(output_path), "%s/%s",
1518 log_opts.basedir,
1519 "tables_with_oids.txt");
1521 /* Find any tables declared WITH OIDS */
1522 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1524 PGresult *res;
1525 bool db_used = false;
1526 int ntups;
1527 int rowno;
1528 int i_nspname,
1529 i_relname;
1530 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1531 PGconn *conn = connectToServer(cluster, active_db->db_name);
1533 res = executeQueryOrDie(conn,
1534 "SELECT n.nspname, c.relname "
1535 "FROM pg_catalog.pg_class c, "
1536 " pg_catalog.pg_namespace n "
1537 "WHERE c.relnamespace = n.oid AND "
1538 " c.relhasoids AND"
1539 " n.nspname NOT IN ('pg_catalog')");
1541 ntups = PQntuples(res);
1542 i_nspname = PQfnumber(res, "nspname");
1543 i_relname = PQfnumber(res, "relname");
1544 for (rowno = 0; rowno < ntups; rowno++)
1546 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1547 pg_fatal("could not open file \"%s\": %m", output_path);
1548 if (!db_used)
1550 fprintf(script, "In database: %s\n", active_db->db_name);
1551 db_used = true;
1553 fprintf(script, " %s.%s\n",
1554 PQgetvalue(res, rowno, i_nspname),
1555 PQgetvalue(res, rowno, i_relname));
1558 PQclear(res);
1560 PQfinish(conn);
1563 if (script)
1565 fclose(script);
1566 pg_log(PG_REPORT, "fatal");
1567 pg_fatal("Your installation contains tables declared WITH OIDS, which is not\n"
1568 "supported anymore. Consider removing the oid column using\n"
1569 " ALTER TABLE ... SET WITHOUT OIDS;\n"
1570 "A list of tables with the problem is in the file:\n"
1571 " %s", output_path);
1573 else
1574 check_ok();
1579 * check_for_pg_role_prefix()
1581 * Versions older than 9.6 should not have any pg_* roles
1583 static void
1584 check_for_pg_role_prefix(ClusterInfo *cluster)
1586 PGresult *res;
1587 PGconn *conn = connectToServer(cluster, "template1");
1588 int ntups;
1589 int i_roloid;
1590 int i_rolname;
1591 FILE *script = NULL;
1592 char output_path[MAXPGPATH];
1594 prep_status("Checking for roles starting with \"pg_\"");
1596 snprintf(output_path, sizeof(output_path), "%s/%s",
1597 log_opts.basedir,
1598 "pg_role_prefix.txt");
1600 res = executeQueryOrDie(conn,
1601 "SELECT oid AS roloid, rolname "
1602 "FROM pg_catalog.pg_roles "
1603 "WHERE rolname ~ '^pg_'");
1605 ntups = PQntuples(res);
1606 i_roloid = PQfnumber(res, "roloid");
1607 i_rolname = PQfnumber(res, "rolname");
1608 for (int rowno = 0; rowno < ntups; rowno++)
1610 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1611 pg_fatal("could not open file \"%s\": %m", output_path);
1612 fprintf(script, "%s (oid=%s)\n",
1613 PQgetvalue(res, rowno, i_rolname),
1614 PQgetvalue(res, rowno, i_roloid));
1617 PQclear(res);
1619 PQfinish(conn);
1621 if (script)
1623 fclose(script);
1624 pg_log(PG_REPORT, "fatal");
1625 pg_fatal("Your installation contains roles starting with \"pg_\".\n"
1626 "\"pg_\" is a reserved prefix for system roles. The cluster\n"
1627 "cannot be upgraded until these roles are renamed.\n"
1628 "A list of roles starting with \"pg_\" is in the file:\n"
1629 " %s", output_path);
1631 else
1632 check_ok();
1636 * Verify that no user-defined encoding conversions exist.
1638 static void
1639 check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
1641 int dbnum;
1642 FILE *script = NULL;
1643 char output_path[MAXPGPATH];
1645 prep_status("Checking for user-defined encoding conversions");
1647 snprintf(output_path, sizeof(output_path), "%s/%s",
1648 log_opts.basedir,
1649 "encoding_conversions.txt");
1651 /* Find any user defined encoding conversions */
1652 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1654 PGresult *res;
1655 bool db_used = false;
1656 int ntups;
1657 int rowno;
1658 int i_conoid,
1659 i_conname,
1660 i_nspname;
1661 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1662 PGconn *conn = connectToServer(cluster, active_db->db_name);
1665 * The query below hardcodes FirstNormalObjectId as 16384 rather than
1666 * interpolating that C #define into the query because, if that
1667 * #define is ever changed, the cutoff we want to use is the value
1668 * used by pre-version 14 servers, not that of some future version.
1670 res = executeQueryOrDie(conn,
1671 "SELECT c.oid as conoid, c.conname, n.nspname "
1672 "FROM pg_catalog.pg_conversion c, "
1673 " pg_catalog.pg_namespace n "
1674 "WHERE c.connamespace = n.oid AND "
1675 " c.oid >= 16384");
1676 ntups = PQntuples(res);
1677 i_conoid = PQfnumber(res, "conoid");
1678 i_conname = PQfnumber(res, "conname");
1679 i_nspname = PQfnumber(res, "nspname");
1680 for (rowno = 0; rowno < ntups; rowno++)
1682 if (script == NULL &&
1683 (script = fopen_priv(output_path, "w")) == NULL)
1684 pg_fatal("could not open file \"%s\": %m", output_path);
1685 if (!db_used)
1687 fprintf(script, "In database: %s\n", active_db->db_name);
1688 db_used = true;
1690 fprintf(script, " (oid=%s) %s.%s\n",
1691 PQgetvalue(res, rowno, i_conoid),
1692 PQgetvalue(res, rowno, i_nspname),
1693 PQgetvalue(res, rowno, i_conname));
1696 PQclear(res);
1698 PQfinish(conn);
1701 if (script)
1703 fclose(script);
1704 pg_log(PG_REPORT, "fatal");
1705 pg_fatal("Your installation contains user-defined encoding conversions.\n"
1706 "The conversion function parameters changed in PostgreSQL version 14\n"
1707 "so this cluster cannot currently be upgraded. You can remove the\n"
1708 "encoding conversions in the old cluster and restart the upgrade.\n"
1709 "A list of user-defined encoding conversions is in the file:\n"
1710 " %s", output_path);
1712 else
1713 check_ok();
1717 * check_new_cluster_logical_replication_slots()
1719 * Verify that there are no logical replication slots on the new cluster and
1720 * that the parameter settings necessary for creating slots are sufficient.
1722 static void
1723 check_new_cluster_logical_replication_slots(void)
1725 PGresult *res;
1726 PGconn *conn;
1727 int nslots_on_old;
1728 int nslots_on_new;
1729 int max_replication_slots;
1730 char *wal_level;
1732 /* Logical slots can be migrated since PG17. */
1733 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1600)
1734 return;
1736 nslots_on_old = count_old_cluster_logical_slots();
1738 /* Quick return if there are no logical slots to be migrated. */
1739 if (nslots_on_old == 0)
1740 return;
1742 conn = connectToServer(&new_cluster, "template1");
1744 prep_status("Checking for new cluster logical replication slots");
1746 res = executeQueryOrDie(conn, "SELECT count(*) "
1747 "FROM pg_catalog.pg_replication_slots "
1748 "WHERE slot_type = 'logical' AND "
1749 "temporary IS FALSE;");
1751 if (PQntuples(res) != 1)
1752 pg_fatal("could not count the number of logical replication slots");
1754 nslots_on_new = atoi(PQgetvalue(res, 0, 0));
1756 if (nslots_on_new)
1757 pg_fatal("Expected 0 logical replication slots but found %d.",
1758 nslots_on_new);
1760 PQclear(res);
1762 res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
1763 "WHERE name IN ('wal_level', 'max_replication_slots') "
1764 "ORDER BY name DESC;");
1766 if (PQntuples(res) != 2)
1767 pg_fatal("could not determine parameter settings on new cluster");
1769 wal_level = PQgetvalue(res, 0, 0);
1771 if (strcmp(wal_level, "logical") != 0)
1772 pg_fatal("wal_level must be \"logical\", but is set to \"%s\"",
1773 wal_level);
1775 max_replication_slots = atoi(PQgetvalue(res, 1, 0));
1777 if (nslots_on_old > max_replication_slots)
1778 pg_fatal("max_replication_slots (%d) must be greater than or equal to the number of "
1779 "logical replication slots (%d) on the old cluster",
1780 max_replication_slots, nslots_on_old);
1782 PQclear(res);
1783 PQfinish(conn);
1785 check_ok();
1789 * check_new_cluster_subscription_configuration()
1791 * Verify that the max_replication_slots configuration specified is enough for
1792 * creating the subscriptions. This is required to create the replication
1793 * origin for each subscription.
1795 static void
1796 check_new_cluster_subscription_configuration(void)
1798 PGresult *res;
1799 PGconn *conn;
1800 int nsubs_on_old;
1801 int max_replication_slots;
1803 /* Subscriptions and their dependencies can be migrated since PG17. */
1804 if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
1805 return;
1807 nsubs_on_old = count_old_cluster_subscriptions();
1809 /* Quick return if there are no subscriptions to be migrated. */
1810 if (nsubs_on_old == 0)
1811 return;
1813 prep_status("Checking for new cluster configuration for subscriptions");
1815 conn = connectToServer(&new_cluster, "template1");
1817 res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
1818 "WHERE name = 'max_replication_slots';");
1820 if (PQntuples(res) != 1)
1821 pg_fatal("could not determine parameter settings on new cluster");
1823 max_replication_slots = atoi(PQgetvalue(res, 0, 0));
1824 if (nsubs_on_old > max_replication_slots)
1825 pg_fatal("max_replication_slots (%d) must be greater than or equal to the number of "
1826 "subscriptions (%d) on the old cluster",
1827 max_replication_slots, nsubs_on_old);
1829 PQclear(res);
1830 PQfinish(conn);
1832 check_ok();
1836 * check_old_cluster_for_valid_slots()
1838 * Verify that all the logical slots are valid and have consumed all the WAL
1839 * before shutdown.
1841 static void
1842 check_old_cluster_for_valid_slots(bool live_check)
1844 char output_path[MAXPGPATH];
1845 FILE *script = NULL;
1847 prep_status("Checking for valid logical replication slots");
1849 snprintf(output_path, sizeof(output_path), "%s/%s",
1850 log_opts.basedir,
1851 "invalid_logical_slots.txt");
1853 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1855 LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
1857 for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
1859 LogicalSlotInfo *slot = &slot_arr->slots[slotnum];
1861 /* Is the slot usable? */
1862 if (slot->invalid)
1864 if (script == NULL &&
1865 (script = fopen_priv(output_path, "w")) == NULL)
1866 pg_fatal("could not open file \"%s\": %m", output_path);
1868 fprintf(script, "The slot \"%s\" is invalid\n",
1869 slot->slotname);
1871 continue;
1875 * Do additional check to ensure that all logical replication
1876 * slots have consumed all the WAL before shutdown.
1878 * Note: This can be satisfied only when the old cluster has been
1879 * shut down, so we skip this for live checks.
1881 if (!live_check && !slot->caught_up)
1883 if (script == NULL &&
1884 (script = fopen_priv(output_path, "w")) == NULL)
1885 pg_fatal("could not open file \"%s\": %m", output_path);
1887 fprintf(script,
1888 "The slot \"%s\" has not consumed the WAL yet\n",
1889 slot->slotname);
1894 if (script)
1896 fclose(script);
1898 pg_log(PG_REPORT, "fatal");
1899 pg_fatal("Your installation contains logical replication slots that can't be upgraded.\n"
1900 "You can remove invalid slots and/or consume the pending WAL for other slots,\n"
1901 "and then restart the upgrade.\n"
1902 "A list of the problematic slots is in the file:\n"
1903 " %s", output_path);
1906 check_ok();
1910 * check_old_cluster_subscription_state()
1912 * Verify that the replication origin corresponding to each of the
1913 * subscriptions are present and each of the subscribed tables is in
1914 * 'i' (initialize) or 'r' (ready) state.
1916 static void
1917 check_old_cluster_subscription_state(void)
1919 FILE *script = NULL;
1920 char output_path[MAXPGPATH];
1921 int ntup;
1923 prep_status("Checking for subscription state");
1925 snprintf(output_path, sizeof(output_path), "%s/%s",
1926 log_opts.basedir,
1927 "subs_invalid.txt");
1928 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
1930 PGresult *res;
1931 DbInfo *active_db = &old_cluster.dbarr.dbs[dbnum];
1932 PGconn *conn = connectToServer(&old_cluster, active_db->db_name);
1934 /* We need to check for pg_replication_origin only once. */
1935 if (dbnum == 0)
1938 * Check that all the subscriptions have their respective
1939 * replication origin.
1941 res = executeQueryOrDie(conn,
1942 "SELECT d.datname, s.subname "
1943 "FROM pg_catalog.pg_subscription s "
1944 "LEFT OUTER JOIN pg_catalog.pg_replication_origin o "
1945 " ON o.roname = 'pg_' || s.oid "
1946 "INNER JOIN pg_catalog.pg_database d "
1947 " ON d.oid = s.subdbid "
1948 "WHERE o.roname iS NULL;");
1950 ntup = PQntuples(res);
1951 for (int i = 0; i < ntup; i++)
1953 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1954 pg_fatal("could not open file \"%s\": %m", output_path);
1955 fprintf(script, "The replication origin is missing for database:\"%s\" subscription:\"%s\"\n",
1956 PQgetvalue(res, i, 0),
1957 PQgetvalue(res, i, 1));
1959 PQclear(res);
1963 * We don't allow upgrade if there is a risk of dangling slot or
1964 * origin corresponding to initial sync after upgrade.
1966 * A slot/origin not created yet refers to the 'i' (initialize) state,
1967 * while 'r' (ready) state refers to a slot/origin created previously
1968 * but already dropped. These states are supported for pg_upgrade. The
1969 * other states listed below are not supported:
1971 * a) SUBREL_STATE_DATASYNC: A relation upgraded while in this state
1972 * would retain a replication slot, which could not be dropped by the
1973 * sync worker spawned after the upgrade because the subscription ID
1974 * used for the slot name won't match anymore.
1976 * b) SUBREL_STATE_SYNCDONE: A relation upgraded while in this state
1977 * would retain the replication origin when there is a failure in
1978 * tablesync worker immediately after dropping the replication slot in
1979 * the publisher.
1981 * c) SUBREL_STATE_FINISHEDCOPY: A tablesync worker spawned to work on
1982 * a relation upgraded while in this state would expect an origin ID
1983 * with the OID of the subscription used before the upgrade, causing
1984 * it to fail.
1986 * d) SUBREL_STATE_SYNCWAIT, SUBREL_STATE_CATCHUP and
1987 * SUBREL_STATE_UNKNOWN: These states are not stored in the catalog,
1988 * so we need not allow these states.
1990 res = executeQueryOrDie(conn,
1991 "SELECT r.srsubstate, s.subname, n.nspname, c.relname "
1992 "FROM pg_catalog.pg_subscription_rel r "
1993 "LEFT JOIN pg_catalog.pg_subscription s"
1994 " ON r.srsubid = s.oid "
1995 "LEFT JOIN pg_catalog.pg_class c"
1996 " ON r.srrelid = c.oid "
1997 "LEFT JOIN pg_catalog.pg_namespace n"
1998 " ON c.relnamespace = n.oid "
1999 "WHERE r.srsubstate NOT IN ('i', 'r') "
2000 "ORDER BY s.subname");
2002 ntup = PQntuples(res);
2003 for (int i = 0; i < ntup; i++)
2005 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
2006 pg_fatal("could not open file \"%s\": %m", output_path);
2008 fprintf(script, "The table sync state \"%s\" is not allowed for database:\"%s\" subscription:\"%s\" schema:\"%s\" relation:\"%s\"\n",
2009 PQgetvalue(res, i, 0),
2010 active_db->db_name,
2011 PQgetvalue(res, i, 1),
2012 PQgetvalue(res, i, 2),
2013 PQgetvalue(res, i, 3));
2016 PQclear(res);
2017 PQfinish(conn);
2020 if (script)
2022 fclose(script);
2023 pg_log(PG_REPORT, "fatal");
2024 pg_fatal("Your installation contains subscriptions without origin or having relations not in i (initialize) or r (ready) state.\n"
2025 "You can allow the initial sync to finish for all relations and then restart the upgrade.\n"
2026 "A list of the problematic subscriptions is in the file:\n"
2027 " %s", output_path);
2029 else
2030 check_ok();