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