PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dropcmds.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dropcmds.c
4  * handle various "DROP" operations
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/dropcmds.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/heapam.h"
18 #include "access/htup_details.h"
19 #include "catalog/dependency.h"
20 #include "catalog/namespace.h"
21 #include "catalog/objectaddress.h"
22 #include "catalog/pg_class.h"
23 #include "catalog/pg_proc.h"
24 #include "commands/defrem.h"
25 #include "miscadmin.h"
26 #include "nodes/makefuncs.h"
27 #include "parser/parse_type.h"
28 #include "utils/builtins.h"
29 #include "utils/syscache.h"
30 
31 
32 static void does_not_exist_skipping(ObjectType objtype,
33  Node *object);
34 static bool owningrel_does_not_exist_skipping(List *object,
35  const char **msg, char **name);
36 static bool schema_does_not_exist_skipping(List *object,
37  const char **msg, char **name);
38 static bool type_in_list_does_not_exist_skipping(List *typenames,
39  const char **msg, char **name);
40 
41 
42 /*
43  * Drop one or more objects.
44  *
45  * We don't currently handle all object types here. Relations, for example,
46  * require special handling, because (for example) indexes have additional
47  * locking requirements.
48  *
49  * We look up all the objects first, and then delete them in a single
50  * performMultipleDeletions() call. This avoids unnecessary DROP RESTRICT
51  * errors if there are dependencies between them.
52  */
53 void
55 {
56  ObjectAddresses *objects;
57  ListCell *cell1;
58 
59  objects = new_object_addresses();
60 
61  foreach(cell1, stmt->objects)
62  {
63  ObjectAddress address;
64  Node *object = lfirst(cell1);
65  Relation relation = NULL;
66  Oid namespaceId;
67 
68  /* Get an ObjectAddress for the object. */
69  address = get_object_address(stmt->removeType,
70  object,
71  &relation,
73  stmt->missing_ok);
74 
75  /*
76  * Issue NOTICE if supplied object was not found. Note this is only
77  * relevant in the missing_ok case, because otherwise
78  * get_object_address would have thrown an error.
79  */
80  if (!OidIsValid(address.objectId))
81  {
82  Assert(stmt->missing_ok);
83  does_not_exist_skipping(stmt->removeType, object);
84  continue;
85  }
86 
87  /*
88  * Although COMMENT ON FUNCTION, SECURITY LABEL ON FUNCTION, etc. are
89  * happy to operate on an aggregate as on any other function, we have
90  * historically not allowed this for DROP FUNCTION.
91  */
92  if (stmt->removeType == OBJECT_FUNCTION)
93  {
94  Oid funcOid = address.objectId;
95  HeapTuple tup;
96 
97  tup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
98  if (!HeapTupleIsValid(tup)) /* should not happen */
99  elog(ERROR, "cache lookup failed for function %u", funcOid);
100 
101  if (((Form_pg_proc) GETSTRUCT(tup))->proisagg)
102  ereport(ERROR,
103  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
104  errmsg("\"%s\" is an aggregate function",
105  NameListToString(castNode(ObjectWithArgs, object)->objname)),
106  errhint("Use DROP AGGREGATE to drop aggregate functions.")));
107 
108  ReleaseSysCache(tup);
109  }
110 
111  /* Check permissions. */
112  namespaceId = get_object_namespace(&address);
113  if (!OidIsValid(namespaceId) ||
114  !pg_namespace_ownercheck(namespaceId, GetUserId()))
115  check_object_ownership(GetUserId(), stmt->removeType, address,
116  object, relation);
117 
118  /* Release any relcache reference count, but keep lock until commit. */
119  if (relation)
120  heap_close(relation, NoLock);
121 
122  add_exact_object_address(&address, objects);
123  }
124 
125  /* Here we really delete them. */
126  performMultipleDeletions(objects, stmt->behavior, 0);
127 
128  free_object_addresses(objects);
129 }
130 
131 /*
132  * owningrel_does_not_exist_skipping
133  * Subroutine for RemoveObjects
134  *
135  * After determining that a specification for a rule or trigger returns that
136  * the specified object does not exist, test whether its owning relation, and
137  * its schema, exist or not; if they do, return false --- the trigger or rule
138  * itself is missing instead. If the owning relation or its schema do not
139  * exist, fill the error message format string and name, and return true.
140  */
141 static bool
142 owningrel_does_not_exist_skipping(List *object, const char **msg, char **name)
143 {
144  List *parent_object;
145  RangeVar *parent_rel;
146 
147  parent_object = list_truncate(list_copy(object),
148  list_length(object) - 1);
149 
150  if (schema_does_not_exist_skipping(parent_object, msg, name))
151  return true;
152 
153  parent_rel = makeRangeVarFromNameList(parent_object);
154 
155  if (!OidIsValid(RangeVarGetRelid(parent_rel, NoLock, true)))
156  {
157  *msg = gettext_noop("relation \"%s\" does not exist, skipping");
158  *name = NameListToString(parent_object);
159 
160  return true;
161  }
162 
163  return false;
164 }
165 
166 /*
167  * schema_does_not_exist_skipping
168  * Subroutine for RemoveObjects
169  *
170  * After determining that a specification for a schema-qualifiable object
171  * refers to an object that does not exist, test whether the specified schema
172  * exists or not. If no schema was specified, or if the schema does exist,
173  * return false -- the object itself is missing instead. If the specified
174  * schema does not exist, fill the error message format string and the
175  * specified schema name, and return true.
176  */
177 static bool
178 schema_does_not_exist_skipping(List *object, const char **msg, char **name)
179 {
180  RangeVar *rel;
181 
182  rel = makeRangeVarFromNameList(object);
183 
184  if (rel->schemaname != NULL &&
186  {
187  *msg = gettext_noop("schema \"%s\" does not exist, skipping");
188  *name = rel->schemaname;
189 
190  return true;
191  }
192 
193  return false;
194 }
195 
196 /*
197  * type_in_list_does_not_exist_skipping
198  * Subroutine for RemoveObjects
199  *
200  * After determining that a specification for a function, cast, aggregate or
201  * operator returns that the specified object does not exist, test whether the
202  * involved datatypes, and their schemas, exist or not; if they do, return
203  * false --- the original object itself is missing instead. If the datatypes
204  * or schemas do not exist, fill the error message format string and the
205  * missing name, and return true.
206  *
207  * First parameter is a list of TypeNames.
208  */
209 static bool
210 type_in_list_does_not_exist_skipping(List *typenames, const char **msg,
211  char **name)
212 {
213  ListCell *l;
214 
215  foreach(l, typenames)
216  {
217  TypeName *typeName = lfirst_node(TypeName, l);
218 
219  if (typeName != NULL)
220  {
221  if (!OidIsValid(LookupTypeNameOid(NULL, typeName, true)))
222  {
223  /* type doesn't exist, try to find why */
224  if (schema_does_not_exist_skipping(typeName->names, msg, name))
225  return true;
226 
227  *msg = gettext_noop("type \"%s\" does not exist, skipping");
228  *name = TypeNameToString(typeName);
229 
230  return true;
231  }
232  }
233  }
234 
235  return false;
236 }
237 
238 /*
239  * does_not_exist_skipping
240  * Subroutine for RemoveObjects
241  *
242  * Generate a NOTICE stating that the named object was not found, and is
243  * being skipped. This is only relevant when "IF EXISTS" is used; otherwise,
244  * get_object_address() in RemoveObjects would have thrown an ERROR.
245  */
246 static void
248 {
249  const char *msg = NULL;
250  char *name = NULL;
251  char *args = NULL;
252 
253  switch (objtype)
254  {
256  msg = gettext_noop("access method \"%s\" does not exist, skipping");
257  name = strVal((Value *) object);
258  break;
259  case OBJECT_TYPE:
260  case OBJECT_DOMAIN:
261  {
262  TypeName *typ = castNode(TypeName, object);
263 
264  if (!schema_does_not_exist_skipping(typ->names, &msg, &name))
265  {
266  msg = gettext_noop("type \"%s\" does not exist, skipping");
267  name = TypeNameToString(typ);
268  }
269  }
270  break;
271  case OBJECT_COLLATION:
272  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
273  {
274  msg = gettext_noop("collation \"%s\" does not exist, skipping");
275  name = NameListToString(castNode(List, object));
276  }
277  break;
278  case OBJECT_CONVERSION:
279  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
280  {
281  msg = gettext_noop("conversion \"%s\" does not exist, skipping");
282  name = NameListToString(castNode(List, object));
283  }
284  break;
285  case OBJECT_SCHEMA:
286  msg = gettext_noop("schema \"%s\" does not exist, skipping");
287  name = strVal((Value *) object);
288  break;
290  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
291  {
292  msg = gettext_noop("extended statistics \"%s\" do not exist, skipping");
293  name = NameListToString(castNode(List, object));
294  }
295  break;
296  case OBJECT_TSPARSER:
297  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
298  {
299  msg = gettext_noop("text search parser \"%s\" does not exist, skipping");
300  name = NameListToString(castNode(List, object));
301  }
302  break;
303  case OBJECT_TSDICTIONARY:
304  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
305  {
306  msg = gettext_noop("text search dictionary \"%s\" does not exist, skipping");
307  name = NameListToString(castNode(List, object));
308  }
309  break;
310  case OBJECT_TSTEMPLATE:
311  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
312  {
313  msg = gettext_noop("text search template \"%s\" does not exist, skipping");
314  name = NameListToString(castNode(List, object));
315  }
316  break;
318  if (!schema_does_not_exist_skipping(castNode(List, object), &msg, &name))
319  {
320  msg = gettext_noop("text search configuration \"%s\" does not exist, skipping");
321  name = NameListToString(castNode(List, object));
322  }
323  break;
324  case OBJECT_EXTENSION:
325  msg = gettext_noop("extension \"%s\" does not exist, skipping");
326  name = strVal((Value *) object);
327  break;
328  case OBJECT_FUNCTION:
329  {
330  ObjectWithArgs *owa = castNode(ObjectWithArgs, object);
331  if (!schema_does_not_exist_skipping(owa->objname, &msg, &name) &&
332  !type_in_list_does_not_exist_skipping(owa->objargs, &msg, &name))
333  {
334  msg = gettext_noop("function %s(%s) does not exist, skipping");
335  name = NameListToString(owa->objname);
336  args = TypeNameListToString(owa->objargs);
337  }
338  break;
339  }
340  case OBJECT_AGGREGATE:
341  {
342  ObjectWithArgs *owa = castNode(ObjectWithArgs, object);
343  if (!schema_does_not_exist_skipping(owa->objname, &msg, &name) &&
344  !type_in_list_does_not_exist_skipping(owa->objargs, &msg, &name))
345  {
346  msg = gettext_noop("aggregate %s(%s) does not exist, skipping");
347  name = NameListToString(owa->objname);
348  args = TypeNameListToString(owa->objargs);
349  }
350  break;
351  }
352  case OBJECT_OPERATOR:
353  {
354  ObjectWithArgs *owa = castNode(ObjectWithArgs, object);
355  if (!schema_does_not_exist_skipping(owa->objname, &msg, &name) &&
356  !type_in_list_does_not_exist_skipping(owa->objargs, &msg, &name))
357  {
358  msg = gettext_noop("operator %s does not exist, skipping");
359  name = NameListToString(owa->objname);
360  }
361  break;
362  }
363  case OBJECT_LANGUAGE:
364  msg = gettext_noop("language \"%s\" does not exist, skipping");
365  name = strVal((Value *) object);
366  break;
367  case OBJECT_CAST:
368  {
369  if (!type_in_list_does_not_exist_skipping(list_make1(linitial(castNode(List, object))), &msg, &name) &&
371  {
372  /* XXX quote or no quote? */
373  msg = gettext_noop("cast from type %s to type %s does not exist, skipping");
376  }
377  }
378  break;
379  case OBJECT_TRANSFORM:
381  {
382  msg = gettext_noop("transform for type %s language \"%s\" does not exist, skipping");
384  args = strVal(lsecond(castNode(List, object)));
385  }
386  break;
387  case OBJECT_TRIGGER:
388  if (!owningrel_does_not_exist_skipping(castNode(List, object), &msg, &name))
389  {
390  msg = gettext_noop("trigger \"%s\" for relation \"%s\" does not exist, skipping");
391  name = strVal(llast(castNode(List, object)));
393  list_length(castNode(List, object)) - 1));
394  }
395  break;
396  case OBJECT_POLICY:
397  if (!owningrel_does_not_exist_skipping(castNode(List, object), &msg, &name))
398  {
399  msg = gettext_noop("policy \"%s\" for relation \"%s\" does not exist, skipping");
400  name = strVal(llast(castNode(List, object)));
402  list_length(castNode(List, object)) - 1));
403  }
404  break;
406  msg = gettext_noop("event trigger \"%s\" does not exist, skipping");
407  name = strVal((Value *) object);
408  break;
409  case OBJECT_RULE:
410  if (!owningrel_does_not_exist_skipping(castNode(List, object), &msg, &name))
411  {
412  msg = gettext_noop("rule \"%s\" for relation \"%s\" does not exist, skipping");
413  name = strVal(llast(castNode(List, object)));
415  list_length(castNode(List, object)) - 1));
416  }
417  break;
418  case OBJECT_FDW:
419  msg = gettext_noop("foreign-data wrapper \"%s\" does not exist, skipping");
420  name = strVal((Value *) object);
421  break;
423  msg = gettext_noop("server \"%s\" does not exist, skipping");
424  name = strVal((Value *) object);
425  break;
426  case OBJECT_OPCLASS:
427  {
428  List *opcname = list_copy_tail(castNode(List, object), 1);
429 
430  if (!schema_does_not_exist_skipping(opcname, &msg, &name))
431  {
432  msg = gettext_noop("operator class \"%s\" does not exist for access method \"%s\", skipping");
433  name = NameListToString(opcname);
434  args = strVal(linitial(castNode(List, object)));
435  }
436  }
437  break;
438  case OBJECT_OPFAMILY:
439  {
440  List *opfname = list_copy_tail(castNode(List, object), 1);
441 
442  if (!schema_does_not_exist_skipping(opfname, &msg, &name))
443  {
444  msg = gettext_noop("operator family \"%s\" does not exist for access method \"%s\", skipping");
445  name = NameListToString(opfname);
446  args = strVal(linitial(castNode(List, object)));
447  }
448  }
449  break;
450  case OBJECT_PUBLICATION:
451  msg = gettext_noop("publication \"%s\" does not exist, skipping");
452  name = strVal((Value *) object);
453  break;
454  default:
455  elog(ERROR, "unrecognized object type: %d", (int) objtype);
456  break;
457  }
458 
459  if (!args)
460  ereport(NOTICE, (errmsg(msg, name)));
461  else
462  ereport(NOTICE, (errmsg(msg, name, args)));
463 }
static bool schema_does_not_exist_skipping(List *object, const char **msg, char **name)
Definition: dropcmds.c:178
int errhint(const char *fmt,...)
Definition: elog.c:987
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
List * names
Definition: parsenodes.h:209
Oid GetUserId(void)
Definition: miscinit.c:283
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
char * TypeNameToString(const TypeName *typeName)
Definition: parse_type.c:459
List * objects
Definition: parsenodes.h:2541
bool missing_ok
Definition: parsenodes.h:2544
#define llast(l)
Definition: pg_list.h:131
List * list_truncate(List *list, int new_size)
Definition: list.c:350
#define gettext_noop(x)
Definition: c.h:139
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:509
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
char * TypeNameListToString(List *typenames)
Definition: parse_type.c:473
bool pg_namespace_ownercheck(Oid nsp_oid, Oid roleid)
Definition: aclchk.c:4722
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2913
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2094
#define heap_close(r, l)
Definition: heapam.h:97
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2039
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2310
unsigned int Oid
Definition: postgres_ext.h:31
void RemoveObjects(DropStmt *stmt)
Definition: dropcmds.c:54
#define linitial_node(type, l)
Definition: pg_list.h:114
#define OidIsValid(objectId)
Definition: c.h:538
#define lsecond(l)
Definition: pg_list.h:116
ObjectType removeType
Definition: parsenodes.h:2542
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
char * schemaname
Definition: primnodes.h:67
#define list_make1(x1)
Definition: pg_list.h:139
#define linitial(l)
Definition: pg_list.h:111
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define lfirst_node(type, lc)
Definition: pg_list.h:109
#define NoLock
Definition: lockdefs.h:34
DropBehavior behavior
Definition: parsenodes.h:2543
#define ereport(elevel, rest)
Definition: elog.h:122
ObjectType
Definition: parsenodes.h:1575
char * NameListToString(List *names)
Definition: namespace.c:2953
#define lsecond_node(type, l)
Definition: pg_list.h:119
static bool type_in_list_does_not_exist_skipping(List *typenames, const char **msg, char **name)
Definition: dropcmds.c:210
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1116
static bool owningrel_does_not_exist_skipping(List *object, const char **msg, char **name)
Definition: dropcmds.c:142
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define NOTICE
Definition: elog.h:37
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Definition: value.h:42
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
static int list_length(const List *l)
Definition: pg_list.h:89
const char * name
Definition: encode.c:521
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
Oid get_object_namespace(const ObjectAddress *address)
#define AccessExclusiveLock
Definition: lockdefs.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
Definition: dependency.c:362
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static void does_not_exist_skipping(ObjectType objtype, Node *object)
Definition: dropcmds.c:247
Oid LookupNamespaceNoError(const char *nspname)
Definition: namespace.c:2713
Oid LookupTypeNameOid(ParseState *pstate, const TypeName *typeName, bool missing_ok)
Definition: parse_type.c:215