1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_shdepend relation
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.22 2008/01/01 19:45:48 momjian Exp $
13 *-------------------------------------------------------------------------
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "access/xact.h"
20 #include "catalog/catalog.h"
21 #include "catalog/dependency.h"
22 #include "catalog/indexing.h"
23 #include "catalog/pg_authid.h"
24 #include "catalog/pg_conversion.h"
25 #include "catalog/pg_database.h"
26 #include "catalog/pg_language.h"
27 #include "catalog/pg_namespace.h"
28 #include "catalog/pg_operator.h"
29 #include "catalog/pg_proc.h"
30 #include "catalog/pg_shdepend.h"
31 #include "catalog/pg_tablespace.h"
32 #include "catalog/pg_type.h"
33 #include "commands/conversioncmds.h"
34 #include "commands/defrem.h"
35 #include "commands/schemacmds.h"
36 #include "commands/tablecmds.h"
37 #include "commands/typecmds.h"
38 #include "miscadmin.h"
39 #include "utils/acl.h"
40 #include "utils/fmgroids.h"
41 #include "utils/syscache.h"
51 static int getOidListDiff(Oid *list1, int nlist1, Oid *list2, int nlist2,
53 static Oid classIdGetDbId(Oid classId);
54 static void shdepLockAndCheckObject(Oid classId, Oid objectId);
55 static void shdepChangeDep(Relation sdepRel, Oid classid, Oid objid,
56 Oid refclassid, Oid refobjid,
57 SharedDependencyType deptype);
58 static void shdepAddDependency(Relation sdepRel, Oid classId, Oid objectId,
59 Oid refclassId, Oid refobjId,
60 SharedDependencyType deptype);
61 static void shdepDropDependency(Relation sdepRel, Oid classId, Oid objectId,
62 Oid refclassId, Oid refobjId,
63 SharedDependencyType deptype);
64 static void storeObjectDescription(StringInfo descs, objectType type,
65 ObjectAddress *object,
66 SharedDependencyType deptype,
68 static bool isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel);
72 * recordSharedDependencyOn
74 * Record a dependency between 2 objects via their respective ObjectAddresses.
75 * The first argument is the dependent object, the second the one it
76 * references (which must be a shared object).
78 * This locks the referenced object and makes sure it still exists.
79 * Then it creates an entry in pg_shdepend. The lock is kept until
80 * the end of the transaction.
82 * Dependencies on pinned objects are not recorded.
85 recordSharedDependencyOn(ObjectAddress *depender,
86 ObjectAddress *referenced,
87 SharedDependencyType deptype)
92 * Objects in pg_shdepend can't have SubIds.
94 Assert(depender->objectSubId == 0);
95 Assert(referenced->objectSubId == 0);
98 * During bootstrap, do nothing since pg_shdepend may not exist yet.
99 * initdb will fill in appropriate pg_shdepend entries after bootstrap.
101 if (IsBootstrapProcessingMode())
104 sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
106 /* If the referenced object is pinned, do nothing. */
107 if (!isSharedObjectPinned(referenced->classId, referenced->objectId,
110 shdepAddDependency(sdepRel, depender->classId, depender->objectId,
111 referenced->classId, referenced->objectId,
115 heap_close(sdepRel, RowExclusiveLock);
119 * recordDependencyOnOwner
121 * A convenient wrapper of recordSharedDependencyOn -- register the specified
122 * user as owner of the given object.
124 * Note: it's the caller's responsibility to ensure that there isn't an owner
125 * entry for the object already.
128 recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
130 ObjectAddress myself,
133 myself.classId = classId;
134 myself.objectId = objectId;
135 myself.objectSubId = 0;
137 referenced.classId = AuthIdRelationId;
138 referenced.objectId = owner;
139 referenced.objectSubId = 0;
141 recordSharedDependencyOn(&myself, &referenced, SHARED_DEPENDENCY_OWNER);
147 * Update shared dependency records to account for an updated referenced
148 * object. This is an internal workhorse for operations such as changing
151 * There must be no more than one existing entry for the given dependent
152 * object and dependency type! So in practice this can only be used for
153 * updating SHARED_DEPENDENCY_OWNER entries, which should have that property.
155 * If there is no previous entry, we assume it was referencing a PINned
156 * object, so we create a new entry. If the new referenced object is
157 * PINned, we don't create an entry (and drop the old one, if any).
159 * sdepRel must be the pg_shdepend relation, already opened and suitably
163 shdepChangeDep(Relation sdepRel, Oid classid, Oid objid,
164 Oid refclassid, Oid refobjid,
165 SharedDependencyType deptype)
167 Oid dbid = classIdGetDbId(classid);
168 HeapTuple oldtup = NULL;
174 * Make sure the new referenced object doesn't go away while we record the
177 shdepLockAndCheckObject(refclassid, refobjid);
180 * Look for a previous entry
183 Anum_pg_shdepend_dbid,
184 BTEqualStrategyNumber, F_OIDEQ,
185 ObjectIdGetDatum(dbid));
187 Anum_pg_shdepend_classid,
188 BTEqualStrategyNumber, F_OIDEQ,
189 ObjectIdGetDatum(classid));
191 Anum_pg_shdepend_objid,
192 BTEqualStrategyNumber, F_OIDEQ,
193 ObjectIdGetDatum(objid));
195 scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
196 SnapshotNow, 3, key);
198 while ((scantup = systable_getnext(scan)) != NULL)
200 /* Ignore if not of the target dependency type */
201 if (((Form_pg_shdepend) GETSTRUCT(scantup))->deptype != deptype)
203 /* Caller screwed up if multiple matches */
206 "multiple pg_shdepend entries for object %u/%u deptype %c",
207 classid, objid, deptype);
208 oldtup = heap_copytuple(scantup);
211 systable_endscan(scan);
213 if (isSharedObjectPinned(refclassid, refobjid, sdepRel))
215 /* No new entry needed, so just delete existing entry if any */
217 simple_heap_delete(sdepRel, &oldtup->t_self);
221 /* Need to update existing entry */
222 Form_pg_shdepend shForm = (Form_pg_shdepend) GETSTRUCT(oldtup);
224 /* Since oldtup is a copy, we can just modify it in-memory */
225 shForm->refclassid = refclassid;
226 shForm->refobjid = refobjid;
228 simple_heap_update(sdepRel, &oldtup->t_self, oldtup);
230 /* keep indexes current */
231 CatalogUpdateIndexes(sdepRel, oldtup);
235 /* Need to insert new entry */
236 Datum values[Natts_pg_shdepend];
237 bool nulls[Natts_pg_shdepend];
239 memset(nulls, 0, sizeof(nulls));
241 values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(dbid);
242 values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classid);
243 values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objid);
245 values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassid);
246 values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjid);
247 values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);
250 * we are reusing oldtup just to avoid declaring a new variable, but
251 * it's certainly a new tuple
253 oldtup = heap_form_tuple(RelationGetDescr(sdepRel), values, nulls);
254 simple_heap_insert(sdepRel, oldtup);
256 /* keep indexes current */
257 CatalogUpdateIndexes(sdepRel, oldtup);
261 heap_freetuple(oldtup);
265 * changeDependencyOnOwner
267 * Update the shared dependencies to account for the new owner.
270 changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
274 sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
276 /* Adjust the SHARED_DEPENDENCY_OWNER entry */
277 shdepChangeDep(sdepRel, classId, objectId,
278 AuthIdRelationId, newOwnerId,
279 SHARED_DEPENDENCY_OWNER);
282 * There should never be a SHARED_DEPENDENCY_ACL entry for the owner,
283 * so get rid of it if there is one. This can happen if the new owner
284 * was previously granted some rights to the object.
286 * This step is analogous to aclnewowner's removal of duplicate entries
287 * in the ACL. We have to do it to handle this scenario:
288 * A grants some rights on an object to B
289 * ALTER OWNER changes the object's owner to B
290 * ALTER OWNER changes the object's owner to C
291 * The third step would remove all mention of B from the object's ACL,
292 * but we'd still have a SHARED_DEPENDENCY_ACL for B if we did not do
295 * The rule against having a SHARED_DEPENDENCY_ACL entry for the owner
296 * allows us to fix things up in just this one place, without having
297 * to make the various ALTER OWNER routines each know about it.
300 shdepDropDependency(sdepRel, classId, objectId,
301 AuthIdRelationId, newOwnerId,
302 SHARED_DEPENDENCY_ACL);
304 heap_close(sdepRel, RowExclusiveLock);
309 * Helper for updateAclDependencies.
311 * Takes two Oid arrays and returns elements from the first not found in the
312 * second. We assume both arrays are sorted and de-duped, and that the
313 * second array does not contain any values not found in the first.
315 * NOTE: Both input arrays are pfreed.
318 getOidListDiff(Oid *list1, int nlist1, Oid *list2, int nlist2, Oid **diff)
325 AssertArg(nlist1 >= nlist2 && nlist2 >= 0);
327 result = palloc(sizeof(Oid) * (nlist1 - nlist2));
330 for (i = 0, j = 0; i < nlist1 && j < nlist2;)
332 if (list1[i] == list2[j])
337 else if (list1[i] < list2[j])
339 result[k++] = list1[i];
345 elog(WARNING, "invalid element %u in shorter list", list2[j]);
350 for (; i < nlist1; i++)
351 result[k++] = list1[i];
353 /* We should have copied the exact number of elements */
354 AssertState(k == (nlist1 - nlist2));
365 * updateAclDependencies
366 * Update the pg_shdepend info for an object's ACL during GRANT/REVOKE.
368 * classId, objectId: identify the object whose ACL this is
369 * ownerId: role owning the object
370 * isGrant: are we adding or removing ACL entries?
371 * noldmembers, oldmembers: array of roleids appearing in old ACL
372 * nnewmembers, newmembers: array of roleids appearing in new ACL
374 * We calculate the difference between the new and old lists of roles,
375 * and then insert (if it's a grant) or delete (if it's a revoke) from
376 * pg_shdepend as appropiate.
378 * Note that we can't insert blindly at grant, because we would end up with
379 * duplicate registered dependencies. We could check for existence of the
380 * tuple before inserting, but that seems to be more expensive than what we are
381 * doing now. On the other hand, we can't just delete the tuples blindly at
382 * revoke, because the user may still have other privileges.
384 * NOTE: Both input arrays must be sorted and de-duped. They are pfreed
388 updateAclDependencies(Oid classId, Oid objectId, Oid ownerId, bool isGrant,
389 int noldmembers, Oid *oldmembers,
390 int nnewmembers, Oid *newmembers)
398 * Calculate the differences between the old and new lists.
401 ndiff = getOidListDiff(newmembers, nnewmembers,
402 oldmembers, noldmembers, &diff);
404 ndiff = getOidListDiff(oldmembers, noldmembers,
405 newmembers, nnewmembers, &diff);
409 sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
411 /* Add or drop the respective dependency */
412 for (i = 0; i < ndiff; i++)
414 Oid roleid = diff[i];
417 * Skip the owner: he has an OWNER shdep entry instead. (This is
418 * not just a space optimization; it makes ALTER OWNER easier. See
419 * notes in changeDependencyOnOwner.)
421 if (roleid == ownerId)
424 /* Skip pinned roles */
425 if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
429 shdepAddDependency(sdepRel, classId, objectId,
430 AuthIdRelationId, roleid,
431 SHARED_DEPENDENCY_ACL);
433 shdepDropDependency(sdepRel, classId, objectId,
434 AuthIdRelationId, roleid,
435 SHARED_DEPENDENCY_ACL);
438 heap_close(sdepRel, RowExclusiveLock);
445 * A struct to keep track of dependencies found in other databases.
454 * checkSharedDependencies
456 * Check whether there are shared dependency entries for a given shared
457 * object. Returns a string containing a newline-separated list of object
458 * descriptions that depend on the shared object, or NULL if none is found.
459 * The size of the returned string is limited to about MAX_REPORTED_DEPS lines;
460 * if there are more objects than that, the output is returned truncated at
461 * that point while the full message is logged to the postmaster log.
463 * We can find three different kinds of dependencies: dependencies on objects
464 * of the current database; dependencies on shared objects; and dependencies
465 * on objects local to other databases. We can (and do) provide descriptions
466 * of the two former kinds of objects, but we can't do that for "remote"
467 * objects, so we just provide a count of them.
469 * If we find a SHARED_DEPENDENCY_PIN entry, we can error out early.
472 checkSharedDependencies(Oid classId, Oid objectId)
478 int numReportedDeps = 0;
479 int numNotReportedDeps = 0;
480 int numNotReportedDbs = 0;
483 ObjectAddress object;
484 StringInfoData descs;
485 StringInfoData alldescs;
488 * We limit the number of dependencies reported to the client to
489 * MAX_REPORTED_DEPS, since client software may not deal well with
490 * enormous error strings. The server log always gets a full report,
491 * which is collected in a separate StringInfo if and only if we detect
492 * that the client report is going to be truncated.
494 #define MAX_REPORTED_DEPS 100
496 initStringInfo(&descs);
497 initStringInfo(&alldescs);
499 sdepRel = heap_open(SharedDependRelationId, AccessShareLock);
502 Anum_pg_shdepend_refclassid,
503 BTEqualStrategyNumber, F_OIDEQ,
504 ObjectIdGetDatum(classId));
506 Anum_pg_shdepend_refobjid,
507 BTEqualStrategyNumber, F_OIDEQ,
508 ObjectIdGetDatum(objectId));
510 scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
511 SnapshotNow, 2, key);
513 while (HeapTupleIsValid(tup = systable_getnext(scan)))
515 Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
517 /* This case can be dispatched quickly */
518 if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
520 object.classId = classId;
521 object.objectId = objectId;
522 object.objectSubId = 0;
524 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
525 errmsg("cannot drop %s because it is required by the database system",
526 getObjectDescription(&object))));
529 object.classId = sdepForm->classid;
530 object.objectId = sdepForm->objid;
531 object.objectSubId = 0;
534 * If it's a dependency local to this database or it's a shared
535 * object, describe it.
537 * If it's a remote dependency, keep track of it so we can report the
538 * number of them later.
540 if (sdepForm->dbid == MyDatabaseId)
542 if (numReportedDeps < MAX_REPORTED_DEPS)
545 storeObjectDescription(&descs, LOCAL_OBJECT, &object,
546 sdepForm->deptype, 0);
550 numNotReportedDeps++;
551 /* initialize the server-only log line */
552 if (alldescs.len == 0)
553 appendBinaryStringInfo(&alldescs, descs.data, descs.len);
555 storeObjectDescription(&alldescs, LOCAL_OBJECT, &object,
556 sdepForm->deptype, 0);
559 else if (sdepForm->dbid == InvalidOid)
561 if (numReportedDeps < MAX_REPORTED_DEPS)
564 storeObjectDescription(&descs, SHARED_OBJECT, &object,
565 sdepForm->deptype, 0);
569 numNotReportedDeps++;
570 /* initialize the server-only log line */
571 if (alldescs.len == 0)
572 appendBinaryStringInfo(&alldescs, descs.data, descs.len);
574 storeObjectDescription(&alldescs, SHARED_OBJECT, &object,
575 sdepForm->deptype, 0);
580 /* It's not local nor shared, so it must be remote. */
585 * XXX this info is kept on a simple List. Maybe it's not good
586 * for performance, but using a hash table seems needlessly
587 * complex. The expected number of databases is not high anyway,
590 foreach(cell, remDeps)
593 if (dep->dbOid == sdepForm->dbid)
602 dep = (remoteDep *) palloc(sizeof(remoteDep));
603 dep->dbOid = sdepForm->dbid;
605 remDeps = lappend(remDeps, dep);
610 systable_endscan(scan);
612 heap_close(sdepRel, AccessShareLock);
615 * Report dependencies on remote databases. If we're truncating the
616 * output already, don't put a line per database, but a single one for all
617 * of them. Otherwise add as many as fit in MAX_REPORTED_DEPS.
619 foreach(cell, remDeps)
621 remoteDep *dep = lfirst(cell);
623 object.classId = DatabaseRelationId;
624 object.objectId = dep->dbOid;
625 object.objectSubId = 0;
627 if (numReportedDeps < MAX_REPORTED_DEPS)
630 storeObjectDescription(&descs, REMOTE_OBJECT, &object,
631 SHARED_DEPENDENCY_INVALID, dep->count);
636 /* initialize the server-only log line */
637 if (alldescs.len == 0)
638 appendBinaryStringInfo(&alldescs, descs.data, descs.len);
640 storeObjectDescription(&alldescs, REMOTE_OBJECT, &object,
641 SHARED_DEPENDENCY_INVALID, dep->count);
645 list_free_deep(remDeps);
650 pfree(alldescs.data);
654 if (numNotReportedDeps > 0)
655 appendStringInfo(&descs, _("\nand %d other objects "
656 "(see server log for list)"),
658 if (numNotReportedDbs > 0)
659 appendStringInfo(&descs, _("\nand objects in %d other databases "
660 "(see server log for list)"),
663 if (numNotReportedDeps > 0 || numNotReportedDbs > 0)
667 obj.classId = classId;
668 obj.objectId = objectId;
671 (errmsg("there are objects dependent on %s",
672 getObjectDescription(&obj)),
673 errdetail(alldescs.data)));
676 pfree(alldescs.data);
682 * copyTemplateDependencies
684 * Routine to create the initial shared dependencies of a new database.
685 * We simply copy the dependencies from the template database.
688 copyTemplateDependencies(Oid templateDbId, Oid newDbId)
695 CatalogIndexState indstate;
696 Datum values[Natts_pg_shdepend];
697 bool nulls[Natts_pg_shdepend];
698 bool replace[Natts_pg_shdepend];
700 sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
701 sdepDesc = RelationGetDescr(sdepRel);
703 indstate = CatalogOpenIndexes(sdepRel);
705 /* Scan all entries with dbid = templateDbId */
707 Anum_pg_shdepend_dbid,
708 BTEqualStrategyNumber, F_OIDEQ,
709 ObjectIdGetDatum(templateDbId));
711 scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
712 SnapshotNow, 1, key);
714 /* Set up to copy the tuples except for inserting newDbId */
715 memset(values, 0, sizeof(values));
716 memset(nulls, false, sizeof(nulls));
717 memset(replace, false, sizeof(replace));
719 replace[Anum_pg_shdepend_dbid - 1] = true;
720 values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(newDbId);
723 * Copy the entries of the original database, changing the database Id to
724 * that of the new database. Note that because we are not copying rows
725 * with dbId == 0 (ie, rows describing dependent shared objects) we won't
726 * copy the ownership dependency of the template database itself; this is
729 while (HeapTupleIsValid(tup = systable_getnext(scan)))
733 newtup = heap_modify_tuple(tup, sdepDesc, values, nulls, replace);
734 simple_heap_insert(sdepRel, newtup);
736 /* Keep indexes current */
737 CatalogIndexInsert(indstate, newtup);
739 heap_freetuple(newtup);
742 systable_endscan(scan);
744 CatalogCloseIndexes(indstate);
745 heap_close(sdepRel, RowExclusiveLock);
749 * dropDatabaseDependencies
751 * Delete pg_shdepend entries corresponding to a database that's being
755 dropDatabaseDependencies(Oid databaseId)
762 sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
765 * First, delete all the entries that have the database Oid in the dbid
769 Anum_pg_shdepend_dbid,
770 BTEqualStrategyNumber, F_OIDEQ,
771 ObjectIdGetDatum(databaseId));
772 /* We leave the other index fields unspecified */
774 scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
775 SnapshotNow, 1, key);
777 while (HeapTupleIsValid(tup = systable_getnext(scan)))
779 simple_heap_delete(sdepRel, &tup->t_self);
782 systable_endscan(scan);
784 /* Now delete all entries corresponding to the database itself */
785 shdepDropDependency(sdepRel, DatabaseRelationId, databaseId,
786 InvalidOid, InvalidOid,
787 SHARED_DEPENDENCY_INVALID);
789 heap_close(sdepRel, RowExclusiveLock);
793 * deleteSharedDependencyRecordsFor
795 * Delete all pg_shdepend entries corresponding to an object that's being
796 * dropped or modified. The object is assumed to be either a shared object
797 * or local to the current database (the classId tells us which).
800 deleteSharedDependencyRecordsFor(Oid classId, Oid objectId)
804 sdepRel = heap_open(SharedDependRelationId, RowExclusiveLock);
806 shdepDropDependency(sdepRel, classId, objectId,
807 InvalidOid, InvalidOid,
808 SHARED_DEPENDENCY_INVALID);
810 heap_close(sdepRel, RowExclusiveLock);
815 * Internal workhorse for inserting into pg_shdepend
817 * sdepRel must be the pg_shdepend relation, already opened and suitably
821 shdepAddDependency(Relation sdepRel, Oid classId, Oid objectId,
822 Oid refclassId, Oid refobjId,
823 SharedDependencyType deptype)
826 Datum values[Natts_pg_shdepend];
827 bool nulls[Natts_pg_shdepend];
830 * Make sure the object doesn't go away while we record the dependency on
831 * it. DROP routines should lock the object exclusively before they check
832 * shared dependencies.
834 shdepLockAndCheckObject(refclassId, refobjId);
836 memset(nulls, false, sizeof(nulls));
839 * Form the new tuple and record the dependency.
841 values[Anum_pg_shdepend_dbid - 1] = ObjectIdGetDatum(classIdGetDbId(classId));
842 values[Anum_pg_shdepend_classid - 1] = ObjectIdGetDatum(classId);
843 values[Anum_pg_shdepend_objid - 1] = ObjectIdGetDatum(objectId);
845 values[Anum_pg_shdepend_refclassid - 1] = ObjectIdGetDatum(refclassId);
846 values[Anum_pg_shdepend_refobjid - 1] = ObjectIdGetDatum(refobjId);
847 values[Anum_pg_shdepend_deptype - 1] = CharGetDatum(deptype);
849 tup = heap_form_tuple(sdepRel->rd_att, values, nulls);
851 simple_heap_insert(sdepRel, tup);
853 /* keep indexes current */
854 CatalogUpdateIndexes(sdepRel, tup);
861 * shdepDropDependency
862 * Internal workhorse for deleting entries from pg_shdepend.
864 * We drop entries having the following properties:
865 * dependent object is the one identified by classId/objectId
866 * if refclassId isn't InvalidOid, it must match the entry's refclassid
867 * if refobjId isn't InvalidOid, it must match the entry's refobjid
868 * if deptype isn't SHARED_DEPENDENCY_INVALID, it must match entry's deptype
870 * sdepRel must be the pg_shdepend relation, already opened and suitably
874 shdepDropDependency(Relation sdepRel, Oid classId, Oid objectId,
875 Oid refclassId, Oid refobjId,
876 SharedDependencyType deptype)
882 /* Scan for entries matching the dependent object */
884 Anum_pg_shdepend_dbid,
885 BTEqualStrategyNumber, F_OIDEQ,
886 ObjectIdGetDatum(classIdGetDbId(classId)));
888 Anum_pg_shdepend_classid,
889 BTEqualStrategyNumber, F_OIDEQ,
890 ObjectIdGetDatum(classId));
892 Anum_pg_shdepend_objid,
893 BTEqualStrategyNumber, F_OIDEQ,
894 ObjectIdGetDatum(objectId));
896 scan = systable_beginscan(sdepRel, SharedDependDependerIndexId, true,
897 SnapshotNow, 3, key);
899 while (HeapTupleIsValid(tup = systable_getnext(scan)))
901 Form_pg_shdepend shdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
903 /* Filter entries according to additional parameters */
904 if (OidIsValid(refclassId) && shdepForm->refclassid != refclassId)
906 if (OidIsValid(refobjId) && shdepForm->refobjid != refobjId)
908 if (deptype != SHARED_DEPENDENCY_INVALID &&
909 shdepForm->deptype != deptype)
913 simple_heap_delete(sdepRel, &tup->t_self);
916 systable_endscan(scan);
922 * Get the database Id that should be used in pg_shdepend, given the OID
923 * of the catalog containing the object. For shared objects, it's 0
924 * (InvalidOid); for all other objects, it's the current database Id.
927 classIdGetDbId(Oid classId)
931 if (IsSharedRelation(classId))
940 * shdepLockAndCheckObject
942 * Lock the object that we are about to record a dependency on.
943 * After it's locked, verify that it hasn't been dropped while we
944 * weren't looking. If the object has been dropped, this function
948 shdepLockAndCheckObject(Oid classId, Oid objectId)
950 /* AccessShareLock should be OK, since we are not modifying the object */
951 LockSharedObject(classId, objectId, 0, AccessShareLock);
955 case AuthIdRelationId:
956 if (!SearchSysCacheExists(AUTHOID,
957 ObjectIdGetDatum(objectId),
960 (errcode(ERRCODE_UNDEFINED_OBJECT),
961 errmsg("role %u was concurrently dropped",
966 * Currently, this routine need not support any other shared
967 * object types besides roles. If we wanted to record explicit
968 * dependencies on databases or tablespaces, we'd need code along
972 case TableSpaceRelationId:
974 /* For lack of a syscache on pg_tablespace, do this: */
975 char *tablespace = get_tablespace_name(objectId);
977 if (tablespace == NULL)
979 (errcode(ERRCODE_UNDEFINED_OBJECT),
980 errmsg("tablespace %u was concurrently dropped",
988 elog(ERROR, "unrecognized shared classId: %u", classId);
994 * storeObjectDescription
995 * Append the description of a dependent object to "descs"
997 * While searching for dependencies of a shared object, we stash the
998 * descriptions of dependent objects we find in a single string, which we
999 * later pass to ereport() in the DETAIL field when somebody attempts to
1000 * drop a referenced shared object.
1002 * When type is LOCAL_OBJECT or SHARED_OBJECT, we expect object to be the
1003 * dependent object, deptype is the dependency type, and count is not used.
1004 * When type is REMOTE_OBJECT, we expect object to be the database object,
1005 * and count to be nonzero; deptype is not used in this case.
1008 storeObjectDescription(StringInfo descs, objectType type,
1009 ObjectAddress *object,
1010 SharedDependencyType deptype,
1013 char *objdesc = getObjectDescription(object);
1015 /* separate entries with a newline */
1016 if (descs->len != 0)
1017 appendStringInfoChar(descs, '\n');
1023 if (deptype == SHARED_DEPENDENCY_OWNER)
1024 appendStringInfo(descs, _("owner of %s"), objdesc);
1025 else if (deptype == SHARED_DEPENDENCY_ACL)
1026 appendStringInfo(descs, _("access to %s"), objdesc);
1028 elog(ERROR, "unrecognized dependency type: %d",
1033 /* translator: %s will always be "database %s" */
1034 appendStringInfo(descs, _("%d objects in %s"), count, objdesc);
1038 elog(ERROR, "unrecognized object type: %d", type);
1046 * isSharedObjectPinned
1047 * Return whether a given shared object has a SHARED_DEPENDENCY_PIN entry.
1049 * sdepRel must be the pg_shdepend relation, already opened and suitably
1053 isSharedObjectPinned(Oid classId, Oid objectId, Relation sdepRel)
1055 bool result = false;
1060 ScanKeyInit(&key[0],
1061 Anum_pg_shdepend_refclassid,
1062 BTEqualStrategyNumber, F_OIDEQ,
1063 ObjectIdGetDatum(classId));
1064 ScanKeyInit(&key[1],
1065 Anum_pg_shdepend_refobjid,
1066 BTEqualStrategyNumber, F_OIDEQ,
1067 ObjectIdGetDatum(objectId));
1069 scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1070 SnapshotNow, 2, key);
1073 * Since we won't generate additional pg_shdepend entries for pinned
1074 * objects, there can be at most one entry referencing a pinned object.
1075 * Hence, it's sufficient to look at the first returned tuple; we don't
1078 tup = systable_getnext(scan);
1079 if (HeapTupleIsValid(tup))
1081 Form_pg_shdepend shdepForm = (Form_pg_shdepend) GETSTRUCT(tup);
1083 if (shdepForm->deptype == SHARED_DEPENDENCY_PIN)
1087 systable_endscan(scan);
1095 * Drop the objects owned by any one of the given RoleIds. If a role has
1096 * access to an object, the grant will be removed as well (but the object
1097 * will not, of course.)
1099 * We can revoke grants immediately while doing the scan, but drops are
1100 * saved up and done all at once with performMultipleDeletions. This
1101 * is necessary so that we don't get failures from trying to delete
1102 * interdependent objects in the wrong order.
1105 shdepDropOwned(List *roleids, DropBehavior behavior)
1109 ObjectAddresses *deleteobjs;
1111 deleteobjs = new_object_addresses();
1113 sdepRel = heap_open(SharedDependRelationId, AccessExclusiveLock);
1116 * For each role, find the dependent objects and drop them using the
1117 * regular (non-shared) dependency management.
1119 foreach(cell, roleids)
1121 Oid roleid = lfirst_oid(cell);
1126 /* Doesn't work for pinned objects */
1127 if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
1131 obj.classId = AuthIdRelationId;
1132 obj.objectId = roleid;
1133 obj.objectSubId = 0;
1136 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1137 errmsg("cannot drop objects owned by %s because they are "
1138 "required by the database system",
1139 getObjectDescription(&obj))));
1142 ScanKeyInit(&key[0],
1143 Anum_pg_shdepend_refclassid,
1144 BTEqualStrategyNumber, F_OIDEQ,
1145 ObjectIdGetDatum(AuthIdRelationId));
1146 ScanKeyInit(&key[1],
1147 Anum_pg_shdepend_refobjid,
1148 BTEqualStrategyNumber, F_OIDEQ,
1149 ObjectIdGetDatum(roleid));
1151 scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1152 SnapshotNow, 2, key);
1154 while ((tuple = systable_getnext(scan)) != NULL)
1157 GrantObjectType objtype;
1158 InternalGrant istmt;
1159 Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1161 /* We only operate on objects in the current database */
1162 if (sdepForm->dbid != MyDatabaseId)
1165 switch (sdepForm->deptype)
1167 /* Shouldn't happen */
1168 case SHARED_DEPENDENCY_PIN:
1169 case SHARED_DEPENDENCY_INVALID:
1170 elog(ERROR, "unexpected dependency type");
1172 case SHARED_DEPENDENCY_ACL:
1173 switch (sdepForm->classid)
1175 case RelationRelationId:
1176 /* it's OK to use RELATION for a sequence */
1177 istmt.objtype = ACL_OBJECT_RELATION;
1179 case DatabaseRelationId:
1180 istmt.objtype = ACL_OBJECT_DATABASE;
1182 case ProcedureRelationId:
1183 istmt.objtype = ACL_OBJECT_FUNCTION;
1185 case LanguageRelationId:
1186 istmt.objtype = ACL_OBJECT_LANGUAGE;
1188 case NamespaceRelationId:
1189 istmt.objtype = ACL_OBJECT_NAMESPACE;
1191 case TableSpaceRelationId:
1192 istmt.objtype = ACL_OBJECT_TABLESPACE;
1195 elog(ERROR, "unexpected object type %d",
1197 /* keep compiler quiet */
1198 objtype = (GrantObjectType) 0;
1201 istmt.is_grant = false;
1202 istmt.objects = list_make1_oid(sdepForm->objid);
1203 istmt.all_privs = true;
1204 istmt.privileges = ACL_NO_RIGHTS;
1205 istmt.grantees = list_make1_oid(roleid);
1206 istmt.grant_option = false;
1207 istmt.behavior = DROP_CASCADE;
1209 ExecGrantStmt_oids(&istmt);
1211 case SHARED_DEPENDENCY_OWNER:
1212 /* Save it for deletion below */
1213 obj.classId = sdepForm->classid;
1214 obj.objectId = sdepForm->objid;
1215 obj.objectSubId = 0;
1216 add_exact_object_address(&obj, deleteobjs);
1221 systable_endscan(scan);
1224 /* the dependency mechanism does the actual work */
1225 performMultipleDeletions(deleteobjs, behavior);
1227 heap_close(sdepRel, AccessExclusiveLock);
1229 free_object_addresses(deleteobjs);
1233 * shdepReassignOwned
1235 * Change the owner of objects owned by any of the roles in roleids to
1236 * newrole. Grants are not touched.
1239 shdepReassignOwned(List *roleids, Oid newrole)
1244 sdepRel = heap_open(SharedDependRelationId, AccessShareLock);
1246 foreach(cell, roleids)
1251 Oid roleid = lfirst_oid(cell);
1253 /* Refuse to work on pinned roles */
1254 if (isSharedObjectPinned(AuthIdRelationId, roleid, sdepRel))
1258 obj.classId = AuthIdRelationId;
1259 obj.objectId = roleid;
1260 obj.objectSubId = 0;
1263 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
1264 errmsg("cannot drop objects owned by %s because they are "
1265 "required by the database system",
1266 getObjectDescription(&obj))));
1269 * There's no need to tell the whole truth, which is that we
1270 * didn't track these dependencies at all ...
1274 ScanKeyInit(&key[0],
1275 Anum_pg_shdepend_refclassid,
1276 BTEqualStrategyNumber, F_OIDEQ,
1277 ObjectIdGetDatum(AuthIdRelationId));
1278 ScanKeyInit(&key[1],
1279 Anum_pg_shdepend_refobjid,
1280 BTEqualStrategyNumber, F_OIDEQ,
1281 ObjectIdGetDatum(roleid));
1283 scan = systable_beginscan(sdepRel, SharedDependReferenceIndexId, true,
1284 SnapshotNow, 2, key);
1286 while ((tuple = systable_getnext(scan)) != NULL)
1288 Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1290 /* We only operate on objects in the current database */
1291 if (sdepForm->dbid != MyDatabaseId)
1294 /* Unexpected because we checked for pins above */
1295 if (sdepForm->deptype == SHARED_DEPENDENCY_PIN)
1296 elog(ERROR, "unexpected shared pin");
1298 /* We leave non-owner dependencies alone */
1299 if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
1302 /* Issue the appropriate ALTER OWNER call */
1303 switch (sdepForm->classid)
1305 case ConversionRelationId:
1306 AlterConversionOwner_oid(sdepForm->objid, newrole);
1309 case TypeRelationId:
1310 AlterTypeOwnerInternal(sdepForm->objid, newrole, true);
1313 case OperatorRelationId:
1314 AlterOperatorOwner_oid(sdepForm->objid, newrole);
1317 case NamespaceRelationId:
1318 AlterSchemaOwner_oid(sdepForm->objid, newrole);
1321 case RelationRelationId:
1324 * Pass recursing = true so that we don't fail on indexes,
1325 * owned sequences, etc when we happen to visit them
1326 * before their parent table.
1328 ATExecChangeOwner(sdepForm->objid, newrole, true);
1331 case ProcedureRelationId:
1332 AlterFunctionOwner_oid(sdepForm->objid, newrole);
1336 elog(ERROR, "unexpected classid %d", sdepForm->classid);
1339 /* Make sure the next iteration will see my changes */
1340 CommandCounterIncrement();
1343 systable_endscan(scan);
1346 heap_close(sdepRel, AccessShareLock);