]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/check.c
Adjust pg_upgrade check for pg_upgrade_support to happen after the
[postgresql] / contrib / pg_upgrade / check.c
1 /*
2  *      check.c
3  *
4  *      server checks and output routines
5  *
6  *      Copyright (c) 2010-2011, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/check.c
8  */
9
10 #include "pg_upgrade.h"
11
12
13 static void set_locale_and_encoding(ClusterInfo *cluster);
14 static void check_new_cluster_is_empty(void);
15 static void check_old_cluster_has_new_cluster_dbs(void);
16 static void check_locale_and_encoding(ControlData *oldctrl,
17                                                   ControlData *newctrl);
18 static void check_is_super_user(ClusterInfo *cluster);
19 static void check_for_prepared_transactions(ClusterInfo *cluster);
20 static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
21 static void check_for_reg_data_type_usage(ClusterInfo *cluster);
22 static void check_for_support_lib(ClusterInfo *cluster);
23
24
25 void
26 output_check_banner(bool *live_check)
27 {
28         if (user_opts.check && is_server_running(old_cluster.pgdata))
29         {
30                 *live_check = true;
31                 if (old_cluster.port == new_cluster.port)
32                         pg_log(PG_FATAL, "When checking a live server, "
33                                    "the old and new port numbers must be different.\n");
34                 pg_log(PG_REPORT, "Performing Consistency Checks on Old Live Server\n");
35                 pg_log(PG_REPORT, "------------------------------------------------\n");
36         }
37         else
38         {
39                 pg_log(PG_REPORT, "Performing Consistency Checks\n");
40                 pg_log(PG_REPORT, "-----------------------------\n");
41         }
42 }
43
44
45 void
46 check_old_cluster(bool live_check,
47                                   char **sequence_script_file_name)
48 {
49         /* -- OLD -- */
50
51         if (!live_check)
52                 start_postmaster(&old_cluster);
53
54         set_locale_and_encoding(&old_cluster);
55
56         get_pg_database_relfilenode(&old_cluster);
57
58         /* Extract a list of databases and tables from the old cluster */
59         get_db_and_rel_infos(&old_cluster);
60
61         init_tablespaces();
62
63         get_loadable_libraries();
64
65
66         /*
67          * Check for various failure cases
68          */
69         check_is_super_user(&old_cluster);
70         check_for_prepared_transactions(&old_cluster);
71         check_for_reg_data_type_usage(&old_cluster);
72         check_for_isn_and_int8_passing_mismatch(&old_cluster);
73
74         /* old = PG 8.3 checks? */
75         if (GET_MAJOR_VERSION(old_cluster.major_version) <= 803)
76         {
77                 old_8_3_check_for_name_data_type_usage(&old_cluster);
78                 old_8_3_check_for_tsquery_usage(&old_cluster);
79                 if (user_opts.check)
80                 {
81                         old_8_3_rebuild_tsvector_tables(&old_cluster, true);
82                         old_8_3_invalidate_hash_gin_indexes(&old_cluster, true);
83                         old_8_3_invalidate_bpchar_pattern_ops_indexes(&old_cluster, true);
84                 }
85                 else
86
87                         /*
88                          * While we have the old server running, create the script to
89                          * properly restore its sequence values but we report this at the
90                          * end.
91                          */
92                         *sequence_script_file_name =
93                                 old_8_3_create_sequence_script(&old_cluster);
94         }
95
96         /* Pre-PG 9.0 had no large object permissions */
97         if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
98                 new_9_0_populate_pg_largeobject_metadata(&old_cluster, true);
99
100         /*
101          * While not a check option, we do this now because this is the only time
102          * the old server is running.
103          */
104         if (!user_opts.check)
105         {
106                 generate_old_dump();
107                 split_old_dump();
108         }
109
110         if (!live_check)
111                 stop_postmaster(false);
112 }
113
114
115 void
116 check_new_cluster(void)
117 {
118         set_locale_and_encoding(&new_cluster);
119
120         get_db_and_rel_infos(&new_cluster);
121
122         check_new_cluster_is_empty();
123         check_for_prepared_transactions(&new_cluster);
124         check_old_cluster_has_new_cluster_dbs();
125
126         check_loadable_libraries();
127
128         check_locale_and_encoding(&old_cluster.controldata, &new_cluster.controldata);
129
130         if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
131                 check_hard_link();
132 }
133
134
135 void
136 report_clusters_compatible(void)
137 {
138         if (user_opts.check)
139         {
140                 pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
141                 /* stops new cluster */
142                 stop_postmaster(false);
143                 exit(0);
144         }
145
146         pg_log(PG_REPORT, "\n"
147                    "| If pg_upgrade fails after this point, you must\n"
148                    "| re-initdb the new cluster before continuing.\n"
149                    "| You will also need to remove the \".old\" suffix\n"
150                    "| from %s/global/pg_control.old.\n", old_cluster.pgdata);
151 }
152
153
154 void
155 issue_warnings(char *sequence_script_file_name)
156 {
157         /* old = PG 8.3 warnings? */
158         if (GET_MAJOR_VERSION(old_cluster.major_version) <= 803)
159         {
160                 start_postmaster(&new_cluster);
161
162                 /* restore proper sequence values using file created from old server */
163                 if (sequence_script_file_name)
164                 {
165                         prep_status("Adjusting sequences");
166                         exec_prog(true,
167                                           SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
168                                           "--no-psqlrc --port %d --username \"%s\" "
169                                           "-f \"%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
170                                           new_cluster.bindir, new_cluster.port, os_info.user,
171                                           sequence_script_file_name, log_opts.filename);
172                         unlink(sequence_script_file_name);
173                         check_ok();
174                 }
175
176                 old_8_3_rebuild_tsvector_tables(&new_cluster, false);
177                 old_8_3_invalidate_hash_gin_indexes(&new_cluster, false);
178                 old_8_3_invalidate_bpchar_pattern_ops_indexes(&new_cluster, false);
179                 stop_postmaster(false);
180         }
181
182         /* Create dummy large object permissions for old < PG 9.0? */
183         if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
184         {
185                 start_postmaster(&new_cluster);
186                 new_9_0_populate_pg_largeobject_metadata(&new_cluster, false);
187                 stop_postmaster(false);
188         }
189 }
190
191
192 void
193 output_completion_banner(char *deletion_script_file_name)
194 {
195         /* Did we copy the free space files? */
196         if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
197                 pg_log(PG_REPORT,
198                            "| Optimizer statistics are not transferred by pg_upgrade\n"
199                            "| so consider running:\n"
200                            "| \tvacuumdb --all --analyze-only\n"
201                            "| on the newly-upgraded cluster.\n\n");
202         else
203                 pg_log(PG_REPORT,
204                            "| Optimizer statistics and free space information\n"
205                            "| are not transferred by pg_upgrade so consider\n"
206                            "| running:\n"
207                            "| \tvacuumdb --all --analyze\n"
208                            "| on the newly-upgraded cluster.\n\n");
209
210         pg_log(PG_REPORT,
211                    "| Running this script will delete the old cluster's data files:\n"
212                    "| \t%s\n",
213                    deletion_script_file_name);
214 }
215
216
217 void
218 check_cluster_versions(void)
219 {
220         /* get old and new cluster versions */
221         old_cluster.major_version = get_major_server_version(&old_cluster);
222         new_cluster.major_version = get_major_server_version(&new_cluster);
223
224         /*
225          * We allow upgrades from/to the same major version for alpha/beta
226          * upgrades
227          */
228
229         if (GET_MAJOR_VERSION(old_cluster.major_version) < 803)
230                 pg_log(PG_FATAL, "This utility can only upgrade from PostgreSQL version 8.3 and later.\n");
231
232         /* Only current PG version is supported as a target */
233         if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
234                 pg_log(PG_FATAL, "This utility can only upgrade to PostgreSQL version %s.\n",
235                            PG_MAJORVERSION);
236
237         /*
238          * We can't allow downgrading because we use the target pg_dumpall, and
239          * pg_dumpall cannot operate on new datbase versions, only older versions.
240          */
241         if (old_cluster.major_version > new_cluster.major_version)
242                 pg_log(PG_FATAL, "This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
243 }
244
245
246 void
247 check_cluster_compatibility(bool live_check)
248 {
249         check_for_support_lib(&new_cluster);
250
251         /* get/check pg_control data of servers */
252         get_control_data(&old_cluster, live_check);
253         get_control_data(&new_cluster, false);
254         check_control_data(&old_cluster.controldata, &new_cluster.controldata);
255
256         /* Is it 9.0 but without tablespace directories? */
257         if (GET_MAJOR_VERSION(new_cluster.major_version) == 900 &&
258                 new_cluster.controldata.cat_ver < TABLE_SPACE_SUBDIRS_CAT_VER)
259                 pg_log(PG_FATAL, "This utility can only upgrade to PostgreSQL version 9.0 after 2010-01-11\n"
260                            "because of backend API changes made during development.\n");
261 }
262
263
264 /*
265  * set_locale_and_encoding()
266  *
267  * query the database to get the template0 locale
268  */
269 static void
270 set_locale_and_encoding(ClusterInfo *cluster)
271 {
272         ControlData *ctrl = &cluster->controldata;
273         PGconn     *conn;
274         PGresult   *res;
275         int                     i_encoding;
276         int                     cluster_version = cluster->major_version;
277
278         conn = connectToServer(cluster, "template1");
279
280         /* for pg < 80400, we got the values from pg_controldata */
281         if (cluster_version >= 80400)
282         {
283                 int                     i_datcollate;
284                 int                     i_datctype;
285
286                 res = executeQueryOrDie(conn,
287                                                                 "SELECT datcollate, datctype "
288                                                                 "FROM   pg_catalog.pg_database "
289                                                                 "WHERE  datname = 'template0' ");
290                 assert(PQntuples(res) == 1);
291
292                 i_datcollate = PQfnumber(res, "datcollate");
293                 i_datctype = PQfnumber(res, "datctype");
294
295                 ctrl->lc_collate = pg_strdup(PQgetvalue(res, 0, i_datcollate));
296                 ctrl->lc_ctype = pg_strdup(PQgetvalue(res, 0, i_datctype));
297
298                 PQclear(res);
299         }
300
301         res = executeQueryOrDie(conn,
302                                                         "SELECT pg_catalog.pg_encoding_to_char(encoding) "
303                                                         "FROM   pg_catalog.pg_database "
304                                                         "WHERE  datname = 'template0' ");
305         assert(PQntuples(res) == 1);
306
307         i_encoding = PQfnumber(res, "pg_encoding_to_char");
308         ctrl->encoding = pg_strdup(PQgetvalue(res, 0, i_encoding));
309
310         PQclear(res);
311
312         PQfinish(conn);
313 }
314
315
316 /*
317  * check_locale_and_encoding()
318  *
319  *      locale is not in pg_controldata in 8.4 and later so
320  *      we probably had to get via a database query.
321  */
322 static void
323 check_locale_and_encoding(ControlData *oldctrl,
324                                                   ControlData *newctrl)
325 {
326         /* These are often defined with inconsistent case, so use pg_strcasecmp(). */
327         if (pg_strcasecmp(oldctrl->lc_collate, newctrl->lc_collate) != 0)
328                 pg_log(PG_FATAL,
329                            "old and new cluster lc_collate values do not match\n");
330         if (pg_strcasecmp(oldctrl->lc_ctype, newctrl->lc_ctype) != 0)
331                 pg_log(PG_FATAL,
332                            "old and new cluster lc_ctype values do not match\n");
333         if (pg_strcasecmp(oldctrl->encoding, newctrl->encoding) != 0)
334                 pg_log(PG_FATAL,
335                            "old and new cluster encoding values do not match\n");
336 }
337
338
339 static void
340 check_new_cluster_is_empty(void)
341 {
342         int                     dbnum;
343
344         for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
345         {
346                 int                     relnum;
347                 RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
348
349                 for (relnum = 0; relnum < rel_arr->nrels;
350                          relnum++)
351                 {
352                         /* pg_largeobject and its index should be skipped */
353                         if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
354                                 pg_log(PG_FATAL, "New cluster database \"%s\" is not empty\n",
355                                            new_cluster.dbarr.dbs[dbnum].db_name);
356                 }
357         }
358
359 }
360
361
362 /*
363  *      If someone removes the 'postgres' database from the old cluster and
364  *      the new cluster has a 'postgres' database, the number of databases
365  *      will not match.  We actually could upgrade such a setup, but it would
366  *      violate the 1-to-1 mapping of database counts, so we throw an error
367  *      instead.  We would detect this as a database count mismatch during
368  *      upgrade, but we want to detect it during the check phase and report
369  *      the database name.
370  */
371 static void
372 check_old_cluster_has_new_cluster_dbs(void)
373 {
374         int                     old_dbnum,
375                                 new_dbnum;
376
377         for (new_dbnum = 0; new_dbnum < new_cluster.dbarr.ndbs; new_dbnum++)
378         {
379                 for (old_dbnum = 0; old_dbnum < old_cluster.dbarr.ndbs; old_dbnum++)
380                         if (strcmp(old_cluster.dbarr.dbs[old_dbnum].db_name,
381                                            new_cluster.dbarr.dbs[new_dbnum].db_name) == 0)
382                                 break;
383                 if (old_dbnum == old_cluster.dbarr.ndbs)
384                         pg_log(PG_FATAL, "New cluster database \"%s\" does not exist in the old cluster\n",
385                                    new_cluster.dbarr.dbs[new_dbnum].db_name);
386         }
387 }
388
389
390 /*
391  * create_script_for_old_cluster_deletion()
392  *
393  *      This is particularly useful for tablespace deletion.
394  */
395 void
396 create_script_for_old_cluster_deletion(
397                                                                            char **deletion_script_file_name)
398 {
399         FILE       *script = NULL;
400         int                     tblnum;
401
402         *deletion_script_file_name = pg_malloc(MAXPGPATH);
403
404         prep_status("Creating script to delete old cluster");
405
406         snprintf(*deletion_script_file_name, MAXPGPATH, "%s/delete_old_cluster.%s",
407                          os_info.cwd, SCRIPT_EXT);
408
409         if ((script = fopen(*deletion_script_file_name, "w")) == NULL)
410                 pg_log(PG_FATAL, "Could not create necessary file:  %s\n",
411                            *deletion_script_file_name);
412
413 #ifndef WIN32
414         /* add shebang header */
415         fprintf(script, "#!/bin/sh\n\n");
416 #endif
417
418         /* delete old cluster's default tablespace */
419         fprintf(script, RMDIR_CMD " %s\n", old_cluster.pgdata);
420
421         /* delete old cluster's alternate tablespaces */
422         for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
423         {
424                 /*
425                  * Do the old cluster's per-database directories share a directory
426                  * with a new version-specific tablespace?
427                  */
428                 if (strlen(old_cluster.tablespace_suffix) == 0)
429                 {
430                         /* delete per-database directories */
431                         int                     dbnum;
432
433                         fprintf(script, "\n");
434                         /* remove PG_VERSION? */
435                         if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
436                                 fprintf(script, RM_CMD " %s%s/PG_VERSION\n",
437                                  os_info.tablespaces[tblnum], old_cluster.tablespace_suffix);
438
439                         for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
440                         {
441                                 fprintf(script, RMDIR_CMD " %s%s/%d\n",
442                                   os_info.tablespaces[tblnum], old_cluster.tablespace_suffix,
443                                                 old_cluster.dbarr.dbs[dbnum].db_oid);
444                         }
445                 }
446                 else
447
448                         /*
449                          * Simply delete the tablespace directory, which might be ".old"
450                          * or a version-specific subdirectory.
451                          */
452                         fprintf(script, RMDIR_CMD " %s%s\n",
453                                  os_info.tablespaces[tblnum], old_cluster.tablespace_suffix);
454         }
455
456         fclose(script);
457
458 #ifndef WIN32
459         if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
460                 pg_log(PG_FATAL, "Could not add execute permission to file:  %s\n",
461                            *deletion_script_file_name);
462 #endif
463
464         check_ok();
465 }
466
467
468 /*
469  *      check_is_super_user()
470  *
471  *      Make sure we are the super-user.
472  */
473 static void
474 check_is_super_user(ClusterInfo *cluster)
475 {
476         PGresult   *res;
477         PGconn     *conn = connectToServer(cluster, "template1");
478
479         prep_status("Checking database user is a superuser");
480
481         /* Can't use pg_authid because only superusers can view it. */
482         res = executeQueryOrDie(conn,
483                                                         "SELECT rolsuper "
484                                                         "FROM pg_catalog.pg_roles "
485                                                         "WHERE rolname = current_user");
486
487         if (PQntuples(res) != 1 || strcmp(PQgetvalue(res, 0, 0), "t") != 0)
488                 pg_log(PG_FATAL, "database user \"%s\" is not a superuser\n",
489                            os_info.user);
490
491         PQclear(res);
492
493         PQfinish(conn);
494
495         check_ok();
496 }
497
498
499 /*
500  *      check_for_prepared_transactions()
501  *
502  *      Make sure there are no prepared transactions because the storage format
503  *      might have changed.
504  */
505 static void
506 check_for_prepared_transactions(ClusterInfo *cluster)
507 {
508         PGresult   *res;
509         PGconn     *conn = connectToServer(cluster, "template1");
510
511         prep_status("Checking for prepared transactions");
512
513         res = executeQueryOrDie(conn,
514                                                         "SELECT * "
515                                                         "FROM pg_catalog.pg_prepared_xact()");
516
517         if (PQntuples(res) != 0)
518                 pg_log(PG_FATAL, "The %s cluster contains prepared transactions\n",
519                            CLUSTER_NAME(cluster));
520
521         PQclear(res);
522
523         PQfinish(conn);
524
525         check_ok();
526 }
527
528
529 /*
530  *      check_for_isn_and_int8_passing_mismatch()
531  *
532  *      contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
533  *      by value.  The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
534  *      it must match for the old and new servers.
535  */
536 static void
537 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
538 {
539         int                     dbnum;
540         FILE       *script = NULL;
541         bool            found = false;
542         char            output_path[MAXPGPATH];
543
544         prep_status("Checking for contrib/isn with bigint-passing mismatch");
545
546         if (old_cluster.controldata.float8_pass_by_value ==
547                 new_cluster.controldata.float8_pass_by_value)
548         {
549                 /* no mismatch */
550                 check_ok();
551                 return;
552         }
553
554         snprintf(output_path, sizeof(output_path), "%s/contrib_isn_and_int8_pass_by_value.txt",
555                          os_info.cwd);
556
557         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
558         {
559                 PGresult   *res;
560                 bool            db_used = false;
561                 int                     ntups;
562                 int                     rowno;
563                 int                     i_nspname,
564                                         i_proname;
565                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
566                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
567
568                 /* Find any functions coming from contrib/isn */
569                 res = executeQueryOrDie(conn,
570                                                                 "SELECT n.nspname, p.proname "
571                                                                 "FROM   pg_catalog.pg_proc p, "
572                                                                 "               pg_catalog.pg_namespace n "
573                                                                 "WHERE  p.pronamespace = n.oid AND "
574                                                                 "               p.probin = '$libdir/isn'");
575
576                 ntups = PQntuples(res);
577                 i_nspname = PQfnumber(res, "nspname");
578                 i_proname = PQfnumber(res, "proname");
579                 for (rowno = 0; rowno < ntups; rowno++)
580                 {
581                         found = true;
582                         if (script == NULL && (script = fopen(output_path, "w")) == NULL)
583                                 pg_log(PG_FATAL, "Could not create necessary file:  %s\n", output_path);
584                         if (!db_used)
585                         {
586                                 fprintf(script, "Database:  %s\n", active_db->db_name);
587                                 db_used = true;
588                         }
589                         fprintf(script, "  %s.%s\n",
590                                         PQgetvalue(res, rowno, i_nspname),
591                                         PQgetvalue(res, rowno, i_proname));
592                 }
593
594                 PQclear(res);
595
596                 PQfinish(conn);
597         }
598
599         if (script)
600                 fclose(script);
601
602         if (found)
603         {
604                 pg_log(PG_REPORT, "fatal\n");
605                 pg_log(PG_FATAL,
606                            "| Your installation contains \"contrib/isn\" functions\n"
607                            "| which rely on the bigint data type.  Your old and\n"
608                            "| new clusters pass bigint values differently so this\n"
609                            "| cluster cannot currently be upgraded.  You can\n"
610                            "| manually upgrade data that use \"contrib/isn\"\n"
611                            "| facilities and remove \"contrib/isn\" from the\n"
612                            "| old cluster and restart the upgrade.  A list\n"
613                            "| of the problem functions is in the file:\n"
614                            "| \t%s\n\n", output_path);
615         }
616         else
617                 check_ok();
618 }
619
620
621 /*
622  * check_for_reg_data_type_usage()
623  *      pg_upgrade only preserves these system values:
624  *              pg_class.relfilenode
625  *              pg_type.oid
626  *              pg_enum.oid
627  *
628  *      Most of the reg* data types reference system catalog info that is
629  *      not preserved, and hence these data types cannot be used in user
630  *      tables upgraded by pg_upgrade.
631  */
632 static void
633 check_for_reg_data_type_usage(ClusterInfo *cluster)
634 {
635         int                     dbnum;
636         FILE       *script = NULL;
637         bool            found = false;
638         char            output_path[MAXPGPATH];
639
640         prep_status("Checking for reg* system oid user data types");
641
642         snprintf(output_path, sizeof(output_path), "%s/tables_using_reg.txt",
643                          os_info.cwd);
644
645         for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
646         {
647                 PGresult   *res;
648                 bool            db_used = false;
649                 int                     ntups;
650                 int                     rowno;
651                 int                     i_nspname,
652                                         i_relname,
653                                         i_attname;
654                 DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
655                 PGconn     *conn = connectToServer(cluster, active_db->db_name);
656
657                 res = executeQueryOrDie(conn,
658                                                                 "SELECT n.nspname, c.relname, a.attname "
659                                                                 "FROM   pg_catalog.pg_class c, "
660                                                                 "               pg_catalog.pg_namespace n, "
661                                                                 "               pg_catalog.pg_attribute a "
662                                                                 "WHERE  c.oid = a.attrelid AND "
663                                                                 "               NOT a.attisdropped AND "
664                                                                 "               a.atttypid IN ( "
665                   "                     'pg_catalog.regproc'::pg_catalog.regtype, "
666                                                                 "                       'pg_catalog.regprocedure'::pg_catalog.regtype, "
667                   "                     'pg_catalog.regoper'::pg_catalog.regtype, "
668                                                                 "                       'pg_catalog.regoperator'::pg_catalog.regtype, "
669                  "                      'pg_catalog.regclass'::pg_catalog.regtype, "
670                 /* regtype.oid is preserved, so 'regtype' is OK */
671                 "                       'pg_catalog.regconfig'::pg_catalog.regtype, "
672                                                                 "                       'pg_catalog.regdictionary'::pg_catalog.regtype) AND "
673                                                                 "               c.relnamespace = n.oid AND "
674                                                           "             n.nspname != 'pg_catalog' AND "
675                                                  "              n.nspname != 'information_schema'");
676
677                 ntups = PQntuples(res);
678                 i_nspname = PQfnumber(res, "nspname");
679                 i_relname = PQfnumber(res, "relname");
680                 i_attname = PQfnumber(res, "attname");
681                 for (rowno = 0; rowno < ntups; rowno++)
682                 {
683                         found = true;
684                         if (script == NULL && (script = fopen(output_path, "w")) == NULL)
685                                 pg_log(PG_FATAL, "Could not create necessary file:  %s\n", output_path);
686                         if (!db_used)
687                         {
688                                 fprintf(script, "Database:  %s\n", active_db->db_name);
689                                 db_used = true;
690                         }
691                         fprintf(script, "  %s.%s.%s\n",
692                                         PQgetvalue(res, rowno, i_nspname),
693                                         PQgetvalue(res, rowno, i_relname),
694                                         PQgetvalue(res, rowno, i_attname));
695                 }
696
697                 PQclear(res);
698
699                 PQfinish(conn);
700         }
701
702         if (script)
703                 fclose(script);
704
705         if (found)
706         {
707                 pg_log(PG_REPORT, "fatal\n");
708                 pg_log(PG_FATAL,
709                            "| Your installation contains one of the reg* data types in\n"
710                            "| user tables.  These data types reference system oids that\n"
711                            "| are not preserved by pg_upgrade, so this cluster cannot\n"
712                            "| currently be upgraded.  You can remove the problem tables\n"
713                            "| and restart the upgrade.  A list of the problem columns\n"
714                            "| is in the file:\n"
715                            "| \t%s\n\n", output_path);
716         }
717         else
718                 check_ok();
719 }
720
721
722 /*
723  * Test pg_upgrade_support.so is in the proper place.    We cannot copy it
724  * ourselves because install directories are typically root-owned.
725  */
726 static void
727 check_for_support_lib(ClusterInfo *cluster)
728 {
729         char            cmd[MAXPGPATH];
730         char            libdir[MAX_STRING];
731         char            libfile[MAXPGPATH];
732         FILE       *lib_test;
733         FILE       *output;
734
735         snprintf(cmd, sizeof(cmd), "\"%s/pg_config\" --pkglibdir", cluster->bindir);
736
737         if ((output = popen(cmd, "r")) == NULL)
738                 pg_log(PG_FATAL, "Could not get pkglibdir data: %s\n",
739                            getErrorText(errno));
740
741         fgets(libdir, sizeof(libdir), output);
742
743         pclose(output);
744
745         /* Remove trailing newline */
746         if (strchr(libdir, '\n') != NULL)
747                 *strchr(libdir, '\n') = '\0';
748
749         snprintf(libfile, sizeof(libfile), "%s/pg_upgrade_support%s", libdir,
750                          DLSUFFIX);
751
752         if ((lib_test = fopen(libfile, "r")) == NULL)
753                 pg_log(PG_FATAL,
754                            "The pg_upgrade_support module must be created and installed in the %s cluster.\n",
755                                 CLUSTER_NAME(cluster));
756
757         fclose(lib_test);
758 }