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