PostgreSQL Source Code  git master
dependency.h File Reference
Include dependency graph for dependency.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LAST_OCLASS   OCLASS_TRANSFORM
 
#define PERFORM_DELETION_INTERNAL   0x0001 /* internal action */
 
#define PERFORM_DELETION_CONCURRENTLY   0x0002 /* concurrent drop */
 
#define PERFORM_DELETION_QUIETLY   0x0004 /* suppress notices */
 
#define PERFORM_DELETION_SKIP_ORIGINAL   0x0008 /* keep original obj */
 
#define PERFORM_DELETION_SKIP_EXTENSIONS   0x0010 /* keep extensions */
 
#define PERFORM_DELETION_CONCURRENT_LOCK
 

Typedefs

typedef enum DependencyType DependencyType
 
typedef enum SharedDependencyType SharedDependencyType
 
typedef struct ObjectAddresses ObjectAddresses
 
typedef enum ObjectClass ObjectClass
 

Enumerations

enum  DependencyType {
  DEPENDENCY_NORMAL = 'n', DEPENDENCY_AUTO = 'a', DEPENDENCY_INTERNAL = 'i', DEPENDENCY_PARTITION_PRI = 'P',
  DEPENDENCY_PARTITION_SEC = 'S', DEPENDENCY_EXTENSION = 'e', DEPENDENCY_AUTO_EXTENSION = 'x', DEPENDENCY_PIN = 'p'
}
 
enum  SharedDependencyType {
  SHARED_DEPENDENCY_PIN = 'p', SHARED_DEPENDENCY_OWNER = 'o', SHARED_DEPENDENCY_ACL = 'a', SHARED_DEPENDENCY_POLICY = 'r',
  SHARED_DEPENDENCY_INVALID = 0
}
 
enum  ObjectClass {
  OCLASS_CLASS, OCLASS_PROC, OCLASS_TYPE, OCLASS_CAST,
  OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_CONVERSION, OCLASS_DEFAULT,
  OCLASS_LANGUAGE, OCLASS_LARGEOBJECT, OCLASS_OPERATOR, OCLASS_OPCLASS,
  OCLASS_OPFAMILY, OCLASS_AM, OCLASS_AMOP, OCLASS_AMPROC,
  OCLASS_REWRITE, OCLASS_TRIGGER, OCLASS_SCHEMA, OCLASS_STATISTIC_EXT,
  OCLASS_TSPARSER, OCLASS_TSDICT, OCLASS_TSTEMPLATE, OCLASS_TSCONFIG,
  OCLASS_ROLE, OCLASS_DATABASE, OCLASS_TBLSPACE, OCLASS_FDW,
  OCLASS_FOREIGN_SERVER, OCLASS_USER_MAPPING, OCLASS_DEFACL, OCLASS_EXTENSION,
  OCLASS_EVENT_TRIGGER, OCLASS_POLICY, OCLASS_PUBLICATION, OCLASS_PUBLICATION_REL,
  OCLASS_SUBSCRIPTION, OCLASS_TRANSFORM
}
 

Functions

void AcquireDeletionLock (const ObjectAddress *object, int flags)
 
void ReleaseDeletionLock (const ObjectAddress *object)
 
void performDeletion (const ObjectAddress *object, DropBehavior behavior, int flags)
 
void performMultipleDeletions (const ObjectAddresses *objects, DropBehavior behavior, 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 reverse_self)
 
ObjectClass getObjectClass (const ObjectAddress *object)
 
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 sort_object_addresses (ObjectAddresses *addrs)
 
void free_object_addresses (ObjectAddresses *addrs)
 
void recordDependencyOn (const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
 
void recordMultipleDependencies (const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
 
void recordDependencyOnCurrentExtension (const ObjectAddress *object, bool isReplace)
 
long deleteDependencyRecordsFor (Oid classId, Oid objectId, bool skipExtensionDeps)
 
long deleteDependencyRecordsForClass (Oid classId, Oid objectId, Oid refclassId, char deptype)
 
long deleteDependencyRecordsForSpecific (Oid classId, Oid objectId, char deptype, Oid refclassId, Oid refobjectId)
 
long changeDependencyFor (Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
 
long changeDependenciesOf (Oid classId, Oid oldObjectId, Oid newObjectId)
 
long changeDependenciesOn (Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
 
Oid getExtensionOfObject (Oid classId, Oid objectId)
 
ListgetAutoExtensionsOfObject (Oid classId, Oid objectId)
 
bool sequenceIsOwned (Oid seqId, char deptype, Oid *tableId, int32 *colId)
 
ListgetOwnedSequences (Oid relid)
 
Oid getIdentitySequence (Oid relid, AttrNumber attnum, bool missing_ok)
 
Oid get_constraint_index (Oid constraintId)
 
Oid get_index_constraint (Oid indexId)
 
Listget_index_ref_constraints (Oid indexId)
 
void recordSharedDependencyOn (ObjectAddress *depender, ObjectAddress *referenced, SharedDependencyType deptype)
 
void deleteSharedDependencyRecordsFor (Oid classId, Oid objectId, int32 objectSubId)
 
void recordDependencyOnOwner (Oid classId, Oid objectId, Oid owner)
 
void changeDependencyOnOwner (Oid classId, Oid objectId, Oid newOwnerId)
 
void updateAclDependencies (Oid classId, Oid objectId, int32 objectSubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
 
bool checkSharedDependencies (Oid classId, Oid objectId, char **detail_msg, char **detail_log_msg)
 
void shdepLockAndCheckObject (Oid classId, Oid objectId)
 
void copyTemplateDependencies (Oid templateDbId, Oid newDbId)
 
void dropDatabaseDependencies (Oid databaseId)
 
void shdepDropOwned (List *relids, DropBehavior behavior)
 
void shdepReassignOwned (List *relids, Oid newrole)
 

Macro Definition Documentation

◆ LAST_OCLASS

#define LAST_OCLASS   OCLASS_TRANSFORM

Definition at line 131 of file dependency.h.

Referenced by add_object_address().

◆ PERFORM_DELETION_CONCURRENT_LOCK

#define PERFORM_DELETION_CONCURRENT_LOCK
Value:
0x0020 /* normal drop with
* concurrent lock mode */

Definition at line 139 of file dependency.h.

Referenced by doDeletion(), and ReindexRelationConcurrently().

◆ PERFORM_DELETION_CONCURRENTLY

#define PERFORM_DELETION_CONCURRENTLY   0x0002 /* concurrent drop */

Definition at line 135 of file dependency.h.

Referenced by AcquireDeletionLock(), deleteOneObject(), doDeletion(), and RemoveRelations().

◆ PERFORM_DELETION_INTERNAL

◆ PERFORM_DELETION_QUIETLY

#define PERFORM_DELETION_QUIETLY   0x0004 /* suppress notices */

◆ PERFORM_DELETION_SKIP_EXTENSIONS

#define PERFORM_DELETION_SKIP_EXTENSIONS   0x0010 /* keep extensions */

Definition at line 138 of file dependency.h.

Referenced by do_autovacuum(), findDependentObjects(), and RemoveTempRelations().

◆ PERFORM_DELETION_SKIP_ORIGINAL

#define PERFORM_DELETION_SKIP_ORIGINAL   0x0008 /* keep original obj */

Definition at line 137 of file dependency.h.

Referenced by deleteObjectsInList(), and RemoveTempRelations().

Typedef Documentation

◆ DependencyType

◆ ObjectAddresses

Definition at line 83 of file dependency.h.

◆ ObjectClass

typedef enum ObjectClass ObjectClass

◆ SharedDependencyType

Enumeration Type Documentation

◆ DependencyType

Enumerator
DEPENDENCY_NORMAL 
DEPENDENCY_AUTO 
DEPENDENCY_INTERNAL 
DEPENDENCY_PARTITION_PRI 
DEPENDENCY_PARTITION_SEC 
DEPENDENCY_EXTENSION 
DEPENDENCY_AUTO_EXTENSION 
DEPENDENCY_PIN 

Definition at line 31 of file dependency.h.

◆ ObjectClass

Enumerator
OCLASS_CLASS 
OCLASS_PROC 
OCLASS_TYPE 
OCLASS_CAST 
OCLASS_COLLATION 
OCLASS_CONSTRAINT 
OCLASS_CONVERSION 
OCLASS_DEFAULT 
OCLASS_LANGUAGE 
OCLASS_LARGEOBJECT 
OCLASS_OPERATOR 
OCLASS_OPCLASS 
OCLASS_OPFAMILY 
OCLASS_AM 
OCLASS_AMOP 
OCLASS_AMPROC 
OCLASS_REWRITE 
OCLASS_TRIGGER 
OCLASS_SCHEMA 
OCLASS_STATISTIC_EXT 
OCLASS_TSPARSER 
OCLASS_TSDICT 
OCLASS_TSTEMPLATE 
OCLASS_TSCONFIG 
OCLASS_ROLE 
OCLASS_DATABASE 
OCLASS_TBLSPACE 
OCLASS_FDW 
OCLASS_FOREIGN_SERVER 
OCLASS_USER_MAPPING 
OCLASS_DEFACL 
OCLASS_EXTENSION 
OCLASS_EVENT_TRIGGER 
OCLASS_POLICY 
OCLASS_PUBLICATION 
OCLASS_PUBLICATION_REL 
OCLASS_SUBSCRIPTION 
OCLASS_TRANSFORM 

Definition at line 89 of file dependency.h.

90 {
91  OCLASS_CLASS, /* pg_class */
92  OCLASS_PROC, /* pg_proc */
93  OCLASS_TYPE, /* pg_type */
94  OCLASS_CAST, /* pg_cast */
95  OCLASS_COLLATION, /* pg_collation */
96  OCLASS_CONSTRAINT, /* pg_constraint */
97  OCLASS_CONVERSION, /* pg_conversion */
98  OCLASS_DEFAULT, /* pg_attrdef */
99  OCLASS_LANGUAGE, /* pg_language */
100  OCLASS_LARGEOBJECT, /* pg_largeobject */
101  OCLASS_OPERATOR, /* pg_operator */
102  OCLASS_OPCLASS, /* pg_opclass */
103  OCLASS_OPFAMILY, /* pg_opfamily */
104  OCLASS_AM, /* pg_am */
105  OCLASS_AMOP, /* pg_amop */
106  OCLASS_AMPROC, /* pg_amproc */
107  OCLASS_REWRITE, /* pg_rewrite */
108  OCLASS_TRIGGER, /* pg_trigger */
109  OCLASS_SCHEMA, /* pg_namespace */
110  OCLASS_STATISTIC_EXT, /* pg_statistic_ext */
111  OCLASS_TSPARSER, /* pg_ts_parser */
112  OCLASS_TSDICT, /* pg_ts_dict */
113  OCLASS_TSTEMPLATE, /* pg_ts_template */
114  OCLASS_TSCONFIG, /* pg_ts_config */
115  OCLASS_ROLE, /* pg_authid */
116  OCLASS_DATABASE, /* pg_database */
117  OCLASS_TBLSPACE, /* pg_tablespace */
118  OCLASS_FDW, /* pg_foreign_data_wrapper */
119  OCLASS_FOREIGN_SERVER, /* pg_foreign_server */
120  OCLASS_USER_MAPPING, /* pg_user_mapping */
121  OCLASS_DEFACL, /* pg_default_acl */
122  OCLASS_EXTENSION, /* pg_extension */
123  OCLASS_EVENT_TRIGGER, /* pg_event_trigger */
124  OCLASS_POLICY, /* pg_policy */
125  OCLASS_PUBLICATION, /* pg_publication */
126  OCLASS_PUBLICATION_REL, /* pg_publication_rel */
127  OCLASS_SUBSCRIPTION, /* pg_subscription */
128  OCLASS_TRANSFORM /* pg_transform */
129 } ObjectClass;
ObjectClass
Definition: dependency.h:89

◆ SharedDependencyType

Enumerator
SHARED_DEPENDENCY_PIN 
SHARED_DEPENDENCY_OWNER 
SHARED_DEPENDENCY_ACL 
SHARED_DEPENDENCY_POLICY 
SHARED_DEPENDENCY_INVALID 

Definition at line 73 of file dependency.h.

Function Documentation

◆ AcquireDeletionLock()

void AcquireDeletionLock ( const ObjectAddress object,
int  flags 
)

Definition at line 1530 of file dependency.c.

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

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

1531 {
1532  if (object->classId == RelationRelationId)
1533  {
1534  /*
1535  * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1536  * the index for the moment. index_drop() will promote the lock once
1537  * it's safe to do so. In all other cases we need full exclusive
1538  * lock.
1539  */
1540  if (flags & PERFORM_DELETION_CONCURRENTLY)
1542  else
1544  }
1545  else
1546  {
1547  /* assume we should lock the whole object not a sub-object */
1548  LockDatabaseObject(object->classId, object->objectId, 0,
1550  }
1551 }
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:135
void LockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:976
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define AccessExclusiveLock
Definition: lockdefs.h:45
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108

◆ add_exact_object_address()

void add_exact_object_address ( const ObjectAddress object,
ObjectAddresses addrs 
)

Definition at line 2467 of file dependency.c.

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

Referenced by AggregateCreate(), AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterTypeNamespaceInternal(), ATExecDropColumn(), ATPostAlterTypeCleanup(), CastCreate(), CreateConstraintEntry(), CreateProceduralLanguage(), CreateTransform(), DropClonedTriggersFromPartition(), GenerateTypeDependencies(), heap_create_with_catalog(), index_create(), InsertExtensionTuple(), makeConfigurationDependencies(), makeDictionaryDependencies(), makeOperatorDependencies(), makeParserDependencies(), makeTSTemplateDependencies(), PreCommit_on_commit_actions(), ProcedureCreate(), RangeCreate(), recordDependencyOnSingleRelExpr(), ReindexRelationConcurrently(), RemoveObjects(), RemoveRelations(), shdepDropOwned(), and StorePartitionKey().

2469 {
2470  ObjectAddress *item;
2471 
2472  /* enlarge array if needed */
2473  if (addrs->numrefs >= addrs->maxrefs)
2474  {
2475  addrs->maxrefs *= 2;
2476  addrs->refs = (ObjectAddress *)
2477  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2478  Assert(!addrs->extras);
2479  }
2480  /* record this item */
2481  item = addrs->refs + addrs->numrefs;
2482  *item = *object;
2483  addrs->numrefs++;
2484 }
ObjectAddress * refs
Definition: dependency.c:111
#define Assert(condition)
Definition: c.h:745
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
ObjectAddressExtra * extras
Definition: dependency.c:112

◆ changeDependenciesOf()

long changeDependenciesOf ( Oid  classId,
Oid  oldObjectId,
Oid  newObjectId 
)

Definition at line 498 of file pg_depend.c.

References BTEqualStrategyNumber, CatalogTupleUpdate(), DependDependerIndexId, GETSTRUCT, heap_copytuple(), heap_freetuple(), HeapTupleIsValid, sort-test::key, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by index_concurrently_swap().

500 {
501  long count = 0;
502  Relation depRel;
503  ScanKeyData key[2];
504  SysScanDesc scan;
505  HeapTuple tup;
506 
507  depRel = table_open(DependRelationId, RowExclusiveLock);
508 
509  ScanKeyInit(&key[0],
510  Anum_pg_depend_classid,
511  BTEqualStrategyNumber, F_OIDEQ,
512  ObjectIdGetDatum(classId));
513  ScanKeyInit(&key[1],
514  Anum_pg_depend_objid,
515  BTEqualStrategyNumber, F_OIDEQ,
516  ObjectIdGetDatum(oldObjectId));
517 
518  scan = systable_beginscan(depRel, DependDependerIndexId, true,
519  NULL, 2, key);
520 
521  while (HeapTupleIsValid((tup = systable_getnext(scan))))
522  {
523  Form_pg_depend depform;
524 
525  /* make a modifiable copy */
526  tup = heap_copytuple(tup);
527  depform = (Form_pg_depend) GETSTRUCT(tup);
528 
529  depform->objid = newObjectId;
530 
531  CatalogTupleUpdate(depRel, &tup->t_self, tup);
532 
533  heap_freetuple(tup);
534 
535  count++;
536  }
537 
538  systable_endscan(scan);
539 
540  table_close(depRel, RowExclusiveLock);
541 
542  return count;
543 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ changeDependenciesOn()

long changeDependenciesOn ( Oid  refClassId,
Oid  oldRefObjectId,
Oid  newRefObjectId 
)

Definition at line 554 of file pg_depend.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), CatalogTupleUpdate(), ObjectAddress::classId, DependReferenceIndexId, ereport, errcode(), errmsg(), ERROR, getObjectDescription(), GETSTRUCT, heap_copytuple(), heap_freetuple(), HeapTupleIsValid, isObjectPinned(), sort-test::key, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by index_concurrently_swap().

556 {
557  long count = 0;
558  Relation depRel;
559  ScanKeyData key[2];
560  SysScanDesc scan;
561  HeapTuple tup;
562  ObjectAddress objAddr;
563  bool newIsPinned;
564 
565  depRel = table_open(DependRelationId, RowExclusiveLock);
566 
567  /*
568  * If oldRefObjectId is pinned, there won't be any dependency entries on
569  * it --- we can't cope in that case. (This isn't really worth expending
570  * code to fix, in current usage; it just means you can't rename stuff out
571  * of pg_catalog, which would likely be a bad move anyway.)
572  */
573  objAddr.classId = refClassId;
574  objAddr.objectId = oldRefObjectId;
575  objAddr.objectSubId = 0;
576 
577  if (isObjectPinned(&objAddr, depRel))
578  ereport(ERROR,
579  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
580  errmsg("cannot remove dependency on %s because it is a system object",
581  getObjectDescription(&objAddr, false))));
582 
583  /*
584  * We can handle adding a dependency on something pinned, though, since
585  * that just means deleting the dependency entry.
586  */
587  objAddr.objectId = newRefObjectId;
588 
589  newIsPinned = isObjectPinned(&objAddr, depRel);
590 
591  /* Now search for dependency records */
592  ScanKeyInit(&key[0],
593  Anum_pg_depend_refclassid,
594  BTEqualStrategyNumber, F_OIDEQ,
595  ObjectIdGetDatum(refClassId));
596  ScanKeyInit(&key[1],
597  Anum_pg_depend_refobjid,
598  BTEqualStrategyNumber, F_OIDEQ,
599  ObjectIdGetDatum(oldRefObjectId));
600 
601  scan = systable_beginscan(depRel, DependReferenceIndexId, true,
602  NULL, 2, key);
603 
604  while (HeapTupleIsValid((tup = systable_getnext(scan))))
605  {
606  if (newIsPinned)
607  CatalogTupleDelete(depRel, &tup->t_self);
608  else
609  {
610  Form_pg_depend depform;
611 
612  /* make a modifiable copy */
613  tup = heap_copytuple(tup);
614  depform = (Form_pg_depend) GETSTRUCT(tup);
615 
616  depform->refobjid = newRefObjectId;
617 
618  CatalogTupleUpdate(depRel, &tup->t_self, tup);
619 
620  heap_freetuple(tup);
621  }
622 
623  count++;
624  }
625 
626  systable_endscan(scan);
627 
628  table_close(depRel, RowExclusiveLock);
629 
630  return count;
631 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependReferenceIndexId
Definition: indexing.h:162
int errcode(int sqlerrcode)
Definition: elog.c:610
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
static bool isObjectPinned(const ObjectAddress *object, Relation rel)
Definition: pg_depend.c:643
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
int errmsg(const char *fmt,...)
Definition: elog.c:824
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ changeDependencyFor()

long changeDependencyFor ( Oid  classId,
Oid  objectId,
Oid  refClassId,
Oid  oldRefObjectId,
Oid  newRefObjectId 
)

Definition at line 388 of file pg_depend.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), CatalogTupleUpdate(), ObjectAddress::classId, DependDependerIndexId, DEPENDENCY_NORMAL, GETSTRUCT, heap_copytuple(), heap_freetuple(), HeapTupleIsValid, isObjectPinned(), sort-test::key, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, recordDependencyOn(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by AlterExtensionNamespace(), AlterFunction(), AlterObjectNamespace_internal(), AlterRelationNamespaceInternal(), and AlterTypeNamespaceInternal().

391 {
392  long count = 0;
393  Relation depRel;
394  ScanKeyData key[2];
395  SysScanDesc scan;
396  HeapTuple tup;
397  ObjectAddress objAddr;
398  ObjectAddress depAddr;
399  bool oldIsPinned;
400  bool newIsPinned;
401 
402  depRel = table_open(DependRelationId, RowExclusiveLock);
403 
404  /*
405  * Check to see if either oldRefObjectId or newRefObjectId is pinned.
406  * Pinned objects should not have any dependency entries pointing to them,
407  * so in these cases we should add or remove a pg_depend entry, or do
408  * nothing at all, rather than update an entry as in the normal case.
409  */
410  objAddr.classId = refClassId;
411  objAddr.objectId = oldRefObjectId;
412  objAddr.objectSubId = 0;
413 
414  oldIsPinned = isObjectPinned(&objAddr, depRel);
415 
416  objAddr.objectId = newRefObjectId;
417 
418  newIsPinned = isObjectPinned(&objAddr, depRel);
419 
420  if (oldIsPinned)
421  {
422  table_close(depRel, RowExclusiveLock);
423 
424  /*
425  * If both are pinned, we need do nothing. However, return 1 not 0,
426  * else callers will think this is an error case.
427  */
428  if (newIsPinned)
429  return 1;
430 
431  /*
432  * There is no old dependency record, but we should insert a new one.
433  * Assume a normal dependency is wanted.
434  */
435  depAddr.classId = classId;
436  depAddr.objectId = objectId;
437  depAddr.objectSubId = 0;
438  recordDependencyOn(&depAddr, &objAddr, DEPENDENCY_NORMAL);
439 
440  return 1;
441  }
442 
443  /* There should be existing dependency record(s), so search. */
444  ScanKeyInit(&key[0],
445  Anum_pg_depend_classid,
446  BTEqualStrategyNumber, F_OIDEQ,
447  ObjectIdGetDatum(classId));
448  ScanKeyInit(&key[1],
449  Anum_pg_depend_objid,
450  BTEqualStrategyNumber, F_OIDEQ,
451  ObjectIdGetDatum(objectId));
452 
453  scan = systable_beginscan(depRel, DependDependerIndexId, true,
454  NULL, 2, key);
455 
456  while (HeapTupleIsValid((tup = systable_getnext(scan))))
457  {
458  Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
459 
460  if (depform->refclassid == refClassId &&
461  depform->refobjid == oldRefObjectId)
462  {
463  if (newIsPinned)
464  CatalogTupleDelete(depRel, &tup->t_self);
465  else
466  {
467  /* make a modifiable copy */
468  tup = heap_copytuple(tup);
469  depform = (Form_pg_depend) GETSTRUCT(tup);
470 
471  depform->refobjid = newRefObjectId;
472 
473  CatalogTupleUpdate(depRel, &tup->t_self, tup);
474 
475  heap_freetuple(tup);
476  }
477 
478  count++;
479  }
480  }
481 
482  systable_endscan(scan);
483 
484  table_close(depRel, RowExclusiveLock);
485 
486  return count;
487 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
static bool isObjectPinned(const ObjectAddress *object, Relation rel)
Definition: pg_depend.c:643
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ changeDependencyOnOwner()

void changeDependencyOnOwner ( Oid  classId,
Oid  objectId,
Oid  newOwnerId 
)

Definition at line 309 of file pg_shdepend.c.

References RowExclusiveLock, SHARED_DEPENDENCY_ACL, SHARED_DEPENDENCY_OWNER, shdepChangeDep(), shdepDropDependency(), table_close(), and table_open().

Referenced by AlterDatabaseOwner(), AlterEventTriggerOwner_internal(), AlterForeignDataWrapperOwner_internal(), AlterForeignServerOwner_internal(), AlterObjectOwner_internal(), AlterPublicationOwner_internal(), AlterSchemaOwner_internal(), AlterSubscriptionOwner_internal(), AlterTypeOwner_oid(), and ATExecChangeOwner().

310 {
311  Relation sdepRel;
312 
313  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
314 
315  /* Adjust the SHARED_DEPENDENCY_OWNER entry */
316  shdepChangeDep(sdepRel,
317  classId, objectId, 0,
318  AuthIdRelationId, newOwnerId,
320 
321  /*----------
322  * There should never be a SHARED_DEPENDENCY_ACL entry for the owner,
323  * so get rid of it if there is one. This can happen if the new owner
324  * was previously granted some rights to the object.
325  *
326  * This step is analogous to aclnewowner's removal of duplicate entries
327  * in the ACL. We have to do it to handle this scenario:
328  * A grants some rights on an object to B
329  * ALTER OWNER changes the object's owner to B
330  * ALTER OWNER changes the object's owner to C
331  * The third step would remove all mention of B from the object's ACL,
332  * but we'd still have a SHARED_DEPENDENCY_ACL for B if we did not do
333  * things this way.
334  *
335  * The rule against having a SHARED_DEPENDENCY_ACL entry for the owner
336  * allows us to fix things up in just this one place, without having
337  * to make the various ALTER OWNER routines each know about it.
338  *----------
339  */
340  shdepDropDependency(sdepRel, classId, objectId, 0, true,
341  AuthIdRelationId, newOwnerId,
343 
344  table_close(sdepRel, RowExclusiveLock);
345 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static void shdepChangeDep(Relation sdepRel, Oid classid, Oid objid, int32 objsubid, Oid refclassid, Oid refobjid, SharedDependencyType deptype)
Definition: pg_shdepend.c:199
#define RowExclusiveLock
Definition: lockdefs.h:38
static void shdepDropDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, bool drop_subobjects, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:1022
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ checkSharedDependencies()

bool checkSharedDependencies ( Oid  classId,
Oid  objectId,
char **  detail_msg,
char **  detail_log_msg 
)

Definition at line 577 of file pg_shdepend.c.

References AccessShareLock, appendStringInfo(), BTEqualStrategyNumber, remoteDep::count, StringInfoData::data, remoteDep::dbOid, ShDependObjectInfo::deptype, ereport, errcode(), errmsg(), ERROR, getObjectDescription(), GETSTRUCT, HeapTupleIsValid, i, initStringInfo(), InvalidOid, sort-test::key, lappend(), StringInfoData::len, lfirst, list_free_deep(), LOCAL_OBJECT, MAX_REPORTED_DEPS, MyDatabaseId, ngettext, NIL, ShDependObjectInfo::object, ObjectIdGetDatum, ShDependObjectInfo::objtype, palloc(), pfree(), qsort, REMOTE_OBJECT, repalloc(), ScanKeyInit(), shared_dependency_comparator(), SHARED_DEPENDENCY_INVALID, SHARED_DEPENDENCY_PIN, SHARED_OBJECT, SharedDependReferenceIndexId, storeObjectDescription(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by DropRole().

579 {
580  Relation sdepRel;
581  ScanKeyData key[2];
582  SysScanDesc scan;
583  HeapTuple tup;
584  int numReportedDeps = 0;
585  int numNotReportedDeps = 0;
586  int numNotReportedDbs = 0;
587  List *remDeps = NIL;
588  ListCell *cell;
589  ObjectAddress object;
590  ShDependObjectInfo *objects;
591  int numobjects;
592  int allocedobjects;
593  StringInfoData descs;
594  StringInfoData alldescs;
595 
596  /*
597  * We limit the number of dependencies reported to the client to
598  * MAX_REPORTED_DEPS, since client software may not deal well with
599  * enormous error strings. The server log always gets a full report.
600  *
601  * For stability of regression test results, we sort local and shared
602  * objects by OID before reporting them. We don't worry about the order
603  * in which other databases are reported, though.
604  */
605 #define MAX_REPORTED_DEPS 100
606 
607  allocedobjects = 128; /* arbitrary initial array size */
608  objects = (ShDependObjectInfo *)
609  palloc(allocedobjects * sizeof(ShDependObjectInfo));
610  numobjects = 0;
611  initStringInfo(&descs);
612  initStringInfo(&alldescs);
613 
614  sdepRel = table_open(SharedDependRelationId, AccessShareLock);
615 
616  ScanKeyInit(&key[0],
617  Anum_pg_shdepend_refclassid,
618  BTEqualStrategyNumber, F_OIDEQ,
619  ObjectIdGetDatum(classId));
620  ScanKeyInit(&key[1],
621  Anum_pg_shdepend_refobjid,
622  BTEqualStrategyNumber, F_OIDEQ,
623  ObjectIdGetDatum(objectId));
624 
625  scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
626  NULL, 2, key);
627 
628  while (HeapTupleIsValid(tup = systable_getnext(scan)))
629  {
630  Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
631 
632  /* This case can be dispatched quickly */
633  if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
634  {
635  object.classId = classId;
636  object.objectId = objectId;
637  object.objectSubId = 0;
638  ereport(ERROR,
639  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
640  errmsg("cannot drop %s because it is required by the database system",
641  getObjectDescription(&object, false))));
642  }
643 
644  object.classId = sdepForm->classid;
645  object.objectId = sdepForm->objid;
646  object.objectSubId = sdepForm->objsubid;
647 
648  /*
649  * If it's a dependency local to this database or it's a shared
650  * object, add it to the objects array.
651  *
652  * If it's a remote dependency, keep track of it so we can report the
653  * number of them later.
654  */
655  if (sdepForm->dbid == MyDatabaseId ||
656  sdepForm->dbid == InvalidOid)
657  {
658  if (numobjects >= allocedobjects)
659  {
660  allocedobjects *= 2;
661  objects = (ShDependObjectInfo *)
662  repalloc(objects,
663  allocedobjects * sizeof(ShDependObjectInfo));
664  }
665  objects[numobjects].object = object;
666  objects[numobjects].deptype = sdepForm->deptype;
667  objects[numobjects].objtype = (sdepForm->dbid == MyDatabaseId) ?
669  numobjects++;
670  }
671  else
672  {
673  /* It's not local nor shared, so it must be remote. */
674  remoteDep *dep;
675  bool stored = false;
676 
677  /*
678  * XXX this info is kept on a simple List. Maybe it's not good
679  * for performance, but using a hash table seems needlessly
680  * complex. The expected number of databases is not high anyway,
681  * I suppose.
682  */
683  foreach(cell, remDeps)
684  {
685  dep = lfirst(cell);
686  if (dep->dbOid == sdepForm->dbid)
687  {
688  dep->count++;
689  stored = true;
690  break;
691  }
692  }
693  if (!stored)
694  {
695  dep = (remoteDep *) palloc(sizeof(remoteDep));
696  dep->dbOid = sdepForm->dbid;
697  dep->count = 1;
698  remDeps = lappend(remDeps, dep);
699  }
700  }
701  }
702 
703  systable_endscan(scan);
704 
705  table_close(sdepRel, AccessShareLock);
706 
707  /*
708  * Sort and report local and shared objects.
709  */
710  if (numobjects > 1)
711  qsort((void *) objects, numobjects,
713 
714  for (int i = 0; i < numobjects; i++)
715  {
716  if (numReportedDeps < MAX_REPORTED_DEPS)
717  {
718  numReportedDeps++;
719  storeObjectDescription(&descs,
720  objects[i].objtype,
721  &objects[i].object,
722  objects[i].deptype,
723  0);
724  }
725  else
726  numNotReportedDeps++;
727  storeObjectDescription(&alldescs,
728  objects[i].objtype,
729  &objects[i].object,
730  objects[i].deptype,
731  0);
732  }
733 
734  /*
735  * Summarize dependencies in remote databases.
736  */
737  foreach(cell, remDeps)
738  {
739  remoteDep *dep = lfirst(cell);
740 
741  object.classId = DatabaseRelationId;
742  object.objectId = dep->dbOid;
743  object.objectSubId = 0;
744 
745  if (numReportedDeps < MAX_REPORTED_DEPS)
746  {
747  numReportedDeps++;
748  storeObjectDescription(&descs, REMOTE_OBJECT, &object,
750  }
751  else
752  numNotReportedDbs++;
753  storeObjectDescription(&alldescs, REMOTE_OBJECT, &object,
755  }
756 
757  pfree(objects);
758  list_free_deep(remDeps);
759 
760  if (descs.len == 0)
761  {
762  pfree(descs.data);
763  pfree(alldescs.data);
764  *detail_msg = *detail_log_msg = NULL;
765  return false;
766  }
767 
768  if (numNotReportedDeps > 0)
769  appendStringInfo(&descs, ngettext("\nand %d other object "
770  "(see server log for list)",
771  "\nand %d other objects "
772  "(see server log for list)",
773  numNotReportedDeps),
774  numNotReportedDeps);
775  if (numNotReportedDbs > 0)
776  appendStringInfo(&descs, ngettext("\nand objects in %d other database "
777  "(see server log for list)",
778  "\nand objects in %d other databases "
779  "(see server log for list)",
780  numNotReportedDbs),
781  numNotReportedDbs);
782 
783  *detail_msg = descs.data;
784  *detail_log_msg = alldescs.data;
785  return true;
786 }
#define NIL
Definition: pg_list.h:65
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static int shared_dependency_comparator(const void *a, const void *b)
Definition: pg_shdepend.c:509
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:70
#define AccessShareLock
Definition: lockdefs.h:36
#define MAX_REPORTED_DEPS
static void storeObjectDescription(StringInfo descs, SharedDependencyObjectType type, ObjectAddress *object, SharedDependencyType deptype, int count)
Definition: pg_shdepend.c:1182
int errcode(int sqlerrcode)
Definition: elog.c:610
void list_free_deep(List *list)
Definition: list.c:1390
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
SharedDependencyObjectType objtype
Definition: pg_shdepend.c:80
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
void pfree(void *pointer)
Definition: mcxt.c:1057
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
List * lappend(List *list, void *datum)
Definition: list.c:321
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ngettext(s, p, n)
Definition: c.h:1123
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:190
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define SharedDependReferenceIndexId
Definition: indexing.h:236
#define qsort(a, b, c, d)
Definition: port.h:475
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31
ObjectAddress object
Definition: pg_shdepend.c:78

◆ copyTemplateDependencies()

void copyTemplateDependencies ( Oid  templateDbId,
Oid  newDbId 
)

Definition at line 796 of file pg_shdepend.c.

References BTEqualStrategyNumber, CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTuplesMultiInsertWithInfo(), ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecStoreVirtualTuple(), FormData_pg_shdepend, GETSTRUCT, HeapTupleIsValid, i, sort-test::key, MakeSingleTupleTableSlot(), MAX_CATALOG_MULTI_INSERT_BYTES, ObjectIdGetDatum, palloc(), pfree(), RelationGetDescr, RowExclusiveLock, ScanKeyInit(), SharedDependDependerIndexId, systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), TupleTableSlot::tts_values, and TTSOpsHeapTuple.

Referenced by createdb().

797 {
798  Relation sdepRel;
799  TupleDesc sdepDesc;
800  ScanKeyData key[1];
801  SysScanDesc scan;
802  HeapTuple tup;
803  CatalogIndexState indstate;
804  TupleTableSlot **slot;
805  int max_slots,
806  slot_init_count,
807  slot_stored_count;
808 
809  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
810  sdepDesc = RelationGetDescr(sdepRel);
811 
812  /*
813  * Allocate the slots to use, but delay costly initialization until we
814  * know that they will be used.
815  */
817  slot = palloc(sizeof(TupleTableSlot *) * max_slots);
818 
819  indstate = CatalogOpenIndexes(sdepRel);
820 
821  /* Scan all entries with dbid = templateDbId */
822  ScanKeyInit(&key[0],
823  Anum_pg_shdepend_dbid,
824  BTEqualStrategyNumber, F_OIDEQ,
825  ObjectIdGetDatum(templateDbId));
826 
827  scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
828  NULL, 1, key);
829 
830  /* number of slots currently storing tuples */
831  slot_stored_count = 0;
832  /* number of slots currently initialized */
833  slot_init_count = 0;
834 
835  /*
836  * Copy the entries of the original database, changing the database Id to
837  * that of the new database. Note that because we are not copying rows
838  * with dbId == 0 (ie, rows describing dependent shared objects) we won't
839  * copy the ownership dependency of the template database itself; this is
840  * what we want.
841  */
842  while (HeapTupleIsValid(tup = systable_getnext(scan)))
843  {
844  Form_pg_shdepend shdep;
845 
846  if (slot_init_count < max_slots)
847  {
848  slot[slot_stored_count] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple);
849  slot_init_count++;
850  }
851 
852  ExecClearTuple(slot[slot_stored_count]);
853 
854  shdep = (Form_pg_shdepend) GETSTRUCT(tup);
855 
856  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_dbid] = ObjectIdGetDatum(newDbId);
857  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_classid] = shdep->classid;
858  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objid] = shdep->objid;
859  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objsubid] = shdep->objsubid;
860  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refclassid] = shdep->refclassid;
861  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refobjid] = shdep->refobjid;
862  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_deptype] = shdep->deptype;
863 
864  ExecStoreVirtualTuple(slot[slot_stored_count]);
865  slot_stored_count++;
866 
867  /* If slots are full, insert a batch of tuples */
868  if (slot_stored_count == max_slots)
869  {
870  CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
871  slot_stored_count = 0;
872  }
873  }
874 
875  /* Insert any tuples left in the buffer */
876  if (slot_stored_count > 0)
877  CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
878 
879  systable_endscan(scan);
880 
881  CatalogCloseIndexes(indstate);
882  table_close(sdepRel, RowExclusiveLock);
883 
884  /* Drop only the number of slots used */
885  for (int i = 0; i < slot_init_count; i++)
887  pfree(slot);
888 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define SharedDependDependerIndexId
Definition: indexing.h:234
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define RelationGetDescr(relation)
Definition: rel.h:482
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition: indexing.h:36
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:70
Datum * tts_values
Definition: tuptable.h:126
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition: indexing.c:261
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
#define RowExclusiveLock
Definition: lockdefs.h:38
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_shdepend
Definition: pg_shdepend.h:63
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void * palloc(Size size)
Definition: mcxt.c:950
int i
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522

◆ deleteDependencyRecordsFor()

long deleteDependencyRecordsFor ( Oid  classId,
Oid  objectId,
bool  skipExtensionDeps 
)

Definition at line 232 of file pg_depend.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), DependDependerIndexId, DEPENDENCY_EXTENSION, GETSTRUCT, HeapTupleIsValid, sort-test::key, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by AlterPolicy(), CreateProceduralLanguage(), CreateTransform(), DefineQueryRewrite(), GenerateTypeDependencies(), InsertRule(), makeConfigurationDependencies(), makeOperatorDependencies(), ProcedureCreate(), RemoveRoleFromObjectPolicy(), swap_relation_files(), and tryAttachPartitionForeignKey().

234 {
235  long count = 0;
236  Relation depRel;
237  ScanKeyData key[2];
238  SysScanDesc scan;
239  HeapTuple tup;
240 
241  depRel = table_open(DependRelationId, RowExclusiveLock);
242 
243  ScanKeyInit(&key[0],
244  Anum_pg_depend_classid,
245  BTEqualStrategyNumber, F_OIDEQ,
246  ObjectIdGetDatum(classId));
247  ScanKeyInit(&key[1],
248  Anum_pg_depend_objid,
249  BTEqualStrategyNumber, F_OIDEQ,
250  ObjectIdGetDatum(objectId));
251 
252  scan = systable_beginscan(depRel, DependDependerIndexId, true,
253  NULL, 2, key);
254 
255  while (HeapTupleIsValid(tup = systable_getnext(scan)))
256  {
257  if (skipExtensionDeps &&
258  ((Form_pg_depend) GETSTRUCT(tup))->deptype == DEPENDENCY_EXTENSION)
259  continue;
260 
261  CatalogTupleDelete(depRel, &tup->t_self);
262  count++;
263  }
264 
265  systable_endscan(scan);
266 
267  table_close(depRel, RowExclusiveLock);
268 
269  return count;
270 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ deleteDependencyRecordsForClass()

long deleteDependencyRecordsForClass ( Oid  classId,
Oid  objectId,
Oid  refclassId,
char  deptype 
)

Definition at line 282 of file pg_depend.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), DependDependerIndexId, GETSTRUCT, HeapTupleIsValid, sort-test::key, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by AlterForeignDataWrapper(), ApplyExtensionUpdates(), ATExecDetachPartition(), ATExecDropIdentity(), ConstraintSetParentConstraint(), DropClonedTriggersFromPartition(), ExecAlterExtensionContentsStmt(), index_constraint_create(), IndexSetParentIndex(), and process_owned_by().

284 {
285  long count = 0;
286  Relation depRel;
287  ScanKeyData key[2];
288  SysScanDesc scan;
289  HeapTuple tup;
290 
291  depRel = table_open(DependRelationId, RowExclusiveLock);
292 
293  ScanKeyInit(&key[0],
294  Anum_pg_depend_classid,
295  BTEqualStrategyNumber, F_OIDEQ,
296  ObjectIdGetDatum(classId));
297  ScanKeyInit(&key[1],
298  Anum_pg_depend_objid,
299  BTEqualStrategyNumber, F_OIDEQ,
300  ObjectIdGetDatum(objectId));
301 
302  scan = systable_beginscan(depRel, DependDependerIndexId, true,
303  NULL, 2, key);
304 
305  while (HeapTupleIsValid(tup = systable_getnext(scan)))
306  {
307  Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
308 
309  if (depform->refclassid == refclassId && depform->deptype == deptype)
310  {
311  CatalogTupleDelete(depRel, &tup->t_self);
312  count++;
313  }
314  }
315 
316  systable_endscan(scan);
317 
318  table_close(depRel, RowExclusiveLock);
319 
320  return count;
321 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ deleteDependencyRecordsForSpecific()

long deleteDependencyRecordsForSpecific ( Oid  classId,
Oid  objectId,
char  deptype,
Oid  refclassId,
Oid  refobjectId 
)

Definition at line 329 of file pg_depend.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), DependDependerIndexId, GETSTRUCT, HeapTupleIsValid, sort-test::key, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by ExecAlterObjectDependsStmt().

331 {
332  long count = 0;
333  Relation depRel;
334  ScanKeyData key[2];
335  SysScanDesc scan;
336  HeapTuple tup;
337 
338  depRel = table_open(DependRelationId, RowExclusiveLock);
339 
340  ScanKeyInit(&key[0],
341  Anum_pg_depend_classid,
342  BTEqualStrategyNumber, F_OIDEQ,
343  ObjectIdGetDatum(classId));
344  ScanKeyInit(&key[1],
345  Anum_pg_depend_objid,
346  BTEqualStrategyNumber, F_OIDEQ,
347  ObjectIdGetDatum(objectId));
348 
349  scan = systable_beginscan(depRel, DependDependerIndexId, true,
350  NULL, 2, key);
351 
352  while (HeapTupleIsValid(tup = systable_getnext(scan)))
353  {
354  Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
355 
356  if (depform->refclassid == refclassId &&
357  depform->refobjid == refobjectId &&
358  depform->deptype == deptype)
359  {
360  CatalogTupleDelete(depRel, &tup->t_self);
361  count++;
362  }
363  }
364 
365  systable_endscan(scan);
366 
367  table_close(depRel, RowExclusiveLock);
368 
369  return count;
370 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ deleteSharedDependencyRecordsFor()

void deleteSharedDependencyRecordsFor ( Oid  classId,
Oid  objectId,
int32  objectSubId 
)

Definition at line 945 of file pg_shdepend.c.

References InvalidOid, RowExclusiveLock, SHARED_DEPENDENCY_INVALID, shdepDropDependency(), table_close(), and table_open().

Referenced by AlterPolicy(), deleteOneObject(), DropSubscription(), DropTableSpace(), GenerateTypeDependencies(), makeConfigurationDependencies(), makeOperatorDependencies(), and RemoveRoleFromObjectPolicy().

946 {
947  Relation sdepRel;
948 
949  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
950 
951  shdepDropDependency(sdepRel, classId, objectId, objectSubId,
952  (objectSubId == 0),
955 
956  table_close(sdepRel, RowExclusiveLock);
957 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidOid
Definition: postgres_ext.h:36
static void shdepDropDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, bool drop_subobjects, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:1022
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ dropDatabaseDependencies()

void dropDatabaseDependencies ( Oid  databaseId)

Definition at line 897 of file pg_shdepend.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), HeapTupleIsValid, InvalidOid, sort-test::key, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), SHARED_DEPENDENCY_INVALID, SharedDependDependerIndexId, shdepDropDependency(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by dropdb().

898 {
899  Relation sdepRel;
900  ScanKeyData key[1];
901  SysScanDesc scan;
902  HeapTuple tup;
903 
904  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
905 
906  /*
907  * First, delete all the entries that have the database Oid in the dbid
908  * field.
909  */
910  ScanKeyInit(&key[0],
911  Anum_pg_shdepend_dbid,
912  BTEqualStrategyNumber, F_OIDEQ,
913  ObjectIdGetDatum(databaseId));
914  /* We leave the other index fields unspecified */
915 
916  scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
917  NULL, 1, key);
918 
919  while (HeapTupleIsValid(tup = systable_getnext(scan)))
920  {
921  CatalogTupleDelete(sdepRel, &tup->t_self);
922  }
923 
924  systable_endscan(scan);
925 
926  /* Now delete all entries corresponding to the database itself */
927  shdepDropDependency(sdepRel, DatabaseRelationId, databaseId, 0, true,
930 
931  table_close(sdepRel, RowExclusiveLock);
932 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define SharedDependDependerIndexId
Definition: indexing.h:234
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define InvalidOid
Definition: postgres_ext.h:36
static void shdepDropDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, bool drop_subobjects, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:1022
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ free_object_addresses()

◆ get_constraint_index()

Oid get_constraint_index ( Oid  constraintId)

Definition at line 940 of file pg_depend.c.

References AccessShareLock, BTEqualStrategyNumber, DEPENDENCY_INTERNAL, DependReferenceIndexId, get_rel_relkind(), GETSTRUCT, HeapTupleIsValid, Int32GetDatum, InvalidOid, sort-test::key, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by ATPostAlterTypeParse(), infer_arbiter_indexes(), pg_get_constraintdef_worker(), and RememberConstraintForRebuilding().

941 {
942  Oid indexId = InvalidOid;
943  Relation depRel;
944  ScanKeyData key[3];
945  SysScanDesc scan;
946  HeapTuple tup;
947 
948  /* Search the dependency table for the dependent index */
949  depRel = table_open(DependRelationId, AccessShareLock);
950 
951  ScanKeyInit(&key[0],
952  Anum_pg_depend_refclassid,
953  BTEqualStrategyNumber, F_OIDEQ,
954  ObjectIdGetDatum(ConstraintRelationId));
955  ScanKeyInit(&key[1],
956  Anum_pg_depend_refobjid,
957  BTEqualStrategyNumber, F_OIDEQ,
958  ObjectIdGetDatum(constraintId));
959  ScanKeyInit(&key[2],
960  Anum_pg_depend_refobjsubid,
961  BTEqualStrategyNumber, F_INT4EQ,
962  Int32GetDatum(0));
963 
964  scan = systable_beginscan(depRel, DependReferenceIndexId, true,
965  NULL, 3, key);
966 
967  while (HeapTupleIsValid(tup = systable_getnext(scan)))
968  {
969  Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
970 
971  /*
972  * We assume any internal dependency of an index on the constraint
973  * must be what we are looking for.
974  */
975  if (deprec->classid == RelationRelationId &&
976  deprec->objsubid == 0 &&
977  deprec->deptype == DEPENDENCY_INTERNAL)
978  {
979  char relkind = get_rel_relkind(deprec->objid);
980 
981  /*
982  * This is pure paranoia; there shouldn't be any other relkinds
983  * dependent on a constraint.
984  */
985  if (relkind != RELKIND_INDEX &&
986  relkind != RELKIND_PARTITIONED_INDEX)
987  continue;
988 
989  indexId = deprec->objid;
990  break;
991  }
992  }
993 
994  systable_endscan(scan);
995  table_close(depRel, AccessShareLock);
996 
997  return indexId;
998 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:1915
#define DependReferenceIndexId
Definition: indexing.h:162
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Int32GetDatum(X)
Definition: postgres.h:479
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ get_index_constraint()

Oid get_index_constraint ( Oid  indexId)

Definition at line 1007 of file pg_depend.c.

References AccessShareLock, BTEqualStrategyNumber, DependDependerIndexId, DEPENDENCY_INTERNAL, GETSTRUCT, HeapTupleIsValid, Int32GetDatum, InvalidOid, sort-test::key, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by generateClonedIndexStmt(), index_concurrently_swap(), RememberIndexForRebuilding(), RenameRelationInternal(), and transformIndexConstraint().

1008 {
1009  Oid constraintId = InvalidOid;
1010  Relation depRel;
1011  ScanKeyData key[3];
1012  SysScanDesc scan;
1013  HeapTuple tup;
1014 
1015  /* Search the dependency table for the index */
1016  depRel = table_open(DependRelationId, AccessShareLock);
1017 
1018  ScanKeyInit(&key[0],
1019  Anum_pg_depend_classid,
1020  BTEqualStrategyNumber, F_OIDEQ,
1021  ObjectIdGetDatum(RelationRelationId));
1022  ScanKeyInit(&key[1],
1023  Anum_pg_depend_objid,
1024  BTEqualStrategyNumber, F_OIDEQ,
1025  ObjectIdGetDatum(indexId));
1026  ScanKeyInit(&key[2],
1027  Anum_pg_depend_objsubid,
1028  BTEqualStrategyNumber, F_INT4EQ,
1029  Int32GetDatum(0));
1030 
1031  scan = systable_beginscan(depRel, DependDependerIndexId, true,
1032  NULL, 3, key);
1033 
1034  while (HeapTupleIsValid(tup = systable_getnext(scan)))
1035  {
1036  Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
1037 
1038  /*
1039  * We assume any internal dependency on a constraint must be what we
1040  * are looking for.
1041  */
1042  if (deprec->refclassid == ConstraintRelationId &&
1043  deprec->refobjsubid == 0 &&
1044  deprec->deptype == DEPENDENCY_INTERNAL)
1045  {
1046  constraintId = deprec->refobjid;
1047  break;
1048  }
1049  }
1050 
1051  systable_endscan(scan);
1052  table_close(depRel, AccessShareLock);
1053 
1054  return constraintId;
1055 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Int32GetDatum(X)
Definition: postgres.h:479
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ get_index_ref_constraints()

List* get_index_ref_constraints ( Oid  indexId)

Definition at line 1063 of file pg_depend.c.

References AccessShareLock, BTEqualStrategyNumber, DEPENDENCY_NORMAL, DependReferenceIndexId, GETSTRUCT, HeapTupleIsValid, Int32GetDatum, sort-test::key, lappend_oid(), NIL, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by index_concurrently_swap().

1064 {
1065  List *result = NIL;
1066  Relation depRel;
1067  ScanKeyData key[3];
1068  SysScanDesc scan;
1069  HeapTuple tup;
1070 
1071  /* Search the dependency table for the index */
1072  depRel = table_open(DependRelationId, AccessShareLock);
1073 
1074  ScanKeyInit(&key[0],
1075  Anum_pg_depend_refclassid,
1076  BTEqualStrategyNumber, F_OIDEQ,
1077  ObjectIdGetDatum(RelationRelationId));
1078  ScanKeyInit(&key[1],
1079  Anum_pg_depend_refobjid,
1080  BTEqualStrategyNumber, F_OIDEQ,
1081  ObjectIdGetDatum(indexId));
1082  ScanKeyInit(&key[2],
1083  Anum_pg_depend_refobjsubid,
1084  BTEqualStrategyNumber, F_INT4EQ,
1085  Int32GetDatum(0));
1086 
1087  scan = systable_beginscan(depRel, DependReferenceIndexId, true,
1088  NULL, 3, key);
1089 
1090  while (HeapTupleIsValid(tup = systable_getnext(scan)))
1091  {
1092  Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
1093 
1094  /*
1095  * We assume any normal dependency from a constraint must be what we
1096  * are looking for.
1097  */
1098  if (deprec->classid == ConstraintRelationId &&
1099  deprec->objsubid == 0 &&
1100  deprec->deptype == DEPENDENCY_NORMAL)
1101  {
1102  result = lappend_oid(result, deprec->objid);
1103  }
1104  }
1105 
1106  systable_endscan(scan);
1107  table_close(depRel, AccessShareLock);
1108 
1109  return result;
1110 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependReferenceIndexId
Definition: indexing.h:162
#define AccessShareLock
Definition: lockdefs.h:36
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Int32GetDatum(X)
Definition: postgres.h:479
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ getAutoExtensionsOfObject()

List* getAutoExtensionsOfObject ( Oid  classId,
Oid  objectId 
)

Definition at line 747 of file pg_depend.c.

References AccessShareLock, BTEqualStrategyNumber, DependDependerIndexId, DEPENDENCY_AUTO_EXTENSION, GETSTRUCT, HeapTupleIsValid, sort-test::key, lappend_oid(), NIL, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by ExecAlterObjectDependsStmt().

748 {
749  List *result = NIL;
750  Relation depRel;
751  ScanKeyData key[2];
752  SysScanDesc scan;
753  HeapTuple tup;
754 
755  depRel = table_open(DependRelationId, AccessShareLock);
756 
757  ScanKeyInit(&key[0],
758  Anum_pg_depend_classid,
759  BTEqualStrategyNumber, F_OIDEQ,
760  ObjectIdGetDatum(classId));
761  ScanKeyInit(&key[1],
762  Anum_pg_depend_objid,
763  BTEqualStrategyNumber, F_OIDEQ,
764  ObjectIdGetDatum(objectId));
765 
766  scan = systable_beginscan(depRel, DependDependerIndexId, true,
767  NULL, 2, key);
768 
769  while (HeapTupleIsValid((tup = systable_getnext(scan))))
770  {
771  Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
772 
773  if (depform->refclassid == ExtensionRelationId &&
774  depform->deptype == DEPENDENCY_AUTO_EXTENSION)
775  result = lappend_oid(result, depform->refobjid);
776  }
777 
778  systable_endscan(scan);
779 
780  table_close(depRel, AccessShareLock);
781 
782  return result;
783 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
#define AccessShareLock
Definition: lockdefs.h:36
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ getExtensionOfObject()

Oid getExtensionOfObject ( Oid  classId,
Oid  objectId 
)

Definition at line 701 of file pg_depend.c.

References AccessShareLock, BTEqualStrategyNumber, DependDependerIndexId, DEPENDENCY_EXTENSION, GETSTRUCT, HeapTupleIsValid, InvalidOid, sort-test::key, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by AlterExtensionNamespace(), ExecAlterExtensionContentsStmt(), lookup_shippable(), pg_extension_config_dump(), and recordDependencyOnCurrentExtension().

702 {
703  Oid result = InvalidOid;
704  Relation depRel;
705  ScanKeyData key[2];
706  SysScanDesc scan;
707  HeapTuple tup;
708 
709  depRel = table_open(DependRelationId, AccessShareLock);
710 
711  ScanKeyInit(&key[0],
712  Anum_pg_depend_classid,
713  BTEqualStrategyNumber, F_OIDEQ,
714  ObjectIdGetDatum(classId));
715  ScanKeyInit(&key[1],
716  Anum_pg_depend_objid,
717  BTEqualStrategyNumber, F_OIDEQ,
718  ObjectIdGetDatum(objectId));
719 
720  scan = systable_beginscan(depRel, DependDependerIndexId, true,
721  NULL, 2, key);
722 
723  while (HeapTupleIsValid((tup = systable_getnext(scan))))
724  {
725  Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
726 
727  if (depform->refclassid == ExtensionRelationId &&
728  depform->deptype == DEPENDENCY_EXTENSION)
729  {
730  result = depform->refobjid;
731  break; /* no need to keep scanning */
732  }
733  }
734 
735  systable_endscan(scan);
736 
737  table_close(depRel, AccessShareLock);
738 
739  return result;
740 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ getIdentitySequence()

Oid getIdentitySequence ( Oid  relid,
AttrNumber  attnum,
bool  missing_ok 
)

Definition at line 914 of file pg_depend.c.

References DEPENDENCY_INTERNAL, elog, ERROR, getOwnedSequences_internal(), InvalidOid, linitial_oid, and list_length().

Referenced by ATExecDropIdentity(), build_column_default(), transformAlterTableStmt(), and transformTableLikeClause().

915 {
917 
918  if (list_length(seqlist) > 1)
919  elog(ERROR, "more than one owned sequence found");
920  else if (list_length(seqlist) < 1)
921  {
922  if (missing_ok)
923  return InvalidOid;
924  else
925  elog(ERROR, "no owned sequence found");
926  }
927 
928  return linitial_oid(seqlist);
929 }
static List * getOwnedSequences_internal(Oid relid, AttrNumber attnum, char deptype)
Definition: pg_depend.c:846
#define ERROR
Definition: elog.h:43
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:79
#define linitial_oid(l)
Definition: pg_list.h:197
static int list_length(const List *l)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:214
Definition: pg_list.h:50

◆ getObjectClass()

ObjectClass getObjectClass ( const ObjectAddress object)

Definition at line 2722 of file dependency.c.

References ObjectAddress::classId, elog, ERROR, ObjectAddress::objectSubId, OCLASS_AM, OCLASS_AMOP, OCLASS_AMPROC, OCLASS_CAST, OCLASS_CLASS, OCLASS_COLLATION, OCLASS_CONSTRAINT, OCLASS_CONVERSION, OCLASS_DATABASE, OCLASS_DEFACL, OCLASS_DEFAULT, OCLASS_EVENT_TRIGGER, OCLASS_EXTENSION, OCLASS_FDW, OCLASS_FOREIGN_SERVER, OCLASS_LANGUAGE, OCLASS_LARGEOBJECT, OCLASS_OPCLASS, OCLASS_OPERATOR, OCLASS_OPFAMILY, OCLASS_POLICY, OCLASS_PROC, OCLASS_PUBLICATION, OCLASS_PUBLICATION_REL, OCLASS_REWRITE, OCLASS_ROLE, OCLASS_SCHEMA, OCLASS_STATISTIC_EXT, OCLASS_SUBSCRIPTION, OCLASS_TBLSPACE, OCLASS_TRANSFORM, OCLASS_TRIGGER, OCLASS_TSCONFIG, OCLASS_TSDICT, OCLASS_TSPARSER, OCLASS_TSTEMPLATE, OCLASS_TYPE, and OCLASS_USER_MAPPING.

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

2723 {
2724  /* only pg_class entries can have nonzero objectSubId */
2725  if (object->classId != RelationRelationId &&
2726  object->objectSubId != 0)
2727  elog(ERROR, "invalid non-zero objectSubId for object class %u",
2728  object->classId);
2729 
2730  switch (object->classId)
2731  {
2732  case RelationRelationId:
2733  /* caller must check objectSubId */
2734  return OCLASS_CLASS;
2735 
2736  case ProcedureRelationId:
2737  return OCLASS_PROC;
2738 
2739  case TypeRelationId:
2740  return OCLASS_TYPE;
2741 
2742  case CastRelationId:
2743  return OCLASS_CAST;
2744 
2745  case CollationRelationId:
2746  return OCLASS_COLLATION;
2747 
2748  case ConstraintRelationId:
2749  return OCLASS_CONSTRAINT;
2750 
2751  case ConversionRelationId:
2752  return OCLASS_CONVERSION;
2753 
2754  case AttrDefaultRelationId:
2755  return OCLASS_DEFAULT;
2756 
2757  case LanguageRelationId:
2758  return OCLASS_LANGUAGE;
2759 
2760  case LargeObjectRelationId:
2761  return OCLASS_LARGEOBJECT;
2762 
2763  case OperatorRelationId:
2764  return OCLASS_OPERATOR;
2765 
2766  case OperatorClassRelationId:
2767  return OCLASS_OPCLASS;
2768 
2769  case OperatorFamilyRelationId:
2770  return OCLASS_OPFAMILY;
2771 
2772  case AccessMethodRelationId:
2773  return OCLASS_AM;
2774 
2775  case AccessMethodOperatorRelationId:
2776  return OCLASS_AMOP;
2777 
2778  case AccessMethodProcedureRelationId:
2779  return OCLASS_AMPROC;
2780 
2781  case RewriteRelationId:
2782  return OCLASS_REWRITE;
2783 
2784  case TriggerRelationId:
2785  return OCLASS_TRIGGER;
2786 
2787  case NamespaceRelationId:
2788  return OCLASS_SCHEMA;
2789 
2790  case StatisticExtRelationId:
2791  return OCLASS_STATISTIC_EXT;
2792 
2793  case TSParserRelationId:
2794  return OCLASS_TSPARSER;
2795 
2796  case TSDictionaryRelationId:
2797  return OCLASS_TSDICT;
2798 
2799  case TSTemplateRelationId:
2800  return OCLASS_TSTEMPLATE;
2801 
2802  case TSConfigRelationId:
2803  return OCLASS_TSCONFIG;
2804 
2805  case AuthIdRelationId:
2806  return OCLASS_ROLE;
2807 
2808  case DatabaseRelationId:
2809  return OCLASS_DATABASE;
2810 
2811  case TableSpaceRelationId:
2812  return OCLASS_TBLSPACE;
2813 
2814  case ForeignDataWrapperRelationId:
2815  return OCLASS_FDW;
2816 
2817  case ForeignServerRelationId:
2818  return OCLASS_FOREIGN_SERVER;
2819 
2820  case UserMappingRelationId:
2821  return OCLASS_USER_MAPPING;
2822 
2823  case DefaultAclRelationId:
2824  return OCLASS_DEFACL;
2825 
2826  case ExtensionRelationId:
2827  return OCLASS_EXTENSION;
2828 
2829  case EventTriggerRelationId:
2830  return OCLASS_EVENT_TRIGGER;
2831 
2832  case PolicyRelationId:
2833  return OCLASS_POLICY;
2834 
2835  case PublicationRelationId:
2836  return OCLASS_PUBLICATION;
2837 
2838  case PublicationRelRelationId:
2839  return OCLASS_PUBLICATION_REL;
2840 
2841  case SubscriptionRelationId:
2842  return OCLASS_SUBSCRIPTION;
2843 
2844  case TransformRelationId:
2845  return OCLASS_TRANSFORM;
2846  }
2847 
2848  /* shouldn't get here */
2849  elog(ERROR, "unrecognized object class: %u", object->classId);
2850  return OCLASS_CLASS; /* keep compiler quiet */
2851 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:214

◆ getOwnedSequences()

List* getOwnedSequences ( Oid  relid)

Definition at line 905 of file pg_depend.c.

References getOwnedSequences_internal().

Referenced by ExecuteTruncateGuts().

906 {
907  return getOwnedSequences_internal(relid, 0, 0);
908 }
static List * getOwnedSequences_internal(Oid relid, AttrNumber attnum, char deptype)
Definition: pg_depend.c:846

◆ new_object_addresses()

ObjectAddresses* new_object_addresses ( void  )

Definition at line 2412 of file dependency.c.

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

Referenced by AggregateCreate(), AlterExtensionNamespace(), AlterTableNamespace(), AlterTypeNamespace(), ATExecDropColumn(), ATPostAlterTypeCleanup(), CastCreate(), CreateConstraintEntry(), CreateProceduralLanguage(), CreateTransform(), DropClonedTriggersFromPartition(), GenerateTypeDependencies(), heap_create_with_catalog(), index_create(), InsertExtensionTuple(), makeConfigurationDependencies(), makeDictionaryDependencies(), makeOperatorDependencies(), makeParserDependencies(), makeTSTemplateDependencies(), performDeletion(), performMultipleDeletions(), PreCommit_on_commit_actions(), ProcedureCreate(), RangeCreate(), recordDependencyOnExpr(), recordDependencyOnSingleRelExpr(), ReindexRelationConcurrently(), RemoveObjects(), RemoveRelations(), shdepDropOwned(), and StorePartitionKey().

2413 {
2414  ObjectAddresses *addrs;
2415 
2416  addrs = palloc(sizeof(ObjectAddresses));
2417 
2418  addrs->numrefs = 0;
2419  addrs->maxrefs = 32;
2420  addrs->refs = (ObjectAddress *)
2421  palloc(addrs->maxrefs * sizeof(ObjectAddress));
2422  addrs->extras = NULL; /* until/unless needed */
2423 
2424  return addrs;
2425 }
ObjectAddress * refs
Definition: dependency.c:111
ObjectAddressExtra * extras
Definition: dependency.c:112
void * palloc(Size size)
Definition: mcxt.c:950

◆ object_address_present()

bool object_address_present ( const ObjectAddress object,
const ObjectAddresses addrs 
)

Definition at line 2527 of file dependency.c.

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

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

2529 {
2530  int i;
2531 
2532  for (i = addrs->numrefs - 1; i >= 0; i--)
2533  {
2534  const ObjectAddress *thisobj = addrs->refs + i;
2535 
2536  if (object->classId == thisobj->classId &&
2537  object->objectId == thisobj->objectId)
2538  {
2539  if (object->objectSubId == thisobj->objectSubId ||
2540  thisobj->objectSubId == 0)
2541  return true;
2542  }
2543  }
2544 
2545  return false;
2546 }
ObjectAddress * refs
Definition: dependency.c:111
int i

◆ performDeletion()

void performDeletion ( const ObjectAddress object,
DropBehavior  behavior,
int  flags 
)

Definition at line 312 of file dependency.c.

References AcquireDeletionLock(), deleteObjectsInList(), DEPFLAG_ORIGINAL, findDependentObjects(), free_object_addresses(), new_object_addresses(), reportDependentObjects(), RowExclusiveLock, table_close(), and table_open().

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

314 {
315  Relation depRel;
316  ObjectAddresses *targetObjects;
317 
318  /*
319  * We save some cycles by opening pg_depend just once and passing the
320  * Relation pointer down to all the recursive deletion steps.
321  */
322  depRel = table_open(DependRelationId, RowExclusiveLock);
323 
324  /*
325  * Acquire deletion lock on the target object. (Ideally the caller has
326  * done this already, but many places are sloppy about it.)
327  */
328  AcquireDeletionLock(object, 0);
329 
330  /*
331  * Construct a list of objects to delete (ie, the given object plus
332  * everything directly or indirectly dependent on it).
333  */
334  targetObjects = new_object_addresses();
335 
336  findDependentObjects(object,
338  flags,
339  NULL, /* empty stack */
340  targetObjects,
341  NULL, /* no pendingObjects */
342  &depRel);
343 
344  /*
345  * Check if deletion is allowed, and report about cascaded deletes.
346  */
347  reportDependentObjects(targetObjects,
348  behavior,
349  flags,
350  object);
351 
352  /* do the deed */
353  deleteObjectsInList(targetObjects, &depRel, flags);
354 
355  /* And clean up */
356  free_object_addresses(targetObjects);
357 
358  table_close(depRel, RowExclusiveLock);
359 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:471
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
#define DEPFLAG_ORIGINAL
Definition: dependency.c:97
#define RowExclusiveLock
Definition: lockdefs.h:38
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:1024
void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1530
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:224
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ performMultipleDeletions()

void performMultipleDeletions ( const ObjectAddresses objects,
DropBehavior  behavior,
int  flags 
)

Definition at line 371 of file dependency.c.

References AcquireDeletionLock(), deleteObjectsInList(), DEPFLAG_ORIGINAL, findDependentObjects(), free_object_addresses(), i, new_object_addresses(), ObjectAddresses::numrefs, ObjectAddresses::refs, reportDependentObjects(), RowExclusiveLock, table_close(), and table_open().

Referenced by ATExecDropColumn(), ATPostAlterTypeCleanup(), DropClonedTriggersFromPartition(), PreCommit_on_commit_actions(), ReindexRelationConcurrently(), RemoveObjects(), RemoveRelations(), and shdepDropOwned().

373 {
374  Relation depRel;
375  ObjectAddresses *targetObjects;
376  int i;
377 
378  /* No work if no objects... */
379  if (objects->numrefs <= 0)
380  return;
381 
382  /*
383  * We save some cycles by opening pg_depend just once and passing the
384  * Relation pointer down to all the recursive deletion steps.
385  */
386  depRel = table_open(DependRelationId, RowExclusiveLock);
387 
388  /*
389  * Construct a list of objects to delete (ie, the given objects plus
390  * everything directly or indirectly dependent on them). Note that
391  * because we pass the whole objects list as pendingObjects context, we
392  * won't get a failure from trying to delete an object that is internally
393  * dependent on another one in the list; we'll just skip that object and
394  * delete it when we reach its owner.
395  */
396  targetObjects = new_object_addresses();
397 
398  for (i = 0; i < objects->numrefs; i++)
399  {
400  const ObjectAddress *thisobj = objects->refs + i;
401 
402  /*
403  * Acquire deletion lock on each target object. (Ideally the caller
404  * has done this already, but many places are sloppy about it.)
405  */
406  AcquireDeletionLock(thisobj, flags);
407 
408  findDependentObjects(thisobj,
410  flags,
411  NULL, /* empty stack */
412  targetObjects,
413  objects,
414  &depRel);
415  }
416 
417  /*
418  * Check if deletion is allowed, and report about cascaded deletes.
419  *
420  * If there's exactly one object being deleted, report it the same way as
421  * in performDeletion(), else we have to be vaguer.
422  */
423  reportDependentObjects(targetObjects,
424  behavior,
425  flags,
426  (objects->numrefs == 1 ? objects->refs : NULL));
427 
428  /* do the deed */
429  deleteObjectsInList(targetObjects, &depRel, flags);
430 
431  /* And clean up */
432  free_object_addresses(targetObjects);
433 
434  table_close(depRel, RowExclusiveLock);
435 }
static void findDependentObjects(const ObjectAddress *object, int objflags, int flags, ObjectAddressStack *stack, ObjectAddresses *targetObjects, const ObjectAddresses *pendingObjects, Relation *depRel)
Definition: dependency.c:471
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
#define DEPFLAG_ORIGINAL
Definition: dependency.c:97
ObjectAddress * refs
Definition: dependency.c:111
#define RowExclusiveLock
Definition: lockdefs.h:38
static void reportDependentObjects(const ObjectAddresses *targetObjects, DropBehavior behavior, int flags, const ObjectAddress *origObject)
Definition: dependency.c:1024
void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1530
int i
static void deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel, int flags)
Definition: dependency.c:224
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ record_object_address_dependencies()

void record_object_address_dependencies ( const ObjectAddress depender,
ObjectAddresses referenced,
DependencyType  behavior 
)

Definition at line 2676 of file dependency.c.

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

Referenced by AggregateCreate(), CastCreate(), CreateConstraintEntry(), CreateProceduralLanguage(), CreateTransform(), GenerateTypeDependencies(), heap_create_with_catalog(), index_create(), InsertExtensionTuple(), makeConfigurationDependencies(), makeDictionaryDependencies(), makeOperatorDependencies(), makeParserDependencies(), makeTSTemplateDependencies(), ProcedureCreate(), RangeCreate(), and StorePartitionKey().

2679 {
2681  recordMultipleDependencies(depender,
2682  referenced->refs, referenced->numrefs,
2683  behavior);
2684 }
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:2308
ObjectAddress * refs
Definition: dependency.c:111
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:55

◆ recordDependencyOn()

void recordDependencyOn ( const ObjectAddress depender,
const ObjectAddress referenced,
DependencyType  behavior 
)

◆ recordDependencyOnCurrentExtension()

void recordDependencyOnCurrentExtension ( const ObjectAddress object,
bool  isReplace 
)

Definition at line 180 of file pg_depend.c.

References Assert, ObjectAddress::classId, creating_extension, CurrentExtensionObject, DEPENDENCY_EXTENSION, ereport, errcode(), errmsg(), ERROR, get_extension_name(), getExtensionOfObject(), getObjectDescription(), ObjectAddress::objectId, ObjectAddress::objectSubId, OidIsValid, and recordDependencyOn().

Referenced by CastCreate(), CollationCreate(), ConversionCreate(), CreateAccessMethod(), CreateForeignDataWrapper(), CreateForeignServer(), CreateOpFamily(), CreateProceduralLanguage(), CreateTransform(), DefineOpClass(), GenerateTypeDependencies(), heap_create_with_catalog(), insert_event_trigger_tuple(), makeConfigurationDependencies(), makeDictionaryDependencies(), makeOperatorDependencies(), makeParserDependencies(), makeTSTemplateDependencies(), NamespaceCreate(), and ProcedureCreate().

182 {
183  /* Only whole objects can be extension members */
184  Assert(object->objectSubId == 0);
185 
186  if (creating_extension)
187  {
188  ObjectAddress extension;
189 
190  /* Only need to check for existing membership if isReplace */
191  if (isReplace)
192  {
193  Oid oldext;
194 
195  oldext = getExtensionOfObject(object->classId, object->objectId);
196  if (OidIsValid(oldext))
197  {
198  /* If already a member of this extension, nothing to do */
199  if (oldext == CurrentExtensionObject)
200  return;
201  /* Already a member of some other extension, so reject */
202  ereport(ERROR,
203  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
204  errmsg("%s is already a member of extension \"%s\"",
205  getObjectDescription(object, false),
206  get_extension_name(oldext))));
207  }
208  }
209 
210  /* OK, record it as a member of CurrentExtensionObject */
211  extension.classId = ExtensionRelationId;
212  extension.objectId = CurrentExtensionObject;
213  extension.objectSubId = 0;
214 
215  recordDependencyOn(object, &extension, DEPENDENCY_EXTENSION);
216  }
217 }
Oid CurrentExtensionObject
Definition: extension.c:72
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:701
char * get_extension_name(Oid ext_oid)
Definition: extension.c:185
int errcode(int sqlerrcode)
Definition: elog.c:610
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
#define ERROR
Definition: elog.h:43
bool creating_extension
Definition: extension.c:71
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ recordDependencyOnExpr()

void recordDependencyOnExpr ( const ObjectAddress depender,
Node expr,
List rtable,
DependencyType  behavior 
)

Definition at line 1584 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(), and RemoveRoleFromObjectPolicy().

1587 {
1589 
1590  context.addrs = new_object_addresses();
1591 
1592  /* Set up interpretation for Vars at varlevelsup = 0 */
1593  context.rtables = list_make1(rtable);
1594 
1595  /* Scan the expression tree for referenceable objects */
1596  find_expr_references_walker(expr, &context);
1597 
1598  /* Remove any duplicates */
1600 
1601  /* And record 'em */
1602  recordMultipleDependencies(depender,
1603  context.addrs->refs, context.addrs->numrefs,
1604  behavior);
1605 
1606  free_object_addresses(context.addrs);
1607 }
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1730
ObjectAddresses * addrs
Definition: dependency.c:137
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:2308
#define list_make1(x1)
Definition: pg_list.h:227
ObjectAddress * refs
Definition: dependency.c:111
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:55

◆ recordDependencyOnOwner()

void recordDependencyOnOwner ( Oid  classId,
Oid  objectId,
Oid  owner 
)

Definition at line 164 of file pg_shdepend.c.

References ObjectAddress::classId, ObjectAddress::objectId, ObjectAddress::objectSubId, recordSharedDependencyOn(), and SHARED_DEPENDENCY_OWNER.

Referenced by CollationCreate(), ConversionCreate(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateOpFamily(), CreateProceduralLanguage(), CreatePublication(), CreateStatistics(), CreateSubscription(), CreateTableSpace(), CreateUserMapping(), DefineOpClass(), GenerateTypeDependencies(), heap_create_with_catalog(), insert_event_trigger_tuple(), InsertExtensionTuple(), inv_create(), makeConfigurationDependencies(), makeDictionaryDependencies(), makeOperatorDependencies(), NamespaceCreate(), ProcedureCreate(), and SetDefaultACL().

165 {
166  ObjectAddress myself,
167  referenced;
168 
169  myself.classId = classId;
170  myself.objectId = objectId;
171  myself.objectSubId = 0;
172 
173  referenced.classId = AuthIdRelationId;
174  referenced.objectId = owner;
175  referenced.objectSubId = 0;
176 
177  recordSharedDependencyOn(&myself, &referenced, SHARED_DEPENDENCY_OWNER);
178 }
void recordSharedDependencyOn(ObjectAddress *depender, ObjectAddress *referenced, SharedDependencyType deptype)
Definition: pg_shdepend.c:120

◆ recordDependencyOnSingleRelExpr()

void recordDependencyOnSingleRelExpr ( const ObjectAddress depender,
Node expr,
Oid  relId,
DependencyType  behavior,
DependencyType  self_behavior,
bool  reverse_self 
)

Definition at line 1627 of file dependency.c.

References AccessShareLock, 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, recordDependencyOn(), recordMultipleDependencies(), ObjectAddresses::refs, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, find_expr_references_context::rtables, RTE_RELATION, RangeTblEntry::rtekind, T_RangeTblEntry, and RangeTblEntry::type.

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

1632 {
1634  RangeTblEntry rte;
1635 
1636  context.addrs = new_object_addresses();
1637 
1638  /* We gin up a rather bogus rangetable list to handle Vars */
1639  MemSet(&rte, 0, sizeof(rte));
1640  rte.type = T_RangeTblEntry;
1641  rte.rtekind = RTE_RELATION;
1642  rte.relid = relId;
1643  rte.relkind = RELKIND_RELATION; /* no need for exactness here */
1645 
1646  context.rtables = list_make1(list_make1(&rte));
1647 
1648  /* Scan the expression tree for referenceable objects */
1649  find_expr_references_walker(expr, &context);
1650 
1651  /* Remove any duplicates */
1653 
1654  /* Separate self-dependencies if necessary */
1655  if ((behavior != self_behavior || reverse_self) &&
1656  context.addrs->numrefs > 0)
1657  {
1658  ObjectAddresses *self_addrs;
1659  ObjectAddress *outobj;
1660  int oldref,
1661  outrefs;
1662 
1663  self_addrs = new_object_addresses();
1664 
1665  outobj = context.addrs->refs;
1666  outrefs = 0;
1667  for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
1668  {
1669  ObjectAddress *thisobj = context.addrs->refs + oldref;
1670 
1671  if (thisobj->classId == RelationRelationId &&
1672  thisobj->objectId == relId)
1673  {
1674  /* Move this ref into self_addrs */
1675  add_exact_object_address(thisobj, self_addrs);
1676  }
1677  else
1678  {
1679  /* Keep it in context.addrs */
1680  *outobj = *thisobj;
1681  outobj++;
1682  outrefs++;
1683  }
1684  }
1685  context.addrs->numrefs = outrefs;
1686 
1687  /* Record the self-dependencies with the appropriate direction */
1688  if (!reverse_self)
1689  recordMultipleDependencies(depender,
1690  self_addrs->refs, self_addrs->numrefs,
1691  self_behavior);
1692  else
1693  {
1694  /* Can't use recordMultipleDependencies, so do it the hard way */
1695  int selfref;
1696 
1697  for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
1698  {
1699  ObjectAddress *thisobj = self_addrs->refs + selfref;
1700 
1701  recordDependencyOn(thisobj, depender, self_behavior);
1702  }
1703  }
1704 
1705  free_object_addresses(self_addrs);
1706  }
1707 
1708  /* Record the external dependencies */
1709  recordMultipleDependencies(depender,
1710  context.addrs->refs, context.addrs->numrefs,
1711  behavior);
1712 
1713  free_object_addresses(context.addrs);
1714 }
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1730
ObjectAddresses * addrs
Definition: dependency.c:137
#define AccessShareLock
Definition: lockdefs.h:36
#define MemSet(start, val, len)
Definition: c.h:949
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2467
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:2308
#define list_make1(x1)
Definition: pg_list.h:227
ObjectAddress * refs
Definition: dependency.c:111
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:55
RTEKind rtekind
Definition: parsenodes.h:977
NodeTag type
Definition: parsenodes.h:975

◆ recordMultipleDependencies()

void recordMultipleDependencies ( const ObjectAddress depender,
const ObjectAddress referenced,
int  nreferenced,
DependencyType  behavior 
)

Definition at line 55 of file pg_depend.c.

References CatalogCloseIndexes(), CatalogOpenIndexes(), CatalogTuplesMultiInsertWithInfo(), CharGetDatum, ObjectAddress::classId, ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecStoreVirtualTuple(), FormData_pg_depend, i, Int32GetDatum, IsBootstrapProcessingMode, isObjectPinned(), MakeSingleTupleTableSlot(), MAX_CATALOG_MULTI_INSERT_BYTES, Min, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, palloc(), pfree(), RelationGetDescr, RowExclusiveLock, table_close(), table_open(), TupleTableSlot::tts_values, and TTSOpsHeapTuple.

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

59 {
60  Relation dependDesc;
61  CatalogIndexState indstate;
62  TupleTableSlot **slot;
63  int i,
64  max_slots,
65  slot_init_count,
66  slot_stored_count;
67 
68  if (nreferenced <= 0)
69  return; /* nothing to do */
70 
71  /*
72  * During bootstrap, do nothing since pg_depend may not exist yet. initdb
73  * will fill in appropriate pg_depend entries after bootstrap.
74  */
76  return;
77 
78  dependDesc = table_open(DependRelationId, RowExclusiveLock);
79 
80  /*
81  * Allocate the slots to use, but delay costly initialization until we
82  * know that they will be used.
83  */
84  max_slots = Min(nreferenced,
86  slot = palloc(sizeof(TupleTableSlot *) * max_slots);
87 
88  /* Don't open indexes unless we need to make an update */
89  indstate = NULL;
90 
91  /* number of slots currently storing tuples */
92  slot_stored_count = 0;
93  /* number of slots currently initialized */
94  slot_init_count = 0;
95  for (i = 0; i < nreferenced; i++, referenced++)
96  {
97  /*
98  * If the referenced object is pinned by the system, there's no real
99  * need to record dependencies on it. This saves lots of space in
100  * pg_depend, so it's worth the time taken to check.
101  */
102  if (isObjectPinned(referenced, dependDesc))
103  continue;
104 
105  if (slot_init_count < max_slots)
106  {
107  slot[slot_stored_count] = MakeSingleTupleTableSlot(RelationGetDescr(dependDesc),
108  &TTSOpsHeapTuple);
109  slot_init_count++;
110  }
111 
112  ExecClearTuple(slot[slot_stored_count]);
113 
114  /*
115  * Record the dependency. Note we don't bother to check for duplicate
116  * dependencies; there's no harm in them.
117  */
118  slot[slot_stored_count]->tts_values[Anum_pg_depend_refclassid - 1] = ObjectIdGetDatum(referenced->classId);
119  slot[slot_stored_count]->tts_values[Anum_pg_depend_refobjid - 1] = ObjectIdGetDatum(referenced->objectId);
120  slot[slot_stored_count]->tts_values[Anum_pg_depend_refobjsubid - 1] = Int32GetDatum(referenced->objectSubId);
121  slot[slot_stored_count]->tts_values[Anum_pg_depend_deptype - 1] = CharGetDatum((char) behavior);
122  slot[slot_stored_count]->tts_values[Anum_pg_depend_classid - 1] = ObjectIdGetDatum(depender->classId);
123  slot[slot_stored_count]->tts_values[Anum_pg_depend_objid - 1] = ObjectIdGetDatum(depender->objectId);
124  slot[slot_stored_count]->tts_values[Anum_pg_depend_objsubid - 1] = Int32GetDatum(depender->objectSubId);
125 
126  memset(slot[slot_stored_count]->tts_isnull, false,
127  slot[slot_stored_count]->tts_tupleDescriptor->natts * sizeof(bool));
128 
129  ExecStoreVirtualTuple(slot[slot_stored_count]);
130  slot_stored_count++;
131 
132  /* If slots are full, insert a batch of tuples */
133  if (slot_stored_count == max_slots)
134  {
135  /* fetch index info only when we know we need it */
136  if (indstate == NULL)
137  indstate = CatalogOpenIndexes(dependDesc);
138 
139  CatalogTuplesMultiInsertWithInfo(dependDesc, slot, slot_stored_count,
140  indstate);
141  slot_stored_count = 0;
142  }
143  }
144 
145  /* Insert any tuples left in the buffer */
146  if (slot_stored_count > 0)
147  {
148  /* fetch index info only when we know we need it */
149  if (indstate == NULL)
150  indstate = CatalogOpenIndexes(dependDesc);
151 
152  CatalogTuplesMultiInsertWithInfo(dependDesc, slot, slot_stored_count,
153  indstate);
154  }
155 
156  if (indstate != NULL)
157  CatalogCloseIndexes(indstate);
158 
159  table_close(dependDesc, RowExclusiveLock);
160 
161  /* Drop only the number of slots used */
162  for (i = 0; i < slot_init_count; i++)
164  pfree(slot);
165 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define RelationGetDescr(relation)
Definition: rel.h:482
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition: indexing.h:36
#define Min(x, y)
Definition: c.h:927
Datum * tts_values
Definition: tuptable.h:126
static bool isObjectPinned(const ObjectAddress *object, Relation rel)
Definition: pg_depend.c:643
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition: indexing.c:261
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
#define RowExclusiveLock
Definition: lockdefs.h:38
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
#define CharGetDatum(X)
Definition: postgres.h:416
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:393
#define Int32GetDatum(X)
Definition: postgres.h:479
void * palloc(Size size)
Definition: mcxt.c:950
int i
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
void CatalogCloseIndexes(CatalogIndexState indstate)
Definition: indexing.c:61
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
FormData_pg_depend
Definition: pg_depend.h:64
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522

◆ recordSharedDependencyOn()

void recordSharedDependencyOn ( ObjectAddress depender,
ObjectAddress referenced,
SharedDependencyType  deptype 
)

Definition at line 120 of file pg_shdepend.c.

References Assert, ObjectAddress::classId, IsBootstrapProcessingMode, isSharedObjectPinned(), ObjectAddress::objectId, ObjectAddress::objectSubId, RowExclusiveLock, shdepAddDependency(), table_close(), and table_open().

Referenced by AlterPolicy(), CreatePolicy(), recordDependencyOnOwner(), and RemoveRoleFromObjectPolicy().

123 {
124  Relation sdepRel;
125 
126  /*
127  * Objects in pg_shdepend can't have SubIds.
128  */
129  Assert(depender->objectSubId == 0);
130  Assert(referenced->objectSubId == 0);
131 
132  /*
133  * During bootstrap, do nothing since pg_shdepend may not exist yet.
134  * initdb will fill in appropriate pg_shdepend entries after bootstrap.
135  */
137  return;
138 
139  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
140 
141  /* If the referenced object is pinned, do nothing. */
142  if (!isSharedObjectPinned(referenced->classId, referenced->objectId,
143  sdepRel))
144  {
145  shdepAddDependency(sdepRel, depender->classId, depender->objectId,
146  depender->objectSubId,
147  referenced->classId, referenced->objectId,
148  deptype);
149  }
150 
151  table_close(sdepRel, RowExclusiveLock);
152 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1233
static void shdepAddDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:967
#define RowExclusiveLock
Definition: lockdefs.h:38
#define Assert(condition)
Definition: c.h:745
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:393
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ ReleaseDeletionLock()

void ReleaseDeletionLock ( const ObjectAddress object)

Definition at line 1559 of file dependency.c.

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

Referenced by findDependentObjects(), and shdepDropOwned().

1560 {
1561  if (object->classId == RelationRelationId)
1563  else
1564  /* assume we should lock the whole object not a sub-object */
1565  UnlockDatabaseObject(object->classId, object->objectId, 0,
1567 }
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
void UnlockDatabaseObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:997
#define AccessExclusiveLock
Definition: lockdefs.h:45

◆ sequenceIsOwned()

bool sequenceIsOwned ( Oid  seqId,
char  deptype,
Oid tableId,
int32 colId 
)

Definition at line 797 of file pg_depend.c.

References AccessShareLock, BTEqualStrategyNumber, DependDependerIndexId, GETSTRUCT, HeapTupleIsValid, sort-test::key, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by AlterTableNamespace(), ATExecChangeOwner(), and process_owned_by().

798 {
799  bool ret = false;
800  Relation depRel;
801  ScanKeyData key[2];
802  SysScanDesc scan;
803  HeapTuple tup;
804 
805  depRel = table_open(DependRelationId, AccessShareLock);
806 
807  ScanKeyInit(&key[0],
808  Anum_pg_depend_classid,
809  BTEqualStrategyNumber, F_OIDEQ,
810  ObjectIdGetDatum(RelationRelationId));
811  ScanKeyInit(&key[1],
812  Anum_pg_depend_objid,
813  BTEqualStrategyNumber, F_OIDEQ,
814  ObjectIdGetDatum(seqId));
815 
816  scan = systable_beginscan(depRel, DependDependerIndexId, true,
817  NULL, 2, key);
818 
819  while (HeapTupleIsValid((tup = systable_getnext(scan))))
820  {
821  Form_pg_depend depform = (Form_pg_depend) GETSTRUCT(tup);
822 
823  if (depform->refclassid == RelationRelationId &&
824  depform->deptype == deptype)
825  {
826  *tableId = depform->refobjid;
827  *colId = depform->refobjsubid;
828  ret = true;
829  break; /* no need to keep scanning */
830  }
831  }
832 
833  systable_endscan(scan);
834 
835  table_close(depRel, AccessShareLock);
836 
837  return ret;
838 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DependDependerIndexId
Definition: indexing.h:160
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ shdepDropOwned()

void shdepDropOwned ( List relids,
DropBehavior  behavior 
)

Definition at line 1285 of file pg_shdepend.c.

References AcquireDeletionLock(), add_exact_object_address(), BTEqualStrategyNumber, ObjectAddress::classId, elog, ereport, errcode(), errmsg(), ERROR, free_object_addresses(), getObjectDescription(), GETSTRUCT, InvalidOid, isSharedObjectPinned(), sort-test::key, lfirst_oid, MyDatabaseId, new_object_addresses(), ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, performMultipleDeletions(), ReleaseDeletionLock(), RemoveRoleFromObjectACL(), RemoveRoleFromObjectPolicy(), RowExclusiveLock, ScanKeyInit(), SHARED_DEPENDENCY_ACL, SHARED_DEPENDENCY_INVALID, SHARED_DEPENDENCY_OWNER, SHARED_DEPENDENCY_PIN, SHARED_DEPENDENCY_POLICY, SharedDependReferenceIndexId, sort_object_addresses(), systable_beginscan(), systable_endscan(), systable_getnext(), systable_recheck_tuple(), table_close(), and table_open().

Referenced by DropOwnedObjects().

1286 {
1287  Relation sdepRel;
1288  ListCell *cell;
1289  ObjectAddresses *deleteobjs;
1290 
1291  deleteobjs = new_object_addresses();
1292 
1293  /*
1294  * We don't need this strong a lock here, but we'll call routines that
1295  * acquire RowExclusiveLock. Better get that right now to avoid potential
1296  * deadlock failures.
1297  */
1298  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1299 
1300  /*
1301  * For each role, find the dependent objects and drop them using the
1302  * regular (non-shared) dependency management.
1303  */
1304  foreach(cell, roleids)
1305  {
1306  Oid roleid = lfirst_oid(cell);
1307  ScanKeyData key[2];
1308  SysScanDesc scan;
1309  HeapTuple tuple;
1310 
1311  /* Doesn't work for pinned objects */
1312  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
1313  {
1314  ObjectAddress obj;
1315 
1316  obj.classId = AuthIdRelationId;
1317  obj.objectId = roleid;
1318  obj.objectSubId = 0;
1319 
1320  ereport(ERROR,
1321  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1322  errmsg("cannot drop objects owned by %s because they are "
1323  "required by the database system",
1324  getObjectDescription(&obj, false))));
1325  }
1326 
1327  ScanKeyInit(&key[0],
1328  Anum_pg_shdepend_refclassid,
1329  BTEqualStrategyNumber, F_OIDEQ,
1330  ObjectIdGetDatum(AuthIdRelationId));
1331  ScanKeyInit(&key[1],
1332  Anum_pg_shdepend_refobjid,
1333  BTEqualStrategyNumber, F_OIDEQ,
1334  ObjectIdGetDatum(roleid));
1335 
1336  scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1337  NULL, 2, key);
1338 
1339  while ((tuple = systable_getnext(scan)) != NULL)
1340  {
1341  Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1342  ObjectAddress obj;
1343 
1344  /*
1345  * We only operate on shared objects and objects in the current
1346  * database
1347  */
1348  if (sdepForm->dbid != MyDatabaseId &&
1349  sdepForm->dbid != InvalidOid)
1350  continue;
1351 
1352  switch (sdepForm->deptype)
1353  {
1354  /* Shouldn't happen */
1355  case SHARED_DEPENDENCY_PIN:
1357  elog(ERROR, "unexpected dependency type");
1358  break;
1359  case SHARED_DEPENDENCY_ACL:
1360  RemoveRoleFromObjectACL(roleid,
1361  sdepForm->classid,
1362  sdepForm->objid);
1363  break;
1365 
1366  /*
1367  * Try to remove role from policy; if unable to, remove
1368  * policy.
1369  */
1370  if (!RemoveRoleFromObjectPolicy(roleid,
1371  sdepForm->classid,
1372  sdepForm->objid))
1373  {
1374  obj.classId = sdepForm->classid;
1375  obj.objectId = sdepForm->objid;
1376  obj.objectSubId = sdepForm->objsubid;
1377 
1378  /*
1379  * Acquire lock on object, then verify this dependency
1380  * is still relevant. If not, the object might have
1381  * been dropped or the policy modified. Ignore the
1382  * object in that case.
1383  */
1384  AcquireDeletionLock(&obj, 0);
1385  if (!systable_recheck_tuple(scan, tuple))
1386  {
1387  ReleaseDeletionLock(&obj);
1388  break;
1389  }
1390  add_exact_object_address(&obj, deleteobjs);
1391  }
1392  break;
1394  /* If a local object, save it for deletion below */
1395  if (sdepForm->dbid == MyDatabaseId)
1396  {
1397  obj.classId = sdepForm->classid;
1398  obj.objectId = sdepForm->objid;
1399  obj.objectSubId = sdepForm->objsubid;
1400  /* as above */
1401  AcquireDeletionLock(&obj, 0);
1402  if (!systable_recheck_tuple(scan, tuple))
1403  {
1404  ReleaseDeletionLock(&obj);
1405  break;
1406  }
1407  add_exact_object_address(&obj, deleteobjs);
1408  }
1409  break;
1410  }
1411  }
1412 
1413  systable_endscan(scan);
1414  }
1415 
1416  /*
1417  * For stability of deletion-report ordering, sort the objects into
1418  * approximate reverse creation order before deletion. (This might also
1419  * make the deletion go a bit faster, since there's less chance of having
1420  * to rearrange the objects due to dependencies.)
1421  */
1422  sort_object_addresses(deleteobjs);
1423 
1424  /* the dependency mechanism does the actual work */
1425  performMultipleDeletions(deleteobjs, behavior, 0);
1426 
1427  table_close(sdepRel, RowExclusiveLock);
1428 
1429  free_object_addresses(deleteobjs);
1430 }
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1233
void sort_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2695
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:70
void ReleaseDeletionLock(const ObjectAddress *object)
Definition: dependency.c:1559
int errcode(int sqlerrcode)
Definition: elog.c:610
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2467
void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid)
Definition: aclchk.c:1377
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
bool systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
Definition: genam.c:535
bool RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id)
Definition: policy.c:439
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1530
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
Definition: dependency.c:371
#define SharedDependReferenceIndexId
Definition: indexing.h:236
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ shdepLockAndCheckObject()

void shdepLockAndCheckObject ( Oid  classId,
Oid  objectId 
)

Definition at line 1109 of file pg_shdepend.c.

References AccessShareLock, AUTHOID, elog, ereport, errcode(), errmsg(), ERROR, get_database_name(), get_tablespace_name(), LockSharedObject(), ObjectIdGetDatum, pfree(), SearchSysCacheExists1, and tablespace.

Referenced by AlterDatabaseSet(), AlterRoleSet(), shdepAddDependency(), and shdepChangeDep().

1110 {
1111  /* AccessShareLock should be OK, since we are not modifying the object */
1112  LockSharedObject(classId, objectId, 0, AccessShareLock);
1113 
1114  switch (classId)
1115  {
1116  case AuthIdRelationId:
1118  ereport(ERROR,
1119  (errcode(ERRCODE_UNDEFINED_OBJECT),
1120  errmsg("role %u was concurrently dropped",
1121  objectId)));
1122  break;
1123 
1124  /*
1125  * Currently, this routine need not support any other shared
1126  * object types besides roles. If we wanted to record explicit
1127  * dependencies on databases or tablespaces, we'd need code along
1128  * these lines:
1129  */
1130 #ifdef NOT_USED
1131  case TableSpaceRelationId:
1132  {
1133  /* For lack of a syscache on pg_tablespace, do this: */
1134  char *tablespace = get_tablespace_name(objectId);
1135 
1136  if (tablespace == NULL)
1137  ereport(ERROR,
1138  (errcode(ERRCODE_UNDEFINED_OBJECT),
1139  errmsg("tablespace %u was concurrently dropped",
1140  objectId)));
1141  pfree(tablespace);
1142  break;
1143  }
1144 #endif
1145 
1146  case DatabaseRelationId:
1147  {
1148  /* For lack of a syscache on pg_database, do this: */
1149  char *database = get_database_name(objectId);
1150 
1151  if (database == NULL)
1152  ereport(ERROR,
1153  (errcode(ERRCODE_UNDEFINED_OBJECT),
1154  errmsg("database %u was concurrently dropped",
1155  objectId)));
1156  pfree(database);
1157  break;
1158  }
1159 
1160 
1161  default:
1162  elog(ERROR, "unrecognized shared classId: %u", classId);
1163  }
1164 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:610
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:183
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
char * tablespace
Definition: pgbench.c:189
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1017
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * get_tablespace_name(Oid spc_oid)
Definition: tablespace.c:1467
#define elog(elevel,...)
Definition: elog.h:214

◆ shdepReassignOwned()

void shdepReassignOwned ( List relids,
Oid  newrole 
)

Definition at line 1439 of file pg_shdepend.c.

References AccessExclusiveLock, AlterEventTriggerOwner_oid(), AlterForeignDataWrapperOwner_oid(), AlterForeignServerOwner_oid(), AlterObjectOwner_internal(), AlterPublicationOwner_oid(), AlterSchemaOwner_oid(), AlterSubscriptionOwner_oid(), AlterTypeOwner_oid(), ATExecChangeOwner(), BTEqualStrategyNumber, ObjectAddress::classId, CommandCounterIncrement(), elog, ereport, errcode(), errmsg(), ERROR, getObjectDescription(), GETSTRUCT, InvalidOid, isSharedObjectPinned(), sort-test::key, lfirst_oid, MyDatabaseId, NoLock, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, RowExclusiveLock, ScanKeyInit(), SHARED_DEPENDENCY_OWNER, SHARED_DEPENDENCY_PIN, SharedDependReferenceIndexId, systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by ReassignOwnedObjects().

1440 {
1441  Relation sdepRel;
1442  ListCell *cell;
1443 
1444  /*
1445  * We don't need this strong a lock here, but we'll call routines that
1446  * acquire RowExclusiveLock. Better get that right now to avoid potential
1447  * deadlock problems.
1448  */
1449  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1450 
1451  foreach(cell, roleids)
1452  {
1453  SysScanDesc scan;
1454  ScanKeyData key[2];
1455  HeapTuple tuple;
1456  Oid roleid = lfirst_oid(cell);
1457 
1458  /* Refuse to work on pinned roles */
1459  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
1460  {
1461  ObjectAddress obj;
1462 
1463  obj.classId = AuthIdRelationId;
1464  obj.objectId = roleid;
1465  obj.objectSubId = 0;
1466 
1467  ereport(ERROR,
1468  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1469  errmsg("cannot reassign ownership of objects owned by %s because they are required by the database system",
1470  getObjectDescription(&obj, false))));
1471 
1472  /*
1473  * There's no need to tell the whole truth, which is that we
1474  * didn't track these dependencies at all ...
1475  */
1476  }
1477 
1478  ScanKeyInit(&key[0],
1479  Anum_pg_shdepend_refclassid,
1480  BTEqualStrategyNumber, F_OIDEQ,
1481  ObjectIdGetDatum(AuthIdRelationId));
1482  ScanKeyInit(&key[1],
1483  Anum_pg_shdepend_refobjid,
1484  BTEqualStrategyNumber, F_OIDEQ,
1485  ObjectIdGetDatum(roleid));
1486 
1487  scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1488  NULL, 2, key);
1489 
1490  while ((tuple = systable_getnext(scan)) != NULL)
1491  {
1492  Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1493 
1494  /*
1495  * We only operate on shared objects and objects in the current
1496  * database
1497  */
1498  if (sdepForm->dbid != MyDatabaseId &&
1499  sdepForm->dbid != InvalidOid)
1500  continue;
1501 
1502  /* Unexpected because we checked for pins above */
1503  if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
1504  elog(ERROR, "unexpected shared pin");
1505 
1506  /* We leave non-owner dependencies alone */
1507  if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
1508  continue;
1509 
1510  /* Issue the appropriate ALTER OWNER call */
1511  switch (sdepForm->classid)
1512  {
1513  case TypeRelationId:
1514  AlterTypeOwner_oid(sdepForm->objid, newrole, true);
1515  break;
1516 
1517  case NamespaceRelationId:
1518  AlterSchemaOwner_oid(sdepForm->objid, newrole);
1519  break;
1520 
1521  case RelationRelationId:
1522 
1523  /*
1524  * Pass recursing = true so that we don't fail on indexes,
1525  * owned sequences, etc when we happen to visit them
1526  * before their parent table.
1527  */
1528  ATExecChangeOwner(sdepForm->objid, newrole, true, AccessExclusiveLock);
1529  break;
1530 
1531  case DefaultAclRelationId:
1532 
1533  /*
1534  * Ignore default ACLs; they should be handled by DROP
1535  * OWNED, not REASSIGN OWNED.
1536  */
1537  break;
1538 
1539  case UserMappingRelationId:
1540  /* ditto */
1541  break;
1542 
1543  case ForeignServerRelationId:
1544  AlterForeignServerOwner_oid(sdepForm->objid, newrole);
1545  break;
1546 
1547  case ForeignDataWrapperRelationId:
1548  AlterForeignDataWrapperOwner_oid(sdepForm->objid, newrole);
1549  break;
1550 
1551  case EventTriggerRelationId:
1552  AlterEventTriggerOwner_oid(sdepForm->objid, newrole);
1553  break;
1554 
1555  case PublicationRelationId:
1556  AlterPublicationOwner_oid(sdepForm->objid, newrole);
1557  break;
1558 
1559  case SubscriptionRelationId:
1560  AlterSubscriptionOwner_oid(sdepForm->objid, newrole);
1561  break;
1562 
1563  /* Generic alter owner cases */
1564  case CollationRelationId:
1565  case ConversionRelationId:
1566  case OperatorRelationId:
1567  case ProcedureRelationId:
1568  case LanguageRelationId:
1569  case LargeObjectRelationId:
1570  case OperatorFamilyRelationId:
1571  case OperatorClassRelationId:
1572  case ExtensionRelationId:
1573  case StatisticExtRelationId:
1574  case TableSpaceRelationId:
1575  case DatabaseRelationId:
1576  case TSConfigRelationId:
1577  case TSDictionaryRelationId:
1578  {
1579  Oid classId = sdepForm->classid;
1580  Relation catalog;
1581 
1582  if (classId == LargeObjectRelationId)
1583  classId = LargeObjectMetadataRelationId;
1584 
1585  catalog = table_open(classId, RowExclusiveLock);
1586 
1587  AlterObjectOwner_internal(catalog, sdepForm->objid,
1588  newrole);
1589 
1590  table_close(catalog, NoLock);
1591  }
1592  break;
1593 
1594  default:
1595  elog(ERROR, "unexpected classid %u", sdepForm->classid);
1596  break;
1597  }
1598  /* Make sure the next iteration will see my changes */
1600  }
1601 
1602  systable_endscan(scan);
1603  }
1604 
1605  table_close(sdepRel, RowExclusiveLock);
1606 }
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void AlterForeignDataWrapperOwner_oid(Oid fwdId, Oid newOwnerId)
Definition: foreigncmds.c:313
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1233
void AlterSchemaOwner_oid(Oid oid, Oid newOwnerId)
Definition: schemacmds.c:276
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:70
void AlterForeignServerOwner_oid(Oid srvId, Oid newOwnerId)
Definition: foreigncmds.c:450
int errcode(int sqlerrcode)
Definition: elog.c:610
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
void AlterTypeOwner_oid(Oid typeOid, Oid newOwnerId, bool hasDependEntry)
Definition: typecmds.c:3363
void AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
Definition: alter.c:934
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
void ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lockmode)
Definition: tablecmds.c:12472
void CommandCounterIncrement(void)
Definition: xact.c:1021
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
void AlterEventTriggerOwner_oid(Oid trigOid, Oid newOwnerId)
#define ereport(elevel,...)
Definition: elog.h:144
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
void AlterSubscriptionOwner_oid(Oid subid, Oid newOwnerId)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
void AlterPublicationOwner_oid(Oid subid, Oid newOwnerId)
#define SharedDependReferenceIndexId
Definition: indexing.h:236
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ sort_object_addresses()

void sort_object_addresses ( ObjectAddresses addrs)

Definition at line 2695 of file dependency.c.

References ObjectAddresses::numrefs, object_address_comparator(), qsort, and ObjectAddresses::refs.

Referenced by shdepDropOwned().

2696 {
2697  if (addrs->numrefs > 1)
2698  qsort((void *) addrs->refs, addrs->numrefs,
2699  sizeof(ObjectAddress),
2701 }
static int object_address_comparator(const void *a, const void *b)
Definition: dependency.c:2368
ObjectAddress * refs
Definition: dependency.c:111
#define qsort(a, b, c, d)
Definition: port.h:475

◆ updateAclDependencies()

void updateAclDependencies ( Oid  classId,
Oid  objectId,
int32  objectSubId,
Oid  ownerId,
int  noldmembers,
Oid oldmembers,
int  nnewmembers,
Oid newmembers 
)

Definition at line 426 of file pg_shdepend.c.

References getOidListDiff(), i, isSharedObjectPinned(), pfree(), RowExclusiveLock, SHARED_DEPENDENCY_ACL, shdepAddDependency(), shdepDropDependency(), table_close(), and table_open().

Referenced by ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), recordDependencyOnNewAcl(), and SetDefaultACL().

430 {
431  Relation sdepRel;
432  int i;
433 
434  /*
435  * Remove entries that are common to both lists; those represent existing
436  * dependencies we don't need to change.
437  *
438  * OK to overwrite the inputs since we'll pfree them anyway.
439  */
440  getOidListDiff(oldmembers, &noldmembers, newmembers, &nnewmembers);
441 
442  if (noldmembers > 0 || nnewmembers > 0)
443  {
444  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
445 
446  /* Add new dependencies that weren't already present */
447  for (i = 0; i < nnewmembers; i++)
448  {
449  Oid roleid = newmembers[i];
450 
451  /*
452  * Skip the owner: he has an OWNER shdep entry instead. (This is
453  * not just a space optimization; it makes ALTER OWNER easier. See
454  * notes in changeDependencyOnOwner.)
455  */
456  if (roleid == ownerId)
457  continue;
458 
459  /* Skip pinned roles; they don't need dependency entries */
460  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
461  continue;
462 
463  shdepAddDependency(sdepRel, classId, objectId, objsubId,
464  AuthIdRelationId, roleid,
466  }
467 
468  /* Drop no-longer-used old dependencies */
469  for (i = 0; i < noldmembers; i++)
470  {
471  Oid roleid = oldmembers[i];
472 
473  /* Skip the owner, same as above */
474  if (roleid == ownerId)
475  continue;
476 
477  /* Skip pinned roles */
478  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
479  continue;
480 
481  shdepDropDependency(sdepRel, classId, objectId, objsubId,
482  false, /* exact match on objsubId */
483  AuthIdRelationId, roleid,
485  }
486 
487  table_close(sdepRel, RowExclusiveLock);
488  }
489 
490  if (oldmembers)
491  pfree(oldmembers);
492  if (newmembers)
493  pfree(newmembers);
494 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1233
static void getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2)
Definition: pg_shdepend.c:356
static void shdepAddDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:967
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:1057
#define RowExclusiveLock
Definition: lockdefs.h:38
static void shdepDropDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, bool drop_subobjects, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:1022
int i
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39