dependency.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*-------------------------------------------------------------------------
  2. *
  3. * dependency.h
  4. * Routines to support inter-object dependencies.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/catalog/dependency.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef DEPENDENCY_H
  15. #define DEPENDENCY_H
  16. #include "catalog/objectaddress.h"
  17. /*
  18. * Precise semantics of a dependency relationship are specified by the
  19. * DependencyType code (which is stored in a "char" field in pg_depend,
  20. * so we assign ASCII-code values to the enumeration members).
  21. *
  22. * In all cases, a dependency relationship indicates that the referenced
  23. * object may not be dropped without also dropping the dependent object.
  24. * However, there are several subflavors:
  25. *
  26. * DEPENDENCY_NORMAL ('n'): normal relationship between separately-created
  27. * objects. The dependent object may be dropped without affecting the
  28. * referenced object. The referenced object may only be dropped by
  29. * specifying CASCADE, in which case the dependent object is dropped too.
  30. * Example: a table column has a normal dependency on its datatype.
  31. *
  32. * DEPENDENCY_AUTO ('a'): the dependent object can be dropped separately
  33. * from the referenced object, and should be automatically dropped
  34. * (regardless of RESTRICT or CASCADE mode) if the referenced object
  35. * is dropped.
  36. * Example: a named constraint on a table is made auto-dependent on
  37. * the table, so that it will go away if the table is dropped.
  38. *
  39. * DEPENDENCY_INTERNAL ('i'): the dependent object was created as part
  40. * of creation of the referenced object, and is really just a part of
  41. * its internal implementation. A DROP of the dependent object will be
  42. * disallowed outright (we'll tell the user to issue a DROP against the
  43. * referenced object, instead). A DROP of the referenced object will be
  44. * propagated through to drop the dependent object whether CASCADE is
  45. * specified or not.
  46. * Example: a trigger that's created to enforce a foreign-key constraint
  47. * is made internally dependent on the constraint's pg_constraint entry.
  48. *
  49. * DEPENDENCY_EXTENSION ('e'): the dependent object is a member of the
  50. * extension that is the referenced object. The dependent object can be
  51. * dropped only via DROP EXTENSION on the referenced object. Functionally
  52. * this dependency type acts the same as an internal dependency, but it's
  53. * kept separate for clarity and to simplify pg_dump.
  54. *
  55. * DEPENDENCY_AUTO_EXTENSION ('x'): the dependent object is not a member
  56. * of the extension that is the referenced object (and so should not be
  57. * ignored by pg_dump), but cannot function without the extension and
  58. * should be dropped when the extension itself is. The dependent object
  59. * may be dropped on its own as well.
  60. *
  61. * DEPENDENCY_PIN ('p'): there is no dependent object; this type of entry
  62. * is a signal that the system itself depends on the referenced object,
  63. * and so that object must never be deleted. Entries of this type are
  64. * created only during initdb. The fields for the dependent object
  65. * contain zeroes.
  66. *
  67. * Other dependency flavors may be needed in future.
  68. */
  69. typedef enum DependencyType
  70. {
  71. DEPENDENCY_NORMAL = 'n',
  72. DEPENDENCY_AUTO = 'a',
  73. DEPENDENCY_INTERNAL = 'i',
  74. DEPENDENCY_EXTENSION = 'e',
  75. DEPENDENCY_AUTO_EXTENSION = 'x',
  76. DEPENDENCY_PIN = 'p'
  77. } DependencyType;
  78. /*
  79. * There is also a SharedDependencyType enum type that determines the exact
  80. * semantics of an entry in pg_shdepend. Just like regular dependency entries,
  81. * any pg_shdepend entry means that the referenced object cannot be dropped
  82. * unless the dependent object is dropped at the same time. There are some
  83. * additional rules however:
  84. *
  85. * (a) For a SHARED_DEPENDENCY_PIN entry, there is no dependent object --
  86. * rather, the referenced object is an essential part of the system. This
  87. * applies to the initdb-created superuser. Entries of this type are only
  88. * created by initdb; objects in this category don't need further pg_shdepend
  89. * entries if more objects come to depend on them.
  90. *
  91. * (b) a SHARED_DEPENDENCY_OWNER entry means that the referenced object is
  92. * the role owning the dependent object. The referenced object must be
  93. * a pg_authid entry.
  94. *
  95. * (c) a SHARED_DEPENDENCY_ACL entry means that the referenced object is
  96. * a role mentioned in the ACL field of the dependent object. The referenced
  97. * object must be a pg_authid entry. (SHARED_DEPENDENCY_ACL entries are not
  98. * created for the owner of an object; hence two objects may be linked by
  99. * one or the other, but not both, of these dependency types.)
  100. *
  101. * (d) a SHARED_DEPENDENCY_POLICY entry means that the referenced object is
  102. * a role mentioned in a policy object. The referenced object must be a
  103. * pg_authid entry.
  104. *
  105. * SHARED_DEPENDENCY_INVALID is a value used as a parameter in internal
  106. * routines, and is not valid in the catalog itself.
  107. */
  108. typedef enum SharedDependencyType
  109. {
  110. SHARED_DEPENDENCY_PIN = 'p',
  111. SHARED_DEPENDENCY_OWNER = 'o',
  112. SHARED_DEPENDENCY_ACL = 'a',
  113. SHARED_DEPENDENCY_POLICY = 'r',
  114. SHARED_DEPENDENCY_INVALID = 0
  115. } SharedDependencyType;
  116. /* expansible list of ObjectAddresses (private in dependency.c) */
  117. typedef struct ObjectAddresses ObjectAddresses;
  118. /*
  119. * This enum covers all system catalogs whose OIDs can appear in
  120. * pg_depend.classId or pg_shdepend.classId. Keep object_classes[] in sync.
  121. */
  122. typedef enum ObjectClass
  123. {
  124. OCLASS_CLASS, /* pg_class */
  125. OCLASS_PROC, /* pg_proc */
  126. OCLASS_TYPE, /* pg_type */
  127. OCLASS_CAST, /* pg_cast */
  128. OCLASS_COLLATION, /* pg_collation */
  129. OCLASS_CONSTRAINT, /* pg_constraint */
  130. OCLASS_CONVERSION, /* pg_conversion */
  131. OCLASS_DEFAULT, /* pg_attrdef */
  132. OCLASS_LANGUAGE, /* pg_language */
  133. OCLASS_LARGEOBJECT, /* pg_largeobject */
  134. OCLASS_OPERATOR, /* pg_operator */
  135. OCLASS_OPCLASS, /* pg_opclass */
  136. OCLASS_OPFAMILY, /* pg_opfamily */
  137. OCLASS_AM, /* pg_am */
  138. OCLASS_AMOP, /* pg_amop */
  139. OCLASS_AMPROC, /* pg_amproc */
  140. OCLASS_REWRITE, /* pg_rewrite */
  141. OCLASS_TRIGGER, /* pg_trigger */
  142. OCLASS_SCHEMA, /* pg_namespace */
  143. OCLASS_TSPARSER, /* pg_ts_parser */
  144. OCLASS_TSDICT, /* pg_ts_dict */
  145. OCLASS_TSTEMPLATE, /* pg_ts_template */
  146. OCLASS_TSCONFIG, /* pg_ts_config */
  147. OCLASS_ROLE, /* pg_authid */
  148. OCLASS_DATABASE, /* pg_database */
  149. OCLASS_TBLSPACE, /* pg_tablespace */
  150. OCLASS_FDW, /* pg_foreign_data_wrapper */
  151. OCLASS_FOREIGN_SERVER, /* pg_foreign_server */
  152. OCLASS_USER_MAPPING, /* pg_user_mapping */
  153. OCLASS_DEFACL, /* pg_default_acl */
  154. OCLASS_EXTENSION, /* pg_extension */
  155. OCLASS_EVENT_TRIGGER, /* pg_event_trigger */
  156. OCLASS_POLICY, /* pg_policy */
  157. OCLASS_TRANSFORM /* pg_transform */
  158. } ObjectClass;
  159. #define LAST_OCLASS OCLASS_TRANSFORM
  160. /* in dependency.c */
  161. #define PERFORM_DELETION_INTERNAL 0x0001
  162. #define PERFORM_DELETION_CONCURRENTLY 0x0002
  163. extern void performDeletion(const ObjectAddress *object,
  164. DropBehavior behavior, int flags);
  165. extern void performMultipleDeletions(const ObjectAddresses *objects,
  166. DropBehavior behavior, int flags);
  167. extern void deleteWhatDependsOn(const ObjectAddress *object,
  168. bool showNotices);
  169. extern void recordDependencyOnExpr(const ObjectAddress *depender,
  170. Node *expr, List *rtable,
  171. DependencyType behavior);
  172. extern void recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
  173. Node *expr, Oid relId,
  174. DependencyType behavior,
  175. DependencyType self_behavior);
  176. extern ObjectClass getObjectClass(const ObjectAddress *object);
  177. extern ObjectAddresses *new_object_addresses(void);
  178. extern void add_exact_object_address(const ObjectAddress *object,
  179. ObjectAddresses *addrs);
  180. extern bool object_address_present(const ObjectAddress *object,
  181. const ObjectAddresses *addrs);
  182. extern void record_object_address_dependencies(const ObjectAddress *depender,
  183. ObjectAddresses *referenced,
  184. DependencyType behavior);
  185. extern void free_object_addresses(ObjectAddresses *addrs);
  186. /* in pg_depend.c */
  187. extern void recordDependencyOn(const ObjectAddress *depender,
  188. const ObjectAddress *referenced,
  189. DependencyType behavior);
  190. extern void recordMultipleDependencies(const ObjectAddress *depender,
  191. const ObjectAddress *referenced,
  192. int nreferenced,
  193. DependencyType behavior);
  194. extern void recordDependencyOnCurrentExtension(const ObjectAddress *object,
  195. bool isReplace);
  196. extern long deleteDependencyRecordsFor(Oid classId, Oid objectId,
  197. bool skipExtensionDeps);
  198. extern long deleteDependencyRecordsForClass(Oid classId, Oid objectId,
  199. Oid refclassId, char deptype);
  200. extern long changeDependencyFor(Oid classId, Oid objectId,
  201. Oid refClassId, Oid oldRefObjectId,
  202. Oid newRefObjectId);
  203. extern Oid getExtensionOfObject(Oid classId, Oid objectId);
  204. extern bool sequenceIsOwned(Oid seqId, Oid *tableId, int32 *colId);
  205. extern void markSequenceUnowned(Oid seqId);
  206. extern List *getOwnedSequences(Oid relid);
  207. extern Oid get_constraint_index(Oid constraintId);
  208. extern Oid get_index_constraint(Oid indexId);
  209. /* in pg_shdepend.c */
  210. extern void recordSharedDependencyOn(ObjectAddress *depender,
  211. ObjectAddress *referenced,
  212. SharedDependencyType deptype);
  213. extern void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId,
  214. int32 objectSubId);
  215. extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
  216. extern void changeDependencyOnOwner(Oid classId, Oid objectId,
  217. Oid newOwnerId);
  218. extern void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId,
  219. Oid ownerId,
  220. int noldmembers, Oid *oldmembers,
  221. int nnewmembers, Oid *newmembers);
  222. extern bool checkSharedDependencies(Oid classId, Oid objectId,
  223. char **detail_msg, char **detail_log_msg);
  224. extern void shdepLockAndCheckObject(Oid classId, Oid objectId);
  225. extern void copyTemplateDependencies(Oid templateDbId, Oid newDbId);
  226. extern void dropDatabaseDependencies(Oid databaseId);
  227. extern void shdepDropOwned(List *relids, DropBehavior behavior);
  228. extern void shdepReassignOwned(List *relids, Oid newrole);
  229. #endif /* DEPENDENCY_H */