PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dependency.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/dependency.h"
#include "catalog/heap.h"
#include "catalog/index.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_am.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_amproc.h"
#include "catalog/pg_attrdef.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_collation_fn.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_constraint_fn.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_conversion_fn.h"
#include "catalog/pg_database.h"
#include "catalog/pg_default_acl.h"
#include "catalog/pg_depend.h"
#include "catalog/pg_event_trigger.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_init_privs.h"
#include "catalog/pg_language.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_policy.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_publication.h"
#include "catalog/pg_publication_rel.h"
#include "catalog/pg_rewrite.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_transform.h"
#include "catalog/pg_trigger.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_ts_parser.h"
#include "catalog/pg_ts_template.h"
#include "catalog/pg_type.h"
#include "catalog/pg_user_mapping.h"
#include "commands/comment.h"
#include "commands/defrem.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/policy.h"
#include "commands/proclang.h"
#include "commands/publicationcmds.h"
#include "commands/schemacmds.h"
#include "commands/seclabel.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "commands/typecmds.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteRemove.h"
#include "storage/lmgr.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for dependency.c:

Go to the source code of this file.

Data Structures

struct  ObjectAddressExtra
 
struct  ObjectAddresses
 
struct  ObjectAddressStack
 
struct  find_expr_references_context
 

Macros

#define DEPFLAG_ORIGINAL   0x0001 /* an original deletion target */
 
#define DEPFLAG_NORMAL   0x0002 /* reached via normal dependency */
 
#define DEPFLAG_AUTO   0x0004 /* reached via auto dependency */
 
#define DEPFLAG_INTERNAL   0x0008 /* reached via internal dependency */
 
#define DEPFLAG_EXTENSION   0x0010 /* reached via extension dependency */
 
#define DEPFLAG_REVERSE   0x0020 /* reverse internal/extension link */
 
#define MAX_REPORTED_DEPS   100
 

Typedefs

typedef struct ObjectAddressStack ObjectAddressStack
 

Functions

static void findDependentObjects (const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
 
static void reportDependentObjects (const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
 
static void deleteOneObject (const ObjectAddress *object, Relation *depRel, int32 flags)
 
static void doDeletion (const ObjectAddress *object, int flags)
 
static void AcquireDeletionLock (const ObjectAddress *object, int flags)
 
static void ReleaseDeletionLock (const ObjectAddress *object)
 
static bool find_expr_references_walker (Node *node, find_expr_references_context *context)
 
static void eliminate_duplicate_dependencies (ObjectAddresses *addrs)
 
static int object_address_comparator (const void *a, const void *b)
 
static void add_object_address (ObjectClass oclass, Oid objectId, int32 subId, ObjectAddresses *addrs)
 
static void add_exact_object_address_extra (const ObjectAddress *object, const ObjectAddressExtra *extra, ObjectAddresses *addrs)
 
static bool object_address_present_add_flags (const ObjectAddress *object, int flags, ObjectAddresses *addrs)
 
static bool stack_address_present_add_flags (const ObjectAddress *object, int flags, ObjectAddressStack *stack)
 
static void DeleteInitPrivs (const ObjectAddress *object)
 
static void deleteObjectsInList (ObjectAddresses *targetObjects, Relation *depRel, int flags)
 
void performDeletion (const ObjectAddress *object, DropBehavior behavior, int flags)
 
void performMultipleDeletions (const ObjectAddresses *objects, DropBehavior behavior, int flags)
 
static void deleteOneObject (const ObjectAddress *object, Relation *depRel, int flags)
 
void recordDependencyOnExpr (const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
 
void recordDependencyOnSingleRelExpr (const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool ignore_self)
 
ObjectAddressesnew_object_addresses (void)
 
void add_exact_object_address (const ObjectAddress *object, ObjectAddresses *addrs)
 
bool object_address_present (const ObjectAddress *object, const ObjectAddresses *addrs)
 
void record_object_address_dependencies (const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
 
void free_object_addresses (ObjectAddresses *addrs)
 
ObjectClass getObjectClass (const ObjectAddress *object)
 

Variables

static const Oid object_classes []
 

Macro Definition Documentation

#define DEPFLAG_AUTO   0x0004 /* reached via auto dependency */

Definition at line 102 of file dependency.c.

Referenced by findDependentObjects(), and reportDependentObjects().

#define DEPFLAG_EXTENSION   0x0010 /* reached via extension dependency */

Definition at line 104 of file dependency.c.

Referenced by findDependentObjects(), and reportDependentObjects().

#define DEPFLAG_INTERNAL   0x0008 /* reached via internal dependency */

Definition at line 103 of file dependency.c.

Referenced by findDependentObjects(), and reportDependentObjects().

#define DEPFLAG_NORMAL   0x0002 /* reached via normal dependency */

Definition at line 101 of file dependency.c.

Referenced by deleteObjectsInList(), and findDependentObjects().

#define DEPFLAG_ORIGINAL   0x0001 /* an original deletion target */
#define DEPFLAG_REVERSE   0x0020 /* reverse internal/extension link */

Definition at line 105 of file dependency.c.

Referenced by deleteObjectsInList(), and findDependentObjects().

#define MAX_REPORTED_DEPS   100

Referenced by reportDependentObjects().

Typedef Documentation

Function Documentation

static void AcquireDeletionLock ( const ObjectAddress object,
int  flags 
)
static

Definition at line 1286 of file dependency.c.

References AccessExclusiveLock, ObjectAddress::classId, LockDatabaseObject(), LockRelationOid(), ObjectAddress::objectId, PERFORM_DELETION_CONCURRENTLY, RelationRelationId, and ShareUpdateExclusiveLock.

Referenced by findDependentObjects(), performDeletion(), and performMultipleDeletions().

1287 {
1288  if (object->classId == RelationRelationId)
1289  {
1290  /*
1291  * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1292  * the index for the moment. index_drop() will promote the lock once
1293  * it's safe to do so. In all other cases we need full exclusive
1294  * lock.
1295  */
1296  if (flags & PERFORM_DELETION_CONCURRENTLY)
1298  else
1300  }
1301  else
1302  {
1303  /* assume we should lock the whole object not a sub-object */
1304  LockDatabaseObject(object->classId, object->objectId, 0,
1306  }
1307 }
#define RelationRelationId
Definition: pg_class.h:29
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:175
void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:830
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define AccessExclusiveLock
Definition: lockdefs.h:46
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105
void add_exact_object_address ( const ObjectAddress object,
ObjectAddresses addrs 
)

Definition at line 2094 of file dependency.c.

References Assert, ObjectAddresses::extras, ObjectAddresses::maxrefs, ObjectAddresses::numrefs, ObjectAddressStack::object, ObjectAddresses::refs, and repalloc().

Referenced by AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterTypeNamespaceInternal(), makeConfigurationDependencies(), recordDependencyOnSingleRelExpr(), RemoveObjects(), RemoveRelations(), and shdepDropOwned().

2096 {
2097  ObjectAddress *item;
2098 
2099  /* enlarge array if needed */
2100  if (addrs->numrefs >= addrs->maxrefs)
2101  {
2102  addrs->maxrefs *= 2;
2103  addrs->refs = (ObjectAddress *)
2104  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2105  Assert(!addrs->extras);
2106  }
2107  /* record this item */
2108  item = addrs->refs + addrs->numrefs;
2109  *item = *object;
2110  addrs->numrefs++;
2111 }
ObjectAddress * refs
Definition: dependency.c:111
#define Assert(condition)
Definition: c.h:675
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
ObjectAddressExtra * extras
Definition: dependency.c:112
static void add_exact_object_address_extra ( const ObjectAddress object,
const ObjectAddressExtra extra,
ObjectAddresses addrs 
)
static

Definition at line 2119 of file dependency.c.

References ObjectAddresses::extras, ObjectAddresses::maxrefs, ObjectAddresses::numrefs, ObjectAddressStack::object, palloc(), ObjectAddresses::refs, and repalloc().

Referenced by findDependentObjects().

2122 {
2123  ObjectAddress *item;
2124  ObjectAddressExtra *itemextra;
2125 
2126  /* allocate extra space if first time */
2127  if (!addrs->extras)
2128  addrs->extras = (ObjectAddressExtra *)
2129  palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
2130 
2131  /* enlarge array if needed */
2132  if (addrs->numrefs >= addrs->maxrefs)
2133  {
2134  addrs->maxrefs *= 2;
2135  addrs->refs = (ObjectAddress *)
2136  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2137  addrs->extras = (ObjectAddressExtra *)
2138  repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
2139  }
2140  /* record this item */
2141  item = addrs->refs + addrs->numrefs;
2142  *item = *object;
2143  itemextra = addrs->extras + addrs->numrefs;
2144  *itemextra = *extra;
2145  addrs->numrefs++;
2146 }
ObjectAddress * refs
Definition: dependency.c:111
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
ObjectAddressExtra * extras
Definition: dependency.c:112
void * palloc(Size size)
Definition: mcxt.c:849
static void add_object_address ( ObjectClass  oclass,
Oid  objectId,
int32  subId,
ObjectAddresses addrs 
)
static

Definition at line 2061 of file dependency.c.

References Assert, ObjectAddress::classId, ObjectAddresses::extras, LAST_OCLASS, lengthof, ObjectAddresses::maxrefs, ObjectAddresses::numrefs, object_classes, ObjectAddress::objectId, ObjectAddress::objectSubId, ObjectAddresses::refs, repalloc(), and StaticAssertStmt.

Referenced by find_expr_references_walker().

2063 {
2064  ObjectAddress *item;
2065 
2066  /*
2067  * Make sure object_classes is kept up to date with the ObjectClass enum.
2068  */
2070  "object_classes[] must cover all ObjectClasses");
2071 
2072  /* enlarge array if needed */
2073  if (addrs->numrefs >= addrs->maxrefs)
2074  {
2075  addrs->maxrefs *= 2;
2076  addrs->refs = (ObjectAddress *)
2077  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2078  Assert(!addrs->extras);
2079  }
2080  /* record this item */
2081  item = addrs->refs + addrs->numrefs;
2082  item->classId = object_classes[oclass];
2083  item->objectId = objectId;
2084  item->objectSubId = subId;
2085  addrs->numrefs++;
2086 }
#define LAST_OCLASS
Definition: dependency.h:171
#define lengthof(array)
Definition: c.h:562
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:757
static const Oid object_classes[]
Definition: dependency.c:138
ObjectAddress * refs
Definition: dependency.c:111
#define Assert(condition)
Definition: c.h:675
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
ObjectAddressExtra * extras
Definition: dependency.c:112
static void DeleteInitPrivs ( const ObjectAddress object)
static

Definition at line 2460 of file dependency.c.

References Anum_pg_init_privs_classoid, Anum_pg_init_privs_objoid, Anum_pg_init_privs_objsubid, BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, heap_close, heap_open(), HeapTupleIsValid, InitPrivsObjIndexId, InitPrivsRelationId, Int32GetDatum, NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), and HeapTupleData::t_self.

Referenced by deleteOneObject().

2461 {
2462  Relation relation;
2463  ScanKeyData key[3];
2464  SysScanDesc scan;
2465  HeapTuple oldtuple;
2466 
2468 
2469  ScanKeyInit(&key[0],
2471  BTEqualStrategyNumber, F_OIDEQ,
2472  ObjectIdGetDatum(object->objectId));
2473  ScanKeyInit(&key[1],
2475  BTEqualStrategyNumber, F_OIDEQ,
2476  ObjectIdGetDatum(object->classId));
2477  ScanKeyInit(&key[2],
2479  BTEqualStrategyNumber, F_INT4EQ,
2480  Int32GetDatum(object->objectSubId));
2481 
2482  scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
2483  NULL, 3, key);
2484 
2485  while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
2486  CatalogTupleDelete(relation, &oldtuple->t_self);
2487 
2488  systable_endscan(scan);
2489 
2490  heap_close(relation, RowExclusiveLock);
2491 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define Anum_pg_init_privs_classoid
Definition: pg_init_privs.h:70
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
#define InitPrivsObjIndexId
Definition: indexing.h:172
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
ItemPointerData t_self
Definition: htup.h:65
#define InitPrivsRelationId
Definition: pg_init_privs.h:42
#define Anum_pg_init_privs_objoid
Definition: pg_init_privs.h:69
#define RowExclusiveLock
Definition: lockdefs.h:38
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define Anum_pg_init_privs_objsubid
Definition: pg_init_privs.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Int32GetDatum(X)
Definition: postgres.h:485
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static void deleteObjectsInList ( ObjectAddresses targetObjects,
Relation depRel,
int  flags 
)
static

Definition at line 219 of file dependency.c.

References deleteOneObject(), DEPFLAG_NORMAL, DEPFLAG_ORIGINAL, DEPFLAG_REVERSE, EventTriggerSQLDropAddObject(), EventTriggerSupportsObjectClass(), ObjectAddresses::extras, ObjectAddressExtra::flags, getObjectClass(), i, ObjectAddresses::numrefs, PERFORM_DELETION_INTERNAL, PERFORM_DELETION_SKIP_ORIGINAL, ObjectAddresses::refs, and trackDroppedObjectsNeeded().

Referenced by performDeletion(), and performMultipleDeletions().

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 }
static void deleteOneObject(const ObjectAddress *object, Relation *depRel, int32 flags)
bool trackDroppedObjectsNeeded(void)
#define PERFORM_DELETION_SKIP_ORIGINAL
Definition: dependency.h:177
#define DEPFLAG_REVERSE
Definition: dependency.c:105
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2325
#define DEPFLAG_ORIGINAL
Definition: dependency.c:100
ObjectAddress * refs
Definition: dependency.c:111
bool EventTriggerSupportsObjectClass(ObjectClass objclass)
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
ObjectAddressExtra * extras
Definition: dependency.c:112
#define DEPFLAG_NORMAL
Definition: dependency.c:101
int i
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:174
static void deleteOneObject ( const ObjectAddress object,
Relation depRel,
int32  flags 
)
static

Referenced by deleteObjectsInList().

static void deleteOneObject ( const ObjectAddress object,
Relation depRel,
int  flags 
)
static

Definition at line 1000 of file dependency.c.

References Anum_pg_depend_classid, Anum_pg_depend_objid, Anum_pg_depend_objsubid, BTEqualStrategyNumber, CatalogTupleDelete(), ObjectAddress::classId, CommandCounterIncrement(), DeleteComments(), DeleteInitPrivs(), DeleteSecurityLabel(), deleteSharedDependencyRecordsFor(), DependDependerIndexId, DependRelationId, doDeletion(), heap_close, heap_open(), HeapTupleIsValid, Int32GetDatum, InvokeObjectDropHookArg, NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, PERFORM_DELETION_CONCURRENTLY, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), and HeapTupleData::t_self.

1001 {
1002  ScanKeyData key[3];
1003  int nkeys;
1004  SysScanDesc scan;
1005  HeapTuple tup;
1006 
1007  /* DROP hook of the objects being removed */
1008  InvokeObjectDropHookArg(object->classId, object->objectId,
1009  object->objectSubId, flags);
1010 
1011  /*
1012  * Close depRel if we are doing a drop concurrently. The object deletion
1013  * subroutine will commit the current transaction, so we can't keep the
1014  * relation open across doDeletion().
1015  */
1016  if (flags & PERFORM_DELETION_CONCURRENTLY)
1017  heap_close(*depRel, RowExclusiveLock);
1018 
1019  /*
1020  * Delete the object itself, in an object-type-dependent way.
1021  *
1022  * We used to do this after removing the outgoing dependency links, but it
1023  * seems just as reasonable to do it beforehand. In the concurrent case
1024  * we *must* do it in this order, because we can't make any transactional
1025  * updates before calling doDeletion() --- they'd get committed right
1026  * away, which is not cool if the deletion then fails.
1027  */
1028  doDeletion(object, flags);
1029 
1030  /*
1031  * Reopen depRel if we closed it above
1032  */
1033  if (flags & PERFORM_DELETION_CONCURRENTLY)
1035 
1036  /*
1037  * Now remove any pg_depend records that link from this object to others.
1038  * (Any records linking to this object should be gone already.)
1039  *
1040  * When dropping a whole object (subId = 0), remove all pg_depend records
1041  * for its sub-objects too.
1042  */
1043  ScanKeyInit(&key[0],
1045  BTEqualStrategyNumber, F_OIDEQ,
1046  ObjectIdGetDatum(object->classId));
1047  ScanKeyInit(&key[1],
1049  BTEqualStrategyNumber, F_OIDEQ,
1050  ObjectIdGetDatum(object->objectId));
1051  if (object->objectSubId != 0)
1052  {
1053  ScanKeyInit(&key[2],
1055  BTEqualStrategyNumber, F_INT4EQ,
1056  Int32GetDatum(object->objectSubId));
1057  nkeys = 3;
1058  }
1059  else
1060  nkeys = 2;
1061 
1062  scan = systable_beginscan(*depRel, DependDependerIndexId, true,
1063  NULL, nkeys, key);
1064 
1065  while (HeapTupleIsValid(tup = systable_getnext(scan)))
1066  {
1067  CatalogTupleDelete(*depRel, &tup->t_self);
1068  }
1069 
1070  systable_endscan(scan);
1071 
1072  /*
1073  * Delete shared dependency references related to this object. Again, if
1074  * subId = 0, remove records for sub-objects too.
1075  */
1077  object->objectSubId);
1078 
1079 
1080  /*
1081  * Delete any comments, security labels, or initial privileges associated
1082  * with this object. (This is a convenient place to do these things,
1083  * rather than having every object type know to do it.)
1084  */
1085  DeleteComments(object->objectId, object->classId, object->objectSubId);
1086  DeleteSecurityLabel(object);
1087  DeleteInitPrivs(object);
1088 
1089  /*
1090  * CommandCounterIncrement here to ensure that preceding changes are all
1091  * visible to the next deletion step.
1092  */
1094 
1095  /*
1096  * And we're done!
1097  */
1098 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define DependDependerIndexId
Definition: indexing.h:145
#define DependRelationId
Definition: pg_depend.h:29
void DeleteSecurityLabel(const ObjectAddress *object)
Definition: seclabel.c:446
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
#define Anum_pg_depend_objsubid
Definition: pg_depend.h:70
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:175
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:823
void CommandCounterIncrement(void)
Definition: xact.c:922
void DeleteComments(Oid oid, Oid classoid, int32 subid)
Definition: comment.c:325
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
static void doDeletion(const ObjectAddress *object, int flags)
Definition: dependency.c:1104
#define Int32GetDatum(X)
Definition: postgres.h:485
static void DeleteInitPrivs(const ObjectAddress *object)
Definition: dependency.c:2460
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define Anum_pg_depend_classid
Definition: pg_depend.h:68
#define Anum_pg_depend_objid
Definition: pg_depend.h:69
#define InvokeObjectDropHookArg(classId, objectId, subId, dropflags)
Definition: objectaccess.h:156
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static void doDeletion ( const ObjectAddress object,
int  flags 
)
static

Definition at line 1104 of file dependency.c.

References Assert, ObjectAddress::classId, DeleteSequenceTuple(), DropCastById(), DropProceduralLanguageById(), DropTransformById(), elog, ERROR, get_rel_relkind(), getObjectClass(), heap_drop_with_catalog(), index_drop(), LargeObjectDrop(), ObjectAddress::objectId, ObjectAddress::objectSubId, OCLASS_AM, OCLASS_AMOP, OCLASS_AMPROC, OCLASS_CAST, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_CONVERSION, OCLASS_DEFACL, OCLASS_DEFAULT, OCLASS_EVENT_TRIGGER, OCLASS_EXTENSION, OCLASS_FDW, OCLASS_FOREIGN_SERVER, OCLASS_LANGUAGE, OCLASS_LARGEOBJECT, OCLASS_OPCLASS, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_POLICY, OCLASS_PROC, OCLASS_PUBLICATION, OCLASS_PUBLICATION_REL, OCLASS_REWRITE, OCLASS_SCHEMA, OCLASS_STATISTIC_EXT, OCLASS_TRANSFORM, OCLASS_TRIGGER, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TSPARSER, OCLASS_TSTEMPLATE, OCLASS_TYPE, OCLASS_USER_MAPPING, PERFORM_DELETION_CONCURRENTLY, RELKIND_INDEX, RELKIND_SEQUENCE, RemoveAccessMethodById(), RemoveAmOpEntryById(), RemoveAmProcEntryById(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveCollationById(), RemoveConstraintById(), RemoveConversionById(), RemoveDefaultACLById(), RemoveEventTriggerById(), RemoveExtensionById(), RemoveForeignDataWrapperById(), RemoveForeignServerById(), RemoveFunctionById(), RemoveOpClassById(), RemoveOperatorById(), RemoveOpFamilyById(), RemovePolicyById(), RemovePublicationById(), RemovePublicationRelById(), RemoveRewriteRuleById(), RemoveSchemaById(), RemoveStatisticsById(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTSDictionaryById(), RemoveTSParserById(), RemoveTSTemplateById(), RemoveTypeById(), and RemoveUserMappingById().

Referenced by deleteOneObject().

1105 {
1106  switch (getObjectClass(object))
1107  {
1108  case OCLASS_CLASS:
1109  {
1110  char relKind = get_rel_relkind(object->objectId);
1111 
1112  if (relKind == RELKIND_INDEX)
1113  {
1114  bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
1115 
1116  Assert(object->objectSubId == 0);
1117  index_drop(object->objectId, concurrent);
1118  }
1119  else
1120  {
1121  if (object->objectSubId != 0)
1122  RemoveAttributeById(object->objectId,
1123  object->objectSubId);
1124  else
1126  }
1127 
1128  /* for a sequence, in addition to dropping the heap, also
1129  * delete pg_sequence tuple */
1130  if (relKind == RELKIND_SEQUENCE)
1131  DeleteSequenceTuple(object->objectId);
1132  break;
1133  }
1134 
1135  case OCLASS_PROC:
1136  RemoveFunctionById(object->objectId);
1137  break;
1138 
1139  case OCLASS_TYPE:
1140  RemoveTypeById(object->objectId);
1141  break;
1142 
1143  case OCLASS_CAST:
1144  DropCastById(object->objectId);
1145  break;
1146 
1147  case OCLASS_COLLATION:
1148  RemoveCollationById(object->objectId);
1149  break;
1150 
1151  case OCLASS_CONSTRAINT:
1152  RemoveConstraintById(object->objectId);
1153  break;
1154 
1155  case OCLASS_CONVERSION:
1156  RemoveConversionById(object->objectId);
1157  break;
1158 
1159  case OCLASS_DEFAULT:
1161  break;
1162 
1163  case OCLASS_LANGUAGE:
1165  break;
1166 
1167  case OCLASS_LARGEOBJECT:
1168  LargeObjectDrop(object->objectId);
1169  break;
1170 
1171  case OCLASS_OPERATOR:
1172  RemoveOperatorById(object->objectId);
1173  break;
1174 
1175  case OCLASS_OPCLASS:
1176  RemoveOpClassById(object->objectId);
1177  break;
1178 
1179  case OCLASS_OPFAMILY:
1180  RemoveOpFamilyById(object->objectId);
1181  break;
1182 
1183  case OCLASS_AM:
1185  break;
1186 
1187  case OCLASS_AMOP:
1188  RemoveAmOpEntryById(object->objectId);
1189  break;
1190 
1191  case OCLASS_AMPROC:
1193  break;
1194 
1195  case OCLASS_REWRITE:
1197  break;
1198 
1199  case OCLASS_TRIGGER:
1200  RemoveTriggerById(object->objectId);
1201  break;
1202 
1203  case OCLASS_SCHEMA:
1204  RemoveSchemaById(object->objectId);
1205  break;
1206 
1207  case OCLASS_TSPARSER:
1208  RemoveTSParserById(object->objectId);
1209  break;
1210 
1211  case OCLASS_TSDICT:
1213  break;
1214 
1215  case OCLASS_TSTEMPLATE:
1216  RemoveTSTemplateById(object->objectId);
1217  break;
1218 
1219  case OCLASS_TSCONFIG:
1221  break;
1222 
1223  /*
1224  * OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE intentionally not
1225  * handled here
1226  */
1227 
1228  case OCLASS_FDW:
1230  break;
1231 
1232  case OCLASS_FOREIGN_SERVER:
1234  break;
1235 
1236  case OCLASS_USER_MAPPING:
1238  break;
1239 
1240  case OCLASS_DEFACL:
1241  RemoveDefaultACLById(object->objectId);
1242  break;
1243 
1244  case OCLASS_EXTENSION:
1245  RemoveExtensionById(object->objectId);
1246  break;
1247 
1248  case OCLASS_EVENT_TRIGGER:
1250  break;
1251 
1252  case OCLASS_POLICY:
1253  RemovePolicyById(object->objectId);
1254  break;
1255 
1256  case OCLASS_PUBLICATION:
1258  break;
1259 
1262  break;
1263 
1264  case OCLASS_TRANSFORM:
1265  DropTransformById(object->objectId);
1266  break;
1267 
1268  case OCLASS_STATISTIC_EXT:
1269  RemoveStatisticsById(object->objectId);
1270  break;
1271 
1272  default:
1273  elog(ERROR, "unrecognized object class: %u",
1274  object->classId);
1275  }
1276 }
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1197
void RemoveStatisticsById(Oid statsOid)
Definition: statscmds.c:287
void RemoveOpClassById(Oid opclassOid)
Definition: opclasscmds.c:1566
void DropTransformById(Oid transformOid)
void RemoveExtensionById(Oid extId)
Definition: extension.c:1822
void DropProceduralLanguageById(Oid langOid)
Definition: proclang.c:528
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1801
void RemoveForeignDataWrapperById(Oid fdwId)
Definition: foreigncmds.c:837
void RemoveSchemaById(Oid schemaOid)
Definition: schemacmds.c:218
void index_drop(Oid indexId, bool concurrent)
Definition: index.c:1345
void RemovePublicationById(Oid pubid)
void RemovePublicationRelById(Oid proid)
void RemoveAmProcEntryById(Oid entryOid)
Definition: opclasscmds.c:1614
void RemoveTypeById(Oid typeOid)
Definition: typecmds.c:689
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2325
void RemoveCollationById(Oid collationOid)
Definition: pg_collation.c:198
void RemoveUserMappingById(Oid umId)
Definition: foreigncmds.c:1422
void RemoveConversionById(Oid conversionOid)
void RemoveAccessMethodById(Oid amOid)
Definition: amcmds.c:115
void RemoveOperatorById(Oid operOid)
Definition: operatorcmds.c:340
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:175
void RemoveDefaultACLById(Oid defaclOid)
Definition: aclchk.c:1468
#define ERROR
Definition: elog.h:43
void RemoveTSConfigurationById(Oid cfgId)
Definition: tsearchcmds.c:1125
void RemoveOpFamilyById(Oid opfamilyOid)
Definition: opclasscmds.c:1547
void RemovePolicyById(Oid policy_id)
Definition: policy.c:350
void RemoveTSParserById(Oid prsId)
Definition: tsearchcmds.c:292
void RemoveAttributeById(Oid relid, AttrNumber attnum)
Definition: heap.c:1541
void RemoveTSTemplateById(Oid tmplId)
Definition: tsearchcmds.c:821
void DeleteSequenceTuple(Oid relid)
Definition: sequence.c:516
void RemoveEventTriggerById(Oid trigOid)
void RemoveAttrDefaultById(Oid attrdefId)
Definition: heap.c:1686
void LargeObjectDrop(Oid loid)
void RemoveAmOpEntryById(Oid entryOid)
Definition: opclasscmds.c:1585
void RemoveForeignServerById(Oid srvId)
Definition: foreigncmds.c:1084
#define Assert(condition)
Definition: c.h:675
void RemoveFunctionById(Oid funcOid)
void RemoveRewriteRuleById(Oid ruleOid)
Definition: rewriteRemove.c:38
#define RELKIND_INDEX
Definition: pg_class.h:161
void DropCastById(Oid castOid)
#define elog
Definition: elog.h:219
void heap_drop_with_catalog(Oid relid)
Definition: heap.c:1759
#define RELKIND_SEQUENCE
Definition: pg_class.h:162
void RemoveConstraintById(Oid conId)
void RemoveTSDictionaryById(Oid dictId)
Definition: tsearchcmds.c:501
static void eliminate_duplicate_dependencies ( ObjectAddresses addrs)
static

Definition at line 1948 of file dependency.c.

References Assert, ObjectAddress::classId, ObjectAddresses::extras, ObjectAddresses::numrefs, object_address_comparator(), ObjectAddress::objectId, ObjectAddress::objectSubId, qsort, and ObjectAddresses::refs.

Referenced by record_object_address_dependencies(), recordDependencyOnExpr(), and recordDependencyOnSingleRelExpr().

1949 {
1950  ObjectAddress *priorobj;
1951  int oldref,
1952  newrefs;
1953 
1954  /*
1955  * We can't sort if the array has "extra" data, because there's no way to
1956  * keep it in sync. Fortunately that combination of features is not
1957  * needed.
1958  */
1959  Assert(!addrs->extras);
1960 
1961  if (addrs->numrefs <= 1)
1962  return; /* nothing to do */
1963 
1964  /* Sort the refs so that duplicates are adjacent */
1965  qsort((void *) addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
1967 
1968  /* Remove dups */
1969  priorobj = addrs->refs;
1970  newrefs = 1;
1971  for (oldref = 1; oldref < addrs->numrefs; oldref++)
1972  {
1973  ObjectAddress *thisobj = addrs->refs + oldref;
1974 
1975  if (priorobj->classId == thisobj->classId &&
1976  priorobj->objectId == thisobj->objectId)
1977  {
1978  if (priorobj->objectSubId == thisobj->objectSubId)
1979  continue; /* identical, so drop thisobj */
1980 
1981  /*
1982  * If we have a whole-object reference and a reference to a part
1983  * of the same object, we don't need the whole-object reference
1984  * (for example, we don't need to reference both table foo and
1985  * column foo.bar). The whole-object reference will always appear
1986  * first in the sorted list.
1987  */
1988  if (priorobj->objectSubId == 0)
1989  {
1990  /* replace whole ref with partial */
1991  priorobj->objectSubId = thisobj->objectSubId;
1992  continue;
1993  }
1994  }
1995  /* Not identical, so add thisobj to output set */
1996  priorobj++;
1997  *priorobj = *thisobj;
1998  newrefs++;
1999  }
2000 
2001  addrs->numrefs = newrefs;
2002 }
static int object_address_comparator(const void *a, const void *b)
Definition: dependency.c:2008
ObjectAddress * refs
Definition: dependency.c:111
#define Assert(condition)
Definition: c.h:675
ObjectAddressExtra * extras
Definition: dependency.c:112
#define qsort(a, b, c, d)
Definition: port.h:440
static bool find_expr_references_walker ( Node node,
find_expr_references_context context 
)
static

Definition at line 1474 of file dependency.c.

References add_object_address(), find_expr_references_context::addrs, Aggref::aggfnoid, CMD_INSERT, CMD_UPDATE, CollateExpr::collOid, Query::commandType, Const::constcollid, Const::constisnull, OnConflictExpr::constraint, Query::constraintDeps, Const::consttype, Const::constvalue, DatumGetObjectId, DEFAULT_COLLATION_OID, Query::distinctClause, ArrayCoerceExpr::elemfuncid, elog, SortGroupClause::eqop, ereport, errcode(), errmsg(), ERROR, expression_tree_walker(), RangeTblFunction::funccolcollations, RangeTblFunction::funccoltypes, FuncExpr::funcid, Query::groupClause, SetOperationStmt::groupClauses, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, lcons(), lfirst, lfirst_oid, list_copy_tail(), list_delete_first(), list_free(), list_length(), list_nth(), NAMESPACEOID, NULL, ObjectIdGetDatum, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_PROC, OCLASS_SCHEMA, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TYPE, OidIsValid, OPEROID, RowCompareExpr::opfamilies, OpExpr::opno, ScalarArrayOpExpr::opno, RowCompareExpr::opnos, Param::paramcollid, Param::paramtype, PROCOID, QTW_IGNORE_JOINALIASES, query_tree_walker(), REGCLASSOID, REGCONFIGOID, REGDICTIONARYOID, REGNAMESPACEOID, REGOPERATOROID, REGOPEROID, REGPROCEDUREOID, REGPROCOID, REGROLEOID, REGTYPEOID, RangeTblEntry::relid, RELOID, TargetEntry::resjunk, TargetEntry::resno, result, RelabelType::resultcollid, Query::resultRelation, RelabelType::resulttype, CoerceViaIO::resulttype, ArrayCoerceExpr::resulttype, ConvertRowtypeExpr::resulttype, CoerceToDomain::resulttype, RowExpr::row_typeid, rt_fetch, Query::rtable, find_expr_references_context::rtables, RTE_JOIN, RTE_RELATION, RangeTblEntry::rtekind, SearchSysCacheExists1, NextValueExpr::seqid, Query::sortClause, SortGroupClause::sortop, Query::targetList, TSCONFIGOID, TSDICTOID, TableSampleClause::tsmhandler, TYPEOID, Var::varattno, Var::varlevelsup, Var::varno, Query::windowClause, and WindowFunc::winfnoid.

Referenced by recordDependencyOnExpr(), and recordDependencyOnSingleRelExpr().

1476 {
1477  if (node == NULL)
1478  return false;
1479  if (IsA(node, Var))
1480  {
1481  Var *var = (Var *) node;
1482  List *rtable;
1483  RangeTblEntry *rte;
1484 
1485  /* Find matching rtable entry, or complain if not found */
1486  if (var->varlevelsup >= list_length(context->rtables))
1487  elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
1488  rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1489  if (var->varno <= 0 || var->varno > list_length(rtable))
1490  elog(ERROR, "invalid varno %d", var->varno);
1491  rte = rt_fetch(var->varno, rtable);
1492 
1493  /*
1494  * A whole-row Var references no specific columns, so adds no new
1495  * dependency. (We assume that there is a whole-table dependency
1496  * arising from each underlying rangetable entry. While we could
1497  * record such a dependency when finding a whole-row Var that
1498  * references a relation directly, it's quite unclear how to extend
1499  * that to whole-row Vars for JOINs, so it seems better to leave the
1500  * responsibility with the range table. Note that this poses some
1501  * risks for identifying dependencies of stand-alone expressions:
1502  * whole-table references may need to be created separately.)
1503  */
1504  if (var->varattno == InvalidAttrNumber)
1505  return false;
1506  if (rte->rtekind == RTE_RELATION)
1507  {
1508  /* If it's a plain relation, reference this column */
1510  context->addrs);
1511  }
1512  else if (rte->rtekind == RTE_JOIN)
1513  {
1514  /* Scan join output column to add references to join inputs */
1515  List *save_rtables;
1516 
1517  /* We must make the context appropriate for join's level */
1518  save_rtables = context->rtables;
1519  context->rtables = list_copy_tail(context->rtables,
1520  var->varlevelsup);
1521  if (var->varattno <= 0 ||
1522  var->varattno > list_length(rte->joinaliasvars))
1523  elog(ERROR, "invalid varattno %d", var->varattno);
1525  var->varattno - 1),
1526  context);
1527  list_free(context->rtables);
1528  context->rtables = save_rtables;
1529  }
1530  return false;
1531  }
1532  else if (IsA(node, Const))
1533  {
1534  Const *con = (Const *) node;
1535  Oid objoid;
1536 
1537  /* A constant must depend on the constant's datatype */
1539  context->addrs);
1540 
1541  /*
1542  * We must also depend on the constant's collation: it could be
1543  * different from the datatype's, if a CollateExpr was const-folded to
1544  * a simple constant. However we can save work in the most common
1545  * case where the collation is "default", since we know that's pinned.
1546  */
1547  if (OidIsValid(con->constcollid) &&
1550  context->addrs);
1551 
1552  /*
1553  * If it's a regclass or similar literal referring to an existing
1554  * object, add a reference to that object. (Currently, only the
1555  * regclass and regconfig cases have any likely use, but we may as
1556  * well handle all the OID-alias datatypes consistently.)
1557  */
1558  if (!con->constisnull)
1559  {
1560  switch (con->consttype)
1561  {
1562  case REGPROCOID:
1563  case REGPROCEDUREOID:
1564  objoid = DatumGetObjectId(con->constvalue);
1566  ObjectIdGetDatum(objoid)))
1567  add_object_address(OCLASS_PROC, objoid, 0,
1568  context->addrs);
1569  break;
1570  case REGOPEROID:
1571  case REGOPERATOROID:
1572  objoid = DatumGetObjectId(con->constvalue);
1574  ObjectIdGetDatum(objoid)))
1576  context->addrs);
1577  break;
1578  case REGCLASSOID:
1579  objoid = DatumGetObjectId(con->constvalue);
1581  ObjectIdGetDatum(objoid)))
1582  add_object_address(OCLASS_CLASS, objoid, 0,
1583  context->addrs);
1584  break;
1585  case REGTYPEOID:
1586  objoid = DatumGetObjectId(con->constvalue);
1588  ObjectIdGetDatum(objoid)))
1589  add_object_address(OCLASS_TYPE, objoid, 0,
1590  context->addrs);
1591  break;
1592  case REGCONFIGOID:
1593  objoid = DatumGetObjectId(con->constvalue);
1595  ObjectIdGetDatum(objoid)))
1597  context->addrs);
1598  break;
1599  case REGDICTIONARYOID:
1600  objoid = DatumGetObjectId(con->constvalue);
1602  ObjectIdGetDatum(objoid)))
1603  add_object_address(OCLASS_TSDICT, objoid, 0,
1604  context->addrs);
1605  break;
1606 
1607  case REGNAMESPACEOID:
1608  objoid = DatumGetObjectId(con->constvalue);
1610  ObjectIdGetDatum(objoid)))
1611  add_object_address(OCLASS_SCHEMA, objoid, 0,
1612  context->addrs);
1613  break;
1614 
1615  /*
1616  * Dependencies for regrole should be shared among all
1617  * databases, so explicitly inhibit to have dependencies.
1618  */
1619  case REGROLEOID:
1620  ereport(ERROR,
1621  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1622  errmsg("constant of the type %s cannot be used here",
1623  "regrole")));
1624  break;
1625  }
1626  }
1627  return false;
1628  }
1629  else if (IsA(node, Param))
1630  {
1631  Param *param = (Param *) node;
1632 
1633  /* A parameter must depend on the parameter's datatype */
1635  context->addrs);
1636  /* and its collation, just as for Consts */
1637  if (OidIsValid(param->paramcollid) &&
1640  context->addrs);
1641  }
1642  else if (IsA(node, FuncExpr))
1643  {
1644  FuncExpr *funcexpr = (FuncExpr *) node;
1645 
1646  add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
1647  context->addrs);
1648  /* fall through to examine arguments */
1649  }
1650  else if (IsA(node, OpExpr))
1651  {
1652  OpExpr *opexpr = (OpExpr *) node;
1653 
1655  context->addrs);
1656  /* fall through to examine arguments */
1657  }
1658  else if (IsA(node, DistinctExpr))
1659  {
1660  DistinctExpr *distinctexpr = (DistinctExpr *) node;
1661 
1662  add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
1663  context->addrs);
1664  /* fall through to examine arguments */
1665  }
1666  else if (IsA(node, NullIfExpr))
1667  {
1668  NullIfExpr *nullifexpr = (NullIfExpr *) node;
1669 
1670  add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
1671  context->addrs);
1672  /* fall through to examine arguments */
1673  }
1674  else if (IsA(node, ScalarArrayOpExpr))
1675  {
1676  ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1677 
1679  context->addrs);
1680  /* fall through to examine arguments */
1681  }
1682  else if (IsA(node, Aggref))
1683  {
1684  Aggref *aggref = (Aggref *) node;
1685 
1687  context->addrs);
1688  /* fall through to examine arguments */
1689  }
1690  else if (IsA(node, WindowFunc))
1691  {
1692  WindowFunc *wfunc = (WindowFunc *) node;
1693 
1695  context->addrs);
1696  /* fall through to examine arguments */
1697  }
1698  else if (IsA(node, SubPlan))
1699  {
1700  /* Extra work needed here if we ever need this case */
1701  elog(ERROR, "already-planned subqueries not supported");
1702  }
1703  else if (IsA(node, RelabelType))
1704  {
1705  RelabelType *relab = (RelabelType *) node;
1706 
1707  /* since there is no function dependency, need to depend on type */
1709  context->addrs);
1710  /* the collation might not be referenced anywhere else, either */
1711  if (OidIsValid(relab->resultcollid) &&
1714  context->addrs);
1715  }
1716  else if (IsA(node, CoerceViaIO))
1717  {
1718  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1719 
1720  /* since there is no exposed function, need to depend on type */
1722  context->addrs);
1723  }
1724  else if (IsA(node, ArrayCoerceExpr))
1725  {
1726  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1727 
1728  if (OidIsValid(acoerce->elemfuncid))
1730  context->addrs);
1732  context->addrs);
1733  /* fall through to examine arguments */
1734  }
1735  else if (IsA(node, ConvertRowtypeExpr))
1736  {
1737  ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
1738 
1739  /* since there is no function dependency, need to depend on type */
1741  context->addrs);
1742  }
1743  else if (IsA(node, CollateExpr))
1744  {
1745  CollateExpr *coll = (CollateExpr *) node;
1746 
1748  context->addrs);
1749  }
1750  else if (IsA(node, RowExpr))
1751  {
1752  RowExpr *rowexpr = (RowExpr *) node;
1753 
1755  context->addrs);
1756  }
1757  else if (IsA(node, RowCompareExpr))
1758  {
1759  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1760  ListCell *l;
1761 
1762  foreach(l, rcexpr->opnos)
1763  {
1765  context->addrs);
1766  }
1767  foreach(l, rcexpr->opfamilies)
1768  {
1770  context->addrs);
1771  }
1772  /* fall through to examine arguments */
1773  }
1774  else if (IsA(node, CoerceToDomain))
1775  {
1776  CoerceToDomain *cd = (CoerceToDomain *) node;
1777 
1779  context->addrs);
1780  }
1781  else if (IsA(node, OnConflictExpr))
1782  {
1783  OnConflictExpr *onconflict = (OnConflictExpr *) node;
1784 
1785  if (OidIsValid(onconflict->constraint))
1787  context->addrs);
1788  /* fall through to examine arguments */
1789  }
1790  else if (IsA(node, SortGroupClause))
1791  {
1792  SortGroupClause *sgc = (SortGroupClause *) node;
1793 
1795  context->addrs);
1796  if (OidIsValid(sgc->sortop))
1798  context->addrs);
1799  return false;
1800  }
1801  else if (IsA(node, Query))
1802  {
1803  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1804  Query *query = (Query *) node;
1805  ListCell *lc;
1806  bool result;
1807 
1808  /*
1809  * Add whole-relation refs for each plain relation mentioned in the
1810  * subquery's rtable.
1811  *
1812  * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
1813  * RTEs, subqueries, etc, so no need to do that here. But keep it
1814  * from looking at join alias lists.
1815  *
1816  * Note: we don't need to worry about collations mentioned in
1817  * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
1818  * collations referenced in other parts of the Query. We do have to
1819  * worry about collations mentioned in RTE_FUNCTION, but we take care
1820  * of those when we recurse to the RangeTblFunction node(s).
1821  */
1822  foreach(lc, query->rtable)
1823  {
1824  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
1825 
1826  switch (rte->rtekind)
1827  {
1828  case RTE_RELATION:
1830  context->addrs);
1831  break;
1832  default:
1833  break;
1834  }
1835  }
1836 
1837  /*
1838  * If the query is an INSERT or UPDATE, we should create a dependency
1839  * on each target column, to prevent the specific target column from
1840  * being dropped. Although we will visit the TargetEntry nodes again
1841  * during query_tree_walker, we won't have enough context to do this
1842  * conveniently, so do it here.
1843  */
1844  if (query->commandType == CMD_INSERT ||
1845  query->commandType == CMD_UPDATE)
1846  {
1847  RangeTblEntry *rte;
1848 
1849  if (query->resultRelation <= 0 ||
1850  query->resultRelation > list_length(query->rtable))
1851  elog(ERROR, "invalid resultRelation %d",
1852  query->resultRelation);
1853  rte = rt_fetch(query->resultRelation, query->rtable);
1854  if (rte->rtekind == RTE_RELATION)
1855  {
1856  foreach(lc, query->targetList)
1857  {
1858  TargetEntry *tle = (TargetEntry *) lfirst(lc);
1859 
1860  if (tle->resjunk)
1861  continue; /* ignore junk tlist items */
1863  context->addrs);
1864  }
1865  }
1866  }
1867 
1868  /*
1869  * Add dependencies on constraints listed in query's constraintDeps
1870  */
1871  foreach(lc, query->constraintDeps)
1872  {
1874  context->addrs);
1875  }
1876 
1877  /* query_tree_walker ignores ORDER BY etc, but we need those opers */
1878  find_expr_references_walker((Node *) query->sortClause, context);
1879  find_expr_references_walker((Node *) query->groupClause, context);
1880  find_expr_references_walker((Node *) query->windowClause, context);
1881  find_expr_references_walker((Node *) query->distinctClause, context);
1882 
1883  /* Examine substructure of query */
1884  context->rtables = lcons(query->rtable, context->rtables);
1885  result = query_tree_walker(query,
1887  (void *) context,
1889  context->rtables = list_delete_first(context->rtables);
1890  return result;
1891  }
1892  else if (IsA(node, SetOperationStmt))
1893  {
1894  SetOperationStmt *setop = (SetOperationStmt *) node;
1895 
1896  /* we need to look at the groupClauses for operator references */
1897  find_expr_references_walker((Node *) setop->groupClauses, context);
1898  /* fall through to examine child nodes */
1899  }
1900  else if (IsA(node, RangeTblFunction))
1901  {
1902  RangeTblFunction *rtfunc = (RangeTblFunction *) node;
1903  ListCell *ct;
1904 
1905  /*
1906  * Add refs for any datatypes and collations used in a column
1907  * definition list for a RECORD function. (For other cases, it should
1908  * be enough to depend on the function itself.)
1909  */
1910  foreach(ct, rtfunc->funccoltypes)
1911  {
1913  context->addrs);
1914  }
1915  foreach(ct, rtfunc->funccolcollations)
1916  {
1917  Oid collid = lfirst_oid(ct);
1918 
1919  if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
1921  context->addrs);
1922  }
1923  }
1924  else if (IsA(node, TableSampleClause))
1925  {
1926  TableSampleClause *tsc = (TableSampleClause *) node;
1927 
1929  context->addrs);
1930  /* fall through to examine arguments */
1931  }
1932  else if (IsA(node, NextValueExpr))
1933  {
1934  NextValueExpr *nve = (NextValueExpr *) node;
1935 
1937  context->addrs);
1938  }
1939 
1941  (void *) context);
1942 }
Datum constvalue
Definition: primnodes.h:196
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2267
#define REGCLASSOID
Definition: pg_type.h:577
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * joinaliasvars
Definition: parsenodes.h:967
Index varlevelsup
Definition: primnodes.h:173
List * sortClause
Definition: parsenodes.h:156
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1474
#define REGROLEOID
Definition: pg_type.h:585
ObjectAddresses * addrs
Definition: dependency.c:130
List * constraintDeps
Definition: parsenodes.h:166
#define DatumGetObjectId(X)
Definition: postgres.h:506
Oid resulttype
Definition: primnodes.h:811
List * opfamilies
Definition: primnodes.h:1032
int resultRelation
Definition: parsenodes.h:120
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
AttrNumber varattno
Definition: primnodes.h:168
return result
Definition: formatting.c:1618
List * list_copy_tail(const List *oldlist, int nskip)
Definition: list.c:1203
unsigned int Oid
Definition: postgres_ext.h:31
#define REGTYPEOID
Definition: pg_type.h:581
Definition: primnodes.h:163
#define REGOPEROID
Definition: pg_type.h:569
#define OidIsValid(objectId)
Definition: c.h:538
List * windowClause
Definition: parsenodes.h:152
List * targetList
Definition: parsenodes.h:138
Oid consttype
Definition: primnodes.h:192
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:170
bool resjunk
Definition: primnodes.h:1374
List * rtable
Definition: parsenodes.h:135
List * distinctClause
Definition: parsenodes.h:154
Oid funcid
Definition: primnodes.h:448
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Oid paramcollid
Definition: primnodes.h:248
Oid constcollid
Definition: primnodes.h:194
void * list_nth(const List *list, int n)
Definition: list.c:410
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:74
AttrNumber resno
Definition: primnodes.h:1368
#define REGDICTIONARYOID
Definition: pg_type.h:627
Oid winfnoid
Definition: primnodes.h:355
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Index varno
Definition: primnodes.h:166
Oid resulttype
Definition: primnodes.h:791
Oid resultcollid
Definition: primnodes.h:793
Oid aggfnoid
Definition: primnodes.h:294
CmdType commandType
Definition: parsenodes.h:110
List * lcons(void *datum, List *list)
Definition: list.c:259
List * funccolcollations
Definition: parsenodes.h:1053
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1864
Oid row_typeid
Definition: primnodes.h:986
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
#define REGCONFIGOID
Definition: pg_type.h:624
RTEKind rtekind
Definition: parsenodes.h:928
List * groupClause
Definition: parsenodes.h:146
int errmsg(const char *fmt,...)
Definition: elog.c:797
void list_free(List *list)
Definition: list.c:1133
Oid opno
Definition: primnodes.h:495
#define elog
Definition: elog.h:219
#define REGPROCEDUREOID
Definition: pg_type.h:565
Definition: pg_list.h:45
#define REGNAMESPACEOID
Definition: pg_type.h:589
static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId, ObjectAddresses *addrs)
Definition: dependency.c:2061
Oid paramtype
Definition: primnodes.h:246
bool constisnull
Definition: primnodes.h:197
#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 QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
static void findDependentObjects ( const ObjectAddress object,
int  objflags,
int  flags,
ObjectAddressStack stack,
ObjectAddresses targetObjects,
const ObjectAddresses pendingObjects,
Relation depRel 
)
static

Definition at line 462 of file dependency.c.

References AcquireDeletionLock(), add_exact_object_address_extra(), Anum_pg_depend_classid, Anum_pg_depend_objid, Anum_pg_depend_objsubid, Anum_pg_depend_refclassid, Anum_pg_depend_refobjid, Anum_pg_depend_refobjsubid, BTEqualStrategyNumber, ObjectAddress::classId, creating_extension, CurrentExtensionObject, DependDependerIndexId, ObjectAddressExtra::dependee, DEPENDENCY_AUTO, DEPENDENCY_AUTO_EXTENSION, DEPENDENCY_EXTENSION, DEPENDENCY_INTERNAL, DEPENDENCY_NORMAL, DEPENDENCY_PIN, DependReferenceIndexId, DEPFLAG_AUTO, DEPFLAG_EXTENSION, DEPFLAG_INTERNAL, DEPFLAG_NORMAL, DEPFLAG_REVERSE, elog, ereport, errcode(), errhint(), errmsg(), ERROR, ExtensionRelationId, ObjectAddressExtra::flags, ObjectAddressStack::flags, getObjectDescription(), GETSTRUCT, HeapTupleIsValid, Int32GetDatum, ObjectAddressStack::next, NULL, ObjectAddressStack::object, object_address_present(), object_address_present_add_flags(), ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, PERFORM_DELETION_SKIP_EXTENSIONS, ReleaseDeletionLock(), ScanKeyInit(), stack_address_present_add_flags(), systable_beginscan(), systable_endscan(), systable_getnext(), and systable_recheck_tuple().

Referenced by performDeletion(), and performMultipleDeletions().

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:
585 
586  /*
587  * This object is part of the internal implementation of
588  * another object, or is part of the extension that is the
589  * other object. We have three cases:
590  *
591  * 1. At the outermost recursion level, disallow the DROP. (We
592  * just ereport here, rather than proceeding, since no other
593  * dependencies are likely to be interesting.) However, if
594  * the owning object is listed in pendingObjects, just release
595  * the caller's lock and return; we'll eventually complete the
596  * DROP when we reach that entry in the pending list.
597  */
598  if (stack == NULL)
599  {
600  char *otherObjDesc;
601 
602  if (pendingObjects &&
603  object_address_present(&otherObject, pendingObjects))
604  {
605  systable_endscan(scan);
606  /* need to release caller's lock; see notes below */
607  ReleaseDeletionLock(object);
608  return;
609  }
610  otherObjDesc = getObjectDescription(&otherObject);
611  ereport(ERROR,
612  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
613  errmsg("cannot drop %s because %s requires it",
614  getObjectDescription(object),
615  otherObjDesc),
616  errhint("You can drop %s instead.",
617  otherObjDesc)));
618  }
619 
620  /*
621  * 2. When recursing from the other end of this dependency,
622  * it's okay to continue with the deletion. This holds when
623  * recursing from a whole object that includes the nominal
624  * other end as a component, too. Since there can be more
625  * than one "owning" object, we have to allow matches that are
626  * more than one level down in the stack.
627  */
628  if (stack_address_present_add_flags(&otherObject, 0, stack))
629  break;
630 
631  /*
632  * 3. Not all the owning objects have been visited, so
633  * transform this deletion request into a delete of this
634  * owning object.
635  *
636  * First, release caller's lock on this object and get
637  * deletion lock on the owning object. (We must release
638  * caller's lock to avoid deadlock against a concurrent
639  * deletion of the owning object.)
640  */
641  ReleaseDeletionLock(object);
642  AcquireDeletionLock(&otherObject, 0);
643 
644  /*
645  * The owning object might have been deleted while we waited
646  * to lock it; if so, neither it nor the current object are
647  * interesting anymore. We test this by checking the
648  * pg_depend entry (see notes below).
649  */
650  if (!systable_recheck_tuple(scan, tup))
651  {
652  systable_endscan(scan);
653  ReleaseDeletionLock(&otherObject);
654  return;
655  }
656 
657  /*
658  * Okay, recurse to the owning object instead of proceeding.
659  *
660  * We do not need to stack the current object; we want the
661  * traversal order to be as if the original reference had
662  * linked to the owning object instead of this one.
663  *
664  * The dependency type is a "reverse" dependency: we need to
665  * delete the owning object if this one is to be deleted, but
666  * this linkage is never a reason for an automatic deletion.
667  */
668  findDependentObjects(&otherObject,
670  flags,
671  stack,
672  targetObjects,
673  pendingObjects,
674  depRel);
675  /* And we're done here. */
676  systable_endscan(scan);
677  return;
678  case DEPENDENCY_PIN:
679 
680  /*
681  * Should not happen; PIN dependencies should have zeroes in
682  * the depender fields...
683  */
684  elog(ERROR, "incorrect use of PIN dependency with %s",
685  getObjectDescription(object));
686  break;
687  default:
688  elog(ERROR, "unrecognized dependency type '%c' for %s",
689  foundDep->deptype, getObjectDescription(object));
690  break;
691  }
692  }
693 
694  systable_endscan(scan);
695 
696  /*
697  * Now recurse to any dependent objects. We must visit them first since
698  * they have to be deleted before the current object.
699  */
700  mystack.object = object; /* set up a new stack level */
701  mystack.flags = objflags;
702  mystack.next = stack;
703 
704  ScanKeyInit(&key[0],
706  BTEqualStrategyNumber, F_OIDEQ,
707  ObjectIdGetDatum(object->classId));
708  ScanKeyInit(&key[1],
710  BTEqualStrategyNumber, F_OIDEQ,
711  ObjectIdGetDatum(object->objectId));
712  if (object->objectSubId != 0)
713  {
714  ScanKeyInit(&key[2],
716  BTEqualStrategyNumber, F_INT4EQ,
717  Int32GetDatum(object->objectSubId));
718  nkeys = 3;
719  }
720  else
721  nkeys = 2;
722 
723  scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
724  NULL, nkeys, key);
725 
726  while (HeapTupleIsValid(tup = systable_getnext(scan)))
727  {
728  Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
729  int subflags;
730 
731  otherObject.classId = foundDep->classid;
732  otherObject.objectId = foundDep->objid;
733  otherObject.objectSubId = foundDep->objsubid;
734 
735  /*
736  * Must lock the dependent object before recursing to it.
737  */
738  AcquireDeletionLock(&otherObject, 0);
739 
740  /*
741  * The dependent object might have been deleted while we waited to
742  * lock it; if so, we don't need to do anything more with it. We can
743  * test this cheaply and independently of the object's type by seeing
744  * if the pg_depend tuple we are looking at is still live. (If the
745  * object got deleted, the tuple would have been deleted too.)
746  */
747  if (!systable_recheck_tuple(scan, tup))
748  {
749  /* release the now-useless lock */
750  ReleaseDeletionLock(&otherObject);
751  /* and continue scanning for dependencies */
752  continue;
753  }
754 
755  /* Recurse, passing objflags indicating the dependency type */
756  switch (foundDep->deptype)
757  {
758  case DEPENDENCY_NORMAL:
759  subflags = DEPFLAG_NORMAL;
760  break;
761  case DEPENDENCY_AUTO:
763  subflags = DEPFLAG_AUTO;
764  break;
765  case DEPENDENCY_INTERNAL:
766  subflags = DEPFLAG_INTERNAL;
767  break;
769  subflags = DEPFLAG_EXTENSION;
770  break;
771  case DEPENDENCY_PIN:
772 
773  /*
774  * For a PIN dependency we just ereport immediately; there
775  * won't be any others to report.
776  */
777  ereport(ERROR,
778  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
779  errmsg("cannot drop %s because it is required by the database system",
780  getObjectDescription(object))));
781  subflags = 0; /* keep compiler quiet */
782  break;
783  default:
784  elog(ERROR, "unrecognized dependency type '%c' for %s",
785  foundDep->deptype, getObjectDescription(object));
786  subflags = 0; /* keep compiler quiet */
787  break;
788  }
789 
790  findDependentObjects(&otherObject,
791  subflags,
792  flags,
793  &mystack,
794  targetObjects,
795  pendingObjects,
796  depRel);
797  }
798 
799  systable_endscan(scan);
800 
801  /*
802  * Finally, we can add the target object to targetObjects. Be careful to
803  * include any flags that were passed back down to us from inner recursion
804  * levels.
805  */
806  extra.flags = mystack.flags;
807  if (stack)
808  extra.dependee = *stack->object;
809  else
810  memset(&extra.dependee, 0, sizeof(extra.dependee));
811  add_exact_object_address_extra(object, &extra, targetObjects);
812 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:462
Oid CurrentExtensionObject
Definition: extension.c:68
static void ReleaseDeletionLock(const ObjectAddress *object)
Definition: dependency.c:1313
#define Anum_pg_depend_refobjid
Definition: pg_depend.h:72
#define Anum_pg_depend_refobjsubid
Definition: pg_depend.h:73
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:656
static void add_exact_object_address_extra(const ObjectAddress *object, const ObjectAddressExtra *extra, ObjectAddresses *addrs)
Definition: dependency.c:2119
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
Definition: dependency.c:2154
#define DependReferenceIndexId
Definition: indexing.h:147
#define DependDependerIndexId
Definition: indexing.h:145
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DEPFLAG_REVERSE
Definition: dependency.c:105
struct ObjectAddressStack * next
Definition: dependency.c:124
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:328
#define Anum_pg_depend_objsubid
Definition: pg_depend.h:70
char * getObjectDescription(const ObjectAddress *object)
bool systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
Definition: genam.c:454
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define DEPFLAG_AUTO
Definition: dependency.c:102
const ObjectAddress * object
Definition: dependency.c:122
#define DEPFLAG_INTERNAL
Definition: dependency.c:103
static bool object_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddresses *addrs)
Definition: dependency.c:2180
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:61
#define ereport(elevel, rest)
Definition: elog.h:122
static void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1286
bool creating_extension
Definition: extension.c:67
#define DEPFLAG_EXTENSION
Definition: dependency.c:104
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DEPFLAG_NORMAL
Definition: dependency.c:101
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define Anum_pg_depend_classid
Definition: pg_depend.h:68
#define elog
Definition: elog.h:219
#define ExtensionRelationId
Definition: pg_extension.h:29
static bool stack_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddressStack *stack)
Definition: dependency.c:2247
#define PERFORM_DELETION_SKIP_EXTENSIONS
Definition: dependency.h:178
ObjectAddress dependee
Definition: dependency.c:96
#define Anum_pg_depend_objid
Definition: pg_depend.h:69
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void free_object_addresses ( ObjectAddresses addrs)

Definition at line 2310 of file dependency.c.

References ObjectAddresses::extras, pfree(), and ObjectAddresses::refs.

Referenced by AlterTableNamespace(), AlterTypeNamespace(), makeConfigurationDependencies(), performDeletion(), performMultipleDeletions(), recordDependencyOnExpr(), recordDependencyOnSingleRelExpr(), RemoveObjects(), RemoveRelations(), and shdepDropOwned().

2311 {
2312  pfree(addrs->refs);
2313  if (addrs->extras)
2314  pfree(addrs->extras);
2315  pfree(addrs);
2316 }
void pfree(void *pointer)
Definition: mcxt.c:950
ObjectAddress * refs
Definition: dependency.c:111
ObjectAddressExtra * extras
Definition: dependency.c:112
ObjectClass getObjectClass ( const ObjectAddress object)

Definition at line 2325 of file dependency.c.

References AccessMethodOperatorRelationId, AccessMethodProcedureRelationId, AccessMethodRelationId, AttrDefaultRelationId, AuthIdRelationId, CastRelationId, ObjectAddress::classId, CollationRelationId, ConstraintRelationId, ConversionRelationId, DatabaseRelationId, DefaultAclRelationId, elog, ERROR, EventTriggerRelationId, ExtensionRelationId, ForeignDataWrapperRelationId, ForeignServerRelationId, LanguageRelationId, LargeObjectRelationId, NamespaceRelationId, ObjectAddress::objectSubId, OCLASS_AM, OCLASS_AMOP, OCLASS_AMPROC, OCLASS_CAST, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_CONVERSION, OCLASS_DATABASE, OCLASS_DEFACL, OCLASS_DEFAULT, OCLASS_EVENT_TRIGGER, OCLASS_EXTENSION, OCLASS_FDW, OCLASS_FOREIGN_SERVER, OCLASS_LANGUAGE, OCLASS_LARGEOBJECT, OCLASS_OPCLASS, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_POLICY, OCLASS_PROC, OCLASS_PUBLICATION, OCLASS_PUBLICATION_REL, OCLASS_REWRITE, OCLASS_ROLE, OCLASS_SCHEMA, OCLASS_STATISTIC_EXT, OCLASS_SUBSCRIPTION, OCLASS_TBLSPACE, OCLASS_TRANSFORM, OCLASS_TRIGGER, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TSPARSER, OCLASS_TSTEMPLATE, OCLASS_TYPE, OCLASS_USER_MAPPING, OperatorClassRelationId, OperatorFamilyRelationId, OperatorRelationId, PolicyRelationId, ProcedureRelationId, PublicationRelationId, PublicationRelRelationId, RelationRelationId, RewriteRelationId, StatisticExtRelationId, SubscriptionRelationId, TableSpaceRelationId, TransformRelationId, TriggerRelationId, TSConfigRelationId, TSDictionaryRelationId, TSParserRelationId, TSTemplateRelationId, TypeRelationId, and UserMappingRelationId.

Referenced by AlterObjectNamespace_oid(), ATExecAlterColumnType(), deleteObjectsInList(), doDeletion(), EventTriggerSQLDropAddObject(), getObjectDescription(), getObjectIdentityParts(), and getObjectTypeDescription().

2326 {
2327  /* only pg_class entries can have nonzero objectSubId */
2328  if (object->classId != RelationRelationId &&
2329  object->objectSubId != 0)
2330  elog(ERROR, "invalid non-zero objectSubId for object class %u",
2331  object->classId);
2332 
2333  switch (object->classId)
2334  {
2335  case RelationRelationId:
2336  /* caller must check objectSubId */
2337  return OCLASS_CLASS;
2338 
2339  case ProcedureRelationId:
2340  return OCLASS_PROC;
2341 
2342  case TypeRelationId:
2343  return OCLASS_TYPE;
2344 
2345  case CastRelationId:
2346  return OCLASS_CAST;
2347 
2348  case CollationRelationId:
2349  return OCLASS_COLLATION;
2350 
2351  case ConstraintRelationId:
2352  return OCLASS_CONSTRAINT;
2353 
2354  case ConversionRelationId:
2355  return OCLASS_CONVERSION;
2356 
2357  case AttrDefaultRelationId:
2358  return OCLASS_DEFAULT;
2359 
2360  case LanguageRelationId:
2361  return OCLASS_LANGUAGE;
2362 
2363  case LargeObjectRelationId:
2364  return OCLASS_LARGEOBJECT;
2365 
2366  case OperatorRelationId:
2367  return OCLASS_OPERATOR;
2368 
2370  return OCLASS_OPCLASS;
2371 
2373  return OCLASS_OPFAMILY;
2374 
2376  return OCLASS_AM;
2377 
2379  return OCLASS_AMOP;
2380 
2382  return OCLASS_AMPROC;
2383 
2384  case RewriteRelationId:
2385  return OCLASS_REWRITE;
2386 
2387  case TriggerRelationId:
2388  return OCLASS_TRIGGER;
2389 
2390  case NamespaceRelationId:
2391  return OCLASS_SCHEMA;
2392 
2394  return OCLASS_STATISTIC_EXT;
2395 
2396  case TSParserRelationId:
2397  return OCLASS_TSPARSER;
2398 
2400  return OCLASS_TSDICT;
2401 
2402  case TSTemplateRelationId:
2403  return OCLASS_TSTEMPLATE;
2404 
2405  case TSConfigRelationId:
2406  return OCLASS_TSCONFIG;
2407 
2408  case AuthIdRelationId:
2409  return OCLASS_ROLE;
2410 
2411  case DatabaseRelationId:
2412  return OCLASS_DATABASE;
2413 
2414  case TableSpaceRelationId:
2415  return OCLASS_TBLSPACE;
2416 
2418  return OCLASS_FDW;
2419 
2421  return OCLASS_FOREIGN_SERVER;
2422 
2423  case UserMappingRelationId:
2424  return OCLASS_USER_MAPPING;
2425 
2426  case DefaultAclRelationId:
2427  return OCLASS_DEFACL;
2428 
2429  case ExtensionRelationId:
2430  return OCLASS_EXTENSION;
2431 
2433  return OCLASS_EVENT_TRIGGER;
2434 
2435  case PolicyRelationId:
2436  return OCLASS_POLICY;
2437 
2438  case PublicationRelationId:
2439  return OCLASS_PUBLICATION;
2440 
2442  return OCLASS_PUBLICATION_REL;
2443 
2445  return OCLASS_SUBSCRIPTION;
2446 
2447  case TransformRelationId:
2448  return OCLASS_TRANSFORM;
2449  }
2450 
2451  /* shouldn't get here */
2452  elog(ERROR, "unrecognized object class: %u", object->classId);
2453  return OCLASS_CLASS; /* keep compiler quiet */
2454 }
#define NamespaceRelationId
Definition: pg_namespace.h:34
#define OperatorRelationId
Definition: pg_operator.h:32
#define TransformRelationId
Definition: pg_transform.h:25
#define ProcedureRelationId
Definition: pg_proc.h:33
#define DatabaseRelationId
Definition: pg_database.h:29
#define RelationRelationId
Definition: pg_class.h:29
#define OperatorClassRelationId
Definition: pg_opclass.h:49
#define OperatorFamilyRelationId
Definition: pg_opfamily.h:29
#define PublicationRelationId
#define TypeRelationId
Definition: pg_type.h:34
#define DefaultAclRelationId
#define AccessMethodOperatorRelationId
Definition: pg_amop.h:54
#define PublicationRelRelationId
#define AuthIdRelationId
Definition: pg_authid.h:42
#define TSConfigRelationId
Definition: pg_ts_config.h:31
#define ERROR
Definition: elog.h:43
#define TSDictionaryRelationId
Definition: pg_ts_dict.h:31
#define SubscriptionRelationId
#define AccessMethodRelationId
Definition: pg_am.h:32
#define TSParserRelationId
Definition: pg_ts_parser.h:31
#define CollationRelationId
Definition: pg_collation.h:30
#define PolicyRelationId
Definition: pg_policy.h:19
#define RewriteRelationId
Definition: pg_rewrite.h:32
#define ForeignServerRelationId
#define StatisticExtRelationId
#define EventTriggerRelationId
#define TriggerRelationId
Definition: pg_trigger.h:34
#define CastRelationId
Definition: pg_cast.h:31
#define ForeignDataWrapperRelationId
#define TableSpaceRelationId
Definition: pg_tablespace.h:29
#define UserMappingRelationId
#define LanguageRelationId
Definition: pg_language.h:29
#define ConstraintRelationId
Definition: pg_constraint.h:29
#define elog
Definition: elog.h:219
#define ExtensionRelationId
Definition: pg_extension.h:29
#define AttrDefaultRelationId
Definition: pg_attrdef.h:29
#define LargeObjectRelationId
#define AccessMethodProcedureRelationId
Definition: pg_amproc.h:43
#define ConversionRelationId
Definition: pg_conversion.h:38
#define TSTemplateRelationId
ObjectAddresses* new_object_addresses ( void  )

Definition at line 2039 of file dependency.c.

References ObjectAddresses::extras, ObjectAddresses::maxrefs, NULL, ObjectAddresses::numrefs, palloc(), and ObjectAddresses::refs.

Referenced by AlterExtensionNamespace(), AlterTableNamespace(), AlterTypeNamespace(), makeConfigurationDependencies(), performDeletion(), performMultipleDeletions(), recordDependencyOnExpr(), recordDependencyOnSingleRelExpr(), RemoveObjects(), RemoveRelations(), and shdepDropOwned().

2040 {
2041  ObjectAddresses *addrs;
2042 
2043  addrs = palloc(sizeof(ObjectAddresses));
2044 
2045  addrs->numrefs = 0;
2046  addrs->maxrefs = 32;
2047  addrs->refs = (ObjectAddress *)
2048  palloc(addrs->maxrefs * sizeof(ObjectAddress));
2049  addrs->extras = NULL; /* until/unless needed */
2050 
2051  return addrs;
2052 }
ObjectAddress * refs
Definition: dependency.c:111
#define NULL
Definition: c.h:229
ObjectAddressExtra * extras
Definition: dependency.c:112
void * palloc(Size size)
Definition: mcxt.c:849
static int object_address_comparator ( const void *  a,
const void *  b 
)
static

Definition at line 2008 of file dependency.c.

References ObjectAddress::classId, ObjectAddress::objectId, and ObjectAddress::objectSubId.

Referenced by eliminate_duplicate_dependencies().

2009 {
2010  const ObjectAddress *obja = (const ObjectAddress *) a;
2011  const ObjectAddress *objb = (const ObjectAddress *) b;
2012 
2013  if (obja->classId < objb->classId)
2014  return -1;
2015  if (obja->classId > objb->classId)
2016  return 1;
2017  if (obja->objectId < objb->objectId)
2018  return -1;
2019  if (obja->objectId > objb->objectId)
2020  return 1;
2021 
2022  /*
2023  * We sort the subId as an unsigned int so that 0 will come first. See
2024  * logic in eliminate_duplicate_dependencies.
2025  */
2026  if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2027  return -1;
2028  if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2029  return 1;
2030  return 0;
2031 }
bool object_address_present ( const ObjectAddress object,
const ObjectAddresses addrs 
)

Definition at line 2154 of file dependency.c.

References ObjectAddress::classId, i, ObjectAddresses::numrefs, ObjectAddress::objectId, ObjectAddress::objectSubId, and ObjectAddresses::refs.

Referenced by AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterTypeNamespaceInternal(), and findDependentObjects().

2156 {
2157  int i;
2158 
2159  for (i = addrs->numrefs - 1; i >= 0; i--)
2160  {
2161  const ObjectAddress *thisobj = addrs->refs + i;
2162 
2163  if (object->classId == thisobj->classId &&
2164  object->objectId == thisobj->objectId)
2165  {
2166  if (object->objectSubId == thisobj->objectSubId ||
2167  thisobj->objectSubId == 0)
2168  return true;
2169  }
2170  }
2171 
2172  return false;
2173 }
ObjectAddress * refs
Definition: dependency.c:111
int i
static bool object_address_present_add_flags ( const ObjectAddress object,
int  flags,
ObjectAddresses addrs 
)
static

Definition at line 2180 of file dependency.c.

References ObjectAddress::classId, ObjectAddresses::extras, ObjectAddressExtra::flags, ObjectAddressStack::flags, i, ObjectAddresses::numrefs, ObjectAddress::objectId, ObjectAddress::objectSubId, ObjectAddresses::refs, and result.

Referenced by findDependentObjects().

2183 {
2184  bool result = false;
2185  int i;
2186 
2187  for (i = addrs->numrefs - 1; i >= 0; i--)
2188  {
2189  ObjectAddress *thisobj = addrs->refs + i;
2190 
2191  if (object->classId == thisobj->classId &&
2192  object->objectId == thisobj->objectId)
2193  {
2194  if (object->objectSubId == thisobj->objectSubId)
2195  {
2196  ObjectAddressExtra *thisextra = addrs->extras + i;
2197 
2198  thisextra->flags |= flags;
2199  result = true;
2200  }
2201  else if (thisobj->objectSubId == 0)
2202  {
2203  /*
2204  * We get here if we find a need to delete a column after
2205  * having already decided to drop its whole table. Obviously
2206  * we no longer need to drop the subobject, so report that we
2207  * found the subobject in the array. But don't plaster its
2208  * flags on the whole object.
2209  */
2210  result = true;
2211  }
2212  else if (object->objectSubId == 0)
2213  {
2214  /*
2215  * We get here if we find a need to delete a whole table after
2216  * having already decided to drop one of its columns. We
2217  * can't report that the whole object is in the array, but we
2218  * should mark the subobject with the whole object's flags.
2219  *
2220  * It might seem attractive to physically delete the column's
2221  * array entry, or at least mark it as no longer needing
2222  * separate deletion. But that could lead to, e.g., dropping
2223  * the column's datatype before we drop the table, which does
2224  * not seem like a good idea. This is a very rare situation
2225  * in practice, so we just take the hit of doing a separate
2226  * DROP COLUMN action even though we know we're gonna delete
2227  * the table later.
2228  *
2229  * Because there could be other subobjects of this object in
2230  * the array, this case means we always have to loop through
2231  * the whole array; we cannot exit early on a match.
2232  */
2233  ObjectAddressExtra *thisextra = addrs->extras + i;
2234 
2235  thisextra->flags |= flags;
2236  }
2237  }
2238  }
2239 
2240  return result;
2241 }
return result
Definition: formatting.c:1618
ObjectAddress * refs
Definition: dependency.c:111
ObjectAddressExtra * extras
Definition: dependency.c:112
int i
void performDeletion ( const ObjectAddress object,
DropBehavior  behavior,
int  flags 
)

Definition at line 303 of file dependency.c.

References AcquireDeletionLock(), deleteObjectsInList(), DependRelationId, DEPFLAG_ORIGINAL, findDependentObjects(), free_object_addresses(), heap_close, heap_open(), new_object_addresses(), NULL, reportDependentObjects(), and RowExclusiveLock.

Referenced by AlterDomainDropConstraint(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropIdentity(), ATPostAlterTypeCleanup(), DefineQueryRewrite(), do_autovacuum(), dropOperators(), dropProcedures(), finish_heap_swap(), inv_drop(), PreCommit_on_commit_actions(), PublicationDropTables(), RemoveAttrDefault(), RemoveTempRelations(), RemoveUserMapping(), and SetDefaultACL().

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 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:462
#define DependRelationId
Definition: pg_depend.h:29
#define heap_close(r, l)
Definition: heapam.h:97
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2039
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2310
#define DEPFLAG_ORIGINAL
Definition: dependency.c:100
#define RowExclusiveLock
Definition: lockdefs.h:38
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:828
static void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1286
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define NULL
Definition: c.h:229
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:219
void performMultipleDeletions ( const ObjectAddresses objects,
DropBehavior  behavior,
int  flags 
)

Definition at line 362 of file dependency.c.

References AcquireDeletionLock(), deleteObjectsInList(), DependRelationId, DEPFLAG_ORIGINAL, findDependentObjects(), free_object_addresses(), heap_close, heap_open(), i, new_object_addresses(), NULL, ObjectAddresses::numrefs, ObjectAddresses::refs, reportDependentObjects(), and RowExclusiveLock.

Referenced by RemoveObjects(), RemoveRelations(), and shdepDropOwned().

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 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:462
#define DependRelationId
Definition: pg_depend.h:29
#define heap_close(r, l)
Definition: heapam.h:97
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2039
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2310
#define DEPFLAG_ORIGINAL
Definition: dependency.c:100
ObjectAddress * refs
Definition: dependency.c:111
#define RowExclusiveLock
Definition: lockdefs.h:38
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:828
static void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1286
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define NULL
Definition: c.h:229
int i
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:219
void record_object_address_dependencies ( const ObjectAddress depender,
ObjectAddresses referenced,
DependencyType  behavior 
)

Definition at line 2296 of file dependency.c.

References eliminate_duplicate_dependencies(), ObjectAddresses::numrefs, recordMultipleDependencies(), and ObjectAddresses::refs.

Referenced by makeConfigurationDependencies().

2299 {
2301  recordMultipleDependencies(depender,
2302  referenced->refs, referenced->numrefs,
2303  behavior);
2304 }
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:1948
ObjectAddress * refs
Definition: dependency.c:111
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:56
void recordDependencyOnExpr ( const ObjectAddress depender,
Node expr,
List rtable,
DependencyType  behavior 
)

Definition at line 1338 of file dependency.c.

References find_expr_references_context::addrs, eliminate_duplicate_dependencies(), find_expr_references_walker(), free_object_addresses(), list_make1, new_object_addresses(), ObjectAddresses::numrefs, recordMultipleDependencies(), ObjectAddresses::refs, and find_expr_references_context::rtables.

Referenced by AlterPolicy(), CreatePolicy(), CreateTrigger(), GenerateTypeDependencies(), InsertRule(), ProcedureCreate(), RemoveRoleFromObjectPolicy(), and StoreAttrDefault().

1341 {
1343 
1344  context.addrs = new_object_addresses();
1345 
1346  /* Set up interpretation for Vars at varlevelsup = 0 */
1347  context.rtables = list_make1(rtable);
1348 
1349  /* Scan the expression tree for referenceable objects */
1350  find_expr_references_walker(expr, &context);
1351 
1352  /* Remove any duplicates */
1354 
1355  /* And record 'em */
1356  recordMultipleDependencies(depender,
1357  context.addrs->refs, context.addrs->numrefs,
1358  behavior);
1359 
1360  free_object_addresses(context.addrs);
1361 }
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1474
ObjectAddresses * addrs
Definition: dependency.c:130
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2039
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2310
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:1948
#define list_make1(x1)
Definition: pg_list.h:139
ObjectAddress * refs
Definition: dependency.c:111
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:56
void recordDependencyOnSingleRelExpr ( const ObjectAddress depender,
Node expr,
Oid  relId,
DependencyType  behavior,
DependencyType  self_behavior,
bool  ignore_self 
)

Definition at line 1379 of file dependency.c.

References add_exact_object_address(), find_expr_references_context::addrs, ObjectAddress::classId, eliminate_duplicate_dependencies(), find_expr_references_walker(), free_object_addresses(), list_make1, MemSet, new_object_addresses(), ObjectAddresses::numrefs, ObjectAddress::objectId, recordMultipleDependencies(), ObjectAddresses::refs, RelationRelationId, RangeTblEntry::relid, RangeTblEntry::relkind, RELKIND_RELATION, find_expr_references_context::rtables, RTE_RELATION, RangeTblEntry::rtekind, T_RangeTblEntry, and RangeTblEntry::type.

Referenced by CreateConstraintEntry(), index_create(), and StorePartitionKey().

1384 {
1386  RangeTblEntry rte;
1387 
1388  context.addrs = new_object_addresses();
1389 
1390  /* We gin up a rather bogus rangetable list to handle Vars */
1391  MemSet(&rte, 0, sizeof(rte));
1392  rte.type = T_RangeTblEntry;
1393  rte.rtekind = RTE_RELATION;
1394  rte.relid = relId;
1395  rte.relkind = RELKIND_RELATION; /* no need for exactness here */
1396 
1397  context.rtables = list_make1(list_make1(&rte));
1398 
1399  /* Scan the expression tree for referenceable objects */
1400  find_expr_references_walker(expr, &context);
1401 
1402  /* Remove any duplicates */
1404 
1405  /* Separate self-dependencies if necessary */
1406  if (behavior != self_behavior && context.addrs->numrefs > 0)
1407  {
1408  ObjectAddresses *self_addrs;
1409  ObjectAddress *outobj;
1410  int oldref,
1411  outrefs;
1412 
1413  self_addrs = new_object_addresses();
1414 
1415  outobj = context.addrs->refs;
1416  outrefs = 0;
1417  for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1418  {
1419  ObjectAddress *thisobj = context.addrs->refs + oldref;
1420 
1421  if (thisobj->classId == RelationRelationId &&
1422  thisobj->objectId == relId)
1423  {
1424  /* Move this ref into self_addrs */
1425  add_exact_object_address(thisobj, self_addrs);
1426  }
1427  else
1428  {
1429  /* Keep it in context.addrs */
1430  *outobj = *thisobj;
1431  outobj++;
1432  outrefs++;
1433  }
1434  }
1435  context.addrs->numrefs = outrefs;
1436 
1437  /* Record the self-dependencies */
1438  if (!ignore_self)
1439  recordMultipleDependencies(depender,
1440  self_addrs->refs, self_addrs->numrefs,
1441  self_behavior);
1442 
1443  free_object_addresses(self_addrs);
1444  }
1445 
1446  /* Record the external dependencies */
1447  recordMultipleDependencies(depender,
1448  context.addrs->refs, context.addrs->numrefs,
1449  behavior);
1450 
1451  free_object_addresses(context.addrs);
1452 }
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1474
ObjectAddresses * addrs
Definition: dependency.c:130
#define RelationRelationId
Definition: pg_class.h:29
#define MemSet(start, val, len)
Definition: c.h:857
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2094
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2039
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2310
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:1948
#define list_make1(x1)
Definition: pg_list.h:139
ObjectAddress * refs
Definition: dependency.c:111
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:56
RTEKind rtekind
Definition: parsenodes.h:928
NodeTag type
Definition: parsenodes.h:926
#define RELKIND_RELATION
Definition: pg_class.h:160
static void ReleaseDeletionLock ( const ObjectAddress object)
static

Definition at line 1313 of file dependency.c.

References AccessExclusiveLock, ObjectAddress::classId, ObjectAddress::objectId, RelationRelationId, UnlockDatabaseObject(), and UnlockRelationOid().

Referenced by findDependentObjects().

1314 {
1315  if (object->classId == RelationRelationId)
1317  else
1318  /* assume we should lock the whole object not a sub-object */
1319  UnlockDatabaseObject(object->classId, object->objectId, 0,
1321 }
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
#define RelationRelationId
Definition: pg_class.h:29
void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:851
#define AccessExclusiveLock
Definition: lockdefs.h:46
static void reportDependentObjects ( const ObjectAddresses targetObjects,
DropBehavior  behavior,
int  flags,
const ObjectAddress origObject 
)
static

Definition at line 828 of file dependency.c.

References _, appendStringInfo(), appendStringInfoChar(), client_min_messages, StringInfoData::data, DEBUG2, ObjectAddressExtra::dependee, DEPFLAG_AUTO, DEPFLAG_EXTENSION, DEPFLAG_INTERNAL, DEPFLAG_ORIGINAL, DROP_CASCADE, DROP_RESTRICT, ereport, errcode(), errdetail(), errdetail_log(), errhint(), errmsg(), errmsg_internal(), errmsg_plural(), ERROR, ObjectAddresses::extras, ObjectAddressExtra::flags, getObjectDescription(), i, initStringInfo(), StringInfoData::len, LOG, log_min_messages, MAX_REPORTED_DEPS, ngettext, NOTICE, ObjectAddresses::numrefs, PERFORM_DELETION_QUIETLY, pfree(), and ObjectAddresses::refs.

Referenced by performDeletion(), and performMultipleDeletions().

832 {
833  int msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
834  bool ok = true;
835  StringInfoData clientdetail;
836  StringInfoData logdetail;
837  int numReportedClient = 0;
838  int numNotReportedClient = 0;
839  int i;
840 
841  /*
842  * If no error is to be thrown, and the msglevel is too low to be shown to
843  * either client or server log, there's no need to do any of the work.
844  *
845  * Note: this code doesn't know all there is to be known about elog
846  * levels, but it works for NOTICE and DEBUG2, which are the only values
847  * msglevel can currently have. We also assume we are running in a normal
848  * operating environment.
849  */
850  if (behavior == DROP_CASCADE &&
851  msglevel < client_min_messages &&
852  (msglevel < log_min_messages || log_min_messages == LOG))
853  return;
854 
855  /*
856  * We limit the number of dependencies reported to the client to
857  * MAX_REPORTED_DEPS, since client software may not deal well with
858  * enormous error strings. The server log always gets a full report.
859  */
860 #define MAX_REPORTED_DEPS 100
861 
862  initStringInfo(&clientdetail);
863  initStringInfo(&logdetail);
864 
865  /*
866  * We process the list back to front (ie, in dependency order not deletion
867  * order), since this makes for a more understandable display.
868  */
869  for (i = targetObjects->numrefs - 1; i >= 0; i--)
870  {
871  const ObjectAddress *obj = &targetObjects->refs[i];
872  const ObjectAddressExtra *extra = &targetObjects->extras[i];
873  char *objDesc;
874 
875  /* Ignore the original deletion target(s) */
876  if (extra->flags & DEPFLAG_ORIGINAL)
877  continue;
878 
879  objDesc = getObjectDescription(obj);
880 
881  /*
882  * If, at any stage of the recursive search, we reached the object via
883  * an AUTO, INTERNAL, or EXTENSION dependency, then it's okay to
884  * delete it even in RESTRICT mode.
885  */
886  if (extra->flags & (DEPFLAG_AUTO |
889  {
890  /*
891  * auto-cascades are reported at DEBUG2, not msglevel. We don't
892  * try to combine them with the regular message because the
893  * results are too confusing when client_min_messages and
894  * log_min_messages are different.
895  */
896  ereport(DEBUG2,
897  (errmsg("drop auto-cascades to %s",
898  objDesc)));
899  }
900  else if (behavior == DROP_RESTRICT)
901  {
902  char *otherDesc = getObjectDescription(&extra->dependee);
903 
904  if (numReportedClient < MAX_REPORTED_DEPS)
905  {
906  /* separate entries with a newline */
907  if (clientdetail.len != 0)
908  appendStringInfoChar(&clientdetail, '\n');
909  appendStringInfo(&clientdetail, _("%s depends on %s"),
910  objDesc, otherDesc);
911  numReportedClient++;
912  }
913  else
914  numNotReportedClient++;
915  /* separate entries with a newline */
916  if (logdetail.len != 0)
917  appendStringInfoChar(&logdetail, '\n');
918  appendStringInfo(&logdetail, _("%s depends on %s"),
919  objDesc, otherDesc);
920  pfree(otherDesc);
921  ok = false;
922  }
923  else
924  {
925  if (numReportedClient < MAX_REPORTED_DEPS)
926  {
927  /* separate entries with a newline */
928  if (clientdetail.len != 0)
929  appendStringInfoChar(&clientdetail, '\n');
930  appendStringInfo(&clientdetail, _("drop cascades to %s"),
931  objDesc);
932  numReportedClient++;
933  }
934  else
935  numNotReportedClient++;
936  /* separate entries with a newline */
937  if (logdetail.len != 0)
938  appendStringInfoChar(&logdetail, '\n');
939  appendStringInfo(&logdetail, _("drop cascades to %s"),
940  objDesc);
941  }
942 
943  pfree(objDesc);
944  }
945 
946  if (numNotReportedClient > 0)
947  appendStringInfo(&clientdetail, ngettext("\nand %d other object "
948  "(see server log for list)",
949  "\nand %d other objects "
950  "(see server log for list)",
951  numNotReportedClient),
952  numNotReportedClient);
953 
954  if (!ok)
955  {
956  if (origObject)
957  ereport(ERROR,
958  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
959  errmsg("cannot drop %s because other objects depend on it",
960  getObjectDescription(origObject)),
961  errdetail("%s", clientdetail.data),
962  errdetail_log("%s", logdetail.data),
963  errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
964  else
965  ereport(ERROR,
966  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
967  errmsg("cannot drop desired object(s) because other objects depend on them"),
968  errdetail("%s", clientdetail.data),
969  errdetail_log("%s", logdetail.data),
970  errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
971  }
972  else if (numReportedClient > 1)
973  {
974  ereport(msglevel,
975  /* translator: %d always has a value larger than 1 */
976  (errmsg_plural("drop cascades to %d other object",
977  "drop cascades to %d other objects",
978  numReportedClient + numNotReportedClient,
979  numReportedClient + numNotReportedClient),
980  errdetail("%s", clientdetail.data),
981  errdetail_log("%s", logdetail.data)));
982  }
983  else if (numReportedClient == 1)
984  {
985  /* we just use the single item as-is */
986  ereport(msglevel,
987  (errmsg_internal("%s", clientdetail.data)));
988  }
989 
990  pfree(clientdetail.data);
991  pfree(logdetail.data);
992 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:850
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LOG
Definition: elog.h:26
char * getObjectDescription(const ObjectAddress *object)
void pfree(void *pointer)
Definition: mcxt.c:950
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define ERROR
Definition: elog.h:43
#define DEPFLAG_ORIGINAL
Definition: dependency.c:100
#define DEPFLAG_AUTO
Definition: dependency.c:102
#define DEPFLAG_INTERNAL
Definition: dependency.c:103
#define DEBUG2
Definition: elog.h:24
ObjectAddress * refs
Definition: dependency.c:111
int errdetail(const char *fmt,...)
Definition: elog.c:873
int errdetail_log(const char *fmt,...)
Definition: elog.c:921
#define ereport(elevel, rest)
Definition: elog.h:122
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:201
void initStringInfo(StringInfo str)
Definition: stringinfo.c:65
#define ngettext(s, p, n)
Definition: c.h:127
int log_min_messages
Definition: guc.c:455
#define DEPFLAG_EXTENSION
Definition: dependency.c:104
#define NOTICE
Definition: elog.h:37
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define PERFORM_DELETION_QUIETLY
Definition: dependency.h:176
ObjectAddressExtra * extras
Definition: dependency.c:112
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
int client_min_messages
Definition: guc.c:456
#define MAX_REPORTED_DEPS
ObjectAddress dependee
Definition: dependency.c:96
#define _(x)
Definition: elog.c:84
static bool stack_address_present_add_flags ( const ObjectAddress object,
int  flags,
ObjectAddressStack stack 
)
static

Definition at line 2247 of file dependency.c.

References ObjectAddress::classId, ObjectAddressStack::flags, ObjectAddressStack::next, ObjectAddressStack::object, ObjectAddress::objectId, ObjectAddress::objectSubId, and result.

Referenced by findDependentObjects().

2250 {
2251  bool result = false;
2252  ObjectAddressStack *stackptr;
2253 
2254  for (stackptr = stack; stackptr; stackptr = stackptr->next)
2255  {
2256  const ObjectAddress *thisobj = stackptr->object;
2257 
2258  if (object->classId == thisobj->classId &&
2259  object->objectId == thisobj->objectId)
2260  {
2261  if (object->objectSubId == thisobj->objectSubId)
2262  {
2263  stackptr->flags |= flags;
2264  result = true;
2265  }
2266  else if (thisobj->objectSubId == 0)
2267  {
2268  /*
2269  * We're visiting a column with whole table already on stack.
2270  * As in object_address_present_add_flags(), we can skip
2271  * further processing of the subobject, but we don't want to
2272  * propagate flags for the subobject to the whole object.
2273  */
2274  result = true;
2275  }
2276  else if (object->objectSubId == 0)
2277  {
2278  /*
2279  * We're visiting a table with column already on stack. As in
2280  * object_address_present_add_flags(), we should propagate
2281  * flags for the whole object to each of its subobjects.
2282  */
2283  stackptr->flags |= flags;
2284  }
2285  }
2286  }
2287 
2288  return result;
2289 }
return result
Definition: formatting.c:1618
struct ObjectAddressStack * next
Definition: dependency.c:124
const ObjectAddress * object
Definition: dependency.c:122

Variable Documentation

const Oid object_classes[]
static

Definition at line 138 of file dependency.c.

Referenced by add_object_address().