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