LCOV - differential code coverage report
Current view: top level - src/backend/catalog - pg_shdepend.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 81.6 % 499 407 92 6 401 6
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 24 24 1 23
Baseline: lcov-20250906-005545-baseline Branches: 64.5 % 265 171 94 171
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 6 6 6
(360..) days: 81.3 % 493 401 92 401
Function coverage date bins:
(360..) days: 100.0 % 24 24 1 23
Branch coverage date bins:
(360..) days: 64.5 % 265 171 94 171

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

Generated by: LCOV version 2.4-beta