PostgreSQL Source Code  git master
check.c
Go to the documentation of this file.
1 /*
2  * check.c
3  *
4  * server checks and output routines
5  *
6  * Copyright (c) 2010-2019, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/check.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "catalog/pg_authid_d.h"
13 #include "fe_utils/string_utils.h"
14 #include "mb/pg_wchar.h"
15 #include "pg_upgrade.h"
16 
17 
18 static void check_new_cluster_is_empty(void);
19 static void check_databases_are_compatible(void);
20 static void check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb);
21 static bool equivalent_locale(int category, const char *loca, const char *locb);
30 static char *get_canonical_locale_name(int category, const char *locale);
31 
32 
33 /*
34  * fix_path_separator
35  * For non-Windows, just return the argument.
36  * For Windows convert any forward slash to a backslash
37  * such as is suitable for arguments to builtin commands
38  * like RMDIR and DEL.
39  */
40 static char *
41 fix_path_separator(char *path)
42 {
43 #ifdef WIN32
44 
45  char *result;
46  char *c;
47 
48  result = pg_strdup(path);
49 
50  for (c = result; *c != '\0'; c++)
51  if (*c == '/')
52  *c = '\\';
53 
54  return result;
55 #else
56 
57  return path;
58 #endif
59 }
60 
61 void
62 output_check_banner(bool live_check)
63 {
64  if (user_opts.check && live_check)
65  {
67  "Performing Consistency Checks on Old Live Server\n"
68  "------------------------------------------------\n");
69  }
70  else
71  {
73  "Performing Consistency Checks\n"
74  "-----------------------------\n");
75  }
76 }
77 
78 
79 void
81 {
82  /* -- OLD -- */
83 
84  if (!live_check)
86 
87  /* Extract a list of databases and tables from the old cluster */
89 
91 
93 
94 
95  /*
96  * Check for various failure cases
97  */
103 
104  /*
105  * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
106  * supported anymore. Verify there are none, iff applicable.
107  */
110 
111  /*
112  * PG 12 changed the 'sql_identifier' type storage to be based on name,
113  * not varchar, which breaks on-disk format for existing data. So we need
114  * to prevent upgrade when used in user objects (tables, indexes, ...).
115  */
118 
119  /*
120  * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
121  * hash indexes
122  */
124  {
126  if (user_opts.check)
128  }
129 
130  /* 9.5 and below should not have roles starting with pg_ */
133 
137 
138  /* Pre-PG 9.4 had a different 'line' data type internal format */
141 
142  /* Pre-PG 9.0 had no large object permissions */
145 
146  /*
147  * While not a check option, we do this now because this is the only time
148  * the old server is running.
149  */
150  if (!user_opts.check)
152 
153  if (!live_check)
154  stop_postmaster(false);
155 }
156 
157 
158 void
160 {
162 
165 
167 
168  switch (user_opts.transfer_mode)
169  {
170  case TRANSFER_MODE_CLONE:
172  break;
173  case TRANSFER_MODE_COPY:
174  break;
175  case TRANSFER_MODE_LINK:
176  check_hard_link();
177  break;
178  }
179 
181 
183 }
184 
185 
186 void
188 {
189  if (user_opts.check)
190  {
191  pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
192  /* stops new cluster */
193  stop_postmaster(false);
194  exit(0);
195  }
196 
197  pg_log(PG_REPORT, "\n"
198  "If pg_upgrade fails after this point, you must re-initdb the\n"
199  "new cluster before continuing.\n");
200 }
201 
202 
203 void
205 {
206  /*
207  * We unconditionally start/stop the new server because pg_resetwal -o set
208  * wal_level to 'minimum'. If the user is upgrading standby servers using
209  * the rsync instructions, they will need pg_upgrade to write its final
210  * WAL record showing wal_level as 'replica'.
211  */
213 
214  /* Create dummy large object permissions for old < PG 9.0? */
217 
218  /* Reindex hash indexes for old < 10.0 */
221 
222  stop_postmaster(false);
223 }
224 
225 
226 void
227 output_completion_banner(char *analyze_script_file_name,
228  char *deletion_script_file_name)
229 {
230  /* Did we copy the free space files? */
233  "Optimizer statistics are not transferred by pg_upgrade so,\n"
234  "once you start the new server, consider running:\n"
235  " %s\n\n", analyze_script_file_name);
236  else
238  "Optimizer statistics and free space information are not transferred\n"
239  "by pg_upgrade so, once you start the new server, consider running:\n"
240  " %s\n\n", analyze_script_file_name);
241 
242 
243  if (deletion_script_file_name)
245  "Running this script will delete the old cluster's data files:\n"
246  " %s\n",
247  deletion_script_file_name);
248  else
250  "Could not create a script to delete the old cluster's data files\n"
251  "because user-defined tablespaces or the new cluster's data directory\n"
252  "exist in the old cluster directory. The old cluster's contents must\n"
253  "be deleted manually.\n");
254 }
255 
256 
257 void
259 {
260  prep_status("Checking cluster versions");
261 
262  /* cluster versions should already have been obtained */
265 
266  /*
267  * We allow upgrades from/to the same major version for alpha/beta
268  * upgrades
269  */
270 
272  pg_fatal("This utility can only upgrade from PostgreSQL version 8.4 and later.\n");
273 
274  /* Only current PG version is supported as a target */
276  pg_fatal("This utility can only upgrade to PostgreSQL version %s.\n",
277  PG_MAJORVERSION);
278 
279  /*
280  * We can't allow downgrading because we use the target pg_dump, and
281  * pg_dump cannot operate on newer database versions, only current and
282  * older versions.
283  */
285  pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
286 
287  /* Ensure binaries match the designated data directories */
290  pg_fatal("Old cluster data and binary directories are from different major versions.\n");
293  pg_fatal("New cluster data and binary directories are from different major versions.\n");
294 
295  check_ok();
296 }
297 
298 
299 void
301 {
302  /* get/check pg_control data of servers */
303  get_control_data(&old_cluster, live_check);
304  get_control_data(&new_cluster, false);
306 
307  /* We read the real port number for PG >= 9.1 */
308  if (live_check && GET_MAJOR_VERSION(old_cluster.major_version) < 901 &&
310  pg_fatal("When checking a pre-PG 9.1 live old server, "
311  "you must specify the old server's port number.\n");
312 
313  if (live_check && old_cluster.port == new_cluster.port)
314  pg_fatal("When checking a live server, "
315  "the old and new port numbers must be different.\n");
316 }
317 
318 
319 /*
320  * check_locale_and_encoding()
321  *
322  * Check that locale and encoding of a database in the old and new clusters
323  * are compatible.
324  */
325 static void
327 {
328  if (olddb->db_encoding != newdb->db_encoding)
329  pg_fatal("encodings for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
330  olddb->db_name,
333  if (!equivalent_locale(LC_COLLATE, olddb->db_collate, newdb->db_collate))
334  pg_fatal("lc_collate values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
335  olddb->db_name, olddb->db_collate, newdb->db_collate);
336  if (!equivalent_locale(LC_CTYPE, olddb->db_ctype, newdb->db_ctype))
337  pg_fatal("lc_ctype values for database \"%s\" do not match: old \"%s\", new \"%s\"\n",
338  olddb->db_name, olddb->db_ctype, newdb->db_ctype);
339 }
340 
341 /*
342  * equivalent_locale()
343  *
344  * Best effort locale-name comparison. Return false if we are not 100% sure
345  * the locales are equivalent.
346  *
347  * Note: The encoding parts of the names are ignored. This function is
348  * currently used to compare locale names stored in pg_database, and
349  * pg_database contains a separate encoding field. That's compared directly
350  * in check_locale_and_encoding().
351  */
352 static bool
353 equivalent_locale(int category, const char *loca, const char *locb)
354 {
355  const char *chara;
356  const char *charb;
357  char *canona;
358  char *canonb;
359  int lena;
360  int lenb;
361 
362  /*
363  * If the names are equal, the locales are equivalent. Checking this first
364  * avoids calling setlocale() in the common case that the names are equal.
365  * That's a good thing, if setlocale() is buggy, for example.
366  */
367  if (pg_strcasecmp(loca, locb) == 0)
368  return true;
369 
370  /*
371  * Not identical. Canonicalize both names, remove the encoding parts, and
372  * try again.
373  */
374  canona = get_canonical_locale_name(category, loca);
375  chara = strrchr(canona, '.');
376  lena = chara ? (chara - canona) : strlen(canona);
377 
378  canonb = get_canonical_locale_name(category, locb);
379  charb = strrchr(canonb, '.');
380  lenb = charb ? (charb - canonb) : strlen(canonb);
381 
382  if (lena == lenb && pg_strncasecmp(canona, canonb, lena) == 0)
383  {
384  pg_free(canona);
385  pg_free(canonb);
386  return true;
387  }
388 
389  pg_free(canona);
390  pg_free(canonb);
391  return false;
392 }
393 
394 
395 static void
397 {
398  int dbnum;
399 
400  for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
401  {
402  int relnum;
403  RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
404 
405  for (relnum = 0; relnum < rel_arr->nrels;
406  relnum++)
407  {
408  /* pg_largeobject and its index should be skipped */
409  if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
410  pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"\n",
411  new_cluster.dbarr.dbs[dbnum].db_name,
412  rel_arr->rels[relnum].nspname,
413  rel_arr->rels[relnum].relname);
414  }
415  }
416 }
417 
418 /*
419  * Check that every database that already exists in the new cluster is
420  * compatible with the corresponding database in the old one.
421  */
422 static void
424 {
425  int newdbnum;
426  int olddbnum;
427  DbInfo *newdbinfo;
428  DbInfo *olddbinfo;
429 
430  for (newdbnum = 0; newdbnum < new_cluster.dbarr.ndbs; newdbnum++)
431  {
432  newdbinfo = &new_cluster.dbarr.dbs[newdbnum];
433 
434  /* Find the corresponding database in the old cluster */
435  for (olddbnum = 0; olddbnum < old_cluster.dbarr.ndbs; olddbnum++)
436  {
437  olddbinfo = &old_cluster.dbarr.dbs[olddbnum];
438  if (strcmp(newdbinfo->db_name, olddbinfo->db_name) == 0)
439  {
440  check_locale_and_encoding(olddbinfo, newdbinfo);
441  break;
442  }
443  }
444  }
445 }
446 
447 
448 /*
449  * create_script_for_cluster_analyze()
450  *
451  * This incrementally generates better optimizer statistics
452  */
453 void
454 create_script_for_cluster_analyze(char **analyze_script_file_name)
455 {
456  FILE *script = NULL;
457  PQExpBufferData user_specification;
458 
459  prep_status("Creating script to analyze new cluster");
460 
461  initPQExpBuffer(&user_specification);
463  {
464  appendPQExpBufferStr(&user_specification, "-U ");
465  appendShellString(&user_specification, os_info.user);
466  appendPQExpBufferChar(&user_specification, ' ');
467  }
468 
469  *analyze_script_file_name = psprintf("%sanalyze_new_cluster.%s",
471 
472  if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
473  pg_fatal("could not open file \"%s\": %s\n",
474  *analyze_script_file_name, strerror(errno));
475 
476 #ifndef WIN32
477  /* add shebang header */
478  fprintf(script, "#!/bin/sh\n\n");
479 #else
480  /* suppress command echoing */
481  fprintf(script, "@echo off\n");
482 #endif
483 
484  fprintf(script, "echo %sThis script will generate minimal optimizer statistics rapidly%s\n",
486  fprintf(script, "echo %sso your system is usable, and then gather statistics twice more%s\n",
488  fprintf(script, "echo %swith increasing accuracy. When it is done, your system will%s\n",
490  fprintf(script, "echo %shave the default level of optimizer statistics.%s\n",
492  fprintf(script, "echo%s\n\n", ECHO_BLANK);
493 
494  fprintf(script, "echo %sIf you have used ALTER TABLE to modify the statistics target for%s\n",
496  fprintf(script, "echo %sany tables, you might want to remove them and restore them after%s\n",
498  fprintf(script, "echo %srunning this script because they will delay fast statistics generation.%s\n",
500  fprintf(script, "echo%s\n\n", ECHO_BLANK);
501 
502  fprintf(script, "echo %sIf you would like default statistics as quickly as possible, cancel%s\n",
504  fprintf(script, "echo %sthis script and run:%s\n",
506  fprintf(script, "echo %s \"%s/vacuumdb\" %s--all %s%s\n", ECHO_QUOTE,
507  new_cluster.bindir, user_specification.data,
508  /* Did we copy the free space files? */
510  "--analyze-only" : "--analyze", ECHO_QUOTE);
511  fprintf(script, "echo%s\n\n", ECHO_BLANK);
512 
513  fprintf(script, "\"%s/vacuumdb\" %s--all --analyze-in-stages\n",
514  new_cluster.bindir, user_specification.data);
515  /* Did we copy the free space files? */
517  fprintf(script, "\"%s/vacuumdb\" %s--all\n", new_cluster.bindir,
518  user_specification.data);
519 
520  fprintf(script, "echo%s\n\n", ECHO_BLANK);
521  fprintf(script, "echo %sDone%s\n",
523 
524  fclose(script);
525 
526 #ifndef WIN32
527  if (chmod(*analyze_script_file_name, S_IRWXU) != 0)
528  pg_fatal("could not add execute permission to file \"%s\": %s\n",
529  *analyze_script_file_name, strerror(errno));
530 #endif
531 
532  termPQExpBuffer(&user_specification);
533 
534  check_ok();
535 }
536 
537 
538 /*
539  * create_script_for_old_cluster_deletion()
540  *
541  * This is particularly useful for tablespace deletion.
542  */
543 void
544 create_script_for_old_cluster_deletion(char **deletion_script_file_name)
545 {
546  FILE *script = NULL;
547  int tblnum;
548  char old_cluster_pgdata[MAXPGPATH],
549  new_cluster_pgdata[MAXPGPATH];
550 
551  *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
553 
554  strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
555  canonicalize_path(old_cluster_pgdata);
556 
557  strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
558  canonicalize_path(new_cluster_pgdata);
559 
560  /* Some people put the new data directory inside the old one. */
561  if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
562  {
564  "\nWARNING: new data directory should not be inside the old data directory, e.g. %s\n", old_cluster_pgdata);
565 
566  /* Unlink file in case it is left over from a previous run. */
567  unlink(*deletion_script_file_name);
568  pg_free(*deletion_script_file_name);
569  *deletion_script_file_name = NULL;
570  return;
571  }
572 
573  /*
574  * Some users (oddly) create tablespaces inside the cluster data
575  * directory. We can't create a proper old cluster delete script in that
576  * case.
577  */
578  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
579  {
580  char old_tablespace_dir[MAXPGPATH];
581 
582  strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
583  canonicalize_path(old_tablespace_dir);
584  if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
585  {
586  /* reproduce warning from CREATE TABLESPACE that is in the log */
588  "\nWARNING: user-defined tablespace locations should not be inside the data directory, e.g. %s\n", old_tablespace_dir);
589 
590  /* Unlink file in case it is left over from a previous run. */
591  unlink(*deletion_script_file_name);
592  pg_free(*deletion_script_file_name);
593  *deletion_script_file_name = NULL;
594  return;
595  }
596  }
597 
598  prep_status("Creating script to delete old cluster");
599 
600  if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
601  pg_fatal("could not open file \"%s\": %s\n",
602  *deletion_script_file_name, strerror(errno));
603 
604 #ifndef WIN32
605  /* add shebang header */
606  fprintf(script, "#!/bin/sh\n\n");
607 #endif
608 
609  /* delete old cluster's default tablespace */
610  fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
612 
613  /* delete old cluster's alternate tablespaces */
614  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
615  {
616  /*
617  * Do the old cluster's per-database directories share a directory
618  * with a new version-specific tablespace?
619  */
620  if (strlen(old_cluster.tablespace_suffix) == 0)
621  {
622  /* delete per-database directories */
623  int dbnum;
624 
625  fprintf(script, "\n");
626  /* remove PG_VERSION? */
628  fprintf(script, RM_CMD " %s%cPG_VERSION\n",
631 
632  for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
633  fprintf(script, RMDIR_CMD " %c%s%c%d%c\n", PATH_QUOTE,
636  PATH_QUOTE);
637  }
638  else
639  {
640  char *suffix_path = pg_strdup(old_cluster.tablespace_suffix);
641 
642  /*
643  * Simply delete the tablespace directory, which might be ".old"
644  * or a version-specific subdirectory.
645  */
646  fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
648  fix_path_separator(suffix_path), PATH_QUOTE);
649  pfree(suffix_path);
650  }
651  }
652 
653  fclose(script);
654 
655 #ifndef WIN32
656  if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
657  pg_fatal("could not add execute permission to file \"%s\": %s\n",
658  *deletion_script_file_name, strerror(errno));
659 #endif
660 
661  check_ok();
662 }
663 
664 
665 /*
666  * check_is_install_user()
667  *
668  * Check we are the install user, and that the new cluster
669  * has no other users.
670  */
671 static void
673 {
674  PGresult *res;
675  PGconn *conn = connectToServer(cluster, "template1");
676 
677  prep_status("Checking database user is the install user");
678 
679  /* Can't use pg_authid because only superusers can view it. */
680  res = executeQueryOrDie(conn,
681  "SELECT rolsuper, oid "
682  "FROM pg_catalog.pg_roles "
683  "WHERE rolname = current_user "
684  "AND rolname !~ '^pg_'");
685 
686  /*
687  * We only allow the install user in the new cluster (see comment below)
688  * and we preserve pg_authid.oid, so this must be the install user in the
689  * old cluster too.
690  */
691  if (PQntuples(res) != 1 ||
692  atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
693  pg_fatal("database user \"%s\" is not the install user\n",
694  os_info.user);
695 
696  PQclear(res);
697 
698  res = executeQueryOrDie(conn,
699  "SELECT COUNT(*) "
700  "FROM pg_catalog.pg_roles "
701  "WHERE rolname !~ '^pg_'");
702 
703  if (PQntuples(res) != 1)
704  pg_fatal("could not determine the number of users\n");
705 
706  /*
707  * We only allow the install user in the new cluster because other defined
708  * users might match users defined in the old cluster and generate an
709  * error during pg_dump restore.
710  */
711  if (cluster == &new_cluster && atooid(PQgetvalue(res, 0, 0)) != 1)
712  pg_fatal("Only the install user can be defined in the new cluster.\n");
713 
714  PQclear(res);
715 
716  PQfinish(conn);
717 
718  check_ok();
719 }
720 
721 
722 static void
724 {
725  int dbnum;
726  PGconn *conn_template1;
727  PGresult *dbres;
728  int ntups;
729  int i_datname;
730  int i_datallowconn;
731 
732  prep_status("Checking database connection settings");
733 
734  conn_template1 = connectToServer(cluster, "template1");
735 
736  /* get database names */
737  dbres = executeQueryOrDie(conn_template1,
738  "SELECT datname, datallowconn "
739  "FROM pg_catalog.pg_database");
740 
741  i_datname = PQfnumber(dbres, "datname");
742  i_datallowconn = PQfnumber(dbres, "datallowconn");
743 
744  ntups = PQntuples(dbres);
745  for (dbnum = 0; dbnum < ntups; dbnum++)
746  {
747  char *datname = PQgetvalue(dbres, dbnum, i_datname);
748  char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
749 
750  if (strcmp(datname, "template0") == 0)
751  {
752  /* avoid restore failure when pg_dumpall tries to create template0 */
753  if (strcmp(datallowconn, "t") == 0)
754  pg_fatal("template0 must not allow connections, "
755  "i.e. its pg_database.datallowconn must be false\n");
756  }
757  else
758  {
759  /*
760  * avoid datallowconn == false databases from being skipped on
761  * restore
762  */
763  if (strcmp(datallowconn, "f") == 0)
764  pg_fatal("All non-template0 databases must allow connections, "
765  "i.e. their pg_database.datallowconn must be true\n");
766  }
767  }
768 
769  PQclear(dbres);
770 
771  PQfinish(conn_template1);
772 
773  check_ok();
774 }
775 
776 
777 /*
778  * check_for_prepared_transactions()
779  *
780  * Make sure there are no prepared transactions because the storage format
781  * might have changed.
782  */
783 static void
785 {
786  PGresult *res;
787  PGconn *conn = connectToServer(cluster, "template1");
788 
789  prep_status("Checking for prepared transactions");
790 
791  res = executeQueryOrDie(conn,
792  "SELECT * "
793  "FROM pg_catalog.pg_prepared_xacts");
794 
795  if (PQntuples(res) != 0)
796  {
797  if (cluster == &old_cluster)
798  pg_fatal("The source cluster contains prepared transactions\n");
799  else
800  pg_fatal("The target cluster contains prepared transactions\n");
801  }
802 
803  PQclear(res);
804 
805  PQfinish(conn);
806 
807  check_ok();
808 }
809 
810 
811 /*
812  * check_for_isn_and_int8_passing_mismatch()
813  *
814  * contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
815  * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
816  * it must match for the old and new servers.
817  */
818 static void
820 {
821  int dbnum;
822  FILE *script = NULL;
823  bool found = false;
824  char output_path[MAXPGPATH];
825 
826  prep_status("Checking for contrib/isn with bigint-passing mismatch");
827 
830  {
831  /* no mismatch */
832  check_ok();
833  return;
834  }
835 
836  snprintf(output_path, sizeof(output_path),
837  "contrib_isn_and_int8_pass_by_value.txt");
838 
839  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
840  {
841  PGresult *res;
842  bool db_used = false;
843  int ntups;
844  int rowno;
845  int i_nspname,
846  i_proname;
847  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
848  PGconn *conn = connectToServer(cluster, active_db->db_name);
849 
850  /* Find any functions coming from contrib/isn */
851  res = executeQueryOrDie(conn,
852  "SELECT n.nspname, p.proname "
853  "FROM pg_catalog.pg_proc p, "
854  " pg_catalog.pg_namespace n "
855  "WHERE p.pronamespace = n.oid AND "
856  " p.probin = '$libdir/isn'");
857 
858  ntups = PQntuples(res);
859  i_nspname = PQfnumber(res, "nspname");
860  i_proname = PQfnumber(res, "proname");
861  for (rowno = 0; rowno < ntups; rowno++)
862  {
863  found = true;
864  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
865  pg_fatal("could not open file \"%s\": %s\n",
866  output_path, strerror(errno));
867  if (!db_used)
868  {
869  fprintf(script, "In database: %s\n", active_db->db_name);
870  db_used = true;
871  }
872  fprintf(script, " %s.%s\n",
873  PQgetvalue(res, rowno, i_nspname),
874  PQgetvalue(res, rowno, i_proname));
875  }
876 
877  PQclear(res);
878 
879  PQfinish(conn);
880  }
881 
882  if (script)
883  fclose(script);
884 
885  if (found)
886  {
887  pg_log(PG_REPORT, "fatal\n");
888  pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
889  "bigint data type. Your old and new clusters pass bigint values\n"
890  "differently so this cluster cannot currently be upgraded. You can\n"
891  "manually upgrade databases that use \"contrib/isn\" facilities and remove\n"
892  "\"contrib/isn\" from the old cluster and restart the upgrade. A list of\n"
893  "the problem functions is in the file:\n"
894  " %s\n\n", output_path);
895  }
896  else
897  check_ok();
898 }
899 
900 
901 /*
902  * Verify that no tables are declared WITH OIDS.
903  */
904 static void
906 {
907  int dbnum;
908  FILE *script = NULL;
909  bool found = false;
910  char output_path[MAXPGPATH];
911 
912  prep_status("Checking for tables WITH OIDS");
913 
914  snprintf(output_path, sizeof(output_path),
915  "tables_with_oids.txt");
916 
917  /* Find any tables declared WITH OIDS */
918  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
919  {
920  PGresult *res;
921  bool db_used = false;
922  int ntups;
923  int rowno;
924  int i_nspname,
925  i_relname;
926  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
927  PGconn *conn = connectToServer(cluster, active_db->db_name);
928 
929  res = executeQueryOrDie(conn,
930  "SELECT n.nspname, c.relname "
931  "FROM pg_catalog.pg_class c, "
932  " pg_catalog.pg_namespace n "
933  "WHERE c.relnamespace = n.oid AND "
934  " c.relhasoids AND"
935  " n.nspname NOT IN ('pg_catalog')");
936 
937  ntups = PQntuples(res);
938  i_nspname = PQfnumber(res, "nspname");
939  i_relname = PQfnumber(res, "relname");
940  for (rowno = 0; rowno < ntups; rowno++)
941  {
942  found = true;
943  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
944  pg_fatal("could not open file \"%s\": %s\n",
945  output_path, strerror(errno));
946  if (!db_used)
947  {
948  fprintf(script, "In database: %s\n", active_db->db_name);
949  db_used = true;
950  }
951  fprintf(script, " %s.%s\n",
952  PQgetvalue(res, rowno, i_nspname),
953  PQgetvalue(res, rowno, i_relname));
954  }
955 
956  PQclear(res);
957 
958  PQfinish(conn);
959  }
960 
961  if (script)
962  fclose(script);
963 
964  if (found)
965  {
966  pg_log(PG_REPORT, "fatal\n");
967  pg_fatal("Your installation contains tables declared WITH OIDS, which is not supported\n"
968  "anymore. Consider removing the oid column using\n"
969  " ALTER TABLE ... SET WITHOUT OIDS;\n"
970  "A list of tables with the problem is in the file:\n"
971  " %s\n\n", output_path);
972  }
973  else
974  check_ok();
975 }
976 
977 
978 /*
979  * check_for_reg_data_type_usage()
980  * pg_upgrade only preserves these system values:
981  * pg_class.oid
982  * pg_type.oid
983  * pg_enum.oid
984  *
985  * Many of the reg* data types reference system catalog info that is
986  * not preserved, and hence these data types cannot be used in user
987  * tables upgraded by pg_upgrade.
988  */
989 static void
991 {
992  int dbnum;
993  FILE *script = NULL;
994  bool found = false;
995  char output_path[MAXPGPATH];
996 
997  prep_status("Checking for reg* data types in user tables");
998 
999  snprintf(output_path, sizeof(output_path), "tables_using_reg.txt");
1000 
1001  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1002  {
1003  PGresult *res;
1004  bool db_used = false;
1005  int ntups;
1006  int rowno;
1007  int i_nspname,
1008  i_relname,
1009  i_attname;
1010  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1011  PGconn *conn = connectToServer(cluster, active_db->db_name);
1012 
1013  /*
1014  * While several relkinds don't store any data, e.g. views, they can
1015  * be used to define data types of other columns, so we check all
1016  * relkinds.
1017  */
1018  res = executeQueryOrDie(conn,
1019  "SELECT n.nspname, c.relname, a.attname "
1020  "FROM pg_catalog.pg_class c, "
1021  " pg_catalog.pg_namespace n, "
1022  " pg_catalog.pg_attribute a, "
1023  " pg_catalog.pg_type t "
1024  "WHERE c.oid = a.attrelid AND "
1025  " NOT a.attisdropped AND "
1026  " a.atttypid = t.oid AND "
1027  " t.typnamespace = "
1028  " (SELECT oid FROM pg_namespace "
1029  " WHERE nspname = 'pg_catalog') AND"
1030  " t.typname IN ( "
1031  /* regclass.oid is preserved, so 'regclass' is OK */
1032  " 'regconfig', "
1033  " 'regdictionary', "
1034  " 'regnamespace', "
1035  " 'regoper', "
1036  " 'regoperator', "
1037  " 'regproc', "
1038  " 'regprocedure' "
1039  /* regrole.oid is preserved, so 'regrole' is OK */
1040  /* regtype.oid is preserved, so 'regtype' is OK */
1041  " ) AND "
1042  " c.relnamespace = n.oid AND "
1043  " n.nspname NOT IN ('pg_catalog', 'information_schema')");
1044 
1045  ntups = PQntuples(res);
1046  i_nspname = PQfnumber(res, "nspname");
1047  i_relname = PQfnumber(res, "relname");
1048  i_attname = PQfnumber(res, "attname");
1049  for (rowno = 0; rowno < ntups; rowno++)
1050  {
1051  found = true;
1052  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1053  pg_fatal("could not open file \"%s\": %s\n",
1054  output_path, strerror(errno));
1055  if (!db_used)
1056  {
1057  fprintf(script, "In database: %s\n", active_db->db_name);
1058  db_used = true;
1059  }
1060  fprintf(script, " %s.%s.%s\n",
1061  PQgetvalue(res, rowno, i_nspname),
1062  PQgetvalue(res, rowno, i_relname),
1063  PQgetvalue(res, rowno, i_attname));
1064  }
1065 
1066  PQclear(res);
1067 
1068  PQfinish(conn);
1069  }
1070 
1071  if (script)
1072  fclose(script);
1073 
1074  if (found)
1075  {
1076  pg_log(PG_REPORT, "fatal\n");
1077  pg_fatal("Your installation contains one of the reg* data types in user tables.\n"
1078  "These data types reference system OIDs that are not preserved by\n"
1079  "pg_upgrade, so this cluster cannot currently be upgraded. You can\n"
1080  "remove the problem tables and restart the upgrade. A list of the problem\n"
1081  "columns is in the file:\n"
1082  " %s\n\n", output_path);
1083  }
1084  else
1085  check_ok();
1086 }
1087 
1088 
1089 /*
1090  * check_for_jsonb_9_4_usage()
1091  *
1092  * JSONB changed its storage format during 9.4 beta, so check for it.
1093  */
1094 static void
1096 {
1097  int dbnum;
1098  FILE *script = NULL;
1099  bool found = false;
1100  char output_path[MAXPGPATH];
1101 
1102  prep_status("Checking for incompatible \"jsonb\" data type");
1103 
1104  snprintf(output_path, sizeof(output_path), "tables_using_jsonb.txt");
1105 
1106  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
1107  {
1108  PGresult *res;
1109  bool db_used = false;
1110  int ntups;
1111  int rowno;
1112  int i_nspname,
1113  i_relname,
1114  i_attname;
1115  DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
1116  PGconn *conn = connectToServer(cluster, active_db->db_name);
1117 
1118  /*
1119  * While several relkinds don't store any data, e.g. views, they can
1120  * be used to define data types of other columns, so we check all
1121  * relkinds.
1122  */
1123  res = executeQueryOrDie(conn,
1124  "SELECT n.nspname, c.relname, a.attname "
1125  "FROM pg_catalog.pg_class c, "
1126  " pg_catalog.pg_namespace n, "
1127  " pg_catalog.pg_attribute a "
1128  "WHERE c.oid = a.attrelid AND "
1129  " NOT a.attisdropped AND "
1130  " a.atttypid = 'pg_catalog.jsonb'::pg_catalog.regtype AND "
1131  " c.relnamespace = n.oid AND "
1132  /* exclude possible orphaned temp tables */
1133  " n.nspname !~ '^pg_temp_' AND "
1134  " n.nspname NOT IN ('pg_catalog', 'information_schema')");
1135 
1136  ntups = PQntuples(res);
1137  i_nspname = PQfnumber(res, "nspname");
1138  i_relname = PQfnumber(res, "relname");
1139  i_attname = PQfnumber(res, "attname");
1140  for (rowno = 0; rowno < ntups; rowno++)
1141  {
1142  found = true;
1143  if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
1144  pg_fatal("could not open file \"%s\": %s\n",
1145  output_path, strerror(errno));
1146  if (!db_used)
1147  {
1148  fprintf(script, "In database: %s\n", active_db->db_name);
1149  db_used = true;
1150  }
1151  fprintf(script, " %s.%s.%s\n",
1152  PQgetvalue(res, rowno, i_nspname),
1153  PQgetvalue(res, rowno, i_relname),
1154  PQgetvalue(res, rowno, i_attname));
1155  }
1156 
1157  PQclear(res);
1158 
1159  PQfinish(conn);
1160  }
1161 
1162  if (script)
1163  fclose(script);
1164 
1165  if (found)
1166  {
1167  pg_log(PG_REPORT, "fatal\n");
1168  pg_fatal("Your installation contains the \"jsonb\" data type in user tables.\n"
1169  "The internal format of \"jsonb\" changed during 9.4 beta so this cluster cannot currently\n"
1170  "be upgraded. You can remove the problem tables and restart the upgrade. A list\n"
1171  "of the problem columns is in the file:\n"
1172  " %s\n\n", output_path);
1173  }
1174  else
1175  check_ok();
1176 }
1177 
1178 /*
1179  * check_for_pg_role_prefix()
1180  *
1181  * Versions older than 9.6 should not have any pg_* roles
1182  */
1183 static void
1185 {
1186  PGresult *res;
1187  PGconn *conn = connectToServer(cluster, "template1");
1188 
1189  prep_status("Checking for roles starting with \"pg_\"");
1190 
1191  res = executeQueryOrDie(conn,
1192  "SELECT * "
1193  "FROM pg_catalog.pg_roles "
1194  "WHERE rolname ~ '^pg_'");
1195 
1196  if (PQntuples(res) != 0)
1197  {
1198  if (cluster == &old_cluster)
1199  pg_fatal("The source cluster contains roles starting with \"pg_\"\n");
1200  else
1201  pg_fatal("The target cluster contains roles starting with \"pg_\"\n");
1202  }
1203 
1204  PQclear(res);
1205 
1206  PQfinish(conn);
1207 
1208  check_ok();
1209 }
1210 
1211 
1212 /*
1213  * get_canonical_locale_name
1214  *
1215  * Send the locale name to the system, and hope we get back a canonical
1216  * version. This should match the backend's check_locale() function.
1217  */
1218 static char *
1219 get_canonical_locale_name(int category, const char *locale)
1220 {
1221  char *save;
1222  char *res;
1223 
1224  /* get the current setting, so we can restore it. */
1225  save = setlocale(category, NULL);
1226  if (!save)
1227  pg_fatal("failed to get the current locale\n");
1228 
1229  /* 'save' may be pointing at a modifiable scratch variable, so copy it. */
1230  save = pg_strdup(save);
1231 
1232  /* set the locale with setlocale, to see if it accepts it. */
1233  res = setlocale(category, locale);
1234 
1235  if (!res)
1236  pg_fatal("failed to get system locale name for \"%s\"\n", locale);
1237 
1238  res = pg_strdup(res);
1239 
1240  /* restore old value. */
1241  if (!setlocale(category, save))
1242  pg_fatal("failed to restore old locale \"%s\"\n", save);
1243 
1244  pg_free(save);
1245 
1246  return res;
1247 }
void check_file_clone(void)
Definition: file.c:322
uint32 major_version
Definition: pg_upgrade.h:269
void check_hard_link(void)
Definition: file.c:364
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3164
static void check_databases_are_compatible(void)
Definition: check.c:423
#define setlocale(a, b)
Definition: win32_port.h:408
ControlData controldata
Definition: pg_upgrade.h:259
void check_loadable_libraries(void)
Definition: function.c:182
void check_cluster_versions(void)
Definition: check.c:258
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:131
bool path_is_prefix_of_path(const char *path1, const char *path2)
Definition: path.c:438
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
#define PATH_SEPARATOR
Definition: pg_upgrade.h:69
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define SCRIPT_EXT
Definition: pg_upgrade.h:74
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
char * relname
Definition: pg_upgrade.h:136
void canonicalize_path(char *path)
Definition: path.c:254
#define pg_fatal(...)
Definition: pg_rewind.h:43
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4098
void old_9_6_invalidate_hash_indexes(ClusterInfo *cluster, bool check_mode)
Definition: version.c:349
unsigned short port
Definition: pg_upgrade.h:268
NameData datname
Definition: pg_database.h:35
static char * get_canonical_locale_name(int category, const char *locale)
Definition: check.c:1219
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
void check_cluster_compatibility(bool live_check)
Definition: check.c:300
static bool equivalent_locale(int category, const char *loca, const char *locb)
Definition: check.c:353
void check_control_data(ControlData *oldctrl, ControlData *newctrl)
Definition: controldata.c:619
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2770
#define fprintf
Definition: port.h:196
char * nspname
Definition: pg_upgrade.h:135
Oid db_oid
Definition: pg_upgrade.h:180
bool user_specified
Definition: pg_upgrade.h:312
static void check_for_reg_data_type_usage(ClusterInfo *cluster)
Definition: check.c:990
static void check_for_prepared_transactions(ClusterInfo *cluster)
Definition: check.c:784
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
bool datallowconn
Definition: pg_database.h:53
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
char * db_ctype
Definition: pg_upgrade.h:185
void pfree(void *pointer)
Definition: mcxt.c:1056
PGconn * conn
Definition: streamutil.c:56
void output_check_banner(bool live_check)
Definition: check.c:62
#define MAXPGPATH
static void check_proper_datallowconn(ClusterInfo *cluster)
Definition: check.c:723
RelInfo * rels
Definition: pg_upgrade.h:148
#define RMDIR_CMD
Definition: pg_upgrade.h:72
char * c
void prep_status(const char *fmt,...) pg_attribute_printf(1
static void check_for_pg_role_prefix(ClusterInfo *cluster)
Definition: check.c:1184
void new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster, bool check_mode)
Definition: version.c:25
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:103
static void check_for_tables_with_oids(ClusterInfo *cluster)
Definition: check.c:905
void generate_old_dump(void)
Definition: dump.c:18
#define atooid(x)
Definition: postgres_ext.h:42
static void check_ok(void)
Definition: initdb.c:2079
static void check_is_install_user(ClusterInfo *cluster)
Definition: check.c:672
static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
Definition: check.c:819
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
void create_script_for_old_cluster_deletion(char **deletion_script_file_name)
Definition: check.c:544
transferMode transfer_mode
Definition: pg_upgrade.h:294
UserOpts user_opts
Definition: option.c:32
char * bindir
Definition: pg_upgrade.h:264
uint32 cat_ver
Definition: pg_upgrade.h:204
char ** old_tablespaces
Definition: pg_upgrade.h:313
#define DEF_PGUPORT
Definition: pg_upgrade.h:16
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2878
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void stop_postmaster(bool in_atexit)
Definition: server.c:328
const char * tablespace_suffix
Definition: pg_upgrade.h:272
void PQclear(PGresult *res)
Definition: fe-exec.c:695
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:431
static void check_new_cluster_is_empty(void)
Definition: check.c:396
void get_loadable_libraries(void)
Definition: function.c:55
uint32 bin_version
Definition: pg_upgrade.h:271
DbInfoArr dbarr
Definition: pg_upgrade.h:260
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
#define Assert(condition)
Definition: c.h:732
RelInfoArr rel_arr
Definition: pg_upgrade.h:187
void get_db_and_rel_infos(ClusterInfo *cluster)
Definition: info.c:311
void get_control_data(ClusterInfo *cluster, bool live_check)
Definition: controldata.c:34
#define strerror
Definition: port.h:205
#define JSONB_FORMAT_CHANGE_CAT_VER
Definition: pg_upgrade.h:126
#define ECHO_BLANK
Definition: pg_upgrade.h:76
bool check
Definition: pg_upgrade.h:292
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define SCRIPT_PREFIX
Definition: pg_upgrade.h:73
int db_encoding
Definition: pg_upgrade.h:186
static pgpid_t start_postmaster(void)
Definition: pg_ctl.c:445
static void check_for_jsonb_9_4_usage(ClusterInfo *cluster)
Definition: check.c:1095
#define PATH_QUOTE
Definition: pg_upgrade.h:70
void init_tablespaces(void)
Definition: tablespace.c:19
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:382
char * pgdata
Definition: pg_upgrade.h:261
#define S_IRWXU
Definition: win32_port.h:269
int num_old_tablespaces
Definition: pg_upgrade.h:314
OSInfo os_info
Definition: pg_upgrade.c:61
char * db_name
Definition: pg_upgrade.h:181
static char * locale
Definition: initdb.c:125
void old_9_3_check_for_line_data_type_usage(ClusterInfo *cluster)
Definition: version.c:111
void output_completion_banner(char *analyze_script_file_name, char *deletion_script_file_name)
Definition: check.c:227
void create_script_for_cluster_analyze(char **analyze_script_file_name)
Definition: check.c:454
char * db_collate
Definition: pg_upgrade.h:184
void check_new_cluster(void)
Definition: check.c:159
void check_and_dump_old_cluster(bool live_check)
Definition: check.c:80
#define ECHO_QUOTE
Definition: pg_upgrade.h:75
void report_clusters_compatible(void)
Definition: check.c:187
void issue_warnings_and_set_wal_level(void)
Definition: check.c:204
DbInfo * dbs
Definition: pg_upgrade.h:192
static void check_locale_and_encoding(DbInfo *olddb, DbInfo *newdb)
Definition: check.c:326
#define snprintf
Definition: port.h:192
char * user
Definition: pg_upgrade.h:311
#define RM_CMD
Definition: pg_upgrade.h:71
bool float8_pass_by_value
Definition: pg_upgrade.h:222
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
static char * fix_path_separator(char *path)
Definition: check.c:41
void old_11_check_for_sql_identifier_data_type_usage(ClusterInfo *cluster)
Definition: version.c:468
void old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster)
Definition: version.c:237