diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c index 00aef85..abb5f8a 100644 --- a/src/bin/pg_upgrade/check.c +++ b/src/bin/pg_upgrade/check.c @@ -27,6 +27,7 @@ static void check_for_reg_data_type_usage(ClusterInfo *cluster); static void check_for_jsonb_9_4_usage(ClusterInfo *cluster); static void check_for_pg_role_prefix(ClusterInfo *cluster); static char *get_canonical_locale_name(int category, const char *locale); +static void check_for_incomplete_object_definition(ClusterInfo *cluster); /* @@ -142,6 +143,9 @@ check_and_dump_old_cluster(bool live_check) if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) new_9_0_populate_pg_largeobject_metadata(&old_cluster, true); + if (GET_MAJOR_VERSION(old_cluster.major_version) >= 802 && user_opts.check) + check_for_incomplete_object_definition(&old_cluster); + /* * While not a check option, we do this now because this is the only time * the old server is running. @@ -1246,3 +1250,81 @@ get_canonical_locale_name(int category, const char *locale) return res; } + +/* + * check_for_incomplete_object_definition() + */ +static void +check_for_incomplete_object_definition(ClusterInfo *cluster) +{ + int dbnum; + FILE *script = NULL; + bool found = false; + char output_path[MAXPGPATH]; + + prep_status("Checking for incomplete object definition"); + + snprintf(output_path, sizeof(output_path), "incomplete_object_definition.txt"); + + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) + { + PGresult *res; + bool db_used = false; + int ntups; + int rowno; + int i_nspname, + i_relname; + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); + + res = executeQueryOrDie(conn, + "SELECT n.nspname, c.relname " + "FROM pg_catalog.pg_class c, " + " pg_catalog.pg_namespace n, " + " pg_catalog.pg_type t " + "WHERE c.relkind in('r', 'v', 'm', 'p', 'f') AND " + " t.oid = c.reltype AND " + " t.typarray = 0 AND " + " c.relnamespace = n.oid AND " + " n.nspname NOT IN ('pg_catalog', 'information_schema')"); + + ntups = PQntuples(res); + i_nspname = PQfnumber(res, "nspname"); + i_relname = PQfnumber(res, "relname"); + for (rowno = 0; rowno < ntups; rowno++) + { + found = true; + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", + output_path, strerror(errno)); + if (!db_used) + { + fprintf(script, "In database: %s\n", active_db->db_name); + db_used = true; + } + fprintf(script, " %s.%s\n", + PQgetvalue(res, rowno, i_nspname), + PQgetvalue(res, rowno, i_relname)); + } + + PQclear(res); + + PQfinish(conn); + } + + if (script) + fclose(script); + + if (found) + { + pg_log(PG_REPORT, "fatal\n"); + pg_fatal("Your installation contains one or more incomplete table or view definitions.\n" + "They are probably created in single-user mode.\n" + "so this cluster cannot currently be upgraded.\n" + "You can remove the problem tables or views and restart the upgrade.\n" + "A list of the problem objects is in the file:\n" + " %s\n\n", output_path); + } + else + check_ok(); +}