PostgreSQL Source Code  git master
dependency.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * dependency.h
4  * Routines to support inter-object dependencies.
5  *
6  *
7  * Portions Copyright (c) 1996-2018, 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 
17 #include "catalog/objectaddress.h"
18 
19 
20 /*
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).
24  *
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:
28  *
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.
34  *
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
38  * is dropped.
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.
41  *
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
48  * specified or not.
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.
51  *
52  * DEPENDENCY_INTERNAL_AUTO ('I'): the dependent object was created as
53  * part of creation of the referenced object, and is really just a part
54  * of its internal implementation. A DROP of the dependent object will
55  * be disallowed outright (we'll tell the user to issue a DROP against the
56  * referenced object, instead). While a regular internal dependency will
57  * prevent the dependent object from being dropped while any such
58  * dependencies remain, DEPENDENCY_INTERNAL_AUTO will allow such a drop as
59  * long as the object can be found by following any of such dependencies.
60  * Example: an index on a partition is made internal-auto-dependent on
61  * both the partition itself as well as on the index on the parent
62  * partitioned table; so the partition index is dropped together with
63  * either the partition it indexes, or with the parent index it is attached
64  * to.
65 
66  * DEPENDENCY_EXTENSION ('e'): the dependent object is a member of the
67  * extension that is the referenced object. The dependent object can be
68  * dropped only via DROP EXTENSION on the referenced object. Functionally
69  * this dependency type acts the same as an internal dependency, but it's
70  * kept separate for clarity and to simplify pg_dump.
71  *
72  * DEPENDENCY_AUTO_EXTENSION ('x'): the dependent object is not a member
73  * of the extension that is the referenced object (and so should not be
74  * ignored by pg_dump), but cannot function without the extension and
75  * should be dropped when the extension itself is. The dependent object
76  * may be dropped on its own as well.
77  *
78  * DEPENDENCY_PIN ('p'): there is no dependent object; this type of entry
79  * is a signal that the system itself depends on the referenced object,
80  * and so that object must never be deleted. Entries of this type are
81  * created only during initdb. The fields for the dependent object
82  * contain zeroes.
83  *
84  * Other dependency flavors may be needed in future.
85  */
86 
87 typedef enum DependencyType
88 {
97 
98 /*
99  * There is also a SharedDependencyType enum type that determines the exact
100  * semantics of an entry in pg_shdepend. Just like regular dependency entries,
101  * any pg_shdepend entry means that the referenced object cannot be dropped
102  * unless the dependent object is dropped at the same time. There are some
103  * additional rules however:
104  *
105  * (a) For a SHARED_DEPENDENCY_PIN entry, there is no dependent object --
106  * rather, the referenced object is an essential part of the system. This
107  * applies to the initdb-created superuser. Entries of this type are only
108  * created by initdb; objects in this category don't need further pg_shdepend
109  * entries if more objects come to depend on them.
110  *
111  * (b) a SHARED_DEPENDENCY_OWNER entry means that the referenced object is
112  * the role owning the dependent object. The referenced object must be
113  * a pg_authid entry.
114  *
115  * (c) a SHARED_DEPENDENCY_ACL entry means that the referenced object is
116  * a role mentioned in the ACL field of the dependent object. The referenced
117  * object must be a pg_authid entry. (SHARED_DEPENDENCY_ACL entries are not
118  * created for the owner of an object; hence two objects may be linked by
119  * one or the other, but not both, of these dependency types.)
120  *
121  * (d) a SHARED_DEPENDENCY_POLICY entry means that the referenced object is
122  * a role mentioned in a policy object. The referenced object must be a
123  * pg_authid entry.
124  *
125  * SHARED_DEPENDENCY_INVALID is a value used as a parameter in internal
126  * routines, and is not valid in the catalog itself.
127  */
129 {
136 
137 /* expansible list of ObjectAddresses (private in dependency.c) */
139 
140 /*
141  * This enum covers all system catalogs whose OIDs can appear in
142  * pg_depend.classId or pg_shdepend.classId. Keep object_classes[] in sync.
143  */
144 typedef enum ObjectClass
145 {
146  OCLASS_CLASS, /* pg_class */
147  OCLASS_PROC, /* pg_proc */
148  OCLASS_TYPE, /* pg_type */
149  OCLASS_CAST, /* pg_cast */
150  OCLASS_COLLATION, /* pg_collation */
151  OCLASS_CONSTRAINT, /* pg_constraint */
152  OCLASS_CONVERSION, /* pg_conversion */
153  OCLASS_DEFAULT, /* pg_attrdef */
154  OCLASS_LANGUAGE, /* pg_language */
155  OCLASS_LARGEOBJECT, /* pg_largeobject */
156  OCLASS_OPERATOR, /* pg_operator */
157  OCLASS_OPCLASS, /* pg_opclass */
158  OCLASS_OPFAMILY, /* pg_opfamily */
159  OCLASS_AM, /* pg_am */
160  OCLASS_AMOP, /* pg_amop */
161  OCLASS_AMPROC, /* pg_amproc */
162  OCLASS_REWRITE, /* pg_rewrite */
163  OCLASS_TRIGGER, /* pg_trigger */
164  OCLASS_SCHEMA, /* pg_namespace */
165  OCLASS_STATISTIC_EXT, /* pg_statistic_ext */
166  OCLASS_TSPARSER, /* pg_ts_parser */
167  OCLASS_TSDICT, /* pg_ts_dict */
168  OCLASS_TSTEMPLATE, /* pg_ts_template */
169  OCLASS_TSCONFIG, /* pg_ts_config */
170  OCLASS_ROLE, /* pg_authid */
171  OCLASS_DATABASE, /* pg_database */
172  OCLASS_TBLSPACE, /* pg_tablespace */
173  OCLASS_FDW, /* pg_foreign_data_wrapper */
174  OCLASS_FOREIGN_SERVER, /* pg_foreign_server */
175  OCLASS_USER_MAPPING, /* pg_user_mapping */
176  OCLASS_DEFACL, /* pg_default_acl */
177  OCLASS_EXTENSION, /* pg_extension */
178  OCLASS_EVENT_TRIGGER, /* pg_event_trigger */
179  OCLASS_POLICY, /* pg_policy */
180  OCLASS_PUBLICATION, /* pg_publication */
181  OCLASS_PUBLICATION_REL, /* pg_publication_rel */
182  OCLASS_SUBSCRIPTION, /* pg_subscription */
183  OCLASS_TRANSFORM /* pg_transform */
184 } ObjectClass;
185 
186 #define LAST_OCLASS OCLASS_TRANSFORM
187 
188 /* flag bits for performDeletion/performMultipleDeletions: */
189 #define PERFORM_DELETION_INTERNAL 0x0001 /* internal action */
190 #define PERFORM_DELETION_CONCURRENTLY 0x0002 /* concurrent drop */
191 #define PERFORM_DELETION_QUIETLY 0x0004 /* suppress notices */
192 #define PERFORM_DELETION_SKIP_ORIGINAL 0x0008 /* keep original obj */
193 #define PERFORM_DELETION_SKIP_EXTENSIONS 0x0010 /* keep extensions */
194 
195 
196 /* in dependency.c */
197 
198 extern void performDeletion(const ObjectAddress *object,
199  DropBehavior behavior, int flags);
200 
201 extern void performMultipleDeletions(const ObjectAddresses *objects,
202  DropBehavior behavior, int flags);
203 
204 extern void recordDependencyOnExpr(const ObjectAddress *depender,
205  Node *expr, List *rtable,
206  DependencyType behavior);
207 
208 extern void recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
209  Node *expr, Oid relId,
210  DependencyType behavior,
211  DependencyType self_behavior,
212  bool ignore_self);
213 
214 extern ObjectClass getObjectClass(const ObjectAddress *object);
215 
217 
218 extern void add_exact_object_address(const ObjectAddress *object,
219  ObjectAddresses *addrs);
220 
221 extern bool object_address_present(const ObjectAddress *object,
222  const ObjectAddresses *addrs);
223 
224 extern void record_object_address_dependencies(const ObjectAddress *depender,
225  ObjectAddresses *referenced,
226  DependencyType behavior);
227 
228 extern void free_object_addresses(ObjectAddresses *addrs);
229 
230 /* in pg_depend.c */
231 
232 extern void recordDependencyOn(const ObjectAddress *depender,
233  const ObjectAddress *referenced,
234  DependencyType behavior);
235 
236 extern void recordMultipleDependencies(const ObjectAddress *depender,
237  const ObjectAddress *referenced,
238  int nreferenced,
239  DependencyType behavior);
240 
241 extern void recordDependencyOnCurrentExtension(const ObjectAddress *object,
242  bool isReplace);
243 
244 extern long deleteDependencyRecordsFor(Oid classId, Oid objectId,
245  bool skipExtensionDeps);
246 
247 extern long deleteDependencyRecordsForClass(Oid classId, Oid objectId,
248  Oid refclassId, char deptype);
249 
250 extern long changeDependencyFor(Oid classId, Oid objectId,
251  Oid refClassId, Oid oldRefObjectId,
252  Oid newRefObjectId);
253 
254 extern Oid getExtensionOfObject(Oid classId, Oid objectId);
255 
256 extern bool sequenceIsOwned(Oid seqId, char deptype, Oid *tableId, int32 *colId);
257 extern List *getOwnedSequences(Oid relid, AttrNumber attnum);
258 extern Oid getOwnedSequence(Oid relid, AttrNumber attnum);
259 
260 extern Oid get_constraint_index(Oid constraintId);
261 
262 extern Oid get_index_constraint(Oid indexId);
263 
264 /* in pg_shdepend.c */
265 
266 extern void recordSharedDependencyOn(ObjectAddress *depender,
267  ObjectAddress *referenced,
268  SharedDependencyType deptype);
269 
270 extern void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId,
271  int32 objectSubId);
272 
273 extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
274 
275 extern void changeDependencyOnOwner(Oid classId, Oid objectId,
276  Oid newOwnerId);
277 
278 extern void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId,
279  Oid ownerId,
280  int noldmembers, Oid *oldmembers,
281  int nnewmembers, Oid *newmembers);
282 
283 extern bool checkSharedDependencies(Oid classId, Oid objectId,
284  char **detail_msg, char **detail_log_msg);
285 
286 extern void shdepLockAndCheckObject(Oid classId, Oid objectId);
287 
288 extern void copyTemplateDependencies(Oid templateDbId, Oid newDbId);
289 
290 extern void dropDatabaseDependencies(Oid databaseId);
291 
292 extern void shdepDropOwned(List *relids, DropBehavior behavior);
293 
294 extern void shdepReassignOwned(List *relids, Oid newrole);
295 
296 #endif /* DEPENDENCY_H */
Oid getOwnedSequence(Oid relid, AttrNumber attnum)
Definition: pg_depend.c:605
DependencyType
Definition: dependency.h:87
List * getOwnedSequences(Oid relid, AttrNumber attnum)
Definition: pg_depend.c:548
void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:421
bool sequenceIsOwned(Oid seqId, char deptype, Oid *tableId, int32 *colId)
Definition: pg_depend.c:500
void shdepLockAndCheckObject(Oid classId, Oid objectId)
Definition: pg_shdepend.c:987
void shdepReassignOwned(List *relids, Oid newrole)
Definition: pg_shdepend.c:1285
bool checkSharedDependencies(Oid classId, Oid objectId, char **detail_msg, char **detail_log_msg)
Definition: pg_shdepend.c:522
void performDeletion(const ObjectAddress *object, DropBehavior behavior, int flags)
Definition: dependency.c:303
Definition: nodes.h:513
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
Definition: pg_depend.c:241
void recordMultipleDependencies(const ObjectAddress *depender, const ObjectAddress *referenced, int nreferenced, DependencyType behavior)
Definition: pg_depend.c:56
unsigned int Oid
Definition: postgres_ext.h:31
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
Definition: pg_shdepend.c:304
void dropDatabaseDependencies(Oid databaseId)
Definition: pg_shdepend.c:775
signed int int32
Definition: c.h:302
void copyTemplateDependencies(Oid templateDbId, Oid newDbId)
Definition: pg_shdepend.c:711
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:295
ObjectClass
Definition: dependency.h:144
void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
Definition: pg_shdepend.c:823
ObjectClass getObjectClass(const ObjectAddress *object)
Definition: dependency.c:2419
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2404
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
SharedDependencyType
Definition: dependency.h:128
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
Definition: dependency.c:2248
DropBehavior
Definition: parsenodes.h:1702
Oid get_index_constraint(Oid indexId)
Definition: pg_depend.c:689
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:191
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2188
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2133
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
Definition: dependency.c:362
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:447
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
Oid get_constraint_index(Oid constraintId)
Definition: pg_depend.c:626
void recordDependencyOnExpr(const ObjectAddress *depender, Node *expr, List *rtable, DependencyType behavior)
Definition: dependency.c:1363
void recordSharedDependencyOn(ObjectAddress *depender, ObjectAddress *referenced, SharedDependencyType deptype)
Definition: pg_shdepend.c:115
void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, Node *expr, Oid relId, DependencyType behavior, DependencyType self_behavior, bool ignore_self)
Definition: dependency.c:1404
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2390
void shdepDropOwned(List *relids, DropBehavior behavior)
Definition: pg_shdepend.c:1163