1 /*-------------------------------------------------------------------------
2  *
3  * dependency.h
4  *	  Routines to support inter-object dependencies.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, 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_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.
57  *
58  * DEPENDENCY_AUTO_EXTENSION ('x'): the dependent object is not a member
59  * of the extension that is the referenced object (and so should not be
60  * ignored by pg_dump), but cannot function without the extension and
61  * should be dropped when the extension itself is.  The dependent object
62  * may be dropped on its own as well.
63  *
64  * DEPENDENCY_PIN ('p'): there is no dependent object; this type of entry
65  * is a signal that the system itself depends on the referenced object,
66  * and so that object must never be deleted.  Entries of this type are
67  * created only during initdb.  The fields for the dependent object
68  * contain zeroes.
69  *
70  * Other dependency flavors may be needed in future.
71  */
72 
73 typedef enum DependencyType
74 {
75 	DEPENDENCY_NORMAL = 'n',
76 	DEPENDENCY_AUTO = 'a',
77 	DEPENDENCY_INTERNAL = 'i',
78 	DEPENDENCY_EXTENSION = 'e',
79 	DEPENDENCY_AUTO_EXTENSION = 'x',
80 	DEPENDENCY_PIN = 'p'
81 } DependencyType;
82 
83 /*
84  * There is also a SharedDependencyType enum type that determines the exact
85  * semantics of an entry in pg_shdepend.  Just like regular dependency entries,
86  * any pg_shdepend entry means that the referenced object cannot be dropped
87  * unless the dependent object is dropped at the same time.  There are some
88  * additional rules however:
89  *
90  * (a) For a SHARED_DEPENDENCY_PIN entry, there is no dependent object --
91  * rather, the referenced object is an essential part of the system.  This
92  * applies to the initdb-created superuser.  Entries of this type are only
93  * created by initdb; objects in this category don't need further pg_shdepend
94  * entries if more objects come to depend on them.
95  *
96  * (b) a SHARED_DEPENDENCY_OWNER entry means that the referenced object is
97  * the role owning the dependent object.  The referenced object must be
98  * a pg_authid entry.
99  *
100  * (c) a SHARED_DEPENDENCY_ACL entry means that the referenced object is
101  * a role mentioned in the ACL field of the dependent object.  The referenced
102  * object must be a pg_authid entry.  (SHARED_DEPENDENCY_ACL entries are not
103  * created for the owner of an object; hence two objects may be linked by
104  * one or the other, but not both, of these dependency types.)
105  *
106  * (d) a SHARED_DEPENDENCY_POLICY entry means that the referenced object is
107  * a role mentioned in a policy object.  The referenced object must be a
108  * pg_authid entry.
109  *
110  * SHARED_DEPENDENCY_INVALID is a value used as a parameter in internal
111  * routines, and is not valid in the catalog itself.
112  */
113 typedef enum SharedDependencyType
114 {
115 	SHARED_DEPENDENCY_PIN = 'p',
116 	SHARED_DEPENDENCY_OWNER = 'o',
117 	SHARED_DEPENDENCY_ACL = 'a',
118 	SHARED_DEPENDENCY_POLICY = 'r',
119 	SHARED_DEPENDENCY_INVALID = 0
120 } SharedDependencyType;
121 
122 /* expansible list of ObjectAddresses (private in dependency.c) */
123 typedef struct ObjectAddresses ObjectAddresses;
124 
125 /*
126  * This enum covers all system catalogs whose OIDs can appear in
127  * pg_depend.classId or pg_shdepend.classId.  Keep object_classes[] in sync.
128  */
129 typedef enum ObjectClass
130 {
131 	OCLASS_CLASS,				/* pg_class */
132 	OCLASS_PROC,				/* pg_proc */
133 	OCLASS_TYPE,				/* pg_type */
134 	OCLASS_CAST,				/* pg_cast */
135 	OCLASS_COLLATION,			/* pg_collation */
136 	OCLASS_CONSTRAINT,			/* pg_constraint */
137 	OCLASS_CONVERSION,			/* pg_conversion */
138 	OCLASS_DEFAULT,				/* pg_attrdef */
139 	OCLASS_LANGUAGE,			/* pg_language */
140 	OCLASS_LARGEOBJECT,			/* pg_largeobject */
141 	OCLASS_OPERATOR,			/* pg_operator */
142 	OCLASS_OPCLASS,				/* pg_opclass */
143 	OCLASS_OPFAMILY,			/* pg_opfamily */
144 	OCLASS_AM,					/* pg_am */
145 	OCLASS_AMOP,				/* pg_amop */
146 	OCLASS_AMPROC,				/* pg_amproc */
147 	OCLASS_REWRITE,				/* pg_rewrite */
148 	OCLASS_TRIGGER,				/* pg_trigger */
149 	OCLASS_SCHEMA,				/* pg_namespace */
150 	OCLASS_STATISTIC_EXT,		/* pg_statistic_ext */
151 	OCLASS_TSPARSER,			/* pg_ts_parser */
152 	OCLASS_TSDICT,				/* pg_ts_dict */
153 	OCLASS_TSTEMPLATE,			/* pg_ts_template */
154 	OCLASS_TSCONFIG,			/* pg_ts_config */
155 	OCLASS_ROLE,				/* pg_authid */
156 	OCLASS_DATABASE,			/* pg_database */
157 	OCLASS_TBLSPACE,			/* pg_tablespace */
158 	OCLASS_FDW,					/* pg_foreign_data_wrapper */
159 	OCLASS_FOREIGN_SERVER,		/* pg_foreign_server */
160 	OCLASS_USER_MAPPING,		/* pg_user_mapping */
161 	OCLASS_DEFACL,				/* pg_default_acl */
162 	OCLASS_EXTENSION,			/* pg_extension */
163 	OCLASS_EVENT_TRIGGER,		/* pg_event_trigger */
164 	OCLASS_POLICY,				/* pg_policy */
165 	OCLASS_PUBLICATION,			/* pg_publication */
166 	OCLASS_PUBLICATION_REL,		/* pg_publication_rel */
167 	OCLASS_SUBSCRIPTION,		/* pg_subscription */
168 	OCLASS_TRANSFORM			/* pg_transform */
169 } ObjectClass;
170 
171 #define LAST_OCLASS		OCLASS_TRANSFORM
172 
173 /* flag bits for performDeletion/performMultipleDeletions: */
174 #define PERFORM_DELETION_INTERNAL			0x0001	/* internal action */
175 #define PERFORM_DELETION_CONCURRENTLY		0x0002	/* concurrent drop */
176 #define PERFORM_DELETION_QUIETLY			0x0004	/* suppress notices */
177 #define PERFORM_DELETION_SKIP_ORIGINAL		0x0008	/* keep original obj */
178 #define PERFORM_DELETION_SKIP_EXTENSIONS	0x0010	/* keep extensions */
179 
180 
181 /* in dependency.c */
182 
183 extern void AcquireDeletionLock(const ObjectAddress *object, int flags);
184 
185 extern void ReleaseDeletionLock(const ObjectAddress *object);
186 
187 extern void performDeletion(const ObjectAddress *object,
188 				DropBehavior behavior, int flags);
189 
190 extern void performMultipleDeletions(const ObjectAddresses *objects,
191 						 DropBehavior behavior, int flags);
192 
193 extern void recordDependencyOnExpr(const ObjectAddress *depender,
194 					   Node *expr, List *rtable,
195 					   DependencyType behavior);
196 
197 extern void recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
198 								Node *expr, Oid relId,
199 								DependencyType behavior,
200 								DependencyType self_behavior,
201 								bool reverse_self);
202 
203 extern ObjectClass getObjectClass(const ObjectAddress *object);
204 
205 extern ObjectAddresses *new_object_addresses(void);
206 
207 extern void add_exact_object_address(const ObjectAddress *object,
208 						 ObjectAddresses *addrs);
209 
210 extern bool object_address_present(const ObjectAddress *object,
211 					   const ObjectAddresses *addrs);
212 
213 extern void record_object_address_dependencies(const ObjectAddress *depender,
214 								   ObjectAddresses *referenced,
215 								   DependencyType behavior);
216 
217 extern void free_object_addresses(ObjectAddresses *addrs);
218 
219 /* in pg_depend.c */
220 
221 extern void recordDependencyOn(const ObjectAddress *depender,
222 				   const ObjectAddress *referenced,
223 				   DependencyType behavior);
224 
225 extern void recordMultipleDependencies(const ObjectAddress *depender,
226 						   const ObjectAddress *referenced,
227 						   int nreferenced,
228 						   DependencyType behavior);
229 
230 extern void recordDependencyOnCurrentExtension(const ObjectAddress *object,
231 								   bool isReplace);
232 
233 extern long deleteDependencyRecordsFor(Oid classId, Oid objectId,
234 						   bool skipExtensionDeps);
235 
236 extern long deleteDependencyRecordsForClass(Oid classId, Oid objectId,
237 								Oid refclassId, char deptype);
238 
239 extern long changeDependencyFor(Oid classId, Oid objectId,
240 					Oid refClassId, Oid oldRefObjectId,
241 					Oid newRefObjectId);
242 
243 extern Oid	getExtensionOfObject(Oid classId, Oid objectId);
244 extern List *getAutoExtensionsOfObject(Oid classId, Oid objectId);
245 
246 extern bool sequenceIsOwned(Oid seqId, char deptype, Oid *tableId, int32 *colId);
247 extern List *getOwnedSequences(Oid relid, AttrNumber attnum);
248 extern Oid	getOwnedSequence(Oid relid, AttrNumber attnum);
249 
250 extern Oid	get_constraint_index(Oid constraintId);
251 
252 extern Oid	get_index_constraint(Oid indexId);
253 
254 /* in pg_shdepend.c */
255 
256 extern void recordSharedDependencyOn(ObjectAddress *depender,
257 						 ObjectAddress *referenced,
258 						 SharedDependencyType deptype);
259 
260 extern void deleteSharedDependencyRecordsFor(Oid classId, Oid objectId,
261 								 int32 objectSubId);
262 
263 extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
264 
265 extern void changeDependencyOnOwner(Oid classId, Oid objectId,
266 						Oid newOwnerId);
267 
268 extern void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId,
269 					  Oid ownerId,
270 					  int noldmembers, Oid *oldmembers,
271 					  int nnewmembers, Oid *newmembers);
272 
273 extern bool checkSharedDependencies(Oid classId, Oid objectId,
274 						char **detail_msg, char **detail_log_msg);
275 
276 extern void shdepLockAndCheckObject(Oid classId, Oid objectId);
277 
278 extern void copyTemplateDependencies(Oid templateDbId, Oid newDbId);
279 
280 extern void dropDatabaseDependencies(Oid databaseId);
281 
282 extern void shdepDropOwned(List *relids, DropBehavior behavior);
283 
284 extern void shdepReassignOwned(List *relids, Oid newrole);
285 
286 #endif							/* DEPENDENCY_H */
287