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_TABLESPACE = 't', 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_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 recordDependencyOnTablespace (Oid classId, Oid objectId, Oid tablespace)
 
void changeDependencyOnTablespace (Oid classId, Oid objectId, Oid newTablespaceId)
 
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 138 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 146 of file dependency.h.

Referenced by doDeletion(), and ReindexRelationConcurrently().

◆ PERFORM_DELETION_CONCURRENTLY

#define PERFORM_DELETION_CONCURRENTLY   0x0002 /* concurrent drop */

Definition at line 142 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 145 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 144 of file dependency.h.

Referenced by deleteObjectsInList(), and RemoveTempRelations().

Typedef Documentation

◆ DependencyType

◆ ObjectAddresses

Definition at line 90 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 96 of file dependency.h.

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

◆ SharedDependencyType

Enumerator
SHARED_DEPENDENCY_PIN 
SHARED_DEPENDENCY_OWNER 
SHARED_DEPENDENCY_ACL 
SHARED_DEPENDENCY_POLICY 
SHARED_DEPENDENCY_TABLESPACE 
SHARED_DEPENDENCY_INVALID 

Definition at line 79 of file dependency.h.

Function Documentation

◆ AcquireDeletionLock()

void AcquireDeletionLock ( const ObjectAddress object,
int  flags 
)

Definition at line 1524 of file dependency.c.

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

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

1525 {
1526  if (object->classId == RelationRelationId)
1527  {
1528  /*
1529  * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
1530  * the index for the moment. index_drop() will promote the lock once
1531  * it's safe to do so. In all other cases we need full exclusive
1532  * lock.
1533  */
1534  if (flags & PERFORM_DELETION_CONCURRENTLY)
1536  else
1538  }
1539  else
1540  {
1541  /* assume we should lock the whole object not a sub-object */
1542  LockDatabaseObject(object->classId, object->objectId, 0,
1544  }
1545 }
#define PERFORM_DELETION_CONCURRENTLY
Definition: dependency.h:142
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:109

◆ add_exact_object_address()

void add_exact_object_address ( const ObjectAddress object,
ObjectAddresses addrs 
)

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

2494 {
2495  ObjectAddress *item;
2496 
2497  /* enlarge array if needed */
2498  if (addrs->numrefs >= addrs->maxrefs)
2499  {
2500  addrs->maxrefs *= 2;
2501  addrs->refs = (ObjectAddress *)
2502  repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
2503  Assert(!addrs->extras);
2504  }
2505  /* record this item */
2506  item = addrs->refs + addrs->numrefs;
2507  *item = *object;
2508  addrs->numrefs++;
2509 }
ObjectAddress * refs
Definition: dependency.c:111
#define Assert(condition)
Definition: c.h:804
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
int errcode(int sqlerrcode)
Definition: elog.c:698
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
static bool isObjectPinned(const ObjectAddress *object, Relation rel)
Definition: pg_depend.c:643
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependReferenceIndexId
Definition: pg_depend.h:78
#define ereport(elevel,...)
Definition: elog.h:157
#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:909
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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
static bool isObjectPinned(const ObjectAddress *object, Relation rel)
Definition: pg_depend.c:643
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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 313 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().

314 {
315  Relation sdepRel;
316 
317  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
318 
319  /* Adjust the SHARED_DEPENDENCY_OWNER entry */
320  shdepChangeDep(sdepRel,
321  classId, objectId, 0,
322  AuthIdRelationId, newOwnerId,
324 
325  /*----------
326  * There should never be a SHARED_DEPENDENCY_ACL entry for the owner,
327  * so get rid of it if there is one. This can happen if the new owner
328  * was previously granted some rights to the object.
329  *
330  * This step is analogous to aclnewowner's removal of duplicate entries
331  * in the ACL. We have to do it to handle this scenario:
332  * A grants some rights on an object to B
333  * ALTER OWNER changes the object's owner to B
334  * ALTER OWNER changes the object's owner to C
335  * The third step would remove all mention of B from the object's ACL,
336  * but we'd still have a SHARED_DEPENDENCY_ACL for B if we did not do
337  * things this way.
338  *
339  * The rule against having a SHARED_DEPENDENCY_ACL entry for the owner
340  * allows us to fix things up in just this one place, without having
341  * to make the various ALTER OWNER routines each know about it.
342  *----------
343  */
344  shdepDropDependency(sdepRel, classId, objectId, 0, true,
345  AuthIdRelationId, newOwnerId,
347 
348  table_close(sdepRel, RowExclusiveLock);
349 }
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:203
#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:1078
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ changeDependencyOnTablespace()

void changeDependencyOnTablespace ( Oid  classId,
Oid  objectId,
Oid  newTablespaceId 
)

Definition at line 382 of file pg_shdepend.c.

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

Referenced by SetRelationTableSpace().

383 {
384  Relation sdepRel;
385 
386  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
387 
388  if (newTablespaceId != DEFAULTTABLESPACE_OID &&
389  newTablespaceId != InvalidOid)
390  shdepChangeDep(sdepRel,
391  classId, objectId, 0,
392  TableSpaceRelationId, newTablespaceId,
394  else
395  shdepDropDependency(sdepRel,
396  classId, objectId, 0, true,
399 
400  table_close(sdepRel, RowExclusiveLock);
401 }
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:203
#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:1078
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 633 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(), and DropTableSpace().

635 {
636  Relation sdepRel;
637  ScanKeyData key[2];
638  SysScanDesc scan;
639  HeapTuple tup;
640  int numReportedDeps = 0;
641  int numNotReportedDeps = 0;
642  int numNotReportedDbs = 0;
643  List *remDeps = NIL;
644  ListCell *cell;
645  ObjectAddress object;
646  ShDependObjectInfo *objects;
647  int numobjects;
648  int allocedobjects;
649  StringInfoData descs;
650  StringInfoData alldescs;
651 
652  /*
653  * We limit the number of dependencies reported to the client to
654  * MAX_REPORTED_DEPS, since client software may not deal well with
655  * enormous error strings. The server log always gets a full report.
656  *
657  * For stability of regression test results, we sort local and shared
658  * objects by OID before reporting them. We don't worry about the order
659  * in which other databases are reported, though.
660  */
661 #define MAX_REPORTED_DEPS 100
662 
663  allocedobjects = 128; /* arbitrary initial array size */
664  objects = (ShDependObjectInfo *)
665  palloc(allocedobjects * sizeof(ShDependObjectInfo));
666  numobjects = 0;
667  initStringInfo(&descs);
668  initStringInfo(&alldescs);
669 
670  sdepRel = table_open(SharedDependRelationId, AccessShareLock);
671 
672  ScanKeyInit(&key[0],
673  Anum_pg_shdepend_refclassid,
674  BTEqualStrategyNumber, F_OIDEQ,
675  ObjectIdGetDatum(classId));
676  ScanKeyInit(&key[1],
677  Anum_pg_shdepend_refobjid,
678  BTEqualStrategyNumber, F_OIDEQ,
679  ObjectIdGetDatum(objectId));
680 
681  scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
682  NULL, 2, key);
683 
684  while (HeapTupleIsValid(tup = systable_getnext(scan)))
685  {
686  Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
687 
688  /* This case can be dispatched quickly */
689  if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
690  {
691  object.classId = classId;
692  object.objectId = objectId;
693  object.objectSubId = 0;
694  ereport(ERROR,
695  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
696  errmsg("cannot drop %s because it is required by the database system",
697  getObjectDescription(&object, false))));
698  }
699 
700  object.classId = sdepForm->classid;
701  object.objectId = sdepForm->objid;
702  object.objectSubId = sdepForm->objsubid;
703 
704  /*
705  * If it's a dependency local to this database or it's a shared
706  * object, add it to the objects array.
707  *
708  * If it's a remote dependency, keep track of it so we can report the
709  * number of them later.
710  */
711  if (sdepForm->dbid == MyDatabaseId ||
712  sdepForm->dbid == InvalidOid)
713  {
714  if (numobjects >= allocedobjects)
715  {
716  allocedobjects *= 2;
717  objects = (ShDependObjectInfo *)
718  repalloc(objects,
719  allocedobjects * sizeof(ShDependObjectInfo));
720  }
721  objects[numobjects].object = object;
722  objects[numobjects].deptype = sdepForm->deptype;
723  objects[numobjects].objtype = (sdepForm->dbid == MyDatabaseId) ?
725  numobjects++;
726  }
727  else
728  {
729  /* It's not local nor shared, so it must be remote. */
730  remoteDep *dep;
731  bool stored = false;
732 
733  /*
734  * XXX this info is kept on a simple List. Maybe it's not good
735  * for performance, but using a hash table seems needlessly
736  * complex. The expected number of databases is not high anyway,
737  * I suppose.
738  */
739  foreach(cell, remDeps)
740  {
741  dep = lfirst(cell);
742  if (dep->dbOid == sdepForm->dbid)
743  {
744  dep->count++;
745  stored = true;
746  break;
747  }
748  }
749  if (!stored)
750  {
751  dep = (remoteDep *) palloc(sizeof(remoteDep));
752  dep->dbOid = sdepForm->dbid;
753  dep->count = 1;
754  remDeps = lappend(remDeps, dep);
755  }
756  }
757  }
758 
759  systable_endscan(scan);
760 
761  table_close(sdepRel, AccessShareLock);
762 
763  /*
764  * Sort and report local and shared objects.
765  */
766  if (numobjects > 1)
767  qsort((void *) objects, numobjects,
769 
770  for (int i = 0; i < numobjects; i++)
771  {
772  if (numReportedDeps < MAX_REPORTED_DEPS)
773  {
774  numReportedDeps++;
775  storeObjectDescription(&descs,
776  objects[i].objtype,
777  &objects[i].object,
778  objects[i].deptype,
779  0);
780  }
781  else
782  numNotReportedDeps++;
783  storeObjectDescription(&alldescs,
784  objects[i].objtype,
785  &objects[i].object,
786  objects[i].deptype,
787  0);
788  }
789 
790  /*
791  * Summarize dependencies in remote databases.
792  */
793  foreach(cell, remDeps)
794  {
795  remoteDep *dep = lfirst(cell);
796 
797  object.classId = DatabaseRelationId;
798  object.objectId = dep->dbOid;
799  object.objectSubId = 0;
800 
801  if (numReportedDeps < MAX_REPORTED_DEPS)
802  {
803  numReportedDeps++;
804  storeObjectDescription(&descs, REMOTE_OBJECT, &object,
806  }
807  else
808  numNotReportedDbs++;
809  storeObjectDescription(&alldescs, REMOTE_OBJECT, &object,
811  }
812 
813  pfree(objects);
814  list_free_deep(remDeps);
815 
816  if (descs.len == 0)
817  {
818  pfree(descs.data);
819  pfree(alldescs.data);
820  *detail_msg = *detail_log_msg = NULL;
821  return false;
822  }
823 
824  if (numNotReportedDeps > 0)
825  appendStringInfo(&descs, ngettext("\nand %d other object "
826  "(see server log for list)",
827  "\nand %d other objects "
828  "(see server log for list)",
829  numNotReportedDeps),
830  numNotReportedDeps);
831  if (numNotReportedDbs > 0)
832  appendStringInfo(&descs, ngettext("\nand objects in %d other database "
833  "(see server log for list)",
834  "\nand objects in %d other databases "
835  "(see server log for list)",
836  numNotReportedDbs),
837  numNotReportedDbs);
838 
839  *detail_msg = descs.data;
840  *detail_log_msg = alldescs.data;
841  return true;
842 }
#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:565
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:73
#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:1230
int errcode(int sqlerrcode)
Definition: elog.c:698
void list_free_deep(List *list)
Definition: list.c:1405
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
SharedDependencyObjectType objtype
Definition: pg_shdepend.c:81
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define SharedDependReferenceIndexId
Definition: pg_shdepend.h:78
List * lappend(List *list, void *datum)
Definition: list.c:336
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ngettext(s, p, n)
Definition: c.h:1182
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:169
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define qsort(a, b, c, d)
Definition: port.h:504
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:79

◆ copyTemplateDependencies()

void copyTemplateDependencies ( Oid  templateDbId,
Oid  newDbId 
)

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

853 {
854  Relation sdepRel;
855  TupleDesc sdepDesc;
856  ScanKeyData key[1];
857  SysScanDesc scan;
858  HeapTuple tup;
859  CatalogIndexState indstate;
860  TupleTableSlot **slot;
861  int max_slots,
862  slot_init_count,
863  slot_stored_count;
864 
865  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
866  sdepDesc = RelationGetDescr(sdepRel);
867 
868  /*
869  * Allocate the slots to use, but delay costly initialization until we
870  * know that they will be used.
871  */
873  slot = palloc(sizeof(TupleTableSlot *) * max_slots);
874 
875  indstate = CatalogOpenIndexes(sdepRel);
876 
877  /* Scan all entries with dbid = templateDbId */
878  ScanKeyInit(&key[0],
879  Anum_pg_shdepend_dbid,
880  BTEqualStrategyNumber, F_OIDEQ,
881  ObjectIdGetDatum(templateDbId));
882 
883  scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
884  NULL, 1, key);
885 
886  /* number of slots currently storing tuples */
887  slot_stored_count = 0;
888  /* number of slots currently initialized */
889  slot_init_count = 0;
890 
891  /*
892  * Copy the entries of the original database, changing the database Id to
893  * that of the new database. Note that because we are not copying rows
894  * with dbId == 0 (ie, rows describing dependent shared objects) we won't
895  * copy the ownership dependency of the template database itself; this is
896  * what we want.
897  */
898  while (HeapTupleIsValid(tup = systable_getnext(scan)))
899  {
900  Form_pg_shdepend shdep;
901 
902  if (slot_init_count < max_slots)
903  {
904  slot[slot_stored_count] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple);
905  slot_init_count++;
906  }
907 
908  ExecClearTuple(slot[slot_stored_count]);
909 
910  shdep = (Form_pg_shdepend) GETSTRUCT(tup);
911 
912  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_dbid] = ObjectIdGetDatum(newDbId);
913  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_classid] = shdep->classid;
914  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objid] = shdep->objid;
915  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objsubid] = shdep->objsubid;
916  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refclassid] = shdep->refclassid;
917  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refobjid] = shdep->refobjid;
918  slot[slot_stored_count]->tts_values[Anum_pg_shdepend_deptype] = shdep->deptype;
919 
920  ExecStoreVirtualTuple(slot[slot_stored_count]);
921  slot_stored_count++;
922 
923  /* If slots are full, insert a batch of tuples */
924  if (slot_stored_count == max_slots)
925  {
926  CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
927  slot_stored_count = 0;
928  }
929  }
930 
931  /* Insert any tuples left in the buffer */
932  if (slot_stored_count > 0)
933  CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
934 
935  systable_endscan(scan);
936 
937  CatalogCloseIndexes(indstate);
938  table_close(sdepRel, RowExclusiveLock);
939 
940  /* Drop only the number of slots used */
941  for (int i = 0; i < slot_init_count; i++)
943  pfree(slot);
944 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define RelationGetDescr(relation)
Definition: rel.h:495
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition: indexing.h:33
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:73
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition: indexing.c:261
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
#define RowExclusiveLock
Definition: lockdefs.h:38
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_shdepend
Definition: pg_shdepend.h:66
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
void * palloc(Size size)
Definition: mcxt.c:1062
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 SharedDependDependerIndexId
Definition: pg_shdepend.h:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552

◆ 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(), CreateTrigger(), 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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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(), ATExecDropIdentity(), ConstraintSetParentConstraint(), DetachPartitionFinalize(), 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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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 1001 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().

1002 {
1003  Relation sdepRel;
1004 
1005  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1006 
1007  shdepDropDependency(sdepRel, classId, objectId, objectSubId,
1008  (objectSubId == 0),
1011 
1012  table_close(sdepRel, RowExclusiveLock);
1013 }
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:1078
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ dropDatabaseDependencies()

void dropDatabaseDependencies ( Oid  databaseId)

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

954 {
955  Relation sdepRel;
956  ScanKeyData key[1];
957  SysScanDesc scan;
958  HeapTuple tup;
959 
960  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
961 
962  /*
963  * First, delete all the entries that have the database Oid in the dbid
964  * field.
965  */
966  ScanKeyInit(&key[0],
967  Anum_pg_shdepend_dbid,
968  BTEqualStrategyNumber, F_OIDEQ,
969  ObjectIdGetDatum(databaseId));
970  /* We leave the other index fields unspecified */
971 
972  scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
973  NULL, 1, key);
974 
975  while (HeapTupleIsValid(tup = systable_getnext(scan)))
976  {
977  CatalogTupleDelete(sdepRel, &tup->t_self);
978  }
979 
980  systable_endscan(scan);
981 
982  /* Now delete all entries corresponding to the database itself */
983  shdepDropDependency(sdepRel, DatabaseRelationId, databaseId, 0, true,
986 
987  table_close(sdepRel, RowExclusiveLock);
988 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
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:1078
#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 SharedDependDependerIndexId
Definition: pg_shdepend.h:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ free_object_addresses()

◆ get_index_constraint()

Oid get_index_constraint ( Oid  indexId)

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

939 {
940  Oid constraintId = InvalidOid;
941  Relation depRel;
942  ScanKeyData key[3];
943  SysScanDesc scan;
944  HeapTuple tup;
945 
946  /* Search the dependency table for the index */
947  depRel = table_open(DependRelationId, AccessShareLock);
948 
949  ScanKeyInit(&key[0],
950  Anum_pg_depend_classid,
951  BTEqualStrategyNumber, F_OIDEQ,
952  ObjectIdGetDatum(RelationRelationId));
953  ScanKeyInit(&key[1],
954  Anum_pg_depend_objid,
955  BTEqualStrategyNumber, F_OIDEQ,
956  ObjectIdGetDatum(indexId));
957  ScanKeyInit(&key[2],
958  Anum_pg_depend_objsubid,
959  BTEqualStrategyNumber, F_INT4EQ,
960  Int32GetDatum(0));
961 
962  scan = systable_beginscan(depRel, DependDependerIndexId, true,
963  NULL, 3, key);
964 
965  while (HeapTupleIsValid(tup = systable_getnext(scan)))
966  {
967  Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
968 
969  /*
970  * We assume any internal dependency on a constraint must be what we
971  * are looking for.
972  */
973  if (deprec->refclassid == ConstraintRelationId &&
974  deprec->refobjsubid == 0 &&
975  deprec->deptype == DEPENDENCY_INTERNAL)
976  {
977  constraintId = deprec->refobjid;
978  break;
979  }
980  }
981 
982  systable_endscan(scan);
983  table_close(depRel, AccessShareLock);
984 
985  return constraintId;
986 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Int32GetDatum(X)
Definition: postgres.h:523
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 994 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().

995 {
996  List *result = NIL;
997  Relation depRel;
998  ScanKeyData key[3];
999  SysScanDesc scan;
1000  HeapTuple tup;
1001 
1002  /* Search the dependency table for the index */
1003  depRel = table_open(DependRelationId, AccessShareLock);
1004 
1005  ScanKeyInit(&key[0],
1006  Anum_pg_depend_refclassid,
1007  BTEqualStrategyNumber, F_OIDEQ,
1008  ObjectIdGetDatum(RelationRelationId));
1009  ScanKeyInit(&key[1],
1010  Anum_pg_depend_refobjid,
1011  BTEqualStrategyNumber, F_OIDEQ,
1012  ObjectIdGetDatum(indexId));
1013  ScanKeyInit(&key[2],
1014  Anum_pg_depend_refobjsubid,
1015  BTEqualStrategyNumber, F_INT4EQ,
1016  Int32GetDatum(0));
1017 
1018  scan = systable_beginscan(depRel, DependReferenceIndexId, true,
1019  NULL, 3, key);
1020 
1021  while (HeapTupleIsValid(tup = systable_getnext(scan)))
1022  {
1023  Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
1024 
1025  /*
1026  * We assume any normal dependency from a constraint must be what we
1027  * are looking for.
1028  */
1029  if (deprec->classid == ConstraintRelationId &&
1030  deprec->objsubid == 0 &&
1031  deprec->deptype == DEPENDENCY_NORMAL)
1032  {
1033  result = lappend_oid(result, deprec->objid);
1034  }
1035  }
1036 
1037  systable_endscan(scan);
1038  table_close(depRel, AccessShareLock);
1039 
1040  return result;
1041 }
#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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define AccessShareLock
Definition: lockdefs.h:36
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependReferenceIndexId
Definition: pg_depend.h:78
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Int32GetDatum(X)
Definition: postgres.h:523
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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define AccessShareLock
Definition: lockdefs.h:36
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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:46
#define InvalidOid
Definition: postgres_ext.h:36
int16 attnum
Definition: pg_attribute.h:83
#define linitial_oid(l)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:232
Definition: pg_list.h:50

◆ getObjectClass()

ObjectClass getObjectClass ( const ObjectAddress object)

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

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

◆ 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 2437 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().

2438 {
2439  ObjectAddresses *addrs;
2440 
2441  addrs = palloc(sizeof(ObjectAddresses));
2442 
2443  addrs->numrefs = 0;
2444  addrs->maxrefs = 32;
2445  addrs->refs = (ObjectAddress *)
2446  palloc(addrs->maxrefs * sizeof(ObjectAddress));
2447  addrs->extras = NULL; /* until/unless needed */
2448 
2449  return addrs;
2450 }
ObjectAddress * refs
Definition: dependency.c:111
ObjectAddressExtra * extras
Definition: dependency.c:112
void * palloc(Size size)
Definition: mcxt.c:1062

◆ object_address_present()

bool object_address_present ( const ObjectAddress object,
const ObjectAddresses addrs 
)

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

2554 {
2555  int i;
2556 
2557  for (i = addrs->numrefs - 1; i >= 0; i--)
2558  {
2559  const ObjectAddress *thisobj = addrs->refs + i;
2560 
2561  if (object->classId == thisobj->classId &&
2562  object->objectId == thisobj->objectId)
2563  {
2564  if (object->objectSubId == thisobj->objectSubId ||
2565  thisobj->objectSubId == 0)
2566  return true;
2567  }
2568  }
2569 
2570  return false;
2571 }
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(), ATExecDropConstraint(), ATExecDropIdentity(), DefineQueryRewrite(), DetachPartitionFinalize(), 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:2437
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2732
#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:1524
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:2437
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2732
#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:1524
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 2701 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().

2704 {
2706  recordMultipleDependencies(depender,
2707  referenced->refs, referenced->numrefs,
2708  behavior);
2709 }
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:2333
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 
)

Definition at line 43 of file pg_depend.c.

References recordMultipleDependencies().

Referenced by add_column_collation_dependency(), add_column_datatype_dependency(), addFkRecurseReferenced(), addFkRecurseReferencing(), AddNewAttributeTuples(), AlterForeignDataWrapper(), AlterFunction(), AlterPolicy(), ApplyExtensionUpdates(), ATExecAddOf(), changeDependencyFor(), CloneFkReferencing(), CollationCreate(), ConstraintSetParentConstraint(), ConversionCreate(), create_toast_table(), CreateAccessMethod(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreatePolicy(), CreateStatistics(), CreateTrigger(), CreateUserMapping(), DefineOpClass(), ExecAlterExtensionContentsStmt(), ExecAlterObjectDependsStmt(), GenerateTypeDependencies(), index_constraint_create(), index_create(), IndexSetParentIndex(), insert_event_trigger_tuple(), InsertRule(), makeMultirangeConstructors(), makeRangeConstructors(), process_owned_by(), publication_add_relation(), RangeCreate(), recordDependencyOnCurrentExtension(), recordDependencyOnSingleRelExpr(), RemoveRoleFromObjectPolicy(), SetDefaultACL(), StoreAttrDefault(), StoreCatalogInheritance1(), storeOperators(), StorePartitionKey(), storeProcedures(), and swap_relation_files().

46 {
47  recordMultipleDependencies(depender, referenced, 1, behavior);
48 }
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:55

◆ 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:698
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:710
#define ERROR
Definition: elog.h:46
bool creating_extension
Definition: extension.c:71
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ recordDependencyOnExpr()

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

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

1581 {
1583 
1584  context.addrs = new_object_addresses();
1585 
1586  /* Set up interpretation for Vars at varlevelsup = 0 */
1587  context.rtables = list_make1(rtable);
1588 
1589  /* Scan the expression tree for referenceable objects */
1590  find_expr_references_walker(expr, &context);
1591 
1592  /* Remove any duplicates */
1594 
1595  /* And record 'em */
1596  recordMultipleDependencies(depender,
1597  context.addrs->refs, context.addrs->numrefs,
1598  behavior);
1599 
1600  free_object_addresses(context.addrs);
1601 }
static bool find_expr_references_walker(Node *node, find_expr_references_context *context)
Definition: dependency.c:1724
ObjectAddresses * addrs
Definition: dependency.c:137
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2437
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2732
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs)
Definition: dependency.c:2333
#define list_make1(x1)
Definition: pg_list.h:206
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 165 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().

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

◆ recordDependencyOnSingleRelExpr()

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

Definition at line 1621 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(), CreateStatistics(), index_create(), StoreAttrDefault(), and StorePartitionKey().

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

◆ recordDependencyOnTablespace()

void recordDependencyOnTablespace ( Oid  classId,
Oid  objectId,
Oid  tablespace 
)

Definition at line 361 of file pg_shdepend.c.

References ObjectAddressSet, recordSharedDependencyOn(), and SHARED_DEPENDENCY_TABLESPACE.

Referenced by heap_create().

362 {
363  ObjectAddress myself,
364  referenced;
365 
366  ObjectAddressSet(myself, classId, objectId);
367  ObjectAddressSet(referenced, TableSpaceRelationId, tablespace);
368 
369  recordSharedDependencyOn(&myself, &referenced,
371 }
void recordSharedDependencyOn(ObjectAddress *depender, ObjectAddress *referenced, SharedDependencyType deptype)
Definition: pg_shdepend.c:121
char * tablespace
Definition: pgbench.c:224
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40

◆ 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:495
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
#define MAX_CATALOG_MULTI_INSERT_BYTES
Definition: indexing.h:33
#define Min(x, y)
Definition: c.h:986
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:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void CatalogTuplesMultiInsertWithInfo(Relation heapRel, TupleTableSlot **slot, int ntuples, CatalogIndexState indstate)
Definition: indexing.c:261
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
#define RowExclusiveLock
Definition: lockdefs.h:38
CatalogIndexState CatalogOpenIndexes(Relation heapRel)
Definition: indexing.c:43
#define CharGetDatum(X)
Definition: postgres.h:460
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406
#define Int32GetDatum(X)
Definition: postgres.h:523
void * palloc(Size size)
Definition: mcxt.c:1062
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:66
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1552

◆ recordSharedDependencyOn()

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

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

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

◆ ReleaseDeletionLock()

void ReleaseDeletionLock ( const ObjectAddress object)

Definition at line 1553 of file dependency.c.

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

Referenced by findDependentObjects(), and shdepDropOwned().

1554 {
1555  if (object->classId == RelationRelationId)
1557  else
1558  /* assume we should lock the whole object not a sub-object */
1559  UnlockDatabaseObject(object->classId, object->objectId, 0,
1561 }
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:200
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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:73
#define DependDependerIndexId
Definition: pg_depend.h:76
#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 1335 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().

1336 {
1337  Relation sdepRel;
1338  ListCell *cell;
1339  ObjectAddresses *deleteobjs;
1340 
1341  deleteobjs = new_object_addresses();
1342 
1343  /*
1344  * We don't need this strong a lock here, but we'll call routines that
1345  * acquire RowExclusiveLock. Better get that right now to avoid potential
1346  * deadlock failures.
1347  */
1348  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1349 
1350  /*
1351  * For each role, find the dependent objects and drop them using the
1352  * regular (non-shared) dependency management.
1353  */
1354  foreach(cell, roleids)
1355  {
1356  Oid roleid = lfirst_oid(cell);
1357  ScanKeyData key[2];
1358  SysScanDesc scan;
1359  HeapTuple tuple;
1360 
1361  /* Doesn't work for pinned objects */
1362  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
1363  {
1364  ObjectAddress obj;
1365 
1366  obj.classId = AuthIdRelationId;
1367  obj.objectId = roleid;
1368  obj.objectSubId = 0;
1369 
1370  ereport(ERROR,
1371  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1372  errmsg("cannot drop objects owned by %s because they are "
1373  "required by the database system",
1374  getObjectDescription(&obj, false))));
1375  }
1376 
1377  ScanKeyInit(&key[0],
1378  Anum_pg_shdepend_refclassid,
1379  BTEqualStrategyNumber, F_OIDEQ,
1380  ObjectIdGetDatum(AuthIdRelationId));
1381  ScanKeyInit(&key[1],
1382  Anum_pg_shdepend_refobjid,
1383  BTEqualStrategyNumber, F_OIDEQ,
1384  ObjectIdGetDatum(roleid));
1385 
1386  scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1387  NULL, 2, key);
1388 
1389  while ((tuple = systable_getnext(scan)) != NULL)
1390  {
1391  Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1392  ObjectAddress obj;
1393 
1394  /*
1395  * We only operate on shared objects and objects in the current
1396  * database
1397  */
1398  if (sdepForm->dbid != MyDatabaseId &&
1399  sdepForm->dbid != InvalidOid)
1400  continue;
1401 
1402  switch (sdepForm->deptype)
1403  {
1404  /* Shouldn't happen */
1405  case SHARED_DEPENDENCY_PIN:
1407  elog(ERROR, "unexpected dependency type");
1408  break;
1409  case SHARED_DEPENDENCY_ACL:
1410  RemoveRoleFromObjectACL(roleid,
1411  sdepForm->classid,
1412  sdepForm->objid);
1413  break;
1415 
1416  /*
1417  * Try to remove role from policy; if unable to, remove
1418  * policy.
1419  */
1420  if (!RemoveRoleFromObjectPolicy(roleid,
1421  sdepForm->classid,
1422  sdepForm->objid))
1423  {
1424  obj.classId = sdepForm->classid;
1425  obj.objectId = sdepForm->objid;
1426  obj.objectSubId = sdepForm->objsubid;
1427 
1428  /*
1429  * Acquire lock on object, then verify this dependency
1430  * is still relevant. If not, the object might have
1431  * been dropped or the policy modified. Ignore the
1432  * object in that case.
1433  */
1434  AcquireDeletionLock(&obj, 0);
1435  if (!systable_recheck_tuple(scan, tuple))
1436  {
1437  ReleaseDeletionLock(&obj);
1438  break;
1439  }
1440  add_exact_object_address(&obj, deleteobjs);
1441  }
1442  break;
1444  /* If a local object, save it for deletion below */
1445  if (sdepForm->dbid == MyDatabaseId)
1446  {
1447  obj.classId = sdepForm->classid;
1448  obj.objectId = sdepForm->objid;
1449  obj.objectSubId = sdepForm->objsubid;
1450  /* as above */
1451  AcquireDeletionLock(&obj, 0);
1452  if (!systable_recheck_tuple(scan, tuple))
1453  {
1454  ReleaseDeletionLock(&obj);
1455  break;
1456  }
1457  add_exact_object_address(&obj, deleteobjs);
1458  }
1459  break;
1460  }
1461  }
1462 
1463  systable_endscan(scan);
1464  }
1465 
1466  /*
1467  * For stability of deletion-report ordering, sort the objects into
1468  * approximate reverse creation order before deletion. (This might also
1469  * make the deletion go a bit faster, since there's less chance of having
1470  * to rearrange the objects due to dependencies.)
1471  */
1472  sort_object_addresses(deleteobjs);
1473 
1474  /* the dependency mechanism does the actual work */
1475  performMultipleDeletions(deleteobjs, behavior, 0);
1476 
1477  table_close(sdepRel, RowExclusiveLock);
1478 
1479  free_object_addresses(deleteobjs);
1480 }
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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1283
void sort_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2720
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:73
void ReleaseDeletionLock(const ObjectAddress *object)
Definition: dependency.c:1553
int errcode(int sqlerrcode)
Definition: elog.c:698
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2492
void RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid)
Definition: aclchk.c:1396
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2437
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2732
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:383
bool systable_recheck_tuple(SysScanDesc sysscan, HeapTuple tup)
Definition: genam.c:561
bool RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id)
Definition: policy.c:419
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define SharedDependReferenceIndexId
Definition: pg_shdepend.h:78
#define RowExclusiveLock
Definition: lockdefs.h:38
void AcquireDeletionLock(const ObjectAddress *object, int flags)
Definition: dependency.c:1524
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
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
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:171

◆ shdepLockAndCheckObject()

void shdepLockAndCheckObject ( Oid  classId,
Oid  objectId 
)

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

1166 {
1167  /* AccessShareLock should be OK, since we are not modifying the object */
1168  LockSharedObject(classId, objectId, 0, AccessShareLock);
1169 
1170  switch (classId)
1171  {
1172  case AuthIdRelationId:
1174  ereport(ERROR,
1175  (errcode(ERRCODE_UNDEFINED_OBJECT),
1176  errmsg("role %u was concurrently dropped",
1177  objectId)));
1178  break;
1179 
1180  case TableSpaceRelationId:
1181  {
1182  /* For lack of a syscache on pg_tablespace, do this: */
1183  char *tablespace = get_tablespace_name(objectId);
1184 
1185  if (tablespace == NULL)
1186  ereport(ERROR,
1187  (errcode(ERRCODE_UNDEFINED_OBJECT),
1188  errmsg("tablespace %u was concurrently dropped",
1189  objectId)));
1190  pfree(tablespace);
1191  break;
1192  }
1193 
1194  case DatabaseRelationId:
1195  {
1196  /* For lack of a syscache on pg_database, do this: */
1197  char *database = get_database_name(objectId);
1198 
1199  if (database == NULL)
1200  ereport(ERROR,
1201  (errcode(ERRCODE_UNDEFINED_OBJECT),
1202  errmsg("database %u was concurrently dropped",
1203  objectId)));
1204  pfree(database);
1205  break;
1206  }
1207 
1208 
1209  default:
1210  elog(ERROR, "unrecognized shared classId: %u", classId);
1211  }
1212 }
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SearchSysCacheExists1(cacheId, key1)
Definition: syscache.h:184
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2155
char * tablespace
Definition: pgbench.c:224
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1017
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * get_tablespace_name(Oid spc_oid)
Definition: tablespace.c:1479
#define elog(elevel,...)
Definition: elog.h:232

◆ shdepReassignOwned()

void shdepReassignOwned ( List relids,
Oid  newrole 
)

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

1490 {
1491  Relation sdepRel;
1492  ListCell *cell;
1493 
1494  /*
1495  * We don't need this strong a lock here, but we'll call routines that
1496  * acquire RowExclusiveLock. Better get that right now to avoid potential
1497  * deadlock problems.
1498  */
1499  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1500 
1501  foreach(cell, roleids)
1502  {
1503  SysScanDesc scan;
1504  ScanKeyData key[2];
1505  HeapTuple tuple;
1506  Oid roleid = lfirst_oid(cell);
1507 
1508  /* Refuse to work on pinned roles */
1509  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
1510  {
1511  ObjectAddress obj;
1512 
1513  obj.classId = AuthIdRelationId;
1514  obj.objectId = roleid;
1515  obj.objectSubId = 0;
1516 
1517  ereport(ERROR,
1518  (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1519  errmsg("cannot reassign ownership of objects owned by %s because they are required by the database system",
1520  getObjectDescription(&obj, false))));
1521 
1522  /*
1523  * There's no need to tell the whole truth, which is that we
1524  * didn't track these dependencies at all ...
1525  */
1526  }
1527 
1528  ScanKeyInit(&key[0],
1529  Anum_pg_shdepend_refclassid,
1530  BTEqualStrategyNumber, F_OIDEQ,
1531  ObjectIdGetDatum(AuthIdRelationId));
1532  ScanKeyInit(&key[1],
1533  Anum_pg_shdepend_refobjid,
1534  BTEqualStrategyNumber, F_OIDEQ,
1535  ObjectIdGetDatum(roleid));
1536 
1537  scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1538  NULL, 2, key);
1539 
1540  while ((tuple = systable_getnext(scan)) != NULL)
1541  {
1542  Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1543 
1544  /*
1545  * We only operate on shared objects and objects in the current
1546  * database
1547  */
1548  if (sdepForm->dbid != MyDatabaseId &&
1549  sdepForm->dbid != InvalidOid)
1550  continue;
1551 
1552  /* Unexpected because we checked for pins above */
1553  if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
1554  elog(ERROR, "unexpected shared pin");
1555 
1556  /* We leave non-owner dependencies alone */
1557  if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
1558  continue;
1559 
1560  /* Issue the appropriate ALTER OWNER call */
1561  switch (sdepForm->classid)
1562  {
1563  case TypeRelationId:
1564  AlterTypeOwner_oid(sdepForm->objid, newrole, true);
1565  break;
1566 
1567  case NamespaceRelationId:
1568  AlterSchemaOwner_oid(sdepForm->objid, newrole);
1569  break;
1570 
1571  case RelationRelationId:
1572 
1573  /*
1574  * Pass recursing = true so that we don't fail on indexes,
1575  * owned sequences, etc when we happen to visit them
1576  * before their parent table.
1577  */
1578  ATExecChangeOwner(sdepForm->objid, newrole, true, AccessExclusiveLock);
1579  break;
1580 
1581  case DefaultAclRelationId:
1582 
1583  /*
1584  * Ignore default ACLs; they should be handled by DROP
1585  * OWNED, not REASSIGN OWNED.
1586  */
1587  break;
1588 
1589  case UserMappingRelationId:
1590  /* ditto */
1591  break;
1592 
1593  case ForeignServerRelationId:
1594  AlterForeignServerOwner_oid(sdepForm->objid, newrole);
1595  break;
1596 
1597  case ForeignDataWrapperRelationId:
1598  AlterForeignDataWrapperOwner_oid(sdepForm->objid, newrole);
1599  break;
1600 
1601  case EventTriggerRelationId:
1602  AlterEventTriggerOwner_oid(sdepForm->objid, newrole);
1603  break;
1604 
1605  case PublicationRelationId:
1606  AlterPublicationOwner_oid(sdepForm->objid, newrole);
1607  break;
1608 
1609  case SubscriptionRelationId:
1610  AlterSubscriptionOwner_oid(sdepForm->objid, newrole);
1611  break;
1612 
1613  /* Generic alter owner cases */
1614  case CollationRelationId:
1615  case ConversionRelationId:
1616  case OperatorRelationId:
1617  case ProcedureRelationId:
1618  case LanguageRelationId:
1619  case LargeObjectRelationId:
1620  case OperatorFamilyRelationId:
1621  case OperatorClassRelationId:
1622  case ExtensionRelationId:
1623  case StatisticExtRelationId:
1624  case TableSpaceRelationId:
1625  case DatabaseRelationId:
1626  case TSConfigRelationId:
1627  case TSDictionaryRelationId:
1628  {
1629  Oid classId = sdepForm->classid;
1630  Relation catalog;
1631 
1632  if (classId == LargeObjectRelationId)
1633  classId = LargeObjectMetadataRelationId;
1634 
1635  catalog = table_open(classId, RowExclusiveLock);
1636 
1637  AlterObjectOwner_internal(catalog, sdepForm->objid,
1638  newrole);
1639 
1640  table_close(catalog, NoLock);
1641  }
1642  break;
1643 
1644  default:
1645  elog(ERROR, "unexpected classid %u", sdepForm->classid);
1646  break;
1647  }
1648  /* Make sure the next iteration will see my changes */
1650  }
1651 
1652  systable_endscan(scan);
1653  }
1654 
1655  table_close(sdepRel, RowExclusiveLock);
1656 }
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:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
void AlterForeignDataWrapperOwner_oid(Oid fwdId, Oid newOwnerId)
Definition: foreigncmds.c:313
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1283
void AlterSchemaOwner_oid(Oid oid, Oid newOwnerId)
Definition: schemacmds.c:276
FormData_pg_shdepend * Form_pg_shdepend
Definition: pg_shdepend.h:73
void AlterForeignServerOwner_oid(Oid srvId, Oid newOwnerId)
Definition: foreigncmds.c:450
int errcode(int sqlerrcode)
Definition: elog.c:698
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:383
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define SharedDependReferenceIndexId
Definition: pg_shdepend.h:78
void AlterTypeOwner_oid(Oid typeOid, Oid newOwnerId, bool hasDependEntry)
Definition: typecmds.c:3800
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:13142
void CommandCounterIncrement(void)
Definition: xact.c:1021
Oid MyDatabaseId
Definition: globals.c:88
#define InvalidOid
Definition: postgres_ext.h:36
void AlterEventTriggerOwner_oid(Oid trigOid, Oid newOwnerId)
#define ereport(elevel,...)
Definition: elog.h:157
#define AccessExclusiveLock
Definition: lockdefs.h:45
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
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)
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:171

◆ sort_object_addresses()

void sort_object_addresses ( ObjectAddresses addrs)

Definition at line 2720 of file dependency.c.

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

Referenced by shdepDropOwned().

2721 {
2722  if (addrs->numrefs > 1)
2723  qsort((void *) addrs->refs, addrs->numrefs,
2724  sizeof(ObjectAddress),
2726 }
static int object_address_comparator(const void *a, const void *b)
Definition: dependency.c:2393
ObjectAddress * refs
Definition: dependency.c:111
#define qsort(a, b, c, d)
Definition: port.h:504

◆ updateAclDependencies()

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

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

486 {
487  Relation sdepRel;
488  int i;
489 
490  /*
491  * Remove entries that are common to both lists; those represent existing
492  * dependencies we don't need to change.
493  *
494  * OK to overwrite the inputs since we'll pfree them anyway.
495  */
496  getOidListDiff(oldmembers, &noldmembers, newmembers, &nnewmembers);
497 
498  if (noldmembers > 0 || nnewmembers > 0)
499  {
500  sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
501 
502  /* Add new dependencies that weren't already present */
503  for (i = 0; i < nnewmembers; i++)
504  {
505  Oid roleid = newmembers[i];
506 
507  /*
508  * Skip the owner: he has an OWNER shdep entry instead. (This is
509  * not just a space optimization; it makes ALTER OWNER easier. See
510  * notes in changeDependencyOnOwner.)
511  */
512  if (roleid == ownerId)
513  continue;
514 
515  /* Skip pinned roles; they don't need dependency entries */
516  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
517  continue;
518 
519  shdepAddDependency(sdepRel, classId, objectId, objsubId,
520  AuthIdRelationId, roleid,
522  }
523 
524  /* Drop no-longer-used old dependencies */
525  for (i = 0; i < noldmembers; i++)
526  {
527  Oid roleid = oldmembers[i];
528 
529  /* Skip the owner, same as above */
530  if (roleid == ownerId)
531  continue;
532 
533  /* Skip pinned roles */
534  if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
535  continue;
536 
537  shdepDropDependency(sdepRel, classId, objectId, objsubId,
538  false, /* exact match on objsubId */
539  AuthIdRelationId, roleid,
541  }
542 
543  table_close(sdepRel, RowExclusiveLock);
544  }
545 
546  if (oldmembers)
547  pfree(oldmembers);
548  if (newmembers)
549  pfree(newmembers);
550 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
Definition: pg_shdepend.c:1283
static void getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2)
Definition: pg_shdepend.c:412
static void shdepAddDependency(Relation sdepRel, Oid classId, Oid objectId, int32 objsubId, Oid refclassId, Oid refobjId, SharedDependencyType deptype)
Definition: pg_shdepend.c:1023
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:1169
#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:1078
int i
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39