Skip to content

Commit bbcd016

Browse files
committed
DROP ... IF EXISTS for the following cases: language, tablespace, trigger, rule, opclass, function, aggregate. operator, and cast.
1 parent e79cc2d commit bbcd016

File tree

15 files changed

+271
-58
lines changed

15 files changed

+271
-58
lines changed

src/backend/commands/aggregatecmds.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/aggregatecmds.c,v 1.34 2006/04/15 17:45:33 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/aggregatecmds.c,v 1.35 2006/06/16 20:23:44 adunstan Exp $
1313
*
1414
* DESCRIPTION
1515
* The "DefineFoo" routines take the parse tree and pick out the
@@ -211,7 +211,21 @@ RemoveAggregate(RemoveFuncStmt *stmt)
211211
ObjectAddress object;
212212

213213
/* Look up function and make sure it's an aggregate */
214-
procOid = LookupAggNameTypeNames(aggName, aggArgs, false);
214+
procOid = LookupAggNameTypeNames(aggName, aggArgs, stmt->missing_ok);
215+
216+
if (!OidIsValid(procOid))
217+
{
218+
/* we only get here if stmt->missing_ok is true */
219+
220+
/* XXX might need better message here */
221+
222+
ereport(NOTICE,
223+
(errmsg("aggregate %s does not exist ... skipping",
224+
stmt->name)));
225+
226+
227+
return;
228+
}
215229

216230
/*
217231
* Find the function tuple, do permissions and validity checks

src/backend/commands/functioncmds.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.74 2006/04/15 17:45:34 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.75 2006/06/16 20:23:44 adunstan Exp $
1414
*
1515
* DESCRIPTION
1616
* These routines take the parse tree and pick out the
@@ -687,7 +687,16 @@ RemoveFunction(RemoveFuncStmt *stmt)
687687
/*
688688
* Find the function, do permissions and validity checks
689689
*/
690-
funcOid = LookupFuncNameTypeNames(functionName, argTypes, false);
690+
funcOid = LookupFuncNameTypeNames(functionName, argTypes, stmt->missing_ok);
691+
if (stmt->missing_ok &&!OidIsValid(funcOid))
692+
{
693+
ereport(NOTICE,
694+
(errmsg("function %s(%s) does not exist ... skipping",
695+
NameListToString(functionName),
696+
NameListToString(argTypes))));
697+
return;
698+
}
699+
691700

692701
tup = SearchSysCache(PROCOID,
693702
ObjectIdGetDatum(funcOid),
@@ -1377,6 +1386,7 @@ DropCast(DropCastStmt *stmt)
13771386
HeapTuple tuple;
13781387
ObjectAddress object;
13791388

1389+
/* when dropping a cast, the types must exist even if you use IF EXISTS */
13801390
sourcetypeid = typenameTypeId(NULL, stmt->sourcetype);
13811391
targettypeid = typenameTypeId(NULL, stmt->targettype);
13821392

@@ -1385,11 +1395,23 @@ DropCast(DropCastStmt *stmt)
13851395
ObjectIdGetDatum(targettypeid),
13861396
0, 0);
13871397
if (!HeapTupleIsValid(tuple))
1388-
ereport(ERROR,
1389-
(errcode(ERRCODE_UNDEFINED_OBJECT),
1390-
errmsg("cast from type %s to type %s does not exist",
1391-
TypeNameToString(stmt->sourcetype),
1392-
TypeNameToString(stmt->targettype))));
1398+
{
1399+
if (! stmt->missing_ok)
1400+
ereport(ERROR,
1401+
(errcode(ERRCODE_UNDEFINED_OBJECT),
1402+
errmsg("cast from type %s to type %s does not exist",
1403+
TypeNameToString(stmt->sourcetype),
1404+
TypeNameToString(stmt->targettype))));
1405+
else
1406+
ereport(NOTICE,
1407+
(errmsg("cast from type %s to type %s does not exist ... skipping",
1408+
TypeNameToString(stmt->sourcetype),
1409+
TypeNameToString(stmt->targettype))));
1410+
1411+
return;
1412+
}
1413+
1414+
13931415

13941416
/* Permission check */
13951417
if (!pg_type_ownercheck(sourcetypeid, GetUserId())

src/backend/commands/opclasscmds.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/opclasscmds.c,v 1.45 2006/05/02 22:25:10 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/opclasscmds.c,v 1.46 2006/06/16 20:23:44 adunstan Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -700,21 +700,40 @@ RemoveOpClass(RemoveOpClassStmt *stmt)
700700
/* Unqualified opclass name, so search the search path */
701701
opcID = OpclassnameGetOpcid(amID, opcname);
702702
if (!OidIsValid(opcID))
703-
ereport(ERROR,
704-
(errcode(ERRCODE_UNDEFINED_OBJECT),
705-
errmsg("operator class \"%s\" does not exist for access method \"%s\"",
706-
opcname, stmt->amname)));
703+
{
704+
if (! stmt -> missing_ok )
705+
ereport(ERROR,
706+
(errcode(ERRCODE_UNDEFINED_OBJECT),
707+
errmsg("operator class \"%s\" does not exist for access method \"%s\"",
708+
opcname, stmt->amname)));
709+
else
710+
ereport(NOTICE,
711+
(errmsg("operator class \"%s\" does not exist for access method \"%s\"",
712+
opcname, stmt->amname)));
713+
714+
return;
715+
}
716+
707717
tuple = SearchSysCache(CLAOID,
708718
ObjectIdGetDatum(opcID),
709719
0, 0, 0);
710720
}
711721

712722
if (!HeapTupleIsValid(tuple))
713-
ereport(ERROR,
714-
(errcode(ERRCODE_UNDEFINED_OBJECT),
715-
errmsg("operator class \"%s\" does not exist for access method \"%s\"",
716-
NameListToString(stmt->opclassname), stmt->amname)));
717-
723+
{
724+
725+
if (! stmt->missing_ok )
726+
ereport(ERROR,
727+
(errcode(ERRCODE_UNDEFINED_OBJECT),
728+
errmsg("operator class \"%s\" does not exist for access method \"%s\"",
729+
NameListToString(stmt->opclassname), stmt->amname)));
730+
else
731+
ereport(NOTICE,
732+
(errmsg("operator class \"%s\" does not exist for access method \"%s\"",
733+
NameListToString(stmt->opclassname), stmt->amname)));
734+
return;
735+
}
736+
718737
opcID = HeapTupleGetOid(tuple);
719738

720739
/* Permission check: must own opclass or its namespace */

src/backend/commands/operatorcmds.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/operatorcmds.c,v 1.30 2006/04/15 17:45:34 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/operatorcmds.c,v 1.31 2006/06/16 20:23:44 adunstan Exp $
1313
*
1414
* DESCRIPTION
1515
* The "DefineFoo" routines take the parse tree and pick out the
@@ -213,7 +213,15 @@ RemoveOperator(RemoveFuncStmt *stmt)
213213
Assert(list_length(stmt->args) == 2);
214214
operOid = LookupOperNameTypeNames(NULL, operatorName,
215215
typeName1, typeName2,
216-
false, -1);
216+
stmt->missing_ok, -1);
217+
218+
if (stmt->missing_ok &&!OidIsValid(operOid) )
219+
{
220+
ereport(NOTICE,
221+
(errmsg("operator %s does not exist ... skipping",
222+
NameListToString(operatorName))));
223+
return;
224+
}
217225

218226
tup = SearchSysCache(OPEROID,
219227
ObjectIdGetDatum(operOid),

src/backend/commands/proclang.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/proclang.c,v 1.64 2006/03/05 15:58:24 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/proclang.c,v 1.65 2006/06/16 20:23:44 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -396,9 +396,18 @@ DropProceduralLanguage(DropPLangStmt *stmt)
396396
CStringGetDatum(languageName),
397397
0, 0, 0);
398398
if (!HeapTupleIsValid(langTup))
399-
ereport(ERROR,
400-
(errcode(ERRCODE_UNDEFINED_OBJECT),
401-
errmsg("language \"%s\" does not exist", languageName)));
399+
{
400+
if (! stmt->missing_ok)
401+
ereport(ERROR,
402+
(errcode(ERRCODE_UNDEFINED_OBJECT),
403+
errmsg("language \"%s\" does not exist", languageName)));
404+
else
405+
ereport(NOTICE,
406+
(errmsg("language \"%s\" does not exist ... skipping",
407+
languageName)));
408+
409+
return;
410+
}
402411

403412
object.classId = LanguageRelationId;
404413
object.objectId = HeapTupleGetOid(langTup);

src/backend/commands/tablespace.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.34 2006/03/29 21:17:38 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.35 2006/06/16 20:23:44 adunstan Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -403,10 +403,25 @@ DropTableSpace(DropTableSpaceStmt *stmt)
403403
tuple = heap_getnext(scandesc, ForwardScanDirection);
404404

405405
if (!HeapTupleIsValid(tuple))
406-
ereport(ERROR,
407-
(errcode(ERRCODE_UNDEFINED_OBJECT),
408-
errmsg("tablespace \"%s\" does not exist",
409-
tablespacename)));
406+
{
407+
if ( ! stmt->missing_ok )
408+
{
409+
ereport(ERROR,
410+
(errcode(ERRCODE_UNDEFINED_OBJECT),
411+
errmsg("tablespace \"%s\" does not exist",
412+
tablespacename)));
413+
}
414+
else
415+
{
416+
ereport(NOTICE,
417+
(errmsg("tablespace \"%s\" does not exist ... skipping",
418+
tablespacename)));
419+
/* XXX I assume I need one or both of these next two calls */
420+
heap_endscan(scandesc);
421+
heap_close(rel, NoLock);
422+
}
423+
return;
424+
}
410425

411426
tablespaceoid = HeapTupleGetOid(tuple);
412427

src/backend/commands/trigger.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.202 2006/05/30 14:01:57 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.203 2006/06/16 20:23:44 adunstan Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -452,7 +452,8 @@ CreateTrigger(CreateTrigStmt *stmt, bool forConstraint)
452452
* DropTrigger - drop an individual trigger by name
453453
*/
454454
void
455-
DropTrigger(Oid relid, const char *trigname, DropBehavior behavior)
455+
DropTrigger(Oid relid, const char *trigname, DropBehavior behavior,
456+
bool missing_ok)
456457
{
457458
Relation tgrel;
458459
ScanKeyData skey[2];
@@ -481,10 +482,21 @@ DropTrigger(Oid relid, const char *trigname, DropBehavior behavior)
481482
tup = systable_getnext(tgscan);
482483

483484
if (!HeapTupleIsValid(tup))
484-
ereport(ERROR,
485-
(errcode(ERRCODE_UNDEFINED_OBJECT),
486-
errmsg("trigger \"%s\" for table \"%s\" does not exist",
487-
trigname, get_rel_name(relid))));
485+
{
486+
if (! missing_ok)
487+
ereport(ERROR,
488+
(errcode(ERRCODE_UNDEFINED_OBJECT),
489+
errmsg("trigger \"%s\" for table \"%s\" does not exist",
490+
trigname, get_rel_name(relid))));
491+
else
492+
ereport(NOTICE,
493+
(errmsg("trigger \"%s\" for table \"%s\" does not exist ...skipping",
494+
trigname, get_rel_name(relid))));
495+
/* cleanup */
496+
systable_endscan(tgscan);
497+
heap_close(tgrel, AccessShareLock);
498+
return;
499+
}
488500

489501
if (!pg_class_ownercheck(relid, GetUserId()))
490502
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS,

src/backend/nodes/copyfuncs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.335 2006/04/30 18:30:38 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.336 2006/06/16 20:23:44 adunstan Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2075,6 +2075,7 @@ _copyRemoveFuncStmt(RemoveFuncStmt *from)
20752075
COPY_NODE_FIELD(name);
20762076
COPY_NODE_FIELD(args);
20772077
COPY_SCALAR_FIELD(behavior);
2078+
COPY_SCALAR_FIELD(missing_ok);
20782079

20792080
return newnode;
20802081
}
@@ -2087,6 +2088,7 @@ _copyRemoveOpClassStmt(RemoveOpClassStmt *from)
20872088
COPY_NODE_FIELD(opclassname);
20882089
COPY_STRING_FIELD(amname);
20892090
COPY_SCALAR_FIELD(behavior);
2091+
COPY_SCALAR_FIELD(missing_ok);
20902092

20912093
return newnode;
20922094
}
@@ -2414,6 +2416,7 @@ _copyDropTableSpaceStmt(DropTableSpaceStmt *from)
24142416
DropTableSpaceStmt *newnode = makeNode(DropTableSpaceStmt);
24152417

24162418
COPY_STRING_FIELD(tablespacename);
2419+
COPY_SCALAR_FIELD(missing_ok);
24172420

24182421
return newnode;
24192422
}
@@ -2447,6 +2450,7 @@ _copyDropPropertyStmt(DropPropertyStmt *from)
24472450
COPY_STRING_FIELD(property);
24482451
COPY_SCALAR_FIELD(removeType);
24492452
COPY_SCALAR_FIELD(behavior);
2453+
COPY_SCALAR_FIELD(missing_ok);
24502454

24512455
return newnode;
24522456
}
@@ -2471,6 +2475,7 @@ _copyDropPLangStmt(DropPLangStmt *from)
24712475

24722476
COPY_STRING_FIELD(plname);
24732477
COPY_SCALAR_FIELD(behavior);
2478+
COPY_SCALAR_FIELD(missing_ok);
24742479

24752480
return newnode;
24762481
}
@@ -2606,6 +2611,7 @@ _copyDropCastStmt(DropCastStmt *from)
26062611
COPY_NODE_FIELD(sourcetype);
26072612
COPY_NODE_FIELD(targettype);
26082613
COPY_SCALAR_FIELD(behavior);
2614+
COPY_SCALAR_FIELD(missing_ok);
26092615

26102616
return newnode;
26112617
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy