LCOV - differential code coverage report
Current view: top level - src/backend/catalog - dependency.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 90.1 % 850 766 18 66 63 703 18 149
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 27 27 5 22 2
Baseline: 16@8cea358b128 Branches: 79.1 % 612 484 4 124 19 465
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 78.0 % 82 64 18 63 1
(240..) days: 91.4 % 768 702 66 702
Function coverage date bins:
[..60] days: 100.0 % 1 1 1
(240..) days: 100.0 % 26 26 4 22
Branch coverage date bins:
[..60] days: 82.6 % 23 19 4 19
(240..) days: 78.9 % 589 465 124 465

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

Generated by: LCOV version 2.1-beta2-3-g6141622