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 : 119627 : 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 [ - + ]: 119627 : if (IsBootstrapProcessingMode())
142 : 0 : return;
143 : :
144 : 119627 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
145 : :
146 : : /* If the referenced object is pinned, do nothing. */
147 [ + + ]: 119627 : if (!IsPinnedObject(referenced->classId, referenced->objectId))
148 : : {
149 : 3480 : shdepAddDependency(sdepRel, depender->classId, depender->objectId,
150 : : depender->objectSubId,
151 : : referenced->classId, referenced->objectId,
152 : : deptype);
153 : : }
154 : :
155 : 119627 : 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 : 119352 : recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
169 : : {
170 : : ObjectAddress myself,
171 : : referenced;
172 : :
173 : 119352 : myself.classId = classId;
174 : 119352 : myself.objectId = objectId;
175 : 119352 : myself.objectSubId = 0;
176 : :
177 : 119352 : referenced.classId = AuthIdRelationId;
178 : 119352 : referenced.objectId = owner;
179 : 119352 : referenced.objectSubId = 0;
180 : :
181 : 119352 : recordSharedDependencyOn(&myself, &referenced, SHARED_DEPENDENCY_OWNER);
182 : 119352 : }
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 : 578 : shdepChangeDep(Relation sdepRel,
207 : : Oid classid, Oid objid, int32 objsubid,
208 : : Oid refclassid, Oid refobjid,
209 : : SharedDependencyType deptype)
210 : : {
211 : 578 : Oid dbid = classIdGetDbId(classid);
212 : 578 : 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 : 578 : shdepLockAndCheckObject(refclassid, refobjid);
222 : :
223 : : /*
224 : : * Look for a previous entry
225 : : */
226 : 578 : ScanKeyInit(&key[0],
227 : : Anum_pg_shdepend_dbid,
228 : : BTEqualStrategyNumber, F_OIDEQ,
229 : : ObjectIdGetDatum(dbid));
230 : 578 : ScanKeyInit(&key[1],
231 : : Anum_pg_shdepend_classid,
232 : : BTEqualStrategyNumber, F_OIDEQ,
233 : : ObjectIdGetDatum(classid));
234 : 578 : ScanKeyInit(&key[2],
235 : : Anum_pg_shdepend_objid,
236 : : BTEqualStrategyNumber, F_OIDEQ,
237 : : ObjectIdGetDatum(objid));
238 : 578 : ScanKeyInit(&key[3],
239 : : Anum_pg_shdepend_objsubid,
240 : : BTEqualStrategyNumber, F_INT4EQ,
241 : : Int32GetDatum(objsubid));
242 : :
243 : 578 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
244 : : NULL, 4, key);
245 : :
246 [ + + ]: 921 : while ((scantup = systable_getnext(scan)) != NULL)
247 : : {
248 : : /* Ignore if not of the target dependency type */
249 [ + + ]: 343 : if (((Form_pg_shdepend) GETSTRUCT(scantup))->deptype != deptype)
250 : 46 : continue;
251 : : /* Caller screwed up if multiple matches */
252 [ - + ]: 297 : if (oldtup)
253 [ # # ]: 0 : elog(ERROR,
254 : : "multiple pg_shdepend entries for object %u/%u/%d deptype %c",
255 : : classid, objid, objsubid, deptype);
256 : 297 : oldtup = heap_copytuple(scantup);
257 : : }
258 : :
259 : 578 : systable_endscan(scan);
260 : :
261 [ + + ]: 578 : if (IsPinnedObject(refclassid, refobjid))
262 : : {
263 : : /* No new entry needed, so just delete existing entry if any */
264 [ + + ]: 59 : if (oldtup)
265 : 50 : CatalogTupleDelete(sdepRel, &oldtup->t_self);
266 : : }
267 [ + + ]: 519 : else if (oldtup)
268 : : {
269 : : /* Need to update existing entry */
270 : 247 : Form_pg_shdepend shForm = (Form_pg_shdepend) GETSTRUCT(oldtup);
271 : :
272 : : /* Since oldtup is a copy, we can just modify it in-memory */
273 : 247 : shForm->refclassid = refclassid;
274 : 247 : shForm->refobjid = refobjid;
275 : :
276 : 247 : 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 : 272 : memset(nulls, false, sizeof(nulls));
285 : :
286 : 272 : values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(dbid);
287 : 272 : values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classid);
288 : 272 : values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objid);
289 : 272 : values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(objsubid);
290 : :
291 : 272 : values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassid);
292 : 272 : values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjid);
293 : 272 : 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 : 272 : oldtup = heap_form_tuple(RelationGetDescr(sdepRel), values, nulls);
300 : 272 : CatalogTupleInsert(sdepRel, oldtup);
301 : : }
302 : :
303 [ + + ]: 578 : if (oldtup)
304 : 569 : heap_freetuple(oldtup);
305 : 578 : }
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 : 570 : changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
317 : : {
318 : : Relation sdepRel;
319 : :
320 : 570 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
321 : :
322 : : /* Adjust the SHARED_DEPENDENCY_OWNER entry */
323 : 570 : 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 : 570 : 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 : 570 : table_close(sdepRel, RowExclusiveLock);
358 : 570 : }
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 : 94 : recordDependencyOnTablespace(Oid classId, Oid objectId, Oid tablespace)
371 : : {
372 : : ObjectAddress myself,
373 : : referenced;
374 : :
375 : 94 : ObjectAddressSet(myself, classId, objectId);
376 : 94 : ObjectAddressSet(referenced, TableSpaceRelationId, tablespace);
377 : :
378 : 94 : recordSharedDependencyOn(&myself, &referenced,
379 : : SHARED_DEPENDENCY_TABLESPACE);
380 : 94 : }
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 : 15716 : getOidListDiff(Oid *list1, int *nlist1, Oid *list2, int *nlist2)
422 : : {
423 : : int in1,
424 : : in2,
425 : : out1,
426 : : out2;
427 : :
428 : 15716 : in1 = in2 = out1 = out2 = 0;
429 [ + + + + ]: 23027 : while (in1 < *nlist1 && in2 < *nlist2)
430 : : {
431 [ + + ]: 7311 : if (list1[in1] == list2[in2])
432 : : {
433 : : /* skip over duplicates */
434 : 7198 : in1++;
435 : 7198 : in2++;
436 : : }
437 [ + + ]: 113 : else if (list1[in1] < list2[in2])
438 : : {
439 : : /* list1[in1] is not in list2 */
440 : 75 : 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 [ + + ]: 16299 : while (in1 < *nlist1)
451 : : {
452 : 583 : list1[out1++] = list1[in1++];
453 : : }
454 : :
455 : : /* any remaining list2 entries are not in list1 */
456 [ + + ]: 26779 : while (in2 < *nlist2)
457 : : {
458 : 11063 : list2[out2++] = list2[in2++];
459 : : }
460 : :
461 : 15716 : *nlist1 = out1;
462 : 15716 : *nlist2 = out2;
463 : 15716 : }
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 : 15181 : updateAclDependencies(Oid classId, Oid objectId, int32 objsubId,
492 : : Oid ownerId,
493 : : int noldmembers, Oid *oldmembers,
494 : : int nnewmembers, Oid *newmembers)
495 : : {
496 : 15181 : updateAclDependenciesWorker(classId, objectId, objsubId,
497 : : ownerId, SHARED_DEPENDENCY_ACL,
498 : : noldmembers, oldmembers,
499 : : nnewmembers, newmembers);
500 : 15181 : }
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 : 15716 : 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 : 15716 : getOidListDiff(oldmembers, &noldmembers, newmembers, &nnewmembers);
540 : :
541 [ + + + + ]: 15716 : if (noldmembers > 0 || nnewmembers > 0)
542 : : {
543 : 10585 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
544 : :
545 : : /* Add new dependencies that weren't already present */
546 [ + + ]: 21686 : for (i = 0; i < nnewmembers; i++)
547 : : {
548 : 11101 : 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 [ + + + + ]: 11101 : if (deptype == SHARED_DEPENDENCY_ACL && roleid == ownerId)
558 : 8383 : continue;
559 : :
560 : : /* Skip pinned roles; they don't need dependency entries */
561 [ + + ]: 2718 : if (IsPinnedObject(AuthIdRelationId, roleid))
562 : 1119 : continue;
563 : :
564 : 1599 : shdepAddDependency(sdepRel, classId, objectId, objsubId,
565 : : AuthIdRelationId, roleid,
566 : : deptype);
567 : : }
568 : :
569 : : /* Drop no-longer-used old dependencies */
570 [ + + ]: 11243 : for (i = 0; i < noldmembers; i++)
571 : : {
572 : 658 : Oid roleid = oldmembers[i];
573 : :
574 : : /* Skip the owner for ACL entries, same as above */
575 [ + + + + ]: 658 : if (deptype == SHARED_DEPENDENCY_ACL && roleid == ownerId)
576 : 73 : continue;
577 : :
578 : : /* Skip pinned roles */
579 [ + + ]: 585 : if (IsPinnedObject(AuthIdRelationId, roleid))
580 : 61 : continue;
581 : :
582 : 524 : shdepDropDependency(sdepRel, classId, objectId, objsubId,
583 : : false, /* exact match on objsubId */
584 : : AuthIdRelationId, roleid,
585 : : deptype);
586 : : }
587 : :
588 : 10585 : table_close(sdepRel, RowExclusiveLock);
589 : : }
590 : :
591 [ + + ]: 15716 : if (oldmembers)
592 : 6471 : pfree(oldmembers);
593 [ + + ]: 15716 : if (newmembers)
594 : 15537 : pfree(newmembers);
595 : 15716 : }
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 : 1149 : 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 : 1149 : int numReportedDeps = 0;
684 : 1149 : int numNotReportedDeps = 0;
685 : 1149 : int numNotReportedDbs = 0;
686 : 1149 : 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 [ - + ]: 1149 : 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 : 1149 : allocedobjects = 128; /* arbitrary initial array size */
719 : 1149 : objects = palloc_array(ShDependObjectInfo, allocedobjects);
720 : 1149 : numobjects = 0;
721 : 1149 : initStringInfo(&descs);
722 : 1149 : initStringInfo(&alldescs);
723 : :
724 : 1149 : sdepRel = table_open(SharedDependRelationId, AccessShareLock);
725 : :
726 : 1149 : ScanKeyInit(&key[0],
727 : : Anum_pg_shdepend_refclassid,
728 : : BTEqualStrategyNumber, F_OIDEQ,
729 : : ObjectIdGetDatum(classId));
730 : 1149 : ScanKeyInit(&key[1],
731 : : Anum_pg_shdepend_refobjid,
732 : : BTEqualStrategyNumber, F_OIDEQ,
733 : : ObjectIdGetDatum(objectId));
734 : :
735 : 1149 : scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
736 : : NULL, 2, key);
737 : :
738 [ + + ]: 1332 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
739 : : {
740 : 183 : Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
741 : :
742 : 183 : object.classId = sdepForm->classid;
743 : 183 : object.objectId = sdepForm->objid;
744 : 183 : 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 [ + + ]: 183 : if (sdepForm->dbid == MyDatabaseId ||
754 [ + - ]: 50 : sdepForm->dbid == InvalidOid)
755 : : {
756 [ - + ]: 183 : if (numobjects >= allocedobjects)
757 : : {
758 : 0 : allocedobjects *= 2;
759 : 0 : objects = repalloc_array(objects, ShDependObjectInfo, allocedobjects);
760 : : }
761 : 183 : objects[numobjects].object = object;
762 : 183 : objects[numobjects].deptype = sdepForm->deptype;
763 : 183 : objects[numobjects].objtype = (sdepForm->dbid == MyDatabaseId) ?
764 : 183 : LOCAL_OBJECT : SHARED_OBJECT;
765 : 183 : 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 : 1149 : systable_endscan(scan);
800 : :
801 : 1149 : table_close(sdepRel, AccessShareLock);
802 : :
803 : : /*
804 : : * Sort and report local and shared objects.
805 : : */
806 [ + + ]: 1149 : if (numobjects > 1)
807 : 37 : qsort(objects, numobjects,
808 : : sizeof(ShDependObjectInfo), shared_dependency_comparator);
809 : :
810 [ + + ]: 1332 : for (int i = 0; i < numobjects; i++)
811 : : {
812 [ + - ]: 183 : if (numReportedDeps < MAX_REPORTED_DEPS)
813 : : {
814 : 183 : numReportedDeps++;
815 : 183 : storeObjectDescription(&descs,
816 : 183 : objects[i].objtype,
817 : 183 : &objects[i].object,
818 : 183 : objects[i].deptype,
819 : : 0);
820 : : }
821 : : else
822 : 0 : numNotReportedDeps++;
823 : 183 : storeObjectDescription(&alldescs,
824 : 183 : objects[i].objtype,
825 : 183 : &objects[i].object,
826 : 183 : objects[i].deptype,
827 : : 0);
828 : : }
829 : :
830 : : /*
831 : : * Summarize dependencies in remote databases.
832 : : */
833 [ - + - - : 1149 : 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 : 1149 : pfree(objects);
854 : 1149 : list_free_deep(remDeps);
855 : :
856 [ + + ]: 1149 : if (descs.len == 0)
857 : : {
858 : 1063 : pfree(descs.data);
859 : 1063 : pfree(alldescs.data);
860 : 1063 : *detail_msg = *detail_log_msg = NULL;
861 : 1063 : return false;
862 : : }
863 : :
864 [ - + ]: 86 : 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 [ - + ]: 86 : 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 : 86 : *detail_msg = descs.data;
880 : 86 : *detail_log_msg = alldescs.data;
881 : 86 : 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 : 426 : 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 : 426 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
906 : 426 : 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 : 426 : max_slots = MAX_CATALOG_MULTI_INSERT_BYTES / sizeof(FormData_pg_shdepend);
913 : 426 : slot = palloc_array(TupleTableSlot *, max_slots);
914 : :
915 : 426 : indstate = CatalogOpenIndexes(sdepRel);
916 : :
917 : : /* Scan all entries with dbid = templateDbId */
918 : 426 : ScanKeyInit(&key[0],
919 : : Anum_pg_shdepend_dbid,
920 : : BTEqualStrategyNumber, F_OIDEQ,
921 : : ObjectIdGetDatum(templateDbId));
922 : :
923 : 426 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
924 : : NULL, 1, key);
925 : :
926 : : /* number of slots currently storing tuples */
927 : 426 : slot_stored_count = 0;
928 : : /* number of slots currently initialized */
929 : 426 : 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 [ + + ]: 438 : 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 [ + + ]: 426 : if (slot_stored_count > 0)
976 : 6 : CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slot_stored_count, indstate);
977 : :
978 : 426 : systable_endscan(scan);
979 : :
980 : 426 : CatalogCloseIndexes(indstate);
981 : 426 : table_close(sdepRel, RowExclusiveLock);
982 : :
983 : : /* Drop only the number of slots used */
984 [ + + ]: 438 : for (int i = 0; i < slot_init_count; i++)
985 : 12 : ExecDropSingleTupleTableSlot(slot[i]);
986 : 426 : pfree(slot);
987 : 426 : }
988 : :
989 : : /*
990 : : * dropDatabaseDependencies
991 : : *
992 : : * Delete pg_shdepend entries corresponding to a database that's being
993 : : * dropped.
994 : : */
995 : : void
996 : 58 : dropDatabaseDependencies(Oid databaseId)
997 : : {
998 : : Relation sdepRel;
999 : : ScanKeyData key[1];
1000 : : SysScanDesc scan;
1001 : : HeapTuple tup;
1002 : :
1003 : 58 : 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 : 58 : 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 : 58 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
1016 : : NULL, 1, key);
1017 : :
1018 [ + + ]: 59 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
1019 : : {
1020 : 1 : CatalogTupleDelete(sdepRel, &tup->t_self);
1021 : : }
1022 : :
1023 : 58 : systable_endscan(scan);
1024 : :
1025 : : /* Now delete all entries corresponding to the database itself */
1026 : 58 : shdepDropDependency(sdepRel, DatabaseRelationId, databaseId, 0, true,
1027 : : InvalidOid, InvalidOid,
1028 : : SHARED_DEPENDENCY_INVALID);
1029 : :
1030 : 58 : table_close(sdepRel, RowExclusiveLock);
1031 : 58 : }
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 : 161716 : deleteSharedDependencyRecordsFor(Oid classId, Oid objectId, int32 objectSubId)
1045 : : {
1046 : : Relation sdepRel;
1047 : :
1048 : 161716 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1049 : :
1050 : 161716 : shdepDropDependency(sdepRel, classId, objectId, objectSubId,
1051 : : (objectSubId == 0),
1052 : : InvalidOid, InvalidOid,
1053 : : SHARED_DEPENDENCY_INVALID);
1054 : :
1055 : 161716 : table_close(sdepRel, RowExclusiveLock);
1056 : 161716 : }
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 : 5079 : 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 : 5079 : shdepLockAndCheckObject(refclassId, refobjId);
1081 : :
1082 : 5079 : memset(nulls, false, sizeof(nulls));
1083 : :
1084 : : /*
1085 : : * Form the new tuple and record the dependency.
1086 : : */
1087 : 5079 : values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(classIdGetDbId(classId));
1088 : 5079 : values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classId);
1089 : 5079 : values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objectId);
1090 : 5079 : values[Anum_pg_shdepend_objsubid - 1] = Int32GetDatum(objsubId);
1091 : :
1092 : 5079 : values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassId);
1093 : 5079 : values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjId);
1094 : 5079 : values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);
1095 : :
1096 : 5079 : tup = heap_form_tuple(sdepRel->rd_att, values, nulls);
1097 : :
1098 : 5079 : CatalogTupleInsert(sdepRel, tup);
1099 : :
1100 : : /* clean up */
1101 : 5079 : heap_freetuple(tup);
1102 : 5079 : }
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 : 162880 : 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 : 162880 : ScanKeyInit(&key[0],
1134 : : Anum_pg_shdepend_dbid,
1135 : : BTEqualStrategyNumber, F_OIDEQ,
1136 : : ObjectIdGetDatum(classIdGetDbId(classId)));
1137 : 162880 : ScanKeyInit(&key[1],
1138 : : Anum_pg_shdepend_classid,
1139 : : BTEqualStrategyNumber, F_OIDEQ,
1140 : : ObjectIdGetDatum(classId));
1141 : 162880 : ScanKeyInit(&key[2],
1142 : : Anum_pg_shdepend_objid,
1143 : : BTEqualStrategyNumber, F_OIDEQ,
1144 : : ObjectIdGetDatum(objectId));
1145 [ + + ]: 162880 : if (drop_subobjects)
1146 : 160934 : nkeys = 3;
1147 : : else
1148 : : {
1149 : 1946 : ScanKeyInit(&key[3],
1150 : : Anum_pg_shdepend_objsubid,
1151 : : BTEqualStrategyNumber, F_INT4EQ,
1152 : : Int32GetDatum(objsubId));
1153 : 1946 : nkeys = 4;
1154 : : }
1155 : :
1156 : 162880 : scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
1157 : : NULL, nkeys, key);
1158 : :
1159 [ + + ]: 168416 : while (HeapTupleIsValid(tup = systable_getnext(scan)))
1160 : : {
1161 : 5536 : Form_pg_shdepend shdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
1162 : :
1163 : : /* Filter entries according to additional parameters */
1164 [ + + - + ]: 5536 : if (OidIsValid(refclassId) && shdepForm->refclassid != refclassId)
1165 : 0 : continue;
1166 [ + + + + ]: 5536 : if (OidIsValid(refobjId) && shdepForm->refobjid != refobjId)
1167 : 655 : continue;
1168 [ + + ]: 4881 : if (deptype != SHARED_DEPENDENCY_INVALID &&
1169 [ + + ]: 565 : shdepForm->deptype != deptype)
1170 : 28 : continue;
1171 : :
1172 : : /* OK, delete it */
1173 : 4853 : CatalogTupleDelete(sdepRel, &tup->t_self);
1174 : : }
1175 : :
1176 : 162880 : systable_endscan(scan);
1177 : 162880 : }
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 : 168537 : classIdGetDbId(Oid classId)
1188 : : {
1189 : : Oid dbId;
1190 : :
1191 [ + + ]: 168537 : if (IsSharedRelation(classId))
1192 : 1176 : dbId = InvalidOid;
1193 : : else
1194 : 167361 : dbId = MyDatabaseId;
1195 : :
1196 : 168537 : return dbId;
1197 : : }
1198 : :
1199 : : /*
1200 : : * shdepLockAndCheckObject
1201 : : *
1202 : : * Lock the object that we are about to record a dependency on.
1203 : : * After it's locked, verify that it hasn't been dropped while we
1204 : : * weren't looking. If the object has been dropped, this function
1205 : : * does not return!
1206 : : */
1207 : : void
1208 : 6381 : shdepLockAndCheckObject(Oid classId, Oid objectId)
1209 : : {
1210 : : /* AccessShareLock should be OK, since we are not modifying the object */
1211 : 6381 : LockSharedObject(classId, objectId, 0, AccessShareLock);
1212 : :
1213 [ + + + - ]: 6381 : switch (classId)
1214 : : {
1215 : 5599 : case AuthIdRelationId:
1216 [ - + ]: 5599 : if (!SearchSysCacheExists1(AUTHOID, ObjectIdGetDatum(objectId)))
1217 [ # # ]: 0 : ereport(ERROR,
1218 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1219 : : errmsg("role %u was concurrently dropped",
1220 : : objectId)));
1221 : 5599 : break;
1222 : :
1223 : 102 : case TableSpaceRelationId:
1224 : : {
1225 : : /* For lack of a syscache on pg_tablespace, do this: */
1226 : 102 : char *tablespace = get_tablespace_name(objectId);
1227 : :
1228 [ - + ]: 102 : if (tablespace == NULL)
1229 [ # # ]: 0 : ereport(ERROR,
1230 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1231 : : errmsg("tablespace %u was concurrently dropped",
1232 : : objectId)));
1233 : 102 : pfree(tablespace);
1234 : 102 : break;
1235 : : }
1236 : :
1237 : 680 : case DatabaseRelationId:
1238 : : {
1239 : : /* For lack of a syscache on pg_database, do this: */
1240 : 680 : char *database = get_database_name(objectId);
1241 : :
1242 [ - + ]: 680 : if (database == NULL)
1243 [ # # ]: 0 : ereport(ERROR,
1244 : : (errcode(ERRCODE_UNDEFINED_OBJECT),
1245 : : errmsg("database %u was concurrently dropped",
1246 : : objectId)));
1247 : 680 : pfree(database);
1248 : 680 : break;
1249 : : }
1250 : :
1251 : :
1252 : 0 : default:
1253 [ # # ]: 0 : elog(ERROR, "unrecognized shared classId: %u", classId);
1254 : : }
1255 : 6381 : }
1256 : :
1257 : :
1258 : : /*
1259 : : * storeObjectDescription
1260 : : * Append the description of a dependent object to "descs"
1261 : : *
1262 : : * While searching for dependencies of a shared object, we stash the
1263 : : * descriptions of dependent objects we find in a single string, which we
1264 : : * later pass to ereport() in the DETAIL field when somebody attempts to
1265 : : * drop a referenced shared object.
1266 : : *
1267 : : * When type is LOCAL_OBJECT or SHARED_OBJECT, we expect object to be the
1268 : : * dependent object, deptype is the dependency type, and count is not used.
1269 : : * When type is REMOTE_OBJECT, we expect object to be the database object,
1270 : : * and count to be nonzero; deptype is not used in this case.
1271 : : */
1272 : : static void
1273 : 366 : storeObjectDescription(StringInfo descs,
1274 : : SharedDependencyObjectType type,
1275 : : ObjectAddress *object,
1276 : : SharedDependencyType deptype,
1277 : : int count)
1278 : : {
1279 : 366 : char *objdesc = getObjectDescription(object, false);
1280 : :
1281 : : /*
1282 : : * An object being dropped concurrently doesn't need to be reported.
1283 : : */
1284 [ - + ]: 366 : if (objdesc == NULL)
1285 : 0 : return;
1286 : :
1287 : : /* separate entries with a newline */
1288 [ + + ]: 366 : if (descs->len != 0)
1289 : 194 : appendStringInfoChar(descs, '\n');
1290 : :
1291 [ + - - ]: 366 : switch (type)
1292 : : {
1293 : 366 : case LOCAL_OBJECT:
1294 : : case SHARED_OBJECT:
1295 [ + + ]: 366 : if (deptype == SHARED_DEPENDENCY_OWNER)
1296 : 162 : appendStringInfo(descs, _("owner of %s"), objdesc);
1297 [ + + ]: 204 : else if (deptype == SHARED_DEPENDENCY_ACL)
1298 : 180 : appendStringInfo(descs, _("privileges for %s"), objdesc);
1299 [ - + ]: 24 : else if (deptype == SHARED_DEPENDENCY_INITACL)
1300 : 0 : appendStringInfo(descs, _("initial privileges for %s"), objdesc);
1301 [ + + ]: 24 : else if (deptype == SHARED_DEPENDENCY_POLICY)
1302 : 16 : appendStringInfo(descs, _("target of %s"), objdesc);
1303 [ + - ]: 8 : else if (deptype == SHARED_DEPENDENCY_TABLESPACE)
1304 : 8 : appendStringInfo(descs, _("tablespace for %s"), objdesc);
1305 : : else
1306 [ # # ]: 0 : elog(ERROR, "unrecognized dependency type: %d",
1307 : : (int) deptype);
1308 : 366 : break;
1309 : :
1310 : 0 : case REMOTE_OBJECT:
1311 : : /* translator: %s will always be "database %s" */
1312 : 0 : appendStringInfo(descs, ngettext("%d object in %s",
1313 : : "%d objects in %s",
1314 : : count),
1315 : : count, objdesc);
1316 : 0 : break;
1317 : :
1318 : 0 : default:
1319 [ # # ]: 0 : elog(ERROR, "unrecognized object type: %d", type);
1320 : : }
1321 : :
1322 : 366 : pfree(objdesc);
1323 : : }
1324 : :
1325 : :
1326 : : /*
1327 : : * shdepDropOwned
1328 : : *
1329 : : * Drop the objects owned by any one of the given RoleIds. If a role has
1330 : : * access to an object, the grant will be removed as well (but the object
1331 : : * will not, of course).
1332 : : *
1333 : : * We can revoke grants immediately while doing the scan, but drops are
1334 : : * saved up and done all at once with performMultipleDeletions. This
1335 : : * is necessary so that we don't get failures from trying to delete
1336 : : * interdependent objects in the wrong order.
1337 : : */
1338 : : void
1339 : 88 : shdepDropOwned(List *roleids, DropBehavior behavior)
1340 : : {
1341 : : Relation sdepRel;
1342 : : ListCell *cell;
1343 : : ObjectAddresses *deleteobjs;
1344 : :
1345 : 88 : deleteobjs = new_object_addresses();
1346 : :
1347 : : /*
1348 : : * We don't need this strong a lock here, but we'll call routines that
1349 : : * acquire RowExclusiveLock. Better get that right now to avoid potential
1350 : : * deadlock failures.
1351 : : */
1352 : 88 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1353 : :
1354 : : /*
1355 : : * For each role, find the dependent objects and drop them using the
1356 : : * regular (non-shared) dependency management.
1357 : : */
1358 [ + - + + : 190 : foreach(cell, roleids)
+ + ]
1359 : : {
1360 : 102 : Oid roleid = lfirst_oid(cell);
1361 : : ScanKeyData key[2];
1362 : : SysScanDesc scan;
1363 : : HeapTuple tuple;
1364 : :
1365 : : /* Doesn't work for pinned objects */
1366 [ - + ]: 102 : if (IsPinnedObject(AuthIdRelationId, roleid))
1367 : : {
1368 : : ObjectAddress obj;
1369 : :
1370 : 0 : obj.classId = AuthIdRelationId;
1371 : 0 : obj.objectId = roleid;
1372 : 0 : obj.objectSubId = 0;
1373 : :
1374 [ # # ]: 0 : ereport(ERROR,
1375 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1376 : : errmsg("cannot drop objects owned by %s because they are "
1377 : : "required by the database system",
1378 : : getObjectDescription(&obj, false))));
1379 : : }
1380 : :
1381 : 102 : ScanKeyInit(&key[0],
1382 : : Anum_pg_shdepend_refclassid,
1383 : : BTEqualStrategyNumber, F_OIDEQ,
1384 : : ObjectIdGetDatum(AuthIdRelationId));
1385 : 102 : ScanKeyInit(&key[1],
1386 : : Anum_pg_shdepend_refobjid,
1387 : : BTEqualStrategyNumber, F_OIDEQ,
1388 : : ObjectIdGetDatum(roleid));
1389 : :
1390 : 102 : scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1391 : : NULL, 2, key);
1392 : :
1393 [ + + ]: 560 : while ((tuple = systable_getnext(scan)) != NULL)
1394 : : {
1395 : 458 : Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1396 : : ObjectAddress obj;
1397 : :
1398 : : /*
1399 : : * We only operate on shared objects and objects in the current
1400 : : * database
1401 : : */
1402 [ + + ]: 458 : if (sdepForm->dbid != MyDatabaseId &&
1403 [ + + ]: 37 : sdepForm->dbid != InvalidOid)
1404 : 12 : continue;
1405 : :
1406 [ - + + + : 446 : switch (sdepForm->deptype)
+ - ]
1407 : : {
1408 : : /* Shouldn't happen */
1409 : 0 : case SHARED_DEPENDENCY_INVALID:
1410 [ # # ]: 0 : elog(ERROR, "unexpected dependency type");
1411 : : break;
1412 : 29 : case SHARED_DEPENDENCY_POLICY:
1413 : :
1414 : : /*
1415 : : * Try to remove role from policy; if unable to, remove
1416 : : * policy.
1417 : : */
1418 [ + + ]: 29 : if (!RemoveRoleFromObjectPolicy(roleid,
1419 : : sdepForm->classid,
1420 : : sdepForm->objid))
1421 : : {
1422 : 13 : obj.classId = sdepForm->classid;
1423 : 13 : obj.objectId = sdepForm->objid;
1424 : 13 : obj.objectSubId = sdepForm->objsubid;
1425 : :
1426 : : /*
1427 : : * Acquire lock on object, then verify this dependency
1428 : : * is still relevant. If not, the object might have
1429 : : * been dropped or the policy modified. Ignore the
1430 : : * object in that case.
1431 : : */
1432 : 13 : AcquireDeletionLock(&obj, 0);
1433 [ - + ]: 13 : if (!systable_recheck_tuple(scan, tuple))
1434 : : {
1435 : 0 : ReleaseDeletionLock(&obj);
1436 : 0 : break;
1437 : : }
1438 : 13 : add_exact_object_address(&obj, deleteobjs);
1439 : : }
1440 : 29 : break;
1441 : 166 : case SHARED_DEPENDENCY_ACL:
1442 : :
1443 : : /*
1444 : : * Dependencies on role grants are recorded using
1445 : : * SHARED_DEPENDENCY_ACL, but unlike a regular ACL list
1446 : : * which stores all permissions for a particular object in
1447 : : * a single ACL array, there's a separate catalog row for
1448 : : * each grant - so removing the grant just means removing
1449 : : * the entire row.
1450 : : */
1451 [ + + ]: 166 : if (sdepForm->classid != AuthMemRelationId)
1452 : : {
1453 : 160 : RemoveRoleFromObjectACL(roleid,
1454 : : sdepForm->classid,
1455 : : sdepForm->objid);
1456 : 160 : break;
1457 : : }
1458 : : pg_fallthrough;
1459 : :
1460 : : case SHARED_DEPENDENCY_OWNER:
1461 : :
1462 : : /*
1463 : : * Save it for deletion below, if it's a local object or a
1464 : : * role grant. Other shared objects, such as databases,
1465 : : * should not be removed here.
1466 : : */
1467 [ + + ]: 243 : if (sdepForm->dbid == MyDatabaseId ||
1468 [ + + ]: 7 : sdepForm->classid == AuthMemRelationId)
1469 : : {
1470 : 242 : obj.classId = sdepForm->classid;
1471 : 242 : obj.objectId = sdepForm->objid;
1472 : 242 : obj.objectSubId = sdepForm->objsubid;
1473 : : /* as above */
1474 : 242 : AcquireDeletionLock(&obj, 0);
1475 [ + + ]: 242 : if (!systable_recheck_tuple(scan, tuple))
1476 : : {
1477 : 1 : ReleaseDeletionLock(&obj);
1478 : 1 : break;
1479 : : }
1480 : 241 : add_exact_object_address(&obj, deleteobjs);
1481 : : }
1482 : 242 : break;
1483 : 14 : case SHARED_DEPENDENCY_INITACL:
1484 : :
1485 : : /*
1486 : : * Any mentions of the role that remain in pg_init_privs
1487 : : * entries are just dropped. This is the same policy as
1488 : : * we apply to regular ACLs.
1489 : : */
1490 : :
1491 : : /* Shouldn't see a role grant here */
1492 : : Assert(sdepForm->classid != AuthMemRelationId);
1493 : 14 : RemoveRoleFromInitPriv(roleid,
1494 : : sdepForm->classid,
1495 : : sdepForm->objid,
1496 : : sdepForm->objsubid);
1497 : 14 : break;
1498 : : }
1499 : : }
1500 : :
1501 : 102 : systable_endscan(scan);
1502 : : }
1503 : :
1504 : : /*
1505 : : * For stability of deletion-report ordering, sort the objects into
1506 : : * approximate reverse creation order before deletion. (This might also
1507 : : * make the deletion go a bit faster, since there's less chance of having
1508 : : * to rearrange the objects due to dependencies.)
1509 : : */
1510 : 88 : sort_object_addresses(deleteobjs);
1511 : :
1512 : : /* the dependency mechanism does the actual work */
1513 : 88 : performMultipleDeletions(deleteobjs, behavior, 0);
1514 : :
1515 : 84 : table_close(sdepRel, RowExclusiveLock);
1516 : :
1517 : 84 : free_object_addresses(deleteobjs);
1518 : 84 : }
1519 : :
1520 : : /*
1521 : : * shdepReassignOwned
1522 : : *
1523 : : * Change the owner of objects owned by any of the roles in roleids to
1524 : : * newrole. Grants are not touched.
1525 : : */
1526 : : void
1527 : 24 : shdepReassignOwned(List *roleids, Oid newrole)
1528 : : {
1529 : : Relation sdepRel;
1530 : : ListCell *cell;
1531 : :
1532 : : /*
1533 : : * We don't need this strong a lock here, but we'll call routines that
1534 : : * acquire RowExclusiveLock. Better get that right now to avoid potential
1535 : : * deadlock problems.
1536 : : */
1537 : 24 : sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
1538 : :
1539 [ + - + + : 48 : foreach(cell, roleids)
+ + ]
1540 : : {
1541 : : SysScanDesc scan;
1542 : : ScanKeyData key[2];
1543 : : HeapTuple tuple;
1544 : 24 : Oid roleid = lfirst_oid(cell);
1545 : :
1546 : : /* Refuse to work on pinned roles */
1547 [ - + ]: 24 : if (IsPinnedObject(AuthIdRelationId, roleid))
1548 : : {
1549 : : ObjectAddress obj;
1550 : :
1551 : 0 : obj.classId = AuthIdRelationId;
1552 : 0 : obj.objectId = roleid;
1553 : 0 : obj.objectSubId = 0;
1554 : :
1555 [ # # ]: 0 : ereport(ERROR,
1556 : : (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1557 : : errmsg("cannot reassign ownership of objects owned by %s because they are required by the database system",
1558 : : getObjectDescription(&obj, false))));
1559 : :
1560 : : /*
1561 : : * There's no need to tell the whole truth, which is that we
1562 : : * didn't track these dependencies at all ...
1563 : : */
1564 : : }
1565 : :
1566 : 24 : ScanKeyInit(&key[0],
1567 : : Anum_pg_shdepend_refclassid,
1568 : : BTEqualStrategyNumber, F_OIDEQ,
1569 : : ObjectIdGetDatum(AuthIdRelationId));
1570 : 24 : ScanKeyInit(&key[1],
1571 : : Anum_pg_shdepend_refobjid,
1572 : : BTEqualStrategyNumber, F_OIDEQ,
1573 : : ObjectIdGetDatum(roleid));
1574 : :
1575 : 24 : scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1576 : : NULL, 2, key);
1577 : :
1578 [ + + ]: 168 : while ((tuple = systable_getnext(scan)) != NULL)
1579 : : {
1580 : 144 : Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1581 : : MemoryContext cxt,
1582 : : oldcxt;
1583 : :
1584 : : /*
1585 : : * We only operate on shared objects and objects in the current
1586 : : * database
1587 : : */
1588 [ + + ]: 144 : if (sdepForm->dbid != MyDatabaseId &&
1589 [ + + ]: 24 : sdepForm->dbid != InvalidOid)
1590 : 2 : continue;
1591 : :
1592 : : /*
1593 : : * The various DDL routines called here tend to leak memory in
1594 : : * CurrentMemoryContext. That's not a problem when they're only
1595 : : * called once per command; but in this usage where we might be
1596 : : * touching many objects, it can amount to a serious memory leak.
1597 : : * Fix that by running each call in a short-lived context.
1598 : : */
1599 : 142 : cxt = AllocSetContextCreate(CurrentMemoryContext,
1600 : : "shdepReassignOwned",
1601 : : ALLOCSET_DEFAULT_SIZES);
1602 : 142 : oldcxt = MemoryContextSwitchTo(cxt);
1603 : :
1604 : : /* Perform the appropriate processing */
1605 [ + + + - ]: 142 : switch (sdepForm->deptype)
1606 : : {
1607 : 101 : case SHARED_DEPENDENCY_OWNER:
1608 : 101 : shdepReassignOwned_Owner(sdepForm, newrole);
1609 : 101 : break;
1610 : 12 : case SHARED_DEPENDENCY_INITACL:
1611 : 12 : shdepReassignOwned_InitAcl(sdepForm, roleid, newrole);
1612 : 12 : break;
1613 : 29 : case SHARED_DEPENDENCY_ACL:
1614 : : case SHARED_DEPENDENCY_POLICY:
1615 : : case SHARED_DEPENDENCY_TABLESPACE:
1616 : : /* Nothing to do for these entry types */
1617 : 29 : break;
1618 : 0 : default:
1619 [ # # ]: 0 : elog(ERROR, "unrecognized dependency type: %d",
1620 : : (int) sdepForm->deptype);
1621 : : break;
1622 : : }
1623 : :
1624 : : /* Clean up */
1625 : 142 : MemoryContextSwitchTo(oldcxt);
1626 : 142 : MemoryContextDelete(cxt);
1627 : :
1628 : : /* Make sure the next iteration will see my changes */
1629 : 142 : CommandCounterIncrement();
1630 : : }
1631 : :
1632 : 24 : systable_endscan(scan);
1633 : : }
1634 : :
1635 : 24 : table_close(sdepRel, RowExclusiveLock);
1636 : 24 : }
1637 : :
1638 : : /*
1639 : : * shdepReassignOwned_Owner
1640 : : *
1641 : : * shdepReassignOwned's processing of SHARED_DEPENDENCY_OWNER entries
1642 : : */
1643 : : static void
1644 : 101 : shdepReassignOwned_Owner(Form_pg_shdepend sdepForm, Oid newrole)
1645 : : {
1646 : : /* Issue the appropriate ALTER OWNER call */
1647 [ + + + + : 101 : switch (sdepForm->classid)
+ + - - -
+ + - ]
1648 : : {
1649 : 13 : case TypeRelationId:
1650 : 13 : AlterTypeOwner_oid(sdepForm->objid, newrole, true);
1651 : 13 : break;
1652 : :
1653 : 5 : case NamespaceRelationId:
1654 : 5 : AlterSchemaOwner_oid(sdepForm->objid, newrole);
1655 : 5 : break;
1656 : :
1657 : 48 : case RelationRelationId:
1658 : :
1659 : : /*
1660 : : * Pass recursing = true so that we don't fail on indexes, owned
1661 : : * sequences, etc when we happen to visit them before their parent
1662 : : * table.
1663 : : */
1664 : 48 : ATExecChangeOwner(sdepForm->objid, newrole, true, AccessExclusiveLock);
1665 : 48 : break;
1666 : :
1667 : 4 : case DefaultAclRelationId:
1668 : :
1669 : : /*
1670 : : * Ignore default ACLs; they should be handled by DROP OWNED, not
1671 : : * REASSIGN OWNED.
1672 : : */
1673 : 4 : break;
1674 : :
1675 : 9 : case UserMappingRelationId:
1676 : : /* ditto */
1677 : 9 : break;
1678 : :
1679 : 8 : case ForeignServerRelationId:
1680 : 8 : AlterForeignServerOwner_oid(sdepForm->objid, newrole);
1681 : 8 : break;
1682 : :
1683 : 0 : case ForeignDataWrapperRelationId:
1684 : 0 : AlterForeignDataWrapperOwner_oid(sdepForm->objid, newrole);
1685 : 0 : break;
1686 : :
1687 : 0 : case EventTriggerRelationId:
1688 : 0 : AlterEventTriggerOwner_oid(sdepForm->objid, newrole);
1689 : 0 : break;
1690 : :
1691 : 0 : case PublicationRelationId:
1692 : 0 : AlterPublicationOwner_oid(sdepForm->objid, newrole);
1693 : 0 : break;
1694 : :
1695 : 2 : case SubscriptionRelationId:
1696 : 2 : AlterSubscriptionOwner_oid(sdepForm->objid, newrole);
1697 : 2 : break;
1698 : :
1699 : : /* Generic alter owner cases */
1700 : 12 : case CollationRelationId:
1701 : : case ConversionRelationId:
1702 : : case OperatorRelationId:
1703 : : case ProcedureRelationId:
1704 : : case LanguageRelationId:
1705 : : case LargeObjectRelationId:
1706 : : case OperatorFamilyRelationId:
1707 : : case OperatorClassRelationId:
1708 : : case ExtensionRelationId:
1709 : : case StatisticExtRelationId:
1710 : : case TableSpaceRelationId:
1711 : : case DatabaseRelationId:
1712 : : case TSConfigRelationId:
1713 : : case TSDictionaryRelationId:
1714 : 12 : AlterObjectOwner_internal(sdepForm->classid,
1715 : : sdepForm->objid,
1716 : : newrole);
1717 : 12 : break;
1718 : :
1719 : 0 : default:
1720 [ # # ]: 0 : elog(ERROR, "unexpected classid %u", sdepForm->classid);
1721 : : break;
1722 : : }
1723 : 101 : }
1724 : :
1725 : : /*
1726 : : * shdepReassignOwned_InitAcl
1727 : : *
1728 : : * shdepReassignOwned's processing of SHARED_DEPENDENCY_INITACL entries
1729 : : */
1730 : : static void
1731 : 12 : shdepReassignOwned_InitAcl(Form_pg_shdepend sdepForm, Oid oldrole, Oid newrole)
1732 : : {
1733 : : /*
1734 : : * Currently, REASSIGN OWNED replaces mentions of oldrole with newrole in
1735 : : * pg_init_privs entries, just as it does in the object's regular ACL.
1736 : : * This is less than ideal, since pg_init_privs ought to retain a
1737 : : * historical record of the situation at the end of CREATE EXTENSION.
1738 : : * However, there are two big stumbling blocks to doing something
1739 : : * different:
1740 : : *
1741 : : * 1. If we don't replace the references, what is to happen if the old
1742 : : * role gets dropped? (DROP OWNED's current answer is to just delete the
1743 : : * pg_init_privs entry, which is surely ahistorical.)
1744 : : *
1745 : : * 2. It's unlikely that pg_dump will cope nicely with pg_init_privs
1746 : : * entries that are based on a different owner than the object now has ---
1747 : : * the more so given that pg_init_privs doesn't record the original owner
1748 : : * explicitly. (This problem actually exists anyway given that a bare
1749 : : * ALTER OWNER won't update pg_init_privs, but we don't need REASSIGN
1750 : : * OWNED making it worse.)
1751 : : */
1752 : 12 : ReplaceRoleInInitPriv(oldrole, newrole,
1753 : : sdepForm->classid,
1754 : : sdepForm->objid,
1755 : : sdepForm->objsubid);
1756 : 12 : }
|