8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -285,7 +285,8 @@ static void ATExecSetStorage(Relation rel, const char *colName,
285
285
Node * newValue );
286
286
static void ATExecDropColumn (List * * wqueue , Relation rel , const char * colName ,
287
287
DropBehavior behavior ,
288
- bool recurse , bool recursing );
288
+ bool recurse , bool recursing ,
289
+ bool missing_ok );
289
290
static void ATExecAddIndex (AlteredTableInfo * tab , Relation rel ,
290
291
IndexStmt * stmt , bool is_rebuild );
291
292
static void ATExecAddConstraint (List * * wqueue ,
@@ -298,8 +299,9 @@ static void ATAddCheckConstraint(List **wqueue,
298
299
static void ATAddForeignKeyConstraint (AlteredTableInfo * tab , Relation rel ,
299
300
FkConstraint * fkconstraint );
300
301
static 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 );
303
305
static void ATPrepAlterColumnType (List * * wqueue ,
304
306
AlteredTableInfo * tab , Relation rel ,
305
307
bool recurse , bool recursing ,
@@ -2620,11 +2622,11 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
2620
2622
break ;
2621
2623
case AT_DropColumn : /* DROP COLUMN */
2622
2624
ATExecDropColumn (wqueue , rel , cmd -> name ,
2623
- cmd -> behavior , false, false);
2625
+ cmd -> behavior , false, false, cmd -> missing_ok );
2624
2626
break ;
2625
2627
case AT_DropColumnRecurse : /* DROP COLUMN with recursion */
2626
2628
ATExecDropColumn (wqueue , rel , cmd -> name ,
2627
- cmd -> behavior , true, false);
2629
+ cmd -> behavior , true, false, cmd -> missing_ok );
2628
2630
break ;
2629
2631
case AT_AddIndex : /* ADD INDEX */
2630
2632
ATExecAddIndex (tab , rel , (IndexStmt * ) cmd -> def , false);
@@ -2639,10 +2641,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
2639
2641
ATExecAddConstraint (wqueue , tab , rel , cmd -> def , true);
2640
2642
break ;
2641
2643
case 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 );
2643
2647
break ;
2644
2648
case 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 );
2646
2652
break ;
2647
2653
case AT_AlterColumnType : /* ALTER COLUMN TYPE */
2648
2654
ATExecAlterColumnType (tab , rel , cmd -> name , (TypeName * ) cmd -> def );
@@ -4160,7 +4166,8 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue)
4160
4166
static void
4161
4167
ATExecDropColumn (List * * wqueue , Relation rel , const char * colName ,
4162
4168
DropBehavior behavior ,
4163
- bool recurse , bool recursing )
4169
+ bool recurse , bool recursing ,
4170
+ bool missing_ok )
4164
4171
{
4165
4172
HeapTuple tuple ;
4166
4173
Form_pg_attribute targetatt ;
@@ -4176,11 +4183,21 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
4176
4183
* get the number of the attribute
4177
4184
*/
4178
4185
tuple = 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
+ }
4184
4201
targetatt = (Form_pg_attribute ) GETSTRUCT (tuple );
4185
4202
4186
4203
attnum = targetatt -> attnum ;
@@ -4246,7 +4263,8 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
4246
4263
{
4247
4264
/* Time to delete this child column, too */
4248
4265
ATExecDropColumn (wqueue , childrel , colName ,
4249
- behavior , true, true);
4266
+ behavior , true, true,
4267
+ false);
4250
4268
}
4251
4269
else
4252
4270
{
@@ -5360,7 +5378,8 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint,
5360
5378
static void
5361
5379
ATExecDropConstraint (Relation rel , const char * constrName ,
5362
5380
DropBehavior behavior ,
5363
- bool recurse , bool recursing )
5381
+ bool recurse , bool recursing ,
5382
+ bool missing_ok )
5364
5383
{
5365
5384
List * children ;
5366
5385
ListCell * child ;
@@ -5422,12 +5441,22 @@ ATExecDropConstraint(Relation rel, const char *constrName,
5422
5441
5423
5442
systable_endscan (scan );
5424
5443
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
+ }
5431
5460
/*
5432
5461
* Propagate to children as appropriate. Unlike most other ALTER
5433
5462
* 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,
5490
5519
{
5491
5520
/* Time to delete this child constraint, too */
5492
5521
ATExecDropConstraint (childrel , constrName , behavior ,
5493
- true, true);
5522
+ true, true,
5523
+ false);
5494
5524
}
5495
5525
else
5496
5526
{
0 commit comments