4 * server checks and output routines
6 * Copyright (c) 2010, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/check.c
10 #include "pg_upgrade.h"
13 static void set_locale_and_encoding(ClusterInfo *cluster);
14 static void check_new_db_is_empty(void);
15 static void check_locale_and_encoding(ControlData *oldctrl,
16 ControlData *newctrl);
17 static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
18 static void check_for_reg_data_type_usage(ClusterInfo *cluster);
22 output_check_banner(bool *live_check)
24 if (user_opts.check && is_server_running(old_cluster.pgdata))
27 if (old_cluster.port == new_cluster.port)
28 pg_log(PG_FATAL, "When checking a live server, "
29 "the old and new port numbers must be different.\n");
30 pg_log(PG_REPORT, "PerForming Consistency Checks on Old Live Server\n");
31 pg_log(PG_REPORT, "------------------------------------------------\n");
35 pg_log(PG_REPORT, "Performing Consistency Checks\n");
36 pg_log(PG_REPORT, "-----------------------------\n");
42 check_old_cluster(bool live_check,
43 char **sequence_script_file_name)
48 start_postmaster(&old_cluster, false);
50 set_locale_and_encoding(&old_cluster);
52 get_pg_database_relfilenode(&old_cluster);
54 /* Extract a list of databases and tables from the old cluster */
55 get_db_and_rel_infos(&old_cluster);
59 get_loadable_libraries();
63 * Check for various failure cases
66 check_for_reg_data_type_usage(&old_cluster);
67 check_for_isn_and_int8_passing_mismatch(&old_cluster);
69 /* old = PG 8.3 checks? */
70 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 803)
72 old_8_3_check_for_name_data_type_usage(&old_cluster);
73 old_8_3_check_for_tsquery_usage(&old_cluster);
76 old_8_3_rebuild_tsvector_tables(&old_cluster, true);
77 old_8_3_invalidate_hash_gin_indexes(&old_cluster, true);
78 old_8_3_invalidate_bpchar_pattern_ops_indexes(&old_cluster, true);
83 * While we have the old server running, create the script to
84 * properly restore its sequence values but we report this at the
87 *sequence_script_file_name =
88 old_8_3_create_sequence_script(&old_cluster);
91 /* Pre-PG 9.0 had no large object permissions */
92 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
93 new_9_0_populate_pg_largeobject_metadata(&old_cluster, true);
96 * While not a check option, we do this now because this is the only time
97 * the old server is running.
106 stop_postmaster(false, false);
111 check_new_cluster(void)
113 set_locale_and_encoding(&new_cluster);
115 check_new_db_is_empty();
117 check_loadable_libraries();
119 check_locale_and_encoding(&old_cluster.controldata, &new_cluster.controldata);
121 if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
127 report_clusters_compatible(void)
131 pg_log(PG_REPORT, "\n*Clusters are compatible*\n");
132 /* stops new cluster */
133 stop_postmaster(false, false);
137 pg_log(PG_REPORT, "\n"
138 "| If pg_upgrade fails after this point, you must\n"
139 "| re-initdb the new cluster before continuing.\n"
140 "| You will also need to remove the \".old\" suffix\n"
141 "| from %s/global/pg_control.old.\n", old_cluster.pgdata);
146 issue_warnings(char *sequence_script_file_name)
148 /* old = PG 8.3 warnings? */
149 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 803)
151 start_postmaster(&new_cluster, true);
153 /* restore proper sequence values using file created from old server */
154 if (sequence_script_file_name)
156 prep_status("Adjusting sequences");
158 SYSTEMQUOTE "\"%s/psql\" --set ON_ERROR_STOP=on "
159 "--no-psqlrc --port %d --username \"%s\" "
160 "-f \"%s\" --dbname template1 >> \"%s\"" SYSTEMQUOTE,
161 new_cluster.bindir, new_cluster.port, os_info.user,
162 sequence_script_file_name, log_opts.filename);
163 unlink(sequence_script_file_name);
167 old_8_3_rebuild_tsvector_tables(&new_cluster, false);
168 old_8_3_invalidate_hash_gin_indexes(&new_cluster, false);
169 old_8_3_invalidate_bpchar_pattern_ops_indexes(&new_cluster, false);
170 stop_postmaster(false, true);
173 /* Create dummy large object permissions for old < PG 9.0? */
174 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
176 start_postmaster(&new_cluster, true);
177 new_9_0_populate_pg_largeobject_metadata(&new_cluster, false);
178 stop_postmaster(false, true);
184 output_completion_banner(char *deletion_script_file_name)
186 /* Did we copy the free space files? */
187 if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
189 "| Optimizer statistics is not transferred by pg_upgrade\n"
190 "| so consider running:\n"
191 "| \tvacuumdb --all --analyze-only\n"
192 "| on the newly-upgraded cluster.\n\n");
195 "| Optimizer statistics and free space information\n"
196 "| are not transferred by pg_upgrade so consider\n"
198 "| \tvacuumdb --all --analyze\n"
199 "| on the newly-upgraded cluster.\n\n");
202 "| Running this script will delete the old cluster's data files:\n"
204 deletion_script_file_name);
209 check_cluster_versions(void)
211 /* get old and new cluster versions */
212 old_cluster.major_version = get_major_server_version(&old_cluster, &old_cluster.major_version_str);
213 new_cluster.major_version = get_major_server_version(&new_cluster, &new_cluster.major_version_str);
215 /* We allow upgrades from/to the same major version for alpha/beta upgrades */
217 if (GET_MAJOR_VERSION(old_cluster.major_version) < 803)
218 pg_log(PG_FATAL, "This utility can only upgrade from PostgreSQL version 8.3 and later.\n");
220 /* Only current PG version is supported as a target */
221 if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
222 pg_log(PG_FATAL, "This utility can only upgrade to PostgreSQL version %s.\n",
226 * We can't allow downgrading because we use the target pg_dumpall, and
227 * pg_dumpall cannot operate on new datbase versions, only older versions.
229 if (old_cluster.major_version > new_cluster.major_version)
230 pg_log(PG_FATAL, "This utility cannot be used to downgrade to older major PostgreSQL versions.\n");
235 check_cluster_compatibility(bool live_check)
237 char libfile[MAXPGPATH];
241 * Test pg_upgrade_support.so is in the proper place. We cannot copy it
242 * ourselves because install directories are typically root-owned.
244 snprintf(libfile, sizeof(libfile), "%s/pg_upgrade_support%s", new_cluster.libpath,
247 if ((lib_test = fopen(libfile, "r")) == NULL)
249 "\npg_upgrade_support%s must be created and installed in %s\n", DLSUFFIX, libfile);
253 /* get/check pg_control data of servers */
254 get_control_data(&old_cluster, live_check);
255 get_control_data(&new_cluster, false);
256 check_control_data(&old_cluster.controldata, &new_cluster.controldata);
258 /* Is it 9.0 but without tablespace directories? */
259 if (GET_MAJOR_VERSION(new_cluster.major_version) == 900 &&
260 new_cluster.controldata.cat_ver < TABLE_SPACE_SUBDIRS)
261 pg_log(PG_FATAL, "This utility can only upgrade to PostgreSQL version 9.0 after 2010-01-11\n"
262 "because of backend API changes made during development.\n");
267 * set_locale_and_encoding()
269 * query the database to get the template0 locale
272 set_locale_and_encoding(ClusterInfo *cluster)
274 ControlData *ctrl = &cluster->controldata;
278 int cluster_version = cluster->major_version;
280 conn = connectToServer(cluster, "template1");
282 /* for pg < 80400, we got the values from pg_controldata */
283 if (cluster_version >= 80400)
288 res = executeQueryOrDie(conn,
289 "SELECT datcollate, datctype "
290 "FROM pg_catalog.pg_database "
291 "WHERE datname = 'template0' ");
292 assert(PQntuples(res) == 1);
294 i_datcollate = PQfnumber(res, "datcollate");
295 i_datctype = PQfnumber(res, "datctype");
297 ctrl->lc_collate = pg_strdup(PQgetvalue(res, 0, i_datcollate));
298 ctrl->lc_ctype = pg_strdup(PQgetvalue(res, 0, i_datctype));
303 res = executeQueryOrDie(conn,
304 "SELECT pg_catalog.pg_encoding_to_char(encoding) "
305 "FROM pg_catalog.pg_database "
306 "WHERE datname = 'template0' ");
307 assert(PQntuples(res) == 1);
309 i_encoding = PQfnumber(res, "pg_encoding_to_char");
310 ctrl->encoding = pg_strdup(PQgetvalue(res, 0, i_encoding));
319 * check_locale_and_encoding()
321 * locale is not in pg_controldata in 8.4 and later so
322 * we probably had to get via a database query.
325 check_locale_and_encoding(ControlData *oldctrl,
326 ControlData *newctrl)
328 if (strcmp(oldctrl->lc_collate, newctrl->lc_collate) != 0)
330 "old and new cluster lc_collate values do not match\n");
331 if (strcmp(oldctrl->lc_ctype, newctrl->lc_ctype) != 0)
333 "old and new cluster lc_ctype values do not match\n");
334 if (strcmp(oldctrl->encoding, newctrl->encoding) != 0)
336 "old and new cluster encoding values do not match\n");
341 check_new_db_is_empty(void)
346 get_db_and_rel_infos(&new_cluster);
348 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
351 RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
353 for (relnum = 0; relnum < rel_arr->nrels;
356 /* pg_largeobject and its index should be skipped */
357 if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
365 dbarr_free(&new_cluster.dbarr);
368 pg_log(PG_FATAL, "New cluster is not empty; exiting\n");
373 * create_script_for_old_cluster_deletion()
375 * This is particularly useful for tablespace deletion.
378 create_script_for_old_cluster_deletion(
379 char **deletion_script_file_name)
384 *deletion_script_file_name = pg_malloc(MAXPGPATH);
386 prep_status("Creating script to delete old cluster");
388 snprintf(*deletion_script_file_name, MAXPGPATH, "%s/delete_old_cluster.%s",
389 os_info.cwd, SCRIPT_EXT);
391 if ((script = fopen(*deletion_script_file_name, "w")) == NULL)
392 pg_log(PG_FATAL, "Could not create necessary file: %s\n",
393 *deletion_script_file_name);
396 /* add shebang header */
397 fprintf(script, "#!/bin/sh\n\n");
400 /* delete old cluster's default tablespace */
401 fprintf(script, RMDIR_CMD " %s\n", old_cluster.pgdata);
403 /* delete old cluster's alternate tablespaces */
404 for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
407 * Do the old cluster's per-database directories share a directory
408 * with a new version-specific tablespace?
410 if (strlen(old_cluster.tablespace_suffix) == 0)
412 /* delete per-database directories */
415 fprintf(script, "\n");
416 /* remove PG_VERSION? */
417 if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
418 fprintf(script, RM_CMD " %s%s/PG_VERSION\n",
419 os_info.tablespaces[tblnum], old_cluster.tablespace_suffix);
421 for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
423 fprintf(script, RMDIR_CMD " %s%s/%d\n",
424 os_info.tablespaces[tblnum], old_cluster.tablespace_suffix,
425 old_cluster.dbarr.dbs[dbnum].db_oid);
431 * Simply delete the tablespace directory, which might be ".old"
432 * or a version-specific subdirectory.
434 fprintf(script, RMDIR_CMD " %s%s\n",
435 os_info.tablespaces[tblnum], old_cluster.tablespace_suffix);
441 if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
442 pg_log(PG_FATAL, "Could not add execute permission to file: %s\n",
443 *deletion_script_file_name);
451 * check_for_isn_and_int8_passing_mismatch()
453 * /contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
454 * by value. The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
455 * it must match for the old and new servers.
458 check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
463 char output_path[MAXPGPATH];
465 prep_status("Checking for /contrib/isn with bigint-passing mismatch");
467 if (old_cluster.controldata.float8_pass_by_value ==
468 new_cluster.controldata.float8_pass_by_value)
475 snprintf(output_path, sizeof(output_path), "%s/contrib_isn_and_int8_pass_by_value.txt",
478 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
481 bool db_used = false;
486 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
487 PGconn *conn = connectToServer(cluster, active_db->db_name);
489 /* Find any functions coming from contrib/isn */
490 res = executeQueryOrDie(conn,
491 "SELECT n.nspname, p.proname "
492 "FROM pg_catalog.pg_proc p, "
493 " pg_catalog.pg_namespace n "
494 "WHERE p.pronamespace = n.oid AND "
495 " p.probin = '$libdir/isn'");
497 ntups = PQntuples(res);
498 i_nspname = PQfnumber(res, "nspname");
499 i_proname = PQfnumber(res, "proname");
500 for (rowno = 0; rowno < ntups; rowno++)
503 if (script == NULL && (script = fopen(output_path, "w")) == NULL)
504 pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path);
507 fprintf(script, "Database: %s\n", active_db->db_name);
510 fprintf(script, " %s.%s\n",
511 PQgetvalue(res, rowno, i_nspname),
512 PQgetvalue(res, rowno, i_proname));
523 pg_log(PG_REPORT, "fatal\n");
525 "| Your installation contains \"/contrib/isn\" functions\n"
526 "| which rely on the bigint data type. Your old and\n"
527 "| new clusters pass bigint values differently so this\n"
528 "| cluster cannot currently be upgraded. You can\n"
529 "| manually upgrade data that use \"/contrib/isn\"\n"
530 "| facilities and remove \"/contrib/isn\" from the\n"
531 "| old cluster and restart the upgrade. A list\n"
532 "| of the problem functions is in the file:\n"
533 "| \t%s\n\n", output_path);
541 * check_for_reg_data_type_usage()
542 * pg_upgrade only preserves these system values:
543 * pg_class.relfilenode
547 * Most of the reg* data types reference system catalog info that is
548 * not preserved, and hence these data types cannot be used in user
549 * tables upgraded by pg_upgrade.
552 check_for_reg_data_type_usage(ClusterInfo *cluster)
557 char output_path[MAXPGPATH];
559 prep_status("Checking for reg* system oid user data types");
561 snprintf(output_path, sizeof(output_path), "%s/tables_using_reg.txt",
564 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
567 bool db_used = false;
573 DbInfo *active_db = &cluster->dbarr.dbs[dbnum];
574 PGconn *conn = connectToServer(cluster, active_db->db_name);
576 res = executeQueryOrDie(conn,
577 "SELECT n.nspname, c.relname, a.attname "
578 "FROM pg_catalog.pg_class c, "
579 " pg_catalog.pg_namespace n, "
580 " pg_catalog.pg_attribute a "
581 "WHERE c.oid = a.attrelid AND "
582 " NOT a.attisdropped AND "
584 " 'pg_catalog.regproc'::pg_catalog.regtype, "
585 " 'pg_catalog.regprocedure'::pg_catalog.regtype, "
586 " 'pg_catalog.regoper'::pg_catalog.regtype, "
587 " 'pg_catalog.regoperator'::pg_catalog.regtype, "
588 " 'pg_catalog.regclass'::pg_catalog.regtype, "
589 /* regtype.oid is preserved, so 'regtype' is OK */
590 " 'pg_catalog.regconfig'::pg_catalog.regtype, "
591 " 'pg_catalog.regdictionary'::pg_catalog.regtype) AND "
592 " c.relnamespace = n.oid AND "
593 " n.nspname != 'pg_catalog' AND "
594 " n.nspname != 'information_schema'");
596 ntups = PQntuples(res);
597 i_nspname = PQfnumber(res, "nspname");
598 i_relname = PQfnumber(res, "relname");
599 i_attname = PQfnumber(res, "attname");
600 for (rowno = 0; rowno < ntups; rowno++)
603 if (script == NULL && (script = fopen(output_path, "w")) == NULL)
604 pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path);
607 fprintf(script, "Database: %s\n", active_db->db_name);
610 fprintf(script, " %s.%s.%s\n",
611 PQgetvalue(res, rowno, i_nspname),
612 PQgetvalue(res, rowno, i_relname),
613 PQgetvalue(res, rowno, i_attname));
624 pg_log(PG_REPORT, "fatal\n");
626 "| Your installation contains one of the reg* data types in\n"
627 "| user tables. These data types reference system oids that\n"
628 "| are not preserved by pg_upgrade, so this cluster cannot\n"
629 "| currently be upgraded. You can remove the problem tables\n"
630 "| and restart the upgrade. A list of the problem columns\n"
631 "| is in the file:\n"
632 "| \t%s\n\n", output_path);