From: Bruce Momjian <bruce@momjian.us>
Date: Wed, 7 Sep 2011 18:42:36 +0000 (-0400)
Subject: In pg_upgrade, disallow migration of 8.3 clusters using contrib/ltree
X-Git-Tag: REL9_0_5~8
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c3106a340f3177105b80a0a6e878eba1986ae9fe;p=postgresql

In pg_upgrade, disallow migration of 8.3 clusters using contrib/ltree
because its internal format was changed in 8.4.

Backpatch to 9.0 and 9.1.

Report by depesz, diagnosis by Tom.
---

diff --git a/contrib/pg_upgrade/check.c b/contrib/pg_upgrade/check.c
index 37c38c1b97..720f1305a5 100644
--- a/contrib/pg_upgrade/check.c
+++ b/contrib/pg_upgrade/check.c
@@ -72,6 +72,7 @@ check_old_cluster(migratorContext *ctx, bool live_check,
 	{
 		old_8_3_check_for_name_data_type_usage(ctx, CLUSTER_OLD);
 		old_8_3_check_for_tsquery_usage(ctx, CLUSTER_OLD);
+		old_8_3_check_ltree_usage(ctx, CLUSTER_OLD);
 		if (ctx->check)
 		{
 			old_8_3_rebuild_tsvector_tables(ctx, true, CLUSTER_OLD);
diff --git a/contrib/pg_upgrade/pg_upgrade.h b/contrib/pg_upgrade/pg_upgrade.h
index 41c4b11245..7a02fa1946 100644
--- a/contrib/pg_upgrade/pg_upgrade.h
+++ b/contrib/pg_upgrade/pg_upgrade.h
@@ -394,6 +394,8 @@ void old_8_3_check_for_name_data_type_usage(migratorContext *ctx,
 									   Cluster whichCluster);
 void old_8_3_check_for_tsquery_usage(migratorContext *ctx,
 								Cluster whichCluster);
+void old_8_3_check_ltree_usage(migratorContext *ctx,
+								Cluster whichCluster);
 void old_8_3_rebuild_tsvector_tables(migratorContext *ctx,
 								bool check_mode, Cluster whichCluster);
 void old_8_3_invalidate_hash_gin_indexes(migratorContext *ctx,
diff --git a/contrib/pg_upgrade/version_old_8_3.c b/contrib/pg_upgrade/version_old_8_3.c
index 6fcd61b9c4..7e3a7aacec 100644
--- a/contrib/pg_upgrade/version_old_8_3.c
+++ b/contrib/pg_upgrade/version_old_8_3.c
@@ -203,6 +203,86 @@ old_8_3_check_for_tsquery_usage(migratorContext *ctx, Cluster whichCluster)
 }
 
 
+/*
+ *	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(migratorContext *ctx, Cluster whichCluster)
+{
+	ClusterInfo *active_cluster = (whichCluster == CLUSTER_OLD) ?
+	&ctx->old : &ctx->new;
+	int			dbnum;
+	FILE	   *script = NULL;
+	bool		found = false;
+	char		output_path[MAXPGPATH];
+
+	prep_status(ctx, "Checking for /contrib/ltree");
+
+	snprintf(output_path, sizeof(output_path), "%s/contrib_ltree.txt",
+			 ctx->cwd);
+
+	for (dbnum = 0; dbnum < active_cluster->dbarr.ndbs; dbnum++)
+	{
+		PGresult   *res;
+		bool		db_used = false;
+		int			ntups;
+		int			rowno;
+		int			i_nspname,
+					i_proname;
+		DbInfo	   *active_db = &active_cluster->dbarr.dbs[dbnum];
+		PGconn	   *conn = connectToServer(ctx, active_db->db_name, whichCluster);
+
+		/* Find any functions coming from contrib/ltree */
+		res = executeQueryOrDie(ctx, 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(output_path, "w")) == NULL)
+				pg_log(ctx, PG_FATAL, "Could not create necessary file:  %s\n", output_path);
+			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 (found)
+	{
+		fclose(script);
+		pg_log(ctx, PG_REPORT, "fatal\n");
+		pg_log(ctx, 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"
+			   "| \t%s\n\n", output_path);
+	}
+	else
+		check_ok(ctx);
+}
+
+
 /*
  * old_8_3_rebuild_tsvector_tables()
  *	8.3 -> 8.4
diff --git a/doc/src/sgml/pgupgrade.sgml b/doc/src/sgml/pgupgrade.sgml
index 31f1c3d1d4..74fba2e8e0 100644
--- a/doc/src/sgml/pgupgrade.sgml
+++ b/doc/src/sgml/pgupgrade.sgml
@@ -464,6 +464,11 @@ psql --username postgres --file script.sql postgres
    </itemizedlist>
   </para>
 
+  <para>
+   pg_upgrade will not work if the <filename>ltree</>
+   contrib module is installed in a database.
+  </para>
+
   <para>
    You must drop any such columns and migrate them manually.
   </para>