Age Owner Branch data TLA 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 *
756 nathan@postgresql.or 314 :CBC 51 : data_type_check_query(int checknum)
315 : : {
316 : 51 : DataTypesUsageChecks *check = &data_types_usage_checks[checknum];
317 : :
318 : 51 : 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 : 168 : process_data_type_check(DbInfo *dbinfo, PGresult *res, void *arg)
372 : : {
373 : 168 : struct data_type_check_state *state = (struct data_type_check_state *) arg;
374 : 168 : int ntups = PQntuples(res);
375 : : char output_path[MAXPGPATH];
746 376 : 168 : int i_nspname = PQfnumber(res, "nspname");
377 : 168 : int i_relname = PQfnumber(res, "relname");
378 : 168 : int i_attname = PQfnumber(res, "attname");
379 : 168 : FILE *script = NULL;
380 : :
381 [ + - ]: 168 : if (ntups == 0)
382 : 168 : return;
383 : :
746 nathan@postgresql.or 384 :UBC 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));
465 alvherre@kurilemu.de 402 : 0 : appendPQExpBuffer(*state->report, "\n%s\n%s\n %s\n",
746 nathan@postgresql.or 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
756 nathan@postgresql.or 443 :CBC 17 : check_for_data_types_usage(ClusterInfo *cluster)
444 : : {
445 : 17 : PQExpBuffer report = NULL;
446 : 17 : DataTypesUsageChecks *tmp = data_types_usage_checks;
937 dgustafsson@postgres 447 : 17 : int n_data_types_usage_checks = 0;
756 nathan@postgresql.or 448 : 17 : UpgradeTask *task = upgrade_task_create();
449 : 17 : char **queries = NULL;
450 : : struct data_type_check_state *states;
451 : :
777 peter@eisentraut.org 452 : 17 : prep_status("Checking data type usage");
453 : :
454 : : /* Gather number of checks to perform */
937 dgustafsson@postgres 455 [ + + ]: 153 : while (tmp->status != NULL)
456 : : {
457 : 136 : n_data_types_usage_checks++;
458 : 136 : tmp++;
459 : : }
460 : :
461 : : /* Allocate memory for queries and for task states */
227 michael@paquier.xyz 462 : 17 : queries = pg_malloc0_array(char *, n_data_types_usage_checks);
463 : 17 : states = pg_malloc0_array(struct data_type_check_state, n_data_types_usage_checks);
464 : :
756 nathan@postgresql.or 465 [ + + ]: 153 : for (int i = 0; i < n_data_types_usage_checks; i++)
466 : : {
467 : 136 : DataTypesUsageChecks *check = &data_types_usage_checks[i];
468 : :
469 [ - + ]: 136 : if (check->threshold_version == MANUAL_CHECK)
470 : : {
756 nathan@postgresql.or 471 [ # # ]:LBC (16) : 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 [ # # ]: (16) : if (!check->version_hook(cluster))
478 : (16) : continue;
479 : : }
756 nathan@postgresql.or 480 [ + + ]:CBC 136 : else if (check->threshold_version != ALL_VERSIONS)
481 : : {
482 [ + - ]: 85 : if (GET_MAJOR_VERSION(cluster->major_version) > check->threshold_version)
483 : 85 : continue;
484 : : }
485 : : else
486 [ - + ]: 51 : Assert(check->threshold_version == ALL_VERSIONS);
487 : :
488 : 51 : queries[i] = data_type_check_query(i);
489 : :
490 : 51 : states[i].check = check;
491 : 51 : states[i].report = &report;
492 : :
493 : 51 : upgrade_task_add_step(task, queries[i], process_data_type_check,
494 : 51 : 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 : 17 : upgrade_task_run(task, cluster);
502 : 17 : upgrade_task_free(task);
503 : :
504 [ - + ]: 17 : if (report)
505 : : {
756 nathan@postgresql.or 506 :UBC 0 : pg_fatal("Data type checks failed: %s", report->data);
507 : : destroyPQExpBuffer(report);
508 : : }
509 : :
756 nathan@postgresql.or 510 [ + + ]:CBC 153 : for (int i = 0; i < n_data_types_usage_checks; i++)
511 : : {
512 [ + + ]: 136 : if (queries[i])
513 : 51 : pg_free(queries[i]);
514 : : }
515 : 17 : pg_free(queries);
516 : 17 : pg_free(states);
517 : :
937 dgustafsson@postgres 518 : 17 : check_ok();
519 : 17 : }
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 *
5152 andrew@dunslane.net 529 : 24 : 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 : 24 : return path;
546 : : #endif
547 : : }
548 : :
549 : : void
808 nathan@postgresql.or 550 : 22 : output_check_banner(void)
551 : : {
552 [ + + ]: 22 : if (user_opts.live_check)
553 : : {
3338 peter_e@gmx.net 554 :GBC 2 : pg_log(PG_REPORT,
555 : : "Performing Consistency Checks on Old Live Server\n"
556 : : "------------------------------------------------");
557 : : }
558 : : else
559 : : {
3338 peter_e@gmx.net 560 :CBC 20 : pg_log(PG_REPORT,
561 : : "Performing Consistency Checks\n"
562 : : "-----------------------------");
563 : : }
5997 bruce@momjian.us 564 : 22 : }
565 : :
566 : :
567 : : void
808 nathan@postgresql.or 568 : 20 : check_and_dump_old_cluster(void)
569 : : {
570 : : /* -- OLD -- */
571 : :
572 [ + + ]: 20 : if (!user_opts.live_check)
5009 bruce@momjian.us 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 : : */
705 dgustafsson@postgres 579 : 20 : check_for_connection_status(&old_cluster);
580 : :
581 : : /*
582 : : * Check for encodings that are no longer supported.
583 : : */
187 tmunro@postgresql.or 584 : 19 : 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 : : */
233 andrew@dunslane.net 591 [ - + ]: 19 : if (GET_MAJOR_VERSION(old_cluster.major_version) < 1900)
233 andrew@dunslane.net 592 :UBC 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 : : */
808 nathan@postgresql.or 598 :CBC 19 : get_db_rel_and_slot_infos(&old_cluster);
599 : :
5837 bruce@momjian.us 600 : 19 : init_tablespaces();
601 : :
602 : 19 : get_loadable_libraries();
603 : :
604 : :
605 : : /*
606 : : * Check for various failure cases
607 : : */
4452 608 : 19 : check_is_install_user(&old_cluster);
5599 609 : 19 : check_for_prepared_transactions(&old_cluster);
23 heikki.linnakangas@i 610 : 19 : check_for_oldestxid_consistency(&old_cluster);
5763 bruce@momjian.us 611 : 19 : check_for_isn_and_int8_passing_mismatch(&old_cluster);
612 : :
1082 akapila@postgresql.o 613 [ + - ]: 19 : 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 : : */
808 nathan@postgresql.or 619 : 19 : 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 : : */
446 akapila@postgresql.o 626 : 18 : get_subscription_info(&old_cluster);
1014 627 : 18 : check_old_cluster_subscription_state();
628 : : }
629 : :
756 nathan@postgresql.or 630 : 17 : 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 : : */
557 jdavis@postgresql.or 636 : 17 : 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 : : */
2020 heikki.linnakangas@i 644 [ - + ]: 17 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
2020 heikki.linnakangas@i 645 :UBC 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 : : */
2216 tgl@sss.pgh.pa.us 651 [ - + ]:CBC 17 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
2216 tgl@sss.pgh.pa.us 652 :UBC 0 : check_for_user_defined_postfix_ops(&old_cluster);
653 : :
654 : : /*
655 : : * PG 14 changed polymorphic functions from anyarray to
656 : : * anycompatiblearray.
657 : : */
1560 tgl@sss.pgh.pa.us 658 [ - + ]:CBC 17 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
1560 tgl@sss.pgh.pa.us 659 :UBC 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 : : */
2883 andres@anarazel.de 665 [ - + ]:CBC 17 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
2883 andres@anarazel.de 666 :UBC 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 : : */
465 alvherre@kurilemu.de 673 [ - + ]:CBC 17 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1800)
465 alvherre@kurilemu.de 674 :UBC 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 : : */
277 tgl@sss.pgh.pa.us 688 [ - + ]:CBC 17 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1800)
277 tgl@sss.pgh.pa.us 689 :UBC 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 : : */
5837 bruce@momjian.us 695 [ + + ]:CBC 17 : if (!user_opts.check)
696 : 13 : generate_old_dump();
697 : :
808 nathan@postgresql.or 698 [ + + ]: 17 : if (!user_opts.live_check)
5649 bruce@momjian.us 699 : 16 : stop_postmaster(false);
5997 700 : 17 : }
701 : :
702 : :
703 : : void
5837 704 : 17 : check_new_cluster(void)
705 : : {
808 nathan@postgresql.or 706 : 17 : get_db_rel_and_slot_infos(&new_cluster);
707 : :
5655 bruce@momjian.us 708 : 17 : check_new_cluster_is_empty();
709 : :
5837 710 : 17 : check_loadable_libraries();
711 : :
2896 peter_e@gmx.net 712 [ + + + + : 17 : switch (user_opts.transfer_mode)
+ - ]
713 : : {
714 : 1 : case TRANSFER_MODE_CLONE:
715 : 1 : check_file_clone();
2896 peter_e@gmx.net 716 :UBC 0 : break;
2896 peter_e@gmx.net 717 :CBC 13 : case TRANSFER_MODE_COPY:
718 : 13 : break;
950 tmunro@postgresql.or 719 : 1 : case TRANSFER_MODE_COPY_FILE_RANGE:
720 : 1 : check_copy_file_range();
721 : 1 : break;
2896 peter_e@gmx.net 722 : 1 : case TRANSFER_MODE_LINK:
566 nathan@postgresql.or 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);
2896 peter_e@gmx.net 737 : 1 : break;
738 : : }
739 : :
4452 bruce@momjian.us 740 : 16 : check_is_install_user(&new_cluster);
741 : :
5234 742 : 16 : check_for_prepared_transactions(&new_cluster);
743 : :
1146 dgustafsson@postgres 744 : 16 : check_for_new_tablespace_dir();
745 : :
446 akapila@postgresql.o 746 : 16 : check_new_cluster_replication_slots();
747 : :
1014 748 : 13 : check_new_cluster_subscription_configuration();
5997 bruce@momjian.us 749 : 12 : }
750 : :
751 : :
752 : : void
5837 753 : 12 : report_clusters_compatible(void)
754 : : {
755 [ + + ]: 12 : if (user_opts.check)
756 : : {
1553 tgl@sss.pgh.pa.us 757 : 2 : pg_log(PG_REPORT, "\n*Clusters are compatible*");
758 : : /* stops new cluster */
5649 bruce@momjian.us 759 : 2 : stop_postmaster(false);
760 : :
1587 michael@paquier.xyz 761 : 2 : cleanup_output_dirs();
5668 peter_e@gmx.net 762 : 2 : exit(0);
763 : : }
764 : :
5837 bruce@momjian.us 765 : 10 : pg_log(PG_REPORT, "\n"
766 : : "If pg_upgrade fails after this point, you must re-initdb the\n"
767 : : "new cluster before continuing.");
5997 768 : 10 : }
769 : :
770 : :
771 : : void
3401 772 : 10 : 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 : 10 : start_postmaster(&new_cluster, true);
781 : :
1896 782 : 10 : report_extension_updates(&new_cluster);
783 : :
3401 784 : 10 : stop_postmaster(false);
5997 785 : 10 : }
786 : :
787 : :
788 : : void
2163 magnus@hagander.net 789 : 10 : output_completion_banner(char *deletion_script_file_name)
790 : : {
791 : : PQExpBufferData user_specification;
792 : :
793 : 10 : initPQExpBuffer(&user_specification);
794 [ - + ]: 10 : if (os_info.user_specified)
795 : : {
2163 magnus@hagander.net 796 :UBC 0 : appendPQExpBufferStr(&user_specification, "-U ");
797 : 0 : appendShellString(&user_specification, os_info.user);
798 : 0 : appendPQExpBufferChar(&user_specification, ' ');
799 : : }
800 : :
2197 bruce@momjian.us 801 :CBC 10 : 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 : :
4988 809 [ + - ]: 10 : if (deletion_script_file_name)
810 : 10 : 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
4988 bruce@momjian.us 815 :UBC 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 : :
2163 magnus@hagander.net 821 :CBC 10 : termPQExpBuffer(&user_specification);
5997 bruce@momjian.us 822 : 10 : }
823 : :
824 : :
825 : : void
5837 826 : 22 : check_cluster_versions(void)
827 : : {
5591 828 : 22 : prep_status("Checking cluster versions");
829 : :
830 : : /* cluster versions should already have been obtained */
3252 tgl@sss.pgh.pa.us 831 [ - + ]: 22 : Assert(old_cluster.major_version != 0);
832 [ - + ]: 22 : 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 : :
102 nathan@postgresql.or 839 [ - + ]:GNC 22 : if (GET_MAJOR_VERSION(old_cluster.major_version) < 1000)
1553 tgl@sss.pgh.pa.us 840 :UBC 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 */
5837 bruce@momjian.us 844 [ - + ]:CBC 22 : if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
1553 tgl@sss.pgh.pa.us 845 :UBC 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 : : */
5837 bruce@momjian.us 853 [ - + ]:CBC 22 : if (old_cluster.major_version > new_cluster.major_version)
1553 tgl@sss.pgh.pa.us 854 :UBC 0 : pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.");
855 : :
856 : : /* Ensure binaries match the designated data directories */
5591 bruce@momjian.us 857 :CBC 22 : if (GET_MAJOR_VERSION(old_cluster.major_version) !=
858 [ - + ]: 22 : GET_MAJOR_VERSION(old_cluster.bin_version))
1553 tgl@sss.pgh.pa.us 859 :UBC 0 : pg_fatal("Old cluster data and binary directories are from different major versions.");
5591 bruce@momjian.us 860 :CBC 22 : if (GET_MAJOR_VERSION(new_cluster.major_version) !=
861 [ - + ]: 22 : GET_MAJOR_VERSION(new_cluster.bin_version))
1553 tgl@sss.pgh.pa.us 862 :UBC 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 : : */
598 msawada@postgresql.o 871 [ + - ]:CBC 22 : if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1800 &&
872 [ + + ]: 22 : user_opts.char_signedness != -1)
483 peter@eisentraut.org 873 : 1 : pg_fatal("The option %s cannot be used for upgrades from PostgreSQL %s and later.",
874 : : "--set-char-signedness", "18");
875 : :
5591 bruce@momjian.us 876 : 21 : check_ok();
5997 877 : 21 : }
878 : :
879 : :
880 : : void
808 nathan@postgresql.or 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);
5837 bruce@momjian.us 886 : 21 : check_control_data(&old_cluster.controldata, &new_cluster.controldata);
887 : :
808 nathan@postgresql.or 888 [ + + + + ]: 21 : if (user_opts.live_check && old_cluster.port == new_cluster.port)
4759 peter_e@gmx.net 889 :GBC 1 : pg_fatal("When checking a live server, "
890 : : "the old and new port numbers must be different.");
5997 bruce@momjian.us 891 :CBC 20 : }
892 : :
893 : :
894 : : static void
5655 895 : 17 : check_new_cluster_is_empty(void)
896 : : {
897 : : int dbnum;
898 : :
5837 899 [ + + ]: 51 : for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
900 : : {
901 : : int relnum;
902 : 34 : RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
903 : :
5997 904 [ + + ]: 170 : for (relnum = 0; relnum < rel_arr->nrels;
905 : 136 : relnum++)
906 : : {
907 : : /* pg_largeobject and its index should be skipped */
908 [ - + ]: 136 : if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
1553 tgl@sss.pgh.pa.us 909 :UBC 0 : pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
3069 peter_e@gmx.net 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 : : }
4385 heikki.linnakangas@i 915 :CBC 17 : }
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
1146 dgustafsson@postgres 927 : 16 : check_for_new_tablespace_dir(void)
928 : : {
929 : : int tblnum;
930 : : char new_tablespace_dir[MAXPGPATH];
931 : :
154 peter@eisentraut.org 932 : 16 : prep_status("Checking new cluster tablespace directories");
933 : :
439 nathan@postgresql.or 934 [ + + ]: 20 : for (tblnum = 0; tblnum < new_cluster.num_tablespaces; tblnum++)
935 : : {
936 : : struct stat statbuf;
937 : :
2188 bruce@momjian.us 938 : 4 : snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
439 nathan@postgresql.or 939 : 4 : new_cluster.tablespaces[tblnum],
940 : : new_cluster.tablespace_suffix);
941 : :
2188 bruce@momjian.us 942 [ + - - + ]: 4 : if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
1553 tgl@sss.pgh.pa.us 943 :UBC 0 : pg_fatal("new cluster tablespace directory already exists: \"%s\"",
944 : : new_tablespace_dir);
945 : : }
946 : :
2188 bruce@momjian.us 947 :CBC 16 : check_ok();
948 : 16 : }
949 : :
950 : : /*
951 : : * create_script_for_old_cluster_deletion()
952 : : *
953 : : * This is particularly useful for tablespace deletion.
954 : : */
955 : : void
5591 956 : 10 : create_script_for_old_cluster_deletion(char **deletion_script_file_name)
957 : : {
5997 958 : 10 : FILE *script = NULL;
959 : : int tblnum;
960 : : char old_cluster_pgdata[MAXPGPATH],
961 : : new_cluster_pgdata[MAXPGPATH];
962 : : char *old_tblspc_suffix;
963 : :
4384 964 : 10 : *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
965 : : SCRIPT_PREFIX, SCRIPT_EXT);
966 : :
3889 967 : 10 : strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
968 : 10 : canonicalize_path(old_cluster_pgdata);
969 : :
970 : 10 : strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
971 : 10 : canonicalize_path(new_cluster_pgdata);
972 : :
973 : : /* Some people put the new data directory inside the old one. */
974 [ - + ]: 10 : if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
975 : : {
3889 bruce@momjian.us 976 :UBC 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);
103 peter@eisentraut.org 981 :UNC 0 : pfree(*deletion_script_file_name);
3889 bruce@momjian.us 982 :UBC 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 : : */
439 nathan@postgresql.or 991 [ + + ]:CBC 14 : 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 */
4185 bruce@momjian.us 1000 :UBC 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. */
4988 1004 : 0 : unlink(*deletion_script_file_name);
103 peter@eisentraut.org 1005 :UNC 0 : pfree(*deletion_script_file_name);
4988 bruce@momjian.us 1006 :UBC 0 : *deletion_script_file_name = NULL;
1007 : 0 : return;
1008 : : }
1009 : : }
1010 : :
4988 bruce@momjian.us 1011 :CBC 10 : prep_status("Creating script to delete old cluster");
1012 : :
5327 1013 [ - + ]: 10 : if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
944 michael@paquier.xyz 1014 :UBC 0 : pg_fatal("could not open file \"%s\": %m",
1015 : : *deletion_script_file_name);
1016 : :
1017 : : #ifndef WIN32
1018 : : /* add shebang header */
5997 bruce@momjian.us 1019 :CBC 10 : fprintf(script, "#!/bin/sh\n\n");
1020 : : #endif
1021 : :
1022 : : /* delete old cluster's default tablespace */
4172 1023 : 10 : 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 */
574 nathan@postgresql.or 1027 : 10 : old_tblspc_suffix = pg_strdup(old_cluster.tablespace_suffix);
1028 : 10 : fix_path_separator(old_tblspc_suffix);
439 1029 [ + + ]: 14 : for (tblnum = 0; tblnum < old_cluster.num_tablespaces; tblnum++)
574 1030 : 4 : fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
439 1031 : 4 : fix_path_separator(old_cluster.tablespaces[tblnum]),
1032 : : old_tblspc_suffix, PATH_QUOTE);
103 peter@eisentraut.org 1033 :GNC 10 : pg_free(old_tblspc_suffix);
1034 : :
5997 bruce@momjian.us 1035 :CBC 10 : fclose(script);
1036 : :
1037 : : #ifndef WIN32
1038 [ - + ]: 10 : if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
944 michael@paquier.xyz 1039 :UBC 0 : pg_fatal("could not add execute permission to file \"%s\": %m",
1040 : : *deletion_script_file_name);
1041 : : #endif
1042 : :
5837 bruce@momjian.us 1043 :CBC 10 : 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
4452 1054 : 35 : check_is_install_user(ClusterInfo *cluster)
1055 : : {
1056 : : PGresult *res;
5637 1057 : 35 : PGconn *conn = connectToServer(cluster, "template1");
1058 : :
4452 1059 : 35 : prep_status("Checking database user is the install user");
1060 : :
1061 : : /* Can't use pg_authid because only superusers can view it. */
5637 1062 : 35 : 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 : : */
4452 1073 [ + - ]: 35 : if (PQntuples(res) != 1 ||
1074 [ - + ]: 35 : atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
1553 tgl@sss.pgh.pa.us 1075 :UBC 0 : pg_fatal("database user \"%s\" is not the install user",
1076 : : os_info.user);
1077 : :
5234 bruce@momjian.us 1078 :CBC 35 : PQclear(res);
1079 : :
1080 : 35 : res = executeQueryOrDie(conn,
1081 : : "SELECT COUNT(*) "
1082 : : "FROM pg_catalog.pg_roles "
1083 : : "WHERE rolname !~ '^pg_'");
1084 : :
1085 [ - + ]: 35 : if (PQntuples(res) != 1)
1553 tgl@sss.pgh.pa.us 1086 :UBC 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 : : */
1301 dgustafsson@postgres 1093 [ + + - + ]:CBC 35 : if (cluster == &new_cluster && strcmp(PQgetvalue(res, 0, 0), "1") != 0)
1553 tgl@sss.pgh.pa.us 1094 :UBC 0 : pg_fatal("Only the install user can be defined in the new cluster.");
1095 : :
5637 bruce@momjian.us 1096 :CBC 35 : PQclear(res);
1097 : :
1098 : 35 : PQfinish(conn);
1099 : :
5635 1100 : 35 : check_ok();
5637 1101 : 35 : }
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
705 dgustafsson@postgres 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 : : int i_datconnlimit;
1663 1121 : 20 : FILE *script = NULL;
1122 : : char output_path[MAXPGPATH];
1123 : :
4167 bruce@momjian.us 1124 : 20 : prep_status("Checking database connection settings");
1125 : :
1663 dgustafsson@postgres 1126 : 20 : snprintf(output_path, sizeof(output_path), "%s/%s",
1127 : : log_opts.basedir,
1128 : : "databases_cannot_connect_to.txt");
1129 : :
4167 bruce@momjian.us 1130 : 20 : conn_template1 = connectToServer(cluster, "template1");
1131 : :
1132 : : /* get database names */
1133 : 20 : dbres = executeQueryOrDie(conn_template1,
1134 : : "SELECT datname, datallowconn, datconnlimit "
1135 : : "FROM pg_catalog.pg_database");
1136 : :
1137 : 20 : i_datname = PQfnumber(dbres, "datname");
1138 : 20 : i_datallowconn = PQfnumber(dbres, "datallowconn");
705 dgustafsson@postgres 1139 : 20 : i_datconnlimit = PQfnumber(dbres, "datconnlimit");
1140 : :
4167 bruce@momjian.us 1141 : 20 : ntups = PQntuples(dbres);
1142 [ + + ]: 107 : for (dbnum = 0; dbnum < ntups; dbnum++)
1143 : : {
1144 : 87 : char *datname = PQgetvalue(dbres, dbnum, i_datname);
1145 : 87 : char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
705 dgustafsson@postgres 1146 : 87 : char *datconnlimit = PQgetvalue(dbres, dbnum, i_datconnlimit);
1147 : :
4167 bruce@momjian.us 1148 [ + + ]: 87 : if (strcmp(datname, "template0") == 0)
1149 : : {
1150 : : /* avoid restore failure when pg_dumpall tries to create template0 */
1151 [ - + ]: 20 : if (strcmp(datallowconn, "t") == 0)
4167 bruce@momjian.us 1152 :UBC 0 : pg_fatal("template0 must not allow connections, "
1153 : : "i.e. its pg_database.datallowconn must be false");
1154 : : }
1155 : : else
1156 : : {
1157 : : /*
1158 : : * Avoid datallowconn == false databases from being skipped on
1159 : : * restore, and ensure that no databases are marked invalid with
1160 : : * datconnlimit == -2.
1161 : : */
705 dgustafsson@postgres 1162 [ + - + + ]:CBC 67 : if ((strcmp(datallowconn, "f") == 0) || strcmp(datconnlimit, "-2") == 0)
1163 : : {
1663 1164 [ + - - + ]: 1 : if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
944 michael@paquier.xyz 1165 :UBC 0 : pg_fatal("could not open file \"%s\": %m", output_path);
1166 : :
1663 dgustafsson@postgres 1167 :CBC 1 : fprintf(script, "%s\n", datname);
1168 : : }
1169 : : }
1170 : : }
1171 : :
4167 bruce@momjian.us 1172 : 20 : PQclear(dbres);
1173 : :
1174 : 20 : PQfinish(conn_template1);
1175 : :
1663 dgustafsson@postgres 1176 [ + + ]: 20 : if (script)
1177 : : {
1503 1178 : 1 : fclose(script);
1553 tgl@sss.pgh.pa.us 1179 : 1 : pg_log(PG_REPORT, "fatal");
1663 dgustafsson@postgres 1180 : 1 : pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
1181 : : "pg_database.datallowconn must be true and pg_database.datconnlimit\n"
1182 : : "must not be -2. Your installation contains non-template0 databases\n"
1183 : : "which cannot be connected to. Consider allowing connection for all\n"
1184 : : "non-template0 databases or drop the databases which do not allow\n"
1185 : : "connections. A list of databases with the problem is in the file:\n"
1186 : : " %s", output_path);
1187 : : }
1188 : : else
1189 : 19 : check_ok();
4167 bruce@momjian.us 1190 : 19 : }
1191 : :
1192 : :
1193 : : /*
1194 : : * check_for_unsupported_encodings()
1195 : : */
1196 : : static void
187 tmunro@postgresql.or 1197 : 19 : check_for_unsupported_encodings(ClusterInfo *cluster)
1198 : : {
1199 : : int i_datname;
1200 : : int i_encoding;
1201 : : int ntups;
1202 : : PGresult *res;
1203 : : PGconn *conn;
1204 : 19 : FILE *script = NULL;
1205 : : char output_path[MAXPGPATH];
1206 : :
1207 : 19 : prep_status("Checking for unsupported encodings");
1208 : :
1209 : 19 : snprintf(output_path, sizeof(output_path), "%s/%s",
1210 : : log_opts.basedir,
1211 : : "databases_unsupported_encoding.txt");
1212 : :
1213 : 19 : conn = connectToServer(cluster, "template1");
1214 : :
1215 : 19 : res = executeQueryOrDie(conn,
1216 : : "SELECT datname, encoding "
1217 : : "FROM pg_catalog.pg_database");
1218 : 19 : ntups = PQntuples(res);
1219 : 19 : i_datname = PQfnumber(res, "datname");
1220 : 19 : i_encoding = PQfnumber(res, "encoding");
1221 [ + + ]: 98 : for (int rowno = 0; rowno < ntups; rowno++)
1222 : : {
1223 : 79 : char *datname = PQgetvalue(res, rowno, i_datname);
1224 : 79 : int encoding = atoi(PQgetvalue(res, rowno, i_encoding));
1225 : :
1226 [ + - + - : 79 : if (!PG_VALID_BE_ENCODING(encoding))
- + ]
1227 : : {
187 tmunro@postgresql.or 1228 [ # # # # ]:UBC 0 : if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1229 : 0 : pg_fatal("could not open file \"%s\": %m", output_path);
1230 : :
1231 : 0 : fprintf(script, "%s\n", datname);
1232 : : }
1233 : : }
187 tmunro@postgresql.or 1234 :CBC 19 : PQclear(res);
1235 : 19 : PQfinish(conn);
1236 : :
1237 [ - + ]: 19 : if (script)
1238 : : {
187 tmunro@postgresql.or 1239 :UBC 0 : fclose(script);
1240 : 0 : pg_log(PG_REPORT, "fatal");
1241 : 0 : pg_fatal("Your installation contains databases using encodings that are\n"
1242 : : "no longer supported. Consider dumping and restoring with UTF8.\n"
1243 : : "A list of databases with unsupported encodings is in the file:\n"
1244 : : " %s", output_path);
1245 : : }
1246 : : else
187 tmunro@postgresql.or 1247 :CBC 19 : check_ok();
1248 : 19 : }
1249 : :
1250 : :
1251 : : /*
1252 : : * check_for_prepared_transactions()
1253 : : *
1254 : : * Make sure there are no prepared transactions because the storage format
1255 : : * might have changed.
1256 : : */
1257 : : static void
5599 bruce@momjian.us 1258 : 35 : check_for_prepared_transactions(ClusterInfo *cluster)
1259 : : {
1260 : : PGresult *res;
1261 : 35 : PGconn *conn = connectToServer(cluster, "template1");
1262 : :
1263 : 35 : prep_status("Checking for prepared transactions");
1264 : :
1265 : 35 : res = executeQueryOrDie(conn,
1266 : : "SELECT * "
1267 : : "FROM pg_catalog.pg_prepared_xacts");
1268 : :
1269 [ - + ]: 35 : if (PQntuples(res) != 0)
1270 : : {
3377 alvherre@alvh.no-ip. 1271 [ # # ]:UBC 0 : if (cluster == &old_cluster)
1553 tgl@sss.pgh.pa.us 1272 : 0 : pg_fatal("The source cluster contains prepared transactions");
1273 : : else
1274 : 0 : pg_fatal("The target cluster contains prepared transactions");
1275 : : }
1276 : :
5599 bruce@momjian.us 1277 :CBC 35 : PQclear(res);
1278 : :
1279 : 35 : PQfinish(conn);
1280 : :
1281 : 35 : check_ok();
1282 : 35 : }
1283 : :
1284 : : /*
1285 : : * Callback function for processing result of query for
1286 : : * check_for_isn_and_int8_passing_mismatch()'s UpgradeTask. If the query
1287 : : * returned any rows (i.e., the check failed), write the details to the report
1288 : : * file.
1289 : : */
1290 : : static void
756 nathan@postgresql.or 1291 :UBC 0 : process_isn_and_int8_passing_mismatch(DbInfo *dbinfo, PGresult *res, void *arg)
1292 : : {
1293 : 0 : int ntups = PQntuples(res);
1294 : 0 : int i_nspname = PQfnumber(res, "nspname");
1295 : 0 : int i_proname = PQfnumber(res, "proname");
1296 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1297 : :
746 1298 [ # # ]: 0 : if (ntups == 0)
1299 : 0 : return;
1300 : :
1301 [ # # ]: 0 : if (report->file == NULL &&
1302 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1303 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1304 : :
1305 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1306 : :
756 1307 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1308 : 0 : fprintf(report->file, " %s.%s\n",
1309 : : PQgetvalue(res, rowno, i_nspname),
1310 : : PQgetvalue(res, rowno, i_proname));
1311 : : }
1312 : :
1313 : : /*
1314 : : * check_for_isn_and_int8_passing_mismatch()
1315 : : *
1316 : : * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
1317 : : * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
1318 : : * it must match for the old and new servers.
1319 : : */
1320 : : static void
5763 bruce@momjian.us 1321 :CBC 19 : check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
1322 : : {
1323 : : UpgradeTask *task;
1324 : : UpgradeTaskReport report;
756 nathan@postgresql.or 1325 : 19 : const char *query = "SELECT n.nspname, p.proname "
1326 : : "FROM pg_catalog.pg_proc p, "
1327 : : " pg_catalog.pg_namespace n "
1328 : : "WHERE p.pronamespace = n.oid AND "
1329 : : " p.probin = '$libdir/isn'";
1330 : :
5625 peter_e@gmx.net 1331 : 19 : prep_status("Checking for contrib/isn with bigint-passing mismatch");
1332 : :
5837 bruce@momjian.us 1333 : 19 : if (old_cluster.controldata.float8_pass_by_value ==
1334 [ + - ]: 19 : new_cluster.controldata.float8_pass_by_value)
1335 : : {
1336 : : /* no mismatch */
1337 : 19 : check_ok();
5923 1338 : 19 : return;
1339 : : }
1340 : :
756 nathan@postgresql.or 1341 :UBC 0 : report.file = NULL;
1342 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1343 : : log_opts.basedir,
1344 : : "contrib_isn_and_int8_pass_by_value.txt");
1345 : :
1346 : 0 : task = upgrade_task_create();
1347 : 0 : upgrade_task_add_step(task, query, process_isn_and_int8_passing_mismatch,
1348 : : true, &report);
1349 : 0 : upgrade_task_run(task, cluster);
1350 : 0 : upgrade_task_free(task);
1351 : :
1352 [ # # ]: 0 : if (report.file)
1353 : : {
1354 : 0 : fclose(report.file);
1553 tgl@sss.pgh.pa.us 1355 : 0 : pg_log(PG_REPORT, "fatal");
4759 peter_e@gmx.net 1356 : 0 : pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
1357 : : "bigint data type. Your old and new clusters pass bigint values\n"
1358 : : "differently so this cluster cannot currently be upgraded. You can\n"
1359 : : "manually dump databases in the old cluster that use \"contrib/isn\"\n"
1360 : : "facilities, drop them, perform the upgrade, and then restore them. A\n"
1361 : : "list of the problem functions is in the file:\n"
1362 : : " %s", report.path);
1363 : : }
1364 : : else
5837 bruce@momjian.us 1365 : 0 : check_ok();
1366 : : }
1367 : :
1368 : : /*
1369 : : * Callback function for processing result of query for
1370 : : * check_for_user_defined_postfix_ops()'s UpgradeTask. If the query returned
1371 : : * any rows (i.e., the check failed), write the details to the report file.
1372 : : */
1373 : : static void
756 nathan@postgresql.or 1374 : 0 : process_user_defined_postfix_ops(DbInfo *dbinfo, PGresult *res, void *arg)
1375 : : {
1376 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1377 : 0 : int ntups = PQntuples(res);
1378 : 0 : int i_oproid = PQfnumber(res, "oproid");
1379 : 0 : int i_oprnsp = PQfnumber(res, "oprnsp");
1380 : 0 : int i_oprname = PQfnumber(res, "oprname");
1381 : 0 : int i_typnsp = PQfnumber(res, "typnsp");
1382 : 0 : int i_typname = PQfnumber(res, "typname");
1383 : :
746 1384 [ # # ]: 0 : if (ntups == 0)
756 1385 : 0 : return;
1386 : :
746 1387 [ # # ]: 0 : if (report->file == NULL &&
1388 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1389 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1390 : :
1391 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1392 : :
756 1393 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1394 : 0 : fprintf(report->file, " (oid=%s) %s.%s (%s.%s, NONE)\n",
1395 : : PQgetvalue(res, rowno, i_oproid),
1396 : : PQgetvalue(res, rowno, i_oprnsp),
1397 : : PQgetvalue(res, rowno, i_oprname),
1398 : : PQgetvalue(res, rowno, i_typnsp),
1399 : : PQgetvalue(res, rowno, i_typname));
1400 : : }
1401 : :
1402 : : /*
1403 : : * Verify that no user defined postfix operators exist.
1404 : : */
1405 : : static void
1406 : 0 : check_for_user_defined_postfix_ops(ClusterInfo *cluster)
1407 : : {
1408 : : UpgradeTaskReport report;
1409 : 0 : UpgradeTask *task = upgrade_task_create();
1410 : : const char *query;
1411 : :
1412 : : /*
1413 : : * The query below hardcodes FirstNormalObjectId as 16384 rather than
1414 : : * interpolating that C #define into the query because, if that #define is
1415 : : * ever changed, the cutoff we want to use is the value used by
1416 : : * pre-version 14 servers, not that of some future version.
1417 : : */
1418 : 0 : query = "SELECT o.oid AS oproid, "
1419 : : " n.nspname AS oprnsp, "
1420 : : " o.oprname, "
1421 : : " tn.nspname AS typnsp, "
1422 : : " t.typname "
1423 : : "FROM pg_catalog.pg_operator o, "
1424 : : " pg_catalog.pg_namespace n, "
1425 : : " pg_catalog.pg_type t, "
1426 : : " pg_catalog.pg_namespace tn "
1427 : : "WHERE o.oprnamespace = n.oid AND "
1428 : : " o.oprleft = t.oid AND "
1429 : : " t.typnamespace = tn.oid AND "
1430 : : " o.oprright = 0 AND "
1431 : : " o.oid >= 16384";
1432 : :
1433 : 0 : prep_status("Checking for user-defined postfix operators");
1434 : :
1435 : 0 : report.file = NULL;
1436 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1437 : : log_opts.basedir,
1438 : : "postfix_ops.txt");
1439 : :
1440 : 0 : upgrade_task_add_step(task, query, process_user_defined_postfix_ops,
1441 : : true, &report);
1442 : 0 : upgrade_task_run(task, cluster);
1443 : 0 : upgrade_task_free(task);
1444 : :
1445 [ # # ]: 0 : if (report.file)
1446 : : {
1447 : 0 : fclose(report.file);
1553 tgl@sss.pgh.pa.us 1448 : 0 : pg_log(PG_REPORT, "fatal");
2216 1449 : 0 : pg_fatal("Your installation contains user-defined postfix operators, which are not\n"
1450 : : "supported anymore. Consider dropping the postfix operators and replacing\n"
1451 : : "them with prefix operators or function calls.\n"
1452 : : "A list of user-defined postfix operators is in the file:\n"
1453 : : " %s", report.path);
1454 : : }
1455 : : else
1456 : 0 : check_ok();
1457 : 0 : }
1458 : :
1459 : : /*
1460 : : * Callback function for processing results of query for
1461 : : * check_for_incompatible_polymorphics()'s UpgradeTask. If the query returned
1462 : : * any rows (i.e., the check failed), write the details to the report file.
1463 : : */
1464 : : static void
756 nathan@postgresql.or 1465 : 0 : process_incompat_polymorphics(DbInfo *dbinfo, PGresult *res, void *arg)
1466 : : {
1467 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1468 : 0 : int ntups = PQntuples(res);
1469 : 0 : int i_objkind = PQfnumber(res, "objkind");
1470 : 0 : int i_objname = PQfnumber(res, "objname");
1471 : :
746 1472 [ # # ]: 0 : if (ntups == 0)
1473 : 0 : return;
1474 : :
1475 [ # # ]: 0 : if (report->file == NULL &&
1476 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1477 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1478 : :
1479 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1480 : :
1481 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
756 1482 : 0 : fprintf(report->file, " %s: %s\n",
1483 : : PQgetvalue(res, rowno, i_objkind),
1484 : : PQgetvalue(res, rowno, i_objname));
1485 : : }
1486 : :
1487 : : /*
1488 : : * check_for_incompatible_polymorphics()
1489 : : *
1490 : : * Make sure nothing is using old polymorphic functions with
1491 : : * anyarray/anyelement rather than the new anycompatible variants.
1492 : : */
1493 : : static void
1560 tgl@sss.pgh.pa.us 1494 : 0 : check_for_incompatible_polymorphics(ClusterInfo *cluster)
1495 : : {
1496 : : PQExpBufferData old_polymorphics;
756 nathan@postgresql.or 1497 : 0 : UpgradeTask *task = upgrade_task_create();
1498 : : UpgradeTaskReport report;
1499 : : char *query;
1500 : :
1560 tgl@sss.pgh.pa.us 1501 : 0 : prep_status("Checking for incompatible polymorphic functions");
1502 : :
756 nathan@postgresql.or 1503 : 0 : report.file = NULL;
1504 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1505 : : log_opts.basedir,
1506 : : "incompatible_polymorphics.txt");
1507 : :
1508 : : /* The set of problematic functions varies a bit in different versions */
1560 tgl@sss.pgh.pa.us 1509 : 0 : initPQExpBuffer(&old_polymorphics);
1510 : :
1511 : 0 : appendPQExpBufferStr(&old_polymorphics,
1512 : : "'array_append(anyarray,anyelement)'"
1513 : : ", 'array_cat(anyarray,anyarray)'"
1514 : : ", 'array_prepend(anyelement,anyarray)'");
1515 : :
102 nathan@postgresql.or 1516 :UNC 0 : appendPQExpBufferStr(&old_polymorphics,
1517 : : ", 'array_remove(anyarray,anyelement)'"
1518 : : ", 'array_replace(anyarray,anyelement,anyelement)'");
1519 : :
1520 : 0 : appendPQExpBufferStr(&old_polymorphics,
1521 : : ", 'array_position(anyarray,anyelement)'"
1522 : : ", 'array_position(anyarray,anyelement,integer)'"
1523 : : ", 'array_positions(anyarray,anyelement)'"
1524 : : ", 'width_bucket(anyelement,anyarray)'");
1525 : :
1526 : : /*
1527 : : * The query below hardcodes FirstNormalObjectId as 16384 rather than
1528 : : * interpolating that C #define into the query because, if that #define is
1529 : : * ever changed, the cutoff we want to use is the value used by
1530 : : * pre-version 14 servers, not that of some future version.
1531 : : */
1532 : :
1533 : : /* Aggregate transition functions */
756 nathan@postgresql.or 1534 :UBC 0 : query = psprintf("SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1535 : : "FROM pg_proc AS p "
1536 : : "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1537 : : "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
1538 : : "WHERE p.oid >= 16384 "
1539 : : "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
1540 : : "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1541 : :
1542 : : /* Aggregate final functions */
1543 : : "UNION ALL "
1544 : : "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
1545 : : "FROM pg_proc AS p "
1546 : : "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
1547 : : "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
1548 : : "WHERE p.oid >= 16384 "
1549 : : "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
1550 : : "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
1551 : :
1552 : : /* Operators */
1553 : : "UNION ALL "
1554 : : "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
1555 : : "FROM pg_operator AS op "
1556 : : "WHERE op.oid >= 16384 "
1557 : : "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
1558 : : "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[])",
1559 : : old_polymorphics.data,
1560 : : old_polymorphics.data,
1561 : : old_polymorphics.data);
1562 : :
1563 : 0 : upgrade_task_add_step(task, query, process_incompat_polymorphics,
1564 : : true, &report);
1565 : 0 : upgrade_task_run(task, cluster);
1566 : 0 : upgrade_task_free(task);
1567 : :
1568 [ # # ]: 0 : if (report.file)
1569 : : {
1570 : 0 : fclose(report.file);
1553 tgl@sss.pgh.pa.us 1571 : 0 : pg_log(PG_REPORT, "fatal");
1560 1572 : 0 : pg_fatal("Your installation contains user-defined objects that refer to internal\n"
1573 : : "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
1574 : : "These user-defined objects must be dropped before upgrading and restored\n"
1575 : : "afterwards, changing them to refer to the new corresponding functions with\n"
1576 : : "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
1577 : : "A list of the problematic objects is in the file:\n"
1578 : : " %s", report.path);
1579 : : }
1580 : : else
1581 : 0 : check_ok();
1582 : :
1583 : 0 : termPQExpBuffer(&old_polymorphics);
103 peter@eisentraut.org 1584 :UNC 0 : pfree(query);
1560 tgl@sss.pgh.pa.us 1585 :UBC 0 : }
1586 : :
1587 : : /*
1588 : : * Callback function for processing results of query for
1589 : : * check_for_tables_with_oids()'s UpgradeTask. If the query returned any rows
1590 : : * (i.e., the check failed), write the details to the report file.
1591 : : */
1592 : : static void
756 nathan@postgresql.or 1593 : 0 : process_with_oids_check(DbInfo *dbinfo, PGresult *res, void *arg)
1594 : : {
1595 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1596 : 0 : int ntups = PQntuples(res);
1597 : 0 : int i_nspname = PQfnumber(res, "nspname");
1598 : 0 : int i_relname = PQfnumber(res, "relname");
1599 : :
746 1600 [ # # ]: 0 : if (ntups == 0)
756 1601 : 0 : return;
1602 : :
746 1603 [ # # ]: 0 : if (report->file == NULL &&
1604 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1605 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1606 : :
1607 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1608 : :
756 1609 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1610 : 0 : fprintf(report->file, " %s.%s\n",
1611 : : PQgetvalue(res, rowno, i_nspname),
1612 : : PQgetvalue(res, rowno, i_relname));
1613 : : }
1614 : :
1615 : : /*
1616 : : * Verify that no tables are declared WITH OIDS.
1617 : : */
1618 : : static void
1619 : 0 : check_for_tables_with_oids(ClusterInfo *cluster)
1620 : : {
1621 : : UpgradeTaskReport report;
1622 : 0 : UpgradeTask *task = upgrade_task_create();
1623 : 0 : const char *query = "SELECT n.nspname, c.relname "
1624 : : "FROM pg_catalog.pg_class c, "
1625 : : " pg_catalog.pg_namespace n "
1626 : : "WHERE c.relnamespace = n.oid AND "
1627 : : " c.relhasoids AND"
1628 : : " n.nspname NOT IN ('pg_catalog')";
1629 : :
1630 : 0 : prep_status("Checking for tables WITH OIDS");
1631 : :
1632 : 0 : report.file = NULL;
1633 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1634 : : log_opts.basedir,
1635 : : "tables_with_oids.txt");
1636 : :
1637 : 0 : upgrade_task_add_step(task, query, process_with_oids_check,
1638 : : true, &report);
1639 : 0 : upgrade_task_run(task, cluster);
1640 : 0 : upgrade_task_free(task);
1641 : :
1642 [ # # ]: 0 : if (report.file)
1643 : : {
1644 : 0 : fclose(report.file);
1553 tgl@sss.pgh.pa.us 1645 : 0 : pg_log(PG_REPORT, "fatal");
2511 bruce@momjian.us 1646 : 0 : pg_fatal("Your installation contains tables declared WITH OIDS, which is not\n"
1647 : : "supported anymore. Consider removing the oid column using\n"
1648 : : " ALTER TABLE ... SET WITHOUT OIDS;\n"
1649 : : "A list of tables with the problem is in the file:\n"
1650 : : " %s", report.path);
1651 : : }
1652 : : else
2883 andres@anarazel.de 1653 : 0 : check_ok();
1654 : 0 : }
1655 : :
1656 : : /*
1657 : : * Callback function for processing results of query for
1658 : : * check_for_not_null_inheritance.
1659 : : */
1660 : : static void
465 alvherre@kurilemu.de 1661 : 0 : process_inconsistent_notnull(DbInfo *dbinfo, PGresult *res, void *arg)
1662 : : {
1663 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1664 : 0 : int ntups = PQntuples(res);
1665 : 0 : int i_nspname = PQfnumber(res, "nspname");
1666 : 0 : int i_relname = PQfnumber(res, "relname");
1667 : 0 : int i_attname = PQfnumber(res, "attname");
1668 : :
1669 [ # # ]: 0 : if (ntups == 0)
1670 : 0 : return;
1671 : :
1672 [ # # ]: 0 : if (report->file == NULL &&
1673 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1674 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1675 : :
1676 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1677 : :
1678 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1679 : : {
1680 : 0 : fprintf(report->file, " %s.%s.%s\n",
1681 : : PQgetvalue(res, rowno, i_nspname),
1682 : : PQgetvalue(res, rowno, i_relname),
1683 : : PQgetvalue(res, rowno, i_attname));
1684 : : }
1685 : : }
1686 : :
1687 : : /*
1688 : : * check_for_not_null_inheritance()
1689 : : *
1690 : : * An attempt to create child tables lacking not-null constraints that are
1691 : : * present in their parents errors out. This can no longer occur since 18,
1692 : : * but previously there were various ways for that to happen. Check that
1693 : : * the cluster to be upgraded doesn't have any of those problems.
1694 : : */
1695 : : static void
1696 : 0 : check_for_not_null_inheritance(ClusterInfo *cluster)
1697 : : {
1698 : : UpgradeTaskReport report;
1699 : : UpgradeTask *task;
1700 : : const char *query;
1701 : :
1702 : 0 : prep_status("Checking for not-null constraint inconsistencies");
1703 : :
1704 : 0 : report.file = NULL;
1705 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1706 : : log_opts.basedir,
1707 : : "not_null_inconsistent_columns.txt");
1708 : :
1709 : 0 : query = "SELECT nspname, cc.relname, ac.attname "
1710 : : "FROM pg_catalog.pg_inherits i, pg_catalog.pg_attribute ac, "
1711 : : " pg_catalog.pg_attribute ap, pg_catalog.pg_class cc, "
1712 : : " pg_catalog.pg_namespace nc "
1713 : : "WHERE cc.oid = ac.attrelid AND i.inhrelid = ac.attrelid "
1714 : : " AND i.inhparent = ap.attrelid AND ac.attname = ap.attname "
1715 : : " AND cc.relnamespace = nc.oid "
1716 : : " AND ap.attnum > 0 and ap.attnotnull AND NOT ac.attnotnull";
1717 : :
1718 : 0 : task = upgrade_task_create();
1719 : 0 : upgrade_task_add_step(task, query,
1720 : : process_inconsistent_notnull,
1721 : : true, &report);
1722 : 0 : upgrade_task_run(task, cluster);
1723 : 0 : upgrade_task_free(task);
1724 : :
1725 [ # # ]: 0 : if (report.file)
1726 : : {
1727 : 0 : fclose(report.file);
1728 : 0 : pg_log(PG_REPORT, "fatal");
1729 : 0 : pg_fatal("Your installation contains inconsistent NOT NULL constraints.\n"
1730 : : "If the parent column(s) are NOT NULL, then the child column must\n"
1731 : : "also be marked NOT NULL, or the upgrade will fail.\n"
1732 : : "You can fix this by running\n"
1733 : : " ALTER TABLE tablename ALTER column SET NOT NULL;\n"
1734 : : "on each column listed in the file:\n"
1735 : : " %s", report.path);
1736 : : }
1737 : : else
1738 : 0 : check_ok();
1739 : 0 : }
1740 : :
1741 : : /*
1742 : : * Callback function for processing results of query for
1743 : : * check_for_gist_inet_ops()'s UpgradeTask. If the query returned any rows
1744 : : * (i.e., the check failed), write the details to the report file.
1745 : : */
1746 : : static void
277 tgl@sss.pgh.pa.us 1747 : 0 : process_gist_inet_ops_check(DbInfo *dbinfo, PGresult *res, void *arg)
1748 : : {
1749 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1750 : 0 : int ntups = PQntuples(res);
1751 : 0 : int i_nspname = PQfnumber(res, "nspname");
1752 : 0 : int i_relname = PQfnumber(res, "relname");
1753 : :
1754 [ # # ]: 0 : if (ntups == 0)
1755 : 0 : return;
1756 : :
1757 [ # # ]: 0 : if (report->file == NULL &&
1758 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1759 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1760 : :
1761 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1762 : :
1763 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1764 : 0 : fprintf(report->file, " %s.%s\n",
1765 : : PQgetvalue(res, rowno, i_nspname),
1766 : : PQgetvalue(res, rowno, i_relname));
1767 : : }
1768 : :
1769 : : /*
1770 : : * Verify that no indexes use gist_inet_ops/gist_cidr_ops, unless the
1771 : : * opclasses have been changed to not-opcdefault (which would allow
1772 : : * the old server to dump the index definitions with explicit opclasses).
1773 : : */
1774 : : static void
1775 : 0 : check_for_gist_inet_ops(ClusterInfo *cluster)
1776 : : {
1777 : : UpgradeTaskReport report;
1778 : 0 : UpgradeTask *task = upgrade_task_create();
1779 : 0 : const char *query = "SELECT nc.nspname, cc.relname "
1780 : : "FROM pg_catalog.pg_opclass oc, pg_catalog.pg_index i, "
1781 : : " pg_catalog.pg_class cc, pg_catalog.pg_namespace nc "
1782 : : "WHERE oc.opcmethod = " CppAsString2(GIST_AM_OID)
1783 : : " AND oc.opcname IN ('gist_inet_ops', 'gist_cidr_ops')"
1784 : : " AND oc.opcdefault"
1785 : : " AND oc.oid = any(i.indclass)"
1786 : : " AND i.indexrelid = cc.oid AND cc.relnamespace = nc.oid";
1787 : :
1788 : 0 : prep_status("Checking for uses of gist_inet_ops/gist_cidr_ops");
1789 : :
1790 : 0 : report.file = NULL;
1791 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1792 : : log_opts.basedir,
1793 : : "gist_inet_ops.txt");
1794 : :
1795 : 0 : upgrade_task_add_step(task, query, process_gist_inet_ops_check,
1796 : : true, &report);
1797 : 0 : upgrade_task_run(task, cluster);
1798 : 0 : upgrade_task_free(task);
1799 : :
1800 [ # # ]: 0 : if (report.file)
1801 : : {
1802 : 0 : fclose(report.file);
1803 : 0 : pg_log(PG_REPORT, "fatal");
83 peter@eisentraut.org 1804 : 0 : pg_fatal("Your installation contains indexes that use the btree_gist extension's\n"
1805 : : "gist_inet_ops or gist_cidr_ops operator classes, which cannot be\n"
1806 : : "binary-upgraded. Replace them with indexes that use the built-in GiST\n"
1807 : : "inet_ops operator class.\n"
1808 : : "A list of indexes with the problem is in the file:\n"
1809 : : " %s", report.path);
1810 : : }
1811 : : else
277 tgl@sss.pgh.pa.us 1812 : 0 : check_ok();
1813 : 0 : }
1814 : :
1815 : : /*
1816 : : * Callback function for processing results of query for
1817 : : * check_for_user_defined_encoding_conversions()'s UpgradeTask. If the query
1818 : : * returned any rows (i.e., the check failed), write the details to the report
1819 : : * file.
1820 : : */
1821 : : static void
756 nathan@postgresql.or 1822 : 0 : process_user_defined_encoding_conversions(DbInfo *dbinfo, PGresult *res, void *arg)
1823 : : {
1824 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1825 : 0 : int ntups = PQntuples(res);
1826 : 0 : int i_conoid = PQfnumber(res, "conoid");
1827 : 0 : int i_conname = PQfnumber(res, "conname");
1828 : 0 : int i_nspname = PQfnumber(res, "nspname");
1829 : :
746 1830 [ # # ]: 0 : if (ntups == 0)
756 1831 : 0 : return;
1832 : :
746 1833 [ # # ]: 0 : if (report->file == NULL &&
1834 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1835 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1836 : :
1837 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1838 : :
756 1839 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1840 : 0 : fprintf(report->file, " (oid=%s) %s.%s\n",
1841 : : PQgetvalue(res, rowno, i_conoid),
1842 : : PQgetvalue(res, rowno, i_nspname),
1843 : : PQgetvalue(res, rowno, i_conname));
1844 : : }
1845 : :
1846 : : /*
1847 : : * Verify that no user-defined encoding conversions exist.
1848 : : */
1849 : : static void
1850 : 0 : check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
1851 : : {
1852 : : UpgradeTaskReport report;
1853 : 0 : UpgradeTask *task = upgrade_task_create();
1854 : : const char *query;
1855 : :
1856 : 0 : prep_status("Checking for user-defined encoding conversions");
1857 : :
1858 : 0 : report.file = NULL;
1859 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1860 : : log_opts.basedir,
1861 : : "encoding_conversions.txt");
1862 : :
1863 : : /*
1864 : : * The query below hardcodes FirstNormalObjectId as 16384 rather than
1865 : : * interpolating that C #define into the query because, if that #define is
1866 : : * ever changed, the cutoff we want to use is the value used by
1867 : : * pre-version 14 servers, not that of some future version.
1868 : : */
1869 : 0 : query = "SELECT c.oid as conoid, c.conname, n.nspname "
1870 : : "FROM pg_catalog.pg_conversion c, "
1871 : : " pg_catalog.pg_namespace n "
1872 : : "WHERE c.connamespace = n.oid AND "
1873 : : " c.oid >= 16384";
1874 : :
1875 : 0 : upgrade_task_add_step(task, query,
1876 : : process_user_defined_encoding_conversions,
1877 : : true, &report);
1878 : 0 : upgrade_task_run(task, cluster);
1879 : 0 : upgrade_task_free(task);
1880 : :
1881 [ # # ]: 0 : if (report.file)
1882 : : {
1883 : 0 : fclose(report.file);
1553 tgl@sss.pgh.pa.us 1884 : 0 : pg_log(PG_REPORT, "fatal");
2020 heikki.linnakangas@i 1885 : 0 : pg_fatal("Your installation contains user-defined encoding conversions.\n"
1886 : : "The conversion function parameters changed in PostgreSQL version 14\n"
1887 : : "so this cluster cannot currently be upgraded. You can remove the\n"
1888 : : "encoding conversions in the old cluster and restart the upgrade.\n"
1889 : : "A list of user-defined encoding conversions is in the file:\n"
1890 : : " %s", report.path);
1891 : : }
1892 : : else
1893 : 0 : check_ok();
1894 : 0 : }
1895 : :
1896 : : /*
1897 : : * Callback function for processing results of query for
1898 : : * check_for_unicode_update()'s UpgradeTask. If the query returned any rows
1899 : : * (i.e., the check failed), write the details to the report file.
1900 : : */
1901 : : static void
557 jdavis@postgresql.or 1902 : 0 : process_unicode_update(DbInfo *dbinfo, PGresult *res, void *arg)
1903 : : {
1904 : 0 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
1905 : 0 : int ntups = PQntuples(res);
1906 : 0 : int i_reloid = PQfnumber(res, "reloid");
1907 : 0 : int i_nspname = PQfnumber(res, "nspname");
1908 : 0 : int i_relname = PQfnumber(res, "relname");
1909 : :
1910 [ # # ]: 0 : if (ntups == 0)
1911 : 0 : return;
1912 : :
1913 [ # # ]: 0 : if (report->file == NULL &&
1914 [ # # ]: 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
1915 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
1916 : :
1917 : 0 : fprintf(report->file, "In database: %s\n", dbinfo->db_name);
1918 : :
1919 [ # # ]: 0 : for (int rowno = 0; rowno < ntups; rowno++)
1920 : 0 : fprintf(report->file, " (oid=%s) %s.%s\n",
1921 : : PQgetvalue(res, rowno, i_reloid),
1922 : : PQgetvalue(res, rowno, i_nspname),
1923 : : PQgetvalue(res, rowno, i_relname));
1924 : : }
1925 : :
1926 : : /*
1927 : : * Check if the Unicode version built into Postgres changed between the old
1928 : : * cluster and the new cluster.
1929 : : */
1930 : : static bool
557 jdavis@postgresql.or 1931 :CBC 17 : unicode_version_changed(ClusterInfo *cluster)
1932 : : {
1933 : 17 : PGconn *conn_template1 = connectToServer(cluster, "template1");
1934 : : PGresult *res;
1935 : : char *old_unicode_version;
1936 : : bool unicode_updated;
1937 : :
1938 : 17 : res = executeQueryOrDie(conn_template1, "SELECT unicode_version()");
1939 : 17 : old_unicode_version = PQgetvalue(res, 0, 0);
1940 : 17 : unicode_updated = (strcmp(old_unicode_version, PG_UNICODE_VERSION) != 0);
1941 : :
1942 : 17 : PQclear(res);
1943 : 17 : PQfinish(conn_template1);
1944 : :
1945 : 17 : return unicode_updated;
1946 : : }
1947 : :
1948 : : /*
1949 : : * check_for_unicode_update()
1950 : : *
1951 : : * Check if the version of Unicode in the old server and the new server
1952 : : * differ. If so, check for indexes, partitioned tables, or constraints that
1953 : : * use expressions with functions dependent on Unicode behavior.
1954 : : */
1955 : : static void
1956 : 17 : check_for_unicode_update(ClusterInfo *cluster)
1957 : : {
1958 : : UpgradeTaskReport report;
1959 : : UpgradeTask *task;
1960 : : const char *query;
1961 : :
1962 : : /*
1963 : : * The builtin provider did not exist prior to version 17. While there are
1964 : : * still problems that could potentially be caught from earlier versions,
1965 : : * such as an index on NORMALIZE(), we don't check for that here.
1966 : : */
1967 [ - + ]: 17 : if (GET_MAJOR_VERSION(cluster->major_version) < 1700)
1968 : 17 : return;
1969 : :
1970 : 17 : prep_status("Checking for objects affected by Unicode update");
1971 : :
1972 [ + - ]: 17 : if (!unicode_version_changed(cluster))
1973 : : {
1974 : 17 : check_ok();
1975 : 17 : return;
1976 : : }
1977 : :
557 jdavis@postgresql.or 1978 :UBC 0 : report.file = NULL;
1979 : 0 : snprintf(report.path, sizeof(report.path), "%s/%s",
1980 : : log_opts.basedir,
1981 : : "unicode_dependent_rels.txt");
1982 : :
1983 : 0 : query =
1984 : : /* collations that use built-in Unicode for character semantics */
1985 : : "WITH collations(collid) AS ( "
1986 : : " SELECT oid FROM pg_collation "
1987 : : " WHERE collprovider='b' AND colllocale IN ('C.UTF-8','PG_UNICODE_FAST') "
1988 : : /* include default collation, if appropriate */
1989 : : " UNION "
1990 : : " SELECT 'pg_catalog.default'::regcollation FROM pg_database "
1991 : : " WHERE datname = current_database() AND "
1992 : : " datlocprovider='b' AND datlocale IN ('C.UTF-8','PG_UNICODE_FAST') "
1993 : : "), "
1994 : : /* functions that use built-in Unicode */
1995 : : "functions(procid) AS ( "
1996 : : " SELECT proc.oid FROM pg_proc proc "
1997 : : " WHERE proname IN ('normalize','unicode_assigned','unicode_version','is_normalized') AND "
1998 : : " pronamespace='pg_catalog'::regnamespace "
1999 : : "), "
2000 : : /* operators that use the input collation for character semantics */
2001 : : "coll_operators(operid, procid, collid) AS ( "
2002 : : " SELECT oper.oid, oper.oprcode, collid FROM pg_operator oper, collations "
2003 : : " WHERE oprname IN ('~', '~*', '!~', '!~*', '~~*', '!~~*') AND "
2004 : : " oprnamespace='pg_catalog'::regnamespace AND "
2005 : : " oprright='pg_catalog.text'::pg_catalog.regtype "
2006 : : "), "
2007 : : /* functions that use the input collation for character semantics */
2008 : : "coll_functions(procid, collid) AS ( "
2009 : : " SELECT proc.oid, collid FROM pg_proc proc, collations "
2010 : : " WHERE pronamespace='pg_catalog'::regnamespace AND "
2011 : : " ((proname IN ('lower','initcap','upper','casefold') AND "
2012 : : " pronargs = 1 AND "
2013 : : " proargtypes[0] = 'pg_catalog.text'::pg_catalog.regtype) OR "
2014 : : " (proname = 'substring' AND pronargs = 2 AND "
2015 : : " proargtypes[0] = 'pg_catalog.text'::pg_catalog.regtype AND "
2016 : : " proargtypes[1] = 'pg_catalog.text'::pg_catalog.regtype) OR "
2017 : : " proname LIKE 'regexp_%') "
2018 : : /* include functions behind the operators listed above */
2019 : : " UNION "
2020 : : " SELECT procid, collid FROM coll_operators "
2021 : : "), "
2022 : :
2023 : : /*
2024 : : * Generate patterns to search a pg_node_tree for the above functions and
2025 : : * operators.
2026 : : */
2027 : : "patterns(p) AS ( "
2028 : : " SELECT '{FUNCEXPR :funcid ' || procid::text || '[ }]' FROM functions "
2029 : : " UNION "
2030 : : " SELECT '{OPEXPR :opno ' || operid::text || ' (:\\w+ \\w+ )*' || "
2031 : : " ':inputcollid ' || collid::text || '[ }]' FROM coll_operators "
2032 : : " UNION "
2033 : : " SELECT '{FUNCEXPR :funcid ' || procid::text || ' (:\\w+ \\w+ )*' || "
2034 : : " ':inputcollid ' || collid::text || '[ }]' FROM coll_functions "
2035 : : ") "
2036 : :
2037 : : /*
2038 : : * Match the patterns against expressions used for relation contents.
2039 : : */
2040 : : "SELECT reloid, relkind, nspname, relname "
2041 : : " FROM ( "
2042 : : " SELECT conrelid "
2043 : : " FROM pg_constraint, patterns WHERE conbin::text ~ p "
2044 : : " UNION "
2045 : : " SELECT indexrelid "
2046 : : " FROM pg_index, patterns WHERE indexprs::text ~ p OR indpred::text ~ p "
2047 : : " UNION "
2048 : : " SELECT partrelid "
2049 : : " FROM pg_partitioned_table, patterns WHERE partexprs::text ~ p "
2050 : : " UNION "
2051 : : " SELECT ev_class "
2052 : : " FROM pg_rewrite, pg_class, patterns "
2053 : : " WHERE ev_class = pg_class.oid AND relkind = 'm' AND ev_action::text ~ p"
2054 : : " ) s(reloid), pg_class c, pg_namespace n, pg_database d "
2055 : : " WHERE s.reloid = c.oid AND c.relnamespace = n.oid AND "
2056 : : " d.datname = current_database() AND "
2057 : : " d.encoding = pg_char_to_encoding('UTF8');";
2058 : :
554 nathan@postgresql.or 2059 : 0 : task = upgrade_task_create();
557 jdavis@postgresql.or 2060 : 0 : upgrade_task_add_step(task, query,
2061 : : process_unicode_update,
2062 : : true, &report);
2063 : 0 : upgrade_task_run(task, cluster);
2064 : 0 : upgrade_task_free(task);
2065 : :
2066 [ # # ]: 0 : if (report.file)
2067 : : {
2068 : 0 : fclose(report.file);
2069 : 0 : report_status(PG_WARNING, "warning");
483 peter@eisentraut.org 2070 : 0 : pg_log(PG_WARNING, "Your installation contains relations that might be affected by a new version of Unicode.\n"
2071 : : "A list of potentially-affected relations is in the file:\n"
2072 : : " %s", report.path);
2073 : : }
2074 : : else
557 jdavis@postgresql.or 2075 : 0 : check_ok();
2076 : : }
2077 : :
2078 : : /*
2079 : : * check_new_cluster_replication_slots()
2080 : : *
2081 : : * Validate the new cluster's readiness for migrating replication slots:
2082 : : * - Ensures no existing logical replication slots in the new cluster when
2083 : : * migrating logical slots.
2084 : : * - Ensure conflict detection slot does not exist in the new cluster when
2085 : : * migrating subscriptions with retain_dead_tuples enabled.
2086 : : * - Ensure that the parameter settings in the new cluster necessary for
2087 : : * creating slots are sufficient.
2088 : : */
2089 : : static void
446 akapila@postgresql.o 2090 :CBC 16 : check_new_cluster_replication_slots(void)
2091 : : {
2092 : : PGresult *res;
2093 : : PGconn *conn;
2094 : : int nslots_on_old;
2095 : : int nslots_on_new;
2096 : : int rdt_slot_on_new;
2097 : : int max_replication_slots;
2098 : : char *output_plugin_libraries;
2099 : : char *wal_level;
2100 : : int i_nslots_on_new;
2101 : : int i_rdt_slot_on_new;
2102 : :
2103 : : /*
2104 : : * Logical slots can be migrated since PG17 and a physical slot
2105 : : * CONFLICT_DETECTION_SLOT can be migrated since PG19.
2106 : : */
1082 2107 [ - + ]: 16 : if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1600)
1082 akapila@postgresql.o 2108 :UBC 0 : return;
2109 : :
1082 akapila@postgresql.o 2110 :CBC 16 : nslots_on_old = count_old_cluster_logical_slots();
2111 : :
2112 : : /*
2113 : : * Quick return if there are no slots to be migrated and no subscriptions
2114 : : * have the retain_dead_tuples option enabled.
2115 : : */
446 2116 [ + + + + ]: 16 : if (nslots_on_old == 0 && !old_cluster.sub_retain_dead_tuples)
1082 2117 : 11 : return;
2118 : :
2119 : 5 : conn = connectToServer(&new_cluster, "template1");
2120 : :
154 peter@eisentraut.org 2121 : 5 : prep_status("Checking new cluster replication slots");
2122 : :
446 akapila@postgresql.o 2123 [ + + ]: 5 : res = executeQueryOrDie(conn, "SELECT %s AS nslots_on_new, %s AS rdt_slot_on_new "
2124 : : "FROM pg_catalog.pg_replication_slots",
2125 : : nslots_on_old > 0
2126 : : ? "COUNT(*) FILTER (WHERE slot_type = 'logical' AND temporary IS FALSE)"
2127 : : : "0",
2128 [ + + ]: 5 : old_cluster.sub_retain_dead_tuples
2129 : : ? "COUNT(*) FILTER (WHERE slot_name = 'pg_conflict_detection')"
2130 : : : "0");
2131 : :
1082 2132 [ - + ]: 5 : if (PQntuples(res) != 1)
446 akapila@postgresql.o 2133 :UBC 0 : pg_fatal("could not count the number of replication slots");
2134 : :
446 akapila@postgresql.o 2135 :CBC 5 : i_nslots_on_new = PQfnumber(res, "nslots_on_new");
2136 : 5 : i_rdt_slot_on_new = PQfnumber(res, "rdt_slot_on_new");
2137 : :
2138 : 5 : nslots_on_new = atoi(PQgetvalue(res, 0, i_nslots_on_new));
2139 : :
1082 2140 [ - + ]: 5 : if (nslots_on_new)
2141 : : {
446 akapila@postgresql.o 2142 [ # # ]:UBC 0 : Assert(nslots_on_old);
777 peter@eisentraut.org 2143 : 0 : pg_fatal("expected 0 logical replication slots but found %d",
2144 : : nslots_on_new);
2145 : : }
2146 : :
446 akapila@postgresql.o 2147 :CBC 5 : rdt_slot_on_new = atoi(PQgetvalue(res, 0, i_rdt_slot_on_new));
2148 : :
2149 [ - + ]: 5 : if (rdt_slot_on_new)
2150 : : {
446 akapila@postgresql.o 2151 [ # # ]:UBC 0 : Assert(old_cluster.sub_retain_dead_tuples);
154 peter@eisentraut.org 2152 : 0 : pg_fatal("replication slot \"%s\" already exists in the new cluster", "pg_conflict_detection");
2153 : : }
2154 : :
1082 akapila@postgresql.o 2155 :CBC 5 : PQclear(res);
2156 : :
2157 : 5 : res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
2158 : : "WHERE name IN ('wal_level', 'output_plugin_libraries', 'max_replication_slots') "
2159 : : "ORDER BY name DESC;");
2160 : :
63 jchampion@postgresql 2161 [ - + ]: 5 : if (PQntuples(res) != 3)
1082 akapila@postgresql.o 2162 :UBC 0 : pg_fatal("could not determine parameter settings on new cluster");
2163 : :
1082 akapila@postgresql.o 2164 :CBC 5 : wal_level = PQgetvalue(res, 0, 0);
2165 : :
293 msawada@postgresql.o 2166 [ + + + - ]: 5 : if ((nslots_on_old > 0 || old_cluster.sub_retain_dead_tuples) &&
446 akapila@postgresql.o 2167 [ - + ]: 5 : strcmp(wal_level, "minimal") == 0)
446 akapila@postgresql.o 2168 :UBC 0 : pg_fatal("\"wal_level\" must be \"replica\" or \"logical\" but is set to \"%s\"",
2169 : : wal_level);
2170 : :
63 jchampion@postgresql 2171 :CBC 5 : output_plugin_libraries = PQgetvalue(res, 1, 0);
2172 : :
2173 : : /*
2174 : : * Make sure the output_plugin_libraries setting covers all plugins needed
2175 : : * by any migrated slots.
2176 : : */
2177 [ + + ]: 5 : if (nslots_on_old > 0)
2178 : : {
2179 : 3 : char *guc_copy = pg_strdup(output_plugin_libraries);
2180 : : char **allowed_plugins;
2181 : : char output_path[MAXPGPATH];
2182 : 3 : FILE *script = NULL;
2183 : :
2184 [ - + ]: 3 : if (!SplitGUCList(guc_copy, ',', &allowed_plugins))
2185 : : {
2186 : : /*
2187 : : * Should not happen. (Frontend and backend GUC_LIST_QUOTE parsing
2188 : : * have to remain compatible for pg_dump at minimum.)
2189 : : */
63 jchampion@postgresql 2190 :UBC 0 : pg_fatal("could not parse \"output_plugin_libraries\" setting '%s'",
2191 : : output_plugin_libraries);
2192 : : }
2193 : :
63 jchampion@postgresql 2194 :CBC 3 : snprintf(output_path, sizeof(output_path), "%s/%s",
2195 : : log_opts.basedir,
2196 : : "disallowed_output_plugins.txt");
2197 : :
2198 [ + + ]: 9 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
2199 : : {
2200 : 6 : LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
2201 : :
2202 [ + + ]: 13 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
2203 : : {
2204 : 7 : LogicalSlotInfo *slot = &slot_arr->slots[slotnum];
2205 : 7 : bool allowed = false;
2206 : :
2207 : : /*
2208 : : * We expect the output_plugin_libraries length to be small in
2209 : : * practice; O(n*m) shouldn't be a problem here.
2210 : : */
2211 [ + + ]: 11 : for (char **p = allowed_plugins; *p; p++)
2212 : : {
2213 [ + + ]: 9 : if (strcmp(slot->plugin, *p) == 0)
2214 : : {
2215 : 5 : allowed = true;
2216 : 5 : break;
2217 : : }
2218 : : }
2219 : :
2220 [ + + ]: 7 : if (!allowed)
2221 : : {
2222 [ + + - + ]: 3 : if (script == NULL &&
2223 : 1 : (script = fopen_priv(output_path, "w")) == NULL)
63 jchampion@postgresql 2224 :UBC 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2225 : :
63 jchampion@postgresql 2226 :CBC 2 : fprintf(script, "The slot \"%s\" uses plugin \"%s\"\n",
2227 : : slot->slotname, slot->plugin);
2228 : : }
2229 : : }
2230 : : }
2231 : :
2232 [ + + ]: 3 : if (script)
2233 : : {
2234 : 1 : fclose(script);
2235 : :
2236 : 1 : pg_log(PG_REPORT, "fatal");
2237 : 1 : pg_fatal("Your installation contains logical replication slots with plugins\n"
2238 : : "that are not allowed by the new cluster's output_plugin_libraries\n"
2239 : : "setting. You can add trusted plugins to output_plugin_libraries\n"
2240 : : "and/or remove affected slots, and then restart the upgrade.\n"
2241 : : "A list of the problematic slots is in the file:\n"
2242 : : " %s", output_path);
2243 : : }
2244 : :
2245 : 2 : pg_free(allowed_plugins);
2246 : 2 : pg_free(guc_copy);
2247 : : }
2248 : :
2249 : 4 : max_replication_slots = atoi(PQgetvalue(res, 2, 0));
2250 : :
446 akapila@postgresql.o 2251 [ + + ]: 4 : if (old_cluster.sub_retain_dead_tuples &&
2252 [ + + ]: 2 : nslots_on_old + 1 > max_replication_slots)
2253 : 1 : pg_fatal("\"max_replication_slots\" (%d) must be greater than or equal to the number of "
2254 : : "logical replication slots in the old cluster plus one additional slot required "
2255 : : "for retaining conflict detection information (%d)",
2256 : : max_replication_slots, nslots_on_old + 1);
2257 : :
1082 2258 [ + + ]: 3 : if (nslots_on_old > max_replication_slots)
878 peter@eisentraut.org 2259 : 1 : pg_fatal("\"max_replication_slots\" (%d) must be greater than or equal to the number of "
2260 : : "logical replication slots (%d) in the old cluster",
2261 : : max_replication_slots, nslots_on_old);
2262 : :
1082 akapila@postgresql.o 2263 : 2 : PQclear(res);
2264 : 2 : PQfinish(conn);
2265 : :
2266 : 2 : check_ok();
2267 : : }
2268 : :
2269 : : /*
2270 : : * check_new_cluster_subscription_configuration()
2271 : : *
2272 : : * Verify that the max_active_replication_origins configuration specified is
2273 : : * enough for creating the subscriptions. This is required to create the
2274 : : * replication origin for each subscription.
2275 : : */
2276 : : static void
1014 2277 : 13 : check_new_cluster_subscription_configuration(void)
2278 : : {
2279 : : PGresult *res;
2280 : : PGconn *conn;
2281 : : int max_active_replication_origins;
2282 : :
2283 : : /* Subscriptions and their dependencies can be migrated since PG17. */
2284 [ - + ]: 13 : if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
1014 akapila@postgresql.o 2285 :UBC 0 : return;
2286 : :
2287 : : /* Quick return if there are no subscriptions to be migrated. */
810 nathan@postgresql.or 2288 [ + + ]:CBC 13 : if (old_cluster.nsubs == 0)
1014 akapila@postgresql.o 2289 : 11 : return;
2290 : :
154 peter@eisentraut.org 2291 : 2 : prep_status("Checking new cluster configuration for subscriptions");
2292 : :
1014 akapila@postgresql.o 2293 : 2 : conn = connectToServer(&new_cluster, "template1");
2294 : :
2295 : 2 : res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
2296 : : "WHERE name = 'max_active_replication_origins';");
2297 : :
2298 [ - + ]: 2 : if (PQntuples(res) != 1)
1014 akapila@postgresql.o 2299 :UBC 0 : pg_fatal("could not determine parameter settings on new cluster");
2300 : :
570 msawada@postgresql.o 2301 :CBC 2 : max_active_replication_origins = atoi(PQgetvalue(res, 0, 0));
2302 [ + + ]: 2 : if (old_cluster.nsubs > max_active_replication_origins)
2303 : 1 : pg_fatal("\"max_active_replication_origins\" (%d) must be greater than or equal to the number of "
2304 : : "subscriptions (%d) in the old cluster",
2305 : : max_active_replication_origins, old_cluster.nsubs);
2306 : :
1014 akapila@postgresql.o 2307 : 1 : PQclear(res);
2308 : 1 : PQfinish(conn);
2309 : :
2310 : 1 : check_ok();
2311 : : }
2312 : :
2313 : : /*
2314 : : * check_old_cluster_for_valid_slots()
2315 : : *
2316 : : * Verify that all the logical slots are valid and have consumed all the WAL
2317 : : * before shutdown.
2318 : : */
2319 : : static void
808 nathan@postgresql.or 2320 : 19 : check_old_cluster_for_valid_slots(void)
2321 : : {
2322 : : char output_path[MAXPGPATH];
1082 akapila@postgresql.o 2323 : 19 : FILE *script = NULL;
2324 : :
154 peter@eisentraut.org 2325 : 19 : prep_status("Checking logical replication slots");
2326 : :
1082 akapila@postgresql.o 2327 : 19 : snprintf(output_path, sizeof(output_path), "%s/%s",
2328 : : log_opts.basedir,
2329 : : "invalid_logical_slots.txt");
2330 : :
2331 [ + + ]: 79 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
2332 : : {
2333 : 60 : LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
2334 : :
2335 [ + + ]: 70 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
2336 : : {
2337 : 10 : LogicalSlotInfo *slot = &slot_arr->slots[slotnum];
2338 : :
2339 : : /* Is the slot usable? */
2340 [ - + ]: 10 : if (slot->invalid)
2341 : : {
1082 akapila@postgresql.o 2342 [ # # # # ]:UBC 0 : if (script == NULL &&
2343 : 0 : (script = fopen_priv(output_path, "w")) == NULL)
944 michael@paquier.xyz 2344 : 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2345 : :
1082 akapila@postgresql.o 2346 : 0 : fprintf(script, "The slot \"%s\" is invalid\n",
2347 : : slot->slotname);
2348 : :
2349 : 0 : continue;
2350 : : }
2351 : :
2352 : : /*
2353 : : * Do additional check to ensure that all logical replication
2354 : : * slots have consumed all the WAL before shutdown.
2355 : : *
2356 : : * Note: This can be satisfied only when the old cluster has been
2357 : : * shut down, so we skip this for live checks.
2358 : : */
808 nathan@postgresql.or 2359 [ + - + + ]:CBC 10 : if (!user_opts.live_check && !slot->caught_up)
2360 : : {
1082 akapila@postgresql.o 2361 [ + + - + ]: 3 : if (script == NULL &&
2362 : 1 : (script = fopen_priv(output_path, "w")) == NULL)
944 michael@paquier.xyz 2363 :UBC 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2364 : :
1082 akapila@postgresql.o 2365 :CBC 2 : fprintf(script,
2366 : : "The slot \"%s\" has not consumed the WAL yet\n",
2367 : : slot->slotname);
2368 : : }
2369 : :
2370 : : /*
2371 : : * The name "pg_conflict_detection" (defined as
2372 : : * CONFLICT_DETECTION_SLOT) has been reserved for logical
2373 : : * replication conflict detection slot since PG19.
2374 : : */
446 2375 [ - + ]: 10 : if (strcmp(slot->slotname, "pg_conflict_detection") == 0)
2376 : : {
446 akapila@postgresql.o 2377 [ # # # # ]:UBC 0 : if (script == NULL &&
2378 : 0 : (script = fopen_priv(output_path, "w")) == NULL)
2379 : 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2380 : :
2381 : 0 : fprintf(script,
2382 : : "The slot name \"%s\" is reserved\n",
2383 : : slot->slotname);
2384 : : }
2385 : : }
2386 : : }
2387 : :
1082 akapila@postgresql.o 2388 [ + + ]:CBC 19 : if (script)
2389 : : {
2390 : 1 : fclose(script);
2391 : :
2392 : 1 : pg_log(PG_REPORT, "fatal");
777 peter@eisentraut.org 2393 : 1 : pg_fatal("Your installation contains logical replication slots that cannot be upgraded.\n"
2394 : : "You can remove invalid slots and/or consume the pending WAL for other slots,\n"
2395 : : "and then restart the upgrade.\n"
2396 : : "A list of the problematic slots is in the file:\n"
2397 : : " %s", output_path);
2398 : : }
2399 : :
1082 akapila@postgresql.o 2400 : 18 : check_ok();
2401 : 18 : }
2402 : :
2403 : : /*
2404 : : * Callback function for processing results of query for
2405 : : * check_old_cluster_subscription_state()'s UpgradeTask. If the query returned
2406 : : * any rows (i.e., the check failed), write the details to the report file.
2407 : : */
2408 : : static void
756 nathan@postgresql.or 2409 : 58 : process_old_sub_state_check(DbInfo *dbinfo, PGresult *res, void *arg)
2410 : : {
2411 : 58 : UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
746 2412 : 58 : int ntups = PQntuples(res);
756 2413 : 58 : int i_srsubstate = PQfnumber(res, "srsubstate");
2414 : 58 : int i_subname = PQfnumber(res, "subname");
2415 : 58 : int i_nspname = PQfnumber(res, "nspname");
2416 : 58 : int i_relname = PQfnumber(res, "relname");
2417 : :
746 2418 [ + + ]: 58 : if (ntups == 0)
2419 : 57 : return;
2420 : :
2421 [ - + ]: 1 : if (report->file == NULL &&
746 nathan@postgresql.or 2422 [ # # ]:UBC 0 : (report->file = fopen_priv(report->path, "w")) == NULL)
2423 : 0 : pg_fatal("could not open file \"%s\": %m", report->path);
2424 : :
746 nathan@postgresql.or 2425 [ + + ]:CBC 2 : for (int i = 0; i < ntups; i++)
756 2426 : 1 : fprintf(report->file, "The table sync state \"%s\" is not allowed for database:\"%s\" subscription:\"%s\" schema:\"%s\" relation:\"%s\"\n",
2427 : : PQgetvalue(res, i, i_srsubstate),
2428 : : dbinfo->db_name,
2429 : : PQgetvalue(res, i, i_subname),
2430 : : PQgetvalue(res, i, i_nspname),
2431 : : PQgetvalue(res, i, i_relname));
2432 : : }
2433 : :
2434 : : /*
2435 : : * check_old_cluster_subscription_state()
2436 : : *
2437 : : * Verify that the replication origin corresponding to each of the
2438 : : * subscriptions are present and each of the subscribed tables is in
2439 : : * 'i' (initialize) or 'r' (ready) state.
2440 : : */
2441 : : static void
1014 akapila@postgresql.o 2442 : 18 : check_old_cluster_subscription_state(void)
2443 : : {
756 nathan@postgresql.or 2444 : 18 : UpgradeTask *task = upgrade_task_create();
2445 : : UpgradeTaskReport report;
2446 : : const char *query;
2447 : : PGresult *res;
2448 : : PGconn *conn;
2449 : : int ntup;
2450 : :
154 peter@eisentraut.org 2451 : 18 : prep_status("Checking subscription state");
2452 : :
756 nathan@postgresql.or 2453 : 18 : report.file = NULL;
2454 : 18 : snprintf(report.path, sizeof(report.path), "%s/%s",
2455 : : log_opts.basedir,
2456 : : "subs_invalid.txt");
2457 : :
2458 : : /*
2459 : : * Check that all the subscriptions have their respective replication
2460 : : * origin. This check only needs to run once.
2461 : : */
2462 : 18 : conn = connectToServer(&old_cluster, old_cluster.dbarr.dbs[0].db_name);
2463 : 18 : res = executeQueryOrDie(conn,
2464 : : "SELECT d.datname, s.subname "
2465 : : "FROM pg_catalog.pg_subscription s "
2466 : : "LEFT OUTER JOIN pg_catalog.pg_replication_origin o "
2467 : : " ON o.roname = 'pg_' || s.oid "
2468 : : "INNER JOIN pg_catalog.pg_database d "
2469 : : " ON d.oid = s.subdbid "
2470 : : "WHERE o.roname IS NULL;");
2471 : 18 : ntup = PQntuples(res);
2472 [ + + ]: 19 : for (int i = 0; i < ntup; i++)
2473 : : {
2474 [ + - ]: 1 : if (report.file == NULL &&
2475 [ - + ]: 1 : (report.file = fopen_priv(report.path, "w")) == NULL)
756 nathan@postgresql.or 2476 :UBC 0 : pg_fatal("could not open file \"%s\": %m", report.path);
756 nathan@postgresql.or 2477 :CBC 1 : fprintf(report.file, "The replication origin is missing for database:\"%s\" subscription:\"%s\"\n",
2478 : : PQgetvalue(res, i, 0),
2479 : : PQgetvalue(res, i, 1));
2480 : : }
2481 : 18 : PQclear(res);
2482 : 18 : PQfinish(conn);
2483 : :
2484 : : /*
2485 : : * We don't allow upgrade if there is a risk of dangling slot or origin
2486 : : * corresponding to initial sync after upgrade.
2487 : : *
2488 : : * A slot/origin not created yet refers to the 'i' (initialize) state,
2489 : : * while 'r' (ready) state refers to a slot/origin created previously but
2490 : : * already dropped. These states are supported for pg_upgrade. The other
2491 : : * states listed below are not supported:
2492 : : *
2493 : : * a) SUBREL_STATE_DATASYNC: A relation upgraded while in this state would
2494 : : * retain a replication slot and origin. The sync worker spawned after the
2495 : : * upgrade cannot drop them because the subscription ID used for the slot
2496 : : * and origin name no longer matches.
2497 : : *
2498 : : * b) SUBREL_STATE_SYNCDONE: A relation upgraded while in this state would
2499 : : * retain the replication origin when there is a failure in tablesync
2500 : : * worker immediately after dropping the replication slot in the
2501 : : * publisher.
2502 : : *
2503 : : * c) SUBREL_STATE_FINISHEDCOPY: A tablesync worker spawned to work on a
2504 : : * relation upgraded while in this state would expect an origin ID with
2505 : : * the OID of the subscription used before the upgrade, causing it to
2506 : : * fail.
2507 : : *
2508 : : * d) SUBREL_STATE_SYNCWAIT, SUBREL_STATE_CATCHUP and
2509 : : * SUBREL_STATE_UNKNOWN: These states are not stored in the catalog, so we
2510 : : * need not allow these states.
2511 : : */
2512 : 18 : query = "SELECT r.srsubstate, s.subname, n.nspname, c.relname "
2513 : : "FROM pg_catalog.pg_subscription_rel r "
2514 : : "LEFT JOIN pg_catalog.pg_subscription s"
2515 : : " ON r.srsubid = s.oid "
2516 : : "LEFT JOIN pg_catalog.pg_class c"
2517 : : " ON r.srrelid = c.oid "
2518 : : "LEFT JOIN pg_catalog.pg_namespace n"
2519 : : " ON c.relnamespace = n.oid "
2520 : : "WHERE r.srsubstate NOT IN ('i', 'r') "
2521 : : "ORDER BY s.subname";
2522 : :
2523 : 18 : upgrade_task_add_step(task, query, process_old_sub_state_check,
2524 : : true, &report);
2525 : :
2526 : 18 : upgrade_task_run(task, &old_cluster);
2527 : 18 : upgrade_task_free(task);
2528 : :
2529 [ + + ]: 18 : if (report.file)
2530 : : {
2531 : 1 : fclose(report.file);
1014 akapila@postgresql.o 2532 : 1 : pg_log(PG_REPORT, "fatal");
2533 : 1 : pg_fatal("Your installation contains subscriptions without origin or having relations not in i (initialize) or r (ready) state.\n"
2534 : : "You can allow the initial sync to finish for all relations and then restart the upgrade.\n"
2535 : : "A list of the problematic subscriptions is in the file:\n"
2536 : : " %s", report.path);
2537 : : }
2538 : : else
2539 : 17 : check_ok();
2540 : 17 : }
2541 : :
2542 : : /*
2543 : : * check_old_cluster_global_names()
2544 : : *
2545 : : * Raise an error if any database, role, or tablespace name contains a newline
2546 : : * or carriage return character. Such names are not allowed in v19 and later.
2547 : : */
2548 : : static void
233 andrew@dunslane.net 2549 :UBC 0 : check_old_cluster_global_names(ClusterInfo *cluster)
2550 : : {
2551 : : int i;
2552 : : PGconn *conn_template1;
2553 : : PGresult *res;
2554 : : int ntups;
231 2555 : 0 : FILE *script = NULL;
2556 : : char output_path[MAXPGPATH];
233 2557 : 0 : int count = 0;
2558 : :
154 peter@eisentraut.org 2559 : 0 : prep_status("Checking names of databases, roles, and tablespaces");
2560 : :
233 andrew@dunslane.net 2561 : 0 : snprintf(output_path, sizeof(output_path), "%s/%s",
2562 : : log_opts.basedir,
2563 : : "db_role_tablespace_invalid_names.txt");
2564 : :
2565 : 0 : conn_template1 = connectToServer(cluster, "template1");
2566 : :
2567 : : /*
2568 : : * Get database, user/role and tablespace names from cluster. Can't use
2569 : : * pg_authid because only superusers can view it.
2570 : : */
2571 : 0 : res = executeQueryOrDie(conn_template1,
2572 : : "SELECT datname AS objname, 'database' AS objtype "
2573 : : "FROM pg_catalog.pg_database UNION ALL "
2574 : : "SELECT rolname AS objname, 'role' AS objtype "
2575 : : "FROM pg_catalog.pg_roles UNION ALL "
2576 : : "SELECT spcname AS objname, 'tablespace' AS objtype "
2577 : : "FROM pg_catalog.pg_tablespace ORDER BY 2 ");
2578 : :
2579 : 0 : ntups = PQntuples(res);
2580 [ # # ]: 0 : for (i = 0; i < ntups; i++)
2581 : : {
231 2582 : 0 : char *objname = PQgetvalue(res, i, 0);
2583 : 0 : char *objtype = PQgetvalue(res, i, 1);
2584 : :
2585 : : /* If name has \n or \r, then report it. */
233 2586 [ # # ]: 0 : if (strpbrk(objname, "\n\r"))
2587 : : {
2588 [ # # # # ]: 0 : if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
2589 : 0 : pg_fatal("could not open file \"%s\": %m", output_path);
2590 : :
2591 : 0 : fprintf(script, "%d : %s name = \"%s\"\n", ++count, objtype, objname);
2592 : : }
2593 : : }
2594 : :
2595 : 0 : PQclear(res);
2596 : 0 : PQfinish(conn_template1);
2597 : :
2598 [ # # ]: 0 : if (script)
2599 : : {
2600 : 0 : fclose(script);
2601 : 0 : pg_log(PG_REPORT, "fatal");
105 peter@eisentraut.org 2602 : 0 : pg_fatal("Your installation contains databases, roles, or tablespaces with names\n"
2603 : : "with invalid characters (newline or carriage return). To fix this,\n"
2604 : : "rename these objects.\n"
2605 : : "A list of all objects with invalid names is in the file:\n"
2606 : : " %s", output_path);
2607 : : }
2608 : : else
233 andrew@dunslane.net 2609 : 0 : check_ok();
2610 : 0 : }
2611 : :
2612 : : /*
2613 : : * check_for_oldestxid_consistency()
2614 : : *
2615 : : * Check that the oldestXid and oldestMultiXID values in the control file are
2616 : : * consistent with the 'datfrozenxid' and 'datminmxid' values in pg_database.
2617 : : *
2618 : : * The invariant is that all 'datfrozenxid' and 'datminmxid' values must be
2619 : : * greater than or equal to the values in the control file. Otherwise you
2620 : : * might already have truncated away clog or multixids that are still needed.
2621 : : * If that has happened, we refuse the upgrade and require the administrator
2622 : : * to deal with the situation first.
2623 : : *
2624 : : * One scenario where that is known to happen is if the cluster was upgraded
2625 : : * in the past to version 9.3 with a buggy pg_upgrade version that didn't copy
2626 : : * the oldestMulti value from the old cluster. See commit a61daa14d5.
2627 : : * That was a long time ago, though, so you're not very likely to encounter
2628 : : * that bug in the wild anymore. Therefore we don't assume that's the cause
2629 : : * or try to do anything clever here. In any case, it's still good to check
2630 : : * to prevent further damage.
2631 : : */
2632 : : static void
23 heikki.linnakangas@i 2633 :CBC 19 : check_for_oldestxid_consistency(ClusterInfo *cluster)
2634 : : {
2635 : : PGconn *conn_template1;
2636 : : PGresult *dbres;
2637 : : int ntups;
2638 : : int i_datname;
2639 : : int i_datfrozenxid;
2640 : : int i_datminmxid;
2641 : :
2642 : 19 : prep_status("Checking oldestXID and oldestMultiXid consistency");
2643 : :
2644 : 19 : conn_template1 = connectToServer(cluster, "template1");
2645 : :
2646 : 19 : dbres = executeQueryOrDie(conn_template1,
2647 : : "SELECT datname, datfrozenxid, datminmxid "
2648 : : "FROM pg_catalog.pg_database");
2649 : :
2650 : 19 : i_datname = PQfnumber(dbres, "datname");
2651 : 19 : i_datfrozenxid = PQfnumber(dbres, "datfrozenxid");
2652 : 19 : i_datminmxid = PQfnumber(dbres, "datminmxid");
2653 : :
2654 : 19 : ntups = PQntuples(dbres);
2655 [ + + ]: 98 : for (int dbnum = 0; dbnum < ntups; dbnum++)
2656 : : {
2657 : 79 : char *datname = PQgetvalue(dbres, dbnum, i_datname);
2658 : 79 : TransactionId datfrozenxid = (TransactionId) str2uint(PQgetvalue(dbres, dbnum, i_datfrozenxid));
2659 : 79 : MultiXactId datminmxid = (MultiXactId) str2uint(PQgetvalue(dbres, dbnum, i_datminmxid));
2660 : :
2661 [ - + ]: 79 : if (TransactionIdPrecedes(datfrozenxid, cluster->controldata.chkpnt_oldstxid))
2662 : : {
23 heikki.linnakangas@i 2663 :UBC 0 : pg_fatal("oldestXID (%u) in the control file is newer than the datfrozenxid (%u) of database \"%s\"",
2664 : : cluster->controldata.chkpnt_oldstxid, datfrozenxid, datname);
2665 : : }
23 heikki.linnakangas@i 2666 [ - + ]:CBC 79 : if (MultiXactIdPrecedes(datminmxid, cluster->controldata.chkpnt_oldstMulti))
2667 : : {
23 heikki.linnakangas@i 2668 :UBC 0 : pg_fatal("oldestMultiXid (%u) in control file is newer than the datminmxid (%u) of database \"%s\"",
2669 : : cluster->controldata.chkpnt_oldstMulti, datminmxid, datname);
2670 : : }
2671 : : }
2672 : :
23 heikki.linnakangas@i 2673 :CBC 19 : PQclear(dbres);
2674 : 19 : PQfinish(conn_template1);
2675 : :
2676 : 19 : check_ok();
23 heikki.linnakangas@i 2677 :GIC 19 : }
|