LCOV - differential code coverage report
Current view: top level - src/backend/catalog - dependency.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 91.0 % 935 851 23 48 13 22 518 8 303 49 522 3
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 28 28 28 27 1
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * dependency.c
       4                 :  *    Routines to support inter-object dependencies.
       5                 :  *
       6                 :  *
       7                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       8                 :  * Portions Copyright (c) 1994, Regents of the University of California
       9                 :  *
      10                 :  * IDENTIFICATION
      11                 :  *    src/backend/catalog/dependency.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/heap.h"
      24                 : #include "catalog/index.h"
      25                 : #include "catalog/objectaccess.h"
      26                 : #include "catalog/pg_am.h"
      27                 : #include "catalog/pg_amop.h"
      28                 : #include "catalog/pg_amproc.h"
      29                 : #include "catalog/pg_attrdef.h"
      30                 : #include "catalog/pg_authid.h"
      31                 : #include "catalog/pg_auth_members.h"
      32                 : #include "catalog/pg_cast.h"
      33                 : #include "catalog/pg_collation.h"
      34                 : #include "catalog/pg_constraint.h"
      35                 : #include "catalog/pg_conversion.h"
      36                 : #include "catalog/pg_database.h"
      37                 : #include "catalog/pg_default_acl.h"
      38                 : #include "catalog/pg_depend.h"
      39                 : #include "catalog/pg_event_trigger.h"
      40                 : #include "catalog/pg_extension.h"
      41                 : #include "catalog/pg_foreign_data_wrapper.h"
      42                 : #include "catalog/pg_foreign_server.h"
      43                 : #include "catalog/pg_init_privs.h"
      44                 : #include "catalog/pg_language.h"
      45                 : #include "catalog/pg_largeobject.h"
      46                 : #include "catalog/pg_namespace.h"
      47                 : #include "catalog/pg_opclass.h"
      48                 : #include "catalog/pg_operator.h"
      49                 : #include "catalog/pg_opfamily.h"
      50                 : #include "catalog/pg_parameter_acl.h"
      51                 : #include "catalog/pg_policy.h"
      52                 : #include "catalog/pg_proc.h"
      53                 : #include "catalog/pg_publication.h"
      54                 : #include "catalog/pg_publication_namespace.h"
      55                 : #include "catalog/pg_publication_rel.h"
      56                 : #include "catalog/pg_rewrite.h"
      57                 : #include "catalog/pg_statistic_ext.h"
      58                 : #include "catalog/pg_subscription.h"
      59                 : #include "catalog/pg_tablespace.h"
      60                 : #include "catalog/pg_transform.h"
      61                 : #include "catalog/pg_trigger.h"
      62                 : #include "catalog/pg_ts_config.h"
      63                 : #include "catalog/pg_ts_dict.h"
      64                 : #include "catalog/pg_ts_parser.h"
      65                 : #include "catalog/pg_ts_template.h"
      66                 : #include "catalog/pg_type.h"
      67                 : #include "catalog/pg_user_mapping.h"
      68                 : #include "commands/comment.h"
      69                 : #include "commands/defrem.h"
      70                 : #include "commands/event_trigger.h"
      71                 : #include "commands/extension.h"
      72                 : #include "commands/policy.h"
      73                 : #include "commands/publicationcmds.h"
      74                 : #include "commands/seclabel.h"
      75                 : #include "commands/sequence.h"
      76                 : #include "commands/trigger.h"
      77                 : #include "commands/typecmds.h"
      78                 : #include "funcapi.h"
      79                 : #include "nodes/nodeFuncs.h"
      80                 : #include "parser/parsetree.h"
      81                 : #include "rewrite/rewriteRemove.h"
      82                 : #include "storage/lmgr.h"
      83                 : #include "utils/acl.h"
      84                 : #include "utils/fmgroids.h"
      85                 : #include "utils/guc.h"
      86                 : #include "utils/lsyscache.h"
      87                 : #include "utils/syscache.h"
      88                 : 
      89                 : 
      90                 : /*
      91                 :  * Deletion processing requires additional state for each ObjectAddress that
      92                 :  * it's planning to delete.  For simplicity and code-sharing we make the
      93                 :  * ObjectAddresses code support arrays with or without this extra state.
      94                 :  */
      95                 : typedef struct
      96                 : {
      97                 :     int         flags;          /* bitmask, see bit definitions below */
      98                 :     ObjectAddress dependee;     /* object whose deletion forced this one */
      99                 : } ObjectAddressExtra;
     100                 : 
     101                 : /* ObjectAddressExtra flag bits */
     102                 : #define DEPFLAG_ORIGINAL    0x0001  /* an original deletion target */
     103                 : #define DEPFLAG_NORMAL      0x0002  /* reached via normal dependency */
     104                 : #define DEPFLAG_AUTO        0x0004  /* reached via auto dependency */
     105                 : #define DEPFLAG_INTERNAL    0x0008  /* reached via internal dependency */
     106                 : #define DEPFLAG_PARTITION   0x0010  /* reached via partition dependency */
     107                 : #define DEPFLAG_EXTENSION   0x0020  /* reached via extension dependency */
     108                 : #define DEPFLAG_REVERSE     0x0040  /* reverse internal/extension link */
     109                 : #define DEPFLAG_IS_PART     0x0080  /* has a partition dependency */
     110                 : #define DEPFLAG_SUBOBJECT   0x0100  /* subobject of another deletable object */
     111                 : 
     112                 : 
     113                 : /* expansible list of ObjectAddresses */
     114                 : struct ObjectAddresses
     115                 : {
     116                 :     ObjectAddress *refs;        /* => palloc'd array */
     117                 :     ObjectAddressExtra *extras; /* => palloc'd array, or NULL if not used */
     118                 :     int         numrefs;        /* current number of references */
     119                 :     int         maxrefs;        /* current size of palloc'd array(s) */
     120                 : };
     121                 : 
     122                 : /* typedef ObjectAddresses appears in dependency.h */
     123                 : 
     124                 : /* threaded list of ObjectAddresses, for recursion detection */
     125                 : typedef struct ObjectAddressStack
     126                 : {
     127                 :     const ObjectAddress *object;    /* object being visited */
     128                 :     int         flags;          /* its current flag bits */
     129                 :     struct ObjectAddressStack *next;    /* next outer stack level */
     130                 : } ObjectAddressStack;
     131                 : 
     132                 : /* temporary storage in findDependentObjects */
     133                 : typedef struct
     134                 : {
     135                 :     ObjectAddress obj;          /* object to be deleted --- MUST BE FIRST */
     136                 :     int         subflags;       /* flags to pass down when recursing to obj */
     137                 : } ObjectAddressAndFlags;
     138                 : 
     139                 : /* for find_expr_references_walker */
     140                 : typedef struct
     141                 : {
     142                 :     ObjectAddresses *addrs;     /* addresses being accumulated */
     143                 :     List       *rtables;        /* list of rangetables to resolve Vars */
     144                 : } find_expr_references_context;
     145                 : 
     146                 : /*
     147                 :  * This constant table maps ObjectClasses to the corresponding catalog OIDs.
     148                 :  * See also getObjectClass().
     149                 :  */
     150                 : static const Oid object_classes[] = {
     151                 :     RelationRelationId,         /* OCLASS_CLASS */
     152                 :     ProcedureRelationId,        /* OCLASS_PROC */
     153                 :     TypeRelationId,             /* OCLASS_TYPE */
     154                 :     CastRelationId,             /* OCLASS_CAST */
     155                 :     CollationRelationId,        /* OCLASS_COLLATION */
     156                 :     ConstraintRelationId,       /* OCLASS_CONSTRAINT */
     157                 :     ConversionRelationId,       /* OCLASS_CONVERSION */
     158                 :     AttrDefaultRelationId,      /* OCLASS_DEFAULT */
     159                 :     LanguageRelationId,         /* OCLASS_LANGUAGE */
     160                 :     LargeObjectRelationId,      /* OCLASS_LARGEOBJECT */
     161                 :     OperatorRelationId,         /* OCLASS_OPERATOR */
     162                 :     OperatorClassRelationId,    /* OCLASS_OPCLASS */
     163                 :     OperatorFamilyRelationId,   /* OCLASS_OPFAMILY */
     164                 :     AccessMethodRelationId,     /* OCLASS_AM */
     165                 :     AccessMethodOperatorRelationId, /* OCLASS_AMOP */
     166                 :     AccessMethodProcedureRelationId,    /* OCLASS_AMPROC */
     167                 :     RewriteRelationId,          /* OCLASS_REWRITE */
     168                 :     TriggerRelationId,          /* OCLASS_TRIGGER */
     169                 :     NamespaceRelationId,        /* OCLASS_SCHEMA */
     170                 :     StatisticExtRelationId,     /* OCLASS_STATISTIC_EXT */
     171                 :     TSParserRelationId,         /* OCLASS_TSPARSER */
     172                 :     TSDictionaryRelationId,     /* OCLASS_TSDICT */
     173                 :     TSTemplateRelationId,       /* OCLASS_TSTEMPLATE */
     174                 :     TSConfigRelationId,         /* OCLASS_TSCONFIG */
     175                 :     AuthIdRelationId,           /* OCLASS_ROLE */
     176                 :     AuthMemRelationId,          /* OCLASS_ROLE_MEMBERSHIP */
     177                 :     DatabaseRelationId,         /* OCLASS_DATABASE */
     178                 :     TableSpaceRelationId,       /* OCLASS_TBLSPACE */
     179                 :     ForeignDataWrapperRelationId,   /* OCLASS_FDW */
     180                 :     ForeignServerRelationId,    /* OCLASS_FOREIGN_SERVER */
     181                 :     UserMappingRelationId,      /* OCLASS_USER_MAPPING */
     182                 :     DefaultAclRelationId,       /* OCLASS_DEFACL */
     183                 :     ExtensionRelationId,        /* OCLASS_EXTENSION */
     184                 :     EventTriggerRelationId,     /* OCLASS_EVENT_TRIGGER */
     185                 :     ParameterAclRelationId,     /* OCLASS_PARAMETER_ACL */
     186                 :     PolicyRelationId,           /* OCLASS_POLICY */
     187                 :     PublicationNamespaceRelationId, /* OCLASS_PUBLICATION_NAMESPACE */
     188                 :     PublicationRelationId,      /* OCLASS_PUBLICATION */
     189                 :     PublicationRelRelationId,   /* OCLASS_PUBLICATION_REL */
     190                 :     SubscriptionRelationId,     /* OCLASS_SUBSCRIPTION */
     191                 :     TransformRelationId         /* OCLASS_TRANSFORM */
     192                 : };
     193                 : 
     194                 : /*
     195                 :  * Make sure object_classes is kept up to date with the ObjectClass enum.
     196                 :  */
     197                 : StaticAssertDecl(lengthof(object_classes) == LAST_OCLASS + 1,
     198                 :                  "object_classes[] must cover all ObjectClasses");
     199                 : 
     200                 : 
     201                 : static void findDependentObjects(const ObjectAddress *object,
     202                 :                                  int objflags,
     203                 :                                  int flags,
     204                 :                                  ObjectAddressStack *stack,
     205                 :                                  ObjectAddresses *targetObjects,
     206                 :                                  const ObjectAddresses *pendingObjects,
     207                 :                                  Relation *depRel);
     208                 : static void reportDependentObjects(const ObjectAddresses *targetObjects,
     209                 :                                    DropBehavior behavior,
     210                 :                                    int flags,
     211                 :                                    const ObjectAddress *origObject);
     212                 : static void deleteOneObject(const ObjectAddress *object,
     213                 :                             Relation *depRel, int32 flags);
     214                 : static void doDeletion(const ObjectAddress *object, int flags);
     215                 : static bool find_expr_references_walker(Node *node,
     216                 :                                         find_expr_references_context *context);
     217                 : static void process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum,
     218                 :                                      find_expr_references_context *context);
     219                 : static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
     220                 : static int  object_address_comparator(const void *a, const void *b);
     221                 : static void add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
     222                 :                                ObjectAddresses *addrs);
     223                 : static void add_exact_object_address_extra(const ObjectAddress *object,
     224                 :                                            const ObjectAddressExtra *extra,
     225                 :                                            ObjectAddresses *addrs);
     226                 : static bool object_address_present_add_flags(const ObjectAddress *object,
     227                 :                                              int flags,
     228                 :                                              ObjectAddresses *addrs);
     229                 : static bool stack_address_present_add_flags(const ObjectAddress *object,
     230                 :                                             int flags,
     231                 :                                             ObjectAddressStack *stack);
     232                 : static void DeleteInitPrivs(const ObjectAddress *object);
     233                 : 
     234                 : 
     235                 : /*
     236                 :  * Go through the objects given running the final actions on them, and execute
     237                 :  * the actual deletion.
     238                 :  */
     239                 : static void
     240 GIC       13606 : deleteObjectsInList(ObjectAddresses *targetObjects, Relation *depRel,
     241                 :                     int flags)
     242                 : {
     243                 :     int         i;
     244                 : 
     245                 :     /*
     246                 :      * Keep track of objects for event triggers, if necessary.
     247                 :      */
     248 CBC       13606 :     if (trackDroppedObjectsNeeded() && !(flags & PERFORM_DELETION_INTERNAL))
     249                 :     {
     250 GIC        1507 :         for (i = 0; i < targetObjects->numrefs; i++)
     251                 :         {
     252            1250 :             const ObjectAddress *thisobj = &targetObjects->refs[i];
     253            1250 :             const ObjectAddressExtra *extra = &targetObjects->extras[i];
     254            1250 :             bool        original = false;
     255            1250 :             bool        normal = false;
     256 ECB             : 
     257 GIC        1250 :             if (extra->flags & DEPFLAG_ORIGINAL)
     258 CBC         296 :                 original = true;
     259 GIC        1250 :             if (extra->flags & DEPFLAG_NORMAL)
     260 CBC         129 :                 normal = true;
     261            1250 :             if (extra->flags & DEPFLAG_REVERSE)
     262 LBC           0 :                 normal = true;
     263 ECB             : 
     264 GIC        1250 :             if (EventTriggerSupportsObjectClass(getObjectClass(thisobj)))
     265 ECB             :             {
     266 CBC        1206 :                 EventTriggerSQLDropAddObject(thisobj, original, normal);
     267 ECB             :             }
     268                 :         }
     269                 :     }
     270 EUB             : 
     271                 :     /*
     272 ECB             :      * Delete all the objects in the proper order, except that if told to, we
     273                 :      * should skip the original object(s).
     274                 :      */
     275 GIC       97474 :     for (i = 0; i < targetObjects->numrefs; i++)
     276                 :     {
     277           83870 :         ObjectAddress *thisobj = targetObjects->refs + i;
     278           83870 :         ObjectAddressExtra *thisextra = targetObjects->extras + i;
     279                 : 
     280           83870 :         if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
     281            4369 :             (thisextra->flags & DEPFLAG_ORIGINAL))
     282             479 :             continue;
     283 ECB             : 
     284 GIC       83391 :         deleteOneObject(thisobj, depRel, flags);
     285 ECB             :     }
     286 CBC       13604 : }
     287                 : 
     288 ECB             : /*
     289                 :  * performDeletion: attempt to drop the specified object.  If CASCADE
     290                 :  * behavior is specified, also drop any dependent objects (recursively).
     291                 :  * If RESTRICT behavior is specified, error out if there are any dependent
     292                 :  * objects, except for those that should be implicitly dropped anyway
     293                 :  * according to the dependency type.
     294                 :  *
     295                 :  * This is the outer control routine for all forms of DROP that drop objects
     296                 :  * that can participate in dependencies.  Note that performMultipleDeletions
     297                 :  * is a variant on the same theme; if you change anything here you'll likely
     298                 :  * need to fix that too.
     299                 :  *
     300                 :  * Bits in the flags argument can include:
     301                 :  *
     302                 :  * PERFORM_DELETION_INTERNAL: indicates that the drop operation is not the
     303                 :  * direct result of a user-initiated action.  For example, when a temporary
     304                 :  * schema is cleaned out so that a new backend can use it, or when a column
     305                 :  * default is dropped as an intermediate step while adding a new one, that's
     306                 :  * an internal operation.  On the other hand, when we drop something because
     307                 :  * the user issued a DROP statement against it, that's not internal. Currently
     308                 :  * this suppresses calling event triggers and making some permissions checks.
     309                 :  *
     310                 :  * PERFORM_DELETION_CONCURRENTLY: perform the drop concurrently.  This does
     311                 :  * not currently work for anything except dropping indexes; don't set it for
     312                 :  * other object types or you may get strange results.
     313                 :  *
     314                 :  * PERFORM_DELETION_QUIETLY: reduce message level from NOTICE to DEBUG2.
     315                 :  *
     316                 :  * PERFORM_DELETION_SKIP_ORIGINAL: do not delete the specified object(s),
     317                 :  * but only what depends on it/them.
     318                 :  *
     319                 :  * PERFORM_DELETION_SKIP_EXTENSIONS: do not delete extensions, even when
     320                 :  * deleting objects that are part of an extension.  This should generally
     321                 :  * be used only when dropping temporary objects.
     322                 :  *
     323                 :  * PERFORM_DELETION_CONCURRENT_LOCK: perform the drop normally but with a lock
     324                 :  * as if it were concurrent.  This is used by REINDEX CONCURRENTLY.
     325                 :  *
     326                 :  */
     327                 : void
     328 GIC        2191 : performDeletion(const ObjectAddress *object,
     329                 :                 DropBehavior behavior, int flags)
     330                 : {
     331                 :     Relation    depRel;
     332                 :     ObjectAddresses *targetObjects;
     333                 : 
     334                 :     /*
     335                 :      * We save some cycles by opening pg_depend just once and passing the
     336 ECB             :      * Relation pointer down to all the recursive deletion steps.
     337                 :      */
     338 GIC        2191 :     depRel = table_open(DependRelationId, RowExclusiveLock);
     339                 : 
     340                 :     /*
     341                 :      * Acquire deletion lock on the target object.  (Ideally the caller has
     342                 :      * done this already, but many places are sloppy about it.)
     343                 :      */
     344            2191 :     AcquireDeletionLock(object, 0);
     345                 : 
     346 ECB             :     /*
     347                 :      * Construct a list of objects to delete (ie, the given object plus
     348                 :      * everything directly or indirectly dependent on it).
     349                 :      */
     350 GIC        2191 :     targetObjects = new_object_addresses();
     351                 : 
     352 CBC        2191 :     findDependentObjects(object,
     353                 :                          DEPFLAG_ORIGINAL,
     354                 :                          flags,
     355                 :                          NULL,  /* empty stack */
     356                 :                          targetObjects,
     357                 :                          NULL,  /* no pendingObjects */
     358 ECB             :                          &depRel);
     359                 : 
     360                 :     /*
     361                 :      * Check if deletion is allowed, and report about cascaded deletes.
     362                 :      */
     363 GIC        2191 :     reportDependentObjects(targetObjects,
     364                 :                            behavior,
     365                 :                            flags,
     366                 :                            object);
     367                 : 
     368                 :     /* do the deed */
     369            2173 :     deleteObjectsInList(targetObjects, &depRel, flags);
     370                 : 
     371 ECB             :     /* And clean up */
     372 GIC        2172 :     free_object_addresses(targetObjects);
     373                 : 
     374            2172 :     table_close(depRel, RowExclusiveLock);
     375            2172 : }
     376                 : 
     377 ECB             : /*
     378                 :  * performMultipleDeletions: Similar to performDeletion, but act on multiple
     379                 :  * objects at once.
     380                 :  *
     381                 :  * The main difference from issuing multiple performDeletion calls is that the
     382                 :  * list of objects that would be implicitly dropped, for each object to be
     383                 :  * dropped, is the union of the implicit-object list for all objects.  This
     384                 :  * makes each check be more relaxed.
     385                 :  */
     386                 : void
     387 GIC       12583 : performMultipleDeletions(const ObjectAddresses *objects,
     388                 :                          DropBehavior behavior, int flags)
     389                 : {
     390                 :     Relation    depRel;
     391                 :     ObjectAddresses *targetObjects;
     392                 :     int         i;
     393                 : 
     394                 :     /* No work if no objects... */
     395 CBC       12583 :     if (objects->numrefs <= 0)
     396 GIC         981 :         return;
     397                 : 
     398                 :     /*
     399                 :      * We save some cycles by opening pg_depend just once and passing the
     400                 :      * Relation pointer down to all the recursive deletion steps.
     401                 :      */
     402           11602 :     depRel = table_open(DependRelationId, RowExclusiveLock);
     403 ECB             : 
     404                 :     /*
     405                 :      * Construct a list of objects to delete (ie, the given objects plus
     406                 :      * everything directly or indirectly dependent on them).  Note that
     407                 :      * because we pass the whole objects list as pendingObjects context, we
     408                 :      * won't get a failure from trying to delete an object that is internally
     409                 :      * dependent on another one in the list; we'll just skip that object and
     410                 :      * delete it when we reach its owner.
     411                 :      */
     412 GIC       11602 :     targetObjects = new_object_addresses();
     413                 : 
     414           25509 :     for (i = 0; i < objects->numrefs; i++)
     415                 :     {
     416           13928 :         const ObjectAddress *thisobj = objects->refs + i;
     417                 : 
     418                 :         /*
     419                 :          * Acquire deletion lock on each target object.  (Ideally the caller
     420 ECB             :          * has done this already, but many places are sloppy about it.)
     421                 :          */
     422 CBC       13928 :         AcquireDeletionLock(thisobj, flags);
     423                 : 
     424           13928 :         findDependentObjects(thisobj,
     425                 :                              DEPFLAG_ORIGINAL,
     426                 :                              flags,
     427                 :                              NULL,  /* empty stack */
     428                 :                              targetObjects,
     429                 :                              objects,
     430 ECB             :                              &depRel);
     431                 :     }
     432                 : 
     433                 :     /*
     434                 :      * Check if deletion is allowed, and report about cascaded deletes.
     435                 :      *
     436                 :      * If there's exactly one object being deleted, report it the same way as
     437                 :      * in performDeletion(), else we have to be vaguer.
     438                 :      */
     439 GIC       11581 :     reportDependentObjects(targetObjects,
     440                 :                            behavior,
     441                 :                            flags,
     442           11581 :                            (objects->numrefs == 1 ? objects->refs : NULL));
     443                 : 
     444                 :     /* do the deed */
     445           11433 :     deleteObjectsInList(targetObjects, &depRel, flags);
     446                 : 
     447 ECB             :     /* And clean up */
     448 GIC       11432 :     free_object_addresses(targetObjects);
     449                 : 
     450 CBC       11432 :     table_close(depRel, RowExclusiveLock);
     451                 : }
     452                 : 
     453 ECB             : /*
     454                 :  * findDependentObjects - find all objects that depend on 'object'
     455                 :  *
     456                 :  * For every object that depends on the starting object, acquire a deletion
     457                 :  * lock on the object, add it to targetObjects (if not already there),
     458                 :  * and recursively find objects that depend on it.  An object's dependencies
     459                 :  * will be placed into targetObjects before the object itself; this means
     460                 :  * that the finished list's order represents a safe deletion order.
     461                 :  *
     462                 :  * The caller must already have a deletion lock on 'object' itself,
     463                 :  * but must not have added it to targetObjects.  (Note: there are corner
     464                 :  * cases where we won't add the object either, and will also release the
     465                 :  * caller-taken lock.  This is a bit ugly, but the API is set up this way
     466                 :  * to allow easy rechecking of an object's liveness after we lock it.  See
     467                 :  * notes within the function.)
     468                 :  *
     469                 :  * When dropping a whole object (subId = 0), we find dependencies for
     470                 :  * its sub-objects too.
     471                 :  *
     472                 :  *  object: the object to add to targetObjects and find dependencies on
     473                 :  *  objflags: flags to be ORed into the object's targetObjects entry
     474                 :  *  flags: PERFORM_DELETION_xxx flags for the deletion operation as a whole
     475                 :  *  stack: list of objects being visited in current recursion; topmost item
     476                 :  *          is the object that we recursed from (NULL for external callers)
     477                 :  *  targetObjects: list of objects that are scheduled to be deleted
     478                 :  *  pendingObjects: list of other objects slated for destruction, but
     479                 :  *          not necessarily in targetObjects yet (can be NULL if none)
     480                 :  *  *depRel: already opened pg_depend relation
     481                 :  *
     482                 :  * Note: objflags describes the reason for visiting this particular object
     483                 :  * at this time, and is not passed down when recursing.  The flags argument
     484                 :  * is passed down, since it describes what we're doing overall.
     485                 :  */
     486                 : static void
     487 GIC      105461 : findDependentObjects(const ObjectAddress *object,
     488                 :                      int objflags,
     489                 :                      int flags,
     490                 :                      ObjectAddressStack *stack,
     491                 :                      ObjectAddresses *targetObjects,
     492                 :                      const ObjectAddresses *pendingObjects,
     493                 :                      Relation *depRel)
     494                 : {
     495 ECB             :     ScanKeyData key[3];
     496                 :     int         nkeys;
     497                 :     SysScanDesc scan;
     498                 :     HeapTuple   tup;
     499                 :     ObjectAddress otherObject;
     500                 :     ObjectAddress owningObject;
     501                 :     ObjectAddress partitionObject;
     502                 :     ObjectAddressAndFlags *dependentObjects;
     503                 :     int         numDependentObjects;
     504                 :     int         maxDependentObjects;
     505                 :     ObjectAddressStack mystack;
     506                 :     ObjectAddressExtra extra;
     507                 : 
     508                 :     /*
     509                 :      * If the target object is already being visited in an outer recursion
     510                 :      * level, just report the current objflags back to that level and exit.
     511                 :      * This is needed to avoid infinite recursion in the face of circular
     512                 :      * dependencies.
     513                 :      *
     514                 :      * The stack check alone would result in dependency loops being broken at
     515                 :      * an arbitrary point, ie, the first member object of the loop to be
     516                 :      * visited is the last one to be deleted.  This is obviously unworkable.
     517                 :      * However, the check for internal dependency below guarantees that we
     518                 :      * will not break a loop at an internal dependency: if we enter the loop
     519                 :      * at an "owned" object we will switch and start at the "owning" object
     520                 :      * instead.  We could probably hack something up to avoid breaking at an
     521                 :      * auto dependency, too, if we had to.  However there are no known cases
     522                 :      * where that would be necessary.
     523                 :      */
     524 GIC      105461 :     if (stack_address_present_add_flags(object, objflags, stack))
     525           19528 :         return;
     526                 : 
     527                 :     /*
     528                 :      * It's also possible that the target object has already been completely
     529                 :      * processed and put into targetObjects.  If so, again we just add the
     530                 :      * specified objflags to its entry and return.
     531                 :      *
     532 ECB             :      * (Note: in these early-exit cases we could release the caller-taken
     533                 :      * lock, since the object is presumably now locked multiple times; but it
     534                 :      * seems not worth the cycles.)
     535                 :      */
     536 GIC      105328 :     if (object_address_present_add_flags(object, objflags, targetObjects))
     537           18700 :         return;
     538                 : 
     539                 :     /*
     540                 :      * If the target object is pinned, we can just error out immediately; it
     541                 :      * won't have any objects recorded as depending on it.
     542                 :      */
     543           86628 :     if (IsPinnedObject(object->classId, object->objectId))
     544 CBC           1 :         ereport(ERROR,
     545 ECB             :                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
     546                 :                  errmsg("cannot drop %s because it is required by the database system",
     547                 :                         getObjectDescription(object, false))));
     548                 : 
     549                 :     /*
     550                 :      * The target object might be internally dependent on some other object
     551                 :      * (its "owner"), and/or be a member of an extension (also considered its
     552                 :      * owner).  If so, and if we aren't recursing from the owning object, we
     553                 :      * have to transform this deletion request into a deletion request of the
     554                 :      * owning object.  (We'll eventually recurse back to this object, but the
     555                 :      * owning object has to be visited first so it will be deleted after.) The
     556                 :      * way to find out about this is to scan the pg_depend entries that show
     557                 :      * what this object depends on.
     558                 :      */
     559 GIC       86627 :     ScanKeyInit(&key[0],
     560                 :                 Anum_pg_depend_classid,
     561                 :                 BTEqualStrategyNumber, F_OIDEQ,
     562           86627 :                 ObjectIdGetDatum(object->classId));
     563           86627 :     ScanKeyInit(&key[1],
     564                 :                 Anum_pg_depend_objid,
     565                 :                 BTEqualStrategyNumber, F_OIDEQ,
     566           86627 :                 ObjectIdGetDatum(object->objectId));
     567 CBC       86627 :     if (object->objectSubId != 0)
     568                 :     {
     569                 :         /* Consider only dependencies of this sub-object */
     570            1034 :         ScanKeyInit(&key[2],
     571 ECB             :                     Anum_pg_depend_objsubid,
     572                 :                     BTEqualStrategyNumber, F_INT4EQ,
     573 GIC        1034 :                     Int32GetDatum(object->objectSubId));
     574 CBC        1034 :         nkeys = 3;
     575 ECB             :     }
     576                 :     else
     577                 :     {
     578                 :         /* Consider dependencies of this object and any sub-objects it has */
     579 GIC       85593 :         nkeys = 2;
     580                 :     }
     581 ECB             : 
     582 CBC       86627 :     scan = systable_beginscan(*depRel, DependDependerIndexId, true,
     583                 :                               NULL, nkeys, key);
     584                 : 
     585                 :     /* initialize variables that loop may fill */
     586 GIC       86627 :     memset(&owningObject, 0, sizeof(owningObject));
     587 CBC       86627 :     memset(&partitionObject, 0, sizeof(partitionObject));
     588                 : 
     589 GIC      206802 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
     590 ECB             :     {
     591 GIC      120870 :         Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
     592                 : 
     593          120870 :         otherObject.classId = foundDep->refclassid;
     594 CBC      120870 :         otherObject.objectId = foundDep->refobjid;
     595          120870 :         otherObject.objectSubId = foundDep->refobjsubid;
     596                 : 
     597 ECB             :         /*
     598                 :          * When scanning dependencies of a whole object, we may find rows
     599                 :          * linking sub-objects of the object to the object itself.  (Normally,
     600                 :          * such a dependency is implicit, but we must make explicit ones in
     601                 :          * some cases involving partitioning.)  We must ignore such rows to
     602                 :          * avoid infinite recursion.
     603                 :          */
     604 GIC      120870 :         if (otherObject.classId == object->classId &&
     605           41746 :             otherObject.objectId == object->objectId &&
     606            1872 :             object->objectSubId == 0)
     607            1860 :             continue;
     608                 : 
     609          119010 :         switch (foundDep->deptype)
     610                 :         {
     611           68207 :             case DEPENDENCY_NORMAL:
     612 ECB             :             case DEPENDENCY_AUTO:
     613                 :             case DEPENDENCY_AUTO_EXTENSION:
     614                 :                 /* no problem */
     615 CBC       68207 :                 break;
     616                 : 
     617            1398 :             case DEPENDENCY_EXTENSION:
     618                 : 
     619 ECB             :                 /*
     620                 :                  * If told to, ignore EXTENSION dependencies altogether.  This
     621                 :                  * flag is normally used to prevent dropping extensions during
     622                 :                  * temporary-object cleanup, even if a temp object was created
     623                 :                  * during an extension script.
     624                 :                  */
     625 CBC        1398 :                 if (flags & PERFORM_DELETION_SKIP_EXTENSIONS)
     626 GIC           2 :                     break;
     627                 : 
     628                 :                 /*
     629                 :                  * If the other object is the extension currently being
     630                 :                  * created/altered, ignore this dependency and continue with
     631                 :                  * the deletion.  This allows dropping of an extension's
     632                 :                  * objects within the extension's scripts, as well as corner
     633 ECB             :                  * cases such as dropping a transient object created within
     634                 :                  * such a script.
     635                 :                  */
     636 GIC        1396 :                 if (creating_extension &&
     637             110 :                     otherObject.classId == ExtensionRelationId &&
     638             110 :                     otherObject.objectId == CurrentExtensionObject)
     639             110 :                     break;
     640                 : 
     641                 :                 /* Otherwise, treat this like an internal dependency */
     642                 :                 /* FALL THRU */
     643                 : 
     644 ECB             :             case DEPENDENCY_INTERNAL:
     645                 : 
     646                 :                 /*
     647                 :                  * This object is part of the internal implementation of
     648                 :                  * another object, or is part of the extension that is the
     649                 :                  * other object.  We have three cases:
     650                 :                  *
     651                 :                  * 1. At the outermost recursion level, we must disallow the
     652                 :                  * DROP.  However, if the owning object is listed in
     653                 :                  * pendingObjects, just release the caller's lock and return;
     654                 :                  * we'll eventually complete the DROP when we reach that entry
     655                 :                  * in the pending list.
     656                 :                  *
     657                 :                  * Note: the above statement is true only if this pg_depend
     658                 :                  * entry still exists by then; in principle, therefore, we
     659                 :                  * could miss deleting an item the user told us to delete.
     660                 :                  * However, no inconsistency can result: since we're at outer
     661                 :                  * level, there is no object depending on this one.
     662                 :                  */
     663 GIC       46547 :                 if (stack == NULL)
     664                 :                 {
     665              40 :                     if (pendingObjects &&
     666              20 :                         object_address_present(&otherObject, pendingObjects))
     667                 :                     {
     668 UIC           0 :                         systable_endscan(scan);
     669                 :                         /* need to release caller's lock; see notes below */
     670               0 :                         ReleaseDeletionLock(object);
     671 LBC           0 :                         return;
     672                 :                     }
     673 ECB             : 
     674                 :                     /*
     675                 :                      * We postpone actually issuing the error message until
     676 EUB             :                      * after this loop, so that we can make the behavior
     677                 :                      * independent of the ordering of pg_depend entries, at
     678                 :                      * least if there's not more than one INTERNAL and one
     679                 :                      * EXTENSION dependency.  (If there's more, we'll complain
     680                 :                      * about a random one of them.)  Prefer to complain about
     681                 :                      * EXTENSION, since that's generally a more important
     682                 :                      * dependency.
     683                 :                      */
     684 GIC          20 :                     if (!OidIsValid(owningObject.classId) ||
     685 UIC           0 :                         foundDep->deptype == DEPENDENCY_EXTENSION)
     686 GIC          20 :                         owningObject = otherObject;
     687              20 :                     break;
     688                 :                 }
     689                 : 
     690                 :                 /*
     691                 :                  * 2. When recursing from the other end of this dependency,
     692 ECB             :                  * it's okay to continue with the deletion.  This holds when
     693 EUB             :                  * recursing from a whole object that includes the nominal
     694 ECB             :                  * other end as a component, too.  Since there can be more
     695                 :                  * than one "owning" object, we have to allow matches that are
     696                 :                  * more than one level down in the stack.
     697                 :                  */
     698 GIC       46527 :                 if (stack_address_present_add_flags(&otherObject, 0, stack))
     699           45832 :                     break;
     700                 : 
     701                 :                 /*
     702                 :                  * 3. Not all the owning objects have been visited, so
     703                 :                  * transform this deletion request into a delete of this
     704                 :                  * owning object.
     705                 :                  *
     706 ECB             :                  * First, release caller's lock on this object and get
     707                 :                  * deletion lock on the owning object.  (We must release
     708                 :                  * caller's lock to avoid deadlock against a concurrent
     709                 :                  * deletion of the owning object.)
     710                 :                  */
     711 GIC         695 :                 ReleaseDeletionLock(object);
     712             695 :                 AcquireDeletionLock(&otherObject, 0);
     713                 : 
     714                 :                 /*
     715                 :                  * The owning object might have been deleted while we waited
     716                 :                  * to lock it; if so, neither it nor the current object are
     717                 :                  * interesting anymore.  We test this by checking the
     718                 :                  * pg_depend entry (see notes below).
     719 ECB             :                  */
     720 CBC         695 :                 if (!systable_recheck_tuple(scan, tup))
     721                 :                 {
     722 UIC           0 :                     systable_endscan(scan);
     723               0 :                     ReleaseDeletionLock(&otherObject);
     724               0 :                     return;
     725                 :                 }
     726                 : 
     727                 :                 /*
     728 ECB             :                  * One way or the other, we're done with the scan; might as
     729                 :                  * well close it down before recursing, to reduce peak
     730 EUB             :                  * resource consumption.
     731                 :                  */
     732 GBC         695 :                 systable_endscan(scan);
     733                 : 
     734                 :                 /*
     735                 :                  * Okay, recurse to the owning object instead of proceeding.
     736                 :                  *
     737                 :                  * We do not need to stack the current object; we want the
     738                 :                  * traversal order to be as if the original reference had
     739                 :                  * linked to the owning object instead of this one.
     740 ECB             :                  *
     741                 :                  * The dependency type is a "reverse" dependency: we need to
     742                 :                  * delete the owning object if this one is to be deleted, but
     743                 :                  * this linkage is never a reason for an automatic deletion.
     744                 :                  */
     745 GIC         695 :                 findDependentObjects(&otherObject,
     746                 :                                      DEPFLAG_REVERSE,
     747                 :                                      flags,
     748                 :                                      stack,
     749                 :                                      targetObjects,
     750                 :                                      pendingObjects,
     751                 :                                      depRel);
     752                 : 
     753 ECB             :                 /*
     754                 :                  * The current target object should have been added to
     755                 :                  * targetObjects while processing the owning object; but it
     756                 :                  * probably got only the flag bits associated with the
     757                 :                  * dependency we're looking at.  We need to add the objflags
     758                 :                  * that were passed to this recursion level, too, else we may
     759                 :                  * get a bogus failure in reportDependentObjects (if, for
     760                 :                  * example, we were called due to a partition dependency).
     761                 :                  *
     762                 :                  * If somehow the current object didn't get scheduled for
     763                 :                  * deletion, bleat.  (That would imply that somebody deleted
     764                 :                  * this dependency record before the recursion got to it.)
     765                 :                  * Another idea would be to reacquire lock on the current
     766                 :                  * object and resume trying to delete it, but it seems not
     767                 :                  * worth dealing with the race conditions inherent in that.
     768                 :                  */
     769 GIC         695 :                 if (!object_address_present_add_flags(object, objflags,
     770                 :                                                       targetObjects))
     771 UIC           0 :                     elog(ERROR, "deletion of owning object %s failed to delete %s",
     772                 :                          getObjectDescription(&otherObject, false),
     773                 :                          getObjectDescription(object, false));
     774                 : 
     775                 :                 /* And we're done here. */
     776 GIC         695 :                 return;
     777 ECB             : 
     778 GIC        2072 :             case DEPENDENCY_PARTITION_PRI:
     779 EUB             : 
     780                 :                 /*
     781                 :                  * Remember that this object has a partition-type dependency.
     782                 :                  * After the dependency scan, we'll complain if we didn't find
     783                 :                  * a reason to delete one of its partition dependencies.
     784 ECB             :                  */
     785 GIC        2072 :                 objflags |= DEPFLAG_IS_PART;
     786 ECB             : 
     787                 :                 /*
     788                 :                  * Also remember the primary partition owner, for error
     789                 :                  * messages.  If there are multiple primary owners (which
     790                 :                  * there should not be), we'll report a random one of them.
     791                 :                  */
     792 GIC        2072 :                 partitionObject = otherObject;
     793 CBC        2072 :                 break;
     794                 : 
     795 GIC        2072 :             case DEPENDENCY_PARTITION_SEC:
     796                 : 
     797                 :                 /*
     798                 :                  * Only use secondary partition owners in error messages if we
     799                 :                  * find no primary owner (which probably shouldn't happen).
     800 ECB             :                  */
     801 CBC        2072 :                 if (!(objflags & DEPFLAG_IS_PART))
     802 UIC           0 :                     partitionObject = otherObject;
     803 ECB             : 
     804                 :                 /*
     805                 :                  * Remember that this object has a partition-type dependency.
     806                 :                  * After the dependency scan, we'll complain if we didn't find
     807                 :                  * a reason to delete one of its partition dependencies.
     808                 :                  */
     809 CBC        2072 :                 objflags |= DEPFLAG_IS_PART;
     810 GBC        2072 :                 break;
     811                 : 
     812 UIC           0 :             default:
     813               0 :                 elog(ERROR, "unrecognized dependency type '%c' for %s",
     814                 :                      foundDep->deptype, getObjectDescription(object, false));
     815                 :                 break;
     816                 :         }
     817 ECB             :     }
     818                 : 
     819 GIC       85932 :     systable_endscan(scan);
     820 EUB             : 
     821                 :     /*
     822                 :      * If we found an INTERNAL or EXTENSION dependency when we're at outer
     823                 :      * level, complain about it now.  If we also found a PARTITION dependency,
     824                 :      * we prefer to report the PARTITION dependency.  This is arbitrary but
     825                 :      * seems to be more useful in practice.
     826                 :      */
     827 CBC       85932 :     if (OidIsValid(owningObject.classId))
     828                 :     {
     829                 :         char       *otherObjDesc;
     830                 : 
     831 GIC          20 :         if (OidIsValid(partitionObject.classId))
     832               6 :             otherObjDesc = getObjectDescription(&partitionObject, false);
     833                 :         else
     834              14 :             otherObjDesc = getObjectDescription(&owningObject, false);
     835 ECB             : 
     836 GIC          20 :         ereport(ERROR,
     837                 :                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
     838                 :                  errmsg("cannot drop %s because %s requires it",
     839 ECB             :                         getObjectDescription(object, false), otherObjDesc),
     840                 :                  errhint("You can drop %s instead.", otherObjDesc)));
     841                 :     }
     842                 : 
     843                 :     /*
     844                 :      * Next, identify all objects that directly depend on the current object.
     845                 :      * To ensure predictable deletion order, we collect them up in
     846                 :      * dependentObjects and sort the list before actually recursing.  (The
     847                 :      * deletion order would be valid in any case, but doing this ensures
     848                 :      * consistent output from DROP CASCADE commands, which is helpful for
     849                 :      * regression testing.)
     850                 :      */
     851 GIC       85912 :     maxDependentObjects = 128;  /* arbitrary initial allocation */
     852                 :     dependentObjects = (ObjectAddressAndFlags *)
     853           85912 :         palloc(maxDependentObjects * sizeof(ObjectAddressAndFlags));
     854           85912 :     numDependentObjects = 0;
     855                 : 
     856           85912 :     ScanKeyInit(&key[0],
     857                 :                 Anum_pg_depend_refclassid,
     858                 :                 BTEqualStrategyNumber, F_OIDEQ,
     859 CBC       85912 :                 ObjectIdGetDatum(object->classId));
     860 GIC       85912 :     ScanKeyInit(&key[1],
     861 ECB             :                 Anum_pg_depend_refobjid,
     862                 :                 BTEqualStrategyNumber, F_OIDEQ,
     863 GIC       85912 :                 ObjectIdGetDatum(object->objectId));
     864 CBC       85912 :     if (object->objectSubId != 0)
     865                 :     {
     866 GIC        1022 :         ScanKeyInit(&key[2],
     867 ECB             :                     Anum_pg_depend_refobjsubid,
     868                 :                     BTEqualStrategyNumber, F_INT4EQ,
     869 GIC        1022 :                     Int32GetDatum(object->objectSubId));
     870            1022 :         nkeys = 3;
     871 ECB             :     }
     872                 :     else
     873 GIC       84890 :         nkeys = 2;
     874 ECB             : 
     875 GIC       85912 :     scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
     876                 :                               NULL, nkeys, key);
     877 ECB             : 
     878 CBC      176419 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
     879                 :     {
     880 GIC       90507 :         Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
     881 ECB             :         int         subflags;
     882                 : 
     883 CBC       90507 :         otherObject.classId = foundDep->classid;
     884 GIC       90507 :         otherObject.objectId = foundDep->objid;
     885           90507 :         otherObject.objectSubId = foundDep->objsubid;
     886 ECB             : 
     887                 :         /*
     888                 :          * If what we found is a sub-object of the current object, just ignore
     889                 :          * it.  (Normally, such a dependency is implicit, but we must make
     890                 :          * explicit ones in some cases involving partitioning.)
     891                 :          */
     892 CBC       90507 :         if (otherObject.classId == object->classId &&
     893           39853 :             otherObject.objectId == object->objectId &&
     894 GIC        1860 :             object->objectSubId == 0)
     895            1860 :             continue;
     896                 : 
     897                 :         /*
     898                 :          * Must lock the dependent object before recursing to it.
     899                 :          */
     900 CBC       88647 :         AcquireDeletionLock(&otherObject, 0);
     901 ECB             : 
     902                 :         /*
     903                 :          * The dependent object might have been deleted while we waited to
     904                 :          * lock it; if so, we don't need to do anything more with it. We can
     905                 :          * test this cheaply and independently of the object's type by seeing
     906                 :          * if the pg_depend tuple we are looking at is still live. (If the
     907                 :          * object got deleted, the tuple would have been deleted too.)
     908                 :          */
     909 GIC       88647 :         if (!systable_recheck_tuple(scan, tup))
     910                 :         {
     911                 :             /* release the now-useless lock */
     912 UIC           0 :             ReleaseDeletionLock(&otherObject);
     913                 :             /* and continue scanning for dependencies */
     914               0 :             continue;
     915                 :         }
     916                 : 
     917 ECB             :         /*
     918                 :          * We do need to delete it, so identify objflags to be passed down,
     919                 :          * which depend on the dependency type.
     920 EUB             :          */
     921 GIC       88647 :         switch (foundDep->deptype)
     922 EUB             :         {
     923 GIC       12127 :             case DEPENDENCY_NORMAL:
     924           12127 :                 subflags = DEPFLAG_NORMAL;
     925           12127 :                 break;
     926           26923 :             case DEPENDENCY_AUTO:
     927                 :             case DEPENDENCY_AUTO_EXTENSION:
     928           26923 :                 subflags = DEPFLAG_AUTO;
     929 CBC       26923 :                 break;
     930 GIC       44550 :             case DEPENDENCY_INTERNAL:
     931 CBC       44550 :                 subflags = DEPFLAG_INTERNAL;
     932           44550 :                 break;
     933            3765 :             case DEPENDENCY_PARTITION_PRI:
     934 ECB             :             case DEPENDENCY_PARTITION_SEC:
     935 GIC        3765 :                 subflags = DEPFLAG_PARTITION;
     936 CBC        3765 :                 break;
     937            1282 :             case DEPENDENCY_EXTENSION:
     938            1282 :                 subflags = DEPFLAG_EXTENSION;
     939            1282 :                 break;
     940 LBC           0 :             default:
     941               0 :                 elog(ERROR, "unrecognized dependency type '%c' for %s",
     942                 :                      foundDep->deptype, getObjectDescription(object, false));
     943 ECB             :                 subflags = 0;   /* keep compiler quiet */
     944                 :                 break;
     945                 :         }
     946                 : 
     947                 :         /* And add it to the pending-objects list */
     948 GBC       88647 :         if (numDependentObjects >= maxDependentObjects)
     949 EUB             :         {
     950                 :             /* enlarge array if needed */
     951 GIC           4 :             maxDependentObjects *= 2;
     952                 :             dependentObjects = (ObjectAddressAndFlags *)
     953               4 :                 repalloc(dependentObjects,
     954                 :                          maxDependentObjects * sizeof(ObjectAddressAndFlags));
     955                 :         }
     956 ECB             : 
     957 GIC       88647 :         dependentObjects[numDependentObjects].obj = otherObject;
     958           88647 :         dependentObjects[numDependentObjects].subflags = subflags;
     959 CBC       88647 :         numDependentObjects++;
     960                 :     }
     961 ECB             : 
     962 GIC       85912 :     systable_endscan(scan);
     963                 : 
     964                 :     /*
     965 ECB             :      * Now we can sort the dependent objects into a stable visitation order.
     966                 :      * It's safe to use object_address_comparator here since the obj field is
     967                 :      * first within ObjectAddressAndFlags.
     968                 :      */
     969 GIC       85912 :     if (numDependentObjects > 1)
     970 GNC       18980 :         qsort(dependentObjects, numDependentObjects,
     971                 :               sizeof(ObjectAddressAndFlags),
     972                 :               object_address_comparator);
     973                 : 
     974                 :     /*
     975                 :      * Now recurse to the dependent objects.  We must visit them first since
     976                 :      * they have to be deleted before the current object.
     977 ECB             :      */
     978 CBC       85912 :     mystack.object = object;    /* set up a new stack level */
     979 GIC       85912 :     mystack.flags = objflags;
     980           85912 :     mystack.next = stack;
     981                 : 
     982          174559 :     for (int i = 0; i < numDependentObjects; i++)
     983                 :     {
     984           88647 :         ObjectAddressAndFlags *depObj = dependentObjects + i;
     985                 : 
     986 CBC       88647 :         findDependentObjects(&depObj->obj,
     987 ECB             :                              depObj->subflags,
     988                 :                              flags,
     989                 :                              &mystack,
     990                 :                              targetObjects,
     991                 :                              pendingObjects,
     992                 :                              depRel);
     993                 :     }
     994                 : 
     995 GIC       85912 :     pfree(dependentObjects);
     996                 : 
     997                 :     /*
     998                 :      * Finally, we can add the target object to targetObjects.  Be careful to
     999                 :      * include any flags that were passed back down to us from inner recursion
    1000                 :      * levels.  Record the "dependee" as being either the most important
    1001                 :      * partition owner if there is one, else the object we recursed from, if
    1002                 :      * any.  (The logic in reportDependentObjects() is such that it can only
    1003 ECB             :      * need one of those objects.)
    1004                 :      */
    1005 GIC       85912 :     extra.flags = mystack.flags;
    1006           85912 :     if (extra.flags & DEPFLAG_IS_PART)
    1007            2066 :         extra.dependee = partitionObject;
    1008           83846 :     else if (stack)
    1009           67954 :         extra.dependee = *stack->object;
    1010                 :     else
    1011           15892 :         memset(&extra.dependee, 0, sizeof(extra.dependee));
    1012           85912 :     add_exact_object_address_extra(object, &extra, targetObjects);
    1013 ECB             : }
    1014                 : 
    1015                 : /*
    1016                 :  * reportDependentObjects - report about dependencies, and fail if RESTRICT
    1017                 :  *
    1018                 :  * Tell the user about dependent objects that we are going to delete
    1019                 :  * (or would need to delete, but are prevented by RESTRICT mode);
    1020                 :  * then error out if there are any and it's not CASCADE mode.
    1021                 :  *
    1022                 :  *  targetObjects: list of objects that are scheduled to be deleted
    1023                 :  *  behavior: RESTRICT or CASCADE
    1024                 :  *  flags: other flags for the deletion operation
    1025                 :  *  origObject: base object of deletion, or NULL if not available
    1026                 :  *      (the latter case occurs in DROP OWNED)
    1027                 :  */
    1028                 : static void
    1029 GIC       13772 : reportDependentObjects(const ObjectAddresses *targetObjects,
    1030                 :                        DropBehavior behavior,
    1031                 :                        int flags,
    1032                 :                        const ObjectAddress *origObject)
    1033                 : {
    1034           13772 :     int         msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
    1035           13772 :     bool        ok = true;
    1036                 :     StringInfoData clientdetail;
    1037 ECB             :     StringInfoData logdetail;
    1038 GIC       13772 :     int         numReportedClient = 0;
    1039           13772 :     int         numNotReportedClient = 0;
    1040                 :     int         i;
    1041                 : 
    1042 ECB             :     /*
    1043                 :      * If we need to delete any partition-dependent objects, make sure that
    1044                 :      * we're deleting at least one of their partition dependencies, too. That
    1045                 :      * can be detected by checking that we reached them by a PARTITION
    1046                 :      * dependency at some point.
    1047                 :      *
    1048                 :      * We just report the first such object, as in most cases the only way to
    1049                 :      * trigger this complaint is to explicitly try to delete one partition of
    1050                 :      * a partitioned object.
    1051                 :      */
    1052 GIC       99669 :     for (i = 0; i < targetObjects->numrefs; i++)
    1053                 :     {
    1054           85912 :         const ObjectAddressExtra *extra = &targetObjects->extras[i];
    1055                 : 
    1056           85912 :         if ((extra->flags & DEPFLAG_IS_PART) &&
    1057            2066 :             !(extra->flags & DEPFLAG_PARTITION))
    1058                 :         {
    1059              15 :             const ObjectAddress *object = &targetObjects->refs[i];
    1060 CBC          15 :             char       *otherObjDesc = getObjectDescription(&extra->dependee,
    1061                 :                                                             false);
    1062 ECB             : 
    1063 GIC          15 :             ereport(ERROR,
    1064 ECB             :                     (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
    1065                 :                      errmsg("cannot drop %s because %s requires it",
    1066                 :                             getObjectDescription(object, false), otherObjDesc),
    1067                 :                      errhint("You can drop %s instead.", otherObjDesc)));
    1068                 :         }
    1069                 :     }
    1070                 : 
    1071                 :     /*
    1072                 :      * If no error is to be thrown, and the msglevel is too low to be shown to
    1073                 :      * either client or server log, there's no need to do any of the rest of
    1074                 :      * the work.
    1075                 :      */
    1076 GIC       13757 :     if (behavior == DROP_CASCADE &&
    1077            1599 :         !message_level_is_interesting(msglevel))
    1078             511 :         return;
    1079                 : 
    1080                 :     /*
    1081                 :      * We limit the number of dependencies reported to the client to
    1082                 :      * MAX_REPORTED_DEPS, since client software may not deal well with
    1083                 :      * enormous error strings.  The server log always gets a full report.
    1084 ECB             :      */
    1085                 : #define MAX_REPORTED_DEPS 100
    1086                 : 
    1087 GIC       13246 :     initStringInfo(&clientdetail);
    1088           13246 :     initStringInfo(&logdetail);
    1089                 : 
    1090                 :     /*
    1091                 :      * We process the list back to front (ie, in dependency order not deletion
    1092                 :      * order), since this makes for a more understandable display.
    1093                 :      */
    1094           93411 :     for (i = targetObjects->numrefs - 1; i >= 0; i--)
    1095 ECB             :     {
    1096 CBC       80165 :         const ObjectAddress *obj = &targetObjects->refs[i];
    1097 GIC       80165 :         const ObjectAddressExtra *extra = &targetObjects->extras[i];
    1098                 :         char       *objDesc;
    1099                 : 
    1100                 :         /* Ignore the original deletion target(s) */
    1101           80165 :         if (extra->flags & DEPFLAG_ORIGINAL)
    1102 CBC       15566 :             continue;
    1103                 : 
    1104 ECB             :         /* Also ignore sub-objects; we'll report the whole object elsewhere */
    1105 CBC       64599 :         if (extra->flags & DEPFLAG_SUBOBJECT)
    1106 UIC           0 :             continue;
    1107                 : 
    1108 GIC       64599 :         objDesc = getObjectDescription(obj, false);
    1109 ECB             : 
    1110                 :         /* An object being dropped concurrently doesn't need to be reported */
    1111 GIC       64599 :         if (objDesc == NULL)
    1112 UIC           0 :             continue;
    1113 ECB             : 
    1114 EUB             :         /*
    1115                 :          * If, at any stage of the recursive search, we reached the object via
    1116 ECB             :          * an AUTO, INTERNAL, PARTITION, or EXTENSION dependency, then it's
    1117                 :          * okay to delete it even in RESTRICT mode.
    1118                 :          */
    1119 CBC       64599 :         if (extra->flags & (DEPFLAG_AUTO |
    1120 EUB             :                             DEPFLAG_INTERNAL |
    1121                 :                             DEPFLAG_PARTITION |
    1122                 :                             DEPFLAG_EXTENSION))
    1123                 :         {
    1124                 :             /*
    1125                 :              * auto-cascades are reported at DEBUG2, not msglevel.  We don't
    1126                 :              * try to combine them with the regular message because the
    1127 ECB             :              * results are too confusing when client_min_messages and
    1128                 :              * log_min_messages are different.
    1129                 :              */
    1130 GIC       62294 :             ereport(DEBUG2,
    1131                 :                     (errmsg_internal("drop auto-cascades to %s",
    1132                 :                                      objDesc)));
    1133                 :         }
    1134            2305 :         else if (behavior == DROP_RESTRICT)
    1135                 :         {
    1136             266 :             char       *otherDesc = getObjectDescription(&extra->dependee,
    1137                 :                                                          false);
    1138 ECB             : 
    1139 GIC         266 :             if (otherDesc)
    1140                 :             {
    1141             266 :                 if (numReportedClient < MAX_REPORTED_DEPS)
    1142 ECB             :                 {
    1143                 :                     /* separate entries with a newline */
    1144 CBC         266 :                     if (clientdetail.len != 0)
    1145 GIC         115 :                         appendStringInfoChar(&clientdetail, '\n');
    1146             266 :                     appendStringInfo(&clientdetail, _("%s depends on %s"),
    1147 ECB             :                                      objDesc, otherDesc);
    1148 GIC         266 :                     numReportedClient++;
    1149 ECB             :                 }
    1150                 :                 else
    1151 UIC           0 :                     numNotReportedClient++;
    1152 ECB             :                 /* separate entries with a newline */
    1153 CBC         266 :                 if (logdetail.len != 0)
    1154             115 :                     appendStringInfoChar(&logdetail, '\n');
    1155 GIC         266 :                 appendStringInfo(&logdetail, _("%s depends on %s"),
    1156 ECB             :                                  objDesc, otherDesc);
    1157 GIC         266 :                 pfree(otherDesc);
    1158                 :             }
    1159 EUB             :             else
    1160 UIC           0 :                 numNotReportedClient++;
    1161 CBC         266 :             ok = false;
    1162 ECB             :         }
    1163                 :         else
    1164                 :         {
    1165 CBC        2039 :             if (numReportedClient < MAX_REPORTED_DEPS)
    1166                 :             {
    1167                 :                 /* separate entries with a newline */
    1168 GBC        2039 :                 if (clientdetail.len != 0)
    1169 CBC        1415 :                     appendStringInfoChar(&clientdetail, '\n');
    1170 GIC        2039 :                 appendStringInfo(&clientdetail, _("drop cascades to %s"),
    1171                 :                                  objDesc);
    1172            2039 :                 numReportedClient++;
    1173 ECB             :             }
    1174                 :             else
    1175 UIC           0 :                 numNotReportedClient++;
    1176 ECB             :             /* separate entries with a newline */
    1177 CBC        2039 :             if (logdetail.len != 0)
    1178            1415 :                 appendStringInfoChar(&logdetail, '\n');
    1179 GIC        2039 :             appendStringInfo(&logdetail, _("drop cascades to %s"),
    1180 ECB             :                              objDesc);
    1181                 :         }
    1182                 : 
    1183 GBC       64599 :         pfree(objDesc);
    1184                 :     }
    1185 ECB             : 
    1186 CBC       13246 :     if (numNotReportedClient > 0)
    1187 LBC           0 :         appendStringInfo(&clientdetail, ngettext("\nand %d other object "
    1188                 :                                                  "(see server log for list)",
    1189                 :                                                  "\nand %d other objects "
    1190                 :                                                  "(see server log for list)",
    1191 ECB             :                                                  numNotReportedClient),
    1192                 :                          numNotReportedClient);
    1193                 : 
    1194 CBC       13246 :     if (!ok)
    1195 EUB             :     {
    1196 GIC         151 :         if (origObject)
    1197             148 :             ereport(ERROR,
    1198                 :                     (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
    1199                 :                      errmsg("cannot drop %s because other objects depend on it",
    1200                 :                             getObjectDescription(origObject, false)),
    1201                 :                      errdetail_internal("%s", clientdetail.data),
    1202 ECB             :                      errdetail_log("%s", logdetail.data),
    1203                 :                      errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
    1204                 :         else
    1205 CBC           3 :             ereport(ERROR,
    1206                 :                     (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
    1207                 :                      errmsg("cannot drop desired object(s) because other objects depend on them"),
    1208                 :                      errdetail_internal("%s", clientdetail.data),
    1209                 :                      errdetail_log("%s", logdetail.data),
    1210                 :                      errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
    1211                 :     }
    1212 GIC       13095 :     else if (numReportedClient > 1)
    1213 ECB             :     {
    1214 GIC         304 :         ereport(msglevel,
    1215                 :                 (errmsg_plural("drop cascades to %d other object",
    1216                 :                                "drop cascades to %d other objects",
    1217                 :                                numReportedClient + numNotReportedClient,
    1218                 :                                numReportedClient + numNotReportedClient),
    1219                 :                  errdetail_internal("%s", clientdetail.data),
    1220 ECB             :                  errdetail_log("%s", logdetail.data)));
    1221                 :     }
    1222 CBC       12791 :     else if (numReportedClient == 1)
    1223                 :     {
    1224                 :         /* we just use the single item as-is */
    1225 GIC         320 :         ereport(msglevel,
    1226                 :                 (errmsg_internal("%s", clientdetail.data)));
    1227                 :     }
    1228                 : 
    1229           13095 :     pfree(clientdetail.data);
    1230 CBC       13095 :     pfree(logdetail.data);
    1231                 : }
    1232                 : 
    1233 ECB             : /*
    1234                 :  * Drop an object by OID.  Works for most catalogs, if no special processing
    1235                 :  * is needed.
    1236                 :  */
    1237                 : static void
    1238 CBC        1540 : DropObjectById(const ObjectAddress *object)
    1239                 : {
    1240                 :     int         cacheId;
    1241                 :     Relation    rel;
    1242                 :     HeapTuple   tup;
    1243                 : 
    1244 GIC        1540 :     cacheId = get_object_catcache_oid(object->classId);
    1245                 : 
    1246 CBC        1540 :     rel = table_open(object->classId, RowExclusiveLock);
    1247                 : 
    1248                 :     /*
    1249                 :      * Use the system cache for the oid column, if one exists.
    1250                 :      */
    1251 GIC        1540 :     if (cacheId >= 0)
    1252 ECB             :     {
    1253 GIC         833 :         tup = SearchSysCache1(cacheId, ObjectIdGetDatum(object->objectId));
    1254 CBC         833 :         if (!HeapTupleIsValid(tup))
    1255 UIC           0 :             elog(ERROR, "cache lookup failed for %s %u",
    1256                 :                  get_object_class_descr(object->classId), object->objectId);
    1257                 : 
    1258 GIC         833 :         CatalogTupleDelete(rel, &tup->t_self);
    1259 ECB             : 
    1260 GIC         833 :         ReleaseSysCache(tup);
    1261 ECB             :     }
    1262                 :     else
    1263 EUB             :     {
    1264                 :         ScanKeyData skey[1];
    1265                 :         SysScanDesc scan;
    1266 ECB             : 
    1267 GIC         707 :         ScanKeyInit(&skey[0],
    1268 CBC         707 :                     get_object_attnum_oid(object->classId),
    1269                 :                     BTEqualStrategyNumber, F_OIDEQ,
    1270 GIC         707 :                     ObjectIdGetDatum(object->objectId));
    1271                 : 
    1272             707 :         scan = systable_beginscan(rel, get_object_oid_index(object->classId), true,
    1273                 :                                   NULL, 1, skey);
    1274                 : 
    1275 ECB             :         /* we expect exactly one match */
    1276 CBC         707 :         tup = systable_getnext(scan);
    1277 GIC         707 :         if (!HeapTupleIsValid(tup))
    1278 LBC           0 :             elog(ERROR, "could not find tuple for %s %u",
    1279                 :                  get_object_class_descr(object->classId), object->objectId);
    1280 ECB             : 
    1281 GIC         707 :         CatalogTupleDelete(rel, &tup->t_self);
    1282                 : 
    1283             707 :         systable_endscan(scan);
    1284 ECB             :     }
    1285                 : 
    1286 GBC        1540 :     table_close(rel, RowExclusiveLock);
    1287 GIC        1540 : }
    1288                 : 
    1289 ECB             : /*
    1290                 :  * deleteOneObject: delete a single object for performDeletion.
    1291                 :  *
    1292                 :  * *depRel is the already-open pg_depend relation.
    1293                 :  */
    1294                 : static void
    1295 CBC       83391 : deleteOneObject(const ObjectAddress *object, Relation *depRel, int flags)
    1296                 : {
    1297                 :     ScanKeyData key[3];
    1298                 :     int         nkeys;
    1299                 :     SysScanDesc scan;
    1300                 :     HeapTuple   tup;
    1301                 : 
    1302                 :     /* DROP hook of the objects being removed */
    1303           83391 :     InvokeObjectDropHookArg(object->classId, object->objectId,
    1304                 :                             object->objectSubId, flags);
    1305                 : 
    1306                 :     /*
    1307                 :      * Close depRel if we are doing a drop concurrently.  The object deletion
    1308                 :      * subroutine will commit the current transaction, so we can't keep the
    1309                 :      * relation open across doDeletion().
    1310                 :      */
    1311           83391 :     if (flags & PERFORM_DELETION_CONCURRENTLY)
    1312 GIC          52 :         table_close(*depRel, RowExclusiveLock);
    1313                 : 
    1314                 :     /*
    1315                 :      * Delete the object itself, in an object-type-dependent way.
    1316                 :      *
    1317                 :      * We used to do this after removing the outgoing dependency links, but it
    1318                 :      * seems just as reasonable to do it beforehand.  In the concurrent case
    1319 ECB             :      * we *must* do it in this order, because we can't make any transactional
    1320                 :      * updates before calling doDeletion() --- they'd get committed right
    1321                 :      * away, which is not cool if the deletion then fails.
    1322                 :      */
    1323 GIC       83391 :     doDeletion(object, flags);
    1324                 : 
    1325                 :     /*
    1326                 :      * Reopen depRel if we closed it above
    1327                 :      */
    1328           83389 :     if (flags & PERFORM_DELETION_CONCURRENTLY)
    1329              52 :         *depRel = table_open(DependRelationId, RowExclusiveLock);
    1330                 : 
    1331 ECB             :     /*
    1332                 :      * Now remove any pg_depend records that link from this object to others.
    1333                 :      * (Any records linking to this object should be gone already.)
    1334                 :      *
    1335                 :      * When dropping a whole object (subId = 0), remove all pg_depend records
    1336                 :      * for its sub-objects too.
    1337                 :      */
    1338 GIC       83389 :     ScanKeyInit(&key[0],
    1339                 :                 Anum_pg_depend_classid,
    1340                 :                 BTEqualStrategyNumber, F_OIDEQ,
    1341           83389 :                 ObjectIdGetDatum(object->classId));
    1342           83389 :     ScanKeyInit(&key[1],
    1343                 :                 Anum_pg_depend_objid,
    1344                 :                 BTEqualStrategyNumber, F_OIDEQ,
    1345           83389 :                 ObjectIdGetDatum(object->objectId));
    1346 CBC       83389 :     if (object->objectSubId != 0)
    1347                 :     {
    1348 GIC         977 :         ScanKeyInit(&key[2],
    1349 ECB             :                     Anum_pg_depend_objsubid,
    1350                 :                     BTEqualStrategyNumber, F_INT4EQ,
    1351 GIC         977 :                     Int32GetDatum(object->objectSubId));
    1352             977 :         nkeys = 3;
    1353 ECB             :     }
    1354                 :     else
    1355 GIC       82412 :         nkeys = 2;
    1356 ECB             : 
    1357 GIC       83389 :     scan = systable_beginscan(*depRel, DependDependerIndexId, true,
    1358                 :                               NULL, nkeys, key);
    1359 ECB             : 
    1360 CBC      199094 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
    1361                 :     {
    1362 GIC      115705 :         CatalogTupleDelete(*depRel, &tup->t_self);
    1363 ECB             :     }
    1364                 : 
    1365 CBC       83389 :     systable_endscan(scan);
    1366                 : 
    1367                 :     /*
    1368 ECB             :      * Delete shared dependency references related to this object.  Again, if
    1369                 :      * subId = 0, remove records for sub-objects too.
    1370                 :      */
    1371 GIC       83389 :     deleteSharedDependencyRecordsFor(object->classId, object->objectId,
    1372           83389 :                                      object->objectSubId);
    1373 ECB             : 
    1374                 : 
    1375                 :     /*
    1376                 :      * Delete any comments, security labels, or initial privileges associated
    1377                 :      * with this object.  (This is a convenient place to do these things,
    1378                 :      * rather than having every object type know to do it.)
    1379                 :      */
    1380 CBC       83389 :     DeleteComments(object->objectId, object->classId, object->objectSubId);
    1381 GIC       83389 :     DeleteSecurityLabel(object);
    1382           83389 :     DeleteInitPrivs(object);
    1383                 : 
    1384                 :     /*
    1385                 :      * CommandCounterIncrement here to ensure that preceding changes are all
    1386                 :      * visible to the next deletion step.
    1387                 :      */
    1388 CBC       83389 :     CommandCounterIncrement();
    1389 ECB             : 
    1390                 :     /*
    1391                 :      * And we're done!
    1392                 :      */
    1393 GIC       83389 : }
    1394                 : 
    1395                 : /*
    1396 ECB             :  * doDeletion: actually delete a single object
    1397                 :  */
    1398                 : static void
    1399 GIC       83391 : doDeletion(const ObjectAddress *object, int flags)
    1400                 : {
    1401 CBC       83391 :     switch (getObjectClass(object))
    1402                 :     {
    1403 GIC       30418 :         case OCLASS_CLASS:
    1404                 :             {
    1405           30418 :                 char        relKind = get_rel_relkind(object->objectId);
    1406                 : 
    1407 CBC       30418 :                 if (relKind == RELKIND_INDEX ||
    1408                 :                     relKind == RELKIND_PARTITIONED_INDEX)
    1409           10275 :                 {
    1410 GIC       10275 :                     bool        concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
    1411 CBC       10275 :                     bool        concurrent_lock_mode = ((flags & PERFORM_DELETION_CONCURRENT_LOCK) != 0);
    1412                 : 
    1413           10275 :                     Assert(object->objectSubId == 0);
    1414 GIC       10275 :                     index_drop(object->objectId, concurrent, concurrent_lock_mode);
    1415 ECB             :                 }
    1416                 :                 else
    1417                 :                 {
    1418 CBC       20143 :                     if (object->objectSubId != 0)
    1419             977 :                         RemoveAttributeById(object->objectId,
    1420 GIC         977 :                                             object->objectSubId);
    1421 ECB             :                     else
    1422 CBC       19166 :                         heap_drop_with_catalog(object->objectId);
    1423                 :                 }
    1424                 : 
    1425                 :                 /*
    1426 ECB             :                  * for a sequence, in addition to dropping the heap, also
    1427                 :                  * delete pg_sequence tuple
    1428                 :                  */
    1429 GIC       30418 :                 if (relKind == RELKIND_SEQUENCE)
    1430 CBC         422 :                     DeleteSequenceTuple(object->objectId);
    1431 GIC       30418 :                 break;
    1432                 :             }
    1433                 : 
    1434            3066 :         case OCLASS_PROC:
    1435            3066 :             RemoveFunctionById(object->objectId);
    1436            3066 :             break;
    1437 ECB             : 
    1438 CBC       29757 :         case OCLASS_TYPE:
    1439           29757 :             RemoveTypeById(object->objectId);
    1440 GIC       29757 :             break;
    1441                 : 
    1442 CBC        8799 :         case OCLASS_CONSTRAINT:
    1443            8799 :             RemoveConstraintById(object->objectId);
    1444            8798 :             break;
    1445                 : 
    1446            1296 :         case OCLASS_DEFAULT:
    1447            1296 :             RemoveAttrDefaultById(object->objectId);
    1448            1296 :             break;
    1449                 : 
    1450              44 :         case OCLASS_LARGEOBJECT:
    1451              44 :             LargeObjectDrop(object->objectId);
    1452              44 :             break;
    1453                 : 
    1454             336 :         case OCLASS_OPERATOR:
    1455             336 :             RemoveOperatorById(object->objectId);
    1456             336 :             break;
    1457                 : 
    1458            1331 :         case OCLASS_REWRITE:
    1459            1331 :             RemoveRewriteRuleById(object->objectId);
    1460            1330 :             break;
    1461                 : 
    1462            5603 :         case OCLASS_TRIGGER:
    1463            5603 :             RemoveTriggerById(object->objectId);
    1464            5603 :             break;
    1465                 : 
    1466             227 :         case OCLASS_STATISTIC_EXT:
    1467             227 :             RemoveStatisticsById(object->objectId);
    1468             227 :             break;
    1469                 : 
    1470              21 :         case OCLASS_TSCONFIG:
    1471              21 :             RemoveTSConfigurationById(object->objectId);
    1472              21 :             break;
    1473                 : 
    1474              48 :         case OCLASS_EXTENSION:
    1475              48 :             RemoveExtensionById(object->objectId);
    1476              48 :             break;
    1477                 : 
    1478             257 :         case OCLASS_POLICY:
    1479             257 :             RemovePolicyById(object->objectId);
    1480             257 :             break;
    1481                 : 
    1482              96 :         case OCLASS_PUBLICATION_NAMESPACE:
    1483              96 :             RemovePublicationSchemaById(object->objectId);
    1484              96 :             break;
    1485                 : 
    1486             374 :         case OCLASS_PUBLICATION_REL:
    1487             374 :             RemovePublicationRelById(object->objectId);
    1488             374 :             break;
    1489                 : 
    1490             178 :         case OCLASS_PUBLICATION:
    1491             178 :             RemovePublicationById(object->objectId);
    1492             178 :             break;
    1493                 : 
    1494            1540 :         case OCLASS_CAST:
    1495 ECB             :         case OCLASS_COLLATION:
    1496                 :         case OCLASS_CONVERSION:
    1497                 :         case OCLASS_LANGUAGE:
    1498                 :         case OCLASS_OPCLASS:
    1499                 :         case OCLASS_OPFAMILY:
    1500                 :         case OCLASS_AM:
    1501                 :         case OCLASS_AMOP:
    1502                 :         case OCLASS_AMPROC:
    1503                 :         case OCLASS_SCHEMA:
    1504                 :         case OCLASS_TSPARSER:
    1505                 :         case OCLASS_TSDICT:
    1506                 :         case OCLASS_TSTEMPLATE:
    1507                 :         case OCLASS_FDW:
    1508                 :         case OCLASS_FOREIGN_SERVER:
    1509                 :         case OCLASS_USER_MAPPING:
    1510                 :         case OCLASS_DEFACL:
    1511                 :         case OCLASS_EVENT_TRIGGER:
    1512                 :         case OCLASS_TRANSFORM:
    1513                 :         case OCLASS_ROLE_MEMBERSHIP:
    1514 GIC        1540 :             DropObjectById(object);
    1515            1540 :             break;
    1516                 : 
    1517                 :             /*
    1518                 :              * These global object types are not supported here.
    1519                 :              */
    1520 UIC           0 :         case OCLASS_ROLE:
    1521                 :         case OCLASS_DATABASE:
    1522                 :         case OCLASS_TBLSPACE:
    1523 ECB             :         case OCLASS_SUBSCRIPTION:
    1524                 :         case OCLASS_PARAMETER_ACL:
    1525 UIC           0 :             elog(ERROR, "global objects cannot be deleted by doDeletion");
    1526                 :             break;
    1527                 : 
    1528                 :             /*
    1529 EUB             :              * There's intentionally no default: case here; we want the
    1530                 :              * compiler to warn if a new OCLASS hasn't been handled above.
    1531                 :              */
    1532                 :     }
    1533 GIC       83389 : }
    1534 EUB             : 
    1535                 : /*
    1536                 :  * AcquireDeletionLock - acquire a suitable lock for deleting an object
    1537                 :  *
    1538                 :  * Accepts the same flags as performDeletion (though currently only
    1539                 :  * PERFORM_DELETION_CONCURRENTLY does anything).
    1540                 :  *
    1541                 :  * We use LockRelation for relations, and otherwise LockSharedObject or
    1542                 :  * LockDatabaseObject as appropriate for the object type.
    1543                 :  */
    1544                 : void
    1545 GIC      105657 : AcquireDeletionLock(const ObjectAddress *object, int flags)
    1546                 : {
    1547          105657 :     if (object->classId == RelationRelationId)
    1548                 :     {
    1549                 :         /*
    1550                 :          * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
    1551                 :          * the index for the moment.  index_drop() will promote the lock once
    1552                 :          * it's safe to do so.  In all other cases we need full exclusive
    1553 ECB             :          * lock.
    1554                 :          */
    1555 CBC       38597 :         if (flags & PERFORM_DELETION_CONCURRENTLY)
    1556 GIC          52 :             LockRelationOid(object->objectId, ShareUpdateExclusiveLock);
    1557                 :         else
    1558           38545 :             LockRelationOid(object->objectId, AccessExclusiveLock);
    1559                 :     }
    1560 GNC       67060 :     else if (object->classId == AuthMemRelationId)
    1561               6 :         LockSharedObject(object->classId, object->objectId, 0,
    1562                 :                          AccessExclusiveLock);
    1563                 :     else
    1564                 :     {
    1565                 :         /* assume we should lock the whole object not a sub-object */
    1566 CBC       67054 :         LockDatabaseObject(object->classId, object->objectId, 0,
    1567 ECB             :                            AccessExclusiveLock);
    1568                 :     }
    1569 CBC      105657 : }
    1570                 : 
    1571 ECB             : /*
    1572                 :  * ReleaseDeletionLock - release an object deletion lock
    1573                 :  *
    1574                 :  * Companion to AcquireDeletionLock.
    1575                 :  */
    1576                 : void
    1577 CBC         695 : ReleaseDeletionLock(const ObjectAddress *object)
    1578                 : {
    1579 GIC         695 :     if (object->classId == RelationRelationId)
    1580 CBC          22 :         UnlockRelationOid(object->objectId, AccessExclusiveLock);
    1581                 :     else
    1582                 :         /* assume we should lock the whole object not a sub-object */
    1583 GIC         673 :         UnlockDatabaseObject(object->classId, object->objectId, 0,
    1584                 :                              AccessExclusiveLock);
    1585             695 : }
    1586                 : 
    1587                 : /*
    1588 ECB             :  * recordDependencyOnExpr - find expression dependencies
    1589                 :  *
    1590                 :  * This is used to find the dependencies of rules, constraint expressions,
    1591                 :  * etc.
    1592                 :  *
    1593                 :  * Given an expression or query in node-tree form, find all the objects
    1594                 :  * it refers to (tables, columns, operators, functions, etc).  Record
    1595                 :  * a dependency of the specified type from the given depender object
    1596                 :  * to each object mentioned in the expression.
    1597                 :  *
    1598                 :  * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
    1599                 :  * It can be NIL if no such variables are expected.
    1600                 :  */
    1601                 : void
    1602 GIC       74160 : recordDependencyOnExpr(const ObjectAddress *depender,
    1603                 :                        Node *expr, List *rtable,
    1604                 :                        DependencyType behavior)
    1605                 : {
    1606                 :     find_expr_references_context context;
    1607                 : 
    1608           74160 :     context.addrs = new_object_addresses();
    1609                 : 
    1610                 :     /* Set up interpretation for Vars at varlevelsup = 0 */
    1611           74160 :     context.rtables = list_make1(rtable);
    1612                 : 
    1613 ECB             :     /* Scan the expression tree for referenceable objects */
    1614 GIC       74160 :     find_expr_references_walker(expr, &context);
    1615                 : 
    1616                 :     /* Remove any duplicates */
    1617           74160 :     eliminate_duplicate_dependencies(context.addrs);
    1618                 : 
    1619 ECB             :     /* And record 'em */
    1620 GIC       74160 :     recordMultipleDependencies(depender,
    1621           74160 :                                context.addrs->refs, context.addrs->numrefs,
    1622 ECB             :                                behavior);
    1623                 : 
    1624 GIC       74160 :     free_object_addresses(context.addrs);
    1625 CBC       74160 : }
    1626                 : 
    1627                 : /*
    1628 ECB             :  * recordDependencyOnSingleRelExpr - find expression dependencies
    1629                 :  *
    1630                 :  * As above, but only one relation is expected to be referenced (with
    1631                 :  * varno = 1 and varlevelsup = 0).  Pass the relation OID instead of a
    1632                 :  * range table.  An additional frammish is that dependencies on that
    1633                 :  * relation's component columns will be marked with 'self_behavior',
    1634                 :  * whereas 'behavior' is used for everything else; also, if 'reverse_self'
    1635                 :  * is true, those dependencies are reversed so that the columns are made
    1636                 :  * to depend on the table not vice versa.
    1637                 :  *
    1638                 :  * NOTE: the caller should ensure that a whole-table dependency on the
    1639                 :  * specified relation is created separately, if one is needed.  In particular,
    1640                 :  * a whole-row Var "relation.*" will not cause this routine to emit any
    1641                 :  * dependency item.  This is appropriate behavior for subexpressions of an
    1642                 :  * ordinary query, so other cases need to cope as necessary.
    1643                 :  */
    1644                 : void
    1645 GIC        4396 : recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
    1646                 :                                 Node *expr, Oid relId,
    1647                 :                                 DependencyType behavior,
    1648                 :                                 DependencyType self_behavior,
    1649                 :                                 bool reverse_self)
    1650                 : {
    1651                 :     find_expr_references_context context;
    1652 GNC        4396 :     RangeTblEntry rte = {0};
    1653                 : 
    1654 GIC        4396 :     context.addrs = new_object_addresses();
    1655                 : 
    1656 ECB             :     /* We gin up a rather bogus rangetable list to handle Vars */
    1657 GIC        4396 :     rte.type = T_RangeTblEntry;
    1658            4396 :     rte.rtekind = RTE_RELATION;
    1659            4396 :     rte.relid = relId;
    1660            4396 :     rte.relkind = RELKIND_RELATION; /* no need for exactness here */
    1661            4396 :     rte.rellockmode = AccessShareLock;
    1662 ECB             : 
    1663 GIC        4396 :     context.rtables = list_make1(list_make1(&rte));
    1664 ECB             : 
    1665                 :     /* Scan the expression tree for referenceable objects */
    1666 GIC        4396 :     find_expr_references_walker(expr, &context);
    1667 ECB             : 
    1668                 :     /* Remove any duplicates */
    1669 CBC        4396 :     eliminate_duplicate_dependencies(context.addrs);
    1670 ECB             : 
    1671                 :     /* Separate self-dependencies if necessary */
    1672 GIC        4396 :     if ((behavior != self_behavior || reverse_self) &&
    1673 CBC         750 :         context.addrs->numrefs > 0)
    1674                 :     {
    1675                 :         ObjectAddresses *self_addrs;
    1676 ECB             :         ObjectAddress *outobj;
    1677                 :         int         oldref,
    1678                 :                     outrefs;
    1679                 : 
    1680 GIC         747 :         self_addrs = new_object_addresses();
    1681                 : 
    1682 CBC         747 :         outobj = context.addrs->refs;
    1683             747 :         outrefs = 0;
    1684 GIC        3132 :         for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
    1685                 :         {
    1686            2385 :             ObjectAddress *thisobj = context.addrs->refs + oldref;
    1687                 : 
    1688            2385 :             if (thisobj->classId == RelationRelationId &&
    1689             947 :                 thisobj->objectId == relId)
    1690 ECB             :             {
    1691                 :                 /* Move this ref into self_addrs */
    1692 CBC         947 :                 add_exact_object_address(thisobj, self_addrs);
    1693 ECB             :             }
    1694                 :             else
    1695                 :             {
    1696                 :                 /* Keep it in context.addrs */
    1697 GIC        1438 :                 *outobj = *thisobj;
    1698 CBC        1438 :                 outobj++;
    1699            1438 :                 outrefs++;
    1700                 :             }
    1701                 :         }
    1702             747 :         context.addrs->numrefs = outrefs;
    1703                 : 
    1704                 :         /* Record the self-dependencies with the appropriate direction */
    1705 GIC         747 :         if (!reverse_self)
    1706             649 :             recordMultipleDependencies(depender,
    1707 CBC         649 :                                        self_addrs->refs, self_addrs->numrefs,
    1708 ECB             :                                        self_behavior);
    1709                 :         else
    1710                 :         {
    1711                 :             /* Can't use recordMultipleDependencies, so do it the hard way */
    1712                 :             int         selfref;
    1713                 : 
    1714 GIC         237 :             for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
    1715 ECB             :             {
    1716 CBC         139 :                 ObjectAddress *thisobj = self_addrs->refs + selfref;
    1717 ECB             : 
    1718 GIC         139 :                 recordDependencyOn(thisobj, depender, self_behavior);
    1719                 :             }
    1720                 :         }
    1721                 : 
    1722             747 :         free_object_addresses(self_addrs);
    1723                 :     }
    1724 ECB             : 
    1725                 :     /* Record the external dependencies */
    1726 CBC        4396 :     recordMultipleDependencies(depender,
    1727 GIC        4396 :                                context.addrs->refs, context.addrs->numrefs,
    1728 ECB             :                                behavior);
    1729                 : 
    1730 GIC        4396 :     free_object_addresses(context.addrs);
    1731            4396 : }
    1732 ECB             : 
    1733                 : /*
    1734                 :  * Recursively search an expression tree for object references.
    1735                 :  *
    1736                 :  * Note: in many cases we do not need to create dependencies on the datatypes
    1737                 :  * involved in an expression, because we'll have an indirect dependency via
    1738                 :  * some other object.  For instance Var nodes depend on a column which depends
    1739                 :  * on the datatype, and OpExpr nodes depend on the operator which depends on
    1740                 :  * the datatype.  However we do need a type dependency if there is no such
    1741                 :  * indirect dependency, as for example in Const and CoerceToDomain nodes.
    1742                 :  *
    1743                 :  * Similarly, we don't need to create dependencies on collations except where
    1744                 :  * the collation is being freshly introduced to the expression.
    1745                 :  */
    1746                 : static bool
    1747 GIC     5554800 : find_expr_references_walker(Node *node,
    1748                 :                             find_expr_references_context *context)
    1749                 : {
    1750         5554800 :     if (node == NULL)
    1751         1785110 :         return false;
    1752         3769690 :     if (IsA(node, Var))
    1753                 :     {
    1754          920954 :         Var        *var = (Var *) node;
    1755                 :         List       *rtable;
    1756                 :         RangeTblEntry *rte;
    1757 ECB             : 
    1758                 :         /* Find matching rtable entry, or complain if not found */
    1759 GIC      920954 :         if (var->varlevelsup >= list_length(context->rtables))
    1760 LBC           0 :             elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
    1761 CBC      920954 :         rtable = (List *) list_nth(context->rtables, var->varlevelsup);
    1762          920954 :         if (var->varno <= 0 || var->varno > list_length(rtable))
    1763 UIC           0 :             elog(ERROR, "invalid varno %d", var->varno);
    1764 CBC      920954 :         rte = rt_fetch(var->varno, rtable);
    1765                 : 
    1766                 :         /*
    1767                 :          * A whole-row Var references no specific columns, so adds no new
    1768                 :          * dependency.  (We assume that there is a whole-table dependency
    1769 ECB             :          * arising from each underlying rangetable entry.  While we could
    1770 EUB             :          * record such a dependency when finding a whole-row Var that
    1771 ECB             :          * references a relation directly, it's quite unclear how to extend
    1772                 :          * that to whole-row Vars for JOINs, so it seems better to leave the
    1773 EUB             :          * responsibility with the range table.  Note that this poses some
    1774 ECB             :          * risks for identifying dependencies of stand-alone expressions:
    1775                 :          * whole-table references may need to be created separately.)
    1776                 :          */
    1777 GIC      920954 :         if (var->varattno == InvalidAttrNumber)
    1778           17047 :             return false;
    1779          903907 :         if (rte->rtekind == RTE_RELATION)
    1780                 :         {
    1781                 :             /* If it's a plain relation, reference this column */
    1782          668263 :             add_object_address(OCLASS_CLASS, rte->relid, var->varattno,
    1783                 :                                context->addrs);
    1784                 :         }
    1785          235644 :         else if (rte->rtekind == RTE_FUNCTION)
    1786                 :         {
    1787 ECB             :             /* Might need to add a dependency on a composite type's column */
    1788                 :             /* (done out of line, because it's a bit bulky) */
    1789 CBC      116384 :             process_function_rte_ref(rte, var->varattno, context);
    1790                 :         }
    1791                 : 
    1792 ECB             :         /*
    1793                 :          * Vars referencing other RTE types require no additional work.  In
    1794                 :          * particular, a join alias Var can be ignored, because it must
    1795                 :          * reference a merged USING column.  The relevant join input columns
    1796                 :          * will also be referenced in the join qual, and any type coercion
    1797                 :          * functions involved in the alias expression will be dealt with when
    1798                 :          * we scan the RTE itself.
    1799                 :          */
    1800 GIC      903907 :         return false;
    1801                 :     }
    1802         2848736 :     else if (IsA(node, Const))
    1803                 :     {
    1804          459647 :         Const      *con = (Const *) node;
    1805                 :         Oid         objoid;
    1806                 : 
    1807                 :         /* A constant must depend on the constant's datatype */
    1808          459647 :         add_object_address(OCLASS_TYPE, con->consttype, 0,
    1809                 :                            context->addrs);
    1810 ECB             : 
    1811                 :         /*
    1812                 :          * We must also depend on the constant's collation: it could be
    1813                 :          * different from the datatype's, if a CollateExpr was const-folded to
    1814                 :          * a simple constant.  However we can save work in the most common
    1815                 :          * case where the collation is "default", since we know that's pinned.
    1816                 :          */
    1817 GIC      459647 :         if (OidIsValid(con->constcollid) &&
    1818 CBC      195983 :             con->constcollid != DEFAULT_COLLATION_OID)
    1819 GIC       49135 :             add_object_address(OCLASS_COLLATION, con->constcollid, 0,
    1820                 :                                context->addrs);
    1821                 : 
    1822                 :         /*
    1823                 :          * If it's a regclass or similar literal referring to an existing
    1824                 :          * object, add a reference to that object.  (Currently, only the
    1825                 :          * regclass and regconfig cases have any likely use, but we may as
    1826                 :          * well handle all the OID-alias datatypes consistently.)
    1827 ECB             :          */
    1828 CBC      459647 :         if (!con->constisnull)
    1829 ECB             :         {
    1830 GIC      383434 :             switch (con->consttype)
    1831                 :             {
    1832 UIC           0 :                 case REGPROCOID:
    1833                 :                 case REGPROCEDUREOID:
    1834               0 :                     objoid = DatumGetObjectId(con->constvalue);
    1835               0 :                     if (SearchSysCacheExists1(PROCOID,
    1836                 :                                               ObjectIdGetDatum(objoid)))
    1837               0 :                         add_object_address(OCLASS_PROC, objoid, 0,
    1838 ECB             :                                            context->addrs);
    1839 UIC           0 :                     break;
    1840 LBC           0 :                 case REGOPEROID:
    1841                 :                 case REGOPERATOROID:
    1842 UBC           0 :                     objoid = DatumGetObjectId(con->constvalue);
    1843 UIC           0 :                     if (SearchSysCacheExists1(OPEROID,
    1844 EUB             :                                               ObjectIdGetDatum(objoid)))
    1845 UBC           0 :                         add_object_address(OCLASS_OPERATOR, objoid, 0,
    1846                 :                                            context->addrs);
    1847               0 :                     break;
    1848 GIC       11058 :                 case REGCLASSOID:
    1849 GBC       11058 :                     objoid = DatumGetObjectId(con->constvalue);
    1850           11058 :                     if (SearchSysCacheExists1(RELOID,
    1851                 :                                               ObjectIdGetDatum(objoid)))
    1852           11058 :                         add_object_address(OCLASS_CLASS, objoid, 0,
    1853 EUB             :                                            context->addrs);
    1854 GIC       11058 :                     break;
    1855 UBC           0 :                 case REGTYPEOID:
    1856 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
    1857 UBC           0 :                     if (SearchSysCacheExists1(TYPEOID,
    1858 ECB             :                                               ObjectIdGetDatum(objoid)))
    1859 LBC           0 :                         add_object_address(OCLASS_TYPE, objoid, 0,
    1860 ECB             :                                            context->addrs);
    1861 UIC           0 :                     break;
    1862 LBC           0 :                 case REGCOLLATIONOID:
    1863 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
    1864 LBC           0 :                     if (SearchSysCacheExists1(COLLOID,
    1865 EUB             :                                               ObjectIdGetDatum(objoid)))
    1866 UBC           0 :                         add_object_address(OCLASS_COLLATION, objoid, 0,
    1867 EUB             :                                            context->addrs);
    1868 UIC           0 :                     break;
    1869 UBC           0 :                 case REGCONFIGOID:
    1870 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
    1871 UBC           0 :                     if (SearchSysCacheExists1(TSCONFIGOID,
    1872 EUB             :                                               ObjectIdGetDatum(objoid)))
    1873 UBC           0 :                         add_object_address(OCLASS_TSCONFIG, objoid, 0,
    1874 EUB             :                                            context->addrs);
    1875 UIC           0 :                     break;
    1876 UBC           0 :                 case REGDICTIONARYOID:
    1877 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
    1878 UBC           0 :                     if (SearchSysCacheExists1(TSDICTOID,
    1879 EUB             :                                               ObjectIdGetDatum(objoid)))
    1880 UBC           0 :                         add_object_address(OCLASS_TSDICT, objoid, 0,
    1881 EUB             :                                            context->addrs);
    1882 UIC           0 :                     break;
    1883 EUB             : 
    1884 GIC         606 :                 case REGNAMESPACEOID:
    1885 GBC         606 :                     objoid = DatumGetObjectId(con->constvalue);
    1886             606 :                     if (SearchSysCacheExists1(NAMESPACEOID,
    1887 EUB             :                                               ObjectIdGetDatum(objoid)))
    1888 GBC         606 :                         add_object_address(OCLASS_SCHEMA, objoid, 0,
    1889                 :                                            context->addrs);
    1890             606 :                     break;
    1891                 : 
    1892 EUB             :                     /*
    1893                 :                      * Dependencies for regrole should be shared among all
    1894 ECB             :                      * databases, so explicitly inhibit to have dependencies.
    1895                 :                      */
    1896 LBC           0 :                 case REGROLEOID:
    1897 UIC           0 :                     ereport(ERROR,
    1898 ECB             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1899                 :                              errmsg("constant of the type %s cannot be used here",
    1900                 :                                     "regrole")));
    1901                 :                     break;
    1902                 :             }
    1903                 :         }
    1904 GIC      459647 :         return false;
    1905                 :     }
    1906 GBC     2389089 :     else if (IsA(node, Param))
    1907 EUB             :     {
    1908 GIC       49493 :         Param      *param = (Param *) node;
    1909                 : 
    1910                 :         /* A parameter must depend on the parameter's datatype */
    1911           49493 :         add_object_address(OCLASS_TYPE, param->paramtype, 0,
    1912                 :                            context->addrs);
    1913                 :         /* and its collation, just as for Consts */
    1914 CBC       49493 :         if (OidIsValid(param->paramcollid) &&
    1915 GIC        8203 :             param->paramcollid != DEFAULT_COLLATION_OID)
    1916 CBC        4848 :             add_object_address(OCLASS_COLLATION, param->paramcollid, 0,
    1917                 :                                context->addrs);
    1918 ECB             :     }
    1919 GIC     2339596 :     else if (IsA(node, FuncExpr))
    1920                 :     {
    1921 CBC      239349 :         FuncExpr   *funcexpr = (FuncExpr *) node;
    1922                 : 
    1923 GIC      239349 :         add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
    1924 ECB             :                            context->addrs);
    1925                 :         /* fall through to examine arguments */
    1926                 :     }
    1927 GIC     2100247 :     else if (IsA(node, OpExpr))
    1928                 :     {
    1929 CBC      258155 :         OpExpr     *opexpr = (OpExpr *) node;
    1930                 : 
    1931          258155 :         add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
    1932                 :                            context->addrs);
    1933 ECB             :         /* fall through to examine arguments */
    1934                 :     }
    1935 GIC     1842092 :     else if (IsA(node, DistinctExpr))
    1936                 :     {
    1937 CBC           6 :         DistinctExpr *distinctexpr = (DistinctExpr *) node;
    1938                 : 
    1939               6 :         add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
    1940                 :                            context->addrs);
    1941 ECB             :         /* fall through to examine arguments */
    1942                 :     }
    1943 GIC     1842086 :     else if (IsA(node, NullIfExpr))
    1944                 :     {
    1945 CBC         317 :         NullIfExpr *nullifexpr = (NullIfExpr *) node;
    1946                 : 
    1947             317 :         add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
    1948                 :                            context->addrs);
    1949 ECB             :         /* fall through to examine arguments */
    1950                 :     }
    1951 GIC     1841769 :     else if (IsA(node, ScalarArrayOpExpr))
    1952                 :     {
    1953 CBC       21245 :         ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
    1954                 : 
    1955           21245 :         add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
    1956                 :                            context->addrs);
    1957 ECB             :         /* fall through to examine arguments */
    1958                 :     }
    1959 GIC     1820524 :     else if (IsA(node, Aggref))
    1960                 :     {
    1961 CBC        4714 :         Aggref     *aggref = (Aggref *) node;
    1962                 : 
    1963            4714 :         add_object_address(OCLASS_PROC, aggref->aggfnoid, 0,
    1964                 :                            context->addrs);
    1965 ECB             :         /* fall through to examine arguments */
    1966                 :     }
    1967 GIC     1815810 :     else if (IsA(node, WindowFunc))
    1968                 :     {
    1969 CBC         333 :         WindowFunc *wfunc = (WindowFunc *) node;
    1970                 : 
    1971             333 :         add_object_address(OCLASS_PROC, wfunc->winfnoid, 0,
    1972                 :                            context->addrs);
    1973 ECB             :         /* fall through to examine arguments */
    1974                 :     }
    1975 GIC     1815477 :     else if (IsA(node, SubscriptingRef))
    1976                 :     {
    1977 CBC        5531 :         SubscriptingRef *sbsref = (SubscriptingRef *) node;
    1978                 : 
    1979 ECB             :         /*
    1980                 :          * The refexpr should provide adequate dependency on refcontainertype,
    1981                 :          * and that type in turn depends on refelemtype.  However, a custom
    1982                 :          * subscripting handler might set refrestype to something different
    1983                 :          * from either of those, in which case we'd better record it.
    1984                 :          */
    1985 CBC        5531 :         if (sbsref->refrestype != sbsref->refcontainertype &&
    1986 GIC        5498 :             sbsref->refrestype != sbsref->refelemtype)
    1987 LBC           0 :             add_object_address(OCLASS_TYPE, sbsref->refrestype, 0,
    1988                 :                                context->addrs);
    1989                 :         /* fall through to examine arguments */
    1990                 :     }
    1991 GIC     1809946 :     else if (IsA(node, SubPlan))
    1992                 :     {
    1993                 :         /* Extra work needed here if we ever need this case */
    1994 UIC           0 :         elog(ERROR, "already-planned subqueries not supported");
    1995 ECB             :     }
    1996 CBC     1809946 :     else if (IsA(node, FieldSelect))
    1997 EUB             :     {
    1998 GIC       43122 :         FieldSelect *fselect = (FieldSelect *) node;
    1999           43122 :         Oid         argtype = getBaseType(exprType((Node *) fselect->arg));
    2000           43122 :         Oid         reltype = get_typ_typrelid(argtype);
    2001 ECB             : 
    2002                 :         /*
    2003                 :          * We need a dependency on the specific column named in FieldSelect,
    2004 EUB             :          * assuming we can identify the pg_class OID for it.  (Probably we
    2005                 :          * always can at the moment, but in future it might be possible for
    2006 ECB             :          * argtype to be RECORDOID.)  If we can make a column dependency then
    2007                 :          * we shouldn't need a dependency on the column's type; but if we
    2008                 :          * can't, make a dependency on the type, as it might not appear
    2009                 :          * anywhere else in the expression.
    2010                 :          */
    2011 GIC       43122 :         if (OidIsValid(reltype))
    2012           24018 :             add_object_address(OCLASS_CLASS, reltype, fselect->fieldnum,
    2013                 :                                context->addrs);
    2014                 :         else
    2015           19104 :             add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
    2016                 :                                context->addrs);
    2017                 :         /* the collation might not be referenced anywhere else, either */
    2018           43122 :         if (OidIsValid(fselect->resultcollid) &&
    2019            5156 :             fselect->resultcollid != DEFAULT_COLLATION_OID)
    2020 UIC           0 :             add_object_address(OCLASS_COLLATION, fselect->resultcollid, 0,
    2021 ECB             :                                context->addrs);
    2022                 :     }
    2023 GIC     1766824 :     else if (IsA(node, FieldStore))
    2024                 :     {
    2025 CBC          30 :         FieldStore *fstore = (FieldStore *) node;
    2026 GIC          30 :         Oid         reltype = get_typ_typrelid(fstore->resulttype);
    2027                 : 
    2028 ECB             :         /* similar considerations to FieldSelect, but multiple column(s) */
    2029 CBC          30 :         if (OidIsValid(reltype))
    2030 EUB             :         {
    2031                 :             ListCell   *l;
    2032                 : 
    2033 CBC          60 :             foreach(l, fstore->fieldnums)
    2034 GIC          30 :                 add_object_address(OCLASS_CLASS, reltype, lfirst_int(l),
    2035 ECB             :                                    context->addrs);
    2036                 :         }
    2037                 :         else
    2038 UIC           0 :             add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
    2039 ECB             :                                context->addrs);
    2040                 :     }
    2041 GIC     1766794 :     else if (IsA(node, RelabelType))
    2042                 :     {
    2043 CBC       42807 :         RelabelType *relab = (RelabelType *) node;
    2044 ECB             : 
    2045                 :         /* since there is no function dependency, need to depend on type */
    2046 GIC       42807 :         add_object_address(OCLASS_TYPE, relab->resulttype, 0,
    2047                 :                            context->addrs);
    2048 EUB             :         /* the collation might not be referenced anywhere else, either */
    2049 GIC       42807 :         if (OidIsValid(relab->resultcollid) &&
    2050            9442 :             relab->resultcollid != DEFAULT_COLLATION_OID)
    2051 CBC        8787 :             add_object_address(OCLASS_COLLATION, relab->resultcollid, 0,
    2052                 :                                context->addrs);
    2053 ECB             :     }
    2054 GIC     1723987 :     else if (IsA(node, CoerceViaIO))
    2055                 :     {
    2056 CBC        7100 :         CoerceViaIO *iocoerce = (CoerceViaIO *) node;
    2057                 : 
    2058                 :         /* since there is no exposed function, need to depend on type */
    2059            7100 :         add_object_address(OCLASS_TYPE, iocoerce->resulttype, 0,
    2060 ECB             :                            context->addrs);
    2061                 :         /* the collation might not be referenced anywhere else, either */
    2062 GIC        7100 :         if (OidIsValid(iocoerce->resultcollid) &&
    2063            7032 :             iocoerce->resultcollid != DEFAULT_COLLATION_OID)
    2064 CBC        2121 :             add_object_address(OCLASS_COLLATION, iocoerce->resultcollid, 0,
    2065                 :                                context->addrs);
    2066 ECB             :     }
    2067 GIC     1716887 :     else if (IsA(node, ArrayCoerceExpr))
    2068                 :     {
    2069 CBC        1518 :         ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
    2070                 : 
    2071                 :         /* as above, depend on type */
    2072            1518 :         add_object_address(OCLASS_TYPE, acoerce->resulttype, 0,
    2073 ECB             :                            context->addrs);
    2074                 :         /* the collation might not be referenced anywhere else, either */
    2075 GIC        1518 :         if (OidIsValid(acoerce->resultcollid) &&
    2076             609 :             acoerce->resultcollid != DEFAULT_COLLATION_OID)
    2077 CBC         303 :             add_object_address(OCLASS_COLLATION, acoerce->resultcollid, 0,
    2078                 :                                context->addrs);
    2079 ECB             :         /* fall through to examine arguments */
    2080                 :     }
    2081 GIC     1715369 :     else if (IsA(node, ConvertRowtypeExpr))
    2082 ECB             :     {
    2083 UIC           0 :         ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
    2084                 : 
    2085 ECB             :         /* since there is no function dependency, need to depend on type */
    2086 LBC           0 :         add_object_address(OCLASS_TYPE, cvt->resulttype, 0,
    2087 ECB             :                            context->addrs);
    2088                 :     }
    2089 GIC     1715369 :     else if (IsA(node, CollateExpr))
    2090                 :     {
    2091 CBC          51 :         CollateExpr *coll = (CollateExpr *) node;
    2092                 : 
    2093 GBC          51 :         add_object_address(OCLASS_COLLATION, coll->collOid, 0,
    2094                 :                            context->addrs);
    2095                 :     }
    2096         1715318 :     else if (IsA(node, RowExpr))
    2097                 :     {
    2098 GIC          27 :         RowExpr    *rowexpr = (RowExpr *) node;
    2099 ECB             : 
    2100 GIC          27 :         add_object_address(OCLASS_TYPE, rowexpr->row_typeid, 0,
    2101 ECB             :                            context->addrs);
    2102                 :     }
    2103 CBC     1715291 :     else if (IsA(node, RowCompareExpr))
    2104                 :     {
    2105 GIC           9 :         RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    2106 ECB             :         ListCell   *l;
    2107                 : 
    2108 CBC          27 :         foreach(l, rcexpr->opnos)
    2109                 :         {
    2110              18 :             add_object_address(OCLASS_OPERATOR, lfirst_oid(l), 0,
    2111                 :                                context->addrs);
    2112                 :         }
    2113              27 :         foreach(l, rcexpr->opfamilies)
    2114                 :         {
    2115              18 :             add_object_address(OCLASS_OPFAMILY, lfirst_oid(l), 0,
    2116                 :                                context->addrs);
    2117                 :         }
    2118 ECB             :         /* fall through to examine arguments */
    2119                 :     }
    2120 CBC     1715282 :     else if (IsA(node, CoerceToDomain))
    2121                 :     {
    2122 GIC      194864 :         CoerceToDomain *cd = (CoerceToDomain *) node;
    2123 ECB             : 
    2124 GIC      194864 :         add_object_address(OCLASS_TYPE, cd->resulttype, 0,
    2125 ECB             :                            context->addrs);
    2126                 :     }
    2127 GIC     1520418 :     else if (IsA(node, NextValueExpr))
    2128                 :     {
    2129 UIC           0 :         NextValueExpr *nve = (NextValueExpr *) node;
    2130 ECB             : 
    2131 UIC           0 :         add_object_address(OCLASS_CLASS, nve->seqid, 0,
    2132 ECB             :                            context->addrs);
    2133                 :     }
    2134 CBC     1520418 :     else if (IsA(node, OnConflictExpr))
    2135                 :     {
    2136 GIC           9 :         OnConflictExpr *onconflict = (OnConflictExpr *) node;
    2137 ECB             : 
    2138 GIC           9 :         if (OidIsValid(onconflict->constraint))
    2139 UBC           0 :             add_object_address(OCLASS_CONSTRAINT, onconflict->constraint, 0,
    2140                 :                                context->addrs);
    2141 EUB             :         /* fall through to examine arguments */
    2142                 :     }
    2143 GIC     1520409 :     else if (IsA(node, SortGroupClause))
    2144 ECB             :     {
    2145 GIC       36645 :         SortGroupClause *sgc = (SortGroupClause *) node;
    2146 ECB             : 
    2147 GIC       36645 :         add_object_address(OCLASS_OPERATOR, sgc->eqop, 0,
    2148 ECB             :                            context->addrs);
    2149 GBC       36645 :         if (OidIsValid(sgc->sortop))
    2150 GIC       36645 :             add_object_address(OCLASS_OPERATOR, sgc->sortop, 0,
    2151                 :                                context->addrs);
    2152           36645 :         return false;
    2153 ECB             :     }
    2154 GIC     1483764 :     else if (IsA(node, WindowClause))
    2155 ECB             :     {
    2156 GIC         333 :         WindowClause *wc = (WindowClause *) node;
    2157 ECB             : 
    2158 GIC         333 :         if (OidIsValid(wc->startInRangeFunc))
    2159 CBC           3 :             add_object_address(OCLASS_PROC, wc->startInRangeFunc, 0,
    2160 ECB             :                                context->addrs);
    2161 GIC         333 :         if (OidIsValid(wc->endInRangeFunc))
    2162 CBC           3 :             add_object_address(OCLASS_PROC, wc->endInRangeFunc, 0,
    2163                 :                                context->addrs);
    2164             333 :         if (OidIsValid(wc->inRangeColl) &&
    2165 UIC           0 :             wc->inRangeColl != DEFAULT_COLLATION_OID)
    2166 LBC           0 :             add_object_address(OCLASS_COLLATION, wc->inRangeColl, 0,
    2167                 :                                context->addrs);
    2168 ECB             :         /* fall through to examine substructure */
    2169                 :     }
    2170 GIC     1483431 :     else if (IsA(node, CTECycleClause))
    2171 ECB             :     {
    2172 CBC           6 :         CTECycleClause *cc = (CTECycleClause *) node;
    2173                 : 
    2174               6 :         if (OidIsValid(cc->cycle_mark_type))
    2175 GBC           6 :             add_object_address(OCLASS_TYPE, cc->cycle_mark_type, 0,
    2176 EUB             :                                context->addrs);
    2177 GIC           6 :         if (OidIsValid(cc->cycle_mark_collation))
    2178               3 :             add_object_address(OCLASS_COLLATION, cc->cycle_mark_collation, 0,
    2179                 :                                context->addrs);
    2180 CBC           6 :         if (OidIsValid(cc->cycle_mark_neop))
    2181 GIC           6 :             add_object_address(OCLASS_OPERATOR, cc->cycle_mark_neop, 0,
    2182 ECB             :                                context->addrs);
    2183                 :         /* fall through to examine substructure */
    2184                 :     }
    2185 CBC     1483425 :     else if (IsA(node, Query))
    2186                 :     {
    2187 ECB             :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
    2188 CBC      100773 :         Query      *query = (Query *) node;
    2189                 :         ListCell   *lc;
    2190 ECB             :         bool        result;
    2191                 : 
    2192                 :         /*
    2193                 :          * Add whole-relation refs for each plain relation mentioned in the
    2194                 :          * subquery's rtable, and ensure we add refs for any type-coercion
    2195                 :          * functions used in join alias lists.
    2196                 :          *
    2197                 :          * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
    2198                 :          * RTEs, subqueries, etc, so no need to do that here.  But we must
    2199                 :          * tell it not to visit join alias lists, or we'll add refs for join
    2200                 :          * input columns whether or not they are actually used in our query.
    2201                 :          *
    2202                 :          * Note: we don't need to worry about collations mentioned in
    2203                 :          * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
    2204                 :          * collations referenced in other parts of the Query.  We do have to
    2205                 :          * worry about collations mentioned in RTE_FUNCTION, but we take care
    2206                 :          * of those when we recurse to the RangeTblFunction node(s).
    2207                 :          */
    2208 GIC      328664 :         foreach(lc, query->rtable)
    2209                 :         {
    2210          227891 :             RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
    2211                 : 
    2212          227891 :             switch (rte->rtekind)
    2213                 :             {
    2214          141618 :                 case RTE_RELATION:
    2215          141618 :                     add_object_address(OCLASS_CLASS, rte->relid, 0,
    2216                 :                                        context->addrs);
    2217          141618 :                     break;
    2218 CBC       44104 :                 case RTE_JOIN:
    2219                 : 
    2220 ECB             :                     /*
    2221                 :                      * Examine joinaliasvars entries only for merged JOIN
    2222                 :                      * USING columns.  Only those entries could contain
    2223                 :                      * type-coercion functions.  Also, their join input
    2224                 :                      * columns must be referenced in the join quals, so this
    2225                 :                      * won't accidentally add refs to otherwise-unused join
    2226                 :                      * input columns.  (We want to ref the type coercion
    2227                 :                      * functions even if the merged column isn't explicitly
    2228                 :                      * used anywhere, to protect possible expansion of the
    2229                 :                      * join RTE as a whole-row var, and because it seems like
    2230                 :                      * a bad idea to allow dropping a function that's present
    2231                 :                      * in our query tree, whether or not it could get called.)
    2232                 :                      */
    2233 GIC       44104 :                     context->rtables = lcons(query->rtable, context->rtables);
    2234           44204 :                     for (int i = 0; i < rte->joinmergedcols; i++)
    2235                 :                     {
    2236             100 :                         Node       *aliasvar = list_nth(rte->joinaliasvars, i);
    2237                 : 
    2238             100 :                         if (!IsA(aliasvar, Var))
    2239              24 :                             find_expr_references_walker(aliasvar, context);
    2240                 :                     }
    2241           44104 :                     context->rtables = list_delete_first(context->rtables);
    2242           44104 :                     break;
    2243 CBC       42169 :                 default:
    2244           42169 :                     break;
    2245                 :             }
    2246 ECB             :         }
    2247                 : 
    2248                 :         /*
    2249                 :          * If the query is an INSERT or UPDATE, we should create a dependency
    2250                 :          * on each target column, to prevent the specific target column from
    2251                 :          * being dropped.  Although we will visit the TargetEntry nodes again
    2252                 :          * during query_tree_walker, we won't have enough context to do this
    2253                 :          * conveniently, so do it here.
    2254                 :          */
    2255 GIC      100773 :         if (query->commandType == CMD_INSERT ||
    2256          100554 :             query->commandType == CMD_UPDATE)
    2257                 :         {
    2258                 :             RangeTblEntry *rte;
    2259                 : 
    2260             620 :             if (query->resultRelation <= 0 ||
    2261             310 :                 query->resultRelation > list_length(query->rtable))
    2262 UIC           0 :                 elog(ERROR, "invalid resultRelation %d",
    2263                 :                      query->resultRelation);
    2264 GIC         310 :             rte = rt_fetch(query->resultRelation, query->rtable);
    2265 CBC         310 :             if (rte->rtekind == RTE_RELATION)
    2266 ECB             :             {
    2267 GIC         960 :                 foreach(lc, query->targetList)
    2268                 :                 {
    2269             650 :                     TargetEntry *tle = (TargetEntry *) lfirst(lc);
    2270 ECB             : 
    2271 CBC         650 :                     if (tle->resjunk)
    2272 GBC           3 :                         continue;   /* ignore junk tlist items */
    2273 GIC         647 :                     add_object_address(OCLASS_CLASS, rte->relid, tle->resno,
    2274 ECB             :                                        context->addrs);
    2275                 :                 }
    2276                 :             }
    2277                 :         }
    2278                 : 
    2279                 :         /*
    2280                 :          * Add dependencies on constraints listed in query's constraintDeps
    2281                 :          */
    2282 CBC      100799 :         foreach(lc, query->constraintDeps)
    2283 ECB             :         {
    2284 GIC          26 :             add_object_address(OCLASS_CONSTRAINT, lfirst_oid(lc), 0,
    2285                 :                                context->addrs);
    2286                 :         }
    2287                 : 
    2288                 :         /* Examine substructure of query */
    2289          100773 :         context->rtables = lcons(query->rtable, context->rtables);
    2290          100773 :         result = query_tree_walker(query,
    2291                 :                                    find_expr_references_walker,
    2292 ECB             :                                    (void *) context,
    2293                 :                                    QTW_IGNORE_JOINALIASES |
    2294                 :                                    QTW_EXAMINE_SORTGROUP);
    2295 GIC      100773 :         context->rtables = list_delete_first(context->rtables);
    2296          100773 :         return result;
    2297                 :     }
    2298         1382652 :     else if (IsA(node, SetOperationStmt))
    2299 ECB             :     {
    2300 CBC       11568 :         SetOperationStmt *setop = (SetOperationStmt *) node;
    2301                 : 
    2302                 :         /* we need to look at the groupClauses for operator references */
    2303 GIC       11568 :         find_expr_references_walker((Node *) setop->groupClauses, context);
    2304                 :         /* fall through to examine child nodes */
    2305 ECB             :     }
    2306 CBC     1371084 :     else if (IsA(node, RangeTblFunction))
    2307                 :     {
    2308           13880 :         RangeTblFunction *rtfunc = (RangeTblFunction *) node;
    2309                 :         ListCell   *ct;
    2310 ECB             : 
    2311                 :         /*
    2312                 :          * Add refs for any datatypes and collations used in a column
    2313                 :          * definition list for a RECORD function.  (For other cases, it should
    2314                 :          * be enough to depend on the function itself.)
    2315                 :          */
    2316 CBC       13934 :         foreach(ct, rtfunc->funccoltypes)
    2317                 :         {
    2318              54 :             add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
    2319                 :                                context->addrs);
    2320                 :         }
    2321 GIC       13934 :         foreach(ct, rtfunc->funccolcollations)
    2322                 :         {
    2323              54 :             Oid         collid = lfirst_oid(ct);
    2324                 : 
    2325              54 :             if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
    2326 LBC           0 :                 add_object_address(OCLASS_COLLATION, collid, 0,
    2327                 :                                    context->addrs);
    2328 ECB             :         }
    2329                 :     }
    2330 GIC     1357204 :     else if (IsA(node, TableFunc))
    2331 ECB             :     {
    2332 GIC           8 :         TableFunc  *tf = (TableFunc *) node;
    2333 ECB             :         ListCell   *ct;
    2334                 : 
    2335                 :         /*
    2336 EUB             :          * Add refs for the datatypes and collations used in the TableFunc.
    2337                 :          */
    2338 GIC          44 :         foreach(ct, tf->coltypes)
    2339                 :         {
    2340 CBC          36 :             add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
    2341                 :                                context->addrs);
    2342 ECB             :         }
    2343 GIC          44 :         foreach(ct, tf->colcollations)
    2344                 :         {
    2345              36 :             Oid         collid = lfirst_oid(ct);
    2346                 : 
    2347              36 :             if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
    2348 LBC           0 :                 add_object_address(OCLASS_COLLATION, collid, 0,
    2349                 :                                    context->addrs);
    2350 ECB             :         }
    2351                 :     }
    2352 GIC     1357196 :     else if (IsA(node, TableSampleClause))
    2353 ECB             :     {
    2354 GIC          10 :         TableSampleClause *tsc = (TableSampleClause *) node;
    2355 ECB             : 
    2356 GIC          10 :         add_object_address(OCLASS_PROC, tsc->tsmhandler, 0,
    2357 ECB             :                            context->addrs);
    2358 EUB             :         /* fall through to examine arguments */
    2359                 :     }
    2360                 : 
    2361 GIC     2251671 :     return expression_tree_walker(node, find_expr_references_walker,
    2362 ECB             :                                   (void *) context);
    2363                 : }
    2364                 : 
    2365                 : /*
    2366                 :  * find_expr_references_walker subroutine: handle a Var reference
    2367                 :  * to an RTE_FUNCTION RTE
    2368                 :  */
    2369                 : static void
    2370 GIC      116384 : process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum,
    2371 ECB             :                          find_expr_references_context *context)
    2372                 : {
    2373 GIC      116384 :     int         atts_done = 0;
    2374                 :     ListCell   *lc;
    2375                 : 
    2376                 :     /*
    2377                 :      * Identify which RangeTblFunction produces this attnum, and see if it
    2378                 :      * returns a composite type.  If so, we'd better make a dependency on the
    2379                 :      * referenced column of the composite type (or actually, of its associated
    2380 ECB             :      * relation).
    2381                 :      */
    2382 GIC      116495 :     foreach(lc, rte->functions)
    2383 ECB             :     {
    2384 GIC      116450 :         RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
    2385                 : 
    2386          116450 :         if (attnum > atts_done &&
    2387          116450 :             attnum <= atts_done + rtfunc->funccolcount)
    2388                 :         {
    2389                 :             TupleDesc   tupdesc;
    2390                 : 
    2391          116339 :             tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr, true);
    2392 CBC      116339 :             if (tupdesc && tupdesc->tdtypeid != RECORDOID)
    2393                 :             {
    2394 ECB             :                 /*
    2395                 :                  * Named composite type, so individual columns could get
    2396                 :                  * dropped.  Make a dependency on this specific column.
    2397                 :                  */
    2398 GIC         111 :                 Oid         reltype = get_typ_typrelid(tupdesc->tdtypeid);
    2399                 : 
    2400             111 :                 Assert(attnum - atts_done <= tupdesc->natts);
    2401 CBC         111 :                 if (OidIsValid(reltype))    /* can this fail? */
    2402             111 :                     add_object_address(OCLASS_CLASS, reltype,
    2403                 :                                        attnum - atts_done,
    2404                 :                                        context->addrs);
    2405 GIC      116339 :                 return;
    2406                 :             }
    2407                 :             /* Nothing to do; function's result type is handled elsewhere */
    2408 CBC      116228 :             return;
    2409                 :         }
    2410             111 :         atts_done += rtfunc->funccolcount;
    2411 ECB             :     }
    2412                 : 
    2413                 :     /* If we get here, must be looking for the ordinality column */
    2414 GIC          45 :     if (rte->funcordinality && attnum == atts_done + 1)
    2415 CBC          45 :         return;
    2416                 : 
    2417                 :     /* this probably can't happen ... */
    2418 LBC           0 :     ereport(ERROR,
    2419                 :             (errcode(ERRCODE_UNDEFINED_COLUMN),
    2420 ECB             :              errmsg("column %d of relation \"%s\" does not exist",
    2421                 :                     attnum, rte->eref->aliasname)));
    2422                 : }
    2423                 : 
    2424                 : /*
    2425                 :  * Given an array of dependency references, eliminate any duplicates.
    2426                 :  */
    2427                 : static void
    2428 GBC      469781 : eliminate_duplicate_dependencies(ObjectAddresses *addrs)
    2429                 : {
    2430                 :     ObjectAddress *priorobj;
    2431                 :     int         oldref,
    2432                 :                 newrefs;
    2433                 : 
    2434                 :     /*
    2435                 :      * We can't sort if the array has "extra" data, because there's no way to
    2436                 :      * keep it in sync.  Fortunately that combination of features is not
    2437                 :      * needed.
    2438 ECB             :      */
    2439 GIC      469781 :     Assert(!addrs->extras);
    2440                 : 
    2441          469781 :     if (addrs->numrefs <= 1)
    2442          143659 :         return;                 /* nothing to do */
    2443                 : 
    2444                 :     /* Sort the refs so that duplicates are adjacent */
    2445 GNC      326122 :     qsort(addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
    2446                 :           object_address_comparator);
    2447                 : 
    2448                 :     /* Remove dups */
    2449 CBC      326122 :     priorobj = addrs->refs;
    2450 GIC      326122 :     newrefs = 1;
    2451 CBC     3738996 :     for (oldref = 1; oldref < addrs->numrefs; oldref++)
    2452 ECB             :     {
    2453 GIC     3412874 :         ObjectAddress *thisobj = addrs->refs + oldref;
    2454                 : 
    2455 CBC     3412874 :         if (priorobj->classId == thisobj->classId &&
    2456 GIC     3006294 :             priorobj->objectId == thisobj->objectId)
    2457                 :         {
    2458         2125120 :             if (priorobj->objectSubId == thisobj->objectSubId)
    2459 CBC     1679332 :                 continue;       /* identical, so drop thisobj */
    2460 ECB             : 
    2461                 :             /*
    2462                 :              * If we have a whole-object reference and a reference to a part
    2463                 :              * of the same object, we don't need the whole-object reference
    2464                 :              * (for example, we don't need to reference both table foo and
    2465                 :              * column foo.bar).  The whole-object reference will always appear
    2466                 :              * first in the sorted list.
    2467                 :              */
    2468 CBC      445788 :             if (priorobj->objectSubId == 0)
    2469 ECB             :             {
    2470                 :                 /* replace whole ref with partial */
    2471 GIC      104572 :                 priorobj->objectSubId = thisobj->objectSubId;
    2472          104572 :                 continue;
    2473                 :             }
    2474                 :         }
    2475                 :         /* Not identical, so add thisobj to output set */
    2476         1628970 :         priorobj++;
    2477         1628970 :         *priorobj = *thisobj;
    2478 CBC     1628970 :         newrefs++;
    2479                 :     }
    2480                 : 
    2481          326122 :     addrs->numrefs = newrefs;
    2482 ECB             : }
    2483                 : 
    2484                 : /*
    2485                 :  * qsort comparator for ObjectAddress items
    2486                 :  */
    2487                 : static int
    2488 CBC    12692148 : object_address_comparator(const void *a, const void *b)
    2489                 : {
    2490 GIC    12692148 :     const ObjectAddress *obja = (const ObjectAddress *) a;
    2491 CBC    12692148 :     const ObjectAddress *objb = (const ObjectAddress *) b;
    2492                 : 
    2493                 :     /*
    2494                 :      * Primary sort key is OID descending.  Most of the time, this will result
    2495                 :      * in putting newer objects before older ones, which is likely to be the
    2496                 :      * right order to delete in.
    2497                 :      */
    2498        12692148 :     if (obja->objectId > objb->objectId)
    2499 GIC     3464856 :         return -1;
    2500 CBC     9227292 :     if (obja->objectId < objb->objectId)
    2501         5503594 :         return 1;
    2502                 : 
    2503                 :     /*
    2504                 :      * Next sort on catalog ID, in case identical OIDs appear in different
    2505                 :      * catalogs.  Sort direction is pretty arbitrary here.
    2506                 :      */
    2507 GIC     3723698 :     if (obja->classId < objb->classId)
    2508 LBC           0 :         return -1;
    2509 CBC     3723698 :     if (obja->classId > objb->classId)
    2510 LBC           0 :         return 1;
    2511 ECB             : 
    2512                 :     /*
    2513                 :      * Last, sort on object subId.
    2514                 :      *
    2515                 :      * We sort the subId as an unsigned int so that 0 (the whole object) will
    2516                 :      * come first.  This is essential for eliminate_duplicate_dependencies,
    2517                 :      * and is also the best order for findDependentObjects.
    2518 EUB             :      */
    2519 CBC     3723698 :     if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
    2520 GBC      850673 :         return -1;
    2521 GIC     2873025 :     if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
    2522          879463 :         return 1;
    2523         1993562 :     return 0;
    2524                 : }
    2525                 : 
    2526                 : /*
    2527                 :  * Routines for handling an expansible array of ObjectAddress items.
    2528                 :  *
    2529 ECB             :  * new_object_addresses: create a new ObjectAddresses array.
    2530                 :  */
    2531                 : ObjectAddresses *
    2532 CBC      497366 : new_object_addresses(void)
    2533 ECB             : {
    2534                 :     ObjectAddresses *addrs;
    2535                 : 
    2536 GIC      497366 :     addrs = palloc(sizeof(ObjectAddresses));
    2537                 : 
    2538          497366 :     addrs->numrefs = 0;
    2539          497366 :     addrs->maxrefs = 32;
    2540          497366 :     addrs->refs = (ObjectAddress *)
    2541          497366 :         palloc(addrs->maxrefs * sizeof(ObjectAddress));
    2542 CBC      497366 :     addrs->extras = NULL;        /* until/unless needed */
    2543                 : 
    2544 GIC      497366 :     return addrs;
    2545                 : }
    2546 ECB             : 
    2547                 : /*
    2548                 :  * Add an entry to an ObjectAddresses array.
    2549                 :  *
    2550                 :  * It is convenient to specify the class by ObjectClass rather than directly
    2551                 :  * by catalog OID.
    2552                 :  */
    2553                 : static void
    2554 CBC     2283748 : add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
    2555                 :                    ObjectAddresses *addrs)
    2556                 : {
    2557                 :     ObjectAddress *item;
    2558                 : 
    2559                 :     /* enlarge array if needed */
    2560 GIC     2283748 :     if (addrs->numrefs >= addrs->maxrefs)
    2561                 :     {
    2562           33962 :         addrs->maxrefs *= 2;
    2563           33962 :         addrs->refs = (ObjectAddress *)
    2564 CBC       33962 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
    2565 GIC       33962 :         Assert(!addrs->extras);
    2566 ECB             :     }
    2567                 :     /* record this item */
    2568 CBC     2283748 :     item = addrs->refs + addrs->numrefs;
    2569         2283748 :     item->classId = object_classes[oclass];
    2570 GIC     2283748 :     item->objectId = objectId;
    2571         2283748 :     item->objectSubId = subId;
    2572 CBC     2283748 :     addrs->numrefs++;
    2573         2283748 : }
    2574 ECB             : 
    2575                 : /*
    2576                 :  * Add an entry to an ObjectAddresses array.
    2577                 :  *
    2578                 :  * As above, but specify entry exactly.
    2579                 :  */
    2580                 : void
    2581 GIC     1570639 : add_exact_object_address(const ObjectAddress *object,
    2582                 :                          ObjectAddresses *addrs)
    2583                 : {
    2584                 :     ObjectAddress *item;
    2585 ECB             : 
    2586                 :     /* enlarge array if needed */
    2587 GIC     1570639 :     if (addrs->numrefs >= addrs->maxrefs)
    2588                 :     {
    2589              13 :         addrs->maxrefs *= 2;
    2590              13 :         addrs->refs = (ObjectAddress *)
    2591 CBC          13 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
    2592 GIC          13 :         Assert(!addrs->extras);
    2593 ECB             :     }
    2594                 :     /* record this item */
    2595 CBC     1570639 :     item = addrs->refs + addrs->numrefs;
    2596         1570639 :     *item = *object;
    2597 GIC     1570639 :     addrs->numrefs++;
    2598         1570639 : }
    2599 ECB             : 
    2600                 : /*
    2601                 :  * Add an entry to an ObjectAddresses array.
    2602                 :  *
    2603                 :  * As above, but specify entry exactly and provide some "extra" data too.
    2604                 :  */
    2605                 : static void
    2606 GIC       85912 : add_exact_object_address_extra(const ObjectAddress *object,
    2607                 :                                const ObjectAddressExtra *extra,
    2608                 :                                ObjectAddresses *addrs)
    2609                 : {
    2610 ECB             :     ObjectAddress *item;
    2611                 :     ObjectAddressExtra *itemextra;
    2612                 : 
    2613                 :     /* allocate extra space if first time */
    2614 GIC       85912 :     if (!addrs->extras)
    2615           13772 :         addrs->extras = (ObjectAddressExtra *)
    2616           13772 :             palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
    2617                 : 
    2618 ECB             :     /* enlarge array if needed */
    2619 CBC       85912 :     if (addrs->numrefs >= addrs->maxrefs)
    2620 ECB             :     {
    2621 GIC         316 :         addrs->maxrefs *= 2;
    2622             316 :         addrs->refs = (ObjectAddress *)
    2623 CBC         316 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
    2624 GIC         316 :         addrs->extras = (ObjectAddressExtra *)
    2625 CBC         316 :             repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
    2626 ECB             :     }
    2627                 :     /* record this item */
    2628 CBC       85912 :     item = addrs->refs + addrs->numrefs;
    2629           85912 :     *item = *object;
    2630 GIC       85912 :     itemextra = addrs->extras + addrs->numrefs;
    2631           85912 :     *itemextra = *extra;
    2632 CBC       85912 :     addrs->numrefs++;
    2633           85912 : }
    2634 ECB             : 
    2635                 : /*
    2636                 :  * Test whether an object is present in an ObjectAddresses array.
    2637                 :  *
    2638                 :  * We return "true" if object is a subobject of something in the array, too.
    2639                 :  */
    2640                 : bool
    2641 GIC         304 : object_address_present(const ObjectAddress *object,
    2642                 :                        const ObjectAddresses *addrs)
    2643                 : {
    2644                 :     int         i;
    2645 ECB             : 
    2646 GIC        1010 :     for (i = addrs->numrefs - 1; i >= 0; i--)
    2647                 :     {
    2648             706 :         const ObjectAddress *thisobj = addrs->refs + i;
    2649                 : 
    2650 CBC         706 :         if (object->classId == thisobj->classId &&
    2651 GIC         184 :             object->objectId == thisobj->objectId)
    2652 ECB             :         {
    2653 UIC           0 :             if (object->objectSubId == thisobj->objectSubId ||
    2654 LBC           0 :                 thisobj->objectSubId == 0)
    2655               0 :                 return true;
    2656                 :         }
    2657 EUB             :     }
    2658                 : 
    2659 GBC         304 :     return false;
    2660                 : }
    2661                 : 
    2662                 : /*
    2663 ECB             :  * As above, except that if the object is present then also OR the given
    2664                 :  * flags into its associated extra data (which must exist).
    2665                 :  */
    2666                 : static bool
    2667 GIC      106023 : object_address_present_add_flags(const ObjectAddress *object,
    2668                 :                                  int flags,
    2669                 :                                  ObjectAddresses *addrs)
    2670                 : {
    2671 CBC      106023 :     bool        result = false;
    2672                 :     int         i;
    2673                 : 
    2674 GIC     2633940 :     for (i = addrs->numrefs - 1; i >= 0; i--)
    2675 ECB             :     {
    2676 GIC     2527917 :         ObjectAddress *thisobj = addrs->refs + i;
    2677                 : 
    2678 CBC     2527917 :         if (object->classId == thisobj->classId &&
    2679 GIC      831816 :             object->objectId == thisobj->objectId)
    2680 ECB             :         {
    2681 GIC       19401 :             if (object->objectSubId == thisobj->objectSubId)
    2682 ECB             :             {
    2683 CBC       19185 :                 ObjectAddressExtra *thisextra = addrs->extras + i;
    2684                 : 
    2685           19185 :                 thisextra->flags |= flags;
    2686 GIC       19185 :                 result = true;
    2687 ECB             :             }
    2688 GIC         216 :             else if (thisobj->objectSubId == 0)
    2689 ECB             :             {
    2690                 :                 /*
    2691                 :                  * We get here if we find a need to delete a column after
    2692                 :                  * having already decided to drop its whole table.  Obviously
    2693                 :                  * we no longer need to drop the subobject, so report that we
    2694                 :                  * found the subobject in the array.  But don't plaster its
    2695                 :                  * flags on the whole object.
    2696                 :                  */
    2697 GIC         210 :                 result = true;
    2698                 :             }
    2699               6 :             else if (object->objectSubId == 0)
    2700                 :             {
    2701 ECB             :                 /*
    2702                 :                  * We get here if we find a need to delete a whole table after
    2703                 :                  * having already decided to drop one of its columns.  We
    2704                 :                  * can't report that the whole object is in the array, but we
    2705                 :                  * should mark the subobject with the whole object's flags.
    2706                 :                  *
    2707                 :                  * It might seem attractive to physically delete the column's
    2708                 :                  * array entry, or at least mark it as no longer needing
    2709                 :                  * separate deletion.  But that could lead to, e.g., dropping
    2710                 :                  * the column's datatype before we drop the table, which does
    2711                 :                  * not seem like a good idea.  This is a very rare situation
    2712                 :                  * in practice, so we just take the hit of doing a separate
    2713                 :                  * DROP COLUMN action even though we know we're gonna delete
    2714                 :                  * the table later.
    2715                 :                  *
    2716                 :                  * What we can do, though, is mark this as a subobject so that
    2717                 :                  * we don't report it separately, which is confusing because
    2718                 :                  * it's unpredictable whether it happens or not.  But do so
    2719                 :                  * only if flags != 0 (flags == 0 is a read-only probe).
    2720                 :                  *
    2721                 :                  * Because there could be other subobjects of this object in
    2722                 :                  * the array, this case means we always have to loop through
    2723                 :                  * the whole array; we cannot exit early on a match.
    2724                 :                  */
    2725 GIC           3 :                 ObjectAddressExtra *thisextra = addrs->extras + i;
    2726                 : 
    2727               3 :                 if (flags)
    2728               3 :                     thisextra->flags |= (flags | DEPFLAG_SUBOBJECT);
    2729 ECB             :             }
    2730                 :         }
    2731                 :     }
    2732                 : 
    2733 GIC      106023 :     return result;
    2734                 : }
    2735                 : 
    2736                 : /*
    2737 ECB             :  * Similar to above, except we search an ObjectAddressStack.
    2738                 :  */
    2739                 : static bool
    2740 GIC      151988 : stack_address_present_add_flags(const ObjectAddress *object,
    2741                 :                                 int flags,
    2742                 :                                 ObjectAddressStack *stack)
    2743                 : {
    2744 CBC      151988 :     bool        result = false;
    2745                 :     ObjectAddressStack *stackptr;
    2746                 : 
    2747 GIC      406994 :     for (stackptr = stack; stackptr; stackptr = stackptr->next)
    2748 ECB             :     {
    2749 GIC      255006 :         const ObjectAddress *thisobj = stackptr->object;
    2750                 : 
    2751 CBC      255006 :         if (object->classId == thisobj->classId &&
    2752 GIC      115387 :             object->objectId == thisobj->objectId)
    2753 ECB             :         {
    2754 GIC       45983 :             if (object->objectSubId == thisobj->objectSubId)
    2755 ECB             :             {
    2756 CBC       45693 :                 stackptr->flags |= flags;
    2757 GIC       45693 :                 result = true;
    2758 ECB             :             }
    2759 GIC         290 :             else if (thisobj->objectSubId == 0)
    2760 ECB             :             {
    2761                 :                 /*
    2762                 :                  * We're visiting a column with whole table already on stack.
    2763                 :                  * As in object_address_present_add_flags(), we can skip
    2764                 :                  * further processing of the subobject, but we don't want to
    2765                 :                  * propagate flags for the subobject to the whole object.
    2766                 :                  */
    2767 GIC         272 :                 result = true;
    2768                 :             }
    2769              18 :             else if (object->objectSubId == 0)
    2770                 :             {
    2771 ECB             :                 /*
    2772                 :                  * We're visiting a table with column already on stack.  As in
    2773                 :                  * object_address_present_add_flags(), we should propagate
    2774                 :                  * flags for the whole object to each of its subobjects.
    2775                 :                  */
    2776 UIC           0 :                 if (flags)
    2777               0 :                     stackptr->flags |= (flags | DEPFLAG_SUBOBJECT);
    2778                 :             }
    2779                 :         }
    2780 EUB             :     }
    2781                 : 
    2782 GIC      151988 :     return result;
    2783                 : }
    2784                 : 
    2785                 : /*
    2786 ECB             :  * Record multiple dependencies from an ObjectAddresses array, after first
    2787                 :  * removing any duplicates.
    2788                 :  */
    2789                 : void
    2790 GIC      391225 : record_object_address_dependencies(const ObjectAddress *depender,
    2791                 :                                    ObjectAddresses *referenced,
    2792                 :                                    DependencyType behavior)
    2793                 : {
    2794 CBC      391225 :     eliminate_duplicate_dependencies(referenced);
    2795 GIC      391225 :     recordMultipleDependencies(depender,
    2796          391225 :                                referenced->refs, referenced->numrefs,
    2797                 :                                behavior);
    2798 CBC      391225 : }
    2799 ECB             : 
    2800                 : /*
    2801                 :  * Sort the items in an ObjectAddresses array.
    2802                 :  *
    2803                 :  * The major sort key is OID-descending, so that newer objects will be listed
    2804                 :  * first in most cases.  This is primarily useful for ensuring stable outputs
    2805                 :  * from regression tests; it's not recommended if the order of the objects is
    2806                 :  * determined by user input, such as the order of targets in a DROP command.
    2807                 :  */
    2808                 : void
    2809 GIC          63 : sort_object_addresses(ObjectAddresses *addrs)
    2810                 : {
    2811              63 :     if (addrs->numrefs > 1)
    2812 GNC          36 :         qsort(addrs->refs, addrs->numrefs,
    2813 ECB             :               sizeof(ObjectAddress),
    2814                 :               object_address_comparator);
    2815 CBC          63 : }
    2816 ECB             : 
    2817                 : /*
    2818                 :  * Clean up when done with an ObjectAddresses array.
    2819                 :  */
    2820                 : void
    2821 GIC      496414 : free_object_addresses(ObjectAddresses *addrs)
    2822                 : {
    2823          496414 :     pfree(addrs->refs);
    2824          496414 :     if (addrs->extras)
    2825 CBC       13604 :         pfree(addrs->extras);
    2826 GIC      496414 :     pfree(addrs);
    2827 CBC      496414 : }
    2828 ECB             : 
    2829                 : /*
    2830                 :  * Determine the class of a given object identified by objectAddress.
    2831                 :  *
    2832                 :  * This function is essentially the reverse mapping for the object_classes[]
    2833                 :  * table.  We implement it as a function because the OIDs aren't consecutive.
    2834                 :  */
    2835                 : ObjectClass
    2836 GIC      159252 : getObjectClass(const ObjectAddress *object)
    2837                 : {
    2838                 :     /* only pg_class entries can have nonzero objectSubId */
    2839          159252 :     if (object->classId != RelationRelationId &&
    2840 CBC      106452 :         object->objectSubId != 0)
    2841 UIC           0 :         elog(ERROR, "invalid non-zero objectSubId for object class %u",
    2842                 :              object->classId);
    2843 ECB             : 
    2844 CBC      159252 :     switch (object->classId)
    2845 EUB             :     {
    2846 GIC       52800 :         case RelationRelationId:
    2847                 :             /* caller must check objectSubId */
    2848 CBC       52800 :             return OCLASS_CLASS;
    2849                 : 
    2850            4871 :         case ProcedureRelationId:
    2851 GIC        4871 :             return OCLASS_PROC;
    2852 ECB             : 
    2853 GIC       59078 :         case TypeRelationId:
    2854 CBC       59078 :             return OCLASS_TYPE;
    2855 ECB             : 
    2856 GIC         297 :         case CastRelationId:
    2857 CBC         297 :             return OCLASS_CAST;
    2858 ECB             : 
    2859 GIC         194 :         case CollationRelationId:
    2860 CBC         194 :             return OCLASS_COLLATION;
    2861 ECB             : 
    2862 GIC       18084 :         case ConstraintRelationId:
    2863 CBC       18084 :             return OCLASS_CONSTRAINT;
    2864 ECB             : 
    2865 GIC          98 :         case ConversionRelationId:
    2866 CBC          98 :             return OCLASS_CONVERSION;
    2867 ECB             : 
    2868 GIC        3123 :         case AttrDefaultRelationId:
    2869 CBC        3123 :             return OCLASS_DEFAULT;
    2870 ECB             : 
    2871 GIC          81 :         case LanguageRelationId:
    2872 CBC          81 :             return OCLASS_LANGUAGE;
    2873 ECB             : 
    2874 GIC          59 :         case LargeObjectRelationId:
    2875 CBC          59 :             return OCLASS_LARGEOBJECT;
    2876 ECB             : 
    2877 GIC         728 :         case OperatorRelationId:
    2878 CBC         728 :             return OCLASS_OPERATOR;
    2879 ECB             : 
    2880 GIC         192 :         case OperatorClassRelationId:
    2881 CBC         192 :             return OCLASS_OPCLASS;
    2882 ECB             : 
    2883 GIC         208 :         case OperatorFamilyRelationId:
    2884 CBC         208 :             return OCLASS_OPFAMILY;
    2885 ECB             : 
    2886 GIC          84 :         case AccessMethodRelationId:
    2887 CBC          84 :             return OCLASS_AM;
    2888 ECB             : 
    2889 GIC         917 :         case AccessMethodOperatorRelationId:
    2890 CBC         917 :             return OCLASS_AMOP;
    2891 ECB             : 
    2892 GIC         366 :         case AccessMethodProcedureRelationId:
    2893 CBC         366 :             return OCLASS_AMPROC;
    2894 ECB             : 
    2895 GIC        2644 :         case RewriteRelationId:
    2896 CBC        2644 :             return OCLASS_REWRITE;
    2897 ECB             : 
    2898 GIC       11202 :         case TriggerRelationId:
    2899 CBC       11202 :             return OCLASS_TRIGGER;
    2900 ECB             : 
    2901 GIC         479 :         case NamespaceRelationId:
    2902 CBC         479 :             return OCLASS_SCHEMA;
    2903 ECB             : 
    2904 GIC         433 :         case StatisticExtRelationId:
    2905 CBC         433 :             return OCLASS_STATISTIC_EXT;
    2906 ECB             : 
    2907 GIC          87 :         case TSParserRelationId:
    2908 CBC          87 :             return OCLASS_TSPARSER;
    2909 ECB             : 
    2910 GIC          96 :         case TSDictionaryRelationId:
    2911 CBC          96 :             return OCLASS_TSDICT;
    2912 ECB             : 
    2913 GIC          87 :         case TSTemplateRelationId:
    2914 CBC          87 :             return OCLASS_TSTEMPLATE;
    2915 ECB             : 
    2916 GIC         100 :         case TSConfigRelationId:
    2917 CBC         100 :             return OCLASS_TSCONFIG;
    2918 ECB             : 
    2919 GIC          59 :         case AuthIdRelationId:
    2920 CBC          59 :             return OCLASS_ROLE;
    2921 ECB             : 
    2922 GNC          42 :         case AuthMemRelationId:
    2923              42 :             return OCLASS_ROLE_MEMBERSHIP;
    2924                 : 
    2925 GIC          21 :         case DatabaseRelationId:
    2926 CBC          21 :             return OCLASS_DATABASE;
    2927 ECB             : 
    2928 GIC          15 :         case TableSpaceRelationId:
    2929 CBC          15 :             return OCLASS_TBLSPACE;
    2930 ECB             : 
    2931 GIC         149 :         case ForeignDataWrapperRelationId:
    2932 CBC         149 :             return OCLASS_FDW;
    2933 ECB             : 
    2934 GIC         211 :         case ForeignServerRelationId:
    2935 CBC         211 :             return OCLASS_FOREIGN_SERVER;
    2936 ECB             : 
    2937 GIC         211 :         case UserMappingRelationId:
    2938 CBC         211 :             return OCLASS_USER_MAPPING;
    2939 ECB             : 
    2940 GIC         186 :         case DefaultAclRelationId:
    2941 CBC         186 :             return OCLASS_DEFACL;
    2942 ECB             : 
    2943 GIC         101 :         case ExtensionRelationId:
    2944 CBC         101 :             return OCLASS_EXTENSION;
    2945 ECB             : 
    2946 GIC         156 :         case EventTriggerRelationId:
    2947 CBC         156 :             return OCLASS_EVENT_TRIGGER;
    2948 ECB             : 
    2949 GIC          80 :         case ParameterAclRelationId:
    2950 CBC          80 :             return OCLASS_PARAMETER_ACL;
    2951 ECB             : 
    2952 GIC         529 :         case PolicyRelationId:
    2953 CBC         529 :             return OCLASS_POLICY;
    2954 ECB             : 
    2955 GIC         224 :         case PublicationNamespaceRelationId:
    2956 CBC         224 :             return OCLASS_PUBLICATION_NAMESPACE;
    2957 ECB             : 
    2958 GIC         235 :         case PublicationRelationId:
    2959 CBC         235 :             return OCLASS_PUBLICATION;
    2960 ECB             : 
    2961 GIC         587 :         case PublicationRelRelationId:
    2962 CBC         587 :             return OCLASS_PUBLICATION_REL;
    2963 ECB             : 
    2964 GIC          57 :         case SubscriptionRelationId:
    2965 CBC          57 :             return OCLASS_SUBSCRIPTION;
    2966 ECB             : 
    2967 GIC          81 :         case TransformRelationId:
    2968 CBC          81 :             return OCLASS_TRANSFORM;
    2969 ECB             :     }
    2970                 : 
    2971                 :     /* shouldn't get here */
    2972 LBC           0 :     elog(ERROR, "unrecognized object class: %u", object->classId);
    2973                 :     return OCLASS_CLASS;        /* keep compiler quiet */
    2974 ECB             : }
    2975                 : 
    2976                 : /*
    2977                 :  * delete initial ACL for extension objects
    2978                 :  */
    2979 EUB             : static void
    2980 GIC       83389 : DeleteInitPrivs(const ObjectAddress *object)
    2981                 : {
    2982                 :     Relation    relation;
    2983                 :     ScanKeyData key[3];
    2984                 :     SysScanDesc scan;
    2985                 :     HeapTuple   oldtuple;
    2986                 : 
    2987 CBC       83389 :     relation = table_open(InitPrivsRelationId, RowExclusiveLock);
    2988                 : 
    2989 GIC       83389 :     ScanKeyInit(&key[0],
    2990                 :                 Anum_pg_init_privs_objoid,
    2991                 :                 BTEqualStrategyNumber, F_OIDEQ,
    2992           83389 :                 ObjectIdGetDatum(object->objectId));
    2993           83389 :     ScanKeyInit(&key[1],
    2994 ECB             :                 Anum_pg_init_privs_classoid,
    2995                 :                 BTEqualStrategyNumber, F_OIDEQ,
    2996 CBC       83389 :                 ObjectIdGetDatum(object->classId));
    2997 GIC       83389 :     ScanKeyInit(&key[2],
    2998                 :                 Anum_pg_init_privs_objsubid,
    2999 ECB             :                 BTEqualStrategyNumber, F_INT4EQ,
    3000 CBC       83389 :                 Int32GetDatum(object->objectSubId));
    3001                 : 
    3002 GIC       83389 :     scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
    3003 ECB             :                               NULL, 3, key);
    3004                 : 
    3005 GIC       83415 :     while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
    3006              26 :         CatalogTupleDelete(relation, &oldtuple->t_self);
    3007 ECB             : 
    3008 GIC       83389 :     systable_endscan(scan);
    3009 ECB             : 
    3010 GIC       83389 :     table_close(relation, RowExclusiveLock);
    3011           83389 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a