|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.176 2005/11/22 18:17:09 momjian Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.177 2006/01/30 16:18:58 tgl Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
@@ -4967,12 +4967,38 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
|
4967 | 4967 |
|
4968 | 4968 | case OCLASS_CONSTRAINT:
|
4969 | 4969 | Assert(foundObject.objectSubId == 0);
|
4970 |
| - if (!list_member_oid(tab->changedConstraintOids, foundObject.objectId)) |
| 4970 | + if (!list_member_oid(tab->changedConstraintOids, |
| 4971 | + foundObject.objectId)) |
4971 | 4972 | {
|
4972 |
| - tab->changedConstraintOids = lappend_oid(tab->changedConstraintOids, |
4973 |
| - foundObject.objectId); |
4974 |
| - tab->changedConstraintDefs = lappend(tab->changedConstraintDefs, |
4975 |
| - pg_get_constraintdef_string(foundObject.objectId)); |
| 4973 | + char *defstring = pg_get_constraintdef_string(foundObject.objectId); |
| 4974 | + |
| 4975 | + /* |
| 4976 | + * Put NORMAL dependencies at the front of the list and |
| 4977 | + * AUTO dependencies at the back. This makes sure that |
| 4978 | + * foreign-key constraints depending on this column will |
| 4979 | + * be dropped before unique or primary-key constraints of |
| 4980 | + * the column; which we must have because the FK |
| 4981 | + * constraints depend on the indexes belonging to the |
| 4982 | + * unique constraints. |
| 4983 | + */ |
| 4984 | + if (foundDep->deptype == DEPENDENCY_NORMAL) |
| 4985 | + { |
| 4986 | + tab->changedConstraintOids = |
| 4987 | + lcons_oid(foundObject.objectId, |
| 4988 | + tab->changedConstraintOids); |
| 4989 | + tab->changedConstraintDefs = |
| 4990 | + lcons(defstring, |
| 4991 | + tab->changedConstraintDefs); |
| 4992 | + } |
| 4993 | + else |
| 4994 | + { |
| 4995 | + tab->changedConstraintOids = |
| 4996 | + lappend_oid(tab->changedConstraintOids, |
| 4997 | + foundObject.objectId); |
| 4998 | + tab->changedConstraintDefs = |
| 4999 | + lappend(tab->changedConstraintDefs, |
| 5000 | + defstring); |
| 5001 | + } |
4976 | 5002 | }
|
4977 | 5003 | break;
|
4978 | 5004 |
|
@@ -5140,9 +5166,11 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab)
|
5140 | 5166 |
|
5141 | 5167 | /*
|
5142 | 5168 | * Now we can drop the existing constraints and indexes --- constraints
|
5143 |
| - * first, since some of them might depend on the indexes. It should be |
5144 |
| - * okay to use DROP_RESTRICT here, since nothing else should be depending |
5145 |
| - * on these objects. |
| 5169 | + * first, since some of them might depend on the indexes. In fact, we |
| 5170 | + * have to delete FOREIGN KEY constraints before UNIQUE constraints, |
| 5171 | + * but we already ordered the constraint list to ensure that would happen. |
| 5172 | + * It should be okay to use DROP_RESTRICT here, since nothing else should |
| 5173 | + * be depending on these objects. |
5146 | 5174 | */
|
5147 | 5175 | foreach(l, tab->changedConstraintOids)
|
5148 | 5176 | {
|
|
0 commit comments