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 17:13:01 Functions: 100.0 % 28 28 28 27 1
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 100.0 % 3 3 3
Legend: Lines: hit not hit (180,240] days: 100.0 % 4 4 4
(240..) days: 90.9 % 928 844 23 48 13 22 518 1 303 49 517
Function coverage date bins:
(240..) days: 50.9 % 55 28 28 27

 Age         Owner                  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
 3665 alvherre                  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                 :      */
 3033 alvherre                  248 CBC       13606 :     if (trackDroppedObjectsNeeded() && !(flags & PERFORM_DELETION_INTERNAL))
                                249                 :     {
 3665 alvherre                  250 GIC        1507 :         for (i = 0; i < targetObjects->numrefs; i++)
                                251                 :         {
 3033                           252            1250 :             const ObjectAddress *thisobj = &targetObjects->refs[i];
                                253            1250 :             const ObjectAddressExtra *extra = &targetObjects->extras[i];
 2878 bruce                     254            1250 :             bool        original = false;
                                255            1250 :             bool        normal = false;
 3033 alvherre                  256 ECB             : 
 3033 alvherre                  257 GIC        1250 :             if (extra->flags & DEPFLAG_ORIGINAL)
 3033 alvherre                  258 CBC         296 :                 original = true;
 3033 alvherre                  259 GIC        1250 :             if (extra->flags & DEPFLAG_NORMAL)
 3033 alvherre                  260 CBC         129 :                 normal = true;
                                261            1250 :             if (extra->flags & DEPFLAG_REVERSE)
 3033 alvherre                  262 LBC           0 :                 normal = true;
 3033 alvherre                  263 ECB             : 
 3033 alvherre                  264 GIC        1250 :             if (EventTriggerSupportsObjectClass(getObjectClass(thisobj)))
 3665 alvherre                  265 ECB             :             {
 3033 alvherre                  266 CBC        1206 :                 EventTriggerSQLDropAddObject(thisobj, original, normal);
 3665 alvherre                  267 ECB             :             }
                                268                 :         }
                                269                 :     }
 3665 alvherre                  270 EUB             : 
                                271                 :     /*
 2319 tgl                       272 ECB             :      * Delete all the objects in the proper order, except that if told to, we
                                273                 :      * should skip the original object(s).
 3665 alvherre                  274                 :      */
 3665 alvherre                  275 GIC       97474 :     for (i = 0; i < targetObjects->numrefs; i++)
                                276                 :     {
                                277           83870 :         ObjectAddress *thisobj = targetObjects->refs + i;
 2319 tgl                       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;
 3665 alvherre                  283 ECB             : 
 3665 alvherre                  284 GIC       83391 :         deleteOneObject(thisobj, depRel, flags);
 3665 alvherre                  285 ECB             :     }
 3665 alvherre                  286 CBC       13604 : }
                                287                 : 
 7576 tgl                       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
 7576 tgl                       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
 7576 tgl                       336 ECB             :      * Relation pointer down to all the recursive deletion steps.
                                337                 :      */
 1539 andres                    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                 :      */
 4020 simon                     344            2191 :     AcquireDeletionLock(object, 0);
                                345                 : 
 6076 alvherre                  346 ECB             :     /*
                                347                 :      * Construct a list of objects to delete (ie, the given object plus
                                348                 :      * everything directly or indirectly dependent on it).
                                349                 :      */
 5418 tgl                       350 GIC        2191 :     targetObjects = new_object_addresses();
                                351                 : 
 5418 tgl                       352 CBC        2191 :     findDependentObjects(object,
                                353                 :                          DEPFLAG_ORIGINAL,
                                354                 :                          flags,
                                355                 :                          NULL,  /* empty stack */
                                356                 :                          targetObjects,
                                357                 :                          NULL,  /* no pendingObjects */
 3777 tgl                       358 ECB             :                          &depRel);
                                359                 : 
 6076 alvherre                  360                 :     /*
                                361                 :      * Check if deletion is allowed, and report about cascaded deletes.
                                362                 :      */
 5418 tgl                       363 GIC        2191 :     reportDependentObjects(targetObjects,
                                364                 :                            behavior,
                                365                 :                            flags,
                                366                 :                            object);
                                367                 : 
                                368                 :     /* do the deed */
 3665 alvherre                  369            2173 :     deleteObjectsInList(targetObjects, &depRel, flags);
                                370                 : 
 5418 tgl                       371 ECB             :     /* And clean up */
 5418 tgl                       372 GIC        2172 :     free_object_addresses(targetObjects);
                                373                 : 
 1539 andres                    374            2172 :     table_close(depRel, RowExclusiveLock);
 6076 alvherre                  375            2172 : }
                                376                 : 
 6076 alvherre                  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
 6076 alvherre                  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... */
 5412 tgl                       395 CBC       12583 :     if (objects->numrefs <= 0)
 5412 tgl                       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                 :      */
 1539 andres                    402           11602 :     depRel = table_open(DependRelationId, RowExclusiveLock);
 6076 alvherre                  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
 5050 bruce                     410                 :      * delete it when we reach its owner.
                                411                 :      */
 5418 tgl                       412 GIC       11602 :     targetObjects = new_object_addresses();
                                413                 : 
 6076 alvherre                  414           25509 :     for (i = 0; i < objects->numrefs; i++)
                                415                 :     {
 5418 tgl                       416           13928 :         const ObjectAddress *thisobj = objects->refs + i;
                                417                 : 
                                418                 :         /*
                                419                 :          * Acquire deletion lock on each target object.  (Ideally the caller
 5418 tgl                       420 ECB             :          * has done this already, but many places are sloppy about it.)
                                421                 :          */
 4020 simon                     422 CBC       13928 :         AcquireDeletionLock(thisobj, flags);
                                423                 : 
 5418 tgl                       424           13928 :         findDependentObjects(thisobj,
                                425                 :                              DEPFLAG_ORIGINAL,
                                426                 :                              flags,
                                427                 :                              NULL,  /* empty stack */
                                428                 :                              targetObjects,
                                429                 :                              objects,
 3777 tgl                       430 ECB             :                              &depRel);
                                431                 :     }
 6076 alvherre                  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                 :      */
 5418 tgl                       439 GIC       11581 :     reportDependentObjects(targetObjects,
                                440                 :                            behavior,
                                441                 :                            flags,
 5412                           442           11581 :                            (objects->numrefs == 1 ? objects->refs : NULL));
                                443                 : 
                                444                 :     /* do the deed */
 3665 alvherre                  445           11433 :     deleteObjectsInList(targetObjects, &depRel, flags);
                                446                 : 
 5418 tgl                       447 ECB             :     /* And clean up */
 5418 tgl                       448 GIC       11432 :     free_object_addresses(targetObjects);
                                449                 : 
 1539 andres                    450 CBC       11432 :     table_close(depRel, RowExclusiveLock);
                                451                 : }
                                452                 : 
 7504 tgl                       453 ECB             : /*
                                454                 :  * findDependentObjects - find all objects that depend on 'object'
                                455                 :  *
 5418                           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),
 3260 bruce                     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
 5418 tgl                       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                 : {
 7522 bruce                     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                 :      */
 2319 tgl                       524 GIC      105461 :     if (stack_address_present_add_flags(object, objflags, stack))
 4246                           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                 :      *
 5418 tgl                       532 ECB             :      * (Note: in these early-exit cases we could release the caller-taken
 5050 bruce                     533                 :      * lock, since the object is presumably now locked multiple times; but it
                                534                 :      * seems not worth the cycles.)
                                535                 :      */
 2319 tgl                       536 GIC      105328 :     if (object_address_present_add_flags(object, objflags, targetObjects))
 5418                           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                 :      */
  633                           543           86628 :     if (IsPinnedObject(object->classId, object->objectId))
  633 tgl                       544 CBC           1 :         ereport(ERROR,
  633 tgl                       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
 4246                           551                 :      * (its "owner"), and/or be a member of an extension (also considered its
 3260 bruce                     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                 :      */
 7088 tgl                       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));
 7576 tgl                       567 CBC       86627 :     if (object->objectSubId != 0)
                                568                 :     {
                                569                 :         /* Consider only dependencies of this sub-object */
 7088                           570            1034 :         ScanKeyInit(&key[2],
 7088 tgl                       571 ECB             :                     Anum_pg_depend_objsubid,
                                572                 :                     BTEqualStrategyNumber, F_INT4EQ,
 7088 tgl                       573 GIC        1034 :                     Int32GetDatum(object->objectSubId));
 7576 tgl                       574 CBC        1034 :         nkeys = 3;
 7576 tgl                       575 ECB             :     }
                                576                 :     else
                                577                 :     {
 1357                           578                 :         /* Consider dependencies of this object and any sub-objects it has */
 7576 tgl                       579 GIC       85593 :         nkeys = 2;
                                580                 :     }
 7576 tgl                       581 ECB             : 
 3777 tgl                       582 CBC       86627 :     scan = systable_beginscan(*depRel, DependDependerIndexId, true,
                                583                 :                               NULL, nkeys, key);
                                584                 : 
                                585                 :     /* initialize variables that loop may fill */
 1518 tgl                       586 GIC       86627 :     memset(&owningObject, 0, sizeof(owningObject));
 1518 tgl                       587 CBC       86627 :     memset(&partitionObject, 0, sizeof(partitionObject));
                                588                 : 
 7576 tgl                       589 GIC      206802 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
 7576 tgl                       590 ECB             :     {
 7522 bruce                     591 GIC      120870 :         Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
                                592                 : 
 7576 tgl                       593          120870 :         otherObject.classId = foundDep->refclassid;
 7576 tgl                       594 CBC      120870 :         otherObject.objectId = foundDep->refobjid;
                                595          120870 :         otherObject.objectSubId = foundDep->refobjsubid;
                                596                 : 
 1357 tgl                       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                 :          */
 1357 tgl                       604 GIC      120870 :         if (otherObject.classId == object->classId &&
                                605           41746 :             otherObject.objectId == object->objectId &&
                                606            1872 :             object->objectSubId == 0)
                                607            1860 :             continue;
                                608                 : 
 7576                           609          119010 :         switch (foundDep->deptype)
                                610                 :         {
                                611           68207 :             case DEPENDENCY_NORMAL:
 7576 tgl                       612 ECB             :             case DEPENDENCY_AUTO:
 2560 alvherre                  613                 :             case DEPENDENCY_AUTO_EXTENSION:
 7576 tgl                       614                 :                 /* no problem */
 7576 tgl                       615 CBC       68207 :                 break;
                                616                 : 
 4443                           617            1398 :             case DEPENDENCY_EXTENSION:
                                618                 : 
 2319 tgl                       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                 :                  */
 2319 tgl                       625 CBC        1398 :                 if (flags & PERFORM_DELETION_SKIP_EXTENSIONS)
 2319 tgl                       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
 2325 tgl                       633 ECB             :                  * cases such as dropping a transient object created within
                                634                 :                  * such a script.
                                635                 :                  */
 2325 tgl                       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                 : 
 2325 tgl                       644 ECB             :             case DEPENDENCY_INTERNAL:
                                645                 : 
 7576                           646                 :                 /*
 7522 bruce                     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                 :                  */
 5418 tgl                       663 GIC       46547 :                 if (stack == NULL)
                                664                 :                 {
 4947                           665              40 :                     if (pendingObjects &&
                                666              20 :                         object_address_present(&otherObject, pendingObjects))
                                667                 :                     {
 5418 tgl                       668 UIC           0 :                         systable_endscan(scan);
                                669                 :                         /* need to release caller's lock; see notes below */
                                670               0 :                         ReleaseDeletionLock(object);
 5418 tgl                       671 LBC           0 :                         return;
                                672                 :                     }
 1518 tgl                       673 ECB             : 
                                674                 :                     /*
                                675                 :                      * We postpone actually issuing the error message until
 1518 tgl                       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                 :                      */
 1518 tgl                       684 GIC          20 :                     if (!OidIsValid(owningObject.classId) ||
 1518 tgl                       685 UIC           0 :                         foundDep->deptype == DEPENDENCY_EXTENSION)
 1518 tgl                       686 GIC          20 :                         owningObject = otherObject;
                                687              20 :                     break;
                                688                 :                 }
                                689                 : 
                                690                 :                 /*
                                691                 :                  * 2. When recursing from the other end of this dependency,
 4246 tgl                       692 ECB             :                  * it's okay to continue with the deletion.  This holds when
 6385 bruce                     693 EUB             :                  * recursing from a whole object that includes the nominal
 4246 tgl                       694 ECB             :                  * other end as a component, too.  Since there can be more
 3955 bruce                     695                 :                  * than one "owning" object, we have to allow matches that are
                                696                 :                  * more than one level down in the stack.
                                697                 :                  */
 4246 tgl                       698 GIC       46527 :                 if (stack_address_present_add_flags(&otherObject, 0, stack))
 7572                           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                 :                  *
 5418 tgl                       706 ECB             :                  * First, release caller's lock on this object and get
 3260 bruce                     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                 :                  */
 5418 tgl                       711 GIC         695 :                 ReleaseDeletionLock(object);
 4020 simon                     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).
 5418 tgl                       719 ECB             :                  */
 5418 tgl                       720 CBC         695 :                 if (!systable_recheck_tuple(scan, tup))
                                721                 :                 {
 5418 tgl                       722 UIC           0 :                     systable_endscan(scan);
                                723               0 :                     ReleaseDeletionLock(&otherObject);
                                724               0 :                     return;
                                725                 :                 }
                                726                 : 
                                727                 :                 /*
 1518 tgl                       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
 1518 tgl                       730 EUB             :                  * resource consumption.
                                731                 :                  */
 1518 tgl                       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.
 4246 tgl                       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                 :                  */
 5418 tgl                       745 GIC         695 :                 findDependentObjects(&otherObject,
                                746                 :                                      DEPFLAG_REVERSE,
                                747                 :                                      flags,
                                748                 :                                      stack,
                                749                 :                                      targetObjects,
                                750                 :                                      pendingObjects,
                                751                 :                                      depRel);
                                752                 : 
 1518 tgl                       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                 :                  */
 1518 tgl                       769 GIC         695 :                 if (!object_address_present_add_flags(object, objflags,
                                770                 :                                                       targetObjects))
 1518 tgl                       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. */
 5418 tgl                       776 GIC         695 :                 return;
 1906 alvherre                  777 ECB             : 
 1518 tgl                       778 GIC        2072 :             case DEPENDENCY_PARTITION_PRI:
 1518 tgl                       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.
 1518 tgl                       784 ECB             :                  */
 1518 tgl                       785 GIC        2072 :                 objflags |= DEPFLAG_IS_PART;
 1518 tgl                       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                 :                  */
 1518 tgl                       792 GIC        2072 :                 partitionObject = otherObject;
 1518 tgl                       793 CBC        2072 :                 break;
                                794                 : 
 1518 tgl                       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).
 1518 tgl                       800 ECB             :                  */
 1518 tgl                       801 CBC        2072 :                 if (!(objflags & DEPFLAG_IS_PART))
 1518 tgl                       802 UIC           0 :                     partitionObject = otherObject;
 1518 tgl                       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                 :                  */
 1518 tgl                       809 CBC        2072 :                 objflags |= DEPFLAG_IS_PART;
 1518 tgl                       810 GBC        2072 :                 break;
                                811                 : 
 7576 tgl                       812 UIC           0 :             default:
 7202                           813               0 :                 elog(ERROR, "unrecognized dependency type '%c' for %s",
                                814                 :                      foundDep->deptype, getObjectDescription(object, false));
                                815                 :                 break;
                                816                 :         }
 7576 tgl                       817 ECB             :     }
                                818                 : 
 7576 tgl                       819 GIC       85932 :     systable_endscan(scan);
 7576 tgl                       820 EUB             : 
 1518                           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                 :      */
 1518 tgl                       827 CBC       85932 :     if (OidIsValid(owningObject.classId))
                                828                 :     {
                                829                 :         char       *otherObjDesc;
                                830                 : 
 1518 tgl                       831 GIC          20 :         if (OidIsValid(partitionObject.classId))
  998 michael                   832               6 :             otherObjDesc = getObjectDescription(&partitionObject, false);
                                833                 :         else
                                834              14 :             otherObjDesc = getObjectDescription(&owningObject, false);
 1518 tgl                       835 ECB             : 
 1518 tgl                       836 GIC          20 :         ereport(ERROR,
                                837                 :                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
                                838                 :                  errmsg("cannot drop %s because %s requires it",
  998 michael                   839 ECB             :                         getObjectDescription(object, false), otherObjDesc),
 1518 tgl                       840                 :                  errhint("You can drop %s instead.", otherObjDesc)));
                                841                 :     }
                                842                 : 
                                843                 :     /*
 1539                           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                 :      */
 1539 tgl                       851 GIC       85912 :     maxDependentObjects = 128;  /* arbitrary initial allocation */
                                852                 :     dependentObjects = (ObjectAddressAndFlags *)
                                853           85912 :         palloc(maxDependentObjects * sizeof(ObjectAddressAndFlags));
                                854           85912 :     numDependentObjects = 0;
                                855                 : 
 7088                           856           85912 :     ScanKeyInit(&key[0],
                                857                 :                 Anum_pg_depend_refclassid,
                                858                 :                 BTEqualStrategyNumber, F_OIDEQ,
 7088 tgl                       859 CBC       85912 :                 ObjectIdGetDatum(object->classId));
 7088 tgl                       860 GIC       85912 :     ScanKeyInit(&key[1],
 7088 tgl                       861 ECB             :                 Anum_pg_depend_refobjid,
                                862                 :                 BTEqualStrategyNumber, F_OIDEQ,
 7088 tgl                       863 GIC       85912 :                 ObjectIdGetDatum(object->objectId));
 7576 tgl                       864 CBC       85912 :     if (object->objectSubId != 0)
                                865                 :     {
 7088 tgl                       866 GIC        1022 :         ScanKeyInit(&key[2],
 7088 tgl                       867 ECB             :                     Anum_pg_depend_refobjsubid,
                                868                 :                     BTEqualStrategyNumber, F_INT4EQ,
 7088 tgl                       869 GIC        1022 :                     Int32GetDatum(object->objectSubId));
 7576                           870            1022 :         nkeys = 3;
 7576 tgl                       871 ECB             :     }
                                872                 :     else
 7576 tgl                       873 GIC       84890 :         nkeys = 2;
 7576 tgl                       874 ECB             : 
 3777 tgl                       875 GIC       85912 :     scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
                                876                 :                               NULL, nkeys, key);
 7576 tgl                       877 ECB             : 
 7576 tgl                       878 CBC      176419 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                                879                 :     {
 7522 bruce                     880 GIC       90507 :         Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
 5050 bruce                     881 ECB             :         int         subflags;
                                882                 : 
 7576 tgl                       883 CBC       90507 :         otherObject.classId = foundDep->classid;
 7576 tgl                       884 GIC       90507 :         otherObject.objectId = foundDep->objid;
                                885           90507 :         otherObject.objectSubId = foundDep->objsubid;
 7576 tgl                       886 ECB             : 
                                887                 :         /*
 1357                           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                 :          */
 1357 tgl                       892 CBC       90507 :         if (otherObject.classId == object->classId &&
                                893           39853 :             otherObject.objectId == object->objectId &&
 1357 tgl                       894 GIC        1860 :             object->objectSubId == 0)
                                895            1860 :             continue;
                                896                 : 
                                897                 :         /*
                                898                 :          * Must lock the dependent object before recursing to it.
                                899                 :          */
 4020 simon                     900 CBC       88647 :         AcquireDeletionLock(&otherObject, 0);
 5418 tgl                       901 ECB             : 
                                902                 :         /*
 5050 bruce                     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.)
 5418 tgl                       908                 :          */
 5418 tgl                       909 GIC       88647 :         if (!systable_recheck_tuple(scan, tup))
                                910                 :         {
                                911                 :             /* release the now-useless lock */
 5418 tgl                       912 UIC           0 :             ReleaseDeletionLock(&otherObject);
                                913                 :             /* and continue scanning for dependencies */
                                914               0 :             continue;
                                915                 :         }
                                916                 : 
 1539 tgl                       917 ECB             :         /*
                                918                 :          * We do need to delete it, so identify objflags to be passed down,
                                919                 :          * which depend on the dependency type.
 1539 tgl                       920 EUB             :          */
 7576 tgl                       921 GIC       88647 :         switch (foundDep->deptype)
 7576 tgl                       922 EUB             :         {
 7576 tgl                       923 GIC       12127 :             case DEPENDENCY_NORMAL:
 5418                           924           12127 :                 subflags = DEPFLAG_NORMAL;
 7576                           925           12127 :                 break;
                                926           26923 :             case DEPENDENCY_AUTO:
                                927                 :             case DEPENDENCY_AUTO_EXTENSION:
 5418                           928           26923 :                 subflags = DEPFLAG_AUTO;
 5418 tgl                       929 CBC       26923 :                 break;
 7576 tgl                       930 GIC       44550 :             case DEPENDENCY_INTERNAL:
 5418 tgl                       931 CBC       44550 :                 subflags = DEPFLAG_INTERNAL;
 7576                           932           44550 :                 break;
 1518                           933            3765 :             case DEPENDENCY_PARTITION_PRI:
 1518 tgl                       934 ECB             :             case DEPENDENCY_PARTITION_SEC:
 1518 tgl                       935 GIC        3765 :                 subflags = DEPFLAG_PARTITION;
 1518 tgl                       936 CBC        3765 :                 break;
 4443                           937            1282 :             case DEPENDENCY_EXTENSION:
                                938            1282 :                 subflags = DEPFLAG_EXTENSION;
                                939            1282 :                 break;
 7576 tgl                       940 LBC           0 :             default:
 7202                           941               0 :                 elog(ERROR, "unrecognized dependency type '%c' for %s",
                                942                 :                      foundDep->deptype, getObjectDescription(object, false));
 5418 tgl                       943 ECB             :                 subflags = 0;   /* keep compiler quiet */
 7576                           944                 :                 break;
                                945                 :         }
 5418                           946                 : 
 1539                           947                 :         /* And add it to the pending-objects list */
 1539 tgl                       948 GBC       88647 :         if (numDependentObjects >= maxDependentObjects)
 1539 tgl                       949 EUB             :         {
                                950                 :             /* enlarge array if needed */
 1539 tgl                       951 GIC           4 :             maxDependentObjects *= 2;
                                952                 :             dependentObjects = (ObjectAddressAndFlags *)
                                953               4 :                 repalloc(dependentObjects,
                                954                 :                          maxDependentObjects * sizeof(ObjectAddressAndFlags));
                                955                 :         }
 1539 tgl                       956 ECB             : 
 1539 tgl                       957 GIC       88647 :         dependentObjects[numDependentObjects].obj = otherObject;
                                958           88647 :         dependentObjects[numDependentObjects].subflags = subflags;
 1539 tgl                       959 CBC       88647 :         numDependentObjects++;
                                960                 :     }
 1539 tgl                       961 ECB             : 
 1539 tgl                       962 GIC       85912 :     systable_endscan(scan);
                                963                 : 
                                964                 :     /*
 1539 tgl                       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                 :      */
 1539 tgl                       969 GIC       85912 :     if (numDependentObjects > 1)
   61 peter                     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.
 1539 tgl                       977 ECB             :      */
 1539 tgl                       978 CBC       85912 :     mystack.object = object;    /* set up a new stack level */
 1539 tgl                       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                 : 
 1539 tgl                       986 CBC       88647 :         findDependentObjects(&depObj->obj,
 1539 tgl                       987 ECB             :                              depObj->subflags,
 2319                           988                 :                              flags,
                                989                 :                              &mystack,
 5418                           990                 :                              targetObjects,
                                991                 :                              pendingObjects,
                                992                 :                              depRel);
                                993                 :     }
 7576                           994                 : 
 1539 tgl                       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
 1518 tgl                      1003 ECB             :      * need one of those objects.)
                               1004                 :      */
 5418 tgl                      1005 GIC       85912 :     extra.flags = mystack.flags;
 1518                          1006           85912 :     if (extra.flags & DEPFLAG_IS_PART)
                               1007            2066 :         extra.dependee = partitionObject;
                               1008           83846 :     else if (stack)
 5418                          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);
 7576 tgl                      1013 ECB             : }
                               1014                 : 
 5418                          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
 5418 tgl                      1029 GIC       13772 : reportDependentObjects(const ObjectAddresses *targetObjects,
                               1030                 :                        DropBehavior behavior,
                               1031                 :                        int flags,
                               1032                 :                        const ObjectAddress *origObject)
                               1033                 : {
 2319                          1034           13772 :     int         msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
 5418                          1035           13772 :     bool        ok = true;
                               1036                 :     StringInfoData clientdetail;
 5415 tgl                      1037 ECB             :     StringInfoData logdetail;
 5415 tgl                      1038 GIC       13772 :     int         numReportedClient = 0;
                               1039           13772 :     int         numNotReportedClient = 0;
                               1040                 :     int         i;
                               1041                 : 
 1518 tgl                      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                 :      */
 1518 tgl                      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];
  998 michael                  1060 CBC          15 :             char       *otherObjDesc = getObjectDescription(&extra->dependee,
                               1061                 :                                                             false);
 1518 tgl                      1062 ECB             : 
 1518 tgl                      1063 GIC          15 :             ereport(ERROR,
 1518 tgl                      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                 : 
 5415                          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                 :      */
 5415 tgl                      1076 GIC       13757 :     if (behavior == DROP_CASCADE &&
  867                          1077            1599 :         !message_level_is_interesting(msglevel))
 5415                          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.
 5415 tgl                      1084 ECB             :      */
                               1085                 : #define MAX_REPORTED_DEPS 100
                               1086                 : 
 5415 tgl                      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                 :      */
 5418                          1094           93411 :     for (i = targetObjects->numrefs - 1; i >= 0; i--)
 5418 tgl                      1095 ECB             :     {
 5418 tgl                      1096 CBC       80165 :         const ObjectAddress *obj = &targetObjects->refs[i];
 5418 tgl                      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)
 5418 tgl                      1102 CBC       15566 :             continue;
                               1103                 : 
 1542 tgl                      1104 ECB             :         /* Also ignore sub-objects; we'll report the whole object elsewhere */
 1542 tgl                      1105 CBC       64599 :         if (extra->flags & DEPFLAG_SUBOBJECT)
 1542 tgl                      1106 UIC           0 :             continue;
                               1107                 : 
  998 michael                  1108 GIC       64599 :         objDesc = getObjectDescription(obj, false);
 5415 tgl                      1109 ECB             : 
  520 alvherre                 1110                 :         /* An object being dropped concurrently doesn't need to be reported */
  520 alvherre                 1111 GIC       64599 :         if (objDesc == NULL)
  520 alvherre                 1112 UIC           0 :             continue;
  520 alvherre                 1113 ECB             : 
 5418 tgl                      1114 EUB             :         /*
                               1115                 :          * If, at any stage of the recursive search, we reached the object via
 1518 tgl                      1116 ECB             :          * an AUTO, INTERNAL, PARTITION, or EXTENSION dependency, then it's
                               1117                 :          * okay to delete it even in RESTRICT mode.
                               1118                 :          */
 4443 tgl                      1119 CBC       64599 :         if (extra->flags & (DEPFLAG_AUTO |
 4443 tgl                      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
 5050 bruce                    1127 ECB             :              * results are too confusing when client_min_messages and
                               1128                 :              * log_min_messages are different.
                               1129                 :              */
 5418 tgl                      1130 GIC       62294 :             ereport(DEBUG2,
                               1131                 :                     (errmsg_internal("drop auto-cascades to %s",
                               1132                 :                                      objDesc)));
                               1133                 :         }
                               1134            2305 :         else if (behavior == DROP_RESTRICT)
                               1135                 :         {
  998 michael                  1136             266 :             char       *otherDesc = getObjectDescription(&extra->dependee,
                               1137                 :                                                          false);
 5415 tgl                      1138 ECB             : 
  520 alvherre                 1139 GIC         266 :             if (otherDesc)
                               1140                 :             {
                               1141             266 :                 if (numReportedClient < MAX_REPORTED_DEPS)
  520 alvherre                 1142 ECB             :                 {
                               1143                 :                     /* separate entries with a newline */
  520 alvherre                 1144 CBC         266 :                     if (clientdetail.len != 0)
  520 alvherre                 1145 GIC         115 :                         appendStringInfoChar(&clientdetail, '\n');
                               1146             266 :                     appendStringInfo(&clientdetail, _("%s depends on %s"),
  520 alvherre                 1147 ECB             :                                      objDesc, otherDesc);
  520 alvherre                 1148 GIC         266 :                     numReportedClient++;
  520 alvherre                 1149 ECB             :                 }
                               1150                 :                 else
  520 alvherre                 1151 UIC           0 :                     numNotReportedClient++;
 5415 tgl                      1152 ECB             :                 /* separate entries with a newline */
  520 alvherre                 1153 CBC         266 :                 if (logdetail.len != 0)
                               1154             115 :                     appendStringInfoChar(&logdetail, '\n');
  520 alvherre                 1155 GIC         266 :                 appendStringInfo(&logdetail, _("%s depends on %s"),
 5415 tgl                      1156 ECB             :                                  objDesc, otherDesc);
  520 alvherre                 1157 GIC         266 :                 pfree(otherDesc);
                               1158                 :             }
 5415 tgl                      1159 EUB             :             else
 5415 tgl                      1160 UIC           0 :                 numNotReportedClient++;
 5418 tgl                      1161 CBC         266 :             ok = false;
 5418 tgl                      1162 ECB             :         }
                               1163                 :         else
                               1164                 :         {
 5415 tgl                      1165 CBC        2039 :             if (numReportedClient < MAX_REPORTED_DEPS)
                               1166                 :             {
                               1167                 :                 /* separate entries with a newline */
 5415 tgl                      1168 GBC        2039 :                 if (clientdetail.len != 0)
 5415 tgl                      1169 CBC        1415 :                     appendStringInfoChar(&clientdetail, '\n');
 5415 tgl                      1170 GIC        2039 :                 appendStringInfo(&clientdetail, _("drop cascades to %s"),
                               1171                 :                                  objDesc);
                               1172            2039 :                 numReportedClient++;
 5415 tgl                      1173 ECB             :             }
                               1174                 :             else
 5415 tgl                      1175 UIC           0 :                 numNotReportedClient++;
 5415 tgl                      1176 ECB             :             /* separate entries with a newline */
 5415 tgl                      1177 CBC        2039 :             if (logdetail.len != 0)
                               1178            1415 :                 appendStringInfoChar(&logdetail, '\n');
 5415 tgl                      1179 GIC        2039 :             appendStringInfo(&logdetail, _("drop cascades to %s"),
 5415 tgl                      1180 ECB             :                              objDesc);
                               1181                 :         }
                               1182                 : 
 5415 tgl                      1183 GBC       64599 :         pfree(objDesc);
                               1184                 :     }
 5418 tgl                      1185 ECB             : 
 5415 tgl                      1186 CBC       13246 :     if (numNotReportedClient > 0)
 5127 peter_e                  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)",
 5127 peter_e                  1191 ECB             :                                                  numNotReportedClient),
                               1192                 :                          numNotReportedClient);
                               1193                 : 
 5418 tgl                      1194 CBC       13246 :     if (!ok)
 5418 tgl                      1195 EUB             :     {
 5418 tgl                      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),
 5415 tgl                      1202 ECB             :                      errdetail_log("%s", logdetail.data),
                               1203                 :                      errhint("Use DROP ... CASCADE to drop the dependent objects too.")));
 5418                          1204                 :         else
 5418 tgl                      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                 :     }
 5415 tgl                      1212 GIC       13095 :     else if (numReportedClient > 1)
 5415 tgl                      1213 ECB             :     {
 5415 tgl                      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),
 5415 tgl                      1220 ECB             :                  errdetail_log("%s", logdetail.data)));
                               1221                 :     }
 5415 tgl                      1222 CBC       12791 :     else if (numReportedClient == 1)
                               1223                 :     {
                               1224                 :         /* we just use the single item as-is */
 5415 tgl                      1225 GIC         320 :         ereport(msglevel,
                               1226                 :                 (errmsg_internal("%s", clientdetail.data)));
                               1227                 :     }
                               1228                 : 
                               1229           13095 :     pfree(clientdetail.data);
 5415 tgl                      1230 CBC       13095 :     pfree(logdetail.data);
                               1231                 : }
                               1232                 : 
 1034 peter                    1233 ECB             : /*
                               1234                 :  * Drop an object by OID.  Works for most catalogs, if no special processing
                               1235                 :  * is needed.
                               1236                 :  */
                               1237                 : static void
 1034 peter                    1238 CBC        1540 : DropObjectById(const ObjectAddress *object)
                               1239                 : {
                               1240                 :     int         cacheId;
                               1241                 :     Relation    rel;
                               1242                 :     HeapTuple   tup;
                               1243                 : 
 1034 peter                    1244 GIC        1540 :     cacheId = get_object_catcache_oid(object->classId);
                               1245                 : 
 1034 peter                    1246 CBC        1540 :     rel = table_open(object->classId, RowExclusiveLock);
                               1247                 : 
                               1248                 :     /*
                               1249                 :      * Use the system cache for the oid column, if one exists.
                               1250                 :      */
 1034 peter                    1251 GIC        1540 :     if (cacheId >= 0)
 1034 peter                    1252 ECB             :     {
 1034 peter                    1253 GIC         833 :         tup = SearchSysCache1(cacheId, ObjectIdGetDatum(object->objectId));
 1034 peter                    1254 CBC         833 :         if (!HeapTupleIsValid(tup))
 1034 peter                    1255 UIC           0 :             elog(ERROR, "cache lookup failed for %s %u",
                               1256                 :                  get_object_class_descr(object->classId), object->objectId);
                               1257                 : 
 1034 peter                    1258 GIC         833 :         CatalogTupleDelete(rel, &tup->t_self);
 1034 peter                    1259 ECB             : 
 1034 peter                    1260 GIC         833 :         ReleaseSysCache(tup);
 1034 peter                    1261 ECB             :     }
                               1262                 :     else
 1034 peter                    1263 EUB             :     {
                               1264                 :         ScanKeyData skey[1];
                               1265                 :         SysScanDesc scan;
 1034 peter                    1266 ECB             : 
 1034 peter                    1267 GIC         707 :         ScanKeyInit(&skey[0],
 1034 peter                    1268 CBC         707 :                     get_object_attnum_oid(object->classId),
                               1269                 :                     BTEqualStrategyNumber, F_OIDEQ,
 1034 peter                    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                 : 
 1034 peter                    1275 ECB             :         /* we expect exactly one match */
 1034 peter                    1276 CBC         707 :         tup = systable_getnext(scan);
 1034 peter                    1277 GIC         707 :         if (!HeapTupleIsValid(tup))
 1034 peter                    1278 LBC           0 :             elog(ERROR, "could not find tuple for %s %u",
                               1279                 :                  get_object_class_descr(object->classId), object->objectId);
 1034 peter                    1280 ECB             : 
 1034 peter                    1281 GIC         707 :         CatalogTupleDelete(rel, &tup->t_self);
                               1282                 : 
                               1283             707 :         systable_endscan(scan);
 1034 peter                    1284 ECB             :     }
                               1285                 : 
 1034 peter                    1286 GBC        1540 :     table_close(rel, RowExclusiveLock);
 1034 peter                    1287 GIC        1540 : }
                               1288                 : 
 5418 tgl                      1289 ECB             : /*
                               1290                 :  * deleteOneObject: delete a single object for performDeletion.
                               1291                 :  *
                               1292                 :  * *depRel is the already-open pg_depend relation.
                               1293                 :  */
                               1294                 : static void
 3777 tgl                      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 */
 3686 rhaas                    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                 :      */
 3824 simon                    1311           83391 :     if (flags & PERFORM_DELETION_CONCURRENTLY)
 1539 andres                   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
 3784 tgl                      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                 :      */
 3824 simon                    1323 GIC       83391 :     doDeletion(object, flags);
                               1324                 : 
                               1325                 :     /*
                               1326                 :      * Reopen depRel if we closed it above
                               1327                 :      */
                               1328           83389 :     if (flags & PERFORM_DELETION_CONCURRENTLY)
 1539 andres                   1329              52 :         *depRel = table_open(DependRelationId, RowExclusiveLock);
                               1330                 : 
 3824 simon                    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
 5418 tgl                      1336                 :      * for its sub-objects too.
                               1337                 :      */
 5418 tgl                      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));
 5418 tgl                      1346 CBC       83389 :     if (object->objectSubId != 0)
                               1347                 :     {
 5418 tgl                      1348 GIC         977 :         ScanKeyInit(&key[2],
 5418 tgl                      1349 ECB             :                     Anum_pg_depend_objsubid,
                               1350                 :                     BTEqualStrategyNumber, F_INT4EQ,
 5418 tgl                      1351 GIC         977 :                     Int32GetDatum(object->objectSubId));
                               1352             977 :         nkeys = 3;
 5418 tgl                      1353 ECB             :     }
                               1354                 :     else
 5418 tgl                      1355 GIC       82412 :         nkeys = 2;
 5418 tgl                      1356 ECB             : 
 3777 tgl                      1357 GIC       83389 :     scan = systable_beginscan(*depRel, DependDependerIndexId, true,
                               1358                 :                               NULL, nkeys, key);
 5418 tgl                      1359 ECB             : 
 5418 tgl                      1360 CBC      199094 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                               1361                 :     {
 2258 tgl                      1362 GIC      115705 :         CatalogTupleDelete(*depRel, &tup->t_self);
 5418 tgl                      1363 ECB             :     }
                               1364                 : 
 5418 tgl                      1365 CBC       83389 :     systable_endscan(scan);
                               1366                 : 
                               1367                 :     /*
 3260 bruce                    1368 ECB             :      * Delete shared dependency references related to this object.  Again, if
                               1369                 :      * subId = 0, remove records for sub-objects too.
 5190 tgl                      1370                 :      */
 5190 tgl                      1371 GIC       83389 :     deleteSharedDependencyRecordsFor(object->classId, object->objectId,
                               1372           83389 :                                      object->objectSubId);
 5190 tgl                      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.)
 5418                          1379                 :      */
 5418 tgl                      1380 CBC       83389 :     DeleteComments(object->objectId, object->classId, object->objectSubId);
 4577 rhaas                    1381 GIC       83389 :     DeleteSecurityLabel(object);
 2559 sfrost                   1382           83389 :     DeleteInitPrivs(object);
                               1383                 : 
                               1384                 :     /*
                               1385                 :      * CommandCounterIncrement here to ensure that preceding changes are all
                               1386                 :      * visible to the next deletion step.
                               1387                 :      */
 5418 tgl                      1388 CBC       83389 :     CommandCounterIncrement();
 5418 tgl                      1389 ECB             : 
                               1390                 :     /*
                               1391                 :      * And we're done!
                               1392                 :      */
 5418 tgl                      1393 GIC       83389 : }
                               1394                 : 
                               1395                 : /*
 7576 tgl                      1396 ECB             :  * doDeletion: actually delete a single object
                               1397                 :  */
                               1398                 : static void
 4020 simon                    1399 GIC       83391 : doDeletion(const ObjectAddress *object, int flags)
                               1400                 : {
 7576 tgl                      1401 CBC       83391 :     switch (getObjectClass(object))
                               1402                 :     {
 7576 tgl                      1403 GIC       30418 :         case OCLASS_CLASS:
                               1404                 :             {
 7507                          1405           30418 :                 char        relKind = get_rel_relkind(object->objectId);
                               1406                 : 
 1906 alvherre                 1407 CBC       30418 :                 if (relKind == RELKIND_INDEX ||
                               1408                 :                     relKind == RELKIND_PARTITIONED_INDEX)
 7522 bruce                    1409           10275 :                 {
 2319 tgl                      1410 GIC       10275 :                     bool        concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
 1472 peter                    1411 CBC       10275 :                     bool        concurrent_lock_mode = ((flags & PERFORM_DELETION_CONCURRENT_LOCK) != 0);
                               1412                 : 
 7522 bruce                    1413           10275 :                     Assert(object->objectSubId == 0);
 1472 peter                    1414 GIC       10275 :                     index_drop(object->objectId, concurrent, concurrent_lock_mode);
 7522 bruce                    1415 ECB             :                 }
                               1416                 :                 else
                               1417                 :                 {
 7522 bruce                    1418 CBC       20143 :                     if (object->objectSubId != 0)
                               1419             977 :                         RemoveAttributeById(object->objectId,
 7522 bruce                    1420 GIC         977 :                                             object->objectSubId);
 7522 bruce                    1421 ECB             :                     else
 7522 bruce                    1422 CBC       19166 :                         heap_drop_with_catalog(object->objectId);
                               1423                 :                 }
                               1424                 : 
                               1425                 :                 /*
 2153 bruce                    1426 ECB             :                  * for a sequence, in addition to dropping the heap, also
                               1427                 :                  * delete pg_sequence tuple
                               1428                 :                  */
 2301 peter_e                  1429 GIC       30418 :                 if (relKind == RELKIND_SEQUENCE)
 2301 peter_e                  1430 CBC         422 :                     DeleteSequenceTuple(object->objectId);
 7522 bruce                    1431 GIC       30418 :                 break;
                               1432                 :             }
                               1433                 : 
 7576 tgl                      1434            3066 :         case OCLASS_PROC:
                               1435            3066 :             RemoveFunctionById(object->objectId);
                               1436            3066 :             break;
 7576 tgl                      1437 ECB             : 
 7576 tgl                      1438 CBC       29757 :         case OCLASS_TYPE:
                               1439           29757 :             RemoveTypeById(object->objectId);
 7576 tgl                      1440 GIC       29757 :             break;
                               1441                 : 
 7576 tgl                      1442 CBC        8799 :         case OCLASS_CONSTRAINT:
                               1443            8799 :             RemoveConstraintById(object->objectId);
                               1444            8798 :             break;
                               1445                 : 
 7573                          1446            1296 :         case OCLASS_DEFAULT:
                               1447            1296 :             RemoveAttrDefaultById(object->objectId);
                               1448            1296 :             break;
                               1449                 : 
 4867 itagaki.takahiro         1450              44 :         case OCLASS_LARGEOBJECT:
                               1451              44 :             LargeObjectDrop(object->objectId);
                               1452              44 :             break;
                               1453                 : 
 7576 tgl                      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                 : 
 2156                          1466             227 :         case OCLASS_STATISTIC_EXT:
                               1467             227 :             RemoveStatisticsById(object->objectId);
                               1468             227 :             break;
                               1469                 : 
 5710                          1470              21 :         case OCLASS_TSCONFIG:
                               1471              21 :             RemoveTSConfigurationById(object->objectId);
                               1472              21 :             break;
                               1473                 : 
 4443                          1474              48 :         case OCLASS_EXTENSION:
                               1475              48 :             RemoveExtensionById(object->objectId);
                               1476              48 :             break;
                               1477                 : 
 3055 sfrost                   1478             257 :         case OCLASS_POLICY:
 3124                          1479             257 :             RemovePolicyById(object->objectId);
                               1480             257 :             break;
                               1481                 : 
  529 akapila                  1482              96 :         case OCLASS_PUBLICATION_NAMESPACE:
                               1483              96 :             RemovePublicationSchemaById(object->objectId);
                               1484              96 :             break;
                               1485                 : 
 2271 peter_e                  1486             374 :         case OCLASS_PUBLICATION_REL:
                               1487             374 :             RemovePublicationRelById(object->objectId);
                               1488             374 :             break;
                               1489                 : 
  578 akapila                  1490             178 :         case OCLASS_PUBLICATION:
                               1491             178 :             RemovePublicationById(object->objectId);
                               1492             178 :             break;
                               1493                 : 
 1034 peter                    1494            1540 :         case OCLASS_CAST:
 1034 peter                    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:
 1034 peter                    1514 GIC        1540 :             DropObjectById(object);
 2905 peter_e                  1515            1540 :             break;
                               1516                 : 
                               1517                 :             /*
                               1518                 :              * These global object types are not supported here.
                               1519                 :              */
 2156 tgl                      1520 UIC           0 :         case OCLASS_ROLE:
                               1521                 :         case OCLASS_DATABASE:
                               1522                 :         case OCLASS_TBLSPACE:
 2156 tgl                      1523 ECB             :         case OCLASS_SUBSCRIPTION:
  368                          1524                 :         case OCLASS_PARAMETER_ACL:
 2156 tgl                      1525 UIC           0 :             elog(ERROR, "global objects cannot be deleted by doDeletion");
                               1526                 :             break;
                               1527                 : 
                               1528                 :             /*
 2156 tgl                      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                 :     }
 7576 tgl                      1533 GIC       83389 : }
 7576 tgl                      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
 4020 simon                    1545 GIC      105657 : AcquireDeletionLock(const ObjectAddress *object, int flags)
                               1546                 : {
 5418 tgl                      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
 3784 tgl                      1553 ECB             :          * lock.
                               1554                 :          */
 3824 simon                    1555 CBC       38597 :         if (flags & PERFORM_DELETION_CONCURRENTLY)
 4020 simon                    1556 GIC          52 :             LockRelationOid(object->objectId, ShareUpdateExclusiveLock);
                               1557                 :         else
                               1558           38545 :             LockRelationOid(object->objectId, AccessExclusiveLock);
                               1559                 :     }
  234 rhaas                    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 */
 5418 tgl                      1566 CBC       67054 :         LockDatabaseObject(object->classId, object->objectId, 0,
 5418 tgl                      1567 ECB             :                            AccessExclusiveLock);
                               1568                 :     }
 5418 tgl                      1569 CBC      105657 : }
                               1570                 : 
 5418 tgl                      1571 ECB             : /*
                               1572                 :  * ReleaseDeletionLock - release an object deletion lock
                               1573                 :  *
                               1574                 :  * Companion to AcquireDeletionLock.
                               1575                 :  */
                               1576                 : void
 5418 tgl                      1577 CBC         695 : ReleaseDeletionLock(const ObjectAddress *object)
                               1578                 : {
 5418 tgl                      1579 GIC         695 :     if (object->classId == RelationRelationId)
 5418 tgl                      1580 CBC          22 :         UnlockRelationOid(object->objectId, AccessExclusiveLock);
                               1581                 :     else
                               1582                 :         /* assume we should lock the whole object not a sub-object */
 5418 tgl                      1583 GIC         673 :         UnlockDatabaseObject(object->classId, object->objectId, 0,
                               1584                 :                              AccessExclusiveLock);
                               1585             695 : }
                               1586                 : 
                               1587                 : /*
 7572 tgl                      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
 7572 tgl                      1602 GIC       74160 : recordDependencyOnExpr(const ObjectAddress *depender,
                               1603                 :                        Node *expr, List *rtable,
                               1604                 :                        DependencyType behavior)
                               1605                 : {
                               1606                 :     find_expr_references_context context;
                               1607                 : 
 6076 alvherre                 1608           74160 :     context.addrs = new_object_addresses();
                               1609                 : 
                               1610                 :     /* Set up interpretation for Vars at varlevelsup = 0 */
 6892 neilc                    1611           74160 :     context.rtables = list_make1(rtable);
                               1612                 : 
 7572 tgl                      1613 ECB             :     /* Scan the expression tree for referenceable objects */
 7572 tgl                      1614 GIC       74160 :     find_expr_references_walker(expr, &context);
                               1615                 : 
                               1616                 :     /* Remove any duplicates */
 6076 alvherre                 1617           74160 :     eliminate_duplicate_dependencies(context.addrs);
                               1618                 : 
 7572 tgl                      1619 ECB             :     /* And record 'em */
 7256 tgl                      1620 GIC       74160 :     recordMultipleDependencies(depender,
  702 tmunro                   1621           74160 :                                context.addrs->refs, context.addrs->numrefs,
  702 tmunro                   1622 ECB             :                                behavior);
                               1623                 : 
 6076 alvherre                 1624 GIC       74160 :     free_object_addresses(context.addrs);
 7256 tgl                      1625 CBC       74160 : }
                               1626                 : 
                               1627                 : /*
 7256 tgl                      1628 ECB             :  * recordDependencyOnSingleRelExpr - find expression dependencies
                               1629                 :  *
                               1630                 :  * As above, but only one relation is expected to be referenced (with
 3260 bruce                    1631                 :  * varno = 1 and varlevelsup = 0).  Pass the relation OID instead of a
 7256 tgl                      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'
 1357                          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
 7256 tgl                      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;
  267 peter                    1652 GNC        4396 :     RangeTblEntry rte = {0};
                               1653                 : 
 6076 alvherre                 1654 GIC        4396 :     context.addrs = new_object_addresses();
                               1655                 : 
 7256 tgl                      1656 ECB             :     /* We gin up a rather bogus rangetable list to handle Vars */
 7256 tgl                      1657 GIC        4396 :     rte.type = T_RangeTblEntry;
                               1658            4396 :     rte.rtekind = RTE_RELATION;
                               1659            4396 :     rte.relid = relId;
 2118                          1660            4396 :     rte.relkind = RELKIND_RELATION; /* no need for exactness here */
 1652                          1661            4396 :     rte.rellockmode = AccessShareLock;
 7256 tgl                      1662 ECB             : 
 6892 neilc                    1663 GIC        4396 :     context.rtables = list_make1(list_make1(&rte));
 7256 tgl                      1664 ECB             : 
                               1665                 :     /* Scan the expression tree for referenceable objects */
 7256 tgl                      1666 GIC        4396 :     find_expr_references_walker(expr, &context);
 7256 tgl                      1667 ECB             : 
                               1668                 :     /* Remove any duplicates */
 6076 alvherre                 1669 CBC        4396 :     eliminate_duplicate_dependencies(context.addrs);
 7256 tgl                      1670 ECB             : 
                               1671                 :     /* Separate self-dependencies if necessary */
 1357 tgl                      1672 GIC        4396 :     if ((behavior != self_behavior || reverse_self) &&
 1357 tgl                      1673 CBC         750 :         context.addrs->numrefs > 0)
                               1674                 :     {
                               1675                 :         ObjectAddresses *self_addrs;
 7256 tgl                      1676 ECB             :         ObjectAddress *outobj;
                               1677                 :         int         oldref,
                               1678                 :                     outrefs;
                               1679                 : 
 6076 alvherre                 1680 GIC         747 :         self_addrs = new_object_addresses();
                               1681                 : 
 6076 alvherre                 1682 CBC         747 :         outobj = context.addrs->refs;
 7256 tgl                      1683             747 :         outrefs = 0;
 6076 alvherre                 1684 GIC        3132 :         for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
                               1685                 :         {
                               1686            2385 :             ObjectAddress *thisobj = context.addrs->refs + oldref;
                               1687                 : 
 6569 tgl                      1688            2385 :             if (thisobj->classId == RelationRelationId &&
 7256                          1689             947 :                 thisobj->objectId == relId)
 7256 tgl                      1690 ECB             :             {
                               1691                 :                 /* Move this ref into self_addrs */
 5418 tgl                      1692 CBC         947 :                 add_exact_object_address(thisobj, self_addrs);
 7256 tgl                      1693 ECB             :             }
                               1694                 :             else
                               1695                 :             {
                               1696                 :                 /* Keep it in context.addrs */
 5418 tgl                      1697 GIC        1438 :                 *outobj = *thisobj;
 7256 tgl                      1698 CBC        1438 :                 outobj++;
                               1699            1438 :                 outrefs++;
                               1700                 :             }
                               1701                 :         }
 6076 alvherre                 1702             747 :         context.addrs->numrefs = outrefs;
                               1703                 : 
                               1704                 :         /* Record the self-dependencies with the appropriate direction */
 1357 tgl                      1705 GIC         747 :         if (!reverse_self)
 2314 rhaas                    1706             649 :             recordMultipleDependencies(depender,
  702 tmunro                   1707 CBC         649 :                                        self_addrs->refs, self_addrs->numrefs,
  702 tmunro                   1708 ECB             :                                        self_behavior);
 1357 tgl                      1709                 :         else
                               1710                 :         {
                               1711                 :             /* Can't use recordMultipleDependencies, so do it the hard way */
                               1712                 :             int         selfref;
                               1713                 : 
 1357 tgl                      1714 GIC         237 :             for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
 1357 tgl                      1715 ECB             :             {
 1357 tgl                      1716 CBC         139 :                 ObjectAddress *thisobj = self_addrs->refs + selfref;
 1357 tgl                      1717 ECB             : 
 1357 tgl                      1718 GIC         139 :                 recordDependencyOn(thisobj, depender, self_behavior);
                               1719                 :             }
                               1720                 :         }
                               1721                 : 
 6076 alvherre                 1722             747 :         free_object_addresses(self_addrs);
                               1723                 :     }
 7256 tgl                      1724 ECB             : 
                               1725                 :     /* Record the external dependencies */
 7572 tgl                      1726 CBC        4396 :     recordMultipleDependencies(depender,
  702 tmunro                   1727 GIC        4396 :                                context.addrs->refs, context.addrs->numrefs,
  702 tmunro                   1728 ECB             :                                behavior);
                               1729                 : 
 6076 alvherre                 1730 GIC        4396 :     free_object_addresses(context.addrs);
 7572 tgl                      1731            4396 : }
 7572 tgl                      1732 ECB             : 
                               1733                 : /*
                               1734                 :  * Recursively search an expression tree for object references.
                               1735                 :  *
 6233                          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
 7572 tgl                      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;
 7572 tgl                      1757 ECB             : 
                               1758                 :         /* Find matching rtable entry, or complain if not found */
 6892 neilc                    1759 GIC      920954 :         if (var->varlevelsup >= list_length(context->rtables))
 7202 tgl                      1760 LBC           0 :             elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
 6892 neilc                    1761 CBC      920954 :         rtable = (List *) list_nth(context->rtables, var->varlevelsup);
                               1762          920954 :         if (var->varno <= 0 || var->varno > list_length(rtable))
 7202 tgl                      1763 UIC           0 :             elog(ERROR, "invalid varno %d", var->varno);
 7572 tgl                      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
 4541 tgl                      1769 ECB             :          * arising from each underlying rangetable entry.  While we could
 4541 tgl                      1770 EUB             :          * record such a dependency when finding a whole-row Var that
 4541 tgl                      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
 4541 tgl                      1773 EUB             :          * responsibility with the range table.  Note that this poses some
 4541 tgl                      1774 ECB             :          * risks for identifying dependencies of stand-alone expressions:
                               1775                 :          * whole-table references may need to be created separately.)
                               1776                 :          */
 6807 tgl                      1777 GIC      920954 :         if (var->varattno == InvalidAttrNumber)
                               1778           17047 :             return false;
 7572                          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                 :         }
  261                          1785          235644 :         else if (rte->rtekind == RTE_FUNCTION)
                               1786                 :         {
  261 tgl                      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) */
  261 tgl                      1789 CBC      116384 :             process_function_rte_ref(rte, var->varattno, context);
                               1790                 :         }
                               1791                 : 
 1186 tgl                      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                 :          */
 7572 tgl                      1800 GIC      903907 :         return false;
                               1801                 :     }
 5363                          1802         2848736 :     else if (IsA(node, Const))
                               1803                 :     {
 6398                          1804          459647 :         Const      *con = (Const *) node;
                               1805                 :         Oid         objoid;
                               1806                 : 
                               1807                 :         /* A constant must depend on the constant's datatype */
 6233                          1808          459647 :         add_object_address(OCLASS_TYPE, con->consttype, 0,
                               1809                 :                            context->addrs);
 4412 tgl                      1810 ECB             : 
                               1811                 :         /*
  702 tmunro                   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                 :          */
  702 tmunro                   1817 GIC      459647 :         if (OidIsValid(con->constcollid) &&
  702 tmunro                   1818 CBC      195983 :             con->constcollid != DEFAULT_COLLATION_OID)
  702 tmunro                   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.)
 6398 tgl                      1827 ECB             :          */
 6398 tgl                      1828 CBC      459647 :         if (!con->constisnull)
 6398 tgl                      1829 ECB             :         {
 6398 tgl                      1830 GIC      383434 :             switch (con->consttype)
                               1831                 :             {
 6398 tgl                      1832 UIC           0 :                 case REGPROCOID:
                               1833                 :                 case REGPROCEDUREOID:
                               1834               0 :                     objoid = DatumGetObjectId(con->constvalue);
 4802 rhaas                    1835               0 :                     if (SearchSysCacheExists1(PROCOID,
                               1836                 :                                               ObjectIdGetDatum(objoid)))
 6398 tgl                      1837               0 :                         add_object_address(OCLASS_PROC, objoid, 0,
 6076 alvherre                 1838 ECB             :                                            context->addrs);
 6398 tgl                      1839 UIC           0 :                     break;
 6398 tgl                      1840 LBC           0 :                 case REGOPEROID:
                               1841                 :                 case REGOPERATOROID:
 6398 tgl                      1842 UBC           0 :                     objoid = DatumGetObjectId(con->constvalue);
 4802 rhaas                    1843 UIC           0 :                     if (SearchSysCacheExists1(OPEROID,
 4802 rhaas                    1844 EUB             :                                               ObjectIdGetDatum(objoid)))
 6398 tgl                      1845 UBC           0 :                         add_object_address(OCLASS_OPERATOR, objoid, 0,
                               1846                 :                                            context->addrs);
                               1847               0 :                     break;
 6398 tgl                      1848 GIC       11058 :                 case REGCLASSOID:
 6398 tgl                      1849 GBC       11058 :                     objoid = DatumGetObjectId(con->constvalue);
 4802 rhaas                    1850           11058 :                     if (SearchSysCacheExists1(RELOID,
                               1851                 :                                               ObjectIdGetDatum(objoid)))
 6398 tgl                      1852           11058 :                         add_object_address(OCLASS_CLASS, objoid, 0,
 6076 alvherre                 1853 EUB             :                                            context->addrs);
 6398 tgl                      1854 GIC       11058 :                     break;
 6398 tgl                      1855 UBC           0 :                 case REGTYPEOID:
 6398 tgl                      1856 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
 4802 rhaas                    1857 UBC           0 :                     if (SearchSysCacheExists1(TYPEOID,
 4802 rhaas                    1858 ECB             :                                               ObjectIdGetDatum(objoid)))
 6398 tgl                      1859 LBC           0 :                         add_object_address(OCLASS_TYPE, objoid, 0,
 6076 alvherre                 1860 ECB             :                                            context->addrs);
 6398 tgl                      1861 UIC           0 :                     break;
  266 tgl                      1862 LBC           0 :                 case REGCOLLATIONOID:
  266 tgl                      1863 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
  266 tgl                      1864 LBC           0 :                     if (SearchSysCacheExists1(COLLOID,
  266 tgl                      1865 EUB             :                                               ObjectIdGetDatum(objoid)))
  266 tgl                      1866 UBC           0 :                         add_object_address(OCLASS_COLLATION, objoid, 0,
  266 tgl                      1867 EUB             :                                            context->addrs);
  266 tgl                      1868 UIC           0 :                     break;
 5710 tgl                      1869 UBC           0 :                 case REGCONFIGOID:
 5710 tgl                      1870 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
 4802 rhaas                    1871 UBC           0 :                     if (SearchSysCacheExists1(TSCONFIGOID,
 4802 rhaas                    1872 EUB             :                                               ObjectIdGetDatum(objoid)))
 5710 tgl                      1873 UBC           0 :                         add_object_address(OCLASS_TSCONFIG, objoid, 0,
 5710 tgl                      1874 EUB             :                                            context->addrs);
 5710 tgl                      1875 UIC           0 :                     break;
 5710 tgl                      1876 UBC           0 :                 case REGDICTIONARYOID:
 5710 tgl                      1877 UIC           0 :                     objoid = DatumGetObjectId(con->constvalue);
 4802 rhaas                    1878 UBC           0 :                     if (SearchSysCacheExists1(TSDICTOID,
 4802 rhaas                    1879 EUB             :                                               ObjectIdGetDatum(objoid)))
 5710 tgl                      1880 UBC           0 :                         add_object_address(OCLASS_TSDICT, objoid, 0,
 5710 tgl                      1881 EUB             :                                            context->addrs);
 5710 tgl                      1882 UIC           0 :                     break;
 2892 andrew                   1883 EUB             : 
 2892 andrew                   1884 GIC         606 :                 case REGNAMESPACEOID:
 2892 andrew                   1885 GBC         606 :                     objoid = DatumGetObjectId(con->constvalue);
                               1886             606 :                     if (SearchSysCacheExists1(NAMESPACEOID,
 2892 andrew                   1887 EUB             :                                               ObjectIdGetDatum(objoid)))
 2892 andrew                   1888 GBC         606 :                         add_object_address(OCLASS_SCHEMA, objoid, 0,
                               1889                 :                                            context->addrs);
                               1890             606 :                     break;
                               1891                 : 
 2878 bruce                    1892 EUB             :                     /*
                               1893                 :                      * Dependencies for regrole should be shared among all
 2878 bruce                    1894 ECB             :                      * databases, so explicitly inhibit to have dependencies.
                               1895                 :                      */
 2892 andrew                   1896 LBC           0 :                 case REGROLEOID:
 2892 andrew                   1897 UIC           0 :                     ereport(ERROR,
 2892 andrew                   1898 ECB             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1899                 :                              errmsg("constant of the type %s cannot be used here",
 2118 tgl                      1900                 :                                     "regrole")));
                               1901                 :                     break;
                               1902                 :             }
                               1903                 :         }
 6398 tgl                      1904 GIC      459647 :         return false;
                               1905                 :     }
 5363 tgl                      1906 GBC     2389089 :     else if (IsA(node, Param))
 6233 tgl                      1907 EUB             :     {
 6233 tgl                      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 */
  702 tmunro                   1914 CBC       49493 :         if (OidIsValid(param->paramcollid) &&
  702 tmunro                   1915 GIC        8203 :             param->paramcollid != DEFAULT_COLLATION_OID)
  702 tmunro                   1916 CBC        4848 :             add_object_address(OCLASS_COLLATION, param->paramcollid, 0,
                               1917                 :                                context->addrs);
 6233 tgl                      1918 ECB             :     }
 5363 tgl                      1919 GIC     2339596 :     else if (IsA(node, FuncExpr))
                               1920                 :     {
 7423 tgl                      1921 CBC      239349 :         FuncExpr   *funcexpr = (FuncExpr *) node;
                               1922                 : 
 7423 tgl                      1923 GIC      239349 :         add_object_address(OCLASS_PROC, funcexpr->funcid, 0,
 6076 alvherre                 1924 ECB             :                            context->addrs);
 7423 tgl                      1925                 :         /* fall through to examine arguments */
                               1926                 :     }
 5363 tgl                      1927 GIC     2100247 :     else if (IsA(node, OpExpr))
                               1928                 :     {
 7188 bruce                    1929 CBC      258155 :         OpExpr     *opexpr = (OpExpr *) node;
                               1930                 : 
 7423 tgl                      1931          258155 :         add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
                               1932                 :                            context->addrs);
 7423 tgl                      1933 ECB             :         /* fall through to examine arguments */
                               1934                 :     }
 5363 tgl                      1935 GIC     1842092 :     else if (IsA(node, DistinctExpr))
                               1936                 :     {
 7188 bruce                    1937 CBC           6 :         DistinctExpr *distinctexpr = (DistinctExpr *) node;
                               1938                 : 
 7423 tgl                      1939               6 :         add_object_address(OCLASS_OPERATOR, distinctexpr->opno, 0,
                               1940                 :                            context->addrs);
 7572 tgl                      1941 ECB             :         /* fall through to examine arguments */
                               1942                 :     }
 4404 tgl                      1943 GIC     1842086 :     else if (IsA(node, NullIfExpr))
                               1944                 :     {
 4404 tgl                      1945 CBC         317 :         NullIfExpr *nullifexpr = (NullIfExpr *) node;
                               1946                 : 
                               1947             317 :         add_object_address(OCLASS_OPERATOR, nullifexpr->opno, 0,
                               1948                 :                            context->addrs);
 7224 tgl                      1949 ECB             :         /* fall through to examine arguments */
                               1950                 :     }
 4404 tgl                      1951 GIC     1841769 :     else if (IsA(node, ScalarArrayOpExpr))
                               1952                 :     {
 4404 tgl                      1953 CBC       21245 :         ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
                               1954                 : 
                               1955           21245 :         add_object_address(OCLASS_OPERATOR, opexpr->opno, 0,
                               1956                 :                            context->addrs);
 7357 tgl                      1957 ECB             :         /* fall through to examine arguments */
                               1958                 :     }
 5363 tgl                      1959 GIC     1820524 :     else if (IsA(node, Aggref))
                               1960                 :     {
 7572 tgl                      1961 CBC        4714 :         Aggref     *aggref = (Aggref *) node;
                               1962                 : 
                               1963            4714 :         add_object_address(OCLASS_PROC, aggref->aggfnoid, 0,
                               1964                 :                            context->addrs);
 7572 tgl                      1965 ECB             :         /* fall through to examine arguments */
                               1966                 :     }
 5215 tgl                      1967 GIC     1815810 :     else if (IsA(node, WindowFunc))
                               1968                 :     {
 5215 tgl                      1969 CBC         333 :         WindowFunc *wfunc = (WindowFunc *) node;
                               1970                 : 
                               1971             333 :         add_object_address(OCLASS_PROC, wfunc->winfnoid, 0,
                               1972                 :                            context->addrs);
 5215 tgl                      1973 ECB             :         /* fall through to examine arguments */
                               1974                 :     }
  851 tgl                      1975 GIC     1815477 :     else if (IsA(node, SubscriptingRef))
                               1976                 :     {
  851 tgl                      1977 CBC        5531 :         SubscriptingRef *sbsref = (SubscriptingRef *) node;
                               1978                 : 
  851 tgl                      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                 :          */
  851 tgl                      1985 CBC        5531 :         if (sbsref->refrestype != sbsref->refcontainertype &&
  851 tgl                      1986 GIC        5498 :             sbsref->refrestype != sbsref->refelemtype)
  851 tgl                      1987 LBC           0 :             add_object_address(OCLASS_TYPE, sbsref->refrestype, 0,
                               1988                 :                                context->addrs);
                               1989                 :         /* fall through to examine arguments */
                               1990                 :     }
 5343 tgl                      1991 GIC     1809946 :     else if (IsA(node, SubPlan))
                               1992                 :     {
                               1993                 :         /* Extra work needed here if we ever need this case */
 6311 tgl                      1994 UIC           0 :         elog(ERROR, "already-planned subqueries not supported");
 6311 tgl                      1995 ECB             :     }
 1994 tgl                      1996 CBC     1809946 :     else if (IsA(node, FieldSelect))
 1994 tgl                      1997 EUB             :     {
 1994 tgl                      1998 GIC       43122 :         FieldSelect *fselect = (FieldSelect *) node;
 1990                          1999           43122 :         Oid         argtype = getBaseType(exprType((Node *) fselect->arg));
                               2000           43122 :         Oid         reltype = get_typ_typrelid(argtype);
 1994 tgl                      2001 ECB             : 
                               2002                 :         /*
                               2003                 :          * We need a dependency on the specific column named in FieldSelect,
 1990 tgl                      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
 1990 tgl                      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                 :          */
 1990 tgl                      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 */
  702 tmunro                   2018           43122 :         if (OidIsValid(fselect->resultcollid) &&
                               2019            5156 :             fselect->resultcollid != DEFAULT_COLLATION_OID)
  702 tmunro                   2020 UIC           0 :             add_object_address(OCLASS_COLLATION, fselect->resultcollid, 0,
  702 tmunro                   2021 ECB             :                                context->addrs);
 1994 tgl                      2022                 :     }
 1994 tgl                      2023 GIC     1766824 :     else if (IsA(node, FieldStore))
                               2024                 :     {
 1994 tgl                      2025 CBC          30 :         FieldStore *fstore = (FieldStore *) node;
 1990 tgl                      2026 GIC          30 :         Oid         reltype = get_typ_typrelid(fstore->resulttype);
                               2027                 : 
 1990 tgl                      2028 ECB             :         /* similar considerations to FieldSelect, but multiple column(s) */
 1990 tgl                      2029 CBC          30 :         if (OidIsValid(reltype))
 1990 tgl                      2030 EUB             :         {
                               2031                 :             ListCell   *l;
                               2032                 : 
 1990 tgl                      2033 CBC          60 :             foreach(l, fstore->fieldnums)
 1990 tgl                      2034 GIC          30 :                 add_object_address(OCLASS_CLASS, reltype, lfirst_int(l),
 1990 tgl                      2035 ECB             :                                    context->addrs);
                               2036                 :         }
                               2037                 :         else
 1990 tgl                      2038 UIC           0 :             add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
 1990 tgl                      2039 ECB             :                                context->addrs);
                               2040                 :     }
 5363 tgl                      2041 GIC     1766794 :     else if (IsA(node, RelabelType))
                               2042                 :     {
 6031 bruce                    2043 CBC       42807 :         RelabelType *relab = (RelabelType *) node;
 6233 tgl                      2044 ECB             : 
                               2045                 :         /* since there is no function dependency, need to depend on type */
 6233 tgl                      2046 GIC       42807 :         add_object_address(OCLASS_TYPE, relab->resulttype, 0,
                               2047                 :                            context->addrs);
  702 tmunro                   2048 EUB             :         /* the collation might not be referenced anywhere else, either */
  702 tmunro                   2049 GIC       42807 :         if (OidIsValid(relab->resultcollid) &&
                               2050            9442 :             relab->resultcollid != DEFAULT_COLLATION_OID)
  702 tmunro                   2051 CBC        8787 :             add_object_address(OCLASS_COLLATION, relab->resultcollid, 0,
                               2052                 :                                context->addrs);
 6233 tgl                      2053 ECB             :     }
 5363 tgl                      2054 GIC     1723987 :     else if (IsA(node, CoerceViaIO))
                               2055                 :     {
 5787 tgl                      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,
 5787 tgl                      2060 ECB             :                            context->addrs);
  702 tmunro                   2061                 :         /* the collation might not be referenced anywhere else, either */
  702 tmunro                   2062 GIC        7100 :         if (OidIsValid(iocoerce->resultcollid) &&
                               2063            7032 :             iocoerce->resultcollid != DEFAULT_COLLATION_OID)
  702 tmunro                   2064 CBC        2121 :             add_object_address(OCLASS_COLLATION, iocoerce->resultcollid, 0,
                               2065                 :                                context->addrs);
 5787 tgl                      2066 ECB             :     }
 5363 tgl                      2067 GIC     1716887 :     else if (IsA(node, ArrayCoerceExpr))
                               2068                 :     {
 5857 tgl                      2069 CBC        1518 :         ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
                               2070                 : 
                               2071                 :         /* as above, depend on type */
                               2072            1518 :         add_object_address(OCLASS_TYPE, acoerce->resulttype, 0,
 5857 tgl                      2073 ECB             :                            context->addrs);
  702 tmunro                   2074                 :         /* the collation might not be referenced anywhere else, either */
  702 tmunro                   2075 GIC        1518 :         if (OidIsValid(acoerce->resultcollid) &&
                               2076             609 :             acoerce->resultcollid != DEFAULT_COLLATION_OID)
  702 tmunro                   2077 CBC         303 :             add_object_address(OCLASS_COLLATION, acoerce->resultcollid, 0,
                               2078                 :                                context->addrs);
 5857 tgl                      2079 ECB             :         /* fall through to examine arguments */
                               2080                 :     }
 5363 tgl                      2081 GIC     1715369 :     else if (IsA(node, ConvertRowtypeExpr))
 6233 tgl                      2082 ECB             :     {
 6233 tgl                      2083 UIC           0 :         ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
                               2084                 : 
 6233 tgl                      2085 ECB             :         /* since there is no function dependency, need to depend on type */
 6233 tgl                      2086 LBC           0 :         add_object_address(OCLASS_TYPE, cvt->resulttype, 0,
 6076 alvherre                 2087 ECB             :                            context->addrs);
                               2088                 :     }
 4412 tgl                      2089 GIC     1715369 :     else if (IsA(node, CollateExpr))
                               2090                 :     {
 4412 tgl                      2091 CBC          51 :         CollateExpr *coll = (CollateExpr *) node;
                               2092                 : 
 4412 tgl                      2093 GBC          51 :         add_object_address(OCLASS_COLLATION, coll->collOid, 0,
                               2094                 :                            context->addrs);
                               2095                 :     }
 5363                          2096         1715318 :     else if (IsA(node, RowExpr))
                               2097                 :     {
 6031 bruce                    2098 GIC          27 :         RowExpr    *rowexpr = (RowExpr *) node;
 6233 tgl                      2099 ECB             : 
 6233 tgl                      2100 GIC          27 :         add_object_address(OCLASS_TYPE, rowexpr->row_typeid, 0,
 6076 alvherre                 2101 ECB             :                            context->addrs);
                               2102                 :     }
 5363 tgl                      2103 CBC     1715291 :     else if (IsA(node, RowCompareExpr))
                               2104                 :     {
 6311 tgl                      2105 GIC           9 :         RowCompareExpr *rcexpr = (RowCompareExpr *) node;
 6311 tgl                      2106 ECB             :         ListCell   *l;
                               2107                 : 
 6311 tgl                      2108 CBC          27 :         foreach(l, rcexpr->opnos)
                               2109                 :         {
                               2110              18 :             add_object_address(OCLASS_OPERATOR, lfirst_oid(l), 0,
                               2111                 :                                context->addrs);
                               2112                 :         }
 5951                          2113              27 :         foreach(l, rcexpr->opfamilies)
                               2114                 :         {
                               2115              18 :             add_object_address(OCLASS_OPFAMILY, lfirst_oid(l), 0,
                               2116                 :                                context->addrs);
                               2117                 :         }
 7394 tgl                      2118 ECB             :         /* fall through to examine arguments */
                               2119                 :     }
 5363 tgl                      2120 CBC     1715282 :     else if (IsA(node, CoerceToDomain))
                               2121                 :     {
 6233 tgl                      2122 GIC      194864 :         CoerceToDomain *cd = (CoerceToDomain *) node;
 6233 tgl                      2123 ECB             : 
 6233 tgl                      2124 GIC      194864 :         add_object_address(OCLASS_TYPE, cd->resulttype, 0,
 6076 alvherre                 2125 ECB             :                            context->addrs);
                               2126                 :     }
 2095 tgl                      2127 GIC     1520418 :     else if (IsA(node, NextValueExpr))
                               2128                 :     {
 2095 tgl                      2129 UIC           0 :         NextValueExpr *nve = (NextValueExpr *) node;
 2095 tgl                      2130 ECB             : 
 2095 tgl                      2131 UIC           0 :         add_object_address(OCLASS_CLASS, nve->seqid, 0,
 2095 tgl                      2132 ECB             :                            context->addrs);
                               2133                 :     }
 2524 tgl                      2134 CBC     1520418 :     else if (IsA(node, OnConflictExpr))
                               2135                 :     {
 2524 tgl                      2136 GIC           9 :         OnConflictExpr *onconflict = (OnConflictExpr *) node;
 2524 tgl                      2137 ECB             : 
 2524 tgl                      2138 GIC           9 :         if (OidIsValid(onconflict->constraint))
 2524 tgl                      2139 UBC           0 :             add_object_address(OCLASS_CONSTRAINT, onconflict->constraint, 0,
                               2140                 :                                context->addrs);
 2524 tgl                      2141 EUB             :         /* fall through to examine arguments */
                               2142                 :     }
 5363 tgl                      2143 GIC     1520409 :     else if (IsA(node, SortGroupClause))
 5363 tgl                      2144 ECB             :     {
 5363 tgl                      2145 GIC       36645 :         SortGroupClause *sgc = (SortGroupClause *) node;
 5363 tgl                      2146 ECB             : 
 5363 tgl                      2147 GIC       36645 :         add_object_address(OCLASS_OPERATOR, sgc->eqop, 0,
 5363 tgl                      2148 ECB             :                            context->addrs);
 5363 tgl                      2149 GBC       36645 :         if (OidIsValid(sgc->sortop))
 5363 tgl                      2150 GIC       36645 :             add_object_address(OCLASS_OPERATOR, sgc->sortop, 0,
                               2151                 :                                context->addrs);
                               2152           36645 :         return false;
 5363 tgl                      2153 ECB             :     }
 1887 tgl                      2154 GIC     1483764 :     else if (IsA(node, WindowClause))
 1887 tgl                      2155 ECB             :     {
 1887 tgl                      2156 GIC         333 :         WindowClause *wc = (WindowClause *) node;
 1887 tgl                      2157 ECB             : 
 1887 tgl                      2158 GIC         333 :         if (OidIsValid(wc->startInRangeFunc))
 1887 tgl                      2159 CBC           3 :             add_object_address(OCLASS_PROC, wc->startInRangeFunc, 0,
 1887 tgl                      2160 ECB             :                                context->addrs);
 1887 tgl                      2161 GIC         333 :         if (OidIsValid(wc->endInRangeFunc))
 1887 tgl                      2162 CBC           3 :             add_object_address(OCLASS_PROC, wc->endInRangeFunc, 0,
                               2163                 :                                context->addrs);
  702 tmunro                   2164             333 :         if (OidIsValid(wc->inRangeColl) &&
  702 tmunro                   2165 UIC           0 :             wc->inRangeColl != DEFAULT_COLLATION_OID)
 1887 tgl                      2166 LBC           0 :             add_object_address(OCLASS_COLLATION, wc->inRangeColl, 0,
                               2167                 :                                context->addrs);
 1887 tgl                      2168 ECB             :         /* fall through to examine substructure */
                               2169                 :     }
  797 peter                    2170 GIC     1483431 :     else if (IsA(node, CTECycleClause))
  797 peter                    2171 ECB             :     {
  797 peter                    2172 CBC           6 :         CTECycleClause *cc = (CTECycleClause *) node;
                               2173                 : 
                               2174               6 :         if (OidIsValid(cc->cycle_mark_type))
  797 peter                    2175 GBC           6 :             add_object_address(OCLASS_TYPE, cc->cycle_mark_type, 0,
  797 peter                    2176 EUB             :                                context->addrs);
  797 peter                    2177 GIC           6 :         if (OidIsValid(cc->cycle_mark_collation))
                               2178               3 :             add_object_address(OCLASS_COLLATION, cc->cycle_mark_collation, 0,
                               2179                 :                                context->addrs);
  797 peter                    2180 CBC           6 :         if (OidIsValid(cc->cycle_mark_neop))
  797 peter                    2181 GIC           6 :             add_object_address(OCLASS_OPERATOR, cc->cycle_mark_neop, 0,
  797 peter                    2182 ECB             :                                context->addrs);
                               2183                 :         /* fall through to examine substructure */
                               2184                 :     }
 5363 tgl                      2185 CBC     1483425 :     else if (IsA(node, Query))
                               2186                 :     {
 7572 tgl                      2187 ECB             :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
 7572 tgl                      2188 CBC      100773 :         Query      *query = (Query *) node;
                               2189                 :         ListCell   *lc;
 7572 tgl                      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
 1186                          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                 :          */
 4628 tgl                      2208 GIC      328664 :         foreach(lc, query->rtable)
                               2209                 :         {
                               2210          227891 :             RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
                               2211                 : 
 6233                          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;
 1186 tgl                      2218 CBC       44104 :                 case RTE_JOIN:
                               2219                 : 
 1186 tgl                      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                 :                      */
 1186 tgl                      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;
 6233 tgl                      2243 CBC       42169 :                 default:
                               2244           42169 :                     break;
                               2245                 :             }
 7572 tgl                      2246 ECB             :         }
                               2247                 : 
 4046                          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                 :          */
 4046 tgl                      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))
 4046 tgl                      2262 UIC           0 :                 elog(ERROR, "invalid resultRelation %d",
                               2263                 :                      query->resultRelation);
 4046 tgl                      2264 GIC         310 :             rte = rt_fetch(query->resultRelation, query->rtable);
 4046 tgl                      2265 CBC         310 :             if (rte->rtekind == RTE_RELATION)
 4046 tgl                      2266 ECB             :             {
 4046 tgl                      2267 GIC         960 :                 foreach(lc, query->targetList)
                               2268                 :                 {
                               2269             650 :                     TargetEntry *tle = (TargetEntry *) lfirst(lc);
 4046 tgl                      2270 ECB             : 
 4046 tgl                      2271 CBC         650 :                     if (tle->resjunk)
 2118 tgl                      2272 GBC           3 :                         continue;   /* ignore junk tlist items */
 4046 tgl                      2273 GIC         647 :                     add_object_address(OCLASS_CLASS, rte->relid, tle->resno,
 4046 tgl                      2274 ECB             :                                        context->addrs);
                               2275                 :                 }
                               2276                 :             }
                               2277                 :         }
                               2278                 : 
 4628                          2279                 :         /*
                               2280                 :          * Add dependencies on constraints listed in query's constraintDeps
                               2281                 :          */
 4628 tgl                      2282 CBC      100799 :         foreach(lc, query->constraintDeps)
 4628 tgl                      2283 ECB             :         {
 4628 tgl                      2284 GIC          26 :             add_object_address(OCLASS_CONSTRAINT, lfirst_oid(lc), 0,
                               2285                 :                                context->addrs);
                               2286                 :         }
                               2287                 : 
                               2288                 :         /* Examine substructure of query */
 7572                          2289          100773 :         context->rtables = lcons(query->rtable, context->rtables);
                               2290          100773 :         result = query_tree_walker(query,
                               2291                 :                                    find_expr_references_walker,
 7515 tgl                      2292 ECB             :                                    (void *) context,
                               2293                 :                                    QTW_IGNORE_JOINALIASES |
 1284 rhodiumtoad              2294                 :                                    QTW_EXAMINE_SORTGROUP);
 6892 neilc                    2295 GIC      100773 :         context->rtables = list_delete_first(context->rtables);
 7572 tgl                      2296          100773 :         return result;
                               2297                 :     }
 5358                          2298         1382652 :     else if (IsA(node, SetOperationStmt))
 5358 tgl                      2299 ECB             :     {
 5358 tgl                      2300 CBC       11568 :         SetOperationStmt *setop = (SetOperationStmt *) node;
                               2301                 : 
                               2302                 :         /* we need to look at the groupClauses for operator references */
 5358 tgl                      2303 GIC       11568 :         find_expr_references_walker((Node *) setop->groupClauses, context);
                               2304                 :         /* fall through to examine child nodes */
 5358 tgl                      2305 ECB             :     }
 3426 tgl                      2306 CBC     1371084 :     else if (IsA(node, RangeTblFunction))
                               2307                 :     {
                               2308           13880 :         RangeTblFunction *rtfunc = (RangeTblFunction *) node;
                               2309                 :         ListCell   *ct;
 3426 tgl                      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                 :          */
 3426 tgl                      2316 CBC       13934 :         foreach(ct, rtfunc->funccoltypes)
                               2317                 :         {
                               2318              54 :             add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
                               2319                 :                                context->addrs);
                               2320                 :         }
 3426 tgl                      2321 GIC       13934 :         foreach(ct, rtfunc->funccolcollations)
                               2322                 :         {
                               2323              54 :             Oid         collid = lfirst_oid(ct);
                               2324                 : 
  702 tmunro                   2325              54 :             if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
 1243 tgl                      2326 LBC           0 :                 add_object_address(OCLASS_COLLATION, collid, 0,
                               2327                 :                                    context->addrs);
 1243 tgl                      2328 ECB             :         }
                               2329                 :     }
 1243 tgl                      2330 GIC     1357204 :     else if (IsA(node, TableFunc))
 1243 tgl                      2331 ECB             :     {
 1243 tgl                      2332 GIC           8 :         TableFunc  *tf = (TableFunc *) node;
 1243 tgl                      2333 ECB             :         ListCell   *ct;
                               2334                 : 
                               2335                 :         /*
 1243 tgl                      2336 EUB             :          * Add refs for the datatypes and collations used in the TableFunc.
                               2337                 :          */
 1243 tgl                      2338 GIC          44 :         foreach(ct, tf->coltypes)
                               2339                 :         {
 1243 tgl                      2340 CBC          36 :             add_object_address(OCLASS_TYPE, lfirst_oid(ct), 0,
                               2341                 :                                context->addrs);
 1243 tgl                      2342 ECB             :         }
 1243 tgl                      2343 GIC          44 :         foreach(ct, tf->colcollations)
                               2344                 :         {
                               2345              36 :             Oid         collid = lfirst_oid(ct);
                               2346                 : 
  702 tmunro                   2347              36 :             if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
 3426 tgl                      2348 LBC           0 :                 add_object_address(OCLASS_COLLATION, collid, 0,
                               2349                 :                                    context->addrs);
 3426 tgl                      2350 ECB             :         }
                               2351                 :     }
 2815 tgl                      2352 GIC     1357196 :     else if (IsA(node, TableSampleClause))
 2815 tgl                      2353 ECB             :     {
 2815 tgl                      2354 GIC          10 :         TableSampleClause *tsc = (TableSampleClause *) node;
 2815 tgl                      2355 ECB             : 
 2815 tgl                      2356 GIC          10 :         add_object_address(OCLASS_PROC, tsc->tsmhandler, 0,
 2815 tgl                      2357 ECB             :                            context->addrs);
 2815 tgl                      2358 EUB             :         /* fall through to examine arguments */
                               2359                 :     }
                               2360                 : 
 7572 tgl                      2361 GIC     2251671 :     return expression_tree_walker(node, find_expr_references_walker,
 7572 tgl                      2362 ECB             :                                   (void *) context);
                               2363                 : }
                               2364                 : 
                               2365                 : /*
  261                          2366                 :  * find_expr_references_walker subroutine: handle a Var reference
                               2367                 :  * to an RTE_FUNCTION RTE
                               2368                 :  */
                               2369                 : static void
  261 tgl                      2370 GIC      116384 : process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum,
  261 tgl                      2371 ECB             :                          find_expr_references_context *context)
                               2372                 : {
  261 tgl                      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
  261 tgl                      2380 ECB             :      * relation).
                               2381                 :      */
  261 tgl                      2382 GIC      116495 :     foreach(lc, rte->functions)
  261 tgl                      2383 ECB             :     {
  261 tgl                      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);
  261 tgl                      2392 CBC      116339 :             if (tupdesc && tupdesc->tdtypeid != RECORDOID)
                               2393                 :             {
  261 tgl                      2394 ECB             :                 /*
                               2395                 :                  * Named composite type, so individual columns could get
                               2396                 :                  * dropped.  Make a dependency on this specific column.
                               2397                 :                  */
  261 tgl                      2398 GIC         111 :                 Oid         reltype = get_typ_typrelid(tupdesc->tdtypeid);
                               2399                 : 
                               2400             111 :                 Assert(attnum - atts_done <= tupdesc->natts);
  261 tgl                      2401 CBC         111 :                 if (OidIsValid(reltype))    /* can this fail? */
                               2402             111 :                     add_object_address(OCLASS_CLASS, reltype,
                               2403                 :                                        attnum - atts_done,
                               2404                 :                                        context->addrs);
  261 tgl                      2405 GIC      116339 :                 return;
                               2406                 :             }
                               2407                 :             /* Nothing to do; function's result type is handled elsewhere */
  261 tgl                      2408 CBC      116228 :             return;
                               2409                 :         }
                               2410             111 :         atts_done += rtfunc->funccolcount;
  261 tgl                      2411 ECB             :     }
                               2412                 : 
                               2413                 :     /* If we get here, must be looking for the ordinality column */
  261 tgl                      2414 GIC          45 :     if (rte->funcordinality && attnum == atts_done + 1)
  261 tgl                      2415 CBC          45 :         return;
                               2416                 : 
                               2417                 :     /* this probably can't happen ... */
  261 tgl                      2418 LBC           0 :     ereport(ERROR,
                               2419                 :             (errcode(ERRCODE_UNDEFINED_COLUMN),
  261 tgl                      2420 ECB             :              errmsg("column %d of relation \"%s\" does not exist",
                               2421                 :                     attnum, rte->eref->aliasname)));
                               2422                 : }
                               2423                 : 
 7572                          2424                 : /*
                               2425                 :  * Given an array of dependency references, eliminate any duplicates.
                               2426                 :  */
                               2427                 : static void
 7572 tgl                      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.
 5418 tgl                      2438 ECB             :      */
 5418 tgl                      2439 GIC      469781 :     Assert(!addrs->extras);
                               2440                 : 
 7572                          2441          469781 :     if (addrs->numrefs <= 1)
                               2442          143659 :         return;                 /* nothing to do */
                               2443                 : 
                               2444                 :     /* Sort the refs so that duplicates are adjacent */
   61 peter                    2445 GNC      326122 :     qsort(addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
                               2446                 :           object_address_comparator);
                               2447                 : 
                               2448                 :     /* Remove dups */
 7572 tgl                      2449 CBC      326122 :     priorobj = addrs->refs;
 7572 tgl                      2450 GIC      326122 :     newrefs = 1;
 7572 tgl                      2451 CBC     3738996 :     for (oldref = 1; oldref < addrs->numrefs; oldref++)
 7572 tgl                      2452 ECB             :     {
 7522 bruce                    2453 GIC     3412874 :         ObjectAddress *thisobj = addrs->refs + oldref;
                               2454                 : 
 7572 tgl                      2455 CBC     3412874 :         if (priorobj->classId == thisobj->classId &&
 7572 tgl                      2456 GIC     3006294 :             priorobj->objectId == thisobj->objectId)
                               2457                 :         {
                               2458         2125120 :             if (priorobj->objectSubId == thisobj->objectSubId)
 7572 tgl                      2459 CBC     1679332 :                 continue;       /* identical, so drop thisobj */
 7522 bruce                    2460 ECB             : 
 7572 tgl                      2461                 :             /*
                               2462                 :              * If we have a whole-object reference and a reference to a part
 6385 bruce                    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                 :              */
 7572 tgl                      2468 CBC      445788 :             if (priorobj->objectSubId == 0)
 7572 tgl                      2469 ECB             :             {
                               2470                 :                 /* replace whole ref with partial */
 7572 tgl                      2471 GIC      104572 :                 priorobj->objectSubId = thisobj->objectSubId;
                               2472          104572 :                 continue;
                               2473                 :             }
                               2474                 :         }
                               2475                 :         /* Not identical, so add thisobj to output set */
                               2476         1628970 :         priorobj++;
 5418                          2477         1628970 :         *priorobj = *thisobj;
 7572 tgl                      2478 CBC     1628970 :         newrefs++;
                               2479                 :     }
                               2480                 : 
                               2481          326122 :     addrs->numrefs = newrefs;
 7572 tgl                      2482 ECB             : }
                               2483                 : 
                               2484                 : /*
                               2485                 :  * qsort comparator for ObjectAddress items
                               2486                 :  */
                               2487                 : static int
 7572 tgl                      2488 CBC    12692148 : object_address_comparator(const void *a, const void *b)
                               2489                 : {
 7572 tgl                      2490 GIC    12692148 :     const ObjectAddress *obja = (const ObjectAddress *) a;
 7572 tgl                      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                 :      */
 1539                          2498        12692148 :     if (obja->objectId > objb->objectId)
 7572 tgl                      2499 GIC     3464856 :         return -1;
 7572 tgl                      2500 CBC     9227292 :     if (obja->objectId < objb->objectId)
 1539                          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                 :      */
 1539 tgl                      2507 GIC     3723698 :     if (obja->classId < objb->classId)
 7572 tgl                      2508 LBC           0 :         return -1;
 1539 tgl                      2509 CBC     3723698 :     if (obja->classId > objb->classId)
 7572 tgl                      2510 LBC           0 :         return 1;
 7522 bruce                    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,
 1539 tgl                      2517                 :      * and is also the best order for findDependentObjects.
 7572 tgl                      2518 EUB             :      */
 7572 tgl                      2519 CBC     3723698 :     if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
 7572 tgl                      2520 GBC      850673 :         return -1;
 7572 tgl                      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                 :  *
 6076 alvherre                 2529 ECB             :  * new_object_addresses: create a new ObjectAddresses array.
 7572 tgl                      2530                 :  */
 6076 alvherre                 2531                 : ObjectAddresses *
 6076 alvherre                 2532 CBC      497366 : new_object_addresses(void)
 7572 tgl                      2533 ECB             : {
                               2534                 :     ObjectAddresses *addrs;
                               2535                 : 
 6076 alvherre                 2536 GIC      497366 :     addrs = palloc(sizeof(ObjectAddresses));
                               2537                 : 
 7572 tgl                      2538          497366 :     addrs->numrefs = 0;
 6076 alvherre                 2539          497366 :     addrs->maxrefs = 32;
 7572 tgl                      2540          497366 :     addrs->refs = (ObjectAddress *)
                               2541          497366 :         palloc(addrs->maxrefs * sizeof(ObjectAddress));
 5418 tgl                      2542 CBC      497366 :     addrs->extras = NULL;        /* until/unless needed */
                               2543                 : 
 6076 alvherre                 2544 GIC      497366 :     return addrs;
                               2545                 : }
 7572 tgl                      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
 6913 tgl                      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 */
 7572 tgl                      2560 GIC     2283748 :     if (addrs->numrefs >= addrs->maxrefs)
                               2561                 :     {
                               2562           33962 :         addrs->maxrefs *= 2;
                               2563           33962 :         addrs->refs = (ObjectAddress *)
 7572 tgl                      2564 CBC       33962 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
 5418 tgl                      2565 GIC       33962 :         Assert(!addrs->extras);
 7572 tgl                      2566 ECB             :     }
                               2567                 :     /* record this item */
 7572 tgl                      2568 CBC     2283748 :     item = addrs->refs + addrs->numrefs;
                               2569         2283748 :     item->classId = object_classes[oclass];
 7572 tgl                      2570 GIC     2283748 :     item->objectId = objectId;
                               2571         2283748 :     item->objectSubId = subId;
 7572 tgl                      2572 CBC     2283748 :     addrs->numrefs++;
                               2573         2283748 : }
 7572 tgl                      2574 ECB             : 
                               2575                 : /*
                               2576                 :  * Add an entry to an ObjectAddresses array.
                               2577                 :  *
                               2578                 :  * As above, but specify entry exactly.
                               2579                 :  */
                               2580                 : void
 7572 tgl                      2581 GIC     1570639 : add_exact_object_address(const ObjectAddress *object,
                               2582                 :                          ObjectAddresses *addrs)
                               2583                 : {
                               2584                 :     ObjectAddress *item;
 7572 tgl                      2585 ECB             : 
                               2586                 :     /* enlarge array if needed */
 7572 tgl                      2587 GIC     1570639 :     if (addrs->numrefs >= addrs->maxrefs)
                               2588                 :     {
                               2589              13 :         addrs->maxrefs *= 2;
                               2590              13 :         addrs->refs = (ObjectAddress *)
 7572 tgl                      2591 CBC          13 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
 5418 tgl                      2592 GIC          13 :         Assert(!addrs->extras);
 7572 tgl                      2593 ECB             :     }
                               2594                 :     /* record this item */
 7572 tgl                      2595 CBC     1570639 :     item = addrs->refs + addrs->numrefs;
                               2596         1570639 :     *item = *object;
 7572 tgl                      2597 GIC     1570639 :     addrs->numrefs++;
                               2598         1570639 : }
 7572 tgl                      2599 ECB             : 
 5418                          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
 5418 tgl                      2606 GIC       85912 : add_exact_object_address_extra(const ObjectAddress *object,
                               2607                 :                                const ObjectAddressExtra *extra,
                               2608                 :                                ObjectAddresses *addrs)
                               2609                 : {
 5418 tgl                      2610 ECB             :     ObjectAddress *item;
                               2611                 :     ObjectAddressExtra *itemextra;
                               2612                 : 
                               2613                 :     /* allocate extra space if first time */
 5418 tgl                      2614 GIC       85912 :     if (!addrs->extras)
                               2615           13772 :         addrs->extras = (ObjectAddressExtra *)
                               2616           13772 :             palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
                               2617                 : 
 5418 tgl                      2618 ECB             :     /* enlarge array if needed */
 5418 tgl                      2619 CBC       85912 :     if (addrs->numrefs >= addrs->maxrefs)
 5418 tgl                      2620 ECB             :     {
 5418 tgl                      2621 GIC         316 :         addrs->maxrefs *= 2;
                               2622             316 :         addrs->refs = (ObjectAddress *)
 5418 tgl                      2623 CBC         316 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
 5418 tgl                      2624 GIC         316 :         addrs->extras = (ObjectAddressExtra *)
 5050 bruce                    2625 CBC         316 :             repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
 5418 tgl                      2626 ECB             :     }
                               2627                 :     /* record this item */
 5418 tgl                      2628 CBC       85912 :     item = addrs->refs + addrs->numrefs;
                               2629           85912 :     *item = *object;
 5418 tgl                      2630 GIC       85912 :     itemextra = addrs->extras + addrs->numrefs;
                               2631           85912 :     *itemextra = *extra;
 5418 tgl                      2632 CBC       85912 :     addrs->numrefs++;
                               2633           85912 : }
 5418 tgl                      2634 ECB             : 
 7572                          2635                 : /*
 7504                          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
 7504 tgl                      2641 GIC         304 : object_address_present(const ObjectAddress *object,
                               2642                 :                        const ObjectAddresses *addrs)
                               2643                 : {
                               2644                 :     int         i;
 7504 tgl                      2645 ECB             : 
 7504 tgl                      2646 GIC        1010 :     for (i = addrs->numrefs - 1; i >= 0; i--)
                               2647                 :     {
 5418                          2648             706 :         const ObjectAddress *thisobj = addrs->refs + i;
                               2649                 : 
 7504 tgl                      2650 CBC         706 :         if (object->classId == thisobj->classId &&
 7504 tgl                      2651 GIC         184 :             object->objectId == thisobj->objectId)
 7572 tgl                      2652 ECB             :         {
 7504 tgl                      2653 UIC           0 :             if (object->objectSubId == thisobj->objectSubId ||
 7504 tgl                      2654 LBC           0 :                 thisobj->objectSubId == 0)
                               2655               0 :                 return true;
                               2656                 :         }
 7572 tgl                      2657 EUB             :     }
                               2658                 : 
 7504 tgl                      2659 GBC         304 :     return false;
                               2660                 : }
                               2661                 : 
                               2662                 : /*
 5418 tgl                      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
 5418 tgl                      2667 GIC      106023 : object_address_present_add_flags(const ObjectAddress *object,
                               2668                 :                                  int flags,
                               2669                 :                                  ObjectAddresses *addrs)
                               2670                 : {
 3071 tgl                      2671 CBC      106023 :     bool        result = false;
                               2672                 :     int         i;
                               2673                 : 
 5418 tgl                      2674 GIC     2633940 :     for (i = addrs->numrefs - 1; i >= 0; i--)
 5418 tgl                      2675 ECB             :     {
 5418 tgl                      2676 GIC     2527917 :         ObjectAddress *thisobj = addrs->refs + i;
                               2677                 : 
 5418 tgl                      2678 CBC     2527917 :         if (object->classId == thisobj->classId &&
 5418 tgl                      2679 GIC      831816 :             object->objectId == thisobj->objectId)
 5418 tgl                      2680 ECB             :         {
 5418 tgl                      2681 GIC       19401 :             if (object->objectSubId == thisobj->objectSubId)
 5418 tgl                      2682 ECB             :             {
 5418 tgl                      2683 CBC       19185 :                 ObjectAddressExtra *thisextra = addrs->extras + i;
                               2684                 : 
                               2685           19185 :                 thisextra->flags |= flags;
 3071 tgl                      2686 GIC       19185 :                 result = true;
 5418 tgl                      2687 ECB             :             }
 3071 tgl                      2688 GIC         216 :             else if (thisobj->objectSubId == 0)
 5418 tgl                      2689 ECB             :             {
                               2690                 :                 /*
                               2691                 :                  * We get here if we find a need to delete a column after
 3260 bruce                    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                 :                  */
 3071 tgl                      2697 GIC         210 :                 result = true;
                               2698                 :             }
                               2699               6 :             else if (object->objectSubId == 0)
                               2700                 :             {
 3071 tgl                      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                 :                  */
 3071 tgl                      2725 GIC           3 :                 ObjectAddressExtra *thisextra = addrs->extras + i;
                               2726                 : 
 1542                          2727               3 :                 if (flags)
                               2728               3 :                     thisextra->flags |= (flags | DEPFLAG_SUBOBJECT);
 5418 tgl                      2729 ECB             :             }
                               2730                 :         }
                               2731                 :     }
                               2732                 : 
 3071 tgl                      2733 GIC      106023 :     return result;
                               2734                 : }
                               2735                 : 
                               2736                 : /*
 4246 tgl                      2737 ECB             :  * Similar to above, except we search an ObjectAddressStack.
                               2738                 :  */
                               2739                 : static bool
 4246 tgl                      2740 GIC      151988 : stack_address_present_add_flags(const ObjectAddress *object,
                               2741                 :                                 int flags,
                               2742                 :                                 ObjectAddressStack *stack)
                               2743                 : {
 3071 tgl                      2744 CBC      151988 :     bool        result = false;
                               2745                 :     ObjectAddressStack *stackptr;
                               2746                 : 
 4246 tgl                      2747 GIC      406994 :     for (stackptr = stack; stackptr; stackptr = stackptr->next)
 4246 tgl                      2748 ECB             :     {
 4246 tgl                      2749 GIC      255006 :         const ObjectAddress *thisobj = stackptr->object;
                               2750                 : 
 4246 tgl                      2751 CBC      255006 :         if (object->classId == thisobj->classId &&
 4246 tgl                      2752 GIC      115387 :             object->objectId == thisobj->objectId)
 4246 tgl                      2753 ECB             :         {
 4246 tgl                      2754 GIC       45983 :             if (object->objectSubId == thisobj->objectSubId)
 4246 tgl                      2755 ECB             :             {
 4246 tgl                      2756 CBC       45693 :                 stackptr->flags |= flags;
 3071 tgl                      2757 GIC       45693 :                 result = true;
 3071 tgl                      2758 ECB             :             }
 3071 tgl                      2759 GIC         290 :             else if (thisobj->objectSubId == 0)
 3071 tgl                      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                 :                  */
 3071 tgl                      2767 GIC         272 :                 result = true;
                               2768                 :             }
                               2769              18 :             else if (object->objectSubId == 0)
                               2770                 :             {
 3071 tgl                      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                 :                  */
 1542 tgl                      2776 UIC           0 :                 if (flags)
                               2777               0 :                     stackptr->flags |= (flags | DEPFLAG_SUBOBJECT);
                               2778                 :             }
                               2779                 :         }
 4246 tgl                      2780 EUB             :     }
                               2781                 : 
 3071 tgl                      2782 GIC      151988 :     return result;
                               2783                 : }
                               2784                 : 
                               2785                 : /*
 5710 tgl                      2786 ECB             :  * Record multiple dependencies from an ObjectAddresses array, after first
                               2787                 :  * removing any duplicates.
                               2788                 :  */
                               2789                 : void
 5710 tgl                      2790 GIC      391225 : record_object_address_dependencies(const ObjectAddress *depender,
                               2791                 :                                    ObjectAddresses *referenced,
                               2792                 :                                    DependencyType behavior)
                               2793                 : {
 5710 tgl                      2794 CBC      391225 :     eliminate_duplicate_dependencies(referenced);
 5710 tgl                      2795 GIC      391225 :     recordMultipleDependencies(depender,
  888 tmunro                   2796          391225 :                                referenced->refs, referenced->numrefs,
                               2797                 :                                behavior);
 5710 tgl                      2798 CBC      391225 : }
 5710 tgl                      2799 ECB             : 
 1481                          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
 1481 tgl                      2809 GIC          63 : sort_object_addresses(ObjectAddresses *addrs)
                               2810                 : {
                               2811              63 :     if (addrs->numrefs > 1)
   61 peter                    2812 GNC          36 :         qsort(addrs->refs, addrs->numrefs,
 1481 tgl                      2813 ECB             :               sizeof(ObjectAddress),
                               2814                 :               object_address_comparator);
 1481 tgl                      2815 CBC          63 : }
 1481 tgl                      2816 ECB             : 
                               2817                 : /*
                               2818                 :  * Clean up when done with an ObjectAddresses array.
 7572                          2819                 :  */
                               2820                 : void
 6076 alvherre                 2821 GIC      496414 : free_object_addresses(ObjectAddresses *addrs)
                               2822                 : {
 7572 tgl                      2823          496414 :     pfree(addrs->refs);
 5418                          2824          496414 :     if (addrs->extras)
 5418 tgl                      2825 CBC       13604 :         pfree(addrs->extras);
 6076 alvherre                 2826 GIC      496414 :     pfree(addrs);
 7572 tgl                      2827 CBC      496414 : }
 7572 tgl                      2828 ECB             : 
 7576                          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
 7576 tgl                      2836 GIC      159252 : getObjectClass(const ObjectAddress *object)
                               2837                 : {
                               2838                 :     /* only pg_class entries can have nonzero objectSubId */
 4525 alvherre                 2839          159252 :     if (object->classId != RelationRelationId &&
 4525 alvherre                 2840 CBC      106452 :         object->objectSubId != 0)
 3672 alvherre                 2841 UIC           0 :         elog(ERROR, "invalid non-zero objectSubId for object class %u",
                               2842                 :              object->classId);
 4525 alvherre                 2843 ECB             : 
 7576 tgl                      2844 CBC      159252 :     switch (object->classId)
 7576 tgl                      2845 EUB             :     {
 6569 tgl                      2846 GIC       52800 :         case RelationRelationId:
                               2847                 :             /* caller must check objectSubId */
 7576 tgl                      2848 CBC       52800 :             return OCLASS_CLASS;
                               2849                 : 
 6569                          2850            4871 :         case ProcedureRelationId:
 7576 tgl                      2851 GIC        4871 :             return OCLASS_PROC;
 7576 tgl                      2852 ECB             : 
 6569 tgl                      2853 GIC       59078 :         case TypeRelationId:
 7576 tgl                      2854 CBC       59078 :             return OCLASS_TYPE;
 7576 tgl                      2855 ECB             : 
 6569 tgl                      2856 GIC         297 :         case CastRelationId:
 6569 tgl                      2857 CBC         297 :             return OCLASS_CAST;
 7576 tgl                      2858 ECB             : 
 4439 peter_e                  2859 GIC         194 :         case CollationRelationId:
 4439 peter_e                  2860 CBC         194 :             return OCLASS_COLLATION;
 4439 peter_e                  2861 ECB             : 
 6569 tgl                      2862 GIC       18084 :         case ConstraintRelationId:
 6569 tgl                      2863 CBC       18084 :             return OCLASS_CONSTRAINT;
 6569 tgl                      2864 ECB             : 
 6569 tgl                      2865 GIC          98 :         case ConversionRelationId:
 6569 tgl                      2866 CBC          98 :             return OCLASS_CONVERSION;
 6569 tgl                      2867 ECB             : 
 6569 tgl                      2868 GIC        3123 :         case AttrDefaultRelationId:
 6569 tgl                      2869 CBC        3123 :             return OCLASS_DEFAULT;
 6569 tgl                      2870 ECB             : 
 6569 tgl                      2871 GIC          81 :         case LanguageRelationId:
 6569 tgl                      2872 CBC          81 :             return OCLASS_LANGUAGE;
 6569 tgl                      2873 ECB             : 
 4867 itagaki.takahiro         2874 GIC          59 :         case LargeObjectRelationId:
 4867 itagaki.takahiro         2875 CBC          59 :             return OCLASS_LARGEOBJECT;
 4867 itagaki.takahiro         2876 ECB             : 
 6569 tgl                      2877 GIC         728 :         case OperatorRelationId:
 6569 tgl                      2878 CBC         728 :             return OCLASS_OPERATOR;
 6569 tgl                      2879 ECB             : 
 6569 tgl                      2880 GIC         192 :         case OperatorClassRelationId:
 6569 tgl                      2881 CBC         192 :             return OCLASS_OPCLASS;
 6569 tgl                      2882 ECB             : 
 5951 tgl                      2883 GIC         208 :         case OperatorFamilyRelationId:
 5951 tgl                      2884 CBC         208 :             return OCLASS_OPFAMILY;
 5951 tgl                      2885 ECB             : 
 2573 alvherre                 2886 GIC          84 :         case AccessMethodRelationId:
 2573 alvherre                 2887 CBC          84 :             return OCLASS_AM;
 2573 alvherre                 2888 ECB             : 
 5951 tgl                      2889 GIC         917 :         case AccessMethodOperatorRelationId:
 5951 tgl                      2890 CBC         917 :             return OCLASS_AMOP;
 5951 tgl                      2891 ECB             : 
 5951 tgl                      2892 GIC         366 :         case AccessMethodProcedureRelationId:
 5951 tgl                      2893 CBC         366 :             return OCLASS_AMPROC;
 5951 tgl                      2894 ECB             : 
 6569 tgl                      2895 GIC        2644 :         case RewriteRelationId:
 6569 tgl                      2896 CBC        2644 :             return OCLASS_REWRITE;
 6569 tgl                      2897 ECB             : 
 6569 tgl                      2898 GIC       11202 :         case TriggerRelationId:
 6569 tgl                      2899 CBC       11202 :             return OCLASS_TRIGGER;
 6569 tgl                      2900 ECB             : 
 6569 tgl                      2901 GIC         479 :         case NamespaceRelationId:
 6569 tgl                      2902 CBC         479 :             return OCLASS_SCHEMA;
 6485 tgl                      2903 ECB             : 
 2207 alvherre                 2904 GIC         433 :         case StatisticExtRelationId:
 2207 alvherre                 2905 CBC         433 :             return OCLASS_STATISTIC_EXT;
 2207 alvherre                 2906 ECB             : 
 5710 tgl                      2907 GIC          87 :         case TSParserRelationId:
 5710 tgl                      2908 CBC          87 :             return OCLASS_TSPARSER;
 5710 tgl                      2909 ECB             : 
 5710 tgl                      2910 GIC          96 :         case TSDictionaryRelationId:
 5710 tgl                      2911 CBC          96 :             return OCLASS_TSDICT;
 5710 tgl                      2912 ECB             : 
 5710 tgl                      2913 GIC          87 :         case TSTemplateRelationId:
 5710 tgl                      2914 CBC          87 :             return OCLASS_TSTEMPLATE;
 5710 tgl                      2915 ECB             : 
 5710 tgl                      2916 GIC         100 :         case TSConfigRelationId:
 5710 tgl                      2917 CBC         100 :             return OCLASS_TSCONFIG;
 5710 tgl                      2918 ECB             : 
 6485 tgl                      2919 GIC          59 :         case AuthIdRelationId:
 6485 tgl                      2920 CBC          59 :             return OCLASS_ROLE;
 6485 tgl                      2921 ECB             : 
  234 rhaas                    2922 GNC          42 :         case AuthMemRelationId:
                               2923              42 :             return OCLASS_ROLE_MEMBERSHIP;
                               2924                 : 
 6485 tgl                      2925 GIC          21 :         case DatabaseRelationId:
 6485 tgl                      2926 CBC          21 :             return OCLASS_DATABASE;
 6485 tgl                      2927 ECB             : 
 6485 tgl                      2928 GIC          15 :         case TableSpaceRelationId:
 6485 tgl                      2929 CBC          15 :             return OCLASS_TBLSPACE;
 5224 peter_e                  2930 ECB             : 
 5224 peter_e                  2931 GIC         149 :         case ForeignDataWrapperRelationId:
 5224 peter_e                  2932 CBC         149 :             return OCLASS_FDW;
 5224 peter_e                  2933 ECB             : 
 5224 peter_e                  2934 GIC         211 :         case ForeignServerRelationId:
 5224 peter_e                  2935 CBC         211 :             return OCLASS_FOREIGN_SERVER;
 5224 peter_e                  2936 ECB             : 
 5224 peter_e                  2937 GIC         211 :         case UserMappingRelationId:
 5224 peter_e                  2938 CBC         211 :             return OCLASS_USER_MAPPING;
 4934 tgl                      2939 ECB             : 
 4934 tgl                      2940 GIC         186 :         case DefaultAclRelationId:
 4934 tgl                      2941 CBC         186 :             return OCLASS_DEFACL;
 4443 tgl                      2942 ECB             : 
 4443 tgl                      2943 GIC         101 :         case ExtensionRelationId:
 4443 tgl                      2944 CBC         101 :             return OCLASS_EXTENSION;
 3917 rhaas                    2945 ECB             : 
 3917 rhaas                    2946 GIC         156 :         case EventTriggerRelationId:
 3917 rhaas                    2947 CBC         156 :             return OCLASS_EVENT_TRIGGER;
 3124 sfrost                   2948 ECB             : 
  368 tgl                      2949 GIC          80 :         case ParameterAclRelationId:
  368 tgl                      2950 CBC          80 :             return OCLASS_PARAMETER_ACL;
  368 tgl                      2951 ECB             : 
 3055 sfrost                   2952 GIC         529 :         case PolicyRelationId:
 3055 sfrost                   2953 CBC         529 :             return OCLASS_POLICY;
 2905 peter_e                  2954 ECB             : 
  529 akapila                  2955 GIC         224 :         case PublicationNamespaceRelationId:
  529 akapila                  2956 CBC         224 :             return OCLASS_PUBLICATION_NAMESPACE;
  529 akapila                  2957 ECB             : 
 2271 peter_e                  2958 GIC         235 :         case PublicationRelationId:
 2271 peter_e                  2959 CBC         235 :             return OCLASS_PUBLICATION;
 2271 peter_e                  2960 ECB             : 
 2271 peter_e                  2961 GIC         587 :         case PublicationRelRelationId:
 2271 peter_e                  2962 CBC         587 :             return OCLASS_PUBLICATION_REL;
 2271 peter_e                  2963 ECB             : 
 2271 peter_e                  2964 GIC          57 :         case SubscriptionRelationId:
 2271 peter_e                  2965 CBC          57 :             return OCLASS_SUBSCRIPTION;
 2271 peter_e                  2966 ECB             : 
 2905 peter_e                  2967 GIC          81 :         case TransformRelationId:
 2905 peter_e                  2968 CBC          81 :             return OCLASS_TRANSFORM;
 7570 tgl                      2969 ECB             :     }
                               2970                 : 
 6569                          2971                 :     /* shouldn't get here */
 7202 tgl                      2972 LBC           0 :     elog(ERROR, "unrecognized object class: %u", object->classId);
                               2973                 :     return OCLASS_CLASS;        /* keep compiler quiet */
 7576 tgl                      2974 ECB             : }
 2559 sfrost                   2975                 : 
                               2976                 : /*
                               2977                 :  * delete initial ACL for extension objects
                               2978                 :  */
 2559 sfrost                   2979 EUB             : static void
 2559 sfrost                   2980 GIC       83389 : DeleteInitPrivs(const ObjectAddress *object)
                               2981                 : {
                               2982                 :     Relation    relation;
                               2983                 :     ScanKeyData key[3];
                               2984                 :     SysScanDesc scan;
                               2985                 :     HeapTuple   oldtuple;
                               2986                 : 
 1539 andres                   2987 CBC       83389 :     relation = table_open(InitPrivsRelationId, RowExclusiveLock);
                               2988                 : 
 2559 sfrost                   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],
 2559 sfrost                   2994 ECB             :                 Anum_pg_init_privs_classoid,
                               2995                 :                 BTEqualStrategyNumber, F_OIDEQ,
 2559 sfrost                   2996 CBC       83389 :                 ObjectIdGetDatum(object->classId));
 2559 sfrost                   2997 GIC       83389 :     ScanKeyInit(&key[2],
                               2998                 :                 Anum_pg_init_privs_objsubid,
 2559 sfrost                   2999 ECB             :                 BTEqualStrategyNumber, F_INT4EQ,
 2559 sfrost                   3000 CBC       83389 :                 Int32GetDatum(object->objectSubId));
                               3001                 : 
 2559 sfrost                   3002 GIC       83389 :     scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
 2559 sfrost                   3003 ECB             :                               NULL, 3, key);
                               3004                 : 
 2559 sfrost                   3005 GIC       83415 :     while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
 2258 tgl                      3006              26 :         CatalogTupleDelete(relation, &oldtuple->t_self);
 2559 sfrost                   3007 ECB             : 
 2559 sfrost                   3008 GIC       83389 :     systable_endscan(scan);
 2559 sfrost                   3009 ECB             : 
 1539 andres                   3010 GIC       83389 :     table_close(relation, RowExclusiveLock);
 2559 sfrost                   3011           83389 : }
        

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