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