]> granicus.if.org Git - postgresql/blob - src/bin/pg_upgrade/version.c
Make the order of the header file includes consistent in non-backend modules.
[postgresql] / src / bin / pg_upgrade / version.c
1 /*
2  *      version.c
3  *
4  *      Postgres-version-specific routines
5  *
6  *      Copyright (c) 2010-2019, PostgreSQL Global Development Group
7  *      src/bin/pg_upgrade/version.c
8  */
9
10 #include "postgres_fe.h"
11
12 #include "catalog/pg_class_d.h"
13 #include "fe_utils/string_utils.h"
14 #include "pg_upgrade.h"
15
16 /*
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
20  */
21 void
22 new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster, bool check_mode)
23 {
24         int                     dbnum;
25         FILE       *script = NULL;
26         bool            found = false;
27         char            output_path[MAXPGPATH];
28
29         prep_status("Checking for large objects");
30
31         snprintf(output_path, sizeof(output_path), "pg_largeobject.sql");
32
33         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
34         {
35                 PGresult   *res;
36                 int                     i_count;
37                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
38                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
39
40                 /* find if there are any large objects */
41                 res = executeQueryOrDie(conn,
42                                                                 "SELECT count(*) "
43                                                                 "FROM   pg_catalog.pg_largeobject ");
44
45                 i_count = PQfnumber(res, "count");
46                 if (atoi(PQgetvalue(res, 0, i_count)) != 0)
47                 {
48                         found = true;
49                         if (!check_mode)
50                         {
51                                 PQExpBufferData connectbuf;
52
53                                 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
54                                         pg_fatal("could not open file \"%s\": %s\n", output_path,
55                                                          strerror(errno));
56
57                                 initPQExpBuffer(&connectbuf);
58                                 appendPsqlMetaConnect(&connectbuf, active_db->db_name);
59                                 fputs(connectbuf.data, script);
60                                 termPQExpBuffer(&connectbuf);
61
62                                 fprintf(script,
63                                                 "SELECT pg_catalog.lo_create(t.loid)\n"
64                                                 "FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) AS t;\n");
65                         }
66                 }
67
68                 PQclear(res);
69                 PQfinish(conn);
70         }
71
72         if (script)
73                 fclose(script);
74
75         if (found)
76         {
77                 report_status(PG_WARNING, "warning");
78                 if (check_mode)
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");
84                 else
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"
89                                    "    %s\n"
90                                    "when executed by psql by the database superuser will set the default\n"
91                                    "permissions.\n\n",
92                                    output_path);
93         }
94         else
95                 check_ok();
96 }
97
98
99 /*
100  * old_9_3_check_for_line_data_type_usage()
101  *      9.3 -> 9.4
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.
106  */
107 void
108 old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster)
109 {
110         int                     dbnum;
111         FILE       *script = NULL;
112         bool            found = false;
113         char            output_path[MAXPGPATH];
114
115         prep_status("Checking for incompatible \"line\" data type");
116
117         snprintf(output_path, sizeof(output_path), "tables_using_line.txt");
118
119         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
120         {
121                 PGresult   *res;
122                 bool            db_used = false;
123                 int                     ntups;
124                 int                     rowno;
125                 int                     i_nspname,
126                                         i_relname,
127                                         i_attname;
128                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
129                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
130
131                 /*
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.
136                  */
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 "
141                                                                 "       UNION ALL "
142                                                                 "       SELECT * FROM ( "
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' "
146                                                                 "                       UNION "
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 "
154                                                                 "       ) foo "
155                                                                 ") "
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 "
163                                                                 "               c.relkind IN ("
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')");
172
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++)
178                 {
179                         found = true;
180                         if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
181                                 pg_fatal("could not open file \"%s\": %s\n", output_path,
182                                                  strerror(errno));
183                         if (!db_used)
184                         {
185                                 fprintf(script, "In database: %s\n", active_db->db_name);
186                                 db_used = true;
187                         }
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));
192                 }
193
194                 PQclear(res);
195
196                 PQfinish(conn);
197         }
198
199         if (script)
200                 fclose(script);
201
202         if (found)
203         {
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);
211         }
212         else
213                 check_ok();
214 }
215
216
217 /*
218  * old_9_6_check_for_unknown_data_type_usage()
219  *      9.6 -> 10
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.
232  */
233 void
234 old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster)
235 {
236         int                     dbnum;
237         FILE       *script = NULL;
238         bool            found = false;
239         char            output_path[MAXPGPATH];
240
241         prep_status("Checking for invalid \"unknown\" user columns");
242
243         snprintf(output_path, sizeof(output_path), "tables_using_unknown.txt");
244
245         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
246         {
247                 PGresult   *res;
248                 bool            db_used = false;
249                 int                     ntups;
250                 int                     rowno;
251                 int                     i_nspname,
252                                         i_relname,
253                                         i_attname;
254                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
255                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
256
257                 /*
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.
262                  */
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 "
267                                                                 "       UNION ALL "
268                                                                 "       SELECT * FROM ( "
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' "
272                                                                 "                       UNION "
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 "
280                                                                 "       ) foo "
281                                                                 ") "
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 "
289                                                                 "               c.relkind IN ("
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')");
297
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++)
303                 {
304                         found = true;
305                         if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
306                                 pg_fatal("could not open file \"%s\": %s\n", output_path,
307                                                  strerror(errno));
308                         if (!db_used)
309                         {
310                                 fprintf(script, "In database: %s\n", active_db->db_name);
311                                 db_used = true;
312                         }
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));
317                 }
318
319                 PQclear(res);
320
321                 PQfinish(conn);
322         }
323
324         if (script)
325                 fclose(script);
326
327         if (found)
328         {
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);
335         }
336         else
337                 check_ok();
338 }
339
340 /*
341  * old_9_6_invalidate_hash_indexes()
342  *      9.6 -> 10
343  *      Hash index binary format has changed from 9.6->10.0
344  */
345 void
346 old_9_6_invalidate_hash_indexes(ClusterInfo *cluster, bool check_mode)
347 {
348         int                     dbnum;
349         FILE       *script = NULL;
350         bool            found = false;
351         char       *output_path = "reindex_hash.sql";
352
353         prep_status("Checking for hash indexes");
354
355         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
356         {
357                 PGresult   *res;
358                 bool            db_used = false;
359                 int                     ntups;
360                 int                     rowno;
361                 int                     i_nspname,
362                                         i_relname;
363                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
364                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
365
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 "
376                                                                 "               a.amname = 'hash'"
377                         );
378
379                 ntups = PQntuples(res);
380                 i_nspname = PQfnumber(res, "nspname");
381                 i_relname = PQfnumber(res, "relname");
382                 for (rowno = 0; rowno < ntups; rowno++)
383                 {
384                         found = true;
385                         if (!check_mode)
386                         {
387                                 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
388                                         pg_fatal("could not open file \"%s\": %s\n", output_path,
389                                                          strerror(errno));
390                                 if (!db_used)
391                                 {
392                                         PQExpBufferData connectbuf;
393
394                                         initPQExpBuffer(&connectbuf);
395                                         appendPsqlMetaConnect(&connectbuf, active_db->db_name);
396                                         fputs(connectbuf.data, script);
397                                         termPQExpBuffer(&connectbuf);
398                                         db_used = true;
399                                 }
400                                 fprintf(script, "REINDEX INDEX %s.%s;\n",
401                                                 quote_identifier(PQgetvalue(res, rowno, i_nspname)),
402                                                 quote_identifier(PQgetvalue(res, rowno, i_relname)));
403                         }
404                 }
405
406                 PQclear(res);
407
408                 if (!check_mode && db_used)
409                 {
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'"));
421                 }
422
423                 PQfinish(conn);
424         }
425
426         if (script)
427                 fclose(script);
428
429         if (found)
430         {
431                 report_status(PG_WARNING, "warning");
432                 if (check_mode)
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");
438                 else
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"
443                                    "    %s\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",
446                                    output_path);
447         }
448         else
449                 check_ok();
450 }
451
452 /*
453  * old_11_check_for_sql_identifier_data_type_usage()
454  *      11 -> 12
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.
459  *
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.
463  */
464 void
465 old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster)
466 {
467         int                     dbnum;
468         FILE       *script = NULL;
469         bool            found = false;
470         char            output_path[MAXPGPATH];
471
472         prep_status("Checking for invalid \"sql_identifier\" user columns");
473
474         snprintf(output_path, sizeof(output_path), "tables_using_sql_identifier.txt");
475
476         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
477         {
478                 PGresult   *res;
479                 bool            db_used = false;
480                 int                     ntups;
481                 int                     rowno;
482                 int                     i_nspname,
483                                         i_relname,
484                                         i_attname;
485                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
486                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
487
488                 /*
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).
491                  */
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 "
496                                                                 "       UNION ALL "
497                                                                 "       SELECT * FROM ( "
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' "
501                                                                 "                       UNION "
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 "
509                                                                 "       ) foo "
510                                                                 ") "
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 "
518                                                                 "               c.relkind IN ("
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')");
527
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++)
533                 {
534                         found = true;
535                         if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
536                                 pg_fatal("could not open file \"%s\": %s\n", output_path,
537                                                  strerror(errno));
538                         if (!db_used)
539                         {
540                                 fprintf(script, "In database: %s\n", active_db->db_name);
541                                 db_used = true;
542                         }
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));
547                 }
548
549                 PQclear(res);
550
551                 PQfinish(conn);
552         }
553
554         if (script)
555                 fclose(script);
556
557         if (found)
558         {
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);
566         }
567         else
568                 check_ok();
569 }