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