PostgreSQL Source Code  git master
dependency.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dependency.c
4  * Routines to support inter-object dependencies.
5  *
6  *
7  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/dependency.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/xact.h"
19 #include "catalog/dependency.h"
20 #include "catalog/heap.h"
21 #include "catalog/index.h"
22 #include "catalog/objectaccess.h"
23 #include "catalog/pg_am.h"
24 #include "catalog/pg_amop.h"
25 #include "catalog/pg_amproc.h"
26 #include "catalog/pg_attrdef.h"
27 #include "catalog/pg_authid.h"
28 #include "catalog/pg_cast.h"
29 #include "catalog/pg_collation.h"
31 #include "catalog/pg_constraint.h"
33 #include "catalog/pg_conversion.h"
35 #include "catalog/pg_database.h"
36 #include "catalog/pg_default_acl.h"
37 #include "catalog/pg_depend.h"
39 #include "catalog/pg_extension.h"
42 #include "catalog/pg_init_privs.h"
43 #include "catalog/pg_language.h"
44 #include "catalog/pg_largeobject.h"
45 #include "catalog/pg_namespace.h"
46 #include "catalog/pg_opclass.h"
47 #include "catalog/pg_operator.h"
48 #include "catalog/pg_opfamily.h"
49 #include "catalog/pg_policy.h"
50 #include "catalog/pg_proc.h"
51 #include "catalog/pg_publication.h"
53 #include "catalog/pg_rewrite.h"
56 #include "catalog/pg_tablespace.h"
57 #include "catalog/pg_transform.h"
58 #include "catalog/pg_trigger.h"
59 #include "catalog/pg_ts_config.h"
60 #include "catalog/pg_ts_dict.h"
61 #include "catalog/pg_ts_parser.h"
62 #include "catalog/pg_ts_template.h"
63 #include "catalog/pg_type.h"
65 #include "commands/comment.h"
66 #include "commands/defrem.h"
67 #include "commands/event_trigger.h"
68 #include "commands/extension.h"
69 #include "commands/policy.h"
70 #include "commands/proclang.h"
72 #include "commands/schemacmds.h"
73 #include "commands/seclabel.h"
74 #include "commands/sequence.h"
75 #include "commands/trigger.h"
76 #include "commands/typecmds.h"
77 #include "nodes/nodeFuncs.h"
78 #include "parser/parsetree.h"
79 #include "rewrite/rewriteRemove.h"
80 #include "storage/lmgr.h"
81 #include "utils/fmgroids.h"
82 #include "utils/guc.h"
83 #include "utils/lsyscache.h"
84 #include "utils/syscache.h"
85 #include "utils/tqual.h"
86 
87 
88 /*
89  * Deletion processing requires additional state for each ObjectAddress that
90  * it's planning to delete. For simplicity and code-sharing we make the
91  * ObjectAddresses code support arrays with or without this extra state.
92  */
93 typedef struct
94 {
95  int flags; /* bitmask, see bit definitions below */
96  ObjectAddress dependee; /* object whose deletion forced this one */
98 
99 /* ObjectAddressExtra flag bits */
100 #define DEPFLAG_ORIGINAL 0x0001 /* an original deletion target */
101 #define DEPFLAG_NORMAL 0x0002 /* reached via normal dependency */
102 #define DEPFLAG_AUTO 0x0004 /* reached via auto dependency */
103 #define DEPFLAG_INTERNAL 0x0008 /* reached via internal dependency */
104 #define DEPFLAG_EXTENSION 0x0010 /* reached via extension dependency */
105 #define DEPFLAG_REVERSE 0x0020 /* reverse internal/extension link */
106 
107 
108 /* expansible list of ObjectAddresses */
110 {
111  ObjectAddress *refs; /* => palloc'd array */
112  ObjectAddressExtra *extras; /* => palloc'd array, or NULL if not used */
113  int numrefs; /* current number of references */
114  int maxrefs; /* current size of palloc'd array(s) */
115 };
116 
117 /* typedef ObjectAddresses appears in dependency.h */
118 
119 /* threaded list of ObjectAddresses, for recursion detection */
120 typedef struct ObjectAddressStack
121 {
122  const ObjectAddress *object; /* object being visited */
123  int flags; /* its current flag bits */
124  struct ObjectAddressStack *next; /* next outer stack level */
126 
127 /* for find_expr_references_walker */
128 typedef struct
129 {
130  ObjectAddresses *addrs; /* addresses being accumulated */
131  List *rtables; /* list of rangetables to resolve Vars */
133 
134 /*
135  * This constant table maps ObjectClasses to the corresponding catalog OIDs.
136  * See also getObjectClass().
137  */
138 static const Oid object_classes[] = {
139  RelationRelationId, /* OCLASS_CLASS */
140  ProcedureRelationId, /* OCLASS_PROC */
141  TypeRelationId, /* OCLASS_TYPE */
142  CastRelationId, /* OCLASS_CAST */
143  CollationRelationId, /* OCLASS_COLLATION */
144  ConstraintRelationId, /* OCLASS_CONSTRAINT */
145  ConversionRelationId, /* OCLASS_CONVERSION */
146  AttrDefaultRelationId, /* OCLASS_DEFAULT */
147  LanguageRelationId, /* OCLASS_LANGUAGE */
148  LargeObjectRelationId, /* OCLASS_LARGEOBJECT */
149  OperatorRelationId, /* OCLASS_OPERATOR */
150  OperatorClassRelationId, /* OCLASS_OPCLASS */
151  OperatorFamilyRelationId, /* OCLASS_OPFAMILY */
152  AccessMethodRelationId, /* OCLASS_AM */
153  AccessMethodOperatorRelationId, /* OCLASS_AMOP */
154  AccessMethodProcedureRelationId, /* OCLASS_AMPROC */
155  RewriteRelationId, /* OCLASS_REWRITE */
156  TriggerRelationId, /* OCLASS_TRIGGER */
157  NamespaceRelationId, /* OCLASS_SCHEMA */
158  StatisticExtRelationId, /* OCLASS_STATISTIC_EXT */
159  TSParserRelationId, /* OCLASS_TSPARSER */
160  TSDictionaryRelationId, /* OCLASS_TSDICT */
161  TSTemplateRelationId, /* OCLASS_TSTEMPLATE */
162  TSConfigRelationId, /* OCLASS_TSCONFIG */
163  AuthIdRelationId, /* OCLASS_ROLE */
164  DatabaseRelationId, /* OCLASS_DATABASE */
165  TableSpaceRelationId, /* OCLASS_TBLSPACE */
166  ForeignDataWrapperRelationId, /* OCLASS_FDW */
167  ForeignServerRelationId, /* OCLASS_FOREIGN_SERVER */
168  UserMappingRelationId, /* OCLASS_USER_MAPPING */
169  DefaultAclRelationId, /* OCLASS_DEFACL */
170  ExtensionRelationId, /* OCLASS_EXTENSION */
171  EventTriggerRelationId, /* OCLASS_EVENT_TRIGGER */
172  PolicyRelationId, /* OCLASS_POLICY */
173  PublicationRelationId, /* OCLASS_PUBLICATION */
174  PublicationRelRelationId, /* OCLASS_PUBLICATION_REL */
175  SubscriptionRelationId, /* OCLASS_SUBSCRIPTION */
176  TransformRelationId /* OCLASS_TRANSFORM */
177 };
178 
179 
180 static void findDependentObjects(const ObjectAddress *object,
181  int objflags,
182  int flags,
183  ObjectAddressStack *stack,
184  ObjectAddresses *targetObjects,
185  const ObjectAddresses *pendingObjects,
186  Relation *depRel);
187 static void reportDependentObjects(const ObjectAddresses *targetObjects,
188  DropBehavior behavior,
189  int flags,
190  const ObjectAddress *origObject);
191 static void deleteOneObject(const ObjectAddress *object,
192  Relation *depRel, int32 flags);
193 static void doDeletion(const ObjectAddress *object, int flags);
194 static void AcquireDeletionLock(const ObjectAddress *object, int flags);
195 static void ReleaseDeletionLock(const ObjectAddress *object);
196 static bool find_expr_references_walker(Node *node,
199 static int object_address_comparator(const void *a, const void *b);
200 static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
201  ObjectAddresses *addrs);
202 static void add_exact_object_address_extra(const ObjectAddress *object,
203  const ObjectAddressExtra *extra,
204  ObjectAddresses *addrs);
205 static bool object_address_present_add_flags(const ObjectAddress *object,
206  int flags,
207  ObjectAddresses *addrs);
208 static bool stack_address_present_add_flags(const ObjectAddress *object,
209  int flags,
210  ObjectAddressStack *stack);
211 static void DeleteInitPrivs(const ObjectAddress *object);
212 
213 
214 /*
215  * Go through the objects given running the final actions on them, and execute
216  * the actual deletion.
217  */
218 static void
220  int flags)
221 {
222  int i;
223 
224  /*
225  * Keep track of objects for event triggers, if necessary.
226  */
228  {
229  for (i = 0; i < targetObjects->numrefs; i++)
230  {
231  const ObjectAddress *thisobj = &targetObjects->refs[i];
232  const ObjectAddressExtra *extra = &targetObjects->extras[i];
233  bool original = false;
234  bool normal = false;
235 
236  if (extra->flags & DEPFLAG_ORIGINAL)
237  original = true;
238  if (extra->flags & DEPFLAG_NORMAL)
239  normal = true;
240  if (extra->flags & DEPFLAG_REVERSE)
241  normal = true;
242 
244  {
245  EventTriggerSQLDropAddObject(thisobj, original, normal);
246  }
247  }
248  }
249 
250  /*
251  * Delete all the objects in the proper order, except that if told to, we
252  * should skip the original object(s).
253  */
254  for (i = 0; i < targetObjects->numrefs; i++)
255  {
256  ObjectAddress *thisobj = targetObjects->refs + i;
257  ObjectAddressExtra *thisextra = targetObjects->extras + i;
258 
259  if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
260  (thisextra->flags & DEPFLAG_ORIGINAL))
261  continue;
262 
263  deleteOneObject(thisobj, depRel, flags);
264  }
265 }
266 
267 /*
268  * performDeletion: attempt to drop the specified object. If CASCADE
269  * behavior is specified, also drop any dependent objects (recursively).
270  * If RESTRICT behavior is specified, error out if there are any dependent
271  * objects, except for those that should be implicitly dropped anyway
272  * according to the dependency type.
273  *
274  * This is the outer control routine for all forms of DROP that drop objects
275  * that can participate in dependencies. Note that performMultipleDeletions
276  * is a variant on the same theme; if you change anything here you'll likely
277  * need to fix that too.
278  *
279  * Bits in the flags argument can include:
280  *
281  * PERFORM_DELETION_INTERNAL: indicates that the drop operation is not the
282  * direct result of a user-initiated action. For example, when a temporary
283  * schema is cleaned out so that a new backend can use it, or when a column
284  * default is dropped as an intermediate step while adding a new one, that's
285  * an internal operation. On the other hand, when we drop something because
286  * the user issued a DROP statement against it, that's not internal. Currently
287  * this suppresses calling event triggers and making some permissions checks.
288  *
289  * PERFORM_DELETION_CONCURRENTLY: perform the drop concurrently. This does
290  * not currently work for anything except dropping indexes; don't set it for
291  * other object types or you may get strange results.
292  *
293  * PERFORM_DELETION_QUIETLY: reduce message level from NOTICE to DEBUG2.
294  *
295  * PERFORM_DELETION_SKIP_ORIGINAL: do not delete the specified object(s),
296  * but only what depends on it/them.
297  *
298  * PERFORM_DELETION_SKIP_EXTENSIONS: do not delete extensions, even when
299  * deleting objects that are part of an extension. This should generally
300  * be used only when dropping temporary objects.
301  */
302 void
304  DropBehavior behavior, int flags)
305 {
306  Relation depRel;
307  ObjectAddresses *targetObjects;
308 
309  /*
310  * We save some cycles by opening pg_depend just once and passing the
311  * Relation pointer down to all the recursive deletion steps.
312  */
314 
315  /*
316  * Acquire deletion lock on the target object. (Ideally the caller has
317  * done this already, but many places are sloppy about it.)
318  */
319  AcquireDeletionLock(object, 0);
320 
321  /*
322  * Construct a list of objects to delete (ie, the given object plus
323  * everything directly or indirectly dependent on it).
324  */
325  targetObjects = new_object_addresses();
326 
327  findDependentObjects(object,
329  flags,
330  NULL, /* empty stack */
331  targetObjects,
332  NULL, /* no pendingObjects */
333  &depRel);
334 
335  /*
336  * Check if deletion is allowed, and report about cascaded deletes.
337  */
338  reportDependentObjects(targetObjects,
339  behavior,
340  flags,
341  object);
342 
343  /* do the deed */
344  deleteObjectsInList(targetObjects, &depRel, flags);
345 
346  /* And clean up */
347  free_object_addresses(targetObjects);
348 
349  heap_close(depRel, RowExclusiveLock);
350 }
351 
352 /*
353  * performMultipleDeletions: Similar to performDeletion, but act on multiple
354  * objects at once.
355  *
356  * The main difference from issuing multiple performDeletion calls is that the
357  * list of objects that would be implicitly dropped, for each object to be
358  * dropped, is the union of the implicit-object list for all objects. This
359  * makes each check be more relaxed.
360  */
361 void
363  DropBehavior behavior, int flags)
364 {
365  Relation depRel;
366  ObjectAddresses *targetObjects;
367  int i;
368 
369  /* No work if no objects... */
370  if (objects->numrefs <= 0)
371  return;
372 
373  /*
374  * We save some cycles by opening pg_depend just once and passing the
375  * Relation pointer down to all the recursive deletion steps.
376  */
378 
379  /*
380  * Construct a list of objects to delete (ie, the given objects plus
381  * everything directly or indirectly dependent on them). Note that
382  * because we pass the whole objects list as pendingObjects context, we
383  * won't get a failure from trying to delete an object that is internally
384  * dependent on another one in the list; we'll just skip that object and
385  * delete it when we reach its owner.
386  */
387  targetObjects = new_object_addresses();
388 
389  for (i = 0; i < objects->numrefs; i++)
390  {
391  const ObjectAddress *thisobj = objects->refs + i;
392 
393  /*
394  * Acquire deletion lock on each target object. (Ideally the caller
395  * has done this already, but many places are sloppy about it.)
396  */
397  AcquireDeletionLock(thisobj, flags);
398 
399  findDependentObjects(thisobj,
401  flags,
402  NULL, /* empty stack */
403  targetObjects,
404  objects,
405  &depRel);
406  }
407 
408  /*
409  * Check if deletion is allowed, and report about cascaded deletes.
410  *
411  * If there's exactly one object being deleted, report it the same way as
412  * in performDeletion(), else we have to be vaguer.
413  */
414  reportDependentObjects(targetObjects,
415  behavior,
416  flags,
417  (objects->numrefs == 1 ? objects->refs : NULL));
418 
419  /* do the deed */
420  deleteObjectsInList(targetObjects, &depRel, flags);
421 
422  /* And clean up */
423  free_object_addresses(targetObjects);
424 
425  heap_close(depRel, RowExclusiveLock);
426 }
427 
428 /*
429  * findDependentObjects - find all objects that depend on 'object'
430  *
431  * For every object that depends on the starting object, acquire a deletion
432  * lock on the object, add it to targetObjects (if not already there),
433  * and recursively find objects that depend on it. An object's dependencies
434  * will be placed into targetObjects before the object itself; this means
435  * that the finished list's order represents a safe deletion order.
436  *
437  * The caller must already have a deletion lock on 'object' itself,
438  * but must not have added it to targetObjects. (Note: there are corner
439  * cases where we won't add the object either, and will also release the
440  * caller-taken lock. This is a bit ugly, but the API is set up this way
441  * to allow easy rechecking of an object's liveness after we lock it. See
442  * notes within the function.)
443  *
444  * When dropping a whole object (subId = 0), we find dependencies for
445  * its sub-objects too.
446  *
447  * object: the object to add to targetObjects and find dependencies on
448  * objflags: flags to be ORed into the object's targetObjects entry
449  * flags: PERFORM_DELETION_xxx flags for the deletion operation as a whole
450  * stack: list of objects being visited in current recursion; topmost item
451  * is the object that we recursed from (NULL for external callers)
452  * targetObjects: list of objects that are scheduled to be deleted
453  * pendingObjects: list of other objects slated for destruction, but
454  * not necessarily in targetObjects yet (can be NULL if none)
455  * *depRel: already opened pg_depend relation
456  *
457  * Note: objflags describes the reason for visiting this particular object
458  * at this time, and is not passed down when recursing. The flags argument
459  * is passed down, since it describes what we're doing overall.
460  */
461 static void
463  int objflags,
464  int flags,
465  ObjectAddressStack *stack,
466  ObjectAddresses *targetObjects,
467  const ObjectAddresses *pendingObjects,
468  Relation *depRel)
469 {
470  ScanKeyData key[3];
471  int nkeys;
472  SysScanDesc scan;
473  HeapTuple tup;
474  ObjectAddress otherObject;
475  ObjectAddressStack mystack;
476  ObjectAddressExtra extra;
477 
478  /*
479  * If the target object is already being visited in an outer recursion
480  * level, just report the current objflags back to that level and exit.
481  * This is needed to avoid infinite recursion in the face of circular
482  * dependencies.
483  *
484  * The stack check alone would result in dependency loops being broken at
485  * an arbitrary point, ie, the first member object of the loop to be
486  * visited is the last one to be deleted. This is obviously unworkable.
487  * However, the check for internal dependency below guarantees that we
488  * will not break a loop at an internal dependency: if we enter the loop
489  * at an "owned" object we will switch and start at the "owning" object
490  * instead. We could probably hack something up to avoid breaking at an
491  * auto dependency, too, if we had to. However there are no known cases
492  * where that would be necessary.
493  */
494  if (stack_address_present_add_flags(object, objflags, stack))
495  return;
496 
497  /*
498  * It's also possible that the target object has already been completely
499  * processed and put into targetObjects. If so, again we just add the
500  * specified objflags to its entry and return.
501  *
502  * (Note: in these early-exit cases we could release the caller-taken
503  * lock, since the object is presumably now locked multiple times; but it
504  * seems not worth the cycles.)
505  */
506  if (object_address_present_add_flags(object, objflags, targetObjects))
507  return;
508 
509  /*
510  * The target object might be internally dependent on some other object
511  * (its "owner"), and/or be a member of an extension (also considered its
512  * owner). If so, and if we aren't recursing from the owning object, we
513  * have to transform this deletion request into a deletion request of the
514  * owning object. (We'll eventually recurse back to this object, but the
515  * owning object has to be visited first so it will be deleted after.) The
516  * way to find out about this is to scan the pg_depend entries that show
517  * what this object depends on.
518  */
519  ScanKeyInit(&key[0],
521  BTEqualStrategyNumber, F_OIDEQ,
522  ObjectIdGetDatum(object->classId));
523  ScanKeyInit(&key[1],
525  BTEqualStrategyNumber, F_OIDEQ,
526  ObjectIdGetDatum(object->objectId));
527  if (object->objectSubId != 0)
528  {
529  ScanKeyInit(&key[2],
531  BTEqualStrategyNumber, F_INT4EQ,
532  Int32GetDatum(object->objectSubId));
533  nkeys = 3;
534  }
535  else
536  nkeys = 2;
537 
538  scan = systable_beginscan(*depRel, DependDependerIndexId, true,
539  NULL, nkeys, key);
540 
541  while (HeapTupleIsValid(tup = systable_getnext(scan)))
542  {
543  Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
544 
545  otherObject.classId = foundDep->refclassid;
546  otherObject.objectId = foundDep->refobjid;
547  otherObject.objectSubId = foundDep->refobjsubid;
548 
549  switch (foundDep->deptype)
550  {
551  case DEPENDENCY_NORMAL:
552  case DEPENDENCY_AUTO:
554  /* no problem */
555  break;
556 
558 
559  /*
560  * If told to, ignore EXTENSION dependencies altogether. This
561  * flag is normally used to prevent dropping extensions during
562  * temporary-object cleanup, even if a temp object was created
563  * during an extension script.
564  */
566  break;
567 
568  /*
569  * If the other object is the extension currently being
570  * created/altered, ignore this dependency and continue with
571  * the deletion. This allows dropping of an extension's
572  * objects within the extension's scripts, as well as corner
573  * cases such as dropping a transient object created within
574  * such a script.
575  */
576  if (creating_extension &&
577  otherObject.classId == ExtensionRelationId &&
578  otherObject.objectId == CurrentExtensionObject)
579  break;
580 
581  /* Otherwise, treat this like an internal dependency */
582  /* FALL THRU */
583 
584  case DEPENDENCY_INTERNAL:
586 
587  /*
588  * This object is part of the internal implementation of
589  * another object, or is part of the extension that is the
590  * other object. We have three cases:
591  *
592  * 1. At the outermost recursion level, disallow the DROP. (We
593  * just ereport here, rather than proceeding, since no other
594  * dependencies are likely to be interesting.) However, if
595  * the owning object is listed in pendingObjects, just release
596  * the caller's lock and return; we'll eventually complete the
597  * DROP when we reach that entry in the pending list.
598  */
599  if (stack == NULL)
600  {
601  char *otherObjDesc;
602 
603  if (pendingObjects &&
604  object_address_present(&otherObject, pendingObjects))
605  {
606  systable_endscan(scan);
607  /* need to release caller's lock; see notes below */
608  ReleaseDeletionLock(object);
609  return;
610  }
611  otherObjDesc = getObjectDescription(&otherObject);
612  ereport(ERROR,
613  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
614  errmsg("cannot drop %s because %s requires it",
615  getObjectDescription(object),
616  otherObjDesc),
617  errhint("You can drop %s instead.",
618  otherObjDesc)));
619  }
620 
621  /*
622  * 2. When recursing from the other end of this dependency,
623  * it's okay to continue with the deletion. This holds when
624  * recursing from a whole object that includes the nominal
625  * other end as a component, too. Since there can be more
626  * than one "owning" object, we have to allow matches that are
627  * more than one level down in the stack.
628  */
629  if (stack_address_present_add_flags(&otherObject, 0, stack))
630  break;
631 
632  /*
633  * 3. Not all the owning objects have been visited, so
634  * transform this deletion request into a delete of this
635  * owning object.
636  *
637  * For INTERNAL_AUTO dependencies, we don't enforce this;
638  * in other words, we don't follow the links back to the
639  * owning object.
640  */
641  if (foundDep->deptype == DEPENDENCY_INTERNAL_AUTO)
642  break;
643 
644  /*
645  * First, release caller's lock on this object and get
646  * deletion lock on the owning object. (We must release
647  * caller's lock to avoid deadlock against a concurrent
648  * deletion of the owning object.)
649  */
650  ReleaseDeletionLock(object);
651  AcquireDeletionLock(&otherObject, 0);
652 
653  /*
654  * The owning object might have been deleted while we waited
655  * to lock it; if so, neither it nor the current object are
656  * interesting anymore. We test this by checking the
657  * pg_depend entry (see notes below).
658  */
659  if (!systable_recheck_tuple(scan, tup))
660  {
661  systable_endscan(scan);
662  ReleaseDeletionLock(&otherObject);
663  return;
664  }
665 
666  /*
667  * Okay, recurse to the owning object instead of proceeding.
668  *
669  * We do not need to stack the current object; we want the
670  * traversal order to be as if the original reference had
671  * linked to the owning object instead of this one.
672  *
673  * The dependency type is a "reverse" dependency: we need to
674  * delete the owning object if this one is to be deleted, but
675  * this linkage is never a reason for an automatic deletion.
676  */
677  findDependentObjects(&otherObject,
679  flags,
680  stack,
681  targetObjects,
682  pendingObjects,
683  depRel);
684  /* And we're done here. */
685  systable_endscan(scan);
686  return;
687 
688  case DEPENDENCY_PIN:
689 
690  /*
691  * Should not happen; PIN dependencies should have zeroes in
692  * the depender fields...
693  */
694  elog(ERROR, "incorrect use of PIN dependency with %s",
695  getObjectDescription(object));
696  break;
697  default:
698  elog(ERROR, "unrecognized dependency type '%c' for %s",
699  foundDep->deptype, getObjectDescription(object));
700  break;
701  }
702  }
703 
704  systable_endscan(scan);
705 
706  /*
707  * Now recurse to any dependent objects. We must visit them first since
708  * they have to be deleted before the current object.
709  */
710  mystack.object = object; /* set up a new stack level */
711  mystack.flags = objflags;
712  mystack.next = stack;
713 
714  ScanKeyInit(&key[0],
716  BTEqualStrategyNumber, F_OIDEQ,
717  ObjectIdGetDatum(object->classId));
718  ScanKeyInit(&key[1],
720  BTEqualStrategyNumber, F_OIDEQ,
721  ObjectIdGetDatum(object->objectId));
722  if (object->objectSubId != 0)
723  {
724  ScanKeyInit(&key[2],
726  BTEqualStrategyNumber, F_INT4EQ,
727  Int32GetDatum(object->objectSubId));
728  nkeys = 3;
729  }
730  else
731  nkeys = 2;
732 
733  scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
734  NULL, nkeys, key);
735 
736  while (HeapTupleIsValid(tup = systable_getnext(scan)))
737  {
738  Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
739  int subflags;
740 
741  otherObject.classId = foundDep->classid;
742  otherObject.objectId = foundDep->objid;
743  otherObject.objectSubId = foundDep->objsubid;
744 
745  /*
746  * Must lock the dependent object before recursing to it.
747  */
748  AcquireDeletionLock(&otherObject, 0);
749 
750  /*
751  * The dependent object might have been deleted while we waited to
752  * lock it; if so, we don't need to do anything more with it. We can
753  * test this cheaply and independently of the object's type by seeing
754  * if the pg_depend tuple we are looking at is still live. (If the
755  * object got deleted, the tuple would have been deleted too.)
756  */
757  if (!systable_recheck_tuple(scan, tup))
758  {
759  /* release the now-useless lock */
760  ReleaseDeletionLock(&otherObject);
761  /* and continue scanning for dependencies */
762  continue;
763  }
764 
765  /* Recurse, passing objflags indicating the dependency type */
766  switch (foundDep->deptype)
767  {
768  case DEPENDENCY_NORMAL:
769  subflags = DEPFLAG_NORMAL;
770  break;
771  case DEPENDENCY_AUTO:
773  subflags = DEPFLAG_AUTO;
774  break;
776  case DEPENDENCY_INTERNAL:
777  subflags = DEPFLAG_INTERNAL;
778  break;
780  subflags = DEPFLAG_EXTENSION;
781  break;
782  case DEPENDENCY_PIN:
783 
784  /*
785  * For a PIN dependency we just ereport immediately; there
786  * won't be any others to report.
787  */
788  ereport(ERROR,
789  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
790  errmsg("cannot drop %s because it is required by the database system",
791  getObjectDescription(object))));
792  subflags = 0; /* keep compiler quiet */
793  break;
794  default:
795  elog(ERROR, "unrecognized dependency type '%c' for %s",
796  foundDep->deptype, getObjectDescription(object));
797  subflags = 0; /* keep compiler quiet */
798  break;
799  }
800 
801  findDependentObjects(&otherObject,
802  subflags,
803  flags,
804  &mystack,
805  targetObjects,
806  pendingObjects,
807  depRel);
808  }
809 
810  systable_endscan(scan);
811 
812  /*
813  * Finally, we can add the target object to targetObjects. Be careful to
814  * include any flags that were passed back down to us from inner recursion
815  * levels.
816  */
817  extra.flags = mystack.flags;
818  if (stack)
819  extra.dependee = *stack->object;
820  else
821  memset(&extra.dependee, 0, sizeof(extra.dependee));
822  add_exact_object_address_extra(object, &extra, targetObjects);
823 }
824 
825 /*
826  * reportDependentObjects - report about dependencies, and fail if RESTRICT
827  *
828  * Tell the user about dependent objects that we are going to delete
829  * (or would need to delete, but are prevented by RESTRICT mode);
830  * then error out if there are any and it's not CASCADE mode.
831  *
832  * targetObjects: list of objects that are scheduled to be deleted
833  * behavior: RESTRICT or CASCADE
834  * flags: other flags for the deletion operation
835  * origObject: base object of deletion, or NULL if not available
836  * (the latter case occurs in DROP OWNED)
837  */
838 static void
840  DropBehavior behavior,
841  int flags,
842  const ObjectAddress *origObject)
843 {
844  int msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
845  bool ok = true;
846  StringInfoData clientdetail;
847  StringInfoData logdetail;
848  int numReportedClient = 0;
849  int numNotReportedClient = 0;
850  int i;
851 
852  /*
853  * If no error is to be thrown, and the msglevel is too low to be shown to
854  * either client or server log, there's no need to do any of the work.
855  *
856  * Note: this code doesn't know all there is to be known about elog
857  * levels, but it works for NOTICE and DEBUG2, which are the only values
858  * msglevel can currently have. We also assume we are running in a normal
859  * operating environment.
860  */
861  if (behavior == DROP_CASCADE &&
862  msglevel < client_min_messages &&
863  (msglevel < log_min_messages || log_min_messages == LOG))
864  return;
865 
866  /*
867  * We limit the number of dependencies reported to the client to
868  * MAX_REPORTED_DEPS, since client software may not deal well with
869  * enormous error strings. The server log always gets a full report.
870  */
871 #define MAX_REPORTED_DEPS 100
872 
873  initStringInfo(&clientdetail);
874  initStringInfo(&logdetail);
875 
876  /*
877  * We process the list back to front (ie, in dependency order not deletion
878  * order), since this makes for a more understandable display.
879  */
880  for (i = targetObjects->numrefs - 1; i >= 0; i--)
881  {
882  const ObjectAddress *obj = &targetObjects->refs[i];
883  const ObjectAddressExtra *extra = &targetObjects->extras[i];
884  char *objDesc;
885 
886  /* Ignore the original deletion target(s) */
887  if (extra->flags & DEPFLAG_ORIGINAL)
888  continue;
889 
890  objDesc = getObjectDescription(obj);
891 
892  /*
893  * If, at any stage of the recursive search, we reached the object via
894  * an AUTO, INTERNAL, or EXTENSION dependency, then it's okay to
895  * delete it even in RESTRICT mode.
896  */
897  if (extra->flags & (DEPFLAG_AUTO |
900  {
901  /*
902  * auto-cascades are reported at DEBUG2, not msglevel. We don't
903  * try to combine them with the regular message because the
904  * results are too confusing when client_min_messages and
905  * log_min_messages are different.
906  */
907  ereport(DEBUG2,
908  (errmsg("drop auto-cascades to %s",
909  objDesc)));
910  }
911  else if (behavior == DROP_RESTRICT)
912  {
913  char *otherDesc = getObjectDescription(&extra->dependee);
914 
915  if (numReportedClient < MAX_REPORTED_DEPS)
916  {
917  /* separate entries with a newline */
918  if (clientdetail.len != 0)
919  appendStringInfoChar(&clientdetail, '\n');
920  appendStringInfo(&clientdetail, _("%s depends on %s"),
921  objDesc, otherDesc);
922  numReportedClient++;
923  }
924  else
925  numNotReportedClient++;
926  /* separate entries with a newline */
927  if (logdetail.len != 0)
928  appendStringInfoChar(&logdetail, '\n');
929  appendStringInfo(&logdetail, _("%s depends on %s"),
930  objDesc, otherDesc);
931  pfree(otherDesc);
932  ok = false;
933  }
934  else
935  {
936  if (numReportedClient < MAX_REPORTED_DEPS)
937  {
938  /* separate entries with a newline */
939  if (clientdetail.len != 0)
940  appendStringInfoChar(&clientdetail, '\n');
941  appendStringInfo(&clientdetail, _("drop cascades to %s"),
942  objDesc);
943  numReportedClient++;
944  }
945  else
946  numNotReportedClient++;
947  /* separate entries with a newline */
948  if (logdetail.len != 0)
949  appendStringInfoChar(&logdetail, '\n');
950  appendStringInfo(&logdetail, _("drop cascades to %s"),
951  objDesc);
952  }
953 
954  pfree(objDesc);
955  }
956 
957  if (numNotReportedClient > 0)
958  appendStringInfo(&clientdetail, ngettext("\nand %d other object "
959  "(see server log for list)",
960  "\nand %d other objects "
961  "(see server log for list)",
962  numNotReportedClient),
963  numNotReportedClient);
964 
965  if (!ok)
966  {
967  if (origObject)
968  ereport(ERROR,
969  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
970  errmsg("cannot drop %s because other objects depend on it",
971  getObjectDescription(origObject)),
972  errdetail("%s", clientdetail.data),
973  errdetail_log("%s", logdetail.data),
974  errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
975  else
976  ereport(ERROR,
977  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
978  errmsg("cannot drop desired object(s) because other objects depend on them"),
979  errdetail("%s", clientdetail.data),
980  errdetail_log("%s", logdetail.data),
981  errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
982  }
983  else if (numReportedClient > 1)
984  {
985  ereport(msglevel,
986  /* translator: %d always has a value larger than 1 */
987  (errmsg_plural("drop cascades to %d other object",
988  "drop cascades to %d other objects",
989  numReportedClient + numNotReportedClient,
990  numReportedClient + numNotReportedClient),
991  errdetail("%s", clientdetail.data),
992  errdetail_log("%s", logdetail.data)));
993  }
994  else if (numReportedClient == 1)
995  {
996  /* we just use the single item as-is */
997  ereport(msglevel,
998  (errmsg_internal("%s", clientdetail.data)));
999  }
1000 
1001  pfree(clientdetail.data);
1002  pfree(logdetail.data);
1003 }
1004 
1005 /*
1006  * deleteOneObject: delete a single object for performDeletion.
1007  *
1008  * *depRel is the already-open pg_depend relation.
1009  */
1010 static void
1011 deleteOneObject(const ObjectAddress *object, Relation *depRel, int flags)
1012 {
1013  ScanKeyData key[3];
1014  int nkeys;
1015  SysScanDesc scan;
1016  HeapTuple tup;
1017 
1018  /* DROP hook of the objects being removed */
1019  InvokeObjectDropHookArg(object->classId, object->objectId,
1020  object->objectSubId, flags);
1021 
1022  /*
1023  * Close depRel if we are doing a drop concurrently. The object deletion
1024  * subroutine will commit the current transaction, so we can't keep the
1025  * relation open across doDeletion().
1026  */
1027  if (flags & PERFORM_DELETION_CONCURRENTLY)
1028  heap_close(*depRel, RowExclusiveLock);
1029 
1030  /*
1031  * Delete the object itself, in an object-type-dependent way.
1032  *
1033  * We used to do this after removing the outgoing dependency links, but it
1034  * seems just as reasonable to do it beforehand. In the concurrent case
1035  * we *must* do it in this order, because we can't make any transactional
1036  * updates before calling doDeletion() --- they'd get committed right
1037  * away, which is not cool if the deletion then fails.
1038  */
1039  doDeletion(object, flags);
1040 
1041  /*
1042  * Reopen depRel if we closed it above
1043  */
1044  if (flags & PERFORM_DELETION_CONCURRENTLY)
1046 
1047  /*
1048  * Now remove any pg_depend records that link from this object to others.
1049  * (Any records linking to this object should be gone already.)
1050  *
1051  * When dropping a whole object (subId = 0), remove all pg_depend records
1052  * for its sub-objects too.
1053  */
1054  ScanKeyInit(&key[0],
1056  BTEqualStrategyNumber, F_OIDEQ,
1057  ObjectIdGetDatum(object->classId));
1058  ScanKeyInit(&key[1],
1060  BTEqualStrategyNumber, F_OIDEQ,
1061  ObjectIdGetDatum(object->objectId));
1062  if (object->objectSubId != 0)
1063  {
1064  ScanKeyInit(&key[2],
1066  BTEqualStrategyNumber, F_INT4EQ,
1067  Int32GetDatum(object->objectSubId));
1068  nkeys = 3;
1069  }
1070  else
1071  nkeys = 2;
1072 
1073  scan = systable_beginscan(*depRel, DependDependerIndexId, true,
1074  NULL, nkeys, key);
1075 
1076  while (HeapTupleIsValid(tup = systable_getnext(scan)))
1077  {
1078  CatalogTupleDelete(*depRel, &tup->t_self);
1079  }
1080 
1081  systable_endscan(scan);
1082 
1083  /*
1084  * Delete shared dependency references related to this object. Again, if
1085  * subId = 0, remove records for sub-objects too.
1086  */
1088  object->objectSubId);
1089 
1090 
1091  /*
1092  * Delete any comments, security labels, or initial privileges associated
1093  * with this object. (This is a convenient place to do these things,
1094  * rather than having every object type know to do it.)
1095  */
1096  DeleteComments(object->objectId, object->classId, object->objectSubId);
1097  DeleteSecurityLabel(object);
1098  DeleteInitPrivs(object);
1099 
1100  /*
1101  * CommandCounterIncrement here to ensure that preceding changes are all
1102  * visible to the next deletion step.
1103  */
1105 
1106  /*
1107  * And we're done!
1108  */
1109 }
1110 
1111 /*
1112  * doDeletion: actually delete a single object
1113  */
1114 static void
1115 doDeletion(const ObjectAddress *object, int flags)
1116 {
1117  switch (getObjectClass(object))
1118  {
1119  case OCLASS_CLASS:
1120  {
1121  char relKind = get_rel_relkind(object->objectId);
1122 
1123  if (relKind == RELKIND_INDEX ||
1124  relKind == RELKIND_PARTITIONED_INDEX)
1125  {
1126  bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
1127 
1128  Assert(object->objectSubId == 0);
1129  index_drop(object->objectId, concurrent);
1130  }
1131  else
1132  {
1133  if (object->objectSubId != 0)
1134  RemoveAttributeById(object->objectId,
1135  object->objectSubId);
1136  else
1138  }
1139 
1140  /*
1141  * for a sequence, in addition to dropping the heap, also
1142  * delete pg_sequence tuple
1143  */
1144  if (relKind == RELKIND_SEQUENCE)
1145  DeleteSequenceTuple(object->objectId);
1146  break;
1147  }
1148 
1149  case OCLASS_PROC:
1150  RemoveFunctionById(object->objectId);
1151  break;
1152 
1153  case OCLASS_TYPE:
1154  RemoveTypeById(object->objectId);
1155  break;
1156 
1157  case OCLASS_CAST:
1158  DropCastById(object->objectId);
1159  break;
1160 
1161  case OCLASS_COLLATION:
1162  RemoveCollationById(object->objectId);
1163  break;
1164 
1165  case OCLASS_CONSTRAINT:
1166  RemoveConstraintById(object->objectId);
1167  break;
1168 
1169  case OCLASS_CONVERSION:
1170  RemoveConversionById(object->objectId);
1171  break;
1172 
1173  case OCLASS_DEFAULT:
1175  break;
1176 
1177  case OCLASS_LANGUAGE:
1179  break;
1180 
1181  case OCLASS_LARGEOBJECT:
1182  LargeObjectDrop(object->objectId);
1183  break;
1184 
1185  case OCLASS_OPERATOR:
1186  RemoveOperatorById(object->objectId);
1187  break;
1188 
1189  case OCLASS_OPCLASS:
1190  RemoveOpClassById(object->objectId);
1191  break;
1192 
1193  case OCLASS_OPFAMILY:
1194  RemoveOpFamilyById(object->objectId);
1195  break;
1196 
1197  case OCLASS_AM:
1199  break;
1200 
1201  case OCLASS_AMOP:
1202  RemoveAmOpEntryById(object->objectId);
1203  break;
1204 
1205  case OCLASS_AMPROC:
1207  break;
1208 
1209  case OCLASS_REWRITE:
1211  break;
1212 
1213  case OCLASS_TRIGGER:
1214  RemoveTriggerById(object->objectId);
1215  break;
1216 
1217  case OCLASS_SCHEMA:
1218  RemoveSchemaById(object->objectId);
1219  break;
1220 
1221  case OCLASS_STATISTIC_EXT:
1222  RemoveStatisticsById(object->objectId);
1223  break;
1224 
1225  case OCLASS_TSPARSER:
1226  RemoveTSParserById(object->objectId);
1227  break;
1228 
1229  case OCLASS_TSDICT:
1231  break;
1232 
1233  case OCLASS_TSTEMPLATE:
1234  RemoveTSTemplateById(object->objectId);
1235  break;
1236 
1237  case OCLASS_TSCONFIG:
1239  break;
1240 
1241  /*
1242  * OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE intentionally not
1243  * handled here
1244  */
1245 
1246  case OCLASS_FDW:
1248  break;
1249 
1250  case OCLASS_FOREIGN_SERVER:
1252  break;
1253 
1254  case OCLASS_USER_MAPPING:
1256  break;
1257 
1258  case OCLASS_DEFACL:
1259  RemoveDefaultACLById(object->objectId);
1260  break;
1261 
1262  case OCLASS_EXTENSION:
1263  RemoveExtensionById(object->objectId);
1264  break;
1265 
1266  case OCLASS_EVENT_TRIGGER:
1268  break;
1269 
1270  case OCLASS_POLICY:
1271  RemovePolicyById(object->objectId);
1272  break;
1273 
1274  case OCLASS_PUBLICATION:
1276  break;
1277 
1280  break;
1281 
1282  case OCLASS_TRANSFORM:
1283  DropTransformById(object->objectId);
1284  break;
1285 
1286  /*
1287  * These global object types are not supported here.
1288  */
1289  case OCLASS_ROLE:
1290  case OCLASS_DATABASE:
1291  case OCLASS_TBLSPACE:
1292  case OCLASS_SUBSCRIPTION:
1293  elog(ERROR, "global objects cannot be deleted by doDeletion");
1294  break;
1295 
1296  /*
1297  * There's intentionally no default: case here; we want the
1298  * compiler to warn if a new OCLASS hasn't been handled above.
1299  */
1300  }
1301 }
1302 
1303 /*
1304  * AcquireDeletionLock - acquire a suitable lock for deleting an object
1305  *
1306  * We use LockRelation for relations, LockDatabaseObject for everything
1307  * else. Note that dependency.c is not concerned with deleting any kind of
1308  * shared-across-databases object, so we have no need for LockSharedObject.
1309  */
1310 static void
1312 {
1313  if (object->classId == RelationRelationId)
1314  {
1315  /*
1316  * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1317  * the index for the moment. index_drop() will promote the lock once
1318  * it's safe to do so. In all other cases we need full exclusive
1319  * lock.
1320  */
1321  if (flags & PERFORM_DELETION_CONCURRENTLY)
1323  else
1325  }
1326  else
1327  {
1328  /* assume we should lock the whole object not a sub-object */
1329  LockDatabaseObject(object->classId, object->objectId, 0,
1331  }
1332 }
1333 
1334 /*
1335  * ReleaseDeletionLock - release an object deletion lock
1336  */
1337 static void
1339 {
1340  if (object->classId == RelationRelationId)
1342  else
1343  /* assume we should lock the whole object not a sub-object */
1344  UnlockDatabaseObject(object->classId, object->objectId, 0,
1346 }
1347 
1348 /*
1349  * recordDependencyOnExpr - find expression dependencies
1350  *
1351  * This is used to find the dependencies of rules, constraint expressions,
1352  * etc.
1353  *
1354  * Given an expression or query in node-tree form, find all the objects
1355  * it refers to (tables, columns, operators, functions, etc). Record
1356  * a dependency of the specified type from the given depender object
1357  * to each object mentioned in the expression.
1358  *
1359  * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
1360  * It can be NIL if no such variables are expected.
1361  */
1362 void
1364  Node *expr, List *rtable,
1365  DependencyType behavior)
1366 {
1368 
1369  context.addrs = new_object_addresses();
1370 
1371  /* Set up interpretation for Vars at varlevelsup = 0 */
1372  context.rtables = list_make1(rtable);
1373 
1374  /* Scan the expression tree for referenceable objects */
1375  find_expr_references_walker(expr, &context);
1376 
1377  /* Remove any duplicates */
1379 
1380  /* And record 'em */
1381  recordMultipleDependencies(depender,
1382  context.addrs->refs, context.addrs->numrefs,
1383  behavior);
1384 
1385  free_object_addresses(context.addrs);
1386 }
1387 
1388 /*
1389  * recordDependencyOnSingleRelExpr - find expression dependencies
1390  *
1391  * As above, but only one relation is expected to be referenced (with
1392  * varno = 1 and varlevelsup = 0). Pass the relation OID instead of a
1393  * range table. An additional frammish is that dependencies on that
1394  * relation (or its component columns) will be marked with 'self_behavior',
1395  * whereas 'behavior' is used for everything else.
1396  *
1397  * NOTE: the caller should ensure that a whole-table dependency on the
1398  * specified relation is created separately, if one is needed. In particular,
1399  * a whole-row Var "relation.*" will not cause this routine to emit any
1400  * dependency item. This is appropriate behavior for subexpressions of an
1401  * ordinary query, so other cases need to cope as necessary.
1402  */
1403 void
1405  Node *expr, Oid relId,
1406  DependencyType behavior,
1407  DependencyType self_behavior,
1408  bool ignore_self)
1409 {
1411  RangeTblEntry rte;
1412 
1413  context.addrs = new_object_addresses();
1414 
1415  /* We gin up a rather bogus rangetable list to handle Vars */
1416  MemSet(&rte, 0, sizeof(rte));
1417  rte.type = T_RangeTblEntry;
1418  rte.rtekind = RTE_RELATION;
1419  rte.relid = relId;
1420  rte.relkind = RELKIND_RELATION; /* no need for exactness here */
1421 
1422  context.rtables = list_make1(list_make1(&rte));
1423 
1424  /* Scan the expression tree for referenceable objects */
1425  find_expr_references_walker(expr, &context);
1426 
1427  /* Remove any duplicates */
1429 
1430  /* Separate self-dependencies if necessary */
1431  if (behavior != self_behavior && context.addrs->numrefs > 0)
1432  {
1433  ObjectAddresses *self_addrs;
1434  ObjectAddress *outobj;
1435  int oldref,
1436  outrefs;
1437 
1438  self_addrs = new_object_addresses();
1439 
1440  outobj = context.addrs->refs;
1441  outrefs = 0;
1442  for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1443  {
1444  ObjectAddress *thisobj = context.addrs->refs + oldref;
1445 
1446  if (thisobj->classId == RelationRelationId &&
1447  thisobj->objectId == relId)
1448  {
1449  /* Move this ref into self_addrs */
1450  add_exact_object_address(thisobj, self_addrs);
1451  }
1452  else
1453  {
1454  /* Keep it in context.addrs */
1455  *outobj = *thisobj;
1456  outobj++;
1457  outrefs++;
1458  }
1459  }
1460  context.addrs->numrefs = outrefs;
1461 
1462  /* Record the self-dependencies */
1463  if (!ignore_self)
1464  recordMultipleDependencies(depender,
1465  self_addrs->refs, self_addrs->numrefs,
1466  self_behavior);
1467 
1468  free_object_addresses(self_addrs);
1469  }
1470 
1471  /* Record the external dependencies */
1472  recordMultipleDependencies(depender,
1473  context.addrs->refs, context.addrs->numrefs,
1474  behavior);
1475 
1476  free_object_addresses(context.addrs);
1477 }
1478 
1479 /*
1480  * Recursively search an expression tree for object references.
1481  *
1482  * Note: we avoid creating references to columns of tables that participate
1483  * in an SQL JOIN construct, but are not actually used anywhere in the query.
1484  * To do so, we do not scan the joinaliasvars list of a join RTE while
1485  * scanning the query rangetable, but instead scan each individual entry
1486  * of the alias list when we find a reference to it.
1487  *
1488  * Note: in many cases we do not need to create dependencies on the datatypes
1489  * involved in an expression, because we'll have an indirect dependency via
1490  * some other object. For instance Var nodes depend on a column which depends
1491  * on the datatype, and OpExpr nodes depend on the operator which depends on
1492  * the datatype. However we do need a type dependency if there is no such
1493  * indirect dependency, as for example in Const and CoerceToDomain nodes.
1494  *
1495  * Similarly, we don't need to create dependencies on collations except where
1496  * the collation is being freshly introduced to the expression.
1497  */
1498 static bool
1501 {
1502  if (node == NULL)
1503  return false;
1504  if (IsA(node, Var))
1505  {
1506  Var *var = (Var *) node;
1507  List *rtable;
1508  RangeTblEntry *rte;
1509 
1510  /* Find matching rtable entry, or complain if not found */
1511  if (var->varlevelsup >= list_length(context->rtables))
1512  elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
1513  rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1514  if (var->varno <= 0 || var->varno > list_length(rtable))
1515  elog(ERROR, "invalid varno %d", var->varno);
1516  rte = rt_fetch(var->varno, rtable);
1517 
1518  /*
1519  * A whole-row Var references no specific columns, so adds no new
1520  * dependency. (We assume that there is a whole-table dependency
1521  * arising from each underlying rangetable entry. While we could
1522  * record such a dependency when finding a whole-row Var that
1523  * references a relation directly, it's quite unclear how to extend
1524  * that to whole-row Vars for JOINs, so it seems better to leave the
1525  * responsibility with the range table. Note that this poses some
1526  * risks for identifying dependencies of stand-alone expressions:
1527  * whole-table references may need to be created separately.)
1528  */
1529  if (var->varattno == InvalidAttrNumber)
1530  return false;
1531  if (rte->rtekind == RTE_RELATION)
1532  {
1533  /* If it's a plain relation, reference this column */
1535  context->addrs);
1536  }
1537  else if (rte->rtekind == RTE_JOIN)
1538  {
1539  /* Scan join output column to add references to join inputs */
1540  List *save_rtables;
1541 
1542  /* We must make the context appropriate for join's level */
1543  save_rtables = context->rtables;
1544  context->rtables = list_copy_tail(context->rtables,
1545  var->varlevelsup);
1546  if (var->varattno <= 0 ||
1547  var->varattno > list_length(rte->joinaliasvars))
1548  elog(ERROR, "invalid varattno %d", var->varattno);
1550  var->varattno - 1),
1551  context);
1552  list_free(context->rtables);
1553  context->rtables = save_rtables;
1554  }
1555  return false;
1556  }
1557  else if (IsA(node, Const))
1558  {
1559  Const *con = (Const *) node;
1560  Oid objoid;
1561 
1562  /* A constant must depend on the constant's datatype */
1564  context->addrs);
1565 
1566  /*
1567  * We must also depend on the constant's collation: it could be
1568  * different from the datatype's, if a CollateExpr was const-folded to
1569  * a simple constant. However we can save work in the most common
1570  * case where the collation is "default", since we know that's pinned.
1571  */
1572  if (OidIsValid(con->constcollid) &&
1575  context->addrs);
1576 
1577  /*
1578  * If it's a regclass or similar literal referring to an existing
1579  * object, add a reference to that object. (Currently, only the
1580  * regclass and regconfig cases have any likely use, but we may as
1581  * well handle all the OID-alias datatypes consistently.)
1582  */
1583  if (!con->constisnull)
1584  {
1585  switch (con->consttype)
1586  {
1587  case REGPROCOID:
1588  case REGPROCEDUREOID:
1589  objoid = DatumGetObjectId(con->constvalue);
1591  ObjectIdGetDatum(objoid)))
1592  add_object_address(OCLASS_PROC, objoid, 0,
1593  context->addrs);
1594  break;
1595  case REGOPEROID:
1596  case REGOPERATOROID:
1597  objoid = DatumGetObjectId(con->constvalue);
1599  ObjectIdGetDatum(objoid)))
1601  context->addrs);
1602  break;
1603  case REGCLASSOID:
1604  objoid = DatumGetObjectId(con->constvalue);
1606  ObjectIdGetDatum(objoid)))
1607  add_object_address(OCLASS_CLASS, objoid, 0,
1608  context->addrs);
1609  break;
1610  case REGTYPEOID:
1611  objoid = DatumGetObjectId(con->constvalue);
1613  ObjectIdGetDatum(objoid)))
1614  add_object_address(OCLASS_TYPE, objoid, 0,
1615  context->addrs);
1616  break;
1617  case REGCONFIGOID:
1618  objoid = DatumGetObjectId(con->constvalue);
1620  ObjectIdGetDatum(objoid)))
1622  context->addrs);
1623  break;
1624  case REGDICTIONARYOID:
1625  objoid = DatumGetObjectId(con->constvalue);
1627  ObjectIdGetDatum(objoid)))
1628  add_object_address(OCLASS_TSDICT, objoid, 0,
1629  context->addrs);
1630  break;
1631 
1632  case REGNAMESPACEOID:
1633  objoid = DatumGetObjectId(con->constvalue);
1635  ObjectIdGetDatum(objoid)))
1636  add_object_address(OCLASS_SCHEMA, objoid, 0,
1637  context->addrs);
1638  break;
1639 
1640  /*
1641  * Dependencies for regrole should be shared among all
1642  * databases, so explicitly inhibit to have dependencies.
1643  */
1644  case REGROLEOID:
1645  ereport(ERROR,
1646  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1647  errmsg("constant of the type %s cannot be used here",
1648  "regrole")));
1649  break;
1650  }
1651  }
1652  return false;
1653  }
1654  else if (IsA(node, Param))
1655  {
1656  Param *param = (Param *) node;
1657 
1658  /* A parameter must depend on the parameter's datatype */
1660  context->addrs);
1661  /* and its collation, just as for Consts */
1662  if (OidIsValid(param->paramcollid) &&
1665  context->addrs);
1666  }
1667  else if (IsA(node, FuncExpr))
1668  {
1669  FuncExpr *funcexpr = (FuncExpr *) node;
1670 
1671  add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
1672  context->addrs);
1673  /* fall through to examine arguments */
1674  }
1675  else if (IsA(node, OpExpr))
1676  {
1677  OpExpr *opexpr = (OpExpr *) node;
1678 
1680  context->addrs);
1681  /* fall through to examine arguments */
1682  }
1683  else if (IsA(node, DistinctExpr))
1684  {
1685  DistinctExpr *distinctexpr = (DistinctExpr *) node;
1686 
1687  add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
1688  context->addrs);
1689  /* fall through to examine arguments */
1690  }
1691  else if (IsA(node, NullIfExpr))
1692  {
1693  NullIfExpr *nullifexpr = (NullIfExpr *) node;
1694 
1695  add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
1696  context->addrs);
1697  /* fall through to examine arguments */
1698  }
1699  else if (IsA(node, ScalarArrayOpExpr))
1700  {
1701  ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1702 
1704  context->addrs);
1705  /* fall through to examine arguments */
1706  }
1707  else if (IsA(node, Aggref))
1708  {
1709  Aggref *aggref = (Aggref *) node;
1710 
1712  context->addrs);
1713  /* fall through to examine arguments */
1714  }
1715  else if (IsA(node, WindowFunc))
1716  {
1717  WindowFunc *wfunc = (WindowFunc *) node;
1718 
1720  context->addrs);
1721  /* fall through to examine arguments */
1722  }
1723  else if (IsA(node, SubPlan))
1724  {
1725  /* Extra work needed here if we ever need this case */
1726  elog(ERROR, "already-planned subqueries not supported");
1727  }
1728  else if (IsA(node, FieldSelect))
1729  {
1730  FieldSelect *fselect = (FieldSelect *) node;
1731  Oid argtype = getBaseType(exprType((Node *) fselect->arg));
1732  Oid reltype = get_typ_typrelid(argtype);
1733 
1734  /*
1735  * We need a dependency on the specific column named in FieldSelect,
1736  * assuming we can identify the pg_class OID for it. (Probably we
1737  * always can at the moment, but in future it might be possible for
1738  * argtype to be RECORDOID.) If we can make a column dependency then
1739  * we shouldn't need a dependency on the column's type; but if we
1740  * can't, make a dependency on the type, as it might not appear
1741  * anywhere else in the expression.
1742  */
1743  if (OidIsValid(reltype))
1744  add_object_address(OCLASS_CLASS, reltype, fselect->fieldnum,
1745  context->addrs);
1746  else
1748  context->addrs);
1749  /* the collation might not be referenced anywhere else, either */
1750  if (OidIsValid(fselect->resultcollid) &&
1751  fselect->resultcollid != DEFAULT_COLLATION_OID)
1753  context->addrs);
1754  }
1755  else if (IsA(node, FieldStore))
1756  {
1757  FieldStore *fstore = (FieldStore *) node;
1758  Oid reltype = get_typ_typrelid(fstore->resulttype);
1759 
1760  /* similar considerations to FieldSelect, but multiple column(s) */
1761  if (OidIsValid(reltype))
1762  {
1763  ListCell *l;
1764 
1765  foreach(l, fstore->fieldnums)
1767  context->addrs);
1768  }
1769  else
1771  context->addrs);
1772  }
1773  else if (IsA(node, RelabelType))
1774  {
1775  RelabelType *relab = (RelabelType *) node;
1776 
1777  /* since there is no function dependency, need to depend on type */
1779  context->addrs);
1780  /* the collation might not be referenced anywhere else, either */
1781  if (OidIsValid(relab->resultcollid) &&
1784  context->addrs);
1785  }
1786  else if (IsA(node, CoerceViaIO))
1787  {
1788  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1789 
1790  /* since there is no exposed function, need to depend on type */
1792  context->addrs);
1793  /* the collation might not be referenced anywhere else, either */
1794  if (OidIsValid(iocoerce->resultcollid) &&
1795  iocoerce->resultcollid != DEFAULT_COLLATION_OID)
1797  context->addrs);
1798  }
1799  else if (IsA(node, ArrayCoerceExpr))
1800  {
1801  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1802 
1803  /* as above, depend on type */
1805  context->addrs);
1806  /* the collation might not be referenced anywhere else, either */
1807  if (OidIsValid(acoerce->resultcollid) &&
1808  acoerce->resultcollid != DEFAULT_COLLATION_OID)
1810  context->addrs);
1811  /* fall through to examine arguments */
1812  }
1813  else if (IsA(node, ConvertRowtypeExpr))
1814  {
1815  ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
1816 
1817  /* since there is no function dependency, need to depend on type */
1819  context->addrs);
1820  }
1821  else if (IsA(node, CollateExpr))
1822  {
1823  CollateExpr *coll = (CollateExpr *) node;
1824 
1826  context->addrs);
1827  }
1828  else if (IsA(node, RowExpr))
1829  {
1830  RowExpr *rowexpr = (RowExpr *) node;
1831 
1833  context->addrs);
1834  }
1835  else if (IsA(node, RowCompareExpr))
1836  {
1837  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1838  ListCell *l;
1839 
1840  foreach(l, rcexpr->opnos)
1841  {
1843  context->addrs);
1844  }
1845  foreach(l, rcexpr->opfamilies)
1846  {
1848  context->addrs);
1849  }
1850  /* fall through to examine arguments */
1851  }
1852  else if (IsA(node, CoerceToDomain))
1853  {
1854  CoerceToDomain *cd = (CoerceToDomain *) node;
1855 
1857  context->addrs);
1858  }
1859  else if (IsA(node, NextValueExpr))
1860  {
1861  NextValueExpr *nve = (NextValueExpr *) node;
1862 
1864  context->addrs);
1865  }
1866  else if (IsA(node, OnConflictExpr))
1867  {
1868  OnConflictExpr *onconflict = (OnConflictExpr *) node;
1869 
1870  if (OidIsValid(onconflict->constraint))
1872  context->addrs);
1873  /* fall through to examine arguments */
1874  }
1875  else if (IsA(node, SortGroupClause))
1876  {
1877  SortGroupClause *sgc = (SortGroupClause *) node;
1878 
1880  context->addrs);
1881  if (OidIsValid(sgc->sortop))
1883  context->addrs);
1884  return false;
1885  }
1886  else if (IsA(node, WindowClause))
1887  {
1888  WindowClause *wc = (WindowClause *) node;
1889 
1890  if (OidIsValid(wc->startInRangeFunc))
1892  context->addrs);
1893  if (OidIsValid(wc->endInRangeFunc))
1895  context->addrs);
1896  if (OidIsValid(wc->inRangeColl) &&
1899  context->addrs);
1900  /* fall through to examine substructure */
1901  }
1902  else if (IsA(node, Query))
1903  {
1904  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1905  Query *query = (Query *) node;
1906  ListCell *lc;
1907  bool result;
1908 
1909  /*
1910  * Add whole-relation refs for each plain relation mentioned in the
1911  * subquery's rtable.
1912  *
1913  * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
1914  * RTEs, subqueries, etc, so no need to do that here. But keep it
1915  * from looking at join alias lists.
1916  *
1917  * Note: we don't need to worry about collations mentioned in
1918  * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
1919  * collations referenced in other parts of the Query. We do have to
1920  * worry about collations mentioned in RTE_FUNCTION, but we take care
1921  * of those when we recurse to the RangeTblFunction node(s).
1922  */
1923  foreach(lc, query->rtable)
1924  {
1925  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
1926 
1927  switch (rte->rtekind)
1928  {
1929  case RTE_RELATION:
1931  context->addrs);
1932  break;
1933  default:
1934  break;
1935  }
1936  }
1937 
1938  /*
1939  * If the query is an INSERT or UPDATE, we should create a dependency
1940  * on each target column, to prevent the specific target column from
1941  * being dropped. Although we will visit the TargetEntry nodes again
1942  * during query_tree_walker, we won't have enough context to do this
1943  * conveniently, so do it here.
1944  */
1945  if (query->commandType == CMD_INSERT ||
1946  query->commandType == CMD_UPDATE)
1947  {
1948  RangeTblEntry *rte;
1949 
1950  if (query->resultRelation <= 0 ||
1951  query->resultRelation > list_length(query->rtable))
1952  elog(ERROR, "invalid resultRelation %d",
1953  query->resultRelation);
1954  rte = rt_fetch(query->resultRelation, query->rtable);
1955  if (rte->rtekind == RTE_RELATION)
1956  {
1957  foreach(lc, query->targetList)
1958  {
1959  TargetEntry *tle = (TargetEntry *) lfirst(lc);
1960 
1961  if (tle->resjunk)
1962  continue; /* ignore junk tlist items */
1964  context->addrs);
1965  }
1966  }
1967  }
1968 
1969  /*
1970  * Add dependencies on constraints listed in query's constraintDeps
1971  */
1972  foreach(lc, query->constraintDeps)
1973  {
1975  context->addrs);
1976  }
1977 
1978  /* query_tree_walker ignores ORDER BY etc, but we need those opers */
1979  find_expr_references_walker((Node *) query->sortClause, context);
1980  find_expr_references_walker((Node *) query->groupClause, context);
1981  find_expr_references_walker((Node *) query->windowClause, context);
1982  find_expr_references_walker((Node *) query->distinctClause, context);
1983 
1984  /* Examine substructure of query */
1985  context->rtables = lcons(query->rtable, context->rtables);
1986  result = query_tree_walker(query,
1988  (void *) context,
1990  context->rtables = list_delete_first(context->rtables);
1991  return result;
1992  }
1993  else if (IsA(node, SetOperationStmt))
1994  {
1995  SetOperationStmt *setop = (SetOperationStmt *) node;
1996 
1997  /* we need to look at the groupClauses for operator references */
1998  find_expr_references_walker((Node *) setop->groupClauses, context);
1999  /* fall through to examine child nodes */
2000  }
2001  else if (IsA(node, RangeTblFunction))
2002  {
2003  RangeTblFunction *rtfunc = (RangeTblFunction *) node;
2004  ListCell *ct;
2005 
2006  /*
2007  * Add refs for any datatypes and collations used in a column
2008  * definition list for a RECORD function. (For other cases, it should
2009  * be enough to depend on the function itself.)
2010  */
2011  foreach(ct, rtfunc->funccoltypes)
2012  {
2014  context->addrs);
2015  }
2016  foreach(ct, rtfunc->funccolcollations)
2017  {
2018  Oid collid = lfirst_oid(ct);
2019 
2020  if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
2022  context->addrs);
2023  }
2024  }
2025  else if (IsA(node, TableSampleClause))
2026  {
2027  TableSampleClause *tsc = (TableSampleClause *) node;
2028 
2030  context->addrs);
2031  /* fall through to examine arguments */
2032  }
2033 
2035  (void *) context);
2036 }
2037 
2038 /*
2039  * Given an array of dependency references, eliminate any duplicates.
2040  */
2041 static void
2043 {
2044  ObjectAddress *priorobj;
2045  int oldref,
2046  newrefs;
2047 
2048  /*
2049  * We can't sort if the array has "extra" data, because there's no way to
2050  * keep it in sync. Fortunately that combination of features is not
2051  * needed.
2052  */
2053  Assert(!addrs->extras);
2054 
2055  if (addrs->numrefs <= 1)
2056  return; /* nothing to do */
2057 
2058  /* Sort the refs so that duplicates are adjacent */
2059  qsort((void *) addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
2061 
2062  /* Remove dups */
2063  priorobj = addrs->refs;
2064  newrefs = 1;
2065  for (oldref = 1; oldref < addrs->numrefs; oldref++)
2066  {
2067  ObjectAddress *thisobj = addrs->refs + oldref;
2068 
2069  if (priorobj->classId == thisobj->classId &&
2070  priorobj->objectId == thisobj->objectId)
2071  {
2072  if (priorobj->objectSubId == thisobj->objectSubId)
2073  continue; /* identical, so drop thisobj */
2074 
2075  /*
2076  * If we have a whole-object reference and a reference to a part
2077  * of the same object, we don't need the whole-object reference
2078  * (for example, we don't need to reference both table foo and
2079  * column foo.bar). The whole-object reference will always appear
2080  * first in the sorted list.
2081  */
2082  if (priorobj->objectSubId == 0)
2083  {
2084  /* replace whole ref with partial */
2085  priorobj->objectSubId = thisobj->objectSubId;
2086  continue;
2087  }
2088  }
2089  /* Not identical, so add thisobj to output set */
2090  priorobj++;
2091  *priorobj = *thisobj;
2092  newrefs++;
2093  }
2094 
2095  addrs->numrefs = newrefs;
2096 }
2097 
2098 /*
2099  * qsort comparator for ObjectAddress items
2100  */
2101 static int
2102 object_address_comparator(const void *a, const void *b)
2103 {
2104  const ObjectAddress *obja = (const ObjectAddress *) a;
2105  const ObjectAddress *objb = (const ObjectAddress *) b;
2106 
2107  if (obja->classId < objb->classId)
2108  return -1;
2109  if (obja->classId > objb->classId)
2110  return 1;
2111  if (obja->objectId < objb->objectId)
2112  return -1;
2113  if (obja->objectId > objb->objectId)
2114  return 1;
2115 
2116  /*
2117  * We sort the subId as an unsigned int so that 0 will come first. See
2118  * logic in eliminate_duplicate_dependencies.
2119  */
2120  if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2121  return -1;
2122  if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2123  return 1;
2124  return 0;
2125 }
2126 
2127 /*
2128  * Routines for handling an expansible array of ObjectAddress items.
2129  *
2130  * new_object_addresses: create a new ObjectAddresses array.
2131  */
2134 {
2135  ObjectAddresses *addrs;
2136 
2137  addrs = palloc(sizeof(ObjectAddresses));
2138 
2139  addrs->numrefs = 0;
2140  addrs->maxrefs = 32;
2141  addrs->refs = (ObjectAddress *)
2142  palloc(addrs->maxrefs * sizeof(ObjectAddress));
2143  addrs->extras = NULL; /* until/unless needed */
2144 
2145  return addrs;
2146 }
2147 
2148 /*
2149  * Add an entry to an ObjectAddresses array.
2150  *
2151  * It is convenient to specify the class by ObjectClass rather than directly
2152  * by catalog OID.
2153  */
2154 static void
2155 add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
2156  ObjectAddresses *addrs)
2157 {
2158  ObjectAddress *item;
2159 
2160  /*
2161  * Make sure object_classes is kept up to date with the ObjectClass enum.
2162  */
2164  "object_classes[] must cover all ObjectClasses");
2165 
2166  /* enlarge array if needed */
2167  if (addrs->numrefs >= addrs->maxrefs)
2168  {
2169  addrs->maxrefs *= 2;
2170  addrs->refs = (ObjectAddress *)
2171  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2172  Assert(!addrs->extras);
2173  }
2174  /* record this item */
2175  item = addrs->refs + addrs->numrefs;
2176  item->classId = object_classes[oclass];
2177  item->objectId = objectId;
2178  item->objectSubId = subId;
2179  addrs->numrefs++;
2180 }
2181 
2182 /*
2183  * Add an entry to an ObjectAddresses array.
2184  *
2185  * As above, but specify entry exactly.
2186  */
2187 void
2189  ObjectAddresses *addrs)
2190 {
2191  ObjectAddress *item;
2192 
2193  /* enlarge array if needed */
2194  if (addrs->numrefs >= addrs->maxrefs)
2195  {
2196  addrs->maxrefs *= 2;
2197  addrs->refs = (ObjectAddress *)
2198  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2199  Assert(!addrs->extras);
2200  }
2201  /* record this item */
2202  item = addrs->refs + addrs->numrefs;
2203  *item = *object;
2204  addrs->numrefs++;
2205 }
2206 
2207 /*
2208  * Add an entry to an ObjectAddresses array.
2209  *
2210  * As above, but specify entry exactly and provide some "extra" data too.
2211  */
2212 static void
2214  const ObjectAddressExtra *extra,
2215  ObjectAddresses *addrs)
2216 {
2217  ObjectAddress *item;
2218  ObjectAddressExtra *itemextra;
2219 
2220  /* allocate extra space if first time */
2221  if (!addrs->extras)
2222  addrs->extras = (ObjectAddressExtra *)
2223  palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
2224 
2225  /* enlarge array if needed */
2226  if (addrs->numrefs >= addrs->maxrefs)
2227  {
2228  addrs->maxrefs *= 2;
2229  addrs->refs = (ObjectAddress *)
2230  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2231  addrs->extras = (ObjectAddressExtra *)
2232  repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
2233  }
2234  /* record this item */
2235  item = addrs->refs + addrs->numrefs;
2236  *item = *object;
2237  itemextra = addrs->extras + addrs->numrefs;
2238  *itemextra = *extra;
2239  addrs->numrefs++;
2240 }
2241 
2242 /*
2243  * Test whether an object is present in an ObjectAddresses array.
2244  *
2245  * We return "true" if object is a subobject of something in the array, too.
2246  */
2247 bool
2249  const ObjectAddresses *addrs)
2250 {
2251  int i;
2252 
2253  for (i = addrs->numrefs - 1; i >= 0; i--)
2254  {
2255  const ObjectAddress *thisobj = addrs->refs + i;
2256 
2257  if (object->classId == thisobj->classId &&
2258  object->objectId == thisobj->objectId)
2259  {
2260  if (object->objectSubId == thisobj->objectSubId ||
2261  thisobj->objectSubId == 0)
2262  return true;
2263  }
2264  }
2265 
2266  return false;
2267 }
2268 
2269 /*
2270  * As above, except that if the object is present then also OR the given
2271  * flags into its associated extra data (which must exist).
2272  */
2273 static bool
2275  int flags,
2276  ObjectAddresses *addrs)
2277 {
2278  bool result = false;
2279  int i;
2280 
2281  for (i = addrs->numrefs - 1; i >= 0; i--)
2282  {
2283  ObjectAddress *thisobj = addrs->refs + i;
2284 
2285  if (object->classId == thisobj->classId &&
2286  object->objectId == thisobj->objectId)
2287  {
2288  if (object->objectSubId == thisobj->objectSubId)
2289  {
2290  ObjectAddressExtra *thisextra = addrs->extras + i;
2291 
2292  thisextra->flags |= flags;
2293  result = true;
2294  }
2295  else if (thisobj->objectSubId == 0)
2296  {
2297  /*
2298  * We get here if we find a need to delete a column after
2299  * having already decided to drop its whole table. Obviously
2300  * we no longer need to drop the subobject, so report that we
2301  * found the subobject in the array. But don't plaster its
2302  * flags on the whole object.
2303  */
2304  result = true;
2305  }
2306  else if (object->objectSubId == 0)
2307  {
2308  /*
2309  * We get here if we find a need to delete a whole table after
2310  * having already decided to drop one of its columns. We
2311  * can't report that the whole object is in the array, but we
2312  * should mark the subobject with the whole object's flags.
2313  *
2314  * It might seem attractive to physically delete the column's
2315  * array entry, or at least mark it as no longer needing
2316  * separate deletion. But that could lead to, e.g., dropping
2317  * the column's datatype before we drop the table, which does
2318  * not seem like a good idea. This is a very rare situation
2319  * in practice, so we just take the hit of doing a separate
2320  * DROP COLUMN action even though we know we're gonna delete
2321  * the table later.
2322  *
2323  * Because there could be other subobjects of this object in
2324  * the array, this case means we always have to loop through
2325  * the whole array; we cannot exit early on a match.
2326  */
2327  ObjectAddressExtra *thisextra = addrs->extras + i;
2328 
2329  thisextra->flags |= flags;
2330  }
2331  }
2332  }
2333 
2334  return result;
2335 }
2336 
2337 /*
2338  * Similar to above, except we search an ObjectAddressStack.
2339  */
2340 static bool
2342  int flags,
2343  ObjectAddressStack *stack)
2344 {
2345  bool result = false;
2346  ObjectAddressStack *stackptr;
2347 
2348  for (stackptr = stack; stackptr; stackptr = stackptr->next)
2349  {
2350  const ObjectAddress *thisobj = stackptr->object;
2351 
2352  if (object->classId == thisobj->classId &&
2353  object->objectId == thisobj->objectId)
2354  {
2355  if (object->objectSubId == thisobj->objectSubId)
2356  {
2357  stackptr->flags |= flags;
2358  result = true;
2359  }
2360  else if (thisobj->objectSubId == 0)
2361  {
2362  /*
2363  * We're visiting a column with whole table already on stack.
2364  * As in object_address_present_add_flags(), we can skip
2365  * further processing of the subobject, but we don't want to
2366  * propagate flags for the subobject to the whole object.
2367  */
2368  result = true;
2369  }
2370  else if (object->objectSubId == 0)
2371  {
2372  /*
2373  * We're visiting a table with column already on stack. As in
2374  * object_address_present_add_flags(), we should propagate
2375  * flags for the whole object to each of its subobjects.
2376  */
2377  stackptr->flags |= flags;
2378  }
2379  }
2380  }
2381 
2382  return result;
2383 }
2384 
2385 /*
2386  * Record multiple dependencies from an ObjectAddresses array, after first
2387  * removing any duplicates.
2388  */
2389 void
2391  ObjectAddresses *referenced,
2392  DependencyType behavior)
2393 {
2395  recordMultipleDependencies(depender,
2396  referenced->refs, referenced->numrefs,
2397  behavior);
2398 }
2399 
2400 /*
2401  * Clean up when done with an ObjectAddresses array.
2402  */
2403 void
2405 {
2406  pfree(addrs->refs);
2407  if (addrs->extras)
2408  pfree(addrs->extras);
2409  pfree(addrs);
2410 }
2411 
2412 /*
2413  * Determine the class of a given object identified by objectAddress.
2414  *
2415  * This function is essentially the reverse mapping for the object_classes[]
2416  * table. We implement it as a function because the OIDs aren't consecutive.
2417  */
2420 {
2421  /* only pg_class entries can have nonzero objectSubId */
2422  if (object->classId != RelationRelationId &&
2423  object->objectSubId != 0)
2424  elog(ERROR, "invalid non-zero objectSubId for object class %u",
2425  object->classId);
2426 
2427  switch (object->classId)
2428  {
2429  case RelationRelationId:
2430  /* caller must check objectSubId */
2431  return OCLASS_CLASS;
2432 
2433  case ProcedureRelationId:
2434  return OCLASS_PROC;
2435 
2436  case TypeRelationId:
2437  return OCLASS_TYPE;
2438 
2439  case CastRelationId:
2440  return OCLASS_CAST;
2441 
2442  case CollationRelationId:
2443  return OCLASS_COLLATION;
2444 
2445  case ConstraintRelationId:
2446  return OCLASS_CONSTRAINT;
2447 
2448  case ConversionRelationId:
2449  return OCLASS_CONVERSION;
2450 
2451  case AttrDefaultRelationId:
2452  return OCLASS_DEFAULT;
2453 
2454  case LanguageRelationId:
2455  return OCLASS_LANGUAGE;
2456 
2457  case LargeObjectRelationId:
2458  return OCLASS_LARGEOBJECT;
2459 
2460  case OperatorRelationId:
2461  return OCLASS_OPERATOR;
2462 
2464  return OCLASS_OPCLASS;
2465 
2467  return OCLASS_OPFAMILY;
2468 
2470  return OCLASS_AM;
2471 
2473  return OCLASS_AMOP;
2474 
2476  return OCLASS_AMPROC;
2477 
2478  case RewriteRelationId:
2479  return OCLASS_REWRITE;
2480 
2481  case TriggerRelationId:
2482  return OCLASS_TRIGGER;
2483 
2484  case NamespaceRelationId:
2485  return OCLASS_SCHEMA;
2486 
2488  return OCLASS_STATISTIC_EXT;
2489 
2490  case TSParserRelationId:
2491  return OCLASS_TSPARSER;
2492 
2494  return OCLASS_TSDICT;
2495 
2496  case TSTemplateRelationId:
2497  return OCLASS_TSTEMPLATE;
2498 
2499  case TSConfigRelationId:
2500  return OCLASS_TSCONFIG;
2501 
2502  case AuthIdRelationId:
2503  return OCLASS_ROLE;
2504 
2505  case DatabaseRelationId:
2506  return OCLASS_DATABASE;
2507 
2508  case TableSpaceRelationId:
2509  return OCLASS_TBLSPACE;
2510 
2512  return OCLASS_FDW;
2513 
2515  return OCLASS_FOREIGN_SERVER;
2516 
2517  case UserMappingRelationId:
2518  return OCLASS_USER_MAPPING;
2519 
2520  case DefaultAclRelationId:
2521  return OCLASS_DEFACL;
2522 
2523  case ExtensionRelationId:
2524  return OCLASS_EXTENSION;
2525 
2527  return OCLASS_EVENT_TRIGGER;
2528 
2529  case PolicyRelationId:
2530  return OCLASS_POLICY;
2531 
2532  case PublicationRelationId:
2533  return OCLASS_PUBLICATION;
2534 
2536  return OCLASS_PUBLICATION_REL;
2537 
2539  return OCLASS_SUBSCRIPTION;
2540 
2541  case TransformRelationId:
2542  return OCLASS_TRANSFORM;
2543  }
2544 
2545  /* shouldn't get here */
2546  elog(ERROR, "unrecognized object class: %u", object->classId);
2547  return OCLASS_CLASS; /* keep compiler quiet */
2548 }
2549 
2550 /*
2551  * delete initial ACL for extension objects
2552  */
2553 static void
2555 {
2556  Relation relation;
2557  ScanKeyData key[3];
2558  SysScanDesc scan;
2559  HeapTuple oldtuple;
2560 
2562 
2563  ScanKeyInit(&key[0],
2565  BTEqualStrategyNumber, F_OIDEQ,
2566  ObjectIdGetDatum(object->objectId));
2567  ScanKeyInit(&key[1],
2569  BTEqualStrategyNumber, F_OIDEQ,
2570  ObjectIdGetDatum(object->classId));
2571  ScanKeyInit(&key[2],
2573  BTEqualStrategyNumber, F_INT4EQ,
2574  Int32GetDatum(object->objectSubId));
2575 
2576  scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
2577  NULL, 3, key);
2578 
2579  while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
2580  CatalogTupleDelete(relation, &oldtuple->t_self);
2581 
2582  systable_endscan(scan);
2583 
2584  heap_close(relation, RowExclusiveLock);
2585 }
Datum constvalue
Definition: primnodes.h:196
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:462
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1275
Oid CurrentExtensionObject
Definition: extension.c:68
static void ReleaseDeletionLock(const ObjectAddress *object)
Definition: dependency.c:1338
DependencyType
Definition: dependency.h:87
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2245
#define REGCLASSOID
Definition: pg_type.h:577
#define Anum_pg_depend_refobjid
Definition: pg_depend.h:72
#define NamespaceRelationId
Definition: pg_namespace.h:34
static int object_address_comparator(const void *a, const void *b)
Definition: dependency.c:2102
#define IsA(nodeptr, _type_)
Definition: nodes.h:564
#define Anum_pg_depend_refobjsubid
Definition: pg_depend.h:73
#define OperatorRelationId
Definition: pg_operator.h:32
List * joinaliasvars
Definition: parsenodes.h:1003
Index varlevelsup
Definition: primnodes.h:173
int errhint(const char *fmt,...)
Definition: elog.c:987
#define Anum_pg_depend_refclassid
Definition: pg_depend.h:71
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
List * sortClause
Definition: parsenodes.h:156
static void add_exact_object_address_extra(const ObjectAddress *object, const ObjectAddressExtra *extra, ObjectAddresses *addrs)
Definition: dependency.c:2213
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1499
void RemoveStatisticsById(Oid statsOid)
Definition: statscmds.c:351
static void deleteOneObject(const ObjectAddress *object, Relation *depRel, int32 flags)
#define TransformRelationId
Definition: pg_transform.h:25
#define REGROLEOID
Definition: pg_type.h:585
void RemoveOpClassById(Oid opclassOid)
Definition: opclasscmds.c:1603
Oid resulttype
Definition: primnodes.h:744
void DropTransformById(Oid transformOid)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:850
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
bool trackDroppedObjectsNeeded(void)
void RemoveExtensionById(Oid extId)
Definition: extension.c:1826
#define ProcedureRelationId
Definition: pg_proc.h:33
ObjectAddresses * addrs
Definition: dependency.c:130
List * constraintDeps
Definition: parsenodes.h:166
void DropProceduralLanguageById(Oid langOid)
Definition: proclang.c:528
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1805
void RemoveForeignDataWrapperById(Oid fdwId)
Definition: foreigncmds.c:837
#define DatumGetObjectId(X)
Definition: postgres.h:483
#define DatabaseRelationId
Definition: pg_database.h:29
#define RelationRelationId
Definition: pg_class.h:29
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2390
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
Definition: dependency.c:2248
#define DependReferenceIndexId
Definition: indexing.h:147
Oid resulttype
Definition: primnodes.h:815
#define Anum_pg_init_privs_classoid
Definition: pg_init_privs.h:69
List * opfamilies
Definition: primnodes.h:1040
#define DependDependerIndexId
Definition: indexing.h:145
#define PERFORM_DELETION_SKIP_ORIGINAL
Definition: dependency.h:192
#define DependRelationId
Definition: pg_depend.h:29
int resultRelation
Definition: parsenodes.h:120
void DeleteSecurityLabel(const ObjectAddress *object)
Definition: seclabel.c:446
#define LAST_OCLASS
Definition: dependency.h:186
Oid endInRangeFunc
Definition: parsenodes.h:1306
#define OperatorClassRelationId
Definition: pg_opclass.h:49
void RemoveSchemaById(Oid schemaOid)
Definition: schemacmds.c:218
Definition: nodes.h:513
#define OperatorFamilyRelationId
Definition: pg_opfamily.h:29
void index_drop(Oid indexId, bool concurrent)
Definition: index.c:1440
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:897
AttrNumber varattno
Definition: primnodes.h:168
Expr * arg
Definition: primnodes.h:742
#define PublicationRelationId
void RemovePublicationById(Oid pubid)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
void RemovePublicationRelById(Oid proid)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2188
#define heap_close(r, l)
Definition: heapam.h:97
#define lengthof(array)
Definition: c.h:618
#define DEPFLAG_REVERSE
Definition: dependency.c:105
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2133
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
void RemoveAmProcEntryById(Oid entryOid)
Definition: opclasscmds.c:1651
#define LOG
Definition: elog.h:26
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2404
unsigned int Oid
Definition: postgres_ext.h:31
#define REGTYPEOID
Definition: pg_type.h:581
Definition: primnodes.h:163
#define InitPrivsObjIndexId
Definition: indexing.h:172
#define TypeRelationId
Definition: pg_type.h:34
void RemoveTypeById(Oid typeOid)
Definition: typecmds.c:689
#define REGOPEROID
Definition: pg_type.h:569
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2419
#define OidIsValid(objectId)
Definition: c.h:594
void RemoveCollationById(Oid collationOid)
Definition: pg_collation.c:211
void RemoveUserMappingById(Oid umId)
Definition: foreigncmds.c:1422
struct ObjectAddressStack * next
Definition: dependency.c:124
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:2042
void RemoveConversionById(Oid conversionOid)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
void RemoveAccessMethodById(Oid amOid)
Definition: amcmds.c:115
#define DefaultAclRelationId
#define Anum_pg_depend_objsubid
Definition: pg_depend.h:70
char * getObjectDescription(const ObjectAddress *object)
#define AccessMethodOperatorRelationId
Definition: pg_amop.h:54
signed int int32
Definition: c.h:302
List * windowClause
Definition: parsenodes.h:152
List * targetList
Definition: parsenodes.h:138
bool systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
Definition: genam.c:454
void RemoveOperatorById(Oid operOid)
Definition: operatorcmds.c:340
#define PublicationRelRelationId
#define AuthIdRelationId
Definition: pg_authid.h:42
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:190
#define list_make1(x1)
Definition: pg_list.h:139
Oid consttype
Definition: primnodes.h:192
#define TSConfigRelationId
Definition: pg_ts_config.h:31
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:784
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:182
Oid get_typ_typrelid(Oid typid)
Definition: lsyscache.c:2475
void pfree(void *pointer)
Definition: mcxt.c:936
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
bool resjunk
Definition: primnodes.h:1382
void RemoveDefaultACLById(Oid defaclOid)
Definition: aclchk.c:1526
List * rtable
Definition: parsenodes.h:135
List * distinctClause
Definition: parsenodes.h:154
Oid funcid
Definition: primnodes.h:449
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define ERROR
Definition: elog.h:43
#define DEPFLAG_ORIGINAL
Definition: dependency.c:100
static const Oid object_classes[]
Definition: dependency.c:138
Oid paramcollid
Definition: primnodes.h:248
#define DEPFLAG_AUTO
Definition: dependency.c:102
#define TSDictionaryRelationId
Definition: pg_ts_dict.h:31
#define lfirst_int(lc)
Definition: pg_list.h:107
void RemoveTSConfigurationById(Oid cfgId)
Definition: tsearchcmds.c:1125
ItemPointerData t_self
Definition: htup.h:65
const ObjectAddress * object
Definition: dependency.c:122
#define DEPFLAG_INTERNAL
Definition: dependency.c:103
Oid constcollid
Definition: primnodes.h:194
Oid resultcollid
Definition: primnodes.h:747
#define DEBUG2
Definition: elog.h:24
void RemoveOpFamilyById(Oid opfamilyOid)
Definition: opclasscmds.c:1584
void RemovePolicyById(Oid policy_id)
Definition: policy.c:350
#define SubscriptionRelationId
#define InitPrivsRelationId
Definition: pg_init_privs.h:42
void * list_nth(const List *list, int n)
Definition: list.c:410
#define Anum_pg_init_privs_objoid
Definition: pg_init_privs.h:68
static bool object_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddresses *addrs)
Definition: dependency.c:2274
#define AccessMethodRelationId
Definition: pg_am.h:32
struct ObjectAddressStack ObjectAddressStack
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
ObjectAddress * refs
Definition: dependency.c:111
ObjectClass
Definition: dependency.h:144
#define RowExclusiveLock
Definition: lockdefs.h:38
void RemoveTSParserById(Oid prsId)
Definition: tsearchcmds.c:292
int errdetail(const char *fmt,...)
Definition: elog.c:873
AttrNumber resno
Definition: primnodes.h:1376
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
Definition: dependency.c:1363
void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:856
void RemoveAttributeById(Oid relid, AttrNumber attnum)
Definition: heap.c:1544
#define REGDICTIONARYOID
Definition: pg_type.h:627
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:303
void RemoveTSTemplateById(Oid tmplId)
Definition: tsearchcmds.c:821
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:839
int errdetail_log(const char *fmt,...)
Definition: elog.c:921
Oid resulttype
Definition: primnodes.h:774
Oid winfnoid
Definition: primnodes.h:355
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:61
void DeleteSequenceTuple(Oid relid)
Definition: sequence.c:516
void RemoveEventTriggerById(Oid trigOid)
void RemoveAttrDefaultById(Oid attrdefId)
Definition: heap.c:1686
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
void LargeObjectDrop(Oid loid)
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:823
void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:877
bool EventTriggerSupportsObjectClass(ObjectClass objclass)
#define RELKIND_PARTITIONED_INDEX
Definition: pg_class.h:169
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
Index varno
Definition: primnodes.h:166
#define ngettext(s, p, n)
Definition: c.h:1011
void RemoveAmOpEntryById(Oid entryOid)
Definition: opclasscmds.c:1622
DropBehavior
Definition: parsenodes.h:1702
static void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1311
void CommandCounterIncrement(void)
Definition: xact.c:915
void DeleteComments(Oid oid, Oid classoid, int32 subid)
Definition: comment.c:325
Oid resultcollid
Definition: primnodes.h:817
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define TSParserRelationId
Definition: pg_ts_parser.h:31
Oid resulttype
Definition: primnodes.h:795
#define CollationRelationId
Definition: pg_collation.h:30
int log_min_messages
Definition: guc.c:452
Oid resultcollid
Definition: primnodes.h:797
void RemoveForeignServerById(Oid srvId)
Definition: foreigncmds.c:1084
bool creating_extension
Definition: extension.c:67
Oid aggfnoid
Definition: primnodes.h:294
#define PolicyRelationId
Definition: pg_policy.h:19
#define RewriteRelationId
Definition: pg_rewrite.h:32
#define DEPFLAG_EXTENSION
Definition: dependency.c:104
#define NOTICE
Definition: elog.h:37
CmdType commandType
Definition: parsenodes.h:110
List * lcons(void *datum, List *list)
Definition: list.c:259
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define Anum_pg_init_privs_objsubid
Definition: pg_init_privs.h:70
#define ForeignServerRelationId
List * funccolcollations
Definition: parsenodes.h:1101
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:688
#define lfirst(lc)
Definition: pg_list.h:106
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:56
#define StatisticExtRelationId
#define PERFORM_DELETION_QUIETLY
Definition: dependency.h:191
#define EventTriggerRelationId
#define TriggerRelationId
Definition: pg_trigger.h:34
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1834
Oid row_typeid
Definition: primnodes.h:991
static int list_length(const List *l)
Definition: pg_list.h:89
#define CastRelationId
Definition: pg_cast.h:31
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
#define ForeignDataWrapperRelationId
static void doDeletion(const ObjectAddress *object, int flags)
Definition: dependency.c:1115
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool ignore_self)
Definition: dependency.c:1404
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:949
#define InvalidAttrNumber
Definition: attnum.h:23
void RemoveFunctionById(Oid funcOid)
#define TableSpaceRelationId
Definition: pg_tablespace.h:29
#define REGCONFIGOID
Definition: pg_type.h:624
RTEKind rtekind
Definition: parsenodes.h:959
ObjectAddressExtra * extras
Definition: dependency.c:112
#define AccessExclusiveLock
Definition: lockdefs.h:45
#define Int32GetDatum(X)
Definition: postgres.h:462
List * groupClause
Definition: parsenodes.h:146
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DEPFLAG_NORMAL
Definition: dependency.c:101
NodeTag type
Definition: parsenodes.h:957
#define UserMappingRelationId
List * fieldnums
Definition: primnodes.h:773
void list_free(List *list)
Definition: list.c:1133
void RemoveRewriteRuleById(Oid ruleOid)
Definition: rewriteRemove.c:38
int i
static void DeleteInitPrivs(const ObjectAddress *object)
Definition: dependency.c:2554
#define RELKIND_INDEX
Definition: pg_class.h:161
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
int client_min_messages
Definition: guc.c:453
#define LanguageRelationId
Definition: pg_language.h:29
#define ConstraintRelationId
Definition: pg_constraint.h:29
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:219
Oid opno
Definition: primnodes.h:496
void DropCastById(Oid castOid)
#define Anum_pg_depend_classid
Definition: pg_depend.h:68
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
Definition: dependency.c:362
#define elog
Definition: elog.h:219
#define MAX_REPORTED_DEPS
#define ExtensionRelationId
Definition: pg_extension.h:29
#define qsort(a, b, c, d)
Definition: port.h:408
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
#define REGPROCEDUREOID
Definition: pg_type.h:565
static bool stack_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddressStack *stack)
Definition: dependency.c:2341
#define AttrDefaultRelationId
Definition: pg_attrdef.h:29
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2275
#define PERFORM_DELETION_SKIP_EXTENSIONS
Definition: dependency.h:193
#define RELKIND_RELATION
Definition: pg_class.h:160
void heap_drop_with_catalog(Oid relid)
Definition: heap.c:1759
#define LargeObjectRelationId
#define AccessMethodProcedureRelationId
Definition: pg_amproc.h:43
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
Definition: pg_list.h:45
#define ConversionRelationId
Definition: pg_conversion.h:38
#define REGNAMESPACEOID
Definition: pg_type.h:589
static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId, ObjectAddresses *addrs)
Definition: dependency.c:2155
ObjectAddress dependee
Definition: dependency.c:96
Oid startInRangeFunc
Definition: parsenodes.h:1305
void RemoveConstraintById(Oid conId)
#define _(x)
Definition: elog.c:84
#define Anum_pg_depend_objid
Definition: pg_depend.h:69
Oid paramtype
Definition: primnodes.h:246
#define InvokeObjectDropHookArg(classId, objectId, subId, dropflags)
Definition: objectaccess.h:156
bool constisnull
Definition: primnodes.h:197
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:108
#define REGOPERATOROID
Definition: pg_type.h:573
List * list_delete_first(List *list)
Definition: list.c:666
#define REGPROCOID
Definition: pg_type.h:320
#define TSTemplateRelationId
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:189
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
void RemoveTSDictionaryById(Oid dictId)
Definition: tsearchcmds.c:501
AttrNumber fieldnum
Definition: primnodes.h:743