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_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 101 of file dependency.c.

Referenced by findDependentObjects(), and reportDependentObjects().

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

Definition at line 103 of file dependency.c.

Referenced by findDependentObjects(), and reportDependentObjects().

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

Definition at line 102 of file dependency.c.

Referenced by findDependentObjects(), and reportDependentObjects().

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

Definition at line 100 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 104 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 1280 of file dependency.c.

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

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

1281 {
1282  if (object->classId == RelationRelationId)
1283  {
1284  /*
1285  * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1286  * the index for the moment. index_drop() will promote the lock once
1287  * it's safe to do so. In all other cases we need full exclusive
1288  * lock.
1289  */
1290  if (flags & PERFORM_DELETION_CONCURRENTLY)
1292  else
1294  }
1295  else
1296  {
1297  /* assume we should lock the whole object not a sub-object */
1298  LockDatabaseObject(object->classId, object->objectId, 0,
1300  }
1301 }
#define RelationRelationId
Definition: pg_class.h:29
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:174
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 2081 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().

2083 {
2084  ObjectAddress *item;
2085 
2086  /* enlarge array if needed */
2087  if (addrs->numrefs >= addrs->maxrefs)
2088  {
2089  addrs->maxrefs *= 2;
2090  addrs->refs = (ObjectAddress *)
2091  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2092  Assert(!addrs->extras);
2093  }
2094  /* record this item */
2095  item = addrs->refs + addrs->numrefs;
2096  *item = *object;
2097  addrs->numrefs++;
2098 }
ObjectAddress * refs
Definition: dependency.c:110
#define Assert(condition)
Definition: c.h:671
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
ObjectAddressExtra * extras
Definition: dependency.c:111
static void add_exact_object_address_extra ( const ObjectAddress object,
const ObjectAddressExtra extra,
ObjectAddresses addrs 
)
static

Definition at line 2106 of file dependency.c.

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

Referenced by findDependentObjects().

2109 {
2110  ObjectAddress *item;
2111  ObjectAddressExtra *itemextra;
2112 
2113  /* allocate extra space if first time */
2114  if (!addrs->extras)
2115  addrs->extras = (ObjectAddressExtra *)
2116  palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
2117 
2118  /* enlarge array if needed */
2119  if (addrs->numrefs >= addrs->maxrefs)
2120  {
2121  addrs->maxrefs *= 2;
2122  addrs->refs = (ObjectAddress *)
2123  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2124  addrs->extras = (ObjectAddressExtra *)
2125  repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
2126  }
2127  /* record this item */
2128  item = addrs->refs + addrs->numrefs;
2129  *item = *object;
2130  itemextra = addrs->extras + addrs->numrefs;
2131  *itemextra = *extra;
2132  addrs->numrefs++;
2133 }
ObjectAddress * refs
Definition: dependency.c:110
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
ObjectAddressExtra * extras
Definition: dependency.c:111
void * palloc(Size size)
Definition: mcxt.c:891
static void add_object_address ( ObjectClass  oclass,
Oid  objectId,
int32  subId,
ObjectAddresses addrs 
)
static

Definition at line 2048 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().

2050 {
2051  ObjectAddress *item;
2052 
2053  /*
2054  * Make sure object_classes is kept up to date with the ObjectClass enum.
2055  */
2057  "object_classes[] must cover all ObjectClasses");
2058 
2059  /* enlarge array if needed */
2060  if (addrs->numrefs >= addrs->maxrefs)
2061  {
2062  addrs->maxrefs *= 2;
2063  addrs->refs = (ObjectAddress *)
2064  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2065  Assert(!addrs->extras);
2066  }
2067  /* record this item */
2068  item = addrs->refs + addrs->numrefs;
2069  item->classId = object_classes[oclass];
2070  item->objectId = objectId;
2071  item->objectSubId = subId;
2072  addrs->numrefs++;
2073 }
#define LAST_OCLASS
Definition: dependency.h:170
#define lengthof(array)
Definition: c.h:558
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:753
static const Oid object_classes[]
Definition: dependency.c:137
ObjectAddress * refs
Definition: dependency.c:110
#define Assert(condition)
Definition: c.h:671
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
ObjectAddressExtra * extras
Definition: dependency.c:111
static void DeleteInitPrivs ( const ObjectAddress object)
static

Definition at line 2444 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().

2445 {
2446  Relation relation;
2447  ScanKeyData key[3];
2448  SysScanDesc scan;
2449  HeapTuple oldtuple;
2450 
2452 
2453  ScanKeyInit(&key[0],
2455  BTEqualStrategyNumber, F_OIDEQ,
2456  ObjectIdGetDatum(object->objectId));
2457  ScanKeyInit(&key[1],
2459  BTEqualStrategyNumber, F_OIDEQ,
2460  ObjectIdGetDatum(object->classId));
2461  ScanKeyInit(&key[2],
2463  BTEqualStrategyNumber, F_INT4EQ,
2464  Int32GetDatum(object->objectSubId));
2465 
2466  scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
2467  NULL, 3, key);
2468 
2469  while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
2470  CatalogTupleDelete(relation, &oldtuple->t_self);
2471 
2472  systable_endscan(scan);
2473 
2474  heap_close(relation, RowExclusiveLock);
2475 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:495
#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:324
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:412
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
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:1287
#define Anum_pg_init_privs_objsubid
Definition: pg_init_privs.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Int32GetDatum(X)
Definition: postgres.h:487
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 217 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().

219 {
220  int i;
221 
222  /*
223  * Keep track of objects for event triggers, if necessary.
224  */
226  {
227  for (i = 0; i < targetObjects->numrefs; i++)
228  {
229  const ObjectAddress *thisobj = &targetObjects->refs[i];
230  const ObjectAddressExtra *extra = &targetObjects->extras[i];
231  bool original = false;
232  bool normal = false;
233 
234  if (extra->flags & DEPFLAG_ORIGINAL)
235  original = true;
236  if (extra->flags & DEPFLAG_NORMAL)
237  normal = true;
238  if (extra->flags & DEPFLAG_REVERSE)
239  normal = true;
240 
242  {
243  EventTriggerSQLDropAddObject(thisobj, original, normal);
244  }
245  }
246  }
247 
248  /*
249  * Delete all the objects in the proper order, except that if told to, we
250  * should skip the original object(s).
251  */
252  for (i = 0; i < targetObjects->numrefs; i++)
253  {
254  ObjectAddress *thisobj = targetObjects->refs + i;
255  ObjectAddressExtra *thisextra = targetObjects->extras + i;
256 
257  if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
258  (thisextra->flags & DEPFLAG_ORIGINAL))
259  continue;
260 
261  deleteOneObject(thisobj, depRel, flags);
262  }
263 }
static void deleteOneObject(const ObjectAddress *object, Relation *depRel, int32 flags)
bool trackDroppedObjectsNeeded(void)
#define PERFORM_DELETION_SKIP_ORIGINAL
Definition: dependency.h:176
#define DEPFLAG_REVERSE
Definition: dependency.c:104
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2312
#define DEPFLAG_ORIGINAL
Definition: dependency.c:99
ObjectAddress * refs
Definition: dependency.c:110
bool EventTriggerSupportsObjectClass(ObjectClass objclass)
void EventTriggerSQLDropAddObject(const ObjectAddress *object, bool original, bool normal)
ObjectAddressExtra * extras
Definition: dependency.c:111
#define DEPFLAG_NORMAL
Definition: dependency.c:100
int i
#define PERFORM_DELETION_INTERNAL
Definition: dependency.h:173
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 998 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.

999 {
1000  ScanKeyData key[3];
1001  int nkeys;
1002  SysScanDesc scan;
1003  HeapTuple tup;
1004 
1005  /* DROP hook of the objects being removed */
1006  InvokeObjectDropHookArg(object->classId, object->objectId,
1007  object->objectSubId, flags);
1008 
1009  /*
1010  * Close depRel if we are doing a drop concurrently. The object deletion
1011  * subroutine will commit the current transaction, so we can't keep the
1012  * relation open across doDeletion().
1013  */
1014  if (flags & PERFORM_DELETION_CONCURRENTLY)
1015  heap_close(*depRel, RowExclusiveLock);
1016 
1017  /*
1018  * Delete the object itself, in an object-type-dependent way.
1019  *
1020  * We used to do this after removing the outgoing dependency links, but it
1021  * seems just as reasonable to do it beforehand. In the concurrent case
1022  * we *must* do it in this order, because we can't make any transactional
1023  * updates before calling doDeletion() --- they'd get committed right
1024  * away, which is not cool if the deletion then fails.
1025  */
1026  doDeletion(object, flags);
1027 
1028  /*
1029  * Reopen depRel if we closed it above
1030  */
1031  if (flags & PERFORM_DELETION_CONCURRENTLY)
1033 
1034  /*
1035  * Now remove any pg_depend records that link from this object to others.
1036  * (Any records linking to this object should be gone already.)
1037  *
1038  * When dropping a whole object (subId = 0), remove all pg_depend records
1039  * for its sub-objects too.
1040  */
1041  ScanKeyInit(&key[0],
1043  BTEqualStrategyNumber, F_OIDEQ,
1044  ObjectIdGetDatum(object->classId));
1045  ScanKeyInit(&key[1],
1047  BTEqualStrategyNumber, F_OIDEQ,
1048  ObjectIdGetDatum(object->objectId));
1049  if (object->objectSubId != 0)
1050  {
1051  ScanKeyInit(&key[2],
1053  BTEqualStrategyNumber, F_INT4EQ,
1054  Int32GetDatum(object->objectSubId));
1055  nkeys = 3;
1056  }
1057  else
1058  nkeys = 2;
1059 
1060  scan = systable_beginscan(*depRel, DependDependerIndexId, true,
1061  NULL, nkeys, key);
1062 
1063  while (HeapTupleIsValid(tup = systable_getnext(scan)))
1064  {
1065  CatalogTupleDelete(*depRel, &tup->t_self);
1066  }
1067 
1068  systable_endscan(scan);
1069 
1070  /*
1071  * Delete shared dependency references related to this object. Again, if
1072  * subId = 0, remove records for sub-objects too.
1073  */
1075  object->objectSubId);
1076 
1077 
1078  /*
1079  * Delete any comments, security labels, or initial privileges associated
1080  * with this object. (This is a convenient place to do these things,
1081  * rather than having every object type know to do it.)
1082  */
1083  DeleteComments(object->objectId, object->classId, object->objectSubId);
1084  DeleteSecurityLabel(object);
1085  DeleteInitPrivs(object);
1086 
1087  /*
1088  * CommandCounterIncrement here to ensure that preceding changes are all
1089  * visible to the next deletion step.
1090  */
1092 
1093  /*
1094  * And we're done!
1095  */
1096 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:495
#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:324
#define Anum_pg_depend_objsubid
Definition: pg_depend.h:70
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:174
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:412
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
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:822
void CommandCounterIncrement(void)
Definition: xact.c:921
void DeleteComments(Oid oid, Oid classoid, int32 subid)
Definition: comment.c:325
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
static void doDeletion(const ObjectAddress *object, int flags)
Definition: dependency.c:1102
#define Int32GetDatum(X)
Definition: postgres.h:487
static void DeleteInitPrivs(const ObjectAddress *object)
Definition: dependency.c:2444
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 1102 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_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(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTSDictionaryById(), RemoveTSParserById(), RemoveTSTemplateById(), RemoveTypeById(), and RemoveUserMappingById().

Referenced by deleteOneObject().

1103 {
1104  switch (getObjectClass(object))
1105  {
1106  case OCLASS_CLASS:
1107  {
1108  char relKind = get_rel_relkind(object->objectId);
1109 
1110  if (relKind == RELKIND_INDEX)
1111  {
1112  bool concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
1113 
1114  Assert(object->objectSubId == 0);
1115  index_drop(object->objectId, concurrent);
1116  }
1117  else
1118  {
1119  if (object->objectSubId != 0)
1120  RemoveAttributeById(object->objectId,
1121  object->objectSubId);
1122  else
1124  }
1125 
1126  /* for a sequence, in addition to dropping the heap, also
1127  * delete pg_sequence tuple */
1128  if (relKind == RELKIND_SEQUENCE)
1129  DeleteSequenceTuple(object->objectId);
1130  break;
1131  }
1132 
1133  case OCLASS_PROC:
1134  RemoveFunctionById(object->objectId);
1135  break;
1136 
1137  case OCLASS_TYPE:
1138  RemoveTypeById(object->objectId);
1139  break;
1140 
1141  case OCLASS_CAST:
1142  DropCastById(object->objectId);
1143  break;
1144 
1145  case OCLASS_COLLATION:
1146  RemoveCollationById(object->objectId);
1147  break;
1148 
1149  case OCLASS_CONSTRAINT:
1150  RemoveConstraintById(object->objectId);
1151  break;
1152 
1153  case OCLASS_CONVERSION:
1154  RemoveConversionById(object->objectId);
1155  break;
1156 
1157  case OCLASS_DEFAULT:
1159  break;
1160 
1161  case OCLASS_LANGUAGE:
1163  break;
1164 
1165  case OCLASS_LARGEOBJECT:
1166  LargeObjectDrop(object->objectId);
1167  break;
1168 
1169  case OCLASS_OPERATOR:
1170  RemoveOperatorById(object->objectId);
1171  break;
1172 
1173  case OCLASS_OPCLASS:
1174  RemoveOpClassById(object->objectId);
1175  break;
1176 
1177  case OCLASS_OPFAMILY:
1178  RemoveOpFamilyById(object->objectId);
1179  break;
1180 
1181  case OCLASS_AM:
1183  break;
1184 
1185  case OCLASS_AMOP:
1186  RemoveAmOpEntryById(object->objectId);
1187  break;
1188 
1189  case OCLASS_AMPROC:
1191  break;
1192 
1193  case OCLASS_REWRITE:
1195  break;
1196 
1197  case OCLASS_TRIGGER:
1198  RemoveTriggerById(object->objectId);
1199  break;
1200 
1201  case OCLASS_SCHEMA:
1202  RemoveSchemaById(object->objectId);
1203  break;
1204 
1205  case OCLASS_TSPARSER:
1206  RemoveTSParserById(object->objectId);
1207  break;
1208 
1209  case OCLASS_TSDICT:
1211  break;
1212 
1213  case OCLASS_TSTEMPLATE:
1214  RemoveTSTemplateById(object->objectId);
1215  break;
1216 
1217  case OCLASS_TSCONFIG:
1219  break;
1220 
1221  /*
1222  * OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE intentionally not
1223  * handled here
1224  */
1225 
1226  case OCLASS_FDW:
1228  break;
1229 
1230  case OCLASS_FOREIGN_SERVER:
1232  break;
1233 
1234  case OCLASS_USER_MAPPING:
1236  break;
1237 
1238  case OCLASS_DEFACL:
1239  RemoveDefaultACLById(object->objectId);
1240  break;
1241 
1242  case OCLASS_EXTENSION:
1243  RemoveExtensionById(object->objectId);
1244  break;
1245 
1246  case OCLASS_EVENT_TRIGGER:
1248  break;
1249 
1250  case OCLASS_POLICY:
1251  RemovePolicyById(object->objectId);
1252  break;
1253 
1254  case OCLASS_PUBLICATION:
1256  break;
1257 
1260  break;
1261 
1262  case OCLASS_TRANSFORM:
1263  DropTransformById(object->objectId);
1264  break;
1265 
1266  default:
1267  elog(ERROR, "unrecognized object class: %u",
1268  object->classId);
1269  }
1270 }
void RemoveTriggerById(Oid trigOid)
Definition: trigger.c:1190
void RemoveOpClassById(Oid opclassOid)
Definition: opclasscmds.c:1582
void DropTransformById(Oid transformOid)
void RemoveExtensionById(Oid extId)
Definition: extension.c:1820
void DropProceduralLanguageById(Oid langOid)
Definition: proclang.c:528
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1769
void RemoveForeignDataWrapperById(Oid fdwId)
Definition: foreigncmds.c:837
void RemoveSchemaById(Oid schemaOid)
Definition: schemacmds.c:217
void index_drop(Oid indexId, bool concurrent)
Definition: index.c:1344
void RemovePublicationById(Oid pubid)
void RemovePublicationRelById(Oid proid)
void RemoveAmProcEntryById(Oid entryOid)
Definition: opclasscmds.c:1630
void RemoveTypeById(Oid typeOid)
Definition: typecmds.c:689
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2312
void RemoveCollationById(Oid collationOid)
Definition: pg_collation.c:174
void RemoveUserMappingById(Oid umId)
Definition: foreigncmds.c:1394
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:174
void RemoveDefaultACLById(Oid defaclOid)
Definition: aclchk.c:1451
#define ERROR
Definition: elog.h:43
void RemoveTSConfigurationById(Oid cfgId)
Definition: tsearchcmds.c:1125
void RemoveOpFamilyById(Oid opfamilyOid)
Definition: opclasscmds.c:1563
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:1535
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:1677
void LargeObjectDrop(Oid loid)
void RemoveAmOpEntryById(Oid entryOid)
Definition: opclasscmds.c:1601
void RemoveForeignServerById(Oid srvId)
Definition: foreigncmds.c:1071
#define Assert(condition)
Definition: c.h:671
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:1750
#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 1935 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().

1936 {
1937  ObjectAddress *priorobj;
1938  int oldref,
1939  newrefs;
1940 
1941  /*
1942  * We can't sort if the array has "extra" data, because there's no way to
1943  * keep it in sync. Fortunately that combination of features is not
1944  * needed.
1945  */
1946  Assert(!addrs->extras);
1947 
1948  if (addrs->numrefs <= 1)
1949  return; /* nothing to do */
1950 
1951  /* Sort the refs so that duplicates are adjacent */
1952  qsort((void *) addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
1954 
1955  /* Remove dups */
1956  priorobj = addrs->refs;
1957  newrefs = 1;
1958  for (oldref = 1; oldref < addrs->numrefs; oldref++)
1959  {
1960  ObjectAddress *thisobj = addrs->refs + oldref;
1961 
1962  if (priorobj->classId == thisobj->classId &&
1963  priorobj->objectId == thisobj->objectId)
1964  {
1965  if (priorobj->objectSubId == thisobj->objectSubId)
1966  continue; /* identical, so drop thisobj */
1967 
1968  /*
1969  * If we have a whole-object reference and a reference to a part
1970  * of the same object, we don't need the whole-object reference
1971  * (for example, we don't need to reference both table foo and
1972  * column foo.bar). The whole-object reference will always appear
1973  * first in the sorted list.
1974  */
1975  if (priorobj->objectSubId == 0)
1976  {
1977  /* replace whole ref with partial */
1978  priorobj->objectSubId = thisobj->objectSubId;
1979  continue;
1980  }
1981  }
1982  /* Not identical, so add thisobj to output set */
1983  priorobj++;
1984  *priorobj = *thisobj;
1985  newrefs++;
1986  }
1987 
1988  addrs->numrefs = newrefs;
1989 }
static int object_address_comparator(const void *a, const void *b)
Definition: dependency.c:1995
ObjectAddress * refs
Definition: dependency.c:110
#define Assert(condition)
Definition: c.h:671
ObjectAddressExtra * extras
Definition: dependency.c:111
#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 1468 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, 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, 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().

1470 {
1471  if (node == NULL)
1472  return false;
1473  if (IsA(node, Var))
1474  {
1475  Var *var = (Var *) node;
1476  List *rtable;
1477  RangeTblEntry *rte;
1478 
1479  /* Find matching rtable entry, or complain if not found */
1480  if (var->varlevelsup >= list_length(context->rtables))
1481  elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
1482  rtable = (List *) list_nth(context->rtables, var->varlevelsup);
1483  if (var->varno <= 0 || var->varno > list_length(rtable))
1484  elog(ERROR, "invalid varno %d", var->varno);
1485  rte = rt_fetch(var->varno, rtable);
1486 
1487  /*
1488  * A whole-row Var references no specific columns, so adds no new
1489  * dependency. (We assume that there is a whole-table dependency
1490  * arising from each underlying rangetable entry. While we could
1491  * record such a dependency when finding a whole-row Var that
1492  * references a relation directly, it's quite unclear how to extend
1493  * that to whole-row Vars for JOINs, so it seems better to leave the
1494  * responsibility with the range table. Note that this poses some
1495  * risks for identifying dependencies of stand-alone expressions:
1496  * whole-table references may need to be created separately.)
1497  */
1498  if (var->varattno == InvalidAttrNumber)
1499  return false;
1500  if (rte->rtekind == RTE_RELATION)
1501  {
1502  /* If it's a plain relation, reference this column */
1504  context->addrs);
1505  }
1506  else if (rte->rtekind == RTE_JOIN)
1507  {
1508  /* Scan join output column to add references to join inputs */
1509  List *save_rtables;
1510 
1511  /* We must make the context appropriate for join's level */
1512  save_rtables = context->rtables;
1513  context->rtables = list_copy_tail(context->rtables,
1514  var->varlevelsup);
1515  if (var->varattno <= 0 ||
1516  var->varattno > list_length(rte->joinaliasvars))
1517  elog(ERROR, "invalid varattno %d", var->varattno);
1519  var->varattno - 1),
1520  context);
1521  list_free(context->rtables);
1522  context->rtables = save_rtables;
1523  }
1524  return false;
1525  }
1526  else if (IsA(node, Const))
1527  {
1528  Const *con = (Const *) node;
1529  Oid objoid;
1530 
1531  /* A constant must depend on the constant's datatype */
1533  context->addrs);
1534 
1535  /*
1536  * We must also depend on the constant's collation: it could be
1537  * different from the datatype's, if a CollateExpr was const-folded to
1538  * a simple constant. However we can save work in the most common
1539  * case where the collation is "default", since we know that's pinned.
1540  */
1541  if (OidIsValid(con->constcollid) &&
1544  context->addrs);
1545 
1546  /*
1547  * If it's a regclass or similar literal referring to an existing
1548  * object, add a reference to that object. (Currently, only the
1549  * regclass and regconfig cases have any likely use, but we may as
1550  * well handle all the OID-alias datatypes consistently.)
1551  */
1552  if (!con->constisnull)
1553  {
1554  switch (con->consttype)
1555  {
1556  case REGPROCOID:
1557  case REGPROCEDUREOID:
1558  objoid = DatumGetObjectId(con->constvalue);
1560  ObjectIdGetDatum(objoid)))
1561  add_object_address(OCLASS_PROC, objoid, 0,
1562  context->addrs);
1563  break;
1564  case REGOPEROID:
1565  case REGOPERATOROID:
1566  objoid = DatumGetObjectId(con->constvalue);
1568  ObjectIdGetDatum(objoid)))
1570  context->addrs);
1571  break;
1572  case REGCLASSOID:
1573  objoid = DatumGetObjectId(con->constvalue);
1575  ObjectIdGetDatum(objoid)))
1576  add_object_address(OCLASS_CLASS, objoid, 0,
1577  context->addrs);
1578  break;
1579  case REGTYPEOID:
1580  objoid = DatumGetObjectId(con->constvalue);
1582  ObjectIdGetDatum(objoid)))
1583  add_object_address(OCLASS_TYPE, objoid, 0,
1584  context->addrs);
1585  break;
1586  case REGCONFIGOID:
1587  objoid = DatumGetObjectId(con->constvalue);
1589  ObjectIdGetDatum(objoid)))
1591  context->addrs);
1592  break;
1593  case REGDICTIONARYOID:
1594  objoid = DatumGetObjectId(con->constvalue);
1596  ObjectIdGetDatum(objoid)))
1597  add_object_address(OCLASS_TSDICT, objoid, 0,
1598  context->addrs);
1599  break;
1600 
1601  case REGNAMESPACEOID:
1602  objoid = DatumGetObjectId(con->constvalue);
1604  ObjectIdGetDatum(objoid)))
1605  add_object_address(OCLASS_SCHEMA, objoid, 0,
1606  context->addrs);
1607  break;
1608 
1609  /*
1610  * Dependencies for regrole should be shared among all
1611  * databases, so explicitly inhibit to have dependencies.
1612  */
1613  case REGROLEOID:
1614  ereport(ERROR,
1615  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1616  errmsg("constant of the type %s cannot be used here",
1617  "regrole")));
1618  break;
1619  }
1620  }
1621  return false;
1622  }
1623  else if (IsA(node, Param))
1624  {
1625  Param *param = (Param *) node;
1626 
1627  /* A parameter must depend on the parameter's datatype */
1629  context->addrs);
1630  /* and its collation, just as for Consts */
1631  if (OidIsValid(param->paramcollid) &&
1634  context->addrs);
1635  }
1636  else if (IsA(node, FuncExpr))
1637  {
1638  FuncExpr *funcexpr = (FuncExpr *) node;
1639 
1640  add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
1641  context->addrs);
1642  /* fall through to examine arguments */
1643  }
1644  else if (IsA(node, OpExpr))
1645  {
1646  OpExpr *opexpr = (OpExpr *) node;
1647 
1649  context->addrs);
1650  /* fall through to examine arguments */
1651  }
1652  else if (IsA(node, DistinctExpr))
1653  {
1654  DistinctExpr *distinctexpr = (DistinctExpr *) node;
1655 
1656  add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
1657  context->addrs);
1658  /* fall through to examine arguments */
1659  }
1660  else if (IsA(node, NullIfExpr))
1661  {
1662  NullIfExpr *nullifexpr = (NullIfExpr *) node;
1663 
1664  add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
1665  context->addrs);
1666  /* fall through to examine arguments */
1667  }
1668  else if (IsA(node, ScalarArrayOpExpr))
1669  {
1670  ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
1671 
1673  context->addrs);
1674  /* fall through to examine arguments */
1675  }
1676  else if (IsA(node, Aggref))
1677  {
1678  Aggref *aggref = (Aggref *) node;
1679 
1681  context->addrs);
1682  /* fall through to examine arguments */
1683  }
1684  else if (IsA(node, WindowFunc))
1685  {
1686  WindowFunc *wfunc = (WindowFunc *) node;
1687 
1689  context->addrs);
1690  /* fall through to examine arguments */
1691  }
1692  else if (IsA(node, SubPlan))
1693  {
1694  /* Extra work needed here if we ever need this case */
1695  elog(ERROR, "already-planned subqueries not supported");
1696  }
1697  else if (IsA(node, RelabelType))
1698  {
1699  RelabelType *relab = (RelabelType *) node;
1700 
1701  /* since there is no function dependency, need to depend on type */
1703  context->addrs);
1704  /* the collation might not be referenced anywhere else, either */
1705  if (OidIsValid(relab->resultcollid) &&
1708  context->addrs);
1709  }
1710  else if (IsA(node, CoerceViaIO))
1711  {
1712  CoerceViaIO *iocoerce = (CoerceViaIO *) node;
1713 
1714  /* since there is no exposed function, need to depend on type */
1716  context->addrs);
1717  }
1718  else if (IsA(node, ArrayCoerceExpr))
1719  {
1720  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
1721 
1722  if (OidIsValid(acoerce->elemfuncid))
1724  context->addrs);
1726  context->addrs);
1727  /* fall through to examine arguments */
1728  }
1729  else if (IsA(node, ConvertRowtypeExpr))
1730  {
1731  ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
1732 
1733  /* since there is no function dependency, need to depend on type */
1735  context->addrs);
1736  }
1737  else if (IsA(node, CollateExpr))
1738  {
1739  CollateExpr *coll = (CollateExpr *) node;
1740 
1742  context->addrs);
1743  }
1744  else if (IsA(node, RowExpr))
1745  {
1746  RowExpr *rowexpr = (RowExpr *) node;
1747 
1749  context->addrs);
1750  }
1751  else if (IsA(node, RowCompareExpr))
1752  {
1753  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1754  ListCell *l;
1755 
1756  foreach(l, rcexpr->opnos)
1757  {
1759  context->addrs);
1760  }
1761  foreach(l, rcexpr->opfamilies)
1762  {
1764  context->addrs);
1765  }
1766  /* fall through to examine arguments */
1767  }
1768  else if (IsA(node, CoerceToDomain))
1769  {
1770  CoerceToDomain *cd = (CoerceToDomain *) node;
1771 
1773  context->addrs);
1774  }
1775  else if (IsA(node, OnConflictExpr))
1776  {
1777  OnConflictExpr *onconflict = (OnConflictExpr *) node;
1778 
1779  if (OidIsValid(onconflict->constraint))
1781  context->addrs);
1782  /* fall through to examine arguments */
1783  }
1784  else if (IsA(node, SortGroupClause))
1785  {
1786  SortGroupClause *sgc = (SortGroupClause *) node;
1787 
1789  context->addrs);
1790  if (OidIsValid(sgc->sortop))
1792  context->addrs);
1793  return false;
1794  }
1795  else if (IsA(node, Query))
1796  {
1797  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1798  Query *query = (Query *) node;
1799  ListCell *lc;
1800  bool result;
1801 
1802  /*
1803  * Add whole-relation refs for each plain relation mentioned in the
1804  * subquery's rtable.
1805  *
1806  * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
1807  * RTEs, subqueries, etc, so no need to do that here. But keep it
1808  * from looking at join alias lists.
1809  *
1810  * Note: we don't need to worry about collations mentioned in
1811  * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
1812  * collations referenced in other parts of the Query. We do have to
1813  * worry about collations mentioned in RTE_FUNCTION, but we take care
1814  * of those when we recurse to the RangeTblFunction node(s).
1815  */
1816  foreach(lc, query->rtable)
1817  {
1818  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
1819 
1820  switch (rte->rtekind)
1821  {
1822  case RTE_RELATION:
1824  context->addrs);
1825  break;
1826  default:
1827  break;
1828  }
1829  }
1830 
1831  /*
1832  * If the query is an INSERT or UPDATE, we should create a dependency
1833  * on each target column, to prevent the specific target column from
1834  * being dropped. Although we will visit the TargetEntry nodes again
1835  * during query_tree_walker, we won't have enough context to do this
1836  * conveniently, so do it here.
1837  */
1838  if (query->commandType == CMD_INSERT ||
1839  query->commandType == CMD_UPDATE)
1840  {
1841  RangeTblEntry *rte;
1842 
1843  if (query->resultRelation <= 0 ||
1844  query->resultRelation > list_length(query->rtable))
1845  elog(ERROR, "invalid resultRelation %d",
1846  query->resultRelation);
1847  rte = rt_fetch(query->resultRelation, query->rtable);
1848  if (rte->rtekind == RTE_RELATION)
1849  {
1850  foreach(lc, query->targetList)
1851  {
1852  TargetEntry *tle = (TargetEntry *) lfirst(lc);
1853 
1854  if (tle->resjunk)
1855  continue; /* ignore junk tlist items */
1857  context->addrs);
1858  }
1859  }
1860  }
1861 
1862  /*
1863  * Add dependencies on constraints listed in query's constraintDeps
1864  */
1865  foreach(lc, query->constraintDeps)
1866  {
1868  context->addrs);
1869  }
1870 
1871  /* query_tree_walker ignores ORDER BY etc, but we need those opers */
1872  find_expr_references_walker((Node *) query->sortClause, context);
1873  find_expr_references_walker((Node *) query->groupClause, context);
1874  find_expr_references_walker((Node *) query->windowClause, context);
1875  find_expr_references_walker((Node *) query->distinctClause, context);
1876 
1877  /* Examine substructure of query */
1878  context->rtables = lcons(query->rtable, context->rtables);
1879  result = query_tree_walker(query,
1881  (void *) context,
1883  context->rtables = list_delete_first(context->rtables);
1884  return result;
1885  }
1886  else if (IsA(node, SetOperationStmt))
1887  {
1888  SetOperationStmt *setop = (SetOperationStmt *) node;
1889 
1890  /* we need to look at the groupClauses for operator references */
1891  find_expr_references_walker((Node *) setop->groupClauses, context);
1892  /* fall through to examine child nodes */
1893  }
1894  else if (IsA(node, RangeTblFunction))
1895  {
1896  RangeTblFunction *rtfunc = (RangeTblFunction *) node;
1897  ListCell *ct;
1898 
1899  /*
1900  * Add refs for any datatypes and collations used in a column
1901  * definition list for a RECORD function. (For other cases, it should
1902  * be enough to depend on the function itself.)
1903  */
1904  foreach(ct, rtfunc->funccoltypes)
1905  {
1907  context->addrs);
1908  }
1909  foreach(ct, rtfunc->funccolcollations)
1910  {
1911  Oid collid = lfirst_oid(ct);
1912 
1913  if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
1915  context->addrs);
1916  }
1917  }
1918  else if (IsA(node, TableSampleClause))
1919  {
1920  TableSampleClause *tsc = (TableSampleClause *) node;
1921 
1923  context->addrs);
1924  /* fall through to examine arguments */
1925  }
1926 
1928  (void *) context);
1929 }
Datum constvalue
Definition: primnodes.h:174
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2238
#define REGCLASSOID
Definition: pg_type.h:565
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * joinaliasvars
Definition: parsenodes.h:921
Index varlevelsup
Definition: primnodes.h:151
List * sortClause
Definition: parsenodes.h:147
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1468
#define REGROLEOID
Definition: pg_type.h:573
ObjectAddresses * addrs
Definition: dependency.c:129
List * constraintDeps
Definition: parsenodes.h:157
#define DatumGetObjectId(X)
Definition: postgres.h:508
Oid resulttype
Definition: primnodes.h:788
List * opfamilies
Definition: primnodes.h:1009
int resultRelation
Definition: parsenodes.h:113
Definition: nodes.h:509
int errcode(int sqlerrcode)
Definition: elog.c:575
AttrNumber varattno
Definition: primnodes.h:146
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:569
Definition: primnodes.h:141
#define REGOPEROID
Definition: pg_type.h:557
#define OidIsValid(objectId)
Definition: c.h:534
List * windowClause
Definition: parsenodes.h:143
List * targetList
Definition: parsenodes.h:131
Oid consttype
Definition: primnodes.h:170
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:167
bool resjunk
Definition: primnodes.h:1337
List * rtable
Definition: parsenodes.h:128
List * distinctClause
Definition: parsenodes.h:145
Oid funcid
Definition: primnodes.h:426
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
Oid paramcollid
Definition: primnodes.h:226
Oid constcollid
Definition: primnodes.h:172
void * list_nth(const List *list, int n)
Definition: list.c:410
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:68
AttrNumber resno
Definition: primnodes.h:1331
#define REGDICTIONARYOID
Definition: pg_type.h:615
Oid winfnoid
Definition: primnodes.h:333
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Index varno
Definition: primnodes.h:144
Oid resulttype
Definition: primnodes.h:768
Oid resultcollid
Definition: primnodes.h:770
Oid aggfnoid
Definition: primnodes.h:272
CmdType commandType
Definition: parsenodes.h:103
List * lcons(void *datum, List *list)
Definition: list.c:259
List * funccolcollations
Definition: parsenodes.h:999
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1852
Oid row_typeid
Definition: primnodes.h:963
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
#define REGCONFIGOID
Definition: pg_type.h:612
RTEKind rtekind
Definition: parsenodes.h:882
List * groupClause
Definition: parsenodes.h:137
int errmsg(const char *fmt,...)
Definition: elog.c:797
void list_free(List *list)
Definition: list.c:1133
List * funccoltypes
Definition: parsenodes.h:997
Oid opno
Definition: primnodes.h:473
#define elog
Definition: elog.h:219
#define REGPROCEDUREOID
Definition: pg_type.h:553
Definition: pg_list.h:45
#define REGNAMESPACEOID
Definition: pg_type.h:577
static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId, ObjectAddresses *addrs)
Definition: dependency.c:2048
Oid paramtype
Definition: primnodes.h:224
bool constisnull
Definition: primnodes.h:175
#define lfirst_oid(lc)
Definition: pg_list.h:108
#define REGOPERATOROID
Definition: pg_type.h:561
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 460 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().

467 {
468  ScanKeyData key[3];
469  int nkeys;
470  SysScanDesc scan;
471  HeapTuple tup;
472  ObjectAddress otherObject;
473  ObjectAddressStack mystack;
474  ObjectAddressExtra extra;
475 
476  /*
477  * If the target object is already being visited in an outer recursion
478  * level, just report the current objflags back to that level and exit.
479  * This is needed to avoid infinite recursion in the face of circular
480  * dependencies.
481  *
482  * The stack check alone would result in dependency loops being broken at
483  * an arbitrary point, ie, the first member object of the loop to be
484  * visited is the last one to be deleted. This is obviously unworkable.
485  * However, the check for internal dependency below guarantees that we
486  * will not break a loop at an internal dependency: if we enter the loop
487  * at an "owned" object we will switch and start at the "owning" object
488  * instead. We could probably hack something up to avoid breaking at an
489  * auto dependency, too, if we had to. However there are no known cases
490  * where that would be necessary.
491  */
492  if (stack_address_present_add_flags(object, objflags, stack))
493  return;
494 
495  /*
496  * It's also possible that the target object has already been completely
497  * processed and put into targetObjects. If so, again we just add the
498  * specified objflags to its entry and return.
499  *
500  * (Note: in these early-exit cases we could release the caller-taken
501  * lock, since the object is presumably now locked multiple times; but it
502  * seems not worth the cycles.)
503  */
504  if (object_address_present_add_flags(object, objflags, targetObjects))
505  return;
506 
507  /*
508  * The target object might be internally dependent on some other object
509  * (its "owner"), and/or be a member of an extension (also considered its
510  * owner). If so, and if we aren't recursing from the owning object, we
511  * have to transform this deletion request into a deletion request of the
512  * owning object. (We'll eventually recurse back to this object, but the
513  * owning object has to be visited first so it will be deleted after.) The
514  * way to find out about this is to scan the pg_depend entries that show
515  * what this object depends on.
516  */
517  ScanKeyInit(&key[0],
519  BTEqualStrategyNumber, F_OIDEQ,
520  ObjectIdGetDatum(object->classId));
521  ScanKeyInit(&key[1],
523  BTEqualStrategyNumber, F_OIDEQ,
524  ObjectIdGetDatum(object->objectId));
525  if (object->objectSubId != 0)
526  {
527  ScanKeyInit(&key[2],
529  BTEqualStrategyNumber, F_INT4EQ,
530  Int32GetDatum(object->objectSubId));
531  nkeys = 3;
532  }
533  else
534  nkeys = 2;
535 
536  scan = systable_beginscan(*depRel, DependDependerIndexId, true,
537  NULL, nkeys, key);
538 
539  while (HeapTupleIsValid(tup = systable_getnext(scan)))
540  {
541  Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
542 
543  otherObject.classId = foundDep->refclassid;
544  otherObject.objectId = foundDep->refobjid;
545  otherObject.objectSubId = foundDep->refobjsubid;
546 
547  switch (foundDep->deptype)
548  {
549  case DEPENDENCY_NORMAL:
550  case DEPENDENCY_AUTO:
552  /* no problem */
553  break;
554 
556 
557  /*
558  * If told to, ignore EXTENSION dependencies altogether. This
559  * flag is normally used to prevent dropping extensions during
560  * temporary-object cleanup, even if a temp object was created
561  * during an extension script.
562  */
564  break;
565 
566  /*
567  * If the other object is the extension currently being
568  * created/altered, ignore this dependency and continue with
569  * the deletion. This allows dropping of an extension's
570  * objects within the extension's scripts, as well as corner
571  * cases such as dropping a transient object created within
572  * such a script.
573  */
574  if (creating_extension &&
575  otherObject.classId == ExtensionRelationId &&
576  otherObject.objectId == CurrentExtensionObject)
577  break;
578 
579  /* Otherwise, treat this like an internal dependency */
580  /* FALL THRU */
581 
582  case DEPENDENCY_INTERNAL:
583 
584  /*
585  * This object is part of the internal implementation of
586  * another object, or is part of the extension that is the
587  * other object. We have three cases:
588  *
589  * 1. At the outermost recursion level, disallow the DROP. (We
590  * just ereport here, rather than proceeding, since no other
591  * dependencies are likely to be interesting.) However, if
592  * the owning object is listed in pendingObjects, just release
593  * the caller's lock and return; we'll eventually complete the
594  * DROP when we reach that entry in the pending list.
595  */
596  if (stack == NULL)
597  {
598  char *otherObjDesc;
599 
600  if (pendingObjects &&
601  object_address_present(&otherObject, pendingObjects))
602  {
603  systable_endscan(scan);
604  /* need to release caller's lock; see notes below */
605  ReleaseDeletionLock(object);
606  return;
607  }
608  otherObjDesc = getObjectDescription(&otherObject);
609  ereport(ERROR,
610  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
611  errmsg("cannot drop %s because %s requires it",
612  getObjectDescription(object),
613  otherObjDesc),
614  errhint("You can drop %s instead.",
615  otherObjDesc)));
616  }
617 
618  /*
619  * 2. When recursing from the other end of this dependency,
620  * it's okay to continue with the deletion. This holds when
621  * recursing from a whole object that includes the nominal
622  * other end as a component, too. Since there can be more
623  * than one "owning" object, we have to allow matches that are
624  * more than one level down in the stack.
625  */
626  if (stack_address_present_add_flags(&otherObject, 0, stack))
627  break;
628 
629  /*
630  * 3. Not all the owning objects have been visited, so
631  * transform this deletion request into a delete of this
632  * owning object.
633  *
634  * First, release caller's lock on this object and get
635  * deletion lock on the owning object. (We must release
636  * caller's lock to avoid deadlock against a concurrent
637  * deletion of the owning object.)
638  */
639  ReleaseDeletionLock(object);
640  AcquireDeletionLock(&otherObject, 0);
641 
642  /*
643  * The owning object might have been deleted while we waited
644  * to lock it; if so, neither it nor the current object are
645  * interesting anymore. We test this by checking the
646  * pg_depend entry (see notes below).
647  */
648  if (!systable_recheck_tuple(scan, tup))
649  {
650  systable_endscan(scan);
651  ReleaseDeletionLock(&otherObject);
652  return;
653  }
654 
655  /*
656  * Okay, recurse to the owning object instead of proceeding.
657  *
658  * We do not need to stack the current object; we want the
659  * traversal order to be as if the original reference had
660  * linked to the owning object instead of this one.
661  *
662  * The dependency type is a "reverse" dependency: we need to
663  * delete the owning object if this one is to be deleted, but
664  * this linkage is never a reason for an automatic deletion.
665  */
666  findDependentObjects(&otherObject,
668  flags,
669  stack,
670  targetObjects,
671  pendingObjects,
672  depRel);
673  /* And we're done here. */
674  systable_endscan(scan);
675  return;
676  case DEPENDENCY_PIN:
677 
678  /*
679  * Should not happen; PIN dependencies should have zeroes in
680  * the depender fields...
681  */
682  elog(ERROR, "incorrect use of PIN dependency with %s",
683  getObjectDescription(object));
684  break;
685  default:
686  elog(ERROR, "unrecognized dependency type '%c' for %s",
687  foundDep->deptype, getObjectDescription(object));
688  break;
689  }
690  }
691 
692  systable_endscan(scan);
693 
694  /*
695  * Now recurse to any dependent objects. We must visit them first since
696  * they have to be deleted before the current object.
697  */
698  mystack.object = object; /* set up a new stack level */
699  mystack.flags = objflags;
700  mystack.next = stack;
701 
702  ScanKeyInit(&key[0],
704  BTEqualStrategyNumber, F_OIDEQ,
705  ObjectIdGetDatum(object->classId));
706  ScanKeyInit(&key[1],
708  BTEqualStrategyNumber, F_OIDEQ,
709  ObjectIdGetDatum(object->objectId));
710  if (object->objectSubId != 0)
711  {
712  ScanKeyInit(&key[2],
714  BTEqualStrategyNumber, F_INT4EQ,
715  Int32GetDatum(object->objectSubId));
716  nkeys = 3;
717  }
718  else
719  nkeys = 2;
720 
721  scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
722  NULL, nkeys, key);
723 
724  while (HeapTupleIsValid(tup = systable_getnext(scan)))
725  {
726  Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
727  int subflags;
728 
729  otherObject.classId = foundDep->classid;
730  otherObject.objectId = foundDep->objid;
731  otherObject.objectSubId = foundDep->objsubid;
732 
733  /*
734  * Must lock the dependent object before recursing to it.
735  */
736  AcquireDeletionLock(&otherObject, 0);
737 
738  /*
739  * The dependent object might have been deleted while we waited to
740  * lock it; if so, we don't need to do anything more with it. We can
741  * test this cheaply and independently of the object's type by seeing
742  * if the pg_depend tuple we are looking at is still live. (If the
743  * object got deleted, the tuple would have been deleted too.)
744  */
745  if (!systable_recheck_tuple(scan, tup))
746  {
747  /* release the now-useless lock */
748  ReleaseDeletionLock(&otherObject);
749  /* and continue scanning for dependencies */
750  continue;
751  }
752 
753  /* Recurse, passing objflags indicating the dependency type */
754  switch (foundDep->deptype)
755  {
756  case DEPENDENCY_NORMAL:
757  subflags = DEPFLAG_NORMAL;
758  break;
759  case DEPENDENCY_AUTO:
761  subflags = DEPFLAG_AUTO;
762  break;
763  case DEPENDENCY_INTERNAL:
764  subflags = DEPFLAG_INTERNAL;
765  break;
767  subflags = DEPFLAG_EXTENSION;
768  break;
769  case DEPENDENCY_PIN:
770 
771  /*
772  * For a PIN dependency we just ereport immediately; there
773  * won't be any others to report.
774  */
775  ereport(ERROR,
776  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
777  errmsg("cannot drop %s because it is required by the database system",
778  getObjectDescription(object))));
779  subflags = 0; /* keep compiler quiet */
780  break;
781  default:
782  elog(ERROR, "unrecognized dependency type '%c' for %s",
783  foundDep->deptype, getObjectDescription(object));
784  subflags = 0; /* keep compiler quiet */
785  break;
786  }
787 
788  findDependentObjects(&otherObject,
789  subflags,
790  flags,
791  &mystack,
792  targetObjects,
793  pendingObjects,
794  depRel);
795  }
796 
797  systable_endscan(scan);
798 
799  /*
800  * Finally, we can add the target object to targetObjects. Be careful to
801  * include any flags that were passed back down to us from inner recursion
802  * levels.
803  */
804  extra.flags = mystack.flags;
805  if (stack)
806  extra.dependee = *stack->object;
807  else
808  memset(&extra.dependee, 0, sizeof(extra.dependee));
809  add_exact_object_address_extra(object, &extra, targetObjects);
810 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:460
Oid CurrentExtensionObject
Definition: extension.c:68
static void ReleaseDeletionLock(const ObjectAddress *object)
Definition: dependency.c:1307
#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:495
#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:2106
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
Definition: dependency.c:2141
#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:104
struct ObjectAddressStack * next
Definition: dependency.c:123
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:324
#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:450
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:412
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define DEPFLAG_AUTO
Definition: dependency.c:101
const ObjectAddress * object
Definition: dependency.c:121
#define DEPFLAG_INTERNAL
Definition: dependency.c:102
static bool object_address_present_add_flags(const ObjectAddress *object, int flags, ObjectAddresses *addrs)
Definition: dependency.c:2167
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:1280
bool creating_extension
Definition: extension.c:67
#define DEPFLAG_EXTENSION
Definition: dependency.c:103
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Int32GetDatum(X)
Definition: postgres.h:487
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define DEPFLAG_NORMAL
Definition: dependency.c:100
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:2234
#define PERFORM_DELETION_SKIP_EXTENSIONS
Definition: dependency.h:177
ObjectAddress dependee
Definition: dependency.c:95
#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 2297 of file dependency.c.

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

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

2298 {
2299  pfree(addrs->refs);
2300  if (addrs->extras)
2301  pfree(addrs->extras);
2302  pfree(addrs);
2303 }
void pfree(void *pointer)
Definition: mcxt.c:992
ObjectAddress * refs
Definition: dependency.c:110
ObjectAddressExtra * extras
Definition: dependency.c:111
ObjectClass getObjectClass ( const ObjectAddress object)

Definition at line 2312 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_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, SubscriptionRelationId, TableSpaceRelationId, TransformRelationId, TriggerRelationId, TSConfigRelationId, TSDictionaryRelationId, TSParserRelationId, TSTemplateRelationId, TypeRelationId, and UserMappingRelationId.

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

2313 {
2314  /* only pg_class entries can have nonzero objectSubId */
2315  if (object->classId != RelationRelationId &&
2316  object->objectSubId != 0)
2317  elog(ERROR, "invalid non-zero objectSubId for object class %u",
2318  object->classId);
2319 
2320  switch (object->classId)
2321  {
2322  case RelationRelationId:
2323  /* caller must check objectSubId */
2324  return OCLASS_CLASS;
2325 
2326  case ProcedureRelationId:
2327  return OCLASS_PROC;
2328 
2329  case TypeRelationId:
2330  return OCLASS_TYPE;
2331 
2332  case CastRelationId:
2333  return OCLASS_CAST;
2334 
2335  case CollationRelationId:
2336  return OCLASS_COLLATION;
2337 
2338  case ConstraintRelationId:
2339  return OCLASS_CONSTRAINT;
2340 
2341  case ConversionRelationId:
2342  return OCLASS_CONVERSION;
2343 
2344  case AttrDefaultRelationId:
2345  return OCLASS_DEFAULT;
2346 
2347  case LanguageRelationId:
2348  return OCLASS_LANGUAGE;
2349 
2350  case LargeObjectRelationId:
2351  return OCLASS_LARGEOBJECT;
2352 
2353  case OperatorRelationId:
2354  return OCLASS_OPERATOR;
2355 
2357  return OCLASS_OPCLASS;
2358 
2360  return OCLASS_OPFAMILY;
2361 
2363  return OCLASS_AM;
2364 
2366  return OCLASS_AMOP;
2367 
2369  return OCLASS_AMPROC;
2370 
2371  case RewriteRelationId:
2372  return OCLASS_REWRITE;
2373 
2374  case TriggerRelationId:
2375  return OCLASS_TRIGGER;
2376 
2377  case NamespaceRelationId:
2378  return OCLASS_SCHEMA;
2379 
2380  case TSParserRelationId:
2381  return OCLASS_TSPARSER;
2382 
2384  return OCLASS_TSDICT;
2385 
2386  case TSTemplateRelationId:
2387  return OCLASS_TSTEMPLATE;
2388 
2389  case TSConfigRelationId:
2390  return OCLASS_TSCONFIG;
2391 
2392  case AuthIdRelationId:
2393  return OCLASS_ROLE;
2394 
2395  case DatabaseRelationId:
2396  return OCLASS_DATABASE;
2397 
2398  case TableSpaceRelationId:
2399  return OCLASS_TBLSPACE;
2400 
2402  return OCLASS_FDW;
2403 
2405  return OCLASS_FOREIGN_SERVER;
2406 
2407  case UserMappingRelationId:
2408  return OCLASS_USER_MAPPING;
2409 
2410  case DefaultAclRelationId:
2411  return OCLASS_DEFACL;
2412 
2413  case ExtensionRelationId:
2414  return OCLASS_EXTENSION;
2415 
2417  return OCLASS_EVENT_TRIGGER;
2418 
2419  case PolicyRelationId:
2420  return OCLASS_POLICY;
2421 
2422  case PublicationRelationId:
2423  return OCLASS_PUBLICATION;
2424 
2426  return OCLASS_PUBLICATION_REL;
2427 
2429  return OCLASS_SUBSCRIPTION;
2430 
2431  case TransformRelationId:
2432  return OCLASS_TRANSFORM;
2433  }
2434 
2435  /* shouldn't get here */
2436  elog(ERROR, "unrecognized object class: %u", object->classId);
2437  return OCLASS_CLASS; /* keep compiler quiet */
2438 }
#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 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 2026 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().

2027 {
2028  ObjectAddresses *addrs;
2029 
2030  addrs = palloc(sizeof(ObjectAddresses));
2031 
2032  addrs->numrefs = 0;
2033  addrs->maxrefs = 32;
2034  addrs->refs = (ObjectAddress *)
2035  palloc(addrs->maxrefs * sizeof(ObjectAddress));
2036  addrs->extras = NULL; /* until/unless needed */
2037 
2038  return addrs;
2039 }
ObjectAddress * refs
Definition: dependency.c:110
#define NULL
Definition: c.h:226
ObjectAddressExtra * extras
Definition: dependency.c:111
void * palloc(Size size)
Definition: mcxt.c:891
static int object_address_comparator ( const void *  a,
const void *  b 
)
static

Definition at line 1995 of file dependency.c.

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

Referenced by eliminate_duplicate_dependencies().

1996 {
1997  const ObjectAddress *obja = (const ObjectAddress *) a;
1998  const ObjectAddress *objb = (const ObjectAddress *) b;
1999 
2000  if (obja->classId < objb->classId)
2001  return -1;
2002  if (obja->classId > objb->classId)
2003  return 1;
2004  if (obja->objectId < objb->objectId)
2005  return -1;
2006  if (obja->objectId > objb->objectId)
2007  return 1;
2008 
2009  /*
2010  * We sort the subId as an unsigned int so that 0 will come first. See
2011  * logic in eliminate_duplicate_dependencies.
2012  */
2013  if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
2014  return -1;
2015  if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
2016  return 1;
2017  return 0;
2018 }
bool object_address_present ( const ObjectAddress object,
const ObjectAddresses addrs 
)

Definition at line 2141 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().

2143 {
2144  int i;
2145 
2146  for (i = addrs->numrefs - 1; i >= 0; i--)
2147  {
2148  const ObjectAddress *thisobj = addrs->refs + i;
2149 
2150  if (object->classId == thisobj->classId &&
2151  object->objectId == thisobj->objectId)
2152  {
2153  if (object->objectSubId == thisobj->objectSubId ||
2154  thisobj->objectSubId == 0)
2155  return true;
2156  }
2157  }
2158 
2159  return false;
2160 }
ObjectAddress * refs
Definition: dependency.c:110
int i
static bool object_address_present_add_flags ( const ObjectAddress object,
int  flags,
ObjectAddresses addrs 
)
static

Definition at line 2167 of file dependency.c.

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

Referenced by findDependentObjects().

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

Definition at line 301 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(), ATPostAlterTypeCleanup(), DefineQueryRewrite(), do_autovacuum(), dropOperators(), dropProcedures(), finish_heap_swap(), inv_drop(), PreCommit_on_commit_actions(), PublicationDropTables(), RemoveAttrDefault(), RemoveTempRelations(), RemoveUserMapping(), and SetDefaultACL().

303 {
304  Relation depRel;
305  ObjectAddresses *targetObjects;
306 
307  /*
308  * We save some cycles by opening pg_depend just once and passing the
309  * Relation pointer down to all the recursive deletion steps.
310  */
312 
313  /*
314  * Acquire deletion lock on the target object. (Ideally the caller has
315  * done this already, but many places are sloppy about it.)
316  */
317  AcquireDeletionLock(object, 0);
318 
319  /*
320  * Construct a list of objects to delete (ie, the given object plus
321  * everything directly or indirectly dependent on it).
322  */
323  targetObjects = new_object_addresses();
324 
325  findDependentObjects(object,
327  flags,
328  NULL, /* empty stack */
329  targetObjects,
330  NULL, /* no pendingObjects */
331  &depRel);
332 
333  /*
334  * Check if deletion is allowed, and report about cascaded deletes.
335  */
336  reportDependentObjects(targetObjects,
337  behavior,
338  flags,
339  object);
340 
341  /* do the deed */
342  deleteObjectsInList(targetObjects, &depRel, flags);
343 
344  /* And clean up */
345  free_object_addresses(targetObjects);
346 
347  heap_close(depRel, RowExclusiveLock);
348 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:460
#define DependRelationId
Definition: pg_depend.h:29
#define heap_close(r, l)
Definition: heapam.h:97
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2026
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2297
#define DEPFLAG_ORIGINAL
Definition: dependency.c:99
#define RowExclusiveLock
Definition: lockdefs.h:38
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:826
static void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1280
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:226
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:217
void performMultipleDeletions ( const ObjectAddresses objects,
DropBehavior  behavior,
int  flags 
)

Definition at line 360 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().

362 {
363  Relation depRel;
364  ObjectAddresses *targetObjects;
365  int i;
366 
367  /* No work if no objects... */
368  if (objects->numrefs <= 0)
369  return;
370 
371  /*
372  * We save some cycles by opening pg_depend just once and passing the
373  * Relation pointer down to all the recursive deletion steps.
374  */
376 
377  /*
378  * Construct a list of objects to delete (ie, the given objects plus
379  * everything directly or indirectly dependent on them). Note that
380  * because we pass the whole objects list as pendingObjects context, we
381  * won't get a failure from trying to delete an object that is internally
382  * dependent on another one in the list; we'll just skip that object and
383  * delete it when we reach its owner.
384  */
385  targetObjects = new_object_addresses();
386 
387  for (i = 0; i < objects->numrefs; i++)
388  {
389  const ObjectAddress *thisobj = objects->refs + i;
390 
391  /*
392  * Acquire deletion lock on each target object. (Ideally the caller
393  * has done this already, but many places are sloppy about it.)
394  */
395  AcquireDeletionLock(thisobj, flags);
396 
397  findDependentObjects(thisobj,
399  flags,
400  NULL, /* empty stack */
401  targetObjects,
402  objects,
403  &depRel);
404  }
405 
406  /*
407  * Check if deletion is allowed, and report about cascaded deletes.
408  *
409  * If there's exactly one object being deleted, report it the same way as
410  * in performDeletion(), else we have to be vaguer.
411  */
412  reportDependentObjects(targetObjects,
413  behavior,
414  flags,
415  (objects->numrefs == 1 ? objects->refs : NULL));
416 
417  /* do the deed */
418  deleteObjectsInList(targetObjects, &depRel, flags);
419 
420  /* And clean up */
421  free_object_addresses(targetObjects);
422 
423  heap_close(depRel, RowExclusiveLock);
424 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:460
#define DependRelationId
Definition: pg_depend.h:29
#define heap_close(r, l)
Definition: heapam.h:97
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2026
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2297
#define DEPFLAG_ORIGINAL
Definition: dependency.c:99
ObjectAddress * refs
Definition: dependency.c:110
#define RowExclusiveLock
Definition: lockdefs.h:38
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:826
static void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1280
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define NULL
Definition: c.h:226
int i
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:217
void record_object_address_dependencies ( const ObjectAddress depender,
ObjectAddresses referenced,
DependencyType  behavior 
)

Definition at line 2283 of file dependency.c.

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

Referenced by makeConfigurationDependencies().

2286 {
2288  recordMultipleDependencies(depender,
2289  referenced->refs, referenced->numrefs,
2290  behavior);
2291 }
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:1935
ObjectAddress * refs
Definition: dependency.c:110
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 1332 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().

1335 {
1337 
1338  context.addrs = new_object_addresses();
1339 
1340  /* Set up interpretation for Vars at varlevelsup = 0 */
1341  context.rtables = list_make1(rtable);
1342 
1343  /* Scan the expression tree for referenceable objects */
1344  find_expr_references_walker(expr, &context);
1345 
1346  /* Remove any duplicates */
1348 
1349  /* And record 'em */
1350  recordMultipleDependencies(depender,
1351  context.addrs->refs, context.addrs->numrefs,
1352  behavior);
1353 
1354  free_object_addresses(context.addrs);
1355 }
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1468
ObjectAddresses * addrs
Definition: dependency.c:129
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2026
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2297
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:1935
#define list_make1(x1)
Definition: pg_list.h:133
ObjectAddress * refs
Definition: dependency.c:110
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 1373 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().

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

Definition at line 1307 of file dependency.c.

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

Referenced by findDependentObjects().

1308 {
1309  if (object->classId == RelationRelationId)
1311  else
1312  /* assume we should lock the whole object not a sub-object */
1313  UnlockDatabaseObject(object->classId, object->objectId, 0,
1315 }
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 826 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().

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

Definition at line 2234 of file dependency.c.

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

Referenced by findDependentObjects().

2237 {
2238  bool result = false;
2239  ObjectAddressStack *stackptr;
2240 
2241  for (stackptr = stack; stackptr; stackptr = stackptr->next)
2242  {
2243  const ObjectAddress *thisobj = stackptr->object;
2244 
2245  if (object->classId == thisobj->classId &&
2246  object->objectId == thisobj->objectId)
2247  {
2248  if (object->objectSubId == thisobj->objectSubId)
2249  {
2250  stackptr->flags |= flags;
2251  result = true;
2252  }
2253  else if (thisobj->objectSubId == 0)
2254  {
2255  /*
2256  * We're visiting a column with whole table already on stack.
2257  * As in object_address_present_add_flags(), we can skip
2258  * further processing of the subobject, but we don't want to
2259  * propagate flags for the subobject to the whole object.
2260  */
2261  result = true;
2262  }
2263  else if (object->objectSubId == 0)
2264  {
2265  /*
2266  * We're visiting a table with column already on stack. As in
2267  * object_address_present_add_flags(), we should propagate
2268  * flags for the whole object to each of its subobjects.
2269  */
2270  stackptr->flags |= flags;
2271  }
2272  }
2273  }
2274 
2275  return result;
2276 }
struct ObjectAddressStack * next
Definition: dependency.c:123
const ObjectAddress * object
Definition: dependency.c:121

Variable Documentation

const Oid object_classes[]
static

Definition at line 137 of file dependency.c.

Referenced by add_object_address().