X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=contrib%2Fpg_upgrade%2Fversion_old_8_3.c;h=f58f74511f756d5ab2ee86d74765bf19bc744646;hb=7e04792a1cbd1763edf72474f6b1fbad2cd0ad31;hp=b5ec6f473ea7354094ece437bc1d2cffed49f2f2;hpb=e13f7e9a711c9117e438516537066bbe61df6455;p=postgresql diff --git a/contrib/pg_upgrade/version_old_8_3.c b/contrib/pg_upgrade/version_old_8_3.c index b5ec6f473e..f58f74511f 100644 --- a/contrib/pg_upgrade/version_old_8_3.c +++ b/contrib/pg_upgrade/version_old_8_3.c @@ -3,10 +3,12 @@ * * Postgres-version-specific routines * - * Copyright (c) 2010, PostgreSQL Global Development Group + * Copyright (c) 2010-2014, PostgreSQL Global Development Group * contrib/pg_upgrade/version_old_8_3.c */ +#include "postgres_fe.h" + #include "pg_upgrade.h" #include "access/transam.h" @@ -19,20 +21,18 @@ * checks tables and indexes. */ void -old_8_3_check_for_name_data_type_usage(Cluster whichCluster) +old_8_3_check_for_name_data_type_usage(ClusterInfo *cluster) { - ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster); int dbnum; FILE *script = NULL; bool found = false; char output_path[MAXPGPATH]; - prep_status("Checking for invalid 'name' user columns"); + prep_status("Checking for invalid \"name\" user columns"); - snprintf(output_path, sizeof(output_path), "%s/tables_using_name.txt", - os_info.cwd); + snprintf(output_path, sizeof(output_path), "tables_using_name.txt"); - for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++) + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) { PGresult *res; bool db_used = false; @@ -41,8 +41,8 @@ old_8_3_check_for_name_data_type_usage(Cluster whichCluster) int i_nspname, i_relname, i_attname; - DbInfo *active_db = &active_cluster->dbarr.dbs[dbnum]; - PGconn *conn = connectToServer(active_db->db_name, whichCluster); + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); /* * With a smaller alignment in 8.4, 'name' cannot be used in a @@ -60,8 +60,10 @@ old_8_3_check_for_name_data_type_usage(Cluster whichCluster) " NOT a.attisdropped AND " " a.atttypid = 'pg_catalog.name'::pg_catalog.regtype AND " " c.relnamespace = n.oid AND " - " n.nspname != 'pg_catalog' AND " - " n.nspname != 'information_schema'"); + /* exclude possible orphaned temp tables */ + " n.nspname !~ '^pg_temp_' AND " + " n.nspname !~ '^pg_toast_temp_' AND " + " n.nspname NOT IN ('pg_catalog', 'information_schema')"); ntups = PQntuples(res); i_nspname = PQfnumber(res, "nspname"); @@ -70,11 +72,11 @@ old_8_3_check_for_name_data_type_usage(Cluster whichCluster) for (rowno = 0; rowno < ntups; rowno++) { found = true; - if (script == NULL && (script = fopen(output_path, "w")) == NULL) - pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path); + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno)); if (!db_used) { - fprintf(script, "Database: %s\n", active_db->db_name); + fprintf(script, "Database: %s\n", active_db->db_name); db_used = true; } fprintf(script, " %s.%s.%s\n", @@ -88,18 +90,18 @@ old_8_3_check_for_name_data_type_usage(Cluster whichCluster) PQfinish(conn); } + if (script) + fclose(script); + if (found) { - fclose(script); pg_log(PG_REPORT, "fatal\n"); - pg_log(PG_FATAL, - "| Your installation contains the \"name\" data type in\n" - "| user tables. This data type changed its internal\n" - "| alignment between your old and new clusters so this\n" - "| cluster cannot currently be upgraded. You can\n" - "| remove the problem tables and restart the migration.\n" - "| A list of the problem columns is in the file:\n" - "| \t%s\n\n", output_path); + pg_fatal("Your installation contains the \"name\" data type in user tables. This\n" + "data type changed its internal alignment between your old and new\n" + "clusters so this cluster cannot currently be upgraded. You can remove\n" + "the problem tables and restart the upgrade. A list of the problem\n" + "columns is in the file:\n" + " %s\n\n", output_path); } else check_ok(); @@ -110,12 +112,11 @@ old_8_3_check_for_name_data_type_usage(Cluster whichCluster) * old_8_3_check_for_tsquery_usage() * 8.3 -> 8.4 * A new 'prefix' field was added to the 'tsquery' data type in 8.4 - * so migration of such fields is impossible. + * so upgrading of such fields is impossible. */ void -old_8_3_check_for_tsquery_usage(Cluster whichCluster) +old_8_3_check_for_tsquery_usage(ClusterInfo *cluster) { - ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster); int dbnum; FILE *script = NULL; bool found = false; @@ -123,10 +124,9 @@ old_8_3_check_for_tsquery_usage(Cluster whichCluster) prep_status("Checking for tsquery user columns"); - snprintf(output_path, sizeof(output_path), "%s/tables_using_tsquery.txt", - os_info.cwd); + snprintf(output_path, sizeof(output_path), "tables_using_tsquery.txt"); - for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++) + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) { PGresult *res; bool db_used = false; @@ -135,8 +135,8 @@ old_8_3_check_for_tsquery_usage(Cluster whichCluster) int i_nspname, i_relname, i_attname; - DbInfo *active_db = &active_cluster->dbarr.dbs[dbnum]; - PGconn *conn = connectToServer(active_db->db_name, whichCluster); + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); /* Find any user-defined tsquery columns */ res = executeQueryOrDie(conn, @@ -144,13 +144,16 @@ old_8_3_check_for_tsquery_usage(Cluster whichCluster) "FROM pg_catalog.pg_class c, " " pg_catalog.pg_namespace n, " " pg_catalog.pg_attribute a " + /* materialized views didn't exist in 8.3, so no need to check 'm' */ "WHERE c.relkind = 'r' AND " " c.oid = a.attrelid AND " " NOT a.attisdropped AND " " a.atttypid = 'pg_catalog.tsquery'::pg_catalog.regtype AND " " c.relnamespace = n.oid AND " - " n.nspname != 'pg_catalog' AND " - " n.nspname != 'information_schema'"); + /* exclude possible orphaned temp tables */ + " n.nspname !~ '^pg_temp_' AND " + " n.nspname !~ '^pg_toast_temp_' AND " + " n.nspname NOT IN ('pg_catalog', 'information_schema')"); ntups = PQntuples(res); i_nspname = PQfnumber(res, "nspname"); @@ -159,11 +162,11 @@ old_8_3_check_for_tsquery_usage(Cluster whichCluster) for (rowno = 0; rowno < ntups; rowno++) { found = true; - if (script == NULL && (script = fopen(output_path, "w")) == NULL) - pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path); + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno)); if (!db_used) { - fprintf(script, "Database: %s\n", active_db->db_name); + fprintf(script, "Database: %s\n", active_db->db_name); db_used = true; } fprintf(script, " %s.%s.%s\n", @@ -177,18 +180,97 @@ old_8_3_check_for_tsquery_usage(Cluster whichCluster) PQfinish(conn); } + if (script) + fclose(script); + if (found) { + pg_log(PG_REPORT, "fatal\n"); + pg_fatal("Your installation contains the \"tsquery\" data type. This data type\n" + "added a new internal field between your old and new clusters so this\n" + "cluster cannot currently be upgraded. You can remove the problem\n" + "columns and restart the upgrade. A list of the problem columns is in the\n" + "file:\n" + " %s\n\n", output_path); + } + else + check_ok(); +} + + +/* + * old_8_3_check_ltree_usage() + * 8.3 -> 8.4 + * The internal ltree structure was changed in 8.4 so upgrading is impossible. + */ +void +old_8_3_check_ltree_usage(ClusterInfo *cluster) +{ + int dbnum; + FILE *script = NULL; + bool found = false; + char output_path[MAXPGPATH]; + + prep_status("Checking for contrib/ltree"); + + snprintf(output_path, sizeof(output_path), "contrib_ltree.txt"); + + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) + { + PGresult *res; + bool db_used = false; + int ntups; + int rowno; + int i_nspname, + i_proname; + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); + + /* Find any functions coming from contrib/ltree */ + res = executeQueryOrDie(conn, + "SELECT n.nspname, p.proname " + "FROM pg_catalog.pg_proc p, " + " pg_catalog.pg_namespace n " + "WHERE p.pronamespace = n.oid AND " + " p.probin = '$libdir/ltree'"); + + ntups = PQntuples(res); + i_nspname = PQfnumber(res, "nspname"); + i_proname = PQfnumber(res, "proname"); + 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, getErrorText(errno)); + if (!db_used) + { + fprintf(script, "Database: %s\n", active_db->db_name); + db_used = true; + } + fprintf(script, " %s.%s\n", + PQgetvalue(res, rowno, i_nspname), + PQgetvalue(res, rowno, i_proname)); + } + + PQclear(res); + + PQfinish(conn); + } + + if (script) fclose(script); + + if (found) + { pg_log(PG_REPORT, "fatal\n"); - pg_log(PG_FATAL, - "| Your installation contains the \"tsquery\" data type.\n" - "| This data type added a new internal field between\n" - "| your old and new clusters so this cluster cannot\n" - "| currently be upgraded. You can remove the problem\n" - "| columns and restart the migration. A list of the\n" - "| problem columns is in the file:\n" - "| \t%s\n\n", output_path); + pg_fatal("Your installation contains the \"ltree\" data type. This data type\n" + "changed its internal storage format between your old and new clusters so this\n" + "cluster cannot currently be upgraded. You can manually upgrade databases\n" + "that use \"contrib/ltree\" facilities and remove \"contrib/ltree\" from the old\n" + "cluster and restart the upgrade. A list of the problem functions is in the\n" + "file:\n" + " %s\n\n", output_path); } else check_ok(); @@ -208,10 +290,8 @@ old_8_3_check_for_tsquery_usage(Cluster whichCluster) * 'c' 'bb' 'aaa' -- 8.3 */ void -old_8_3_rebuild_tsvector_tables(bool check_mode, - Cluster whichCluster) +old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode) { - ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster); int dbnum; FILE *script = NULL; bool found = false; @@ -219,22 +299,21 @@ old_8_3_rebuild_tsvector_tables(bool check_mode, prep_status("Checking for tsvector user columns"); - snprintf(output_path, sizeof(output_path), "%s/rebuild_tsvector_tables.sql", - os_info.cwd); + snprintf(output_path, sizeof(output_path), "rebuild_tsvector_tables.sql"); - for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++) + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) { PGresult *res; bool db_used = false; - char old_nspname[NAMEDATALEN] = "", - old_relname[NAMEDATALEN] = ""; + char nspname[NAMEDATALEN] = "", + relname[NAMEDATALEN] = ""; int ntups; int rowno; int i_nspname, i_relname, i_attname; - DbInfo *active_db = &active_cluster->dbarr.dbs[dbnum]; - PGconn *conn = connectToServer(active_db->db_name, whichCluster); + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); /* Find any user-defined tsvector columns */ res = executeQueryOrDie(conn, @@ -242,13 +321,18 @@ old_8_3_rebuild_tsvector_tables(bool check_mode, "FROM pg_catalog.pg_class c, " " pg_catalog.pg_namespace n, " " pg_catalog.pg_attribute a " + /* materialized views didn't exist in 8.3, so no need to check 'm' */ "WHERE c.relkind = 'r' AND " " c.oid = a.attrelid AND " " NOT a.attisdropped AND " + /* child attribute changes are processed by the parent */ + " a.attinhcount = 0 AND " " a.atttypid = 'pg_catalog.tsvector'::pg_catalog.regtype AND " " c.relnamespace = n.oid AND " - " n.nspname != 'pg_catalog' AND " - " n.nspname != 'information_schema'"); + /* exclude possible orphaned temp tables */ + " n.nspname !~ '^pg_temp_' AND " + " n.nspname !~ '^pg_toast_temp_' AND " + " n.nspname NOT IN ('pg_catalog', 'information_schema')"); /* * This macro is used below to avoid reindexing indexes already rebuilt @@ -260,12 +344,15 @@ old_8_3_rebuild_tsvector_tables(bool check_mode, "FROM pg_catalog.pg_class c, " \ " pg_catalog.pg_namespace n, " \ " pg_catalog.pg_attribute a " \ + /* materialized views didn't exist in 8.3, so no need to check 'm' */ \ "WHERE c.relkind = 'r' AND " \ " c.oid = a.attrelid AND " \ " NOT a.attisdropped AND " \ + /* child attribute changes are processed by the parent */ \ + " a.attinhcount = 0 AND " \ " a.atttypid = 'pg_catalog.tsvector'::pg_catalog.regtype AND " \ " c.relnamespace = n.oid AND " \ - " n.nspname != 'pg_catalog' AND " \ + " n.nspname !~ '^pg_' AND " \ " n.nspname != 'information_schema') " ntups = PQntuples(res); @@ -277,8 +364,8 @@ old_8_3_rebuild_tsvector_tables(bool check_mode, found = true; if (!check_mode) { - if (script == NULL && (script = fopen(output_path, "w")) == NULL) - pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path); + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno)); if (!db_used) { fprintf(script, "\\connect %s\n\n", @@ -287,58 +374,56 @@ old_8_3_rebuild_tsvector_tables(bool check_mode, } /* Rebuild all tsvector collumns with one ALTER TABLE command */ - if (strcmp(PQgetvalue(res, rowno, i_nspname), old_nspname) != 0 || - strcmp(PQgetvalue(res, rowno, i_relname), old_relname) != 0) + if (strcmp(PQgetvalue(res, rowno, i_nspname), nspname) != 0 || + strcmp(PQgetvalue(res, rowno, i_relname), relname) != 0) { - if (strlen(old_nspname) != 0 || strlen(old_relname) != 0) + if (strlen(nspname) != 0 || strlen(relname) != 0) fprintf(script, ";\n\n"); fprintf(script, "ALTER TABLE %s.%s\n", - quote_identifier(PQgetvalue(res, rowno, i_nspname)), - quote_identifier(PQgetvalue(res, rowno, i_relname))); + quote_identifier(PQgetvalue(res, rowno, i_nspname)), + quote_identifier(PQgetvalue(res, rowno, i_relname))); } else fprintf(script, ",\n"); - strlcpy(old_nspname, PQgetvalue(res, rowno, i_nspname), sizeof(old_nspname)); - strlcpy(old_relname, PQgetvalue(res, rowno, i_relname), sizeof(old_relname)); + strlcpy(nspname, PQgetvalue(res, rowno, i_nspname), sizeof(nspname)); + strlcpy(relname, PQgetvalue(res, rowno, i_relname), sizeof(relname)); fprintf(script, "ALTER COLUMN %s " /* This could have been a custom conversion function call. */ "TYPE pg_catalog.tsvector USING %s::pg_catalog.text::pg_catalog.tsvector", - quote_identifier(PQgetvalue(res, rowno, i_attname)), - quote_identifier(PQgetvalue(res, rowno, i_attname))); + quote_identifier(PQgetvalue(res, rowno, i_attname)), + quote_identifier(PQgetvalue(res, rowno, i_attname))); } } - if (strlen(old_nspname) != 0 || strlen(old_relname) != 0) + if (strlen(nspname) != 0 || strlen(relname) != 0) fprintf(script, ";\n\n"); PQclear(res); - /* XXX Mark tables as not accessable somehow */ + /* XXX Mark tables as not accessible somehow */ PQfinish(conn); } + if (script) + fclose(script); + if (found) { - if (!check_mode) - fclose(script); report_status(PG_WARNING, "warning"); if (check_mode) pg_log(PG_WARNING, "\n" - "| Your installation contains tsvector columns.\n" - "| The tsvector internal storage format changed\n" - "| between your old and new clusters so the tables\n" - "| must be rebuilt. After migration, you will be\n" - "| given instructions.\n\n"); + "Your installation contains tsvector columns. The tsvector internal\n" + "storage format changed between your old and new clusters so the tables\n" + "must be rebuilt. After upgrading, you will be given instructions.\n\n"); else pg_log(PG_WARNING, "\n" - "| Your installation contains tsvector columns.\n" - "| The tsvector internal storage format changed\n" - "| between your old and new clusters so the tables\n" - "| must be rebuilt. The file:\n" - "| \t%s\n" - "| when executed by psql by the database super-user\n" - "| will rebuild all tables with tsvector columns.\n\n", + "Your installation contains tsvector columns. The tsvector internal\n" + "storage format changed between your old and new clusters so the tables\n" + "must be rebuilt. The file:\n" + " %s\n" + "when executed by psql by the database superuser will rebuild all tables\n" + "with tsvector columns.\n\n", output_path); } else @@ -349,24 +434,21 @@ old_8_3_rebuild_tsvector_tables(bool check_mode, /* * old_8_3_invalidate_hash_gin_indexes() * 8.3 -> 8.4 - * Hash, Gin, and GiST index binary format has changes from 8.3->8.4 + * Hash and GIN index binary format changed from 8.3->8.4 */ void -old_8_3_invalidate_hash_gin_indexes(bool check_mode, - Cluster whichCluster) +old_8_3_invalidate_hash_gin_indexes(ClusterInfo *cluster, bool check_mode) { - ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster); int dbnum; FILE *script = NULL; bool found = false; char output_path[MAXPGPATH]; - prep_status("Checking for hash and gin indexes"); + prep_status("Checking for hash and GIN indexes"); - snprintf(output_path, sizeof(output_path), "%s/reindex_hash_and_gin.sql", - os_info.cwd); + snprintf(output_path, sizeof(output_path), "reindex_hash_and_gin.sql"); - for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++) + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) { PGresult *res; bool db_used = false; @@ -374,17 +456,17 @@ old_8_3_invalidate_hash_gin_indexes(bool check_mode, int rowno; int i_nspname, i_relname; - DbInfo *active_db = &active_cluster->dbarr.dbs[dbnum]; - PGconn *conn = connectToServer(active_db->db_name, whichCluster); + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); /* find hash and gin indexes */ res = executeQueryOrDie(conn, "SELECT n.nspname, c.relname " - "FROM pg_catalog.pg_class c, " + "FROM pg_catalog.pg_class c, " " pg_catalog.pg_index i, " " pg_catalog.pg_am a, " " pg_catalog.pg_namespace n " - "WHERE i.indexrelid = c.oid AND " + "WHERE i.indexrelid = c.oid AND " " c.relam = a.oid AND " " c.relnamespace = n.oid AND " " a.amname IN ('hash', 'gin') AND " @@ -398,8 +480,8 @@ old_8_3_invalidate_hash_gin_indexes(bool check_mode, found = true; if (!check_mode) { - if (script == NULL && (script = fopen(output_path, "w")) == NULL) - pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path); + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno)); if (!db_used) { fprintf(script, "\\connect %s\n", @@ -407,8 +489,8 @@ old_8_3_invalidate_hash_gin_indexes(bool check_mode, db_used = true; } fprintf(script, "REINDEX INDEX %s.%s;\n", - quote_identifier(PQgetvalue(res, rowno, i_nspname)), - quote_identifier(PQgetvalue(res, rowno, i_relname))); + quote_identifier(PQgetvalue(res, rowno, i_nspname)), + quote_identifier(PQgetvalue(res, rowno, i_relname))); } } @@ -419,10 +501,10 @@ old_8_3_invalidate_hash_gin_indexes(bool check_mode, PQclear(executeQueryOrDie(conn, "UPDATE pg_catalog.pg_index i " "SET indisvalid = false " - "FROM pg_catalog.pg_class c, " + "FROM pg_catalog.pg_class c, " " pg_catalog.pg_am a, " " pg_catalog.pg_namespace n " - "WHERE i.indexrelid = c.oid AND " + "WHERE i.indexrelid = c.oid AND " " c.relam = a.oid AND " " c.relnamespace = n.oid AND " " a.amname IN ('hash', 'gin')")); @@ -430,30 +512,26 @@ old_8_3_invalidate_hash_gin_indexes(bool check_mode, PQfinish(conn); } + if (script) + fclose(script); + if (found) { - if (!check_mode) - fclose(script); report_status(PG_WARNING, "warning"); if (check_mode) pg_log(PG_WARNING, "\n" - "| Your installation contains hash and/or gin\n" - "| indexes. These indexes have different\n" - "| internal formats between your old and new\n" - "| clusters so they must be reindexed with the\n" - "| REINDEX command. After migration, you will\n" - "| be given REINDEX instructions.\n\n"); + "Your installation contains hash and/or GIN indexes. These indexes have\n" + "different internal formats between your old and new clusters, so they\n" + "must be reindexed with the REINDEX command. After upgrading, you will\n" + "be given REINDEX instructions.\n\n"); else pg_log(PG_WARNING, "\n" - "| Your installation contains hash and/or gin\n" - "| indexes. These indexes have different internal\n" - "| formats between your old and new clusters so\n" - "| they must be reindexed with the REINDEX command.\n" - "| The file:\n" - "| \t%s\n" - "| when executed by psql by the database super-user\n" - "| will recreate all invalid indexes; until then,\n" - "| none of these indexes will be used.\n\n", + "Your installation contains hash and/or GIN indexes. These indexes have\n" + "different internal formats between your old and new clusters, so they\n" + "must be reindexed with the REINDEX command. The file:\n" + " %s\n" + "when executed by psql by the database superuser will recreate all invalid\n" + "indexes; until then, none of these indexes will be used.\n\n", output_path); } else @@ -467,10 +545,9 @@ old_8_3_invalidate_hash_gin_indexes(bool check_mode, * 8.4 bpchar_pattern_ops no longer sorts based on trailing spaces */ void -old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, - Cluster whichCluster) +old_8_3_invalidate_bpchar_pattern_ops_indexes(ClusterInfo *cluster, + bool check_mode) { - ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster); int dbnum; FILE *script = NULL; bool found = false; @@ -478,10 +555,9 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, prep_status("Checking for bpchar_pattern_ops indexes"); - snprintf(output_path, sizeof(output_path), "%s/reindex_bpchar_ops.sql", - os_info.cwd); + snprintf(output_path, sizeof(output_path), "reindex_bpchar_ops.sql"); - for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++) + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) { PGresult *res; bool db_used = false; @@ -489,8 +565,8 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, int rowno; int i_nspname, i_relname; - DbInfo *active_db = &active_cluster->dbarr.dbs[dbnum]; - PGconn *conn = connectToServer(active_db->db_name, whichCluster); + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); /* find bpchar_pattern_ops indexes */ @@ -523,8 +599,8 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, found = true; if (!check_mode) { - if (script == NULL && (script = fopen(output_path, "w")) == NULL) - pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path); + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno)); if (!db_used) { fprintf(script, "\\connect %s\n", @@ -532,8 +608,8 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, db_used = true; } fprintf(script, "REINDEX INDEX %s.%s;\n", - quote_identifier(PQgetvalue(res, rowno, i_nspname)), - quote_identifier(PQgetvalue(res, rowno, i_relname))); + quote_identifier(PQgetvalue(res, rowno, i_nspname)), + quote_identifier(PQgetvalue(res, rowno, i_relname))); } } @@ -560,30 +636,26 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, PQfinish(conn); } + if (script) + fclose(script); + if (found) { - if (!check_mode) - fclose(script); report_status(PG_WARNING, "warning"); if (check_mode) pg_log(PG_WARNING, "\n" - "| Your installation contains indexes using\n" - "| \"bpchar_pattern_ops\". These indexes have\n" - "| different internal formats between your old and\n" - "| new clusters so they must be reindexed with the\n" - "| REINDEX command. After migration, you will be\n" - "| given REINDEX instructions.\n\n"); + "Your installation contains indexes using \"bpchar_pattern_ops\". These\n" + "indexes have different internal formats between your old and new clusters\n" + "so they must be reindexed with the REINDEX command. After upgrading, you\n" + "will be given REINDEX instructions.\n\n"); else pg_log(PG_WARNING, "\n" - "| Your installation contains indexes using\n" - "| \"bpchar_pattern_ops\". These indexes have\n" - "| different internal formats between your old and\n" - "| new clusters so they must be reindexed with the\n" - "| REINDEX command. The file:\n" - "| \t%s\n" - "| when executed by psql by the database super-user\n" - "| will recreate all invalid indexes; until then,\n" - "| none of these indexes will be used.\n\n", + "Your installation contains indexes using \"bpchar_pattern_ops\". These\n" + "indexes have different internal formats between your old and new clusters\n" + "so they must be reindexed with the REINDEX command. The file:\n" + " %s\n" + "when executed by psql by the database superuser will recreate all invalid\n" + "indexes; until then, none of these indexes will be used.\n\n", output_path); } else @@ -598,23 +670,22 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(bool check_mode, * we don't transfer sequence files but instead use the CREATE SEQUENCE * command from the schema dump, and use setval() to restore the sequence * value and 'is_called' from the old database. This is safe to run - * by pg_upgrade because sequence files are not transfered from the old + * by pg_upgrade because sequence files are not transferred from the old * server, even in link mode. */ char * -old_8_3_create_sequence_script(Cluster whichCluster) +old_8_3_create_sequence_script(ClusterInfo *cluster) { - ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster); int dbnum; FILE *script = NULL; bool found = false; - char *output_path = pg_malloc(MAXPGPATH); + char *output_path; - snprintf(output_path, MAXPGPATH, "%s/adjust_sequences.sql", os_info.cwd); + output_path = pg_strdup("adjust_sequences.sql"); prep_status("Creating script to adjust sequences"); - for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++) + for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++) { PGresult *res; bool db_used = false; @@ -622,8 +693,8 @@ old_8_3_create_sequence_script(Cluster whichCluster) int rowno; int i_nspname, i_relname; - DbInfo *active_db = &active_cluster->dbarr.dbs[dbnum]; - PGconn *conn = connectToServer(active_db->db_name, whichCluster); + DbInfo *active_db = &cluster->dbarr.dbs[dbnum]; + PGconn *conn = connectToServer(cluster, active_db->db_name); /* Find any sequences */ res = executeQueryOrDie(conn, @@ -632,8 +703,10 @@ old_8_3_create_sequence_script(Cluster whichCluster) " pg_catalog.pg_namespace n " "WHERE c.relkind = 'S' AND " " c.relnamespace = n.oid AND " - " n.nspname != 'pg_catalog' AND " - " n.nspname != 'information_schema'"); + /* exclude possible orphaned temp tables */ + " n.nspname !~ '^pg_temp_' AND " + " n.nspname !~ '^pg_toast_temp_' AND " + " n.nspname NOT IN ('pg_catalog', 'information_schema')"); ntups = PQntuples(res); i_nspname = PQfnumber(res, "nspname"); @@ -648,8 +721,8 @@ old_8_3_create_sequence_script(Cluster whichCluster) found = true; - if (script == NULL && (script = fopen(output_path, "w")) == NULL) - pg_log(PG_FATAL, "Could not create necessary file: %s\n", output_path); + if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL) + pg_fatal("could not open file \"%s\": %s\n", output_path, getErrorText(errno)); if (!db_used) { fprintf(script, "\\connect %s\n\n", @@ -669,7 +742,7 @@ old_8_3_create_sequence_script(Cluster whichCluster) i_is_called = PQfnumber(seq_res, "is_called"); fprintf(script, "SELECT setval('%s.%s', %s, '%s');\n", - quote_identifier(nspname), quote_identifier(relname), + quote_identifier(nspname), quote_identifier(relname), PQgetvalue(seq_res, 0, i_last_value), PQgetvalue(seq_res, 0, i_is_called)); PQclear(seq_res); } @@ -680,7 +753,8 @@ old_8_3_create_sequence_script(Cluster whichCluster) PQfinish(conn); } - if (found) + + if (script) fclose(script); check_ok();