Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_shdepend.c
4 : : * routines to support manipulation of the pg_shdepend relation
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/catalog/pg_shdepend.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include "access/genam.h"
18 : : #include "access/htup_details.h"
19 : : #include "access/table.h"
20 : : #include "access/xact.h"
21 : : #include "catalog/catalog.h"
22 : : #include "catalog/dependency.h"
23 : : #include "catalog/indexing.h"
24 : : #include "catalog/pg_authid.h"
25 : : #include "catalog/pg_auth_members.h"
26 : : #include "catalog/pg_collation.h"
27 : : #include "catalog/pg_conversion.h"
28 : : #include "catalog/pg_database.h"
29 : : #include "catalog/pg_default_acl.h"
30 : : #include "catalog/pg_event_trigger.h"
31 : : #include "catalog/pg_extension.h"
32 : : #include "catalog/pg_foreign_data_wrapper.h"
33 : : #include "catalog/pg_foreign_server.h"
34 : : #include "catalog/pg_language.h"
35 : : #include "catalog/pg_largeobject.h"
36 : : #include "catalog/pg_namespace.h"
37 : : #include "catalog/pg_opclass.h"
38 : : #include "catalog/pg_operator.h"
39 : : #include "catalog/pg_opfamily.h"
40 : : #include "catalog/pg_proc.h"
41 : : #include "catalog/pg_shdepend.h"
42 : : #include "catalog/pg_statistic_ext.h"
43 : : #include "catalog/pg_subscription.h"
44 : : #include "catalog/pg_tablespace.h"
45 : : #include "catalog/pg_ts_config.h"
46 : : #include "catalog/pg_ts_dict.h"
47 : : #include "catalog/pg_type.h"
48 : : #include "catalog/pg_user_mapping.h"
49 : : #include "commands/alter.h"
50 : : #include "commands/defrem.h"
51 : : #include "commands/event_trigger.h"
52 : : #include "commands/policy.h"
53 : : #include "commands/publicationcmds.h"
54 : : #include "commands/schemacmds.h"
55 : : #include "commands/subscriptioncmds.h"
56 : : #include "commands/tablecmds.h"
57 : : #include "commands/tablespace.h"
58 : : #include "commands/typecmds.h"
59 : : #include "miscadmin.h"
60 : : #include "storage/lmgr.h"
61 : : #include "utils/acl.h"
62 : : #include "utils/fmgroids.h"
63 : : #include "utils/lsyscache.h"
64 : : #include "utils/memutils.h"
65 : : #include "utils/syscache.h"
66 : :
67 : : typedef enum
68 : : {
69 : : LOCAL_OBJECT,
70 : : SHARED_OBJECT,
71 : : REMOTE_OBJECT,
72 : : } SharedDependencyObjectType;
73 : :
74 : : typedef struct
75 : : {
76 : : ObjectAddress object;
77 : : char deptype;
78 : : SharedDependencyObjectType objtype;
79 : : } ShDependObjectInfo;
80 : :
81 : : static void getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2);
82 : : static Oid classIdGetDbId(Oid classId);
83 : : static void shdepChangeDep(Relation sdepRel,
84 : : Oid classid, Oid objid, int32 objsubid,
85 : : Oid refclassid, Oid refobjid,
86 : : SharedDependencyType deptype);
87 : : static void updateAclDependenciesWorker(Oid classId, Oid objectId,
88 : : int32 objsubId, Oid ownerId,
89 : : SharedDependencyType deptype,
90 : : int noldmembers, Oid *oldmembers,
91 : : int nnewmembers, Oid *newmembers);
92 : : static void shdepAddDependency(Relation sdepRel,
93 : : Oid classId, Oid objectId, int32 objsubId,
94 : : Oid refclassId, Oid refobjId,
95 : : SharedDependencyType deptype);
96 : : static void shdepDropDependency(Relation sdepRel,
97 : : Oid classId, Oid objectId, int32 objsubId,
98 : : bool drop_subobjects,
99 : : Oid refclassId, Oid refobjId,
100 : : SharedDependencyType deptype);
101 : : static void storeObjectDescription(StringInfo descs,
102 : : SharedDependencyObjectType type,
103 : : ObjectAddress *object,
104 : : SharedDependencyType deptype,
105 : : int count);
106 : : static void shdepReassignOwned_Owner(Form_pg_shdepend sdepForm, Oid newrole);
107 : : static void shdepReassignOwned_InitAcl(Form_pg_shdepend sdepForm,
108 : : Oid oldrole, Oid newrole);
109 : :
110 : :
111 : : /*
112 : : * recordSharedDependencyOn
113 : : *
114 : : * Record a dependency between 2 objects via their respective ObjectAddresses.
115 : : * The first argument is the dependent object, the second the one it
116 : : * references (which must be a shared object).
117 : : *
118 : : * This locks the referenced object and makes sure it still exists.
119 : : * Then it creates an entry in pg_shdepend. The lock is kept until
120 : : * the end of the transaction.
121 : : *
122 : : * Dependencies on pinned objects are not recorded.
123 : : */
124 : : void
125 : 118312 : recordSharedDependencyOn(ObjectAddress *depender,
126 : : ObjectAddress *referenced,
127 : : SharedDependencyType deptype)
128 : : {
129 : : Relation sdepRel;
130 : :
131 : : /*
132 : : * Objects in pg_shdepend can't have SubIds.
133 : : */
134 : : Assert(depender->objectSubId == 0);
135 : : Assert(referenced->objectSubId == 0);
136 : :
137 : : /*
138 : : * During bootstrap, do nothing since pg_shdepend may not exist yet.
139 : : * initdb will fill in appropriate pg_shdepend entries after bootstrap.
140 : : */
141 [ - + ]: 118312 : if (IsBootstrapProcessingMode())
142 : 0 : return;
143 : :
144 : 118312 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
145 : :
146 : : /* If the referenced object is pinned, do nothing. */
147 [ + + ]: 118312 : if (!IsPinnedObject(referenced->classId, referenced->objectId))
148 : : {
149 : 3307 : shdepAddDependency(sdepRel, depender->classId, depender->objectId,
150 : : depender->objectSubId,
151 : : referenced->classId, referenced->objectId,
152 : : deptype);
153 : : }
154 : :
155 : 118311 : table_close(sdepRel, RowExclusiveLock);
156 : : }
157 : :
158 : : /*
159 : : * recordDependencyOnOwner
160 : : *
161 : : * A convenient wrapper of recordSharedDependencyOn -- register the specified
162 : : * user as owner of the given object.
163 : : *
164 : : * Note: it's the caller's responsibility to ensure that there isn't an owner
165 : : * entry for the object already.
166 : : */
167 : : void
168 : 118083 : recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
169 : : {
170 : : ObjectAddress myself,
171 : : referenced;
172 : :
173 : 118083 : myself.classId = classId;
174 : 118083 : myself.objectId = objectId;
175 : 118083 : myself.objectSubId = 0;
176 : :
177 : 118083 : referenced.classId = AuthIdRelationId;
178 : 118083 : referenced.objectId = owner;
179 : 118083 : referenced.objectSubId = 0;
180 : :
181 : 118083 : recordSharedDependencyOn(&myself, &referenced, SHARED_DEPENDENCY_OWNER);
182 : 118083 : }
183 : :
184 : : /*
185 : : * shdepChangeDep
186 : : *
187 : : * Update shared dependency records to account for an updated referenced
188 : : * object. This is an internal workhorse for operations such as changing
189 : : * an object's owner.
190 : : *
191 : : * There must be no more than one existing entry for the given dependent
192 : : * object and dependency type! So in practice this can only be used for
193 : : * updating SHARED_DEPENDENCY_OWNER and SHARED_DEPENDENCY_TABLESPACE
194 : : * entries, which should have that property.
195 : : *
196 : : * If there is no previous entry, we assume it was referencing a PINned
197 : : * object, so we create a new entry. If the new referenced object is
198 : : * PINned, we don't create an entry (and drop the old one, if any).
199 : : * (For tablespaces, we don't record dependencies in certain cases, so
200 : : * there are other possible reasons for entries to be missing.)
201 : : *
202 : : * sdepRel must be the pg_shdepend relation, already opened and suitably
203 : : * locked.
204 : : */
205 : : static void
206 : 525 : shdepChangeDep(Relation sdepRel,
207 : : Oid classid, Oid objid, int32 objsubid,
208 : : Oid refclassid, Oid refobjid,
209 : : SharedDependencyType deptype)
210 : : {
211 : 525 : Oid dbid = classIdGetDbId(classid);
212 : 525 : HeapTuple oldtup = NULL;
213 : : HeapTuple scantup;
214 : : ScanKeyData key[4];
215 : : SysScanDesc scan;
216 : :
217 : : /*
218 : : * Make sure the new referenced object doesn't go away while we record the
219 : : * dependency.
220 : : */
221 : 525 : shdepLockAndCheckObject(refclassid, refobjid);
222 : :
223 : : /*
224 : : * Look for a previous entry
225 : : */
226 : 525 : ScanKeyInit(&key[0],
227 : : Anum_pg_shdepend_dbid,
228 : : BTEqualStrategyNumber, F_OIDEQ,
229 : : ObjectIdGetDatum(dbid));
230 : 525 : ScanKeyInit(&key[1],
231 : : Anum_pg_shdepend_classid,
232 : : BTEqualStrategyNumber, F_OIDEQ,
233 : : ObjectIdGetDatum(classid));
234 : 525 : ScanKeyInit(&key[2],
235 : : Anum_pg_shdepend_objid,
236 : : BTEqualStrategyNumber, F_OIDEQ,
237 : : ObjectIdGetDatum(objid));
238 : 525 : ScanKeyInit(&key[3],
239 : : Anum_pg_shdepend_objsubid,
240 : : BTEqualStrategyNumber, F_INT4EQ,
241 : : Int32GetDatum(objsubid));
242 : :
243 : 525 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
244 : : NULL, 4, key);
245 : :
246 [ + + ]: 825 : while ((scantup = systable_getnext(scan)) != NULL)
247 : : {
248 : : /* Ignore if not of the target dependency type */
249 [ + + ]: 300 : if (((Form_pg_shdepend) GETSTRUCT(scantup))->deptype != deptype)
250 : 47 : continue;
251 : : /* Caller screwed up if multiple matches */
252 [ - + ]: 253 : if (oldtup)
253 [ # # ]: 0 : elog(ERROR,
254 : : "multiple pg_shdepend entries for object %u/%u/%d deptype %c",
255 : : classid, objid, objsubid, deptype);
256 : 253 : oldtup = heap_copytuple(scantup);
257 : : }
258 : :
259 : 525 : systable_endscan(scan);
260 : :
261 [ + + ]: 525 : if (IsPinnedObject(refclassid, refobjid))
262 : : {
263 : : /* No new entry needed, so just delete existing entry if any */
264 [ + + ]: 39 : if (oldtup)
265 : 30 : CatalogTupleDelete(sdepRel, &oldtup->t_self);
266 : : }
267 [ + + ]: 486 : else if (oldtup)
268 : : {
269 : : /* Need to update existing entry */
270 : 223 : Form_pg_shdepend shForm = (Form_pg_shdepend) GETSTRUCT(oldtup);
271 : :
272 : : /* Since oldtup is a copy, we can just modify it in-memory */
273 : 223 : shForm->refclassid = refclassid;
274 : 223 : shForm->refobjid = refobjid;
275 : :
276 : 223 : CatalogTupleUpdate(sdepRel, &oldtup->t_self, oldtup);
277 : : }
278 : : else
279 : : {
280 : : /* Need to insert new entry */
281 : : Datum values[Natts_pg_shdepend];
282 : : bool nulls[Natts_pg_shdepend];
283 : :
284 : 263 : memset(nulls, false, sizeof(nulls));
285 : :
286 : 263 : values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(dbid);
287 : 263 : values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classid);
288 : 263 : values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objid);
289 : 263 : values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(objsubid);
290 : :
291 : 263 : values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassid);
292 : 263 : values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjid);
293 : 263 : values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);
294 : :
295 : : /*
296 : : * we are reusing oldtup just to avoid declaring a new variable, but
297 : : * it's certainly a new tuple
298 : : */
299 : 263 : oldtup = heap_form_tuple(RelationGetDescr(sdepRel), values, nulls);
300 : 263 : CatalogTupleInsert(sdepRel, oldtup);
301 : : }
302 : :
303 [ + + ]: 525 : if (oldtup)
304 : 516 : heap_freetuple(oldtup);
305 : 525 : }
306 : :
307 : : /*
308 : : * changeDependencyOnOwner
309 : : *
310 : : * Update the shared dependencies to account for the new owner.
311 : : *
312 : : * Note: we don't need an objsubid argument because only whole objects
313 : : * have owners.
314 : : */
315 : : void
316 : 517 : changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
317 : : {
318 : : Relation sdepRel;
319 : :
320 : 517 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
321 : :
322 : : /* Adjust the SHARED_DEPENDENCY_OWNER entry */
323 : 517 : shdepChangeDep(sdepRel,
324 : : classId, objectId, 0,
325 : : AuthIdRelationId, newOwnerId,
326 : : SHARED_DEPENDENCY_OWNER);
327 : :
328 : : /*----------
329 : : * There should never be a SHARED_DEPENDENCY_ACL entry for the owner,
330 : : * so get rid of it if there is one. This can happen if the new owner
331 : : * was previously granted some rights to the object.
332 : : *
333 : : * This step is analogous to aclnewowner's removal of duplicate entries
334 : : * in the ACL. We have to do it to handle this scenario:
335 : : * A grants some rights on an object to B
336 : : * ALTER OWNER changes the object's owner to B
337 : : * ALTER OWNER changes the object's owner to C
338 : : * The third step would remove all mention of B from the object's ACL,
339 : : * but we'd still have a SHARED_DEPENDENCY_ACL for B if we did not do
340 : : * things this way.
341 : : *
342 : : * The rule against having a SHARED_DEPENDENCY_ACL entry for the owner
343 : : * allows us to fix things up in just this one place, without having
344 : : * to make the various ALTER OWNER routines each know about it.
345 : : *----------
346 : : */
347 : 517 : shdepDropDependency(sdepRel, classId, objectId, 0, true,
348 : : AuthIdRelationId, newOwnerId,
349 : : SHARED_DEPENDENCY_ACL);
350 : :
351 : : /*
352 : : * However, nothing need be done about SHARED_DEPENDENCY_INITACL entries,
353 : : * since those exist whether or not the role is the object's owner, and
354 : : * ALTER OWNER does not modify the underlying pg_init_privs entry.
355 : : */
356 : :
357 : 517 : table_close(sdepRel, RowExclusiveLock);
358 : 517 : }
359 : :
360 : : /*
361 : : * recordDependencyOnTablespace
362 : : *
363 : : * A convenient wrapper of recordSharedDependencyOn -- register the specified
364 : : * tablespace as default for the given object.
365 : : *
366 : : * Note: it's the caller's responsibility to ensure that there isn't a
367 : : * tablespace entry for the object already.
368 : : */
369 : : void
370 : 76 : recordDependencyOnTablespace(Oid classId, Oid objectId, Oid tablespace)
371 : : {
372 : : ObjectAddress myself,
373 : : referenced;
374 : :
375 : 76 : ObjectAddressSet(myself, classId, objectId);
376 : 76 : ObjectAddressSet(referenced, TableSpaceRelationId, tablespace);
377 : :
378 : 76 : recordSharedDependencyOn(&myself, &referenced,
379 : : SHARED_DEPENDENCY_TABLESPACE);
380 : 75 : }
381 : :
382 : : /*
383 : : * changeDependencyOnTablespace
384 : : *
385 : : * Update the shared dependencies to account for the new tablespace.
386 : : *
387 : : * Note: we don't need an objsubid argument because only whole objects
388 : : * have tablespaces.
389 : : */
390 : : void
391 : 20 : changeDependencyOnTablespace(Oid classId, Oid objectId, Oid newTablespaceId)
392 : : {
393 : : Relation sdepRel;
394 : :
395 : 20 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
396 : :
397 [ + - + + ]: 20 : if (newTablespaceId != DEFAULTTABLESPACE_OID &&
398 : : newTablespaceId != InvalidOid)
399 : 8 : shdepChangeDep(sdepRel,
400 : : classId, objectId, 0,
401 : : TableSpaceRelationId, newTablespaceId,
402 : : SHARED_DEPENDENCY_TABLESPACE);
403 : : else
404 : 12 : shdepDropDependency(sdepRel,
405 : : classId, objectId, 0, true,
406 : : InvalidOid, InvalidOid,
407 : : SHARED_DEPENDENCY_INVALID);
408 : :
409 : 20 : table_close(sdepRel, RowExclusiveLock);
410 : 20 : }
411 : :
412 : : /*
413 : : * getOidListDiff
414 : : * Helper for updateAclDependencies.
415 : : *
416 : : * Takes two Oid arrays and removes elements that are common to both arrays,
417 : : * leaving just those that are in one input but not the other.
418 : : * We assume both arrays have been sorted and de-duped.
419 : : */
420 : : static void
421 : 15124 : getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2)
422 : : {
423 : : int in1,
424 : : in2,
425 : : out1,
426 : : out2;
427 : :
428 : 15124 : in1 = in2 = out1 = out2 = 0;
429 [ + + + + ]: 22333 : while (in1 < *nlist1 && in2 < *nlist2)
430 : : {
431 [ + + ]: 7209 : if (list1[in1] == list2[in2])
432 : : {
433 : : /* skip over duplicates */
434 : 7104 : in1++;
435 : 7104 : in2++;
436 : : }
437 [ + + ]: 105 : else if (list1[in1] < list2[in2])
438 : : {
439 : : /* list1[in1] is not in list2 */
440 : 67 : list1[out1++] = list1[in1++];
441 : : }
442 : : else
443 : : {
444 : : /* list2[in2] is not in list1 */
445 : 38 : list2[out2++] = list2[in2++];
446 : : }
447 : : }
448 : :
449 : : /* any remaining list1 entries are not in list2 */
450 [ + + ]: 15707 : while (in1 < *nlist1)
451 : : {
452 : 583 : list1[out1++] = list1[in1++];
453 : : }
454 : :
455 : : /* any remaining list2 entries are not in list1 */
456 [ + + ]: 25650 : while (in2 < *nlist2)
457 : : {
458 : 10526 : list2[out2++] = list2[in2++];
459 : : }
460 : :
461 : 15124 : *nlist1 = out1;
462 : 15124 : *nlist2 = out2;
463 : 15124 : }
464 : :
465 : : /*
466 : : * updateAclDependencies
467 : : * Update the pg_shdepend info for an object's ACL during GRANT/REVOKE.
468 : : *
469 : : * classId, objectId, objsubId: identify the object whose ACL this is
470 : : * ownerId: role owning the object
471 : : * noldmembers, oldmembers: array of roleids appearing in old ACL
472 : : * nnewmembers, newmembers: array of roleids appearing in new ACL
473 : : *
474 : : * We calculate the differences between the new and old lists of roles,
475 : : * and then insert or delete from pg_shdepend as appropriate.
476 : : *
477 : : * Note that we can't just insert all referenced roles blindly during GRANT,
478 : : * because we would end up with duplicate registered dependencies. We could
479 : : * check for existence of the tuples before inserting, but that seems to be
480 : : * more expensive than what we are doing here. Likewise we can't just delete
481 : : * blindly during REVOKE, because the user may still have other privileges.
482 : : * It is also possible that REVOKE actually adds dependencies, due to
483 : : * instantiation of a formerly implicit default ACL (although at present,
484 : : * all such dependencies should be for the owning role, which we ignore here).
485 : : *
486 : : * NOTE: Both input arrays must be sorted and de-duped. (Typically they
487 : : * are extracted from an ACL array by aclmembers(), which takes care of
488 : : * both requirements.) The arrays are pfreed before return.
489 : : */
490 : : void
491 : 14589 : updateAclDependencies(Oid classId, Oid objectId, int32 objsubId,
492 : : Oid ownerId,
493 : : int noldmembers, Oid *oldmembers,
494 : : int nnewmembers, Oid *newmembers)
495 : : {
496 : 14589 : updateAclDependenciesWorker(classId, objectId, objsubId,
497 : : ownerId, SHARED_DEPENDENCY_ACL,
498 : : noldmembers, oldmembers,
499 : : nnewmembers, newmembers);
500 : 14589 : }
501 : :
502 : : /*
503 : : * updateInitAclDependencies
504 : : * Update the pg_shdepend info for a pg_init_privs entry.
505 : : *
506 : : * Exactly like updateAclDependencies, except we are considering a
507 : : * pg_init_privs ACL for the specified object. Since recording of
508 : : * pg_init_privs role dependencies is the same for owners and non-owners,
509 : : * we do not need an ownerId argument.
510 : : */
511 : : void
512 : 535 : updateInitAclDependencies(Oid classId, Oid objectId, int32 objsubId,
513 : : int noldmembers, Oid *oldmembers,
514 : : int nnewmembers, Oid *newmembers)
515 : : {
516 : 535 : updateAclDependenciesWorker(classId, objectId, objsubId,
517 : : InvalidOid, /* ownerId will not be consulted */
518 : : SHARED_DEPENDENCY_INITACL,
519 : : noldmembers, oldmembers,
520 : : nnewmembers, newmembers);
521 : 535 : }
522 : :
523 : : /* Common code for the above two functions */
524 : : static void
525 : 15124 : updateAclDependenciesWorker(Oid classId, Oid objectId, int32 objsubId,
526 : : Oid ownerId, SharedDependencyType deptype,
527 : : int noldmembers, Oid *oldmembers,
528 : : int nnewmembers, Oid *newmembers)
529 : : {
530 : : Relation sdepRel;
531 : : int i;
532 : :
533 : : /*
534 : : * Remove entries that are common to both lists; those represent existing
535 : : * dependencies we don't need to change.
536 : : *
537 : : * OK to overwrite the inputs since we'll pfree them anyway.
538 : : */
539 : 15124 : getOidListDiff(oldmembers, &noldmembers, newmembers, &nnewmembers);
540 : :
541 [ + + + + ]: 15124 : if (noldmembers > 0 || nnewmembers > 0)
542 : : {
543 : 10052 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
544 : :
545 : : /* Add new dependencies that weren't already present */
546 [ + + ]: 20616 : for (i = 0; i < nnewmembers; i++)
547 : : {
548 : 10564 : Oid roleid = newmembers[i];
549 : :
550 : : /*
551 : : * For SHARED_DEPENDENCY_ACL entries, skip the owner: she has an
552 : : * OWNER shdep entry instead. (This is not just a space
553 : : * optimization; it makes ALTER OWNER easier. See notes in
554 : : * changeDependencyOnOwner.) But for INITACL entries, we record
555 : : * the owner too.
556 : : */
557 [ + + + + ]: 10564 : if (deptype == SHARED_DEPENDENCY_ACL && roleid == ownerId)
558 : 7873 : continue;
559 : :
560 : : /* Skip pinned roles; they don't need dependency entries */
561 [ + + ]: 2691 : if (IsPinnedObject(AuthIdRelationId, roleid))
562 : 1116 : continue;
563 : :
564 : 1575 : shdepAddDependency(sdepRel, classId, objectId, objsubId,
565 : : AuthIdRelationId, roleid,
566 : : deptype);
567 : : }
568 : :
569 : : /* Drop no-longer-used old dependencies */
570 [ + + ]: 10702 : for (i = 0; i < noldmembers; i++)
571 : : {
572 : 650 : Oid roleid = oldmembers[i];
573 : :
574 : : /* Skip the owner for ACL entries, same as above */
575 [ + + + + ]: 650 : if (deptype == SHARED_DEPENDENCY_ACL && roleid == ownerId)
576 : 73 : continue;
577 : :
578 : : /* Skip pinned roles */
579 [ + + ]: 577 : if (IsPinnedObject(AuthIdRelationId, roleid))
580 : 61 : continue;
581 : :
582 : 516 : shdepDropDependency(sdepRel, classId, objectId, objsubId,
583 : : false, /* exact match on objsubId */
584 : : AuthIdRelationId, roleid,
585 : : deptype);
586 : : }
587 : :
588 : 10052 : table_close(sdepRel, RowExclusiveLock);
589 : : }
590 : :
591 [ + + ]: 15124 : if (oldmembers)
592 : 6397 : pfree(oldmembers);
593 [ + + ]: 15124 : if (newmembers)
594 : 14945 : pfree(newmembers);
595 : 15124 : }
596 : :
597 : : /*
598 : : * A struct to keep track of dependencies found in other databases.
599 : : */
600 : : typedef struct
601 : : {
602 : : Oid dbOid;
603 : : int count;
604 : : } remoteDep;
605 : :
606 : : /*
607 : : * qsort comparator for ShDependObjectInfo items
608 : : */
609 : : static int
610 : 105 : shared_dependency_comparator(const void *a, const void *b)
611 : : {
612 : 105 : const ShDependObjectInfo *obja = (const ShDependObjectInfo *) a;
613 : 105 : const ShDependObjectInfo *objb = (const ShDependObjectInfo *) b;
614 : :
615 : : /*
616 : : * Primary sort key is OID ascending.
617 : : */
618 [ + + ]: 105 : if (obja->object.objectId < objb->object.objectId)
619 : 81 : return -1;
620 [ + - ]: 24 : if (obja->object.objectId > objb->object.objectId)
621 : 24 : return 1;
622 : :
623 : : /*
624 : : * Next sort on catalog ID, in case identical OIDs appear in different
625 : : * catalogs. Sort direction is pretty arbitrary here.
626 : : */
627 [ # # ]: 0 : if (obja->object.classId < objb->object.classId)
628 : 0 : return -1;
629 [ # # ]: 0 : if (obja->object.classId > objb->object.classId)
630 : 0 : return 1;
631 : :
632 : : /*
633 : : * Sort on object subId.
634 : : *
635 : : * We sort the subId as an unsigned int so that 0 (the whole object) will
636 : : * come first.
637 : : */
638 [ # # ]: 0 : if ((unsigned int) obja->object.objectSubId < (unsigned int) objb->object.objectSubId)
639 : 0 : return -1;
640 [ # # ]: 0 : if ((unsigned int) obja->object.objectSubId > (unsigned int) objb->object.objectSubId)
641 : 0 : return 1;
642 : :
643 : : /*
644 : : * Last, sort on deptype, in case the same object has multiple dependency
645 : : * types. (Note that there's no need to consider objtype, as that's
646 : : * determined by the catalog OID.)
647 : : */
648 [ # # ]: 0 : if (obja->deptype < objb->deptype)
649 : 0 : return -1;
650 [ # # ]: 0 : if (obja->deptype > objb->deptype)
651 : 0 : return 1;
652 : :
653 : 0 : return 0;
654 : : }
655 : :
656 : : /*
657 : : * checkSharedDependencies
658 : : *
659 : : * Check whether there are shared dependency entries for a given shared
660 : : * object; return true if so.
661 : : *
662 : : * In addition, return a string containing a newline-separated list of object
663 : : * descriptions that depend on the shared object, or NULL if none is found.
664 : : * We actually return two such strings; the "detail" result is suitable for
665 : : * returning to the client as an errdetail() string, and is limited in size.
666 : : * The "detail_log" string is potentially much longer, and should be emitted
667 : : * to the server log only.
668 : : *
669 : : * We can find three different kinds of dependencies: dependencies on objects
670 : : * of the current database; dependencies on shared objects; and dependencies
671 : : * on objects local to other databases. We can (and do) provide descriptions
672 : : * of the two former kinds of objects, but we can't do that for "remote"
673 : : * objects, so we just provide a count of them.
674 : : */
675 : : bool
676 : 1121 : checkSharedDependencies(Oid classId, Oid objectId,
677 : : char **detail_msg, char **detail_log_msg)
678 : : {
679 : : Relation sdepRel;
680 : : ScanKeyData key[2];
681 : : SysScanDesc scan;
682 : : HeapTuple tup;
683 : 1121 : int numReportedDeps = 0;
684 : 1121 : int numNotReportedDeps = 0;
685 : 1121 : int numNotReportedDbs = 0;
686 : 1121 : List *remDeps = NIL;
687 : : ListCell *cell;
688 : : ObjectAddress object;
689 : : ShDependObjectInfo *objects;
690 : : int numobjects;
691 : : int allocedobjects;
692 : : StringInfoData descs;
693 : : StringInfoData alldescs;
694 : :
695 : : /* This case can be dispatched quickly */
696 [ - + ]: 1121 : if (IsPinnedObject(classId, objectId))
697 : : {
698 : 0 : object.classId = classId;
699 : 0 : object.objectId = objectId;
700 : 0 : object.objectSubId = 0;
701 [ # # ]: 0 : ereport(ERROR,
702 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
703 : : errmsg("cannot drop %s because it is required by the database system",
704 : : getObjectDescription(&object, false))));
705 : : }
706 : :
707 : : /*
708 : : * We limit the number of dependencies reported to the client to
709 : : * MAX_REPORTED_DEPS, since client software may not deal well with
710 : : * enormous error strings. The server log always gets a full report.
711 : : *
712 : : * For stability of regression test results, we sort local and shared
713 : : * objects by OID before reporting them. We don't worry about the order
714 : : * in which other databases are reported, though.
715 : : */
716 : : #define MAX_REPORTED_DEPS 100
717 : :
718 : 1121 : allocedobjects = 128; /* arbitrary initial array size */
719 : 1121 : objects = palloc_array(ShDependObjectInfo, allocedobjects);
720 : 1121 : numobjects = 0;
721 : 1121 : initStringInfo(&descs);
722 : 1121 : initStringInfo(&alldescs);
723 : :
724 : 1121 : sdepRel = table_open(SharedDependRelationId, AccessShareLock);
725 : :
726 : 1121 : ScanKeyInit(&key[0],
727 : : Anum_pg_shdepend_refclassid,
728 : : BTEqualStrategyNumber, F_OIDEQ,
729 : : ObjectIdGetDatum(classId));
730 : 1121 : ScanKeyInit(&key[1],
731 : : Anum_pg_shdepend_refobjid,
732 : : BTEqualStrategyNumber, F_OIDEQ,
733 : : ObjectIdGetDatum(objectId));
734 : :
735 : 1121 : scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
736 : : NULL, 2, key);
737 : :
738 [ + + ]: 1309 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
739 : : {
740 : 188 : Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
741 : :
742 : 188 : object.classId = sdepForm->classid;
743 : 188 : object.objectId = sdepForm->objid;
744 : 188 : object.objectSubId = sdepForm->objsubid;
745 : :
746 : : /*
747 : : * If it's a dependency local to this database or it's a shared
748 : : * object, add it to the objects array.
749 : : *
750 : : * If it's a remote dependency, keep track of it so we can report the
751 : : * number of them later.
752 : : */
753 [ + + ]: 188 : if (sdepForm->dbid == MyDatabaseId ||
754 [ + - ]: 50 : sdepForm->dbid == InvalidOid)
755 : : {
756 [ - + ]: 188 : if (numobjects >= allocedobjects)
757 : : {
758 : 0 : allocedobjects *= 2;
759 : 0 : objects = repalloc_array(objects, ShDependObjectInfo, allocedobjects);
760 : : }
761 : 188 : objects[numobjects].object = object;
762 : 188 : objects[numobjects].deptype = sdepForm->deptype;
763 : 188 : objects[numobjects].objtype = (sdepForm->dbid == MyDatabaseId) ?
764 : 188 : LOCAL_OBJECT : SHARED_OBJECT;
765 : 188 : numobjects++;
766 : : }
767 : : else
768 : : {
769 : : /* It's not local nor shared, so it must be remote. */
770 : : remoteDep *dep;
771 : 0 : bool stored = false;
772 : :
773 : : /*
774 : : * XXX this info is kept on a simple List. Maybe it's not good
775 : : * for performance, but using a hash table seems needlessly
776 : : * complex. The expected number of databases is not high anyway,
777 : : * I suppose.
778 : : */
779 [ # # # # : 0 : foreach(cell, remDeps)
# # ]
780 : : {
781 : 0 : dep = lfirst(cell);
782 [ # # ]: 0 : if (dep->dbOid == sdepForm->dbid)
783 : : {
784 : 0 : dep->count++;
785 : 0 : stored = true;
786 : 0 : break;
787 : : }
788 : : }
789 [ # # ]: 0 : if (!stored)
790 : : {
791 : 0 : dep = palloc_object(remoteDep);
792 : 0 : dep->dbOid = sdepForm->dbid;
793 : 0 : dep->count = 1;
794 : 0 : remDeps = lappend(remDeps, dep);
795 : : }
796 : : }
797 : : }
798 : :
799 : 1121 : systable_endscan(scan);
800 : :
801 : 1121 : table_close(sdepRel, AccessShareLock);
802 : :
803 : : /*
804 : : * Sort and report local and shared objects.
805 : : */
806 [ + + ]: 1121 : if (numobjects > 1)
807 : 37 : qsort(objects, numobjects,
808 : : sizeof(ShDependObjectInfo), shared_dependency_comparator);
809 : :
810 [ + + ]: 1309 : for (int i = 0; i < numobjects; i++)
811 : : {
812 [ + - ]: 188 : if (numReportedDeps < MAX_REPORTED_DEPS)
813 : : {
814 : 188 : numReportedDeps++;
815 : 188 : storeObjectDescription(&descs,
816 : 188 : objects[i].objtype,
817 : 188 : &objects[i].object,
818 : 188 : objects[i].deptype,
819 : : 0);
820 : : }
821 : : else
822 : 0 : numNotReportedDeps++;
823 : 188 : storeObjectDescription(&alldescs,
824 : 188 : objects[i].objtype,
825 : 188 : &objects[i].object,
826 : 188 : objects[i].deptype,
827 : : 0);
828 : : }
829 : :
830 : : /*
831 : : * Summarize dependencies in remote databases.
832 : : */
833 [ - + - - : 1121 : foreach(cell, remDeps)
- + ]
834 : : {
835 : 0 : remoteDep *dep = lfirst(cell);
836 : :
837 : 0 : object.classId = DatabaseRelationId;
838 : 0 : object.objectId = dep->dbOid;
839 : 0 : object.objectSubId = 0;
840 : :
841 [ # # ]: 0 : if (numReportedDeps < MAX_REPORTED_DEPS)
842 : : {
843 : 0 : numReportedDeps++;
844 : 0 : storeObjectDescription(&descs, REMOTE_OBJECT, &object,
845 : : SHARED_DEPENDENCY_INVALID, dep->count);
846 : : }
847 : : else
848 : 0 : numNotReportedDbs++;
849 : 0 : storeObjectDescription(&alldescs, REMOTE_OBJECT, &object,
850 : : SHARED_DEPENDENCY_INVALID, dep->count);
851 : : }
852 : :
853 : 1121 : pfree(objects);
854 : 1121 : list_free_deep(remDeps);
855 : :
856 [ + + ]: 1121 : if (descs.len == 0)
857 : : {
858 : 1030 : pfree(descs.data);
859 : 1030 : pfree(alldescs.data);
860 : 1030 : *detail_msg = *detail_log_msg = NULL;
861 : 1030 : return false;
862 : : }
863 : :
864 [ - + ]: 91 : if (numNotReportedDeps > 0)
865 : 0 : appendStringInfo(&descs, ngettext("\nand %d other object "
866 : : "(see server log for list)",
867 : : "\nand %d other objects "
868 : : "(see server log for list)",
869 : : numNotReportedDeps),
870 : : numNotReportedDeps);
871 [ - + ]: 91 : if (numNotReportedDbs > 0)
872 : 0 : appendStringInfo(&descs, ngettext("\nand objects in %d other database "
873 : : "(see server log for list)",
874 : : "\nand objects in %d other databases "
875 : : "(see server log for list)",
876 : : numNotReportedDbs),
877 : : numNotReportedDbs);
878 : :
879 : 91 : *detail_msg = descs.data;
880 : 91 : *detail_log_msg = alldescs.data;
881 : 91 : return true;
882 : : }
883 : :
884 : :
885 : : /*
886 : : * copyTemplateDependencies
887 : : *
888 : : * Routine to create the initial shared dependencies of a new database.
889 : : * We simply copy the dependencies from the template database.
890 : : */
891 : : void
892 : 430 : copyTemplateDependencies(Oid templateDbId, Oid newDbId)
893 : : {
894 : : Relation sdepRel;
895 : : TupleDesc sdepDesc;
896 : : ScanKeyData key[1];
897 : : SysScanDesc scan;
898 : : HeapTuple tup;
899 : : CatalogIndexState indstate;
900 : : TupleTableSlot **slot;
901 : : int max_slots,
902 : : slot_init_count,
903 : : slot_stored_count;
904 : :
905 : 430 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
906 : 430 : sdepDesc = RelationGetDescr(sdepRel);
907 : :
908 : : /*
909 : : * Allocate the slots to use, but delay costly initialization until we
910 : : * know that they will be used.
911 : : */
912 : 430 : max_slots = MAX_CATALOG_MULTI_INSERT_BYTES / sizeof(FormData_pg_shdepend);
913 : 430 : slot = palloc_array(TupleTableSlot *, max_slots);
914 : :
915 : 430 : indstate = CatalogOpenIndexes(sdepRel);
916 : :
917 : : /* Scan all entries with dbid = templateDbId */
918 : 430 : ScanKeyInit(&key[0],
919 : : Anum_pg_shdepend_dbid,
920 : : BTEqualStrategyNumber, F_OIDEQ,
921 : : ObjectIdGetDatum(templateDbId));
922 : :
923 : 430 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
924 : : NULL, 1, key);
925 : :
926 : : /* number of slots currently storing tuples */
927 : 430 : slot_stored_count = 0;
928 : : /* number of slots currently initialized */
929 : 430 : slot_init_count = 0;
930 : :
931 : : /*
932 : : * Copy the entries of the original database, changing the database Id to
933 : : * that of the new database. Note that because we are not copying rows
934 : : * with dbId == 0 (ie, rows describing dependent shared objects) we won't
935 : : * copy the ownership dependency of the template database itself; this is
936 : : * what we want.
937 : : */
938 [ + + ]: 442 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
939 : : {
940 : : Form_pg_shdepend shdep;
941 : :
942 [ + - ]: 12 : if (slot_init_count < max_slots)
943 : : {
944 : 12 : slot[slot_stored_count] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple);
945 : 12 : slot_init_count++;
946 : : }
947 : :
948 : 12 : ExecClearTuple(slot[slot_stored_count]);
949 : :
950 : 12 : memset(slot[slot_stored_count]->tts_isnull, false,
951 : 12 : slot[slot_stored_count]->tts_tupleDescriptor->natts * sizeof(bool));
952 : :
953 : 12 : shdep = (Form_pg_shdepend) GETSTRUCT(tup);
954 : :
955 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(newDbId);
956 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(shdep->classid);
957 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(shdep->objid);
958 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(shdep->objsubid);
959 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(shdep->refclassid);
960 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(shdep->refobjid);
961 : 12 : slot[slot_stored_count]->tts_values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(shdep->deptype);
962 : :
963 : 12 : ExecStoreVirtualTuple(slot[slot_stored_count]);
964 : 12 : slot_stored_count++;
965 : :
966 : : /* If slots are full, insert a batch of tuples */
967 [ - + ]: 12 : if (slot_stored_count == max_slots)
968 : : {
969 : 0 : CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
970 : 0 : slot_stored_count = 0;
971 : : }
972 : : }
973 : :
974 : : /* Insert any tuples left in the buffer */
975 [ + + ]: 430 : if (slot_stored_count > 0)
976 : 6 : CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
977 : :
978 : 430 : systable_endscan(scan);
979 : :
980 : 430 : CatalogCloseIndexes(indstate);
981 : 430 : table_close(sdepRel, RowExclusiveLock);
982 : :
983 : : /* Drop only the number of slots used */
984 [ + + ]: 442 : for (int i = 0; i < slot_init_count; i++)
985 : 12 : ExecDropSingleTupleTableSlot(slot[i]);
986 : 430 : pfree(slot);
987 : 430 : }
988 : :
989 : : /*
990 : : * dropDatabaseDependencies
991 : : *
992 : : * Delete pg_shdepend entries corresponding to a database that's being
993 : : * dropped.
994 : : */
995 : : void
996 : 56 : dropDatabaseDependencies(Oid databaseId)
997 : : {
998 : : Relation sdepRel;
999 : : ScanKeyData key[1];
1000 : : SysScanDesc scan;
1001 : : HeapTuple tup;
1002 : :
1003 : 56 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1004 : :
1005 : : /*
1006 : : * First, delete all the entries that have the database Oid in the dbid
1007 : : * field.
1008 : : */
1009 : 56 : ScanKeyInit(&key[0],
1010 : : Anum_pg_shdepend_dbid,
1011 : : BTEqualStrategyNumber, F_OIDEQ,
1012 : : ObjectIdGetDatum(databaseId));
1013 : : /* We leave the other index fields unspecified */
1014 : :
1015 : 56 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
1016 : : NULL, 1, key);
1017 : :
1018 [ + + ]: 57 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
1019 : : {
1020 : 1 : CatalogTupleDelete(sdepRel, &tup->t_self);
1021 : : }
1022 : :
1023 : 56 : systable_endscan(scan);
1024 : :
1025 : : /* Now delete all entries corresponding to the database itself */
1026 : 56 : shdepDropDependency(sdepRel, DatabaseRelationId, databaseId, 0, true,
1027 : : InvalidOid, InvalidOid,
1028 : : SHARED_DEPENDENCY_INVALID);
1029 : :
1030 : 56 : table_close(sdepRel, RowExclusiveLock);
1031 : 56 : }
1032 : :
1033 : : /*
1034 : : * deleteSharedDependencyRecordsFor
1035 : : *
1036 : : * Delete all pg_shdepend entries corresponding to an object that's being
1037 : : * dropped or modified. The object is assumed to be either a shared object
1038 : : * or local to the current database (the classId tells us which).
1039 : : *
1040 : : * If objectSubId is zero, we are deleting a whole object, so get rid of
1041 : : * pg_shdepend entries for subobjects as well.
1042 : : */
1043 : : void
1044 : 153832 : deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
1045 : : {
1046 : : Relation sdepRel;
1047 : :
1048 : 153832 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1049 : :
1050 : 153832 : shdepDropDependency(sdepRel, classId, objectId, objectSubId,
1051 : : (objectSubId == 0),
1052 : : InvalidOid, InvalidOid,
1053 : : SHARED_DEPENDENCY_INVALID);
1054 : :
1055 : 153832 : table_close(sdepRel, RowExclusiveLock);
1056 : 153832 : }
1057 : :
1058 : : /*
1059 : : * shdepAddDependency
1060 : : * Internal workhorse for inserting into pg_shdepend
1061 : : *
1062 : : * sdepRel must be the pg_shdepend relation, already opened and suitably
1063 : : * locked.
1064 : : */
1065 : : static void
1066 : 4882 : shdepAddDependency(Relation sdepRel,
1067 : : Oid classId, Oid objectId, int32 objsubId,
1068 : : Oid refclassId, Oid refobjId,
1069 : : SharedDependencyType deptype)
1070 : : {
1071 : : HeapTuple tup;
1072 : : Datum values[Natts_pg_shdepend];
1073 : : bool nulls[Natts_pg_shdepend];
1074 : :
1075 : : /*
1076 : : * Make sure the object doesn't go away while we record the dependency on
1077 : : * it. DROP routines should lock the object exclusively before they check
1078 : : * shared dependencies.
1079 : : */
1080 : 4882 : shdepLockAndCheckObject(refclassId, refobjId);
1081 : :
1082 : 4881 : memset(nulls, false, sizeof(nulls));
1083 : :
1084 : : /*
1085 : : * Form the new tuple and record the dependency.
1086 : : */
1087 : 4881 : values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(classIdGetDbId(classId));
1088 : 4881 : values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classId);
1089 : 4881 : values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objectId);
1090 : 4881 : values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(objsubId);
1091 : :
1092 : 4881 : values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassId);
1093 : 4881 : values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjId);
1094 : 4881 : values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);
1095 : :
1096 : 4881 : tup = heap_form_tuple(sdepRel->rd_att, values, nulls);
1097 : :
1098 : 4881 : CatalogTupleInsert(sdepRel, tup);
1099 : :
1100 : : /* clean up */
1101 : 4881 : heap_freetuple(tup);
1102 : 4881 : }
1103 : :
1104 : : /*
1105 : : * shdepDropDependency
1106 : : * Internal workhorse for deleting entries from pg_shdepend.
1107 : : *
1108 : : * We drop entries having the following properties:
1109 : : * dependent object is the one identified by classId/objectId/objsubId
1110 : : * if refclassId isn't InvalidOid, it must match the entry's refclassid
1111 : : * if refobjId isn't InvalidOid, it must match the entry's refobjid
1112 : : * if deptype isn't SHARED_DEPENDENCY_INVALID, it must match entry's deptype
1113 : : *
1114 : : * If drop_subobjects is true, we ignore objsubId and consider all entries
1115 : : * matching classId/objectId.
1116 : : *
1117 : : * sdepRel must be the pg_shdepend relation, already opened and suitably
1118 : : * locked.
1119 : : */
1120 : : static void
1121 : 154933 : shdepDropDependency(Relation sdepRel,
1122 : : Oid classId, Oid objectId, int32 objsubId,
1123 : : bool drop_subobjects,
1124 : : Oid refclassId, Oid refobjId,
1125 : : SharedDependencyType deptype)
1126 : : {
1127 : : ScanKeyData key[4];
1128 : : int nkeys;
1129 : : SysScanDesc scan;
1130 : : HeapTuple tup;
1131 : :
1132 : : /* Scan for entries matching the dependent object */
1133 : 154933 : ScanKeyInit(&key[0],
1134 : : Anum_pg_shdepend_dbid,
1135 : : BTEqualStrategyNumber, F_OIDEQ,
1136 : : ObjectIdGetDatum(classIdGetDbId(classId)));
1137 : 154933 : ScanKeyInit(&key[1],
1138 : : Anum_pg_shdepend_classid,
1139 : : BTEqualStrategyNumber, F_OIDEQ,
1140 : : ObjectIdGetDatum(classId));
1141 : 154933 : ScanKeyInit(&key[2],
1142 : : Anum_pg_shdepend_objid,
1143 : : BTEqualStrategyNumber, F_OIDEQ,
1144 : : ObjectIdGetDatum(objectId));
1145 [ + + ]: 154933 : if (drop_subobjects)
1146 : 153024 : nkeys = 3;
1147 : : else
1148 : : {
1149 : 1909 : ScanKeyInit(&key[3],
1150 : : Anum_pg_shdepend_objsubid,
1151 : : BTEqualStrategyNumber, F_INT4EQ,
1152 : : Int32GetDatum(objsubId));
1153 : 1909 : nkeys = 4;
1154 : : }
1155 : :
1156 : 154933 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
1157 : : NULL, nkeys, key);
1158 : :
1159 [ + + ]: 160237 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
1160 : : {
1161 : 5304 : Form_pg_shdepend shdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
1162 : :
1163 : : /* Filter entries according to additional parameters */
1164 [ + + - + ]: 5304 : if (OidIsValid(refclassId) && shdepForm->refclassid != refclassId)
1165 : 0 : continue;
1166 [ + + + + ]: 5304 : if (OidIsValid(refobjId) && shdepForm->refobjid != refobjId)
1167 : 607 : continue;
1168 [ + + ]: 4697 : if (deptype != SHARED_DEPENDENCY_INVALID &&
1169 [ + + ]: 557 : shdepForm->deptype != deptype)
1170 : 28 : continue;
1171 : :
1172 : : /* OK, delete it */
1173 : 4669 : CatalogTupleDelete(sdepRel, &tup->t_self);
1174 : : }
1175 : :
1176 : 154933 : systable_endscan(scan);
1177 : 154933 : }
1178 : :
1179 : : /*
1180 : : * classIdGetDbId
1181 : : *
1182 : : * Get the database Id that should be used in pg_shdepend, given the OID
1183 : : * of the catalog containing the object. For shared objects, it's 0
1184 : : * (InvalidOid); for all other objects, it's the current database Id.
1185 : : */
1186 : : static Oid
1187 : 160339 : classIdGetDbId(Oid classId)
1188 : : {
1189 : : Oid dbId;
1190 : :
1191 [ + + ]: 160339 : if (IsSharedRelation(classId))
1192 : 1196 : dbId = InvalidOid;
1193 : : else
1194 : 159143 : dbId = MyDatabaseId;
1195 : :
1196 : 160339 : return dbId;
1197 : : }
1198 : :
1199 : : /*
1200 : : * shdepLockAndCheckObject
1201 : : *
1202 : : * Acquire an AccessShareLock on a shared object and verify that it still
1203 : : * exists. This is used when recording a dependency or performing another
1204 : : * operation that must protect the object against a concurrent DROP.
1205 : : */
1206 : : void
1207 : 6171 : shdepLockAndCheckObject(Oid classId, Oid objectId)
1208 : : {
1209 : : /* AccessShareLock is sufficient to prevent a concurrent DROP. */
1210 : 6171 : LockSharedObject(classId, objectId, 0, AccessShareLock);
1211 : :
1212 [ + + + - ]: 6171 : switch (classId)
1213 : : {
1214 : 5367 : case AuthIdRelationId:
1215 [ - + ]: 5367 : if (!SearchSysCacheExists1(AUTHOID, ObjectIdGetDatum(objectId)))
1216 [ # # ]: 0 : ereport(ERROR,
1217 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1218 : : errmsg("role %u was concurrently dropped",
1219 : : objectId)));
1220 : 5367 : break;
1221 : :
1222 : 112 : case TableSpaceRelationId:
1223 : : {
1224 : : /* For lack of a syscache on pg_tablespace, do this: */
1225 : 112 : char *tablespace = get_tablespace_name(objectId);
1226 : :
1227 [ + + ]: 112 : if (tablespace == NULL)
1228 [ + - ]: 1 : ereport(ERROR,
1229 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1230 : : errmsg("tablespace %u was concurrently dropped",
1231 : : objectId)));
1232 : 111 : pfree(tablespace);
1233 : 111 : break;
1234 : : }
1235 : :
1236 : 692 : case DatabaseRelationId:
1237 : : {
1238 : : /* For lack of a syscache on pg_database, do this: */
1239 : 692 : char *database = get_database_name(objectId);
1240 : :
1241 [ - + ]: 692 : if (database == NULL)
1242 [ # # ]: 0 : ereport(ERROR,
1243 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1244 : : errmsg("database %u was concurrently dropped",
1245 : : objectId)));
1246 : 692 : pfree(database);
1247 : 692 : break;
1248 : : }
1249 : :
1250 : :
1251 : 0 : default:
1252 [ # # ]: 0 : elog(ERROR, "unrecognized shared classId: %u", classId);
1253 : : }
1254 : 6170 : }
1255 : :
1256 : :
1257 : : /*
1258 : : * storeObjectDescription
1259 : : * Append the description of a dependent object to "descs"
1260 : : *
1261 : : * While searching for dependencies of a shared object, we stash the
1262 : : * descriptions of dependent objects we find in a single string, which we
1263 : : * later pass to ereport() in the DETAIL field when somebody attempts to
1264 : : * drop a referenced shared object.
1265 : : *
1266 : : * When type is LOCAL_OBJECT or SHARED_OBJECT, we expect object to be the
1267 : : * dependent object, deptype is the dependency type, and count is not used.
1268 : : * When type is REMOTE_OBJECT, we expect object to be the database object,
1269 : : * and count to be nonzero; deptype is not used in this case.
1270 : : */
1271 : : static void
1272 : 376 : storeObjectDescription(StringInfo descs,
1273 : : SharedDependencyObjectType type,
1274 : : ObjectAddress *object,
1275 : : SharedDependencyType deptype,
1276 : : int count)
1277 : : {
1278 : 376 : char *objdesc = getObjectDescription(object, false);
1279 : :
1280 : : /*
1281 : : * An object being dropped concurrently doesn't need to be reported.
1282 : : */
1283 [ - + ]: 376 : if (objdesc == NULL)
1284 : 0 : return;
1285 : :
1286 : : /* separate entries with a newline */
1287 [ + + ]: 376 : if (descs->len != 0)
1288 : 194 : appendStringInfoChar(descs, '\n');
1289 : :
1290 [ + - - ]: 376 : switch (type)
1291 : : {
1292 : 376 : case LOCAL_OBJECT:
1293 : : case SHARED_OBJECT:
1294 [ + + ]: 376 : if (deptype == SHARED_DEPENDENCY_OWNER)
1295 : 162 : appendStringInfo(descs, _("owner of %s"), objdesc);
1296 [ + + ]: 214 : else if (deptype == SHARED_DEPENDENCY_ACL)
1297 : 180 : appendStringInfo(descs, _("privileges for %s"), objdesc);
1298 [ - + ]: 34 : else if (deptype == SHARED_DEPENDENCY_INITACL)
1299 : 0 : appendStringInfo(descs, _("initial privileges for %s"), objdesc);
1300 [ + + ]: 34 : else if (deptype == SHARED_DEPENDENCY_POLICY)
1301 : 16 : appendStringInfo(descs, _("target of %s"), objdesc);
1302 [ + - ]: 18 : else if (deptype == SHARED_DEPENDENCY_TABLESPACE)
1303 : 18 : appendStringInfo(descs, _("tablespace for %s"), objdesc);
1304 : : else
1305 [ # # ]: 0 : elog(ERROR, "unrecognized dependency type: %d",
1306 : : (int) deptype);
1307 : 376 : break;
1308 : :
1309 : 0 : case REMOTE_OBJECT:
1310 : : /* translator: %s will always be "database %s" */
1311 : 0 : appendStringInfo(descs, ngettext("%d object in %s",
1312 : : "%d objects in %s",
1313 : : count),
1314 : : count, objdesc);
1315 : 0 : break;
1316 : :
1317 : 0 : default:
1318 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d", type);
1319 : : }
1320 : :
1321 : 376 : pfree(objdesc);
1322 : : }
1323 : :
1324 : :
1325 : : /*
1326 : : * shdepDropOwned
1327 : : *
1328 : : * Drop the objects owned by any one of the given RoleIds. If a role has
1329 : : * access to an object, the grant will be removed as well (but the object
1330 : : * will not, of course).
1331 : : *
1332 : : * We can revoke grants immediately while doing the scan, but drops are
1333 : : * saved up and done all at once with performMultipleDeletions. This
1334 : : * is necessary so that we don't get failures from trying to delete
1335 : : * interdependent objects in the wrong order.
1336 : : */
1337 : : void
1338 : 94 : shdepDropOwned(List *roleids, DropBehavior behavior)
1339 : : {
1340 : : Relation sdepRel;
1341 : : ListCell *cell;
1342 : : ObjectAddresses *deleteobjs;
1343 : :
1344 : 94 : deleteobjs = new_object_addresses();
1345 : :
1346 : : /*
1347 : : * We don't need this strong a lock here, but we'll call routines that
1348 : : * acquire RowExclusiveLock. Better get that right now to avoid potential
1349 : : * deadlock failures.
1350 : : */
1351 : 94 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1352 : :
1353 : : /*
1354 : : * For each role, find the dependent objects and drop them using the
1355 : : * regular (non-shared) dependency management.
1356 : : */
1357 [ + - + + : 202 : foreach(cell, roleids)
+ + ]
1358 : : {
1359 : 108 : Oid roleid = lfirst_oid(cell);
1360 : : ScanKeyData key[2];
1361 : : SysScanDesc scan;
1362 : : HeapTuple tuple;
1363 : :
1364 : : /* Doesn't work for pinned objects */
1365 [ - + ]: 108 : if (IsPinnedObject(AuthIdRelationId, roleid))
1366 : : {
1367 : : ObjectAddress obj;
1368 : :
1369 : 0 : obj.classId = AuthIdRelationId;
1370 : 0 : obj.objectId = roleid;
1371 : 0 : obj.objectSubId = 0;
1372 : :
1373 [ # # ]: 0 : ereport(ERROR,
1374 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1375 : : errmsg("cannot drop objects owned by %s because they are "
1376 : : "required by the database system",
1377 : : getObjectDescription(&obj, false))));
1378 : : }
1379 : :
1380 : 108 : ScanKeyInit(&key[0],
1381 : : Anum_pg_shdepend_refclassid,
1382 : : BTEqualStrategyNumber, F_OIDEQ,
1383 : : ObjectIdGetDatum(AuthIdRelationId));
1384 : 108 : ScanKeyInit(&key[1],
1385 : : Anum_pg_shdepend_refobjid,
1386 : : BTEqualStrategyNumber, F_OIDEQ,
1387 : : ObjectIdGetDatum(roleid));
1388 : :
1389 : 108 : scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1390 : : NULL, 2, key);
1391 : :
1392 [ + + ]: 555 : while ((tuple = systable_getnext(scan)) != NULL)
1393 : : {
1394 : 447 : Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1395 : : ObjectAddress obj;
1396 : :
1397 : : /*
1398 : : * We only operate on shared objects and objects in the current
1399 : : * database
1400 : : */
1401 [ + + ]: 447 : if (sdepForm->dbid != MyDatabaseId &&
1402 [ - + ]: 28 : sdepForm->dbid != InvalidOid)
1403 : 0 : continue;
1404 : :
1405 [ - + + + : 447 : switch (sdepForm->deptype)
+ - ]
1406 : : {
1407 : : /* Shouldn't happen */
1408 : 0 : case SHARED_DEPENDENCY_INVALID:
1409 [ # # ]: 0 : elog(ERROR, "unexpected dependency type");
1410 : : break;
1411 : 28 : case SHARED_DEPENDENCY_POLICY:
1412 : :
1413 : : /*
1414 : : * Try to remove role from policy; if unable to, remove
1415 : : * policy.
1416 : : */
1417 [ + + ]: 28 : if (!RemoveRoleFromObjectPolicy(roleid,
1418 : : sdepForm->classid,
1419 : : sdepForm->objid))
1420 : : {
1421 : 12 : obj.classId = sdepForm->classid;
1422 : 12 : obj.objectId = sdepForm->objid;
1423 : 12 : obj.objectSubId = sdepForm->objsubid;
1424 : :
1425 : : /*
1426 : : * Acquire lock on object, then verify this dependency
1427 : : * is still relevant. If not, the object might have
1428 : : * been dropped or the policy modified. Ignore the
1429 : : * object in that case.
1430 : : */
1431 : 12 : AcquireDeletionLock(&obj, 0);
1432 [ - + ]: 12 : if (!systable_recheck_tuple(scan, tuple))
1433 : : {
1434 : 0 : ReleaseDeletionLock(&obj);
1435 : 0 : break;
1436 : : }
1437 : 12 : add_exact_object_address(&obj, deleteobjs);
1438 : : }
1439 : 28 : break;
1440 : 170 : case SHARED_DEPENDENCY_ACL:
1441 : :
1442 : : /*
1443 : : * Dependencies on role grants are recorded using
1444 : : * SHARED_DEPENDENCY_ACL, but unlike a regular ACL list
1445 : : * which stores all permissions for a particular object in
1446 : : * a single ACL array, there's a separate catalog row for
1447 : : * each grant - so removing the grant just means removing
1448 : : * the entire row.
1449 : : */
1450 [ + + ]: 170 : if (sdepForm->classid != AuthMemRelationId)
1451 : : {
1452 : : /*
1453 : : * Lock tablespaces before updating their catalog
1454 : : * tuple.
1455 : : */
1456 [ + + ]: 164 : if (sdepForm->classid == TableSpaceRelationId)
1457 : : {
1458 : 4 : LockSharedObject(sdepForm->classid,
1459 : : sdepForm->objid, 0,
1460 : : AccessShareLock);
1461 [ - + ]: 4 : if (!systable_recheck_tuple(scan, tuple))
1462 : : {
1463 : 0 : UnlockSharedObject(sdepForm->classid,
1464 : : sdepForm->objid, 0,
1465 : : AccessShareLock);
1466 : 0 : break;
1467 : : }
1468 : : }
1469 : 164 : RemoveRoleFromObjectACL(roleid,
1470 : : sdepForm->classid,
1471 : : sdepForm->objid);
1472 : 164 : break;
1473 : : }
1474 : : pg_fallthrough;
1475 : :
1476 : : case SHARED_DEPENDENCY_OWNER:
1477 : :
1478 : : /*
1479 : : * Save it for deletion below, if it's a local object or a
1480 : : * role grant. Other shared objects, such as databases,
1481 : : * should not be removed here.
1482 : : */
1483 [ + + ]: 241 : if (sdepForm->dbid == MyDatabaseId ||
1484 [ + - ]: 6 : sdepForm->classid == AuthMemRelationId)
1485 : : {
1486 : 241 : obj.classId = sdepForm->classid;
1487 : 241 : obj.objectId = sdepForm->objid;
1488 : 241 : obj.objectSubId = sdepForm->objsubid;
1489 : : /* as above */
1490 : 241 : AcquireDeletionLock(&obj, 0);
1491 [ + + ]: 241 : if (!systable_recheck_tuple(scan, tuple))
1492 : : {
1493 : 1 : ReleaseDeletionLock(&obj);
1494 : 1 : break;
1495 : : }
1496 : 240 : add_exact_object_address(&obj, deleteobjs);
1497 : : }
1498 : 240 : break;
1499 : 14 : case SHARED_DEPENDENCY_INITACL:
1500 : :
1501 : : /*
1502 : : * Any mentions of the role that remain in pg_init_privs
1503 : : * entries are just dropped. This is the same policy as
1504 : : * we apply to regular ACLs.
1505 : : */
1506 : :
1507 : : /* Shouldn't see a role grant here */
1508 : : Assert(sdepForm->classid != AuthMemRelationId);
1509 : 14 : RemoveRoleFromInitPriv(roleid,
1510 : : sdepForm->classid,
1511 : : sdepForm->objid,
1512 : : sdepForm->objsubid);
1513 : 14 : break;
1514 : : }
1515 : : }
1516 : :
1517 : 108 : systable_endscan(scan);
1518 : : }
1519 : :
1520 : : /*
1521 : : * For stability of deletion-report ordering, sort the objects into
1522 : : * approximate reverse creation order before deletion. (This might also
1523 : : * make the deletion go a bit faster, since there's less chance of having
1524 : : * to rearrange the objects due to dependencies.)
1525 : : */
1526 : 94 : sort_object_addresses(deleteobjs);
1527 : :
1528 : : /* the dependency mechanism does the actual work */
1529 : 94 : performMultipleDeletions(deleteobjs, behavior, 0);
1530 : :
1531 : 90 : table_close(sdepRel, RowExclusiveLock);
1532 : :
1533 : 90 : free_object_addresses(deleteobjs);
1534 : 90 : }
1535 : :
1536 : : /*
1537 : : * shdepReassignOwned
1538 : : *
1539 : : * Change the owner of objects owned by any of the roles in roleids to
1540 : : * newrole. Grants are not touched.
1541 : : */
1542 : : void
1543 : 27 : shdepReassignOwned(List *roleids, Oid newrole)
1544 : : {
1545 : : Relation sdepRel;
1546 : : ListCell *cell;
1547 : :
1548 : : /*
1549 : : * We don't need this strong a lock here, but we'll call routines that
1550 : : * acquire RowExclusiveLock. Better get that right now to avoid potential
1551 : : * deadlock problems.
1552 : : */
1553 : 27 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1554 : :
1555 [ + - + + : 54 : foreach(cell, roleids)
+ + ]
1556 : : {
1557 : : SysScanDesc scan;
1558 : : ScanKeyData key[2];
1559 : : HeapTuple tuple;
1560 : 27 : Oid roleid = lfirst_oid(cell);
1561 : :
1562 : : /* Refuse to work on pinned roles */
1563 [ - + ]: 27 : if (IsPinnedObject(AuthIdRelationId, roleid))
1564 : : {
1565 : : ObjectAddress obj;
1566 : :
1567 : 0 : obj.classId = AuthIdRelationId;
1568 : 0 : obj.objectId = roleid;
1569 : 0 : obj.objectSubId = 0;
1570 : :
1571 [ # # ]: 0 : ereport(ERROR,
1572 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1573 : : errmsg("cannot reassign ownership of objects owned by %s because they are required by the database system",
1574 : : getObjectDescription(&obj, false))));
1575 : :
1576 : : /*
1577 : : * There's no need to tell the whole truth, which is that we
1578 : : * didn't track these dependencies at all ...
1579 : : */
1580 : : }
1581 : :
1582 : 27 : ScanKeyInit(&key[0],
1583 : : Anum_pg_shdepend_refclassid,
1584 : : BTEqualStrategyNumber, F_OIDEQ,
1585 : : ObjectIdGetDatum(AuthIdRelationId));
1586 : 27 : ScanKeyInit(&key[1],
1587 : : Anum_pg_shdepend_refobjid,
1588 : : BTEqualStrategyNumber, F_OIDEQ,
1589 : : ObjectIdGetDatum(roleid));
1590 : :
1591 : 27 : scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1592 : : NULL, 2, key);
1593 : :
1594 [ + + ]: 151 : while ((tuple = systable_getnext(scan)) != NULL)
1595 : : {
1596 : 124 : Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1597 : : MemoryContext cxt,
1598 : : oldcxt;
1599 : :
1600 : : /*
1601 : : * We only operate on shared objects and objects in the current
1602 : : * database
1603 : : */
1604 [ + + ]: 124 : if (sdepForm->dbid != MyDatabaseId &&
1605 [ + + ]: 28 : sdepForm->dbid != InvalidOid)
1606 : 2 : continue;
1607 : :
1608 : : /*
1609 : : * The various DDL routines called here tend to leak memory in
1610 : : * CurrentMemoryContext. That's not a problem when they're only
1611 : : * called once per command; but in this usage where we might be
1612 : : * touching many objects, it can amount to a serious memory leak.
1613 : : * Fix that by running each call in a short-lived context.
1614 : : */
1615 : 122 : cxt = AllocSetContextCreate(CurrentMemoryContext,
1616 : : "shdepReassignOwned",
1617 : : ALLOCSET_DEFAULT_SIZES);
1618 : 122 : oldcxt = MemoryContextSwitchTo(cxt);
1619 : :
1620 : : /* Perform the appropriate processing */
1621 [ + + + - ]: 122 : switch (sdepForm->deptype)
1622 : : {
1623 : 81 : case SHARED_DEPENDENCY_OWNER:
1624 : : /* Lock tablespaces before updating their catalog tuple. */
1625 [ + + ]: 81 : if (sdepForm->classid == TableSpaceRelationId)
1626 : : {
1627 : 4 : LockSharedObject(sdepForm->classid,
1628 : : sdepForm->objid, 0,
1629 : : AccessShareLock);
1630 [ - + ]: 4 : if (!systable_recheck_tuple(scan, tuple))
1631 : : {
1632 : 0 : UnlockSharedObject(sdepForm->classid,
1633 : : sdepForm->objid, 0,
1634 : : AccessShareLock);
1635 : 0 : break;
1636 : : }
1637 : : }
1638 : 81 : shdepReassignOwned_Owner(sdepForm, newrole);
1639 : 81 : break;
1640 : 12 : case SHARED_DEPENDENCY_INITACL:
1641 : 12 : shdepReassignOwned_InitAcl(sdepForm, roleid, newrole);
1642 : 12 : break;
1643 : 29 : case SHARED_DEPENDENCY_ACL:
1644 : : case SHARED_DEPENDENCY_POLICY:
1645 : : case SHARED_DEPENDENCY_TABLESPACE:
1646 : : /* Nothing to do for these entry types */
1647 : 29 : break;
1648 : 0 : default:
1649 [ # # ]: 0 : elog(ERROR, "unrecognized dependency type: %d",
1650 : : (int) sdepForm->deptype);
1651 : : break;
1652 : : }
1653 : :
1654 : : /* Clean up */
1655 : 122 : MemoryContextSwitchTo(oldcxt);
1656 : 122 : MemoryContextDelete(cxt);
1657 : :
1658 : : /* Make sure the next iteration will see my changes */
1659 : 122 : CommandCounterIncrement();
1660 : : }
1661 : :
1662 : 27 : systable_endscan(scan);
1663 : : }
1664 : :
1665 : 27 : table_close(sdepRel, RowExclusiveLock);
1666 : 27 : }
1667 : :
1668 : : /*
1669 : : * shdepReassignOwned_Owner
1670 : : *
1671 : : * shdepReassignOwned's processing of SHARED_DEPENDENCY_OWNER entries
1672 : : */
1673 : : static void
1674 : 81 : shdepReassignOwned_Owner(Form_pg_shdepend sdepForm, Oid newrole)
1675 : : {
1676 : : /* Issue the appropriate ALTER OWNER call */
1677 [ + + + + : 81 : switch (sdepForm->classid)
+ + - - -
+ + - ]
1678 : : {
1679 : 13 : case TypeRelationId:
1680 : 13 : AlterTypeOwner_oid(sdepForm->objid, newrole, true);
1681 : 13 : break;
1682 : :
1683 : 5 : case NamespaceRelationId:
1684 : 5 : AlterSchemaOwner_oid(sdepForm->objid, newrole);
1685 : 5 : break;
1686 : :
1687 : 24 : case RelationRelationId:
1688 : :
1689 : : /*
1690 : : * Pass recursing = true so that we don't fail on indexes, owned
1691 : : * sequences, etc when we happen to visit them before their parent
1692 : : * table.
1693 : : */
1694 : 24 : ATExecChangeOwner(sdepForm->objid, newrole, true, AccessExclusiveLock);
1695 : 24 : break;
1696 : :
1697 : 4 : case DefaultAclRelationId:
1698 : :
1699 : : /*
1700 : : * Ignore default ACLs; they should be handled by DROP OWNED, not
1701 : : * REASSIGN OWNED.
1702 : : */
1703 : 4 : break;
1704 : :
1705 : 9 : case UserMappingRelationId:
1706 : : /* ditto */
1707 : 9 : break;
1708 : :
1709 : 8 : case ForeignServerRelationId:
1710 : 8 : AlterForeignServerOwner_oid(sdepForm->objid, newrole);
1711 : 8 : break;
1712 : :
1713 : 0 : case ForeignDataWrapperRelationId:
1714 : 0 : AlterForeignDataWrapperOwner_oid(sdepForm->objid, newrole);
1715 : 0 : break;
1716 : :
1717 : 0 : case EventTriggerRelationId:
1718 : 0 : AlterEventTriggerOwner_oid(sdepForm->objid, newrole);
1719 : 0 : break;
1720 : :
1721 : 0 : case PublicationRelationId:
1722 : 0 : AlterPublicationOwner_oid(sdepForm->objid, newrole);
1723 : 0 : break;
1724 : :
1725 : 2 : case SubscriptionRelationId:
1726 : 2 : AlterSubscriptionOwner_oid(sdepForm->objid, newrole);
1727 : 2 : break;
1728 : :
1729 : : /* Generic alter owner cases */
1730 : 16 : case CollationRelationId:
1731 : : case ConversionRelationId:
1732 : : case OperatorRelationId:
1733 : : case ProcedureRelationId:
1734 : : case LanguageRelationId:
1735 : : case LargeObjectRelationId:
1736 : : case OperatorFamilyRelationId:
1737 : : case OperatorClassRelationId:
1738 : : case ExtensionRelationId:
1739 : : case StatisticExtRelationId:
1740 : : case TableSpaceRelationId:
1741 : : case DatabaseRelationId:
1742 : : case TSConfigRelationId:
1743 : : case TSDictionaryRelationId:
1744 : 16 : AlterObjectOwner_internal(sdepForm->classid,
1745 : : sdepForm->objid,
1746 : : newrole);
1747 : 16 : break;
1748 : :
1749 : 0 : default:
1750 [ # # ]: 0 : elog(ERROR, "unexpected classid %u", sdepForm->classid);
1751 : : break;
1752 : : }
1753 : 81 : }
1754 : :
1755 : : /*
1756 : : * shdepReassignOwned_InitAcl
1757 : : *
1758 : : * shdepReassignOwned's processing of SHARED_DEPENDENCY_INITACL entries
1759 : : */
1760 : : static void
1761 : 12 : shdepReassignOwned_InitAcl(Form_pg_shdepend sdepForm, Oid oldrole, Oid newrole)
1762 : : {
1763 : : /*
1764 : : * Currently, REASSIGN OWNED replaces mentions of oldrole with newrole in
1765 : : * pg_init_privs entries, just as it does in the object's regular ACL.
1766 : : * This is less than ideal, since pg_init_privs ought to retain a
1767 : : * historical record of the situation at the end of CREATE EXTENSION.
1768 : : * However, there are two big stumbling blocks to doing something
1769 : : * different:
1770 : : *
1771 : : * 1. If we don't replace the references, what is to happen if the old
1772 : : * role gets dropped? (DROP OWNED's current answer is to just delete the
1773 : : * pg_init_privs entry, which is surely ahistorical.)
1774 : : *
1775 : : * 2. It's unlikely that pg_dump will cope nicely with pg_init_privs
1776 : : * entries that are based on a different owner than the object now has ---
1777 : : * the more so given that pg_init_privs doesn't record the original owner
1778 : : * explicitly. (This problem actually exists anyway given that a bare
1779 : : * ALTER OWNER won't update pg_init_privs, but we don't need REASSIGN
1780 : : * OWNED making it worse.)
1781 : : */
1782 : 12 : ReplaceRoleInInitPriv(oldrole, newrole,
1783 : : sdepForm->classid,
1784 : : sdepForm->objid,
1785 : : sdepForm->objsubid);
1786 : 12 : }
|