Line data Source code
1 : /*
2 : * check.c
3 : *
4 : * server checks and output routines
5 : *
6 : * Copyright (c) 2010-2025, PostgreSQL Global Development Group
7 : * src/bin/pg_upgrade/check.c
8 : */
9 :
10 : #include "postgres_fe.h"
11 :
12 : #include "catalog/pg_authid_d.h"
13 : #include "catalog/pg_class_d.h"
14 : #include "fe_utils/string_utils.h"
15 : #include "pg_upgrade.h"
16 : #include "common/unicode_version.h"
17 :
18 : static void check_new_cluster_is_empty(void);
19 : static void check_is_install_user(ClusterInfo *cluster);
20 : static void check_for_connection_status(ClusterInfo *cluster);
21 : static void check_for_prepared_transactions(ClusterInfo *cluster);
22 : static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
23 : static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
24 : static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
25 : static void check_for_tables_with_oids(ClusterInfo *cluster);
26 : static void check_for_not_null_inheritance(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_for_unicode_update(ClusterInfo *cluster);
31 : static void check_new_cluster_logical_replication_slots(void);
32 : static void check_new_cluster_subscription_configuration(void);
33 : static void check_old_cluster_for_valid_slots(void);
34 : static void check_old_cluster_subscription_state(void);
35 :
36 : /*
37 : * DataTypesUsageChecks - definitions of data type checks for the old cluster
38 : * in order to determine if an upgrade can be performed. See the comment on
39 : * data_types_usage_checks below for a more detailed description.
40 : */
41 : typedef struct
42 : {
43 : /* Status line to print to the user */
44 : const char *status;
45 : /* Filename to store report to */
46 : const char *report_filename;
47 : /* Query to extract the oid of the datatype */
48 : const char *base_query;
49 : /* Text to store to report in case of error */
50 : const char *report_text;
51 : /* The latest version where the check applies */
52 : int threshold_version;
53 : /* A function pointer for determining if the check applies */
54 : DataTypesUsageVersionCheck version_hook;
55 : } DataTypesUsageChecks;
56 :
57 : /*
58 : * Special values for threshold_version for indicating that a check applies to
59 : * all versions, or that a custom function needs to be invoked to determine
60 : * if the check applies.
61 : */
62 : #define MANUAL_CHECK 1
63 : #define ALL_VERSIONS -1
64 :
65 : /*--
66 : * Data type usage checks. Each check for problematic data type usage is
67 : * defined in this array with metadata, SQL query for finding the data type
68 : * and functionality for deciding if the check is applicable to the version
69 : * of the old cluster. The struct members are described in detail below:
70 : *
71 : * status A oneline string which can be printed to the user to
72 : * inform about progress. Should not end with newline.
73 : * report_filename The filename in which the list of problems detected by
74 : * the check will be printed.
75 : * base_query A query which extracts the Oid of the datatype checked
76 : * for.
77 : * report_text The text which will be printed to the user to explain
78 : * what the check did, and why it failed. The text should
79 : * end with a newline, and does not need to refer to the
80 : * report_filename as that is automatically appended to
81 : * the report with the path to the log folder.
82 : * threshold_version The major version of PostgreSQL for which to run the
83 : * check. Iff the old cluster is less than, or equal to,
84 : * the threshold version then the check will be executed.
85 : * If the old version is greater than the threshold then
86 : * the check is skipped. If the threshold_version is set
87 : * to ALL_VERSIONS then it will be run unconditionally,
88 : * if set to MANUAL_CHECK then the version_hook function
89 : * will be executed in order to determine whether or not
90 : * to run.
91 : * version_hook A function pointer to a version check function of type
92 : * DataTypesUsageVersionCheck which is used to determine
93 : * if the check is applicable to the old cluster. If the
94 : * version_hook returns true then the check will be run,
95 : * else it will be skipped. The function will only be
96 : * executed iff threshold_version is set to MANUAL_CHECK.
97 : */
98 : static DataTypesUsageChecks data_types_usage_checks[] =
99 : {
100 : /*
101 : * Look for composite types that were made during initdb *or* belong to
102 : * information_schema; that's important in case information_schema was
103 : * dropped and reloaded.
104 : *
105 : * The cutoff OID here should match the source cluster's value of
106 : * FirstNormalObjectId. We hardcode it rather than using that C #define
107 : * because, if that #define is ever changed, our own version's value is
108 : * NOT what to use. Eventually we may need a test on the source cluster's
109 : * version to select the correct value.
110 : */
111 : {
112 : .status = gettext_noop("Checking for system-defined composite types in user tables"),
113 : .report_filename = "tables_using_composite.txt",
114 : .base_query =
115 : "SELECT t.oid FROM pg_catalog.pg_type t "
116 : "LEFT JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid "
117 : " WHERE typtype = 'c' AND (t.oid < 16384 OR nspname = 'information_schema')",
118 : .report_text =
119 : gettext_noop("Your installation contains system-defined composite types in user tables.\n"
120 : "These type OIDs are not stable across PostgreSQL versions,\n"
121 : "so this cluster cannot currently be upgraded. You can drop the\n"
122 : "problem columns and restart the upgrade.\n"),
123 : .threshold_version = ALL_VERSIONS
124 : },
125 :
126 : /*
127 : * 9.3 -> 9.4 Fully implement the 'line' data type in 9.4, which
128 : * previously returned "not enabled" by default and was only functionally
129 : * enabled with a compile-time switch; as of 9.4 "line" has a different
130 : * on-disk representation format.
131 : */
132 : {
133 : .status = gettext_noop("Checking for incompatible \"line\" data type"),
134 : .report_filename = "tables_using_line.txt",
135 : .base_query =
136 : "SELECT 'pg_catalog.line'::pg_catalog.regtype AS oid",
137 : .report_text =
138 : gettext_noop("Your installation contains the \"line\" data type in user tables.\n"
139 : "This data type changed its internal and input/output format\n"
140 : "between your old and new versions so this\n"
141 : "cluster cannot currently be upgraded. You can\n"
142 : "drop the problem columns and restart the upgrade.\n"),
143 : .threshold_version = 903
144 : },
145 :
146 : /*
147 : * pg_upgrade only preserves these system values: pg_class.oid pg_type.oid
148 : * pg_enum.oid
149 : *
150 : * Many of the reg* data types reference system catalog info that is not
151 : * preserved, and hence these data types cannot be used in user tables
152 : * upgraded by pg_upgrade.
153 : */
154 : {
155 : .status = gettext_noop("Checking for reg* data types in user tables"),
156 : .report_filename = "tables_using_reg.txt",
157 :
158 : /*
159 : * Note: older servers will not have all of these reg* types, so we
160 : * have to write the query like this rather than depending on casts to
161 : * regtype.
162 : */
163 : .base_query =
164 : "SELECT oid FROM pg_catalog.pg_type t "
165 : "WHERE t.typnamespace = "
166 : " (SELECT oid FROM pg_catalog.pg_namespace "
167 : " WHERE nspname = 'pg_catalog') "
168 : " AND t.typname IN ( "
169 : /* pg_class.oid is preserved, so 'regclass' is OK */
170 : " 'regcollation', "
171 : " 'regconfig', "
172 : /* pg_database.oid is preserved, so 'regdatabase' is OK */
173 : " 'regdictionary', "
174 : " 'regnamespace', "
175 : " 'regoper', "
176 : " 'regoperator', "
177 : " 'regproc', "
178 : " 'regprocedure' "
179 : /* pg_authid.oid is preserved, so 'regrole' is OK */
180 : /* pg_type.oid is (mostly) preserved, so 'regtype' is OK */
181 : " )",
182 : .report_text =
183 : gettext_noop("Your installation contains one of the reg* data types in user tables.\n"
184 : "These data types reference system OIDs that are not preserved by\n"
185 : "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
186 : "drop the problem columns and restart the upgrade.\n"),
187 : .threshold_version = ALL_VERSIONS
188 : },
189 :
190 : /*
191 : * PG 16 increased the size of the 'aclitem' type, which breaks the
192 : * on-disk format for existing data.
193 : */
194 : {
195 : .status = gettext_noop("Checking for incompatible \"aclitem\" data type"),
196 : .report_filename = "tables_using_aclitem.txt",
197 : .base_query =
198 : "SELECT 'pg_catalog.aclitem'::pg_catalog.regtype AS oid",
199 : .report_text =
200 : gettext_noop("Your installation contains the \"aclitem\" data type in user tables.\n"
201 : "The internal format of \"aclitem\" changed in PostgreSQL version 16\n"
202 : "so this cluster cannot currently be upgraded. You can drop the\n"
203 : "problem columns and restart the upgrade.\n"),
204 : .threshold_version = 1500
205 : },
206 :
207 : /*
208 : * It's no longer allowed to create tables or views with "unknown"-type
209 : * columns. We do not complain about views with such columns, because
210 : * they should get silently converted to "text" columns during the DDL
211 : * dump and reload; it seems unlikely to be worth making users do that by
212 : * hand. However, if there's a table with such a column, the DDL reload
213 : * will fail, so we should pre-detect that rather than failing
214 : * mid-upgrade. Worse, if there's a matview with such a column, the DDL
215 : * reload will silently change it to "text" which won't match the on-disk
216 : * storage (which is like "cstring"). So we *must* reject that.
217 : */
218 : {
219 : .status = gettext_noop("Checking for invalid \"unknown\" user columns"),
220 : .report_filename = "tables_using_unknown.txt",
221 : .base_query =
222 : "SELECT 'pg_catalog.unknown'::pg_catalog.regtype AS oid",
223 : .report_text =
224 : gettext_noop("Your installation contains the \"unknown\" data type in user tables.\n"
225 : "This data type is no longer allowed in tables, so this cluster\n"
226 : "cannot currently be upgraded. You can drop the problem columns\n"
227 : "and restart the upgrade.\n"),
228 : .threshold_version = 906
229 : },
230 :
231 : /*
232 : * PG 12 changed the 'sql_identifier' type storage to be based on name,
233 : * not varchar, which breaks on-disk format for existing data. So we need
234 : * to prevent upgrade when used in user objects (tables, indexes, ...). In
235 : * 12, the sql_identifier data type was switched from name to varchar,
236 : * which does affect the storage (name is by-ref, but not varlena). This
237 : * means user tables using sql_identifier for columns are broken because
238 : * the on-disk format is different.
239 : */
240 : {
241 : .status = gettext_noop("Checking for invalid \"sql_identifier\" user columns"),
242 : .report_filename = "tables_using_sql_identifier.txt",
243 : .base_query =
244 : "SELECT 'information_schema.sql_identifier'::pg_catalog.regtype AS oid",
245 : .report_text =
246 : gettext_noop("Your installation contains the \"sql_identifier\" data type in user tables.\n"
247 : "The on-disk format for this data type has changed, so this\n"
248 : "cluster cannot currently be upgraded. You can drop the problem\n"
249 : "columns and restart the upgrade.\n"),
250 : .threshold_version = 1100
251 : },
252 :
253 : /*
254 : * JSONB changed its storage format during 9.4 beta, so check for it.
255 : */
256 : {
257 : .status = gettext_noop("Checking for incompatible \"jsonb\" data type in user tables"),
258 : .report_filename = "tables_using_jsonb.txt",
259 : .base_query =
260 : "SELECT 'pg_catalog.jsonb'::pg_catalog.regtype AS oid",
261 : .report_text =
262 : gettext_noop("Your installation contains the \"jsonb\" data type in user tables.\n"
263 : "The internal format of \"jsonb\" changed during 9.4 beta so this\n"
264 : "cluster cannot currently be upgraded. You can drop the problem \n"
265 : "columns and restart the upgrade.\n"),
266 : .threshold_version = MANUAL_CHECK,
267 : .version_hook = jsonb_9_4_check_applicable
268 : },
269 :
270 : /*
271 : * PG 12 removed types abstime, reltime, tinterval.
272 : */
273 : {
274 : .status = gettext_noop("Checking for removed \"abstime\" data type in user tables"),
275 : .report_filename = "tables_using_abstime.txt",
276 : .base_query =
277 : "SELECT 'pg_catalog.abstime'::pg_catalog.regtype AS oid",
278 : .report_text =
279 : gettext_noop("Your installation contains the \"abstime\" data type in user tables.\n"
280 : "The \"abstime\" type has been removed in PostgreSQL version 12,\n"
281 : "so this cluster cannot currently be upgraded. You can drop the\n"
282 : "problem columns, or change them to another data type, and restart\n"
283 : "the upgrade.\n"),
284 : .threshold_version = 1100
285 : },
286 : {
287 : .status = gettext_noop("Checking for removed \"reltime\" data type in user tables"),
288 : .report_filename = "tables_using_reltime.txt",
289 : .base_query =
290 : "SELECT 'pg_catalog.reltime'::pg_catalog.regtype AS oid",
291 : .report_text =
292 : gettext_noop("Your installation contains the \"reltime\" data type in user tables.\n"
293 : "The \"reltime\" type has been removed in PostgreSQL version 12,\n"
294 : "so this cluster cannot currently be upgraded. You can drop the\n"
295 : "problem columns, or change them to another data type, and restart\n"
296 : "the upgrade.\n"),
297 : .threshold_version = 1100
298 : },
299 : {
300 : .status = gettext_noop("Checking for removed \"tinterval\" data type in user tables"),
301 : .report_filename = "tables_using_tinterval.txt",
302 : .base_query =
303 : "SELECT 'pg_catalog.tinterval'::pg_catalog.regtype AS oid",
304 : .report_text =
305 : gettext_noop("Your installation contains the \"tinterval\" data type in user tables.\n"
306 : "The \"tinterval\" type has been removed in PostgreSQL version 12,\n"
307 : "so this cluster cannot currently be upgraded. You can drop the\n"
308 : "problem columns, or change them to another data type, and restart\n"
309 : "the upgrade.\n"),
310 : .threshold_version = 1100
311 : },
312 :
313 : /* End of checks marker, must remain last */
314 : {
315 : NULL, NULL, NULL, NULL, 0, NULL
316 : }
317 : };
318 :
319 : /*
320 : * Private state for check_for_data_types_usage()'s UpgradeTask.
321 : */
322 : struct data_type_check_state
323 : {
324 : DataTypesUsageChecks *check; /* the check for this step */
325 : bool result; /* true if check failed for any database */
326 : PQExpBuffer *report; /* buffer for report on failed checks */
327 : };
328 :
329 : /*
330 : * Returns a palloc'd query string for the data type check, for use by
331 : * check_for_data_types_usage()'s UpgradeTask.
332 : */
333 : static char *
334 48 : data_type_check_query(int checknum)
335 : {
336 48 : DataTypesUsageChecks *check = &data_types_usage_checks[checknum];
337 :
338 48 : return psprintf("WITH RECURSIVE oids AS ( "
339 : /* start with the type(s) returned by base_query */
340 : " %s "
341 : " UNION ALL "
342 : " SELECT * FROM ( "
343 : /* inner WITH because we can only reference the CTE once */
344 : " WITH x AS (SELECT oid FROM oids) "
345 : /* domains on any type selected so far */
346 : " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
347 : " UNION ALL "
348 : /* arrays over any type selected so far */
349 : " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typelem = x.oid AND typtype = 'b' "
350 : " UNION ALL "
351 : /* composite types containing any type selected so far */
352 : " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
353 : " WHERE t.typtype = 'c' AND "
354 : " t.oid = c.reltype AND "
355 : " c.oid = a.attrelid AND "
356 : " NOT a.attisdropped AND "
357 : " a.atttypid = x.oid "
358 : " UNION ALL "
359 : /* ranges containing any type selected so far */
360 : " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_range r, x "
361 : " WHERE t.typtype = 'r' AND r.rngtypid = t.oid AND r.rngsubtype = x.oid"
362 : " ) foo "
363 : ") "
364 : /* now look for stored columns of any such type */
365 : "SELECT n.nspname, c.relname, a.attname "
366 : "FROM pg_catalog.pg_class c, "
367 : " pg_catalog.pg_namespace n, "
368 : " pg_catalog.pg_attribute a "
369 : "WHERE c.oid = a.attrelid AND "
370 : " NOT a.attisdropped AND "
371 : " a.atttypid IN (SELECT oid FROM oids) AND "
372 : " c.relkind IN ("
373 : CppAsString2(RELKIND_RELATION) ", "
374 : CppAsString2(RELKIND_MATVIEW) ", "
375 : CppAsString2(RELKIND_INDEX) ") AND "
376 : " c.relnamespace = n.oid AND "
377 : /* exclude possible orphaned temp tables */
378 : " n.nspname !~ '^pg_temp_' AND "
379 : " n.nspname !~ '^pg_toast_temp_' AND "
380 : /* exclude system catalogs, too */
381 : " n.nspname NOT IN ('pg_catalog', 'information_schema')",
382 : check->base_query);
383 : }
384 :
385 : /*
386 : * Callback function for processing results of queries for
387 : * check_for_data_types_usage()'s UpgradeTask. If the query returned any rows
388 : * (i.e., the check failed), write the details to the report file.
389 : */
390 : static void
391 148 : process_data_type_check(DbInfo *dbinfo, PGresult *res, void *arg)
392 : {
393 148 : struct data_type_check_state *state = (struct data_type_check_state *) arg;
394 148 : int ntups = PQntuples(res);
395 : char output_path[MAXPGPATH];
396 148 : int i_nspname = PQfnumber(res, "nspname");
397 148 : int i_relname = PQfnumber(res, "relname");
398 148 : int i_attname = PQfnumber(res, "attname");
399 148 : FILE *script = NULL;
400 :
401 : AssertVariableIsOfType(&process_data_type_check, UpgradeTaskProcessCB);
402 :
403 148 : if (ntups == 0)
404 148 : return;
405 :
406 0 : snprintf(output_path, sizeof(output_path), "%s/%s",
407 : log_opts.basedir,
408 0 : state->check->report_filename);
409 :
410 : /*
411 : * Make sure we have a buffer to save reports to now that we found a first
412 : * failing check.
413 : */
414 0 : if (*state->report == NULL)
415 0 : *state->report = createPQExpBuffer();
416 :
417 : /*
418 : * If this is the first time we see an error for the check in question
419 : * then print a status message of the failure.
420 : */
421 0 : if (!state->result)
422 : {
423 0 : pg_log(PG_REPORT, "failed check: %s", _(state->check->status));
424 0 : appendPQExpBuffer(*state->report, "\n%s\n%s\n %s\n",
425 0 : _(state->check->report_text),
426 : _("A list of the problem columns is in the file:"),
427 : output_path);
428 : }
429 0 : state->result = true;
430 :
431 0 : if ((script = fopen_priv(output_path, "a")) == NULL)
432 0 : pg_fatal("could not open file \"%s\": %m", output_path);
433 :
434 0 : fprintf(script, "In database: %s\n", dbinfo->db_name);
435 :
436 0 : for (int rowno = 0; rowno < ntups; rowno++)
437 0 : fprintf(script, " %s.%s.%s\n",
438 : PQgetvalue(res, rowno, i_nspname),
439 : PQgetvalue(res, rowno, i_relname),
440 : PQgetvalue(res, rowno, i_attname));
441 :
442 0 : fclose(script);
443 : }
444 :
445 : /*
446 : * check_for_data_types_usage()
447 : * Detect whether there are any stored columns depending on given type(s)
448 : *
449 : * If so, write a report to the given file name and signal a failure to the
450 : * user.
451 : *
452 : * The checks to run are defined in a DataTypesUsageChecks structure where
453 : * each check has a metadata for explaining errors to the user, a base_query,
454 : * a report filename and a function pointer hook for validating if the check
455 : * should be executed given the cluster at hand.
456 : *
457 : * base_query should be a SELECT yielding a single column named "oid",
458 : * containing the pg_type OIDs of one or more types that are known to have
459 : * inconsistent on-disk representations across server versions.
460 : *
461 : * We check for the type(s) in tables, matviews, and indexes, but not views;
462 : * there's no storage involved in a view.
463 : */
464 : static void
465 24 : check_for_data_types_usage(ClusterInfo *cluster)
466 : {
467 24 : PQExpBuffer report = NULL;
468 24 : DataTypesUsageChecks *tmp = data_types_usage_checks;
469 24 : int n_data_types_usage_checks = 0;
470 24 : UpgradeTask *task = upgrade_task_create();
471 24 : char **queries = NULL;
472 : struct data_type_check_state *states;
473 :
474 24 : prep_status("Checking data type usage");
475 :
476 : /* Gather number of checks to perform */
477 264 : while (tmp->status != NULL)
478 : {
479 240 : n_data_types_usage_checks++;
480 240 : tmp++;
481 : }
482 :
483 : /* Allocate memory for queries and for task states */
484 24 : queries = pg_malloc0(sizeof(char *) * n_data_types_usage_checks);
485 24 : states = pg_malloc0(sizeof(struct data_type_check_state) * n_data_types_usage_checks);
486 :
487 264 : for (int i = 0; i < n_data_types_usage_checks; i++)
488 : {
489 240 : DataTypesUsageChecks *check = &data_types_usage_checks[i];
490 :
491 240 : if (check->threshold_version == MANUAL_CHECK)
492 : {
493 : Assert(check->version_hook);
494 :
495 : /*
496 : * Make sure that the check applies to the current cluster version
497 : * and skip it if not.
498 : */
499 24 : if (!check->version_hook(cluster))
500 24 : continue;
501 : }
502 216 : else if (check->threshold_version != ALL_VERSIONS)
503 : {
504 168 : if (GET_MAJOR_VERSION(cluster->major_version) > check->threshold_version)
505 168 : continue;
506 : }
507 : else
508 : Assert(check->threshold_version == ALL_VERSIONS);
509 :
510 48 : queries[i] = data_type_check_query(i);
511 :
512 48 : states[i].check = check;
513 48 : states[i].report = &report;
514 :
515 48 : upgrade_task_add_step(task, queries[i], process_data_type_check,
516 48 : true, &states[i]);
517 : }
518 :
519 : /*
520 : * Connect to each database in the cluster and run all defined checks
521 : * against that database before trying the next one.
522 : */
523 24 : upgrade_task_run(task, cluster);
524 24 : upgrade_task_free(task);
525 :
526 24 : if (report)
527 : {
528 0 : pg_fatal("Data type checks failed: %s", report->data);
529 : destroyPQExpBuffer(report);
530 : }
531 :
532 264 : for (int i = 0; i < n_data_types_usage_checks; i++)
533 : {
534 240 : if (queries[i])
535 48 : pg_free(queries[i]);
536 : }
537 24 : pg_free(queries);
538 24 : pg_free(states);
539 :
540 24 : check_ok();
541 24 : }
542 :
543 : /*
544 : * fix_path_separator
545 : * For non-Windows, just return the argument.
546 : * For Windows convert any forward slash to a backslash
547 : * such as is suitable for arguments to builtin commands
548 : * like RMDIR and DEL.
549 : */
550 : static char *
551 32 : fix_path_separator(char *path)
552 : {
553 : #ifdef WIN32
554 :
555 : char *result;
556 : char *c;
557 :
558 : result = pg_strdup(path);
559 :
560 : for (c = result; *c != '\0'; c++)
561 : if (*c == '/')
562 : *c = '\\';
563 :
564 : return result;
565 : #else
566 :
567 32 : return path;
568 : #endif
569 : }
570 :
571 : void
572 32 : output_check_banner(void)
573 : {
574 32 : if (user_opts.live_check)
575 : {
576 0 : pg_log(PG_REPORT,
577 : "Performing Consistency Checks on Old Live Server\n"
578 : "------------------------------------------------");
579 : }
580 : else
581 : {
582 32 : pg_log(PG_REPORT,
583 : "Performing Consistency Checks\n"
584 : "-----------------------------");
585 : }
586 32 : }
587 :
588 :
589 : void
590 30 : check_and_dump_old_cluster(void)
591 : {
592 : /* -- OLD -- */
593 :
594 30 : if (!user_opts.live_check)
595 30 : start_postmaster(&old_cluster, true);
596 :
597 : /*
598 : * First check that all databases allow connections since we'll otherwise
599 : * fail in later stages.
600 : */
601 30 : check_for_connection_status(&old_cluster);
602 :
603 : /*
604 : * Extract a list of databases, tables, and logical replication slots from
605 : * the old cluster.
606 : */
607 28 : get_db_rel_and_slot_infos(&old_cluster);
608 :
609 28 : init_tablespaces();
610 :
611 28 : get_loadable_libraries();
612 :
613 :
614 : /*
615 : * Check for various failure cases
616 : */
617 28 : check_is_install_user(&old_cluster);
618 28 : check_for_prepared_transactions(&old_cluster);
619 28 : check_for_isn_and_int8_passing_mismatch(&old_cluster);
620 :
621 28 : if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1700)
622 : {
623 : /*
624 : * Logical replication slots can be migrated since PG17. See comments
625 : * atop get_old_cluster_logical_slot_infos().
626 : */
627 28 : check_old_cluster_for_valid_slots();
628 :
629 : /*
630 : * Subscriptions and their dependencies can be migrated since PG17.
631 : * Before that the logical slots are not upgraded, so we will not be
632 : * able to upgrade the logical replication clusters completely.
633 : */
634 26 : get_subscription_count(&old_cluster);
635 26 : check_old_cluster_subscription_state();
636 : }
637 :
638 24 : check_for_data_types_usage(&old_cluster);
639 :
640 : /*
641 : * Unicode updates can affect some objects that use expressions with
642 : * functions dependent on Unicode.
643 : */
644 24 : check_for_unicode_update(&old_cluster);
645 :
646 : /*
647 : * PG 14 changed the function signature of encoding conversion functions.
648 : * Conversions from older versions cannot be upgraded automatically
649 : * because the user-defined functions used by the encoding conversions
650 : * need to be changed to match the new signature.
651 : */
652 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
653 0 : check_for_user_defined_encoding_conversions(&old_cluster);
654 :
655 : /*
656 : * Pre-PG 14 allowed user defined postfix operators, which are not
657 : * supported anymore. Verify there are none, iff applicable.
658 : */
659 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
660 0 : check_for_user_defined_postfix_ops(&old_cluster);
661 :
662 : /*
663 : * PG 14 changed polymorphic functions from anyarray to
664 : * anycompatiblearray.
665 : */
666 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
667 0 : check_for_incompatible_polymorphics(&old_cluster);
668 :
669 : /*
670 : * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
671 : * supported anymore. Verify there are none, iff applicable.
672 : */
673 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
674 0 : check_for_tables_with_oids(&old_cluster);
675 :
676 : /*
677 : * Pre-PG 18 allowed child tables to omit not-null constraints that their
678 : * parents columns have, but schema restore fails for them. Verify there
679 : * are none, iff applicable.
680 : */
681 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1800)
682 0 : check_for_not_null_inheritance(&old_cluster);
683 :
684 : /*
685 : * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
686 : * hash indexes
687 : */
688 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
689 : {
690 0 : if (user_opts.check)
691 0 : old_9_6_invalidate_hash_indexes(&old_cluster, true);
692 : }
693 :
694 : /* 9.5 and below should not have roles starting with pg_ */
695 24 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
696 0 : check_for_pg_role_prefix(&old_cluster);
697 :
698 : /*
699 : * While not a check option, we do this now because this is the only time
700 : * the old server is running.
701 : */
702 24 : if (!user_opts.check)
703 20 : generate_old_dump();
704 :
705 24 : if (!user_opts.live_check)
706 24 : stop_postmaster(false);
707 24 : }
708 :
709 :
710 : void
711 24 : check_new_cluster(void)
712 : {
713 24 : get_db_rel_and_slot_infos(&new_cluster);
714 :
715 24 : check_new_cluster_is_empty();
716 :
717 24 : check_loadable_libraries();
718 :
719 24 : switch (user_opts.transfer_mode)
720 : {
721 2 : case TRANSFER_MODE_CLONE:
722 2 : check_file_clone();
723 0 : break;
724 16 : case TRANSFER_MODE_COPY:
725 16 : break;
726 2 : case TRANSFER_MODE_COPY_FILE_RANGE:
727 2 : check_copy_file_range();
728 2 : break;
729 2 : case TRANSFER_MODE_LINK:
730 2 : check_hard_link(TRANSFER_MODE_LINK);
731 2 : break;
732 2 : case TRANSFER_MODE_SWAP:
733 :
734 : /*
735 : * We do the hard link check for --swap, too, since it's an easy
736 : * way to verify the clusters are in the same file system. This
737 : * allows us to take some shortcuts in the file synchronization
738 : * step. With some more effort, we could probably support the
739 : * separate-file-system use case, but this mode is unlikely to
740 : * offer much benefit if we have to copy the files across file
741 : * system boundaries.
742 : */
743 2 : check_hard_link(TRANSFER_MODE_SWAP);
744 :
745 : /*
746 : * There are a few known issues with using --swap to upgrade from
747 : * versions older than 10. For example, the sequence tuple format
748 : * changed in v10, and the visibility map format changed in 9.6.
749 : * While such problems are not insurmountable (and we may have to
750 : * deal with similar problems in the future, anyway), it doesn't
751 : * seem worth the effort to support swap mode for upgrades from
752 : * long-unsupported versions.
753 : */
754 2 : if (GET_MAJOR_VERSION(old_cluster.major_version) < 1000)
755 0 : pg_fatal("Swap mode can only upgrade clusters from PostgreSQL version %s and later.",
756 : "10");
757 :
758 2 : break;
759 : }
760 :
761 22 : check_is_install_user(&new_cluster);
762 :
763 22 : check_for_prepared_transactions(&new_cluster);
764 :
765 22 : check_for_new_tablespace_dir();
766 :
767 22 : check_new_cluster_logical_replication_slots();
768 :
769 20 : check_new_cluster_subscription_configuration();
770 18 : }
771 :
772 :
773 : void
774 18 : report_clusters_compatible(void)
775 : {
776 18 : if (user_opts.check)
777 : {
778 2 : pg_log(PG_REPORT, "\n*Clusters are compatible*");
779 : /* stops new cluster */
780 2 : stop_postmaster(false);
781 :
782 2 : cleanup_output_dirs();
783 2 : exit(0);
784 : }
785 :
786 16 : pg_log(PG_REPORT, "\n"
787 : "If pg_upgrade fails after this point, you must re-initdb the\n"
788 : "new cluster before continuing.");
789 16 : }
790 :
791 :
792 : void
793 16 : issue_warnings_and_set_wal_level(void)
794 : {
795 : /*
796 : * We unconditionally start/stop the new server because pg_resetwal -o set
797 : * wal_level to 'minimum'. If the user is upgrading standby servers using
798 : * the rsync instructions, they will need pg_upgrade to write its final
799 : * WAL record showing wal_level as 'replica'.
800 : */
801 16 : start_postmaster(&new_cluster, true);
802 :
803 : /* Reindex hash indexes for old < 10.0 */
804 16 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
805 0 : old_9_6_invalidate_hash_indexes(&new_cluster, false);
806 :
807 16 : report_extension_updates(&new_cluster);
808 :
809 16 : stop_postmaster(false);
810 16 : }
811 :
812 :
813 : void
814 16 : output_completion_banner(char *deletion_script_file_name)
815 : {
816 : PQExpBufferData user_specification;
817 :
818 16 : initPQExpBuffer(&user_specification);
819 16 : if (os_info.user_specified)
820 : {
821 0 : appendPQExpBufferStr(&user_specification, "-U ");
822 0 : appendShellString(&user_specification, os_info.user);
823 0 : appendPQExpBufferChar(&user_specification, ' ');
824 : }
825 :
826 16 : pg_log(PG_REPORT,
827 : "Some statistics are not transferred by pg_upgrade.\n"
828 : "Once you start the new server, consider running these two commands:\n"
829 : " %s/vacuumdb %s--all --analyze-in-stages --missing-stats-only\n"
830 : " %s/vacuumdb %s--all --analyze-only",
831 : new_cluster.bindir, user_specification.data,
832 : new_cluster.bindir, user_specification.data);
833 :
834 16 : if (deletion_script_file_name)
835 16 : pg_log(PG_REPORT,
836 : "Running this script will delete the old cluster's data files:\n"
837 : " %s",
838 : deletion_script_file_name);
839 : else
840 0 : pg_log(PG_REPORT,
841 : "Could not create a script to delete the old cluster's data files\n"
842 : "because user-defined tablespaces or the new cluster's data directory\n"
843 : "exist in the old cluster directory. The old cluster's contents must\n"
844 : "be deleted manually.");
845 :
846 16 : termPQExpBuffer(&user_specification);
847 16 : }
848 :
849 :
850 : void
851 32 : check_cluster_versions(void)
852 : {
853 32 : prep_status("Checking cluster versions");
854 :
855 : /* cluster versions should already have been obtained */
856 : Assert(old_cluster.major_version != 0);
857 : Assert(new_cluster.major_version != 0);
858 :
859 : /*
860 : * We allow upgrades from/to the same major version for alpha/beta
861 : * upgrades
862 : */
863 :
864 32 : if (GET_MAJOR_VERSION(old_cluster.major_version) < 902)
865 0 : pg_fatal("This utility can only upgrade from PostgreSQL version %s and later.",
866 : "9.2");
867 :
868 : /* Only current PG version is supported as a target */
869 32 : if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
870 0 : pg_fatal("This utility can only upgrade to PostgreSQL version %s.",
871 : PG_MAJORVERSION);
872 :
873 : /*
874 : * We can't allow downgrading because we use the target pg_dump, and
875 : * pg_dump cannot operate on newer database versions, only current and
876 : * older versions.
877 : */
878 32 : if (old_cluster.major_version > new_cluster.major_version)
879 0 : pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.");
880 :
881 : /* Ensure binaries match the designated data directories */
882 32 : if (GET_MAJOR_VERSION(old_cluster.major_version) !=
883 32 : GET_MAJOR_VERSION(old_cluster.bin_version))
884 0 : pg_fatal("Old cluster data and binary directories are from different major versions.");
885 32 : if (GET_MAJOR_VERSION(new_cluster.major_version) !=
886 32 : GET_MAJOR_VERSION(new_cluster.bin_version))
887 0 : pg_fatal("New cluster data and binary directories are from different major versions.");
888 :
889 : /*
890 : * Since from version 18, newly created database clusters always have
891 : * 'signed' default char-signedness, it makes less sense to use
892 : * --set-char-signedness option for upgrading from version 18 or later.
893 : * Users who want to change the default char signedness of the new
894 : * cluster, they can use pg_resetwal manually before the upgrade.
895 : */
896 32 : if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1800 &&
897 32 : user_opts.char_signedness != -1)
898 2 : pg_fatal("The option %s cannot be used for upgrades from PostgreSQL %s and later.",
899 : "--set-char-signedness", "18");
900 :
901 30 : check_ok();
902 30 : }
903 :
904 :
905 : void
906 30 : check_cluster_compatibility(void)
907 : {
908 : /* get/check pg_control data of servers */
909 30 : get_control_data(&old_cluster);
910 30 : get_control_data(&new_cluster);
911 30 : check_control_data(&old_cluster.controldata, &new_cluster.controldata);
912 :
913 30 : if (user_opts.live_check && old_cluster.port == new_cluster.port)
914 0 : pg_fatal("When checking a live server, "
915 : "the old and new port numbers must be different.");
916 30 : }
917 :
918 :
919 : static void
920 24 : check_new_cluster_is_empty(void)
921 : {
922 : int dbnum;
923 :
924 72 : for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
925 : {
926 : int relnum;
927 48 : RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
928 :
929 144 : for (relnum = 0; relnum < rel_arr->nrels;
930 96 : relnum++)
931 : {
932 : /* pg_largeobject and its index should be skipped */
933 96 : if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
934 0 : pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
935 0 : new_cluster.dbarr.dbs[dbnum].db_name,
936 0 : rel_arr->rels[relnum].nspname,
937 0 : rel_arr->rels[relnum].relname);
938 : }
939 : }
940 24 : }
941 :
942 : /*
943 : * A previous run of pg_upgrade might have failed and the new cluster
944 : * directory recreated, but they might have forgotten to remove
945 : * the new cluster's tablespace directories. Therefore, check that
946 : * new cluster tablespace directories do not already exist. If
947 : * they do, it would cause an error while restoring global objects.
948 : * This allows the failure to be detected at check time, rather than
949 : * during schema restore.
950 : */
951 : static void
952 22 : check_for_new_tablespace_dir(void)
953 : {
954 : int tblnum;
955 : char new_tablespace_dir[MAXPGPATH];
956 :
957 22 : prep_status("Checking for new cluster tablespace directories");
958 :
959 22 : for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
960 : {
961 : struct stat statbuf;
962 :
963 0 : snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
964 0 : os_info.old_tablespaces[tblnum],
965 : new_cluster.tablespace_suffix);
966 :
967 0 : if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
968 0 : pg_fatal("new cluster tablespace directory already exists: \"%s\"",
969 : new_tablespace_dir);
970 : }
971 :
972 22 : check_ok();
973 22 : }
974 :
975 : /*
976 : * create_script_for_old_cluster_deletion()
977 : *
978 : * This is particularly useful for tablespace deletion.
979 : */
980 : void
981 16 : create_script_for_old_cluster_deletion(char **deletion_script_file_name)
982 : {
983 16 : FILE *script = NULL;
984 : int tblnum;
985 : char old_cluster_pgdata[MAXPGPATH],
986 : new_cluster_pgdata[MAXPGPATH];
987 : char *old_tblspc_suffix;
988 :
989 16 : *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
990 : SCRIPT_PREFIX, SCRIPT_EXT);
991 :
992 16 : strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
993 16 : canonicalize_path(old_cluster_pgdata);
994 :
995 16 : strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
996 16 : canonicalize_path(new_cluster_pgdata);
997 :
998 : /* Some people put the new data directory inside the old one. */
999 16 : if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
1000 : {
1001 0 : pg_log(PG_WARNING,
1002 : "\nWARNING: new data directory should not be inside the old data directory, i.e. %s", old_cluster_pgdata);
1003 :
1004 : /* Unlink file in case it is left over from a previous run. */
1005 0 : unlink(*deletion_script_file_name);
1006 0 : pg_free(*deletion_script_file_name);
1007 0 : *deletion_script_file_name = NULL;
1008 0 : return;
1009 : }
1010 :
1011 : /*
1012 : * Some users (oddly) create tablespaces inside the cluster data
1013 : * directory. We can't create a proper old cluster delete script in that
1014 : * case.
1015 : */
1016 16 : for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
1017 : {
1018 : char old_tablespace_dir[MAXPGPATH];
1019 :
1020 0 : strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
1021 0 : canonicalize_path(old_tablespace_dir);
1022 0 : if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
1023 : {
1024 : /* reproduce warning from CREATE TABLESPACE that is in the log */
1025 0 : pg_log(PG_WARNING,
1026 : "\nWARNING: user-defined tablespace locations should not be inside the data directory, i.e. %s", old_tablespace_dir);
1027 :
1028 : /* Unlink file in case it is left over from a previous run. */
1029 0 : unlink(*deletion_script_file_name);
1030 0 : pg_free(*deletion_script_file_name);
1031 0 : *deletion_script_file_name = NULL;
1032 0 : return;
1033 : }
1034 : }
1035 :
1036 16 : prep_status("Creating script to delete old cluster");
1037 :
1038 16 : if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
1039 0 : pg_fatal("could not open file \"%s\": %m",
1040 : *deletion_script_file_name);
1041 :
1042 : #ifndef WIN32
1043 : /* add shebang header */
1044 16 : fprintf(script, "#!/bin/sh\n\n");
1045 : #endif
1046 :
1047 : /* delete old cluster's default tablespace */
1048 16 : fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
1049 : fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
1050 :
1051 : /* delete old cluster's alternate tablespaces */
1052 16 : old_tblspc_suffix = pg_strdup(old_cluster.tablespace_suffix);
1053 16 : fix_path_separator(old_tblspc_suffix);
1054 16 : for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
1055 0 : fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
1056 0 : fix_path_separator(os_info.old_tablespaces[tblnum]),
1057 : old_tblspc_suffix, PATH_QUOTE);
1058 16 : pfree(old_tblspc_suffix);
1059 :
1060 16 : fclose(script);
1061 :
1062 : #ifndef WIN32
1063 16 : if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
1064 0 : pg_fatal("could not add execute permission to file \"%s\": %m",
1065 : *deletion_script_file_name);
1066 : #endif
1067 :
1068 16 : check_ok();
1069 : }
1070 :
1071 :
1072 : /*
1073 : * check_is_install_user()
1074 : *
1075 : * Check we are the install user, and that the new cluster
1076 : * has no other users.
1077 : */
1078 : static void
1079 50 : check_is_install_user(ClusterInfo *cluster)
1080 : {
1081 : PGresult *res;
1082 50 : PGconn *conn = connectToServer(cluster, "template1");
1083 :
1084 50 : prep_status("Checking database user is the install user");
1085 :
1086 : /* Can't use pg_authid because only superusers can view it. */
1087 50 : res = executeQueryOrDie(conn,
1088 : "SELECT rolsuper, oid "
1089 : "FROM pg_catalog.pg_roles "
1090 : "WHERE rolname = current_user "
1091 : "AND rolname !~ '^pg_'");
1092 :
1093 : /*
1094 : * We only allow the install user in the new cluster (see comment below)
1095 : * and we preserve pg_authid.oid, so this must be the install user in the
1096 : * old cluster too.
1097 : */
1098 50 : if (PQntuples(res) != 1 ||
1099 50 : atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
1100 0 : pg_fatal("database user \"%s\" is not the install user",
1101 : os_info.user);
1102 :
1103 50 : PQclear(res);
1104 :
1105 50 : res = executeQueryOrDie(conn,
1106 : "SELECT COUNT(*) "
1107 : "FROM pg_catalog.pg_roles "
1108 : "WHERE rolname !~ '^pg_'");
1109 :
1110 50 : if (PQntuples(res) != 1)
1111 0 : pg_fatal("could not determine the number of users");
1112 :
1113 : /*
1114 : * We only allow the install user in the new cluster because other defined
1115 : * users might match users defined in the old cluster and generate an
1116 : * error during pg_dump restore.
1117 : */
1118 50 : if (cluster == &new_cluster && strcmp(PQgetvalue(res, 0, 0), "1") != 0)
1119 0 : pg_fatal("Only the install user can be defined in the new cluster.");
1120 :
1121 50 : PQclear(res);
1122 :
1123 50 : PQfinish(conn);
1124 :
1125 50 : check_ok();
1126 50 : }
1127 :
1128 :
1129 : /*
1130 : * check_for_connection_status
1131 : *
1132 : * Ensure that all non-template0 databases allow connections since they
1133 : * otherwise won't be restored; and that template0 explicitly doesn't allow
1134 : * connections since it would make pg_dumpall --globals restore fail.
1135 : */
1136 : static void
1137 30 : check_for_connection_status(ClusterInfo *cluster)
1138 : {
1139 : int dbnum;
1140 : PGconn *conn_template1;
1141 : PGresult *dbres;
1142 : int ntups;
1143 : int i_datname;
1144 : int i_datallowconn;
1145 : int i_datconnlimit;
1146 30 : FILE *script = NULL;
1147 : char output_path[MAXPGPATH];
1148 :
1149 30 : prep_status("Checking database connection settings");
1150 :
1151 30 : snprintf(output_path, sizeof(output_path), "%s/%s",
1152 : log_opts.basedir,
1153 : "databases_cannot_connect_to.txt");
1154 :
1155 30 : conn_template1 = connectToServer(cluster, "template1");
1156 :
1157 : /* get database names */
1158 30 : dbres = executeQueryOrDie(conn_template1,
1159 : "SELECT datname, datallowconn, datconnlimit "
1160 : "FROM pg_catalog.pg_database");
1161 :
1162 30 : i_datname = PQfnumber(dbres, "datname");
1163 30 : i_datallowconn = PQfnumber(dbres, "datallowconn");
1164 30 : i_datconnlimit = PQfnumber(dbres, "datconnlimit");
1165 :
1166 30 : ntups = PQntuples(dbres);
1167 156 : for (dbnum = 0; dbnum < ntups; dbnum++)
1168 : {
1169 126 : char *datname = PQgetvalue(dbres, dbnum, i_datname);
1170 126 : char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
1171 126 : char *datconnlimit = PQgetvalue(dbres, dbnum, i_datconnlimit);
1172 :
1173 126 : if (strcmp(datname, "template0") == 0)
1174 : {
1175 : /* avoid restore failure when pg_dumpall tries to create template0 */
1176 30 : if (strcmp(datallowconn, "t") == 0)
1177 0 : pg_fatal("template0 must not allow connections, "
1178 : "i.e. its pg_database.datallowconn must be false");
1179 : }
1180 : else
1181 : {
1182 : /*
1183 : * Avoid datallowconn == false databases from being skipped on
1184 : * restore, and ensure that no databases are marked invalid with
1185 : * datconnlimit == -2.
1186 : */
1187 96 : if ((strcmp(datallowconn, "f") == 0) || strcmp(datconnlimit, "-2") == 0)
1188 : {
1189 2 : if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1190 0 : pg_fatal("could not open file \"%s\": %m", output_path);
1191 :
1192 2 : fprintf(script, "%s\n", datname);
1193 : }
1194 : }
1195 : }
1196 :
1197 30 : PQclear(dbres);
1198 :
1199 30 : PQfinish(conn_template1);
1200 :
1201 30 : if (script)
1202 : {
1203 2 : fclose(script);
1204 2 : pg_log(PG_REPORT, "fatal");
1205 2 : pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
1206 : "pg_database.datallowconn must be true and pg_database.datconnlimit\n"
1207 : "must not be -2. Your installation contains non-template0 databases\n"
1208 : "which cannot be connected to. Consider allowing connection for all\n"
1209 : "non-template0 databases or drop the databases which do not allow\n"
1210 : "connections. A list of databases with the problem is in the file:\n"
1211 : " %s", output_path);
1212 : }
1213 : else
1214 28 : check_ok();
1215 28 : }
1216 :
1217 :
1218 : /*
1219 : * check_for_prepared_transactions()
1220 : *
1221 : * Make sure there are no prepared transactions because the storage format
1222 : * might have changed.
1223 : */
1224 : static void
1225 50 : check_for_prepared_transactions(ClusterInfo *cluster)
1226 : {
1227 : PGresult *res;
1228 50 : PGconn *conn = connectToServer(cluster, "template1");
1229 :
1230 50 : prep_status("Checking for prepared transactions");
1231 :
1232 50 : res = executeQueryOrDie(conn,
1233 : "SELECT * "
1234 : "FROM pg_catalog.pg_prepared_xacts");
1235 :
1236 50 : if (PQntuples(res) != 0)
1237 : {
1238 0 : if (cluster == &old_cluster)
1239 0 : pg_fatal("The source cluster contains prepared transactions");
1240 : else
1241 0 : pg_fatal("The target cluster contains prepared transactions");
1242 : }
1243 :
1244 50 : PQclear(res);
1245 :
1246 50 : PQfinish(conn);
1247 :
1248 50 : check_ok();
1249 50 : }
1250 :
1251 : /*
1252 : * Callback function for processing result of query for
1253 : * check_for_isn_and_int8_passing_mismatch()'s UpgradeTask. If the query
1254 : * returned any rows (i.e., the check failed), write the details to the report
1255 : * file.
1256 : */
1257 : static void
1258 0 : process_isn_and_int8_passing_mismatch(DbInfo *dbinfo, PGresult *res, void *arg)
1259 : {
1260 0 : int ntups = PQntuples(res);
1261 0 : int i_nspname = PQfnumber(res, "nspname");
1262 0 : int i_proname = PQfnumber(res, "proname");
1263 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1264 :
1265 : AssertVariableIsOfType(&process_isn_and_int8_passing_mismatch,
1266 : UpgradeTaskProcessCB);
1267 :
1268 0 : if (ntups == 0)
1269 0 : return;
1270 :
1271 0 : if (report->file == NULL &&
1272 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1273 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1274 :
1275 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1276 :
1277 0 : for (int rowno = 0; rowno < ntups; rowno++)
1278 0 : fprintf(report->file, " %s.%s\n",
1279 : PQgetvalue(res, rowno, i_nspname),
1280 : PQgetvalue(res, rowno, i_proname));
1281 : }
1282 :
1283 : /*
1284 : * check_for_isn_and_int8_passing_mismatch()
1285 : *
1286 : * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
1287 : * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
1288 : * it must match for the old and new servers.
1289 : */
1290 : static void
1291 28 : check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
1292 : {
1293 : UpgradeTask *task;
1294 : UpgradeTaskReport report;
1295 28 : const char *query = "SELECT n.nspname, p.proname "
1296 : "FROM pg_catalog.pg_proc p, "
1297 : " pg_catalog.pg_namespace n "
1298 : "WHERE p.pronamespace = n.oid AND "
1299 : " p.probin = '$libdir/isn'";
1300 :
1301 28 : prep_status("Checking for contrib/isn with bigint-passing mismatch");
1302 :
1303 28 : if (old_cluster.controldata.float8_pass_by_value ==
1304 28 : new_cluster.controldata.float8_pass_by_value)
1305 : {
1306 : /* no mismatch */
1307 28 : check_ok();
1308 28 : return;
1309 : }
1310 :
1311 0 : report.file = NULL;
1312 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1313 : log_opts.basedir,
1314 : "contrib_isn_and_int8_pass_by_value.txt");
1315 :
1316 0 : task = upgrade_task_create();
1317 0 : upgrade_task_add_step(task, query, process_isn_and_int8_passing_mismatch,
1318 : true, &report);
1319 0 : upgrade_task_run(task, cluster);
1320 0 : upgrade_task_free(task);
1321 :
1322 0 : if (report.file)
1323 : {
1324 0 : fclose(report.file);
1325 0 : pg_log(PG_REPORT, "fatal");
1326 0 : pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
1327 : "bigint data type. Your old and new clusters pass bigint values\n"
1328 : "differently so this cluster cannot currently be upgraded. You can\n"
1329 : "manually dump databases in the old cluster that use \"contrib/isn\"\n"
1330 : "facilities, drop them, perform the upgrade, and then restore them. A\n"
1331 : "list of the problem functions is in the file:\n"
1332 : " %s", report.path);
1333 : }
1334 : else
1335 0 : check_ok();
1336 : }
1337 :
1338 : /*
1339 : * Callback function for processing result of query for
1340 : * check_for_user_defined_postfix_ops()'s UpgradeTask. If the query returned
1341 : * any rows (i.e., the check failed), write the details to the report file.
1342 : */
1343 : static void
1344 0 : process_user_defined_postfix_ops(DbInfo *dbinfo, PGresult *res, void *arg)
1345 : {
1346 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1347 0 : int ntups = PQntuples(res);
1348 0 : int i_oproid = PQfnumber(res, "oproid");
1349 0 : int i_oprnsp = PQfnumber(res, "oprnsp");
1350 0 : int i_oprname = PQfnumber(res, "oprname");
1351 0 : int i_typnsp = PQfnumber(res, "typnsp");
1352 0 : int i_typname = PQfnumber(res, "typname");
1353 :
1354 : AssertVariableIsOfType(&process_user_defined_postfix_ops,
1355 : UpgradeTaskProcessCB);
1356 :
1357 0 : if (ntups == 0)
1358 0 : return;
1359 :
1360 0 : if (report->file == NULL &&
1361 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1362 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1363 :
1364 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1365 :
1366 0 : for (int rowno = 0; rowno < ntups; rowno++)
1367 0 : fprintf(report->file, " (oid=%s) %s.%s (%s.%s, NONE)\n",
1368 : PQgetvalue(res, rowno, i_oproid),
1369 : PQgetvalue(res, rowno, i_oprnsp),
1370 : PQgetvalue(res, rowno, i_oprname),
1371 : PQgetvalue(res, rowno, i_typnsp),
1372 : PQgetvalue(res, rowno, i_typname));
1373 : }
1374 :
1375 : /*
1376 : * Verify that no user defined postfix operators exist.
1377 : */
1378 : static void
1379 0 : check_for_user_defined_postfix_ops(ClusterInfo *cluster)
1380 : {
1381 : UpgradeTaskReport report;
1382 0 : UpgradeTask *task = upgrade_task_create();
1383 : const char *query;
1384 :
1385 : /*
1386 : * The query below hardcodes FirstNormalObjectId as 16384 rather than
1387 : * interpolating that C #define into the query because, if that #define is
1388 : * ever changed, the cutoff we want to use is the value used by
1389 : * pre-version 14 servers, not that of some future version.
1390 : */
1391 0 : query = "SELECT o.oid AS oproid, "
1392 : " n.nspname AS oprnsp, "
1393 : " o.oprname, "
1394 : " tn.nspname AS typnsp, "
1395 : " t.typname "
1396 : "FROM pg_catalog.pg_operator o, "
1397 : " pg_catalog.pg_namespace n, "
1398 : " pg_catalog.pg_type t, "
1399 : " pg_catalog.pg_namespace tn "
1400 : "WHERE o.oprnamespace = n.oid AND "
1401 : " o.oprleft = t.oid AND "
1402 : " t.typnamespace = tn.oid AND "
1403 : " o.oprright = 0 AND "
1404 : " o.oid >= 16384";
1405 :
1406 0 : prep_status("Checking for user-defined postfix operators");
1407 :
1408 0 : report.file = NULL;
1409 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1410 : log_opts.basedir,
1411 : "postfix_ops.txt");
1412 :
1413 0 : upgrade_task_add_step(task, query, process_user_defined_postfix_ops,
1414 : true, &report);
1415 0 : upgrade_task_run(task, cluster);
1416 0 : upgrade_task_free(task);
1417 :
1418 0 : if (report.file)
1419 : {
1420 0 : fclose(report.file);
1421 0 : pg_log(PG_REPORT, "fatal");
1422 0 : pg_fatal("Your installation contains user-defined postfix operators, which are not\n"
1423 : "supported anymore. Consider dropping the postfix operators and replacing\n"
1424 : "them with prefix operators or function calls.\n"
1425 : "A list of user-defined postfix operators is in the file:\n"
1426 : " %s", report.path);
1427 : }
1428 : else
1429 0 : check_ok();
1430 0 : }
1431 :
1432 : /*
1433 : * Callback function for processing results of query for
1434 : * check_for_incompatible_polymorphics()'s UpgradeTask. If the query returned
1435 : * any rows (i.e., the check failed), write the details to the report file.
1436 : */
1437 : static void
1438 0 : process_incompat_polymorphics(DbInfo *dbinfo, PGresult *res, void *arg)
1439 : {
1440 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1441 0 : int ntups = PQntuples(res);
1442 0 : int i_objkind = PQfnumber(res, "objkind");
1443 0 : int i_objname = PQfnumber(res, "objname");
1444 :
1445 : AssertVariableIsOfType(&process_incompat_polymorphics,
1446 : UpgradeTaskProcessCB);
1447 :
1448 0 : if (ntups == 0)
1449 0 : return;
1450 :
1451 0 : if (report->file == NULL &&
1452 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1453 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1454 :
1455 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1456 :
1457 0 : for (int rowno = 0; rowno < ntups; rowno++)
1458 0 : fprintf(report->file, " %s: %s\n",
1459 : PQgetvalue(res, rowno, i_objkind),
1460 : PQgetvalue(res, rowno, i_objname));
1461 : }
1462 :
1463 : /*
1464 : * check_for_incompatible_polymorphics()
1465 : *
1466 : * Make sure nothing is using old polymorphic functions with
1467 : * anyarray/anyelement rather than the new anycompatible variants.
1468 : */
1469 : static void
1470 0 : check_for_incompatible_polymorphics(ClusterInfo *cluster)
1471 : {
1472 : PQExpBufferData old_polymorphics;
1473 0 : UpgradeTask *task = upgrade_task_create();
1474 : UpgradeTaskReport report;
1475 : char *query;
1476 :
1477 0 : prep_status("Checking for incompatible polymorphic functions");
1478 :
1479 0 : report.file = NULL;
1480 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1481 : log_opts.basedir,
1482 : "incompatible_polymorphics.txt");
1483 :
1484 : /* The set of problematic functions varies a bit in different versions */
1485 0 : initPQExpBuffer(&old_polymorphics);
1486 :
1487 0 : appendPQExpBufferStr(&old_polymorphics,
1488 : "'array_append(anyarray,anyelement)'"
1489 : ", 'array_cat(anyarray,anyarray)'"
1490 : ", 'array_prepend(anyelement,anyarray)'");
1491 :
1492 0 : if (GET_MAJOR_VERSION(cluster->major_version) >= 903)
1493 0 : appendPQExpBufferStr(&old_polymorphics,
1494 : ", 'array_remove(anyarray,anyelement)'"
1495 : ", 'array_replace(anyarray,anyelement,anyelement)'");
1496 :
1497 0 : if (GET_MAJOR_VERSION(cluster->major_version) >= 905)
1498 0 : appendPQExpBufferStr(&old_polymorphics,
1499 : ", 'array_position(anyarray,anyelement)'"
1500 : ", 'array_position(anyarray,anyelement,integer)'"
1501 : ", 'array_positions(anyarray,anyelement)'"
1502 : ", 'width_bucket(anyelement,anyarray)'");
1503 :
1504 : /*
1505 : * The query below hardcodes FirstNormalObjectId as 16384 rather than
1506 : * interpolating that C #define into the query because, if that #define is
1507 : * ever changed, the cutoff we want to use is the value used by
1508 : * pre-version 14 servers, not that of some future version.
1509 : */
1510 :
1511 : /* Aggregate transition functions */
1512 0 : query = psprintf("SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1513 : "FROM pg_proc AS p "
1514 : "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1515 : "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
1516 : "WHERE p.oid >= 16384 "
1517 : "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
1518 : "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1519 :
1520 : /* Aggregate final functions */
1521 : "UNION ALL "
1522 : "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1523 : "FROM pg_proc AS p "
1524 : "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1525 : "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
1526 : "WHERE p.oid >= 16384 "
1527 : "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
1528 : "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1529 :
1530 : /* Operators */
1531 : "UNION ALL "
1532 : "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
1533 : "FROM pg_operator AS op "
1534 : "WHERE op.oid >= 16384 "
1535 : "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
1536 : "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[])",
1537 : old_polymorphics.data,
1538 : old_polymorphics.data,
1539 : old_polymorphics.data);
1540 :
1541 0 : upgrade_task_add_step(task, query, process_incompat_polymorphics,
1542 : true, &report);
1543 0 : upgrade_task_run(task, cluster);
1544 0 : upgrade_task_free(task);
1545 :
1546 0 : if (report.file)
1547 : {
1548 0 : fclose(report.file);
1549 0 : pg_log(PG_REPORT, "fatal");
1550 0 : pg_fatal("Your installation contains user-defined objects that refer to internal\n"
1551 : "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
1552 : "These user-defined objects must be dropped before upgrading and restored\n"
1553 : "afterwards, changing them to refer to the new corresponding functions with\n"
1554 : "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
1555 : "A list of the problematic objects is in the file:\n"
1556 : " %s", report.path);
1557 : }
1558 : else
1559 0 : check_ok();
1560 :
1561 0 : termPQExpBuffer(&old_polymorphics);
1562 0 : pg_free(query);
1563 0 : }
1564 :
1565 : /*
1566 : * Callback function for processing results of query for
1567 : * check_for_tables_with_oids()'s UpgradeTask. If the query returned any rows
1568 : * (i.e., the check failed), write the details to the report file.
1569 : */
1570 : static void
1571 0 : process_with_oids_check(DbInfo *dbinfo, PGresult *res, void *arg)
1572 : {
1573 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1574 0 : int ntups = PQntuples(res);
1575 0 : int i_nspname = PQfnumber(res, "nspname");
1576 0 : int i_relname = PQfnumber(res, "relname");
1577 :
1578 : AssertVariableIsOfType(&process_with_oids_check, UpgradeTaskProcessCB);
1579 :
1580 0 : if (ntups == 0)
1581 0 : return;
1582 :
1583 0 : if (report->file == NULL &&
1584 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1585 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1586 :
1587 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1588 :
1589 0 : for (int rowno = 0; rowno < ntups; rowno++)
1590 0 : fprintf(report->file, " %s.%s\n",
1591 : PQgetvalue(res, rowno, i_nspname),
1592 : PQgetvalue(res, rowno, i_relname));
1593 : }
1594 :
1595 : /*
1596 : * Verify that no tables are declared WITH OIDS.
1597 : */
1598 : static void
1599 0 : check_for_tables_with_oids(ClusterInfo *cluster)
1600 : {
1601 : UpgradeTaskReport report;
1602 0 : UpgradeTask *task = upgrade_task_create();
1603 0 : const char *query = "SELECT n.nspname, c.relname "
1604 : "FROM pg_catalog.pg_class c, "
1605 : " pg_catalog.pg_namespace n "
1606 : "WHERE c.relnamespace = n.oid AND "
1607 : " c.relhasoids AND"
1608 : " n.nspname NOT IN ('pg_catalog')";
1609 :
1610 0 : prep_status("Checking for tables WITH OIDS");
1611 :
1612 0 : report.file = NULL;
1613 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1614 : log_opts.basedir,
1615 : "tables_with_oids.txt");
1616 :
1617 0 : upgrade_task_add_step(task, query, process_with_oids_check,
1618 : true, &report);
1619 0 : upgrade_task_run(task, cluster);
1620 0 : upgrade_task_free(task);
1621 :
1622 0 : if (report.file)
1623 : {
1624 0 : fclose(report.file);
1625 0 : pg_log(PG_REPORT, "fatal");
1626 0 : pg_fatal("Your installation contains tables declared WITH OIDS, which is not\n"
1627 : "supported anymore. Consider removing the oid column using\n"
1628 : " ALTER TABLE ... SET WITHOUT OIDS;\n"
1629 : "A list of tables with the problem is in the file:\n"
1630 : " %s", report.path);
1631 : }
1632 : else
1633 0 : check_ok();
1634 0 : }
1635 :
1636 : /*
1637 : * Callback function for processing results of query for
1638 : * check_for_not_null_inheritance.
1639 : */
1640 : static void
1641 0 : process_inconsistent_notnull(DbInfo *dbinfo, PGresult *res, void *arg)
1642 : {
1643 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1644 0 : int ntups = PQntuples(res);
1645 0 : int i_nspname = PQfnumber(res, "nspname");
1646 0 : int i_relname = PQfnumber(res, "relname");
1647 0 : int i_attname = PQfnumber(res, "attname");
1648 :
1649 : AssertVariableIsOfType(&process_inconsistent_notnull,
1650 : UpgradeTaskProcessCB);
1651 :
1652 0 : if (ntups == 0)
1653 0 : return;
1654 :
1655 0 : if (report->file == NULL &&
1656 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1657 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1658 :
1659 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1660 :
1661 0 : for (int rowno = 0; rowno < ntups; rowno++)
1662 : {
1663 0 : fprintf(report->file, " %s.%s.%s\n",
1664 : PQgetvalue(res, rowno, i_nspname),
1665 : PQgetvalue(res, rowno, i_relname),
1666 : PQgetvalue(res, rowno, i_attname));
1667 : }
1668 : }
1669 :
1670 : /*
1671 : * check_for_not_null_inheritance()
1672 : *
1673 : * An attempt to create child tables lacking not-null constraints that are
1674 : * present in their parents errors out. This can no longer occur since 18,
1675 : * but previously there were various ways for that to happen. Check that
1676 : * the cluster to be upgraded doesn't have any of those problems.
1677 : */
1678 : static void
1679 0 : check_for_not_null_inheritance(ClusterInfo *cluster)
1680 : {
1681 : UpgradeTaskReport report;
1682 : UpgradeTask *task;
1683 : const char *query;
1684 :
1685 0 : prep_status("Checking for not-null constraint inconsistencies");
1686 :
1687 0 : report.file = NULL;
1688 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1689 : log_opts.basedir,
1690 : "not_null_inconsistent_columns.txt");
1691 :
1692 0 : query = "SELECT nspname, cc.relname, ac.attname "
1693 : "FROM pg_catalog.pg_inherits i, pg_catalog.pg_attribute ac, "
1694 : " pg_catalog.pg_attribute ap, pg_catalog.pg_class cc, "
1695 : " pg_catalog.pg_namespace nc "
1696 : "WHERE cc.oid = ac.attrelid AND i.inhrelid = ac.attrelid "
1697 : " AND i.inhparent = ap.attrelid AND ac.attname = ap.attname "
1698 : " AND cc.relnamespace = nc.oid "
1699 : " AND ap.attnum > 0 and ap.attnotnull AND NOT ac.attnotnull";
1700 :
1701 0 : task = upgrade_task_create();
1702 0 : upgrade_task_add_step(task, query,
1703 : process_inconsistent_notnull,
1704 : true, &report);
1705 0 : upgrade_task_run(task, cluster);
1706 0 : upgrade_task_free(task);
1707 :
1708 0 : if (report.file)
1709 : {
1710 0 : fclose(report.file);
1711 0 : pg_log(PG_REPORT, "fatal");
1712 0 : pg_fatal("Your installation contains inconsistent NOT NULL constraints.\n"
1713 : "If the parent column(s) are NOT NULL, then the child column must\n"
1714 : "also be marked NOT NULL, or the upgrade will fail.\n"
1715 : "You can fix this by running\n"
1716 : " ALTER TABLE tablename ALTER column SET NOT NULL;\n"
1717 : "on each column listed in the file:\n"
1718 : " %s", report.path);
1719 : }
1720 : else
1721 0 : check_ok();
1722 0 : }
1723 :
1724 : /*
1725 : * check_for_pg_role_prefix()
1726 : *
1727 : * Versions older than 9.6 should not have any pg_* roles
1728 : */
1729 : static void
1730 0 : check_for_pg_role_prefix(ClusterInfo *cluster)
1731 : {
1732 : PGresult *res;
1733 0 : PGconn *conn = connectToServer(cluster, "template1");
1734 : int ntups;
1735 : int i_roloid;
1736 : int i_rolname;
1737 0 : FILE *script = NULL;
1738 : char output_path[MAXPGPATH];
1739 :
1740 0 : prep_status("Checking for roles starting with \"pg_\"");
1741 :
1742 0 : snprintf(output_path, sizeof(output_path), "%s/%s",
1743 : log_opts.basedir,
1744 : "pg_role_prefix.txt");
1745 :
1746 0 : res = executeQueryOrDie(conn,
1747 : "SELECT oid AS roloid, rolname "
1748 : "FROM pg_catalog.pg_roles "
1749 : "WHERE rolname ~ '^pg_'");
1750 :
1751 0 : ntups = PQntuples(res);
1752 0 : i_roloid = PQfnumber(res, "roloid");
1753 0 : i_rolname = PQfnumber(res, "rolname");
1754 0 : for (int rowno = 0; rowno < ntups; rowno++)
1755 : {
1756 0 : if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1757 0 : pg_fatal("could not open file \"%s\": %m", output_path);
1758 0 : fprintf(script, "%s (oid=%s)\n",
1759 : PQgetvalue(res, rowno, i_rolname),
1760 : PQgetvalue(res, rowno, i_roloid));
1761 : }
1762 :
1763 0 : PQclear(res);
1764 :
1765 0 : PQfinish(conn);
1766 :
1767 0 : if (script)
1768 : {
1769 0 : fclose(script);
1770 0 : pg_log(PG_REPORT, "fatal");
1771 0 : pg_fatal("Your installation contains roles starting with \"pg_\".\n"
1772 : "\"pg_\" is a reserved prefix for system roles. The cluster\n"
1773 : "cannot be upgraded until these roles are renamed.\n"
1774 : "A list of roles starting with \"pg_\" is in the file:\n"
1775 : " %s", output_path);
1776 : }
1777 : else
1778 0 : check_ok();
1779 0 : }
1780 :
1781 : /*
1782 : * Callback function for processing results of query for
1783 : * check_for_user_defined_encoding_conversions()'s UpgradeTask. If the query
1784 : * returned any rows (i.e., the check failed), write the details to the report
1785 : * file.
1786 : */
1787 : static void
1788 0 : process_user_defined_encoding_conversions(DbInfo *dbinfo, PGresult *res, void *arg)
1789 : {
1790 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1791 0 : int ntups = PQntuples(res);
1792 0 : int i_conoid = PQfnumber(res, "conoid");
1793 0 : int i_conname = PQfnumber(res, "conname");
1794 0 : int i_nspname = PQfnumber(res, "nspname");
1795 :
1796 : AssertVariableIsOfType(&process_user_defined_encoding_conversions,
1797 : UpgradeTaskProcessCB);
1798 :
1799 0 : if (ntups == 0)
1800 0 : return;
1801 :
1802 0 : if (report->file == NULL &&
1803 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1804 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1805 :
1806 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1807 :
1808 0 : for (int rowno = 0; rowno < ntups; rowno++)
1809 0 : fprintf(report->file, " (oid=%s) %s.%s\n",
1810 : PQgetvalue(res, rowno, i_conoid),
1811 : PQgetvalue(res, rowno, i_nspname),
1812 : PQgetvalue(res, rowno, i_conname));
1813 : }
1814 :
1815 : /*
1816 : * Verify that no user-defined encoding conversions exist.
1817 : */
1818 : static void
1819 0 : check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
1820 : {
1821 : UpgradeTaskReport report;
1822 0 : UpgradeTask *task = upgrade_task_create();
1823 : const char *query;
1824 :
1825 0 : prep_status("Checking for user-defined encoding conversions");
1826 :
1827 0 : report.file = NULL;
1828 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1829 : log_opts.basedir,
1830 : "encoding_conversions.txt");
1831 :
1832 : /*
1833 : * The query below hardcodes FirstNormalObjectId as 16384 rather than
1834 : * interpolating that C #define into the query because, if that #define is
1835 : * ever changed, the cutoff we want to use is the value used by
1836 : * pre-version 14 servers, not that of some future version.
1837 : */
1838 0 : query = "SELECT c.oid as conoid, c.conname, n.nspname "
1839 : "FROM pg_catalog.pg_conversion c, "
1840 : " pg_catalog.pg_namespace n "
1841 : "WHERE c.connamespace = n.oid AND "
1842 : " c.oid >= 16384";
1843 :
1844 0 : upgrade_task_add_step(task, query,
1845 : process_user_defined_encoding_conversions,
1846 : true, &report);
1847 0 : upgrade_task_run(task, cluster);
1848 0 : upgrade_task_free(task);
1849 :
1850 0 : if (report.file)
1851 : {
1852 0 : fclose(report.file);
1853 0 : pg_log(PG_REPORT, "fatal");
1854 0 : pg_fatal("Your installation contains user-defined encoding conversions.\n"
1855 : "The conversion function parameters changed in PostgreSQL version 14\n"
1856 : "so this cluster cannot currently be upgraded. You can remove the\n"
1857 : "encoding conversions in the old cluster and restart the upgrade.\n"
1858 : "A list of user-defined encoding conversions is in the file:\n"
1859 : " %s", report.path);
1860 : }
1861 : else
1862 0 : check_ok();
1863 0 : }
1864 :
1865 : /*
1866 : * Callback function for processing results of query for
1867 : * check_for_unicode_update()'s UpgradeTask. If the query returned any rows
1868 : * (i.e., the check failed), write the details to the report file.
1869 : */
1870 : static void
1871 0 : process_unicode_update(DbInfo *dbinfo, PGresult *res, void *arg)
1872 : {
1873 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1874 0 : int ntups = PQntuples(res);
1875 0 : int i_reloid = PQfnumber(res, "reloid");
1876 0 : int i_nspname = PQfnumber(res, "nspname");
1877 0 : int i_relname = PQfnumber(res, "relname");
1878 :
1879 0 : if (ntups == 0)
1880 0 : return;
1881 :
1882 0 : if (report->file == NULL &&
1883 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1884 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1885 :
1886 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1887 :
1888 0 : for (int rowno = 0; rowno < ntups; rowno++)
1889 0 : fprintf(report->file, " (oid=%s) %s.%s\n",
1890 : PQgetvalue(res, rowno, i_reloid),
1891 : PQgetvalue(res, rowno, i_nspname),
1892 : PQgetvalue(res, rowno, i_relname));
1893 : }
1894 :
1895 : /*
1896 : * Check if the Unicode version built into Postgres changed between the old
1897 : * cluster and the new cluster.
1898 : */
1899 : static bool
1900 24 : unicode_version_changed(ClusterInfo *cluster)
1901 : {
1902 24 : PGconn *conn_template1 = connectToServer(cluster, "template1");
1903 : PGresult *res;
1904 : char *old_unicode_version;
1905 : bool unicode_updated;
1906 :
1907 24 : res = executeQueryOrDie(conn_template1, "SELECT unicode_version()");
1908 24 : old_unicode_version = PQgetvalue(res, 0, 0);
1909 24 : unicode_updated = (strcmp(old_unicode_version, PG_UNICODE_VERSION) != 0);
1910 :
1911 24 : PQclear(res);
1912 24 : PQfinish(conn_template1);
1913 :
1914 24 : return unicode_updated;
1915 : }
1916 :
1917 : /*
1918 : * check_for_unicode_update()
1919 : *
1920 : * Check if the version of Unicode in the old server and the new server
1921 : * differ. If so, check for indexes, partitioned tables, or constraints that
1922 : * use expressions with functions dependent on Unicode behavior.
1923 : */
1924 : static void
1925 24 : check_for_unicode_update(ClusterInfo *cluster)
1926 : {
1927 : UpgradeTaskReport report;
1928 : UpgradeTask *task;
1929 : const char *query;
1930 :
1931 : /*
1932 : * The builtin provider did not exist prior to version 17. While there are
1933 : * still problems that could potentially be caught from earlier versions,
1934 : * such as an index on NORMALIZE(), we don't check for that here.
1935 : */
1936 24 : if (GET_MAJOR_VERSION(cluster->major_version) < 1700)
1937 24 : return;
1938 :
1939 24 : prep_status("Checking for objects affected by Unicode update");
1940 :
1941 24 : if (!unicode_version_changed(cluster))
1942 : {
1943 24 : check_ok();
1944 24 : return;
1945 : }
1946 :
1947 0 : report.file = NULL;
1948 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1949 : log_opts.basedir,
1950 : "unicode_dependent_rels.txt");
1951 :
1952 0 : query =
1953 : /* collations that use built-in Unicode for character semantics */
1954 : "WITH collations(collid) AS ( "
1955 : " SELECT oid FROM pg_collation "
1956 : " WHERE collprovider='b' AND colllocale IN ('C.UTF-8','PG_UNICODE_FAST') "
1957 : /* include default collation, if appropriate */
1958 : " UNION "
1959 : " SELECT 'pg_catalog.default'::regcollation FROM pg_database "
1960 : " WHERE datname = current_database() AND "
1961 : " datlocprovider='b' AND datlocale IN ('C.UTF-8','PG_UNICODE_FAST') "
1962 : "), "
1963 : /* functions that use built-in Unicode */
1964 : "functions(procid) AS ( "
1965 : " SELECT proc.oid FROM pg_proc proc "
1966 : " WHERE proname IN ('normalize','unicode_assigned','unicode_version','is_normalized') AND "
1967 : " pronamespace='pg_catalog'::regnamespace "
1968 : "), "
1969 : /* operators that use the input collation for character semantics */
1970 : "coll_operators(operid, procid, collid) AS ( "
1971 : " SELECT oper.oid, oper.oprcode, collid FROM pg_operator oper, collations "
1972 : " WHERE oprname IN ('~', '~*', '!~', '!~*', '~~*', '!~~*') AND "
1973 : " oprnamespace='pg_catalog'::regnamespace AND "
1974 : " oprright='text'::regtype "
1975 : "), "
1976 : /* functions that use the input collation for character semantics */
1977 : "coll_functions(procid, collid) AS ( "
1978 : " SELECT proc.oid, collid FROM pg_proc proc, collations "
1979 : " WHERE proname IN ('lower','initcap','upper') AND "
1980 : " pronamespace='pg_catalog'::regnamespace AND "
1981 : " proargtypes[0] = 'text'::regtype "
1982 : /* include functions behind the operators listed above */
1983 : " UNION "
1984 : " SELECT procid, collid FROM coll_operators "
1985 : "), "
1986 :
1987 : /*
1988 : * Generate patterns to search a pg_node_tree for the above functions and
1989 : * operators.
1990 : */
1991 : "patterns(p) AS ( "
1992 : " SELECT '{FUNCEXPR :funcid ' || procid::text || '[ }]' FROM functions "
1993 : " UNION "
1994 : " SELECT '{OPEXPR :opno ' || operid::text || ' (:\\w+ \\w+ )*' || "
1995 : " ':inputcollid ' || collid::text || '[ }]' FROM coll_operators "
1996 : " UNION "
1997 : " SELECT '{FUNCEXPR :funcid ' || procid::text || ' (:\\w+ \\w+ )*' || "
1998 : " ':inputcollid ' || collid::text || '[ }]' FROM coll_functions "
1999 : ") "
2000 :
2001 : /*
2002 : * Match the patterns against expressions used for relation contents.
2003 : */
2004 : "SELECT reloid, relkind, nspname, relname "
2005 : " FROM ( "
2006 : " SELECT conrelid "
2007 : " FROM pg_constraint, patterns WHERE conbin::text ~ p "
2008 : " UNION "
2009 : " SELECT indexrelid "
2010 : " FROM pg_index, patterns WHERE indexprs::text ~ p OR indpred::text ~ p "
2011 : " UNION "
2012 : " SELECT partrelid "
2013 : " FROM pg_partitioned_table, patterns WHERE partexprs::text ~ p "
2014 : " UNION "
2015 : " SELECT ev_class "
2016 : " FROM pg_rewrite, pg_class, patterns "
2017 : " WHERE ev_class = pg_class.oid AND relkind = 'm' AND ev_action::text ~ p"
2018 : " ) s(reloid), pg_class c, pg_namespace n, pg_database d "
2019 : " WHERE s.reloid = c.oid AND c.relnamespace = n.oid AND "
2020 : " d.datname = current_database() AND "
2021 : " d.encoding = pg_char_to_encoding('UTF8');";
2022 :
2023 0 : task = upgrade_task_create();
2024 0 : upgrade_task_add_step(task, query,
2025 : process_unicode_update,
2026 : true, &report);
2027 0 : upgrade_task_run(task, cluster);
2028 0 : upgrade_task_free(task);
2029 :
2030 0 : if (report.file)
2031 : {
2032 0 : fclose(report.file);
2033 0 : report_status(PG_WARNING, "warning");
2034 0 : pg_log(PG_WARNING, "Your installation contains relations that might be affected by a new version of Unicode.\n"
2035 : "A list of potentially-affected relations is in the file:\n"
2036 : " %s", report.path);
2037 : }
2038 : else
2039 0 : check_ok();
2040 : }
2041 :
2042 : /*
2043 : * check_new_cluster_logical_replication_slots()
2044 : *
2045 : * Verify that there are no logical replication slots on the new cluster and
2046 : * that the parameter settings necessary for creating slots are sufficient.
2047 : */
2048 : static void
2049 22 : check_new_cluster_logical_replication_slots(void)
2050 : {
2051 : PGresult *res;
2052 : PGconn *conn;
2053 : int nslots_on_old;
2054 : int nslots_on_new;
2055 : int max_replication_slots;
2056 : char *wal_level;
2057 :
2058 : /* Logical slots can be migrated since PG17. */
2059 22 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1600)
2060 0 : return;
2061 :
2062 22 : nslots_on_old = count_old_cluster_logical_slots();
2063 :
2064 : /* Quick return if there are no logical slots to be migrated. */
2065 22 : if (nslots_on_old == 0)
2066 18 : return;
2067 :
2068 4 : conn = connectToServer(&new_cluster, "template1");
2069 :
2070 4 : prep_status("Checking for new cluster logical replication slots");
2071 :
2072 4 : res = executeQueryOrDie(conn, "SELECT count(*) "
2073 : "FROM pg_catalog.pg_replication_slots "
2074 : "WHERE slot_type = 'logical' AND "
2075 : "temporary IS FALSE;");
2076 :
2077 4 : if (PQntuples(res) != 1)
2078 0 : pg_fatal("could not count the number of logical replication slots");
2079 :
2080 4 : nslots_on_new = atoi(PQgetvalue(res, 0, 0));
2081 :
2082 4 : if (nslots_on_new)
2083 0 : pg_fatal("expected 0 logical replication slots but found %d",
2084 : nslots_on_new);
2085 :
2086 4 : PQclear(res);
2087 :
2088 4 : res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
2089 : "WHERE name IN ('wal_level', 'max_replication_slots') "
2090 : "ORDER BY name DESC;");
2091 :
2092 4 : if (PQntuples(res) != 2)
2093 0 : pg_fatal("could not determine parameter settings on new cluster");
2094 :
2095 4 : wal_level = PQgetvalue(res, 0, 0);
2096 :
2097 4 : if (strcmp(wal_level, "logical") != 0)
2098 0 : pg_fatal("\"wal_level\" must be \"logical\" but is set to \"%s\"",
2099 : wal_level);
2100 :
2101 4 : max_replication_slots = atoi(PQgetvalue(res, 1, 0));
2102 :
2103 4 : if (nslots_on_old > max_replication_slots)
2104 2 : pg_fatal("\"max_replication_slots\" (%d) must be greater than or equal to the number of "
2105 : "logical replication slots (%d) on the old cluster",
2106 : max_replication_slots, nslots_on_old);
2107 :
2108 2 : PQclear(res);
2109 2 : PQfinish(conn);
2110 :
2111 2 : check_ok();
2112 : }
2113 :
2114 : /*
2115 : * check_new_cluster_subscription_configuration()
2116 : *
2117 : * Verify that the max_active_replication_origins configuration specified is
2118 : * enough for creating the subscriptions. This is required to create the
2119 : * replication origin for each subscription.
2120 : */
2121 : static void
2122 20 : check_new_cluster_subscription_configuration(void)
2123 : {
2124 : PGresult *res;
2125 : PGconn *conn;
2126 : int max_active_replication_origins;
2127 :
2128 : /* Subscriptions and their dependencies can be migrated since PG17. */
2129 20 : if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
2130 0 : return;
2131 :
2132 : /* Quick return if there are no subscriptions to be migrated. */
2133 20 : if (old_cluster.nsubs == 0)
2134 16 : return;
2135 :
2136 4 : prep_status("Checking for new cluster configuration for subscriptions");
2137 :
2138 4 : conn = connectToServer(&new_cluster, "template1");
2139 :
2140 4 : res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
2141 : "WHERE name = 'max_active_replication_origins';");
2142 :
2143 4 : if (PQntuples(res) != 1)
2144 0 : pg_fatal("could not determine parameter settings on new cluster");
2145 :
2146 4 : max_active_replication_origins = atoi(PQgetvalue(res, 0, 0));
2147 4 : if (old_cluster.nsubs > max_active_replication_origins)
2148 2 : pg_fatal("\"max_active_replication_origins\" (%d) must be greater than or equal to the number of "
2149 : "subscriptions (%d) on the old cluster",
2150 : max_active_replication_origins, old_cluster.nsubs);
2151 :
2152 2 : PQclear(res);
2153 2 : PQfinish(conn);
2154 :
2155 2 : check_ok();
2156 : }
2157 :
2158 : /*
2159 : * check_old_cluster_for_valid_slots()
2160 : *
2161 : * Verify that all the logical slots are valid and have consumed all the WAL
2162 : * before shutdown.
2163 : */
2164 : static void
2165 28 : check_old_cluster_for_valid_slots(void)
2166 : {
2167 : char output_path[MAXPGPATH];
2168 28 : FILE *script = NULL;
2169 :
2170 28 : prep_status("Checking for valid logical replication slots");
2171 :
2172 28 : snprintf(output_path, sizeof(output_path), "%s/%s",
2173 : log_opts.basedir,
2174 : "invalid_logical_slots.txt");
2175 :
2176 110 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
2177 : {
2178 82 : LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
2179 :
2180 92 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
2181 : {
2182 10 : LogicalSlotInfo *slot = &slot_arr->slots[slotnum];
2183 :
2184 : /* Is the slot usable? */
2185 10 : if (slot->invalid)
2186 : {
2187 0 : if (script == NULL &&
2188 0 : (script = fopen_priv(output_path, "w")) == NULL)
2189 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2190 :
2191 0 : fprintf(script, "The slot \"%s\" is invalid\n",
2192 : slot->slotname);
2193 :
2194 0 : continue;
2195 : }
2196 :
2197 : /*
2198 : * Do additional check to ensure that all logical replication
2199 : * slots have consumed all the WAL before shutdown.
2200 : *
2201 : * Note: This can be satisfied only when the old cluster has been
2202 : * shut down, so we skip this for live checks.
2203 : */
2204 10 : if (!user_opts.live_check && !slot->caught_up)
2205 : {
2206 6 : if (script == NULL &&
2207 2 : (script = fopen_priv(output_path, "w")) == NULL)
2208 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2209 :
2210 4 : fprintf(script,
2211 : "The slot \"%s\" has not consumed the WAL yet\n",
2212 : slot->slotname);
2213 : }
2214 : }
2215 : }
2216 :
2217 28 : if (script)
2218 : {
2219 2 : fclose(script);
2220 :
2221 2 : pg_log(PG_REPORT, "fatal");
2222 2 : pg_fatal("Your installation contains logical replication slots that cannot be upgraded.\n"
2223 : "You can remove invalid slots and/or consume the pending WAL for other slots,\n"
2224 : "and then restart the upgrade.\n"
2225 : "A list of the problematic slots is in the file:\n"
2226 : " %s", output_path);
2227 : }
2228 :
2229 26 : check_ok();
2230 26 : }
2231 :
2232 : /*
2233 : * Callback function for processing results of query for
2234 : * check_old_cluster_subscription_state()'s UpgradeTask. If the query returned
2235 : * any rows (i.e., the check failed), write the details to the report file.
2236 : */
2237 : static void
2238 78 : process_old_sub_state_check(DbInfo *dbinfo, PGresult *res, void *arg)
2239 : {
2240 78 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
2241 78 : int ntups = PQntuples(res);
2242 78 : int i_srsubstate = PQfnumber(res, "srsubstate");
2243 78 : int i_subname = PQfnumber(res, "subname");
2244 78 : int i_nspname = PQfnumber(res, "nspname");
2245 78 : int i_relname = PQfnumber(res, "relname");
2246 :
2247 : AssertVariableIsOfType(&process_old_sub_state_check, UpgradeTaskProcessCB);
2248 :
2249 78 : if (ntups == 0)
2250 76 : return;
2251 :
2252 2 : if (report->file == NULL &&
2253 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
2254 0 : pg_fatal("could not open file \"%s\": %m", report->path);
2255 :
2256 4 : for (int i = 0; i < ntups; i++)
2257 2 : fprintf(report->file, "The table sync state \"%s\" is not allowed for database:\"%s\" subscription:\"%s\" schema:\"%s\" relation:\"%s\"\n",
2258 : PQgetvalue(res, i, i_srsubstate),
2259 : dbinfo->db_name,
2260 : PQgetvalue(res, i, i_subname),
2261 : PQgetvalue(res, i, i_nspname),
2262 : PQgetvalue(res, i, i_relname));
2263 : }
2264 :
2265 : /*
2266 : * check_old_cluster_subscription_state()
2267 : *
2268 : * Verify that the replication origin corresponding to each of the
2269 : * subscriptions are present and each of the subscribed tables is in
2270 : * 'i' (initialize) or 'r' (ready) state.
2271 : */
2272 : static void
2273 26 : check_old_cluster_subscription_state(void)
2274 : {
2275 26 : UpgradeTask *task = upgrade_task_create();
2276 : UpgradeTaskReport report;
2277 : const char *query;
2278 : PGresult *res;
2279 : PGconn *conn;
2280 : int ntup;
2281 :
2282 26 : prep_status("Checking for subscription state");
2283 :
2284 26 : report.file = NULL;
2285 26 : snprintf(report.path, sizeof(report.path), "%s/%s",
2286 : log_opts.basedir,
2287 : "subs_invalid.txt");
2288 :
2289 : /*
2290 : * Check that all the subscriptions have their respective replication
2291 : * origin. This check only needs to run once.
2292 : */
2293 26 : conn = connectToServer(&old_cluster, old_cluster.dbarr.dbs[0].db_name);
2294 26 : res = executeQueryOrDie(conn,
2295 : "SELECT d.datname, s.subname "
2296 : "FROM pg_catalog.pg_subscription s "
2297 : "LEFT OUTER JOIN pg_catalog.pg_replication_origin o "
2298 : " ON o.roname = 'pg_' || s.oid "
2299 : "INNER JOIN pg_catalog.pg_database d "
2300 : " ON d.oid = s.subdbid "
2301 : "WHERE o.roname IS NULL;");
2302 26 : ntup = PQntuples(res);
2303 28 : for (int i = 0; i < ntup; i++)
2304 : {
2305 2 : if (report.file == NULL &&
2306 2 : (report.file = fopen_priv(report.path, "w")) == NULL)
2307 0 : pg_fatal("could not open file \"%s\": %m", report.path);
2308 2 : fprintf(report.file, "The replication origin is missing for database:\"%s\" subscription:\"%s\"\n",
2309 : PQgetvalue(res, i, 0),
2310 : PQgetvalue(res, i, 1));
2311 : }
2312 26 : PQclear(res);
2313 26 : PQfinish(conn);
2314 :
2315 : /*
2316 : * We don't allow upgrade if there is a risk of dangling slot or origin
2317 : * corresponding to initial sync after upgrade.
2318 : *
2319 : * A slot/origin not created yet refers to the 'i' (initialize) state,
2320 : * while 'r' (ready) state refers to a slot/origin created previously but
2321 : * already dropped. These states are supported for pg_upgrade. The other
2322 : * states listed below are not supported:
2323 : *
2324 : * a) SUBREL_STATE_DATASYNC: A relation upgraded while in this state would
2325 : * retain a replication slot, which could not be dropped by the sync
2326 : * worker spawned after the upgrade because the subscription ID used for
2327 : * the slot name won't match anymore.
2328 : *
2329 : * b) SUBREL_STATE_SYNCDONE: A relation upgraded while in this state would
2330 : * retain the replication origin when there is a failure in tablesync
2331 : * worker immediately after dropping the replication slot in the
2332 : * publisher.
2333 : *
2334 : * c) SUBREL_STATE_FINISHEDCOPY: A tablesync worker spawned to work on a
2335 : * relation upgraded while in this state would expect an origin ID with
2336 : * the OID of the subscription used before the upgrade, causing it to
2337 : * fail.
2338 : *
2339 : * d) SUBREL_STATE_SYNCWAIT, SUBREL_STATE_CATCHUP and
2340 : * SUBREL_STATE_UNKNOWN: These states are not stored in the catalog, so we
2341 : * need not allow these states.
2342 : */
2343 26 : query = "SELECT r.srsubstate, s.subname, n.nspname, c.relname "
2344 : "FROM pg_catalog.pg_subscription_rel r "
2345 : "LEFT JOIN pg_catalog.pg_subscription s"
2346 : " ON r.srsubid = s.oid "
2347 : "LEFT JOIN pg_catalog.pg_class c"
2348 : " ON r.srrelid = c.oid "
2349 : "LEFT JOIN pg_catalog.pg_namespace n"
2350 : " ON c.relnamespace = n.oid "
2351 : "WHERE r.srsubstate NOT IN ('i', 'r') "
2352 : "ORDER BY s.subname";
2353 :
2354 26 : upgrade_task_add_step(task, query, process_old_sub_state_check,
2355 : true, &report);
2356 :
2357 26 : upgrade_task_run(task, &old_cluster);
2358 26 : upgrade_task_free(task);
2359 :
2360 26 : if (report.file)
2361 : {
2362 2 : fclose(report.file);
2363 2 : pg_log(PG_REPORT, "fatal");
2364 2 : pg_fatal("Your installation contains subscriptions without origin or having relations not in i (initialize) or r (ready) state.\n"
2365 : "You can allow the initial sync to finish for all relations and then restart the upgrade.\n"
2366 : "A list of the problematic subscriptions is in the file:\n"
2367 : " %s", report.path);
2368 : }
2369 : else
2370 24 : check_ok();
2371 24 : }
|