4 * Postgres-version-specific routines
6 * Copyright (c) 2010-2019, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/version.c
10 #include "postgres_fe.h"
12 #include "catalog/pg_class_d.h"
13 #include "fe_utils/string_utils.h"
14 #include "pg_upgrade.h"
17 * new_9_0_populate_pg_largeobject_metadata()
18 * new >= 9.0, old <= 8.4
19 * 9.0 has a new pg_largeobject permission table
22 new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster, bool check_mode)
27 char output_path[MAXPGPATH];
29 prep_status("Checking for large objects");
31 snprintf(output_path, sizeof(output_path), "pg_largeobject.sql");
33 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
37 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
38 PGconn *conn = connectToServer(cluster, active_db->db_name);
40 /* find if there are any large objects */
41 res = executeQueryOrDie(conn,
43 "FROM pg_catalog.pg_largeobject ");
45 i_count = PQfnumber(res, "count");
46 if (atoi(PQgetvalue(res, 0, i_count)) != 0)
51 PQExpBufferData connectbuf;
53 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
54 pg_fatal("could not open file \"%s\": %s\n", output_path,
57 initPQExpBuffer(&connectbuf);
58 appendPsqlMetaConnect(&connectbuf, active_db->db_name);
59 fputs(connectbuf.data, script);
60 termPQExpBuffer(&connectbuf);
63 "SELECT pg_catalog.lo_create(t.loid)\n"
64 "FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) AS t;\n");
77 report_status(PG_WARNING, "warning");
79 pg_log(PG_WARNING, "\n"
80 "Your installation contains large objects. The new database has an\n"
81 "additional large object permission table. After upgrading, you will be\n"
82 "given a command to populate the pg_largeobject_metadata table with\n"
83 "default permissions.\n\n");
85 pg_log(PG_WARNING, "\n"
86 "Your installation contains large objects. The new database has an\n"
87 "additional large object permission table, so default permissions must be\n"
88 "defined for all large objects. The file\n"
90 "when executed by psql by the database superuser will set the default\n"
100 * old_9_3_check_for_line_data_type_usage()
102 * Fully implement the 'line' data type in 9.4, which previously returned
103 * "not enabled" by default and was only functionally enabled with a
104 * compile-time switch; 9.4 "line" has different binary and text
105 * representation formats; checks tables and indexes.
108 old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster)
113 char output_path[MAXPGPATH];
115 prep_status("Checking for incompatible \"line\" data type");
117 snprintf(output_path, sizeof(output_path), "tables_using_line.txt");
119 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
122 bool db_used = false;
128 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
129 PGconn *conn = connectToServer(cluster, active_db->db_name);
132 * The pg_catalog.line type may be wrapped in a domain or composite
133 * type, or both (9.3 did not allow domains on composite types, but
134 * there may be multi-level composite type). To detect these cases
135 * we need a recursive CTE.
137 res = executeQueryOrDie(conn,
138 "WITH RECURSIVE oids AS ( "
139 /* the pg_catalog.line type itself */
140 " SELECT 'pg_catalog.line'::pg_catalog.regtype AS oid "
143 /* domains on the type */
144 " WITH x AS (SELECT oid FROM oids) "
145 " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
147 /* composite types containing the type */
148 " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
149 " WHERE t.typtype = 'c' AND "
150 " t.oid = c.reltype AND "
151 " c.oid = a.attrelid AND "
152 " NOT a.attisdropped AND "
153 " a.atttypid = x.oid "
156 "SELECT n.nspname, c.relname, a.attname "
157 "FROM pg_catalog.pg_class c, "
158 " pg_catalog.pg_namespace n, "
159 " pg_catalog.pg_attribute a "
160 "WHERE c.oid = a.attrelid AND "
161 " NOT a.attisdropped AND "
162 " a.atttypid IN (SELECT oid FROM oids) AND "
164 CppAsString2(RELKIND_RELATION) ", "
165 CppAsString2(RELKIND_MATVIEW) ", "
166 CppAsString2(RELKIND_INDEX) ") AND "
167 " c.relnamespace = n.oid AND "
168 /* exclude possible orphaned temp tables */
169 " n.nspname !~ '^pg_temp_' AND "
170 " n.nspname !~ '^pg_toast_temp_' AND "
171 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
173 ntups = PQntuples(res);
174 i_nspname = PQfnumber(res, "nspname");
175 i_relname = PQfnumber(res, "relname");
176 i_attname = PQfnumber(res, "attname");
177 for (rowno = 0; rowno < ntups; rowno++)
180 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
181 pg_fatal("could not open file \"%s\": %s\n", output_path,
185 fprintf(script, "In database: %s\n", active_db->db_name);
188 fprintf(script, " %s.%s.%s\n",
189 PQgetvalue(res, rowno, i_nspname),
190 PQgetvalue(res, rowno, i_relname),
191 PQgetvalue(res, rowno, i_attname));
204 pg_log(PG_REPORT, "fatal\n");
205 pg_fatal("Your installation contains the \"line\" data type in user tables. This\n"
206 "data type changed its internal and input/output format between your old\n"
207 "and new clusters so this cluster cannot currently be upgraded. You can\n"
208 "remove the problem tables and restart the upgrade. A list of the problem\n"
209 "columns is in the file:\n"
210 " %s\n\n", output_path);
218 * old_9_6_check_for_unknown_data_type_usage()
220 * It's no longer allowed to create tables or views with "unknown"-type
221 * columns. We do not complain about views with such columns, because
222 * they should get silently converted to "text" columns during the DDL
223 * dump and reload; it seems unlikely to be worth making users do that
224 * by hand. However, if there's a table with such a column, the DDL
225 * reload will fail, so we should pre-detect that rather than failing
226 * mid-upgrade. Worse, if there's a matview with such a column, the
227 * DDL reload will silently change it to "text" which won't match the
228 * on-disk storage (which is like "cstring"). So we *must* reject that.
229 * Also check composite types and domains on the "unknwown" type (even
230 * combinations of both), in case they are used for table columns.
231 * We needn't check indexes, because "unknown" has no opclasses.
234 old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster)
239 char output_path[MAXPGPATH];
241 prep_status("Checking for invalid \"unknown\" user columns");
243 snprintf(output_path, sizeof(output_path), "tables_using_unknown.txt");
245 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
248 bool db_used = false;
254 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
255 PGconn *conn = connectToServer(cluster, active_db->db_name);
258 * The pg_catalog.unknown type may be wrapped in a domain or composite
259 * type, or both (9.3 did not allow domains on composite types, but
260 * there may be multi-level composite type). To detect these cases
261 * we need a recursive CTE.
263 res = executeQueryOrDie(conn,
264 "WITH RECURSIVE oids AS ( "
265 /* the pg_catalog.unknown type itself */
266 " SELECT 'pg_catalog.unknown'::pg_catalog.regtype AS oid "
269 /* domains on the type */
270 " WITH x AS (SELECT oid FROM oids) "
271 " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
273 /* composite types containing the type */
274 " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
275 " WHERE t.typtype = 'c' AND "
276 " t.oid = c.reltype AND "
277 " c.oid = a.attrelid AND "
278 " NOT a.attisdropped AND "
279 " a.atttypid = x.oid "
282 "SELECT n.nspname, c.relname, a.attname "
283 "FROM pg_catalog.pg_class c, "
284 " pg_catalog.pg_namespace n, "
285 " pg_catalog.pg_attribute a "
286 "WHERE c.oid = a.attrelid AND "
287 " NOT a.attisdropped AND "
288 " a.atttypid IN (SELECT oid FROM oids) AND "
290 CppAsString2(RELKIND_RELATION) ", "
291 CppAsString2(RELKIND_MATVIEW) ") AND "
292 " c.relnamespace = n.oid AND "
293 /* exclude possible orphaned temp tables */
294 " n.nspname !~ '^pg_temp_' AND "
295 " n.nspname !~ '^pg_toast_temp_' AND "
296 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
298 ntups = PQntuples(res);
299 i_nspname = PQfnumber(res, "nspname");
300 i_relname = PQfnumber(res, "relname");
301 i_attname = PQfnumber(res, "attname");
302 for (rowno = 0; rowno < ntups; rowno++)
305 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
306 pg_fatal("could not open file \"%s\": %s\n", output_path,
310 fprintf(script, "In database: %s\n", active_db->db_name);
313 fprintf(script, " %s.%s.%s\n",
314 PQgetvalue(res, rowno, i_nspname),
315 PQgetvalue(res, rowno, i_relname),
316 PQgetvalue(res, rowno, i_attname));
329 pg_log(PG_REPORT, "fatal\n");
330 pg_fatal("Your installation contains the \"unknown\" data type in user tables. This\n"
331 "data type is no longer allowed in tables, so this cluster cannot currently\n"
332 "be upgraded. You can remove the problem tables and restart the upgrade.\n"
333 "A list of the problem columns is in the file:\n"
334 " %s\n\n", output_path);
341 * old_9_6_invalidate_hash_indexes()
343 * Hash index binary format has changed from 9.6->10.0
346 old_9_6_invalidate_hash_indexes(ClusterInfo *cluster, bool check_mode)
351 char *output_path = "reindex_hash.sql";
353 prep_status("Checking for hash indexes");
355 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
358 bool db_used = false;
363 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
364 PGconn *conn = connectToServer(cluster, active_db->db_name);
366 /* find hash indexes */
367 res = executeQueryOrDie(conn,
368 "SELECT n.nspname, c.relname "
369 "FROM pg_catalog.pg_class c, "
370 " pg_catalog.pg_index i, "
371 " pg_catalog.pg_am a, "
372 " pg_catalog.pg_namespace n "
373 "WHERE i.indexrelid = c.oid AND "
374 " c.relam = a.oid AND "
375 " c.relnamespace = n.oid AND "
379 ntups = PQntuples(res);
380 i_nspname = PQfnumber(res, "nspname");
381 i_relname = PQfnumber(res, "relname");
382 for (rowno = 0; rowno < ntups; rowno++)
387 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
388 pg_fatal("could not open file \"%s\": %s\n", output_path,
392 PQExpBufferData connectbuf;
394 initPQExpBuffer(&connectbuf);
395 appendPsqlMetaConnect(&connectbuf, active_db->db_name);
396 fputs(connectbuf.data, script);
397 termPQExpBuffer(&connectbuf);
400 fprintf(script, "REINDEX INDEX %s.%s;\n",
401 quote_identifier(PQgetvalue(res, rowno, i_nspname)),
402 quote_identifier(PQgetvalue(res, rowno, i_relname)));
408 if (!check_mode && db_used)
410 /* mark hash indexes as invalid */
411 PQclear(executeQueryOrDie(conn,
412 "UPDATE pg_catalog.pg_index i "
413 "SET indisvalid = false "
414 "FROM pg_catalog.pg_class c, "
415 " pg_catalog.pg_am a, "
416 " pg_catalog.pg_namespace n "
417 "WHERE i.indexrelid = c.oid AND "
418 " c.relam = a.oid AND "
419 " c.relnamespace = n.oid AND "
420 " a.amname = 'hash'"));
431 report_status(PG_WARNING, "warning");
433 pg_log(PG_WARNING, "\n"
434 "Your installation contains hash indexes. These indexes have different\n"
435 "internal formats between your old and new clusters, so they must be\n"
436 "reindexed with the REINDEX command. After upgrading, you will be given\n"
437 "REINDEX instructions.\n\n");
439 pg_log(PG_WARNING, "\n"
440 "Your installation contains hash indexes. These indexes have different\n"
441 "internal formats between your old and new clusters, so they must be\n"
442 "reindexed with the REINDEX command. The file\n"
444 "when executed by psql by the database superuser will recreate all invalid\n"
445 "indexes; until then, none of these indexes will be used.\n\n",
453 * old_11_check_for_sql_identifier_data_type_usage()
455 * In 12, the sql_identifier data type was switched from name to varchar,
456 * which does affect the storage (name is by-ref, but not varlena). This
457 * means user tables using sql_identifier for columns are broken because
458 * the on-disk format is different.
460 * We need to check all objects that might store sql_identifier on disk,
461 * i.e. tables, matviews and indexes. Also check composite types in case
462 * they are used in this context.
465 old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster)
470 char output_path[MAXPGPATH];
472 prep_status("Checking for invalid \"sql_identifier\" user columns");
474 snprintf(output_path, sizeof(output_path), "tables_using_sql_identifier.txt");
476 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
479 bool db_used = false;
485 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
486 PGconn *conn = connectToServer(cluster, active_db->db_name);
489 * We need the recursive CTE because the sql_identifier may be wrapped
490 * either in a domain or composite type, or both (in arbitrary order).
492 res = executeQueryOrDie(conn,
493 "WITH RECURSIVE oids AS ( "
494 /* the sql_identifier type itself */
495 " SELECT 'information_schema.sql_identifier'::pg_catalog.regtype AS oid "
498 /* domains on the type */
499 " WITH x AS (SELECT oid FROM oids) "
500 " SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
502 /* composite types containing the type */
503 " SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
504 " WHERE t.typtype = 'c' AND "
505 " t.oid = c.reltype AND "
506 " c.oid = a.attrelid AND "
507 " NOT a.attisdropped AND "
508 " a.atttypid = x.oid "
511 "SELECT n.nspname, c.relname, a.attname "
512 "FROM pg_catalog.pg_class c, "
513 " pg_catalog.pg_namespace n, "
514 " pg_catalog.pg_attribute a "
515 "WHERE c.oid = a.attrelid AND "
516 " NOT a.attisdropped AND "
517 " a.atttypid IN (SELECT oid FROM oids) AND "
519 CppAsString2(RELKIND_RELATION) ", "
520 CppAsString2(RELKIND_MATVIEW) ", "
521 CppAsString2(RELKIND_INDEX) ") AND "
522 " c.relnamespace = n.oid AND "
523 /* exclude possible orphaned temp tables */
524 " n.nspname !~ '^pg_temp_' AND "
525 " n.nspname !~ '^pg_toast_temp_' AND "
526 " n.nspname NOT IN ('pg_catalog', 'information_schema')");
528 ntups = PQntuples(res);
529 i_nspname = PQfnumber(res, "nspname");
530 i_relname = PQfnumber(res, "relname");
531 i_attname = PQfnumber(res, "attname");
532 for (rowno = 0; rowno < ntups; rowno++)
535 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
536 pg_fatal("could not open file \"%s\": %s\n", output_path,
540 fprintf(script, "In database: %s\n", active_db->db_name);
543 fprintf(script, " %s.%s.%s\n",
544 PQgetvalue(res, rowno, i_nspname),
545 PQgetvalue(res, rowno, i_relname),
546 PQgetvalue(res, rowno, i_attname));
559 pg_log(PG_REPORT, "fatal\n");
560 pg_fatal("Your installation contains the \"sql_identifier\" data type in user tables\n"
561 "and/or indexes. The on-disk format for this data type has changed, so this\n"
562 "cluster cannot currently be upgraded. You can remove the problem tables or\n"
563 "change the data type to \"name\" and restart the upgrade.\n"
564 "A list of the problem columns is in the file:\n"
565 " %s\n\n", output_path);