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