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