88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.290 2009/07/16 06:33:42 petere Exp $
11+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.291 2009/07/20 02:42:27 adunstan Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -285,7 +285,8 @@ static void ATExecSetStorage(Relation rel, const char *colName,
285285Node * newValue );
286286static void ATExecDropColumn (List * * wqueue ,Relation rel ,const char * colName ,
287287DropBehavior behavior ,
288- bool recurse ,bool recursing );
288+ bool recurse ,bool recursing ,
289+ bool missing_ok );
289290static void ATExecAddIndex (AlteredTableInfo * tab ,Relation rel ,
290291IndexStmt * stmt ,bool is_rebuild );
291292static void ATExecAddConstraint (List * * wqueue ,
@@ -298,8 +299,9 @@ static void ATAddCheckConstraint(List **wqueue,
298299static void ATAddForeignKeyConstraint (AlteredTableInfo * tab ,Relation rel ,
299300FkConstraint * fkconstraint );
300301static void ATExecDropConstraint (Relation rel ,const char * constrName ,
301- DropBehavior behavior ,
302- bool recurse ,bool recursing );
302+ DropBehavior behavior ,
303+ bool recurse ,bool recursing ,
304+ bool missing_ok );
303305static void ATPrepAlterColumnType (List * * wqueue ,
304306AlteredTableInfo * tab ,Relation rel ,
305307bool recurse ,bool recursing ,
@@ -2620,11 +2622,11 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
26202622break ;
26212623case AT_DropColumn :/* DROP COLUMN */
26222624ATExecDropColumn (wqueue ,rel ,cmd -> name ,
2623- cmd -> behavior , false, false);
2625+ cmd -> behavior , false, false, cmd -> missing_ok );
26242626break ;
26252627case AT_DropColumnRecurse :/* DROP COLUMN with recursion */
26262628ATExecDropColumn (wqueue ,rel ,cmd -> name ,
2627- cmd -> behavior , true, false);
2629+ cmd -> behavior , true, false, cmd -> missing_ok );
26282630break ;
26292631case AT_AddIndex :/* ADD INDEX */
26302632ATExecAddIndex (tab ,rel , (IndexStmt * )cmd -> def , false);
@@ -2639,10 +2641,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
26392641ATExecAddConstraint (wqueue ,tab ,rel ,cmd -> def , true);
26402642break ;
26412643case AT_DropConstraint :/* DROP CONSTRAINT */
2642- ATExecDropConstraint (rel ,cmd -> name ,cmd -> behavior , false, false);
2644+ ATExecDropConstraint (rel ,cmd -> name ,cmd -> behavior ,
2645+ false, false,
2646+ cmd -> missing_ok );
26432647break ;
26442648case AT_DropConstraintRecurse :/* DROP CONSTRAINT with recursion */
2645- ATExecDropConstraint (rel ,cmd -> name ,cmd -> behavior , true, false);
2649+ ATExecDropConstraint (rel ,cmd -> name ,cmd -> behavior ,
2650+ true, false,
2651+ cmd -> missing_ok );
26462652break ;
26472653case AT_AlterColumnType :/* ALTER COLUMN TYPE */
26482654ATExecAlterColumnType (tab ,rel ,cmd -> name , (TypeName * )cmd -> def );
@@ -4160,7 +4166,8 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue)
41604166static void
41614167ATExecDropColumn (List * * wqueue ,Relation rel ,const char * colName ,
41624168DropBehavior behavior ,
4163- bool recurse ,bool recursing )
4169+ bool recurse ,bool recursing ,
4170+ bool missing_ok )
41644171{
41654172HeapTuple tuple ;
41664173Form_pg_attribute targetatt ;
@@ -4176,11 +4183,21 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
41764183 * get the number of the attribute
41774184 */
41784185tuple = SearchSysCacheAttName (RelationGetRelid (rel ),colName );
4179- if (!HeapTupleIsValid (tuple ))
4180- ereport (ERROR ,
4181- (errcode (ERRCODE_UNDEFINED_COLUMN ),
4182- errmsg ("column \"%s\" of relation \"%s\" does not exist" ,
4183- colName ,RelationGetRelationName (rel ))));
4186+ if (!HeapTupleIsValid (tuple )){
4187+ if (!missing_ok ){
4188+ ereport (ERROR ,
4189+ (errcode (ERRCODE_UNDEFINED_COLUMN ),
4190+ errmsg ("column \"%s\" of relation \"%s\" does not exist" ,
4191+ colName ,RelationGetRelationName (rel ))));
4192+ }
4193+ else
4194+ {
4195+ ereport (NOTICE ,
4196+ (errmsg ("column \"%s\" of relation \"%s\" does not exist, skipping" ,
4197+ colName ,RelationGetRelationName (rel ))));
4198+ return ;
4199+ }
4200+ }
41844201targetatt = (Form_pg_attribute )GETSTRUCT (tuple );
41854202
41864203attnum = targetatt -> attnum ;
@@ -4246,7 +4263,8 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
42464263{
42474264/* Time to delete this child column, too */
42484265ATExecDropColumn (wqueue ,childrel ,colName ,
4249- behavior , true, true);
4266+ behavior , true, true,
4267+ false);
42504268}
42514269else
42524270{
@@ -5360,7 +5378,8 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint,
53605378static void
53615379ATExecDropConstraint (Relation rel ,const char * constrName ,
53625380DropBehavior behavior ,
5363- bool recurse ,bool recursing )
5381+ bool recurse ,bool recursing ,
5382+ bool missing_ok )
53645383{
53655384List * children ;
53665385ListCell * child ;
@@ -5422,12 +5441,22 @@ ATExecDropConstraint(Relation rel, const char *constrName,
54225441
54235442systable_endscan (scan );
54245443
5425- if (!found )
5426- ereport (ERROR ,
5427- (errcode (ERRCODE_UNDEFINED_OBJECT ),
5428- errmsg ("constraint \"%s\" of relation \"%s\" does not exist" ,
5429- constrName ,RelationGetRelationName (rel ))));
5430-
5444+ if (!found ){
5445+ if (!missing_ok ){
5446+ ereport (ERROR ,
5447+ (errcode (ERRCODE_UNDEFINED_OBJECT ),
5448+ errmsg ("constraint \"%s\" of relation \"%s\" does not exist" ,
5449+ constrName ,RelationGetRelationName (rel ))));
5450+ }
5451+ else
5452+ {
5453+ ereport (NOTICE ,
5454+ (errmsg ("constraint \"%s\" of relation \"%s\" does not exist, skipping" ,
5455+ constrName ,RelationGetRelationName (rel ))));
5456+ heap_close (conrel ,RowExclusiveLock );
5457+ return ;
5458+ }
5459+ }
54315460/*
54325461 * Propagate to children as appropriate. Unlike most other ALTER
54335462 * routines, we have to do this one level of recursion at a time; we can't
@@ -5490,7 +5519,8 @@ ATExecDropConstraint(Relation rel, const char *constrName,
54905519{
54915520/* Time to delete this child constraint, too */
54925521ATExecDropConstraint (childrel ,constrName ,behavior ,
5493- true, true);
5522+ true, true,
5523+ false);
54945524}
54955525else
54965526{