LCOV - code coverage report
Current view: top level - src/backend/catalog - pg_shdepend.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 394 484 81.4 %
Date: 2024-05-09 06:10:39 Functions: 22 22 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14