On tor, 2011-02-10 at 06:31 +0200, Peter Eisentraut wrote:
> > ERROR:  cannot drop column from typed table
> > 
> > which probably is because test_type2 has a dropped column.
> 
> It should call
> 
> ALTER TYPE test_type2 DROP ATTRIBUTE xyz CASCADE;
> 
> instead.  That will propagate to the table.

Here is a patch that addresses this problem.

It looks like Noah Misch might have found another problem in this area.
We'll have to investigate that.
diff --git i/src/bin/pg_dump/pg_dump.c w/src/bin/pg_dump/pg_dump.c
index 5561295..4cea954 100644
--- i/src/bin/pg_dump/pg_dump.c
+++ w/src/bin/pg_dump/pg_dump.c
@@ -7889,6 +7889,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
 	int			ntups;
 	int			i_attname;
 	int			i_atttypdefn;
+	int			i_attisdropped;
 	int			i_typrelid;
 	int			i;
 
@@ -7900,11 +7901,11 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
 
 	appendPQExpBuffer(query, "SELECT a.attname, "
 			"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
+					  "a.attisdropped, "
 					  "typrelid "
 					  "FROM pg_catalog.pg_type t, pg_catalog.pg_attribute a "
 					  "WHERE t.oid = '%u'::pg_catalog.oid "
 					  "AND a.attrelid = t.typrelid "
-					  "AND NOT a.attisdropped "
 					  "ORDER BY a.attnum ",
 					  tyinfo->dobj.catId.oid);
 
@@ -7915,6 +7916,7 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
 
 	i_attname = PQfnumber(res, "attname");
 	i_atttypdefn = PQfnumber(res, "atttypdefn");
+	i_attisdropped = PQfnumber(res, "attisdropped");
 	i_typrelid = PQfnumber(res, "typrelid");
 
 	if (binary_upgrade)
@@ -7932,11 +7934,20 @@ dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
 	{
 		char	   *attname;
 		char	   *atttypdefn;
+		bool		attisdropped;
 
 		attname = PQgetvalue(res, i, i_attname);
 		atttypdefn = PQgetvalue(res, i, i_atttypdefn);
+		attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
 
-		appendPQExpBuffer(q, "\n\t%s %s", fmtId(attname), atttypdefn);
+		if (attisdropped)
+		{
+			if (binary_upgrade)
+				/* see under dumpTableSchema() */
+				appendPQExpBuffer(q, "\n\t%s INTEGER /* dummy */", fmtId(attname));
+		}
+		else
+			appendPQExpBuffer(q, "\n\t%s %s", fmtId(attname), atttypdefn);
 		if (i < ntups - 1)
 			appendPQExpBuffer(q, ",");
 	}
@@ -12105,14 +12116,26 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
 									  tbinfo->attlen[j],
 									  tbinfo->attalign[j]);
 					appendStringLiteralAH(q, tbinfo->attnames[j], fout);
-					appendPQExpBuffer(q, "\n  AND attrelid = ");
+					appendPQExpBuffer(q, "\n  AND attrelid IN (");
 					appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
-					appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
+					appendPQExpBuffer(q, "::pg_catalog.regclass");
+					if (tbinfo->reloftype)
+						appendPQExpBuffer(q, ", '%s'::pg_catalog.regclass", tbinfo->reloftype);
+					appendPQExpBuffer(q, ");\n");
 
-					appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
-									  fmtId(tbinfo->dobj.name));
-					appendPQExpBuffer(q, "DROP COLUMN %s;\n",
-									  fmtId(tbinfo->attnames[j]));
+					if (tbinfo->reloftype)
+					{
+						appendPQExpBuffer(q, "ALTER TYPE %s ",
+										  tbinfo->reloftype);
+						appendPQExpBuffer(q, "DROP ATTRIBUTE %s CASCADE;\n",
+										  fmtId(tbinfo->attnames[j]));
+					}
+					else {
+						appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
+										  fmtId(tbinfo->dobj.name));
+						appendPQExpBuffer(q, "DROP COLUMN %s;\n",
+										  fmtId(tbinfo->attnames[j]));
+					}
 				}
 				else if (!tbinfo->attislocal[j])
 				{
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to