LCOV - differential code coverage report
Current view: top level - src/backend/catalog - dependency.c (source / functions) Coverage Total Hit UNC UBC GBC GIC GNC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 90.6 % 859 778 81 3 2 773
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 27 27 1 26
Baseline: lcov-20250906-005545-baseline Branches: 79.7 % 622 496 1 125 3 1 1 491
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 4 4 2 2
(360..) days: 90.5 % 855 774 81 3 771
Function coverage date bins:
(360..) days: 100.0 % 27 27 1 26
Branch coverage date bins:
(30,360] days: 50.0 % 4 2 1 1 1 1
(360..) days: 79.9 % 618 494 124 3 1 490

 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-2025, 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
 4546 alvherre@alvh.no-ip.      185                 :CBC       16060 : 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                 :                :      */
 3914                           193   [ +  +  +  + ]:          16060 :     if (trackDroppedObjectsNeeded() && !(flags & PERFORM_DELETION_INTERNAL))
                                194                 :                :     {
 4546                           195         [ +  + ]:           2230 :         for (i = 0; i < targetObjects->numrefs; i++)
                                196                 :                :         {
 3914                           197                 :           1875 :             const ObjectAddress *thisobj = &targetObjects->refs[i];
                                198                 :           1875 :             const ObjectAddressExtra *extra = &targetObjects->extras[i];
 3759 bruce@momjian.us          199                 :           1875 :             bool        original = false;
                                200                 :           1875 :             bool        normal = false;
                                201                 :                : 
 3914 alvherre@alvh.no-ip.      202         [ +  + ]:           1875 :             if (extra->flags & DEPFLAG_ORIGINAL)
                                203                 :            397 :                 original = true;
                                204         [ +  + ]:           1875 :             if (extra->flags & DEPFLAG_NORMAL)
                                205                 :            171 :                 normal = true;
                                206         [ -  + ]:           1875 :             if (extra->flags & DEPFLAG_REVERSE)
 3914 alvherre@alvh.no-ip.      207                 :UBC           0 :                 normal = true;
                                208                 :                : 
  529 peter@eisentraut.org      209         [ +  + ]:CBC        1875 :             if (EventTriggerSupportsObject(thisobj))
                                210                 :                :             {
 3914 alvherre@alvh.no-ip.      211                 :           1817 :                 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                 :                :      */
 4546                           220         [ +  + ]:         118579 :     for (i = 0; i < targetObjects->numrefs; i++)
                                221                 :                :     {
                                222                 :         102524 :         ObjectAddress *thisobj = targetObjects->refs + i;
 3200 tgl@sss.pgh.pa.us         223                 :         102524 :         ObjectAddressExtra *thisextra = targetObjects->extras + i;
                                224                 :                : 
                                225         [ +  + ]:         102524 :         if ((flags & PERFORM_DELETION_SKIP_ORIGINAL) &&
                                226         [ +  + ]:           4675 :             (thisextra->flags & DEPFLAG_ORIGINAL))
                                227                 :            433 :             continue;
                                228                 :                : 
 4546 alvherre@alvh.no-ip.      229                 :         102091 :         deleteOneObject(thisobj, depRel, flags);
                                230                 :                :     }
                                231                 :          16055 : }
                                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
 8457 tgl@sss.pgh.pa.us         273                 :           2942 : 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                 :                :      */
 2420 andres@anarazel.de        283                 :           2942 :     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                 :                :      */
 4901 simon@2ndQuadrant.co      289                 :           2942 :     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                 :                :      */
 6299 tgl@sss.pgh.pa.us         295                 :           2942 :     targetObjects = new_object_addresses();
                                296                 :                : 
                                297                 :           2942 :     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                 :           2942 :     reportDependentObjects(targetObjects,
                                309                 :                :                            behavior,
                                310                 :                :                            flags,
                                311                 :                :                            object);
                                312                 :                : 
                                313                 :                :     /* do the deed */
 4546 alvherre@alvh.no-ip.      314                 :           2924 :     deleteObjectsInList(targetObjects, &depRel, flags);
                                315                 :                : 
                                316                 :                :     /* And clean up */
 6299 tgl@sss.pgh.pa.us         317                 :           2923 :     free_object_addresses(targetObjects);
                                318                 :                : 
 2420 andres@anarazel.de        319                 :           2923 :     table_close(depRel, RowExclusiveLock);
 6957 alvherre@alvh.no-ip.      320                 :           2923 : }
                                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                 :          14442 : 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... */
 6293 tgl@sss.pgh.pa.us         340         [ +  + ]:          14442 :     if (objects->numrefs <= 0)
                                341                 :           1134 :         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                 :                :      */
 2420 andres@anarazel.de        347                 :          13308 :     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                 :                :      */
 6299 tgl@sss.pgh.pa.us         357                 :          13308 :     targetObjects = new_object_addresses();
                                358                 :                : 
 6957 alvherre@alvh.no-ip.      359         [ +  + ]:          29446 :     for (i = 0; i < objects->numrefs; i++)
                                360                 :                :     {
 6299 tgl@sss.pgh.pa.us         361                 :          16159 :         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                 :                :          */
 4901 simon@2ndQuadrant.co      367                 :          16159 :         AcquireDeletionLock(thisobj, flags);
                                368                 :                : 
 6299 tgl@sss.pgh.pa.us         369                 :          16159 :         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                 :          13287 :     reportDependentObjects(targetObjects,
                                385                 :                :                            behavior,
                                386                 :                :                            flags,
 6293                           387         [ +  + ]:          13287 :                            (objects->numrefs == 1 ? objects->refs : NULL));
                                388                 :                : 
                                389                 :                :     /* do the deed */
 4546 alvherre@alvh.no-ip.      390                 :          13136 :     deleteObjectsInList(targetObjects, &depRel, flags);
                                391                 :                : 
                                392                 :                :     /* And clean up */
 6299 tgl@sss.pgh.pa.us         393                 :          13132 :     free_object_addresses(targetObjects);
                                394                 :                : 
 2420 andres@anarazel.de        395                 :          13132 :     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
 6299 tgl@sss.pgh.pa.us         432                 :         128962 : 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                 :                :      */
 3200                           469         [ +  + ]:         128962 :     if (stack_address_present_add_flags(object, objflags, stack))
 5127                           470                 :          24318 :         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                 :                :      */
  568 akorotkov@postgresql      476                 :         128803 :     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                 :                :      */
 3200 tgl@sss.pgh.pa.us         487         [ +  + ]:         128803 :     if (object_address_present_add_flags(object, objflags, targetObjects))
 6299                           488                 :          23302 :         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                 :                :      */
 1514                           494         [ +  + ]:         105501 :     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                 :                :      */
 7969                           510                 :         105500 :     ScanKeyInit(&key[0],
                                511                 :                :                 Anum_pg_depend_classid,
                                512                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                513                 :         105500 :                 ObjectIdGetDatum(object->classId));
                                514                 :         105500 :     ScanKeyInit(&key[1],
                                515                 :                :                 Anum_pg_depend_objid,
                                516                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                517                 :         105500 :                 ObjectIdGetDatum(object->objectId));
 8457                           518         [ +  + ]:         105500 :     if (object->objectSubId != 0)
                                519                 :                :     {
                                520                 :                :         /* Consider only dependencies of this sub-object */
 7969                           521                 :           1097 :         ScanKeyInit(&key[2],
                                522                 :                :                     Anum_pg_depend_objsubid,
                                523                 :                :                     BTEqualStrategyNumber, F_INT4EQ,
                                524                 :           1097 :                     Int32GetDatum(object->objectSubId));
 8457                           525                 :           1097 :         nkeys = 3;
                                526                 :                :     }
                                527                 :                :     else
                                528                 :                :     {
                                529                 :                :         /* Consider dependencies of this object and any sub-objects it has */
                                530                 :         104403 :         nkeys = 2;
                                531                 :                :     }
                                532                 :                : 
 4658                           533                 :         105500 :     scan = systable_beginscan(*depRel, DependDependerIndexId, true,
                                534                 :                :                               NULL, nkeys, key);
                                535                 :                : 
                                536                 :                :     /* initialize variables that loop may fill */
 2399                           537                 :         105500 :     memset(&owningObject, 0, sizeof(owningObject));
                                538                 :         105500 :     memset(&partitionObject, 0, sizeof(partitionObject));
                                539                 :                : 
 8457                           540         [ +  + ]:         252605 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                                541                 :                :     {
 8403 bruce@momjian.us          542                 :         147962 :         Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
                                543                 :                : 
 8457 tgl@sss.pgh.pa.us         544                 :         147962 :         otherObject.classId = foundDep->refclassid;
                                545                 :         147962 :         otherObject.objectId = foundDep->refobjid;
                                546                 :         147962 :         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                 :                :          */
 2238                           555         [ +  + ]:         147962 :         if (otherObject.classId == object->classId &&
                                556         [ +  + ]:          48677 :             otherObject.objectId == object->objectId &&
                                557         [ +  + ]:           2166 :             object->objectSubId == 0)
                                558                 :           2154 :             continue;
                                559                 :                : 
 8457                           560   [ +  +  +  +  :         145808 :         switch (foundDep->deptype)
                                              +  - ]
                                561                 :                :         {
                                562                 :          85123 :             case DEPENDENCY_NORMAL:
                                563                 :                :             case DEPENDENCY_AUTO:
                                564                 :                :             case DEPENDENCY_AUTO_EXTENSION:
                                565                 :                :                 /* no problem */
                                566                 :          85123 :                 break;
                                567                 :                : 
 5324                           568                 :           2364 :             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                 :                :                  */
 3200                           576         [ +  + ]:           2364 :                 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                 :                :                  */
 3206                           587         [ +  + ]:           2360 :                 if (creating_extension &&
                                588         [ +  - ]:            169 :                     otherObject.classId == ExtensionRelationId &&
                                589         [ +  - ]:            169 :                     otherObject.objectId == CurrentExtensionObject)
                                590                 :            169 :                     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                 :                :                  */
 6299                           614         [ +  + ]:          55554 :                 if (stack == NULL)
                                615                 :                :                 {
 5828                           616   [ +  -  -  + ]:             40 :                     if (pendingObjects &&
                                617                 :             20 :                         object_address_present(&otherObject, pendingObjects))
                                618                 :                :                     {
 6299 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                 :                :                      */
 2399 tgl@sss.pgh.pa.us         635         [ -  + ]:CBC          20 :                     if (!OidIsValid(owningObject.classId) ||
 2399 tgl@sss.pgh.pa.us         636         [ #  # ]:UBC           0 :                         foundDep->deptype == DEPENDENCY_EXTENSION)
 2399 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                 :                :                  */
 5127                           649         [ +  + ]:          55534 :                 if (stack_address_present_add_flags(&otherObject, 0, stack))
 8453                           650                 :          54677 :                     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                 :                :                  */
 6299                           662                 :            857 :                 ReleaseDeletionLock(object);
 4901 simon@2ndQuadrant.co      663                 :            857 :                 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                 :                :                  */
 6299 tgl@sss.pgh.pa.us         671         [ -  + ]:            857 :                 if (!systable_recheck_tuple(scan, tup))
                                672                 :                :                 {
 6299 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                 :                :                  */
 2399 tgl@sss.pgh.pa.us         683                 :CBC         857 :                 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                 :                :                  */
 6299                           696                 :            857 :                 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                 :                :                  */
 2399                           720         [ -  + ]:            857 :                 if (!object_address_present_add_flags(object, objflags,
                                721                 :                :                                                       targetObjects))
 2399 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. */
 6299 tgl@sss.pgh.pa.us         727                 :CBC         857 :                 return;
                                728                 :                : 
 2399                           729                 :           2479 :             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                 :           2479 :                 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                 :           2479 :                 partitionObject = otherObject;
                                744                 :           2479 :                 break;
                                745                 :                : 
                                746                 :           2479 :             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         [ +  + ]:           2479 :                 if (!(objflags & DEPFLAG_IS_PART))
 2399 tgl@sss.pgh.pa.us         753                 :GBC           2 :                     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                 :                :                  */
 2399 tgl@sss.pgh.pa.us         760                 :CBC        2479 :                 objflags |= DEPFLAG_IS_PART;
                                761                 :           2479 :                 break;
                                762                 :                : 
 8457 tgl@sss.pgh.pa.us         763                 :UBC           0 :             default:
 8083                           764         [ #  # ]:              0 :                 elog(ERROR, "unrecognized dependency type '%c' for %s",
                                765                 :                :                      foundDep->deptype, getObjectDescription(object, false));
                                766                 :                :                 break;
                                767                 :                :         }
                                768                 :                :     }
                                769                 :                : 
 8457 tgl@sss.pgh.pa.us         770                 :CBC      104643 :     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                 :                :      */
 2399                           778         [ +  + ]:         104643 :     if (OidIsValid(owningObject.classId))
                                779                 :                :     {
                                780                 :                :         char       *otherObjDesc;
                                781                 :                : 
                                782         [ +  + ]:             20 :         if (OidIsValid(partitionObject.classId))
 1879 michael@paquier.xyz       783                 :              6 :             otherObjDesc = getObjectDescription(&partitionObject, false);
                                784                 :                :         else
                                785                 :             14 :             otherObjDesc = getObjectDescription(&owningObject, false);
                                786                 :                : 
 2399 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                 :                :      */
 2420                           802                 :         104623 :     maxDependentObjects = 128;  /* arbitrary initial allocation */
                                803                 :                :     dependentObjects = (ObjectAddressAndFlags *)
                                804                 :         104623 :         palloc(maxDependentObjects * sizeof(ObjectAddressAndFlags));
                                805                 :         104623 :     numDependentObjects = 0;
                                806                 :                : 
 7969                           807                 :         104623 :     ScanKeyInit(&key[0],
                                808                 :                :                 Anum_pg_depend_refclassid,
                                809                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                810                 :         104623 :                 ObjectIdGetDatum(object->classId));
                                811                 :         104623 :     ScanKeyInit(&key[1],
                                812                 :                :                 Anum_pg_depend_refobjid,
                                813                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                                814                 :         104623 :                 ObjectIdGetDatum(object->objectId));
 8457                           815         [ +  + ]:         104623 :     if (object->objectSubId != 0)
                                816                 :                :     {
 7969                           817                 :           1085 :         ScanKeyInit(&key[2],
                                818                 :                :                     Anum_pg_depend_refobjsubid,
                                819                 :                :                     BTEqualStrategyNumber, F_INT4EQ,
                                820                 :           1085 :                     Int32GetDatum(object->objectSubId));
 8457                           821                 :           1085 :         nkeys = 3;
                                822                 :                :     }
                                823                 :                :     else
                                824                 :         103538 :         nkeys = 2;
                                825                 :                : 
 4658                           826                 :         104623 :     scan = systable_beginscan(*depRel, DependReferenceIndexId, true,
                                827                 :                :                               NULL, nkeys, key);
                                828                 :                : 
 8457                           829         [ +  + ]:         215781 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                                830                 :                :     {
 8403 bruce@momjian.us          831                 :         111158 :         Form_pg_depend foundDep = (Form_pg_depend) GETSTRUCT(tup);
                                832                 :                :         int         subflags;
                                833                 :                : 
 8457 tgl@sss.pgh.pa.us         834                 :         111158 :         otherObject.classId = foundDep->classid;
                                835                 :         111158 :         otherObject.objectId = foundDep->objid;
                                836                 :         111158 :         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                 :                :          */
 2238                           843         [ +  + ]:         111158 :         if (otherObject.classId == object->classId &&
                                844         [ +  + ]:          46469 :             otherObject.objectId == object->objectId &&
                                845         [ +  - ]:           2154 :             object->objectSubId == 0)
                                846                 :           2154 :             continue;
                                847                 :                : 
                                848                 :                :         /*
                                849                 :                :          * Must lock the dependent object before recursing to it.
                                850                 :                :          */
 4901 simon@2ndQuadrant.co      851                 :         109004 :         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                 :                :          */
 6299 tgl@sss.pgh.pa.us         860         [ -  + ]:         109004 :         if (!systable_recheck_tuple(scan, tup))
                                861                 :                :         {
                                862                 :                :             /* release the now-useless lock */
 6299 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                 :                :          */
 8457 tgl@sss.pgh.pa.us         872   [ +  +  +  +  :CBC      109004 :         switch (foundDep->deptype)
                                              +  - ]
                                873                 :                :         {
                                874                 :          14161 :             case DEPENDENCY_NORMAL:
 6299                           875                 :          14161 :                 subflags = DEPFLAG_NORMAL;
 8457                           876                 :          14161 :                 break;
                                877                 :          35679 :             case DEPENDENCY_AUTO:
                                878                 :                :             case DEPENDENCY_AUTO_EXTENSION:
 6299                           879                 :          35679 :                 subflags = DEPFLAG_AUTO;
                                880                 :          35679 :                 break;
 8457                           881                 :          52491 :             case DEPENDENCY_INTERNAL:
 6299                           882                 :          52491 :                 subflags = DEPFLAG_INTERNAL;
 8457                           883                 :          52491 :                 break;
 2399                           884                 :           4525 :             case DEPENDENCY_PARTITION_PRI:
                                885                 :                :             case DEPENDENCY_PARTITION_SEC:
                                886                 :           4525 :                 subflags = DEPFLAG_PARTITION;
                                887                 :           4525 :                 break;
 5324                           888                 :           2148 :             case DEPENDENCY_EXTENSION:
                                889                 :           2148 :                 subflags = DEPFLAG_EXTENSION;
                                890                 :           2148 :                 break;
 8457 tgl@sss.pgh.pa.us         891                 :UBC           0 :             default:
 8083                           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 */
 2420 tgl@sss.pgh.pa.us         899         [ +  + ]:CBC      109004 :         if (numDependentObjects >= maxDependentObjects)
                                900                 :                :         {
                                901                 :                :             /* enlarge array if needed */
                                902                 :              9 :             maxDependentObjects *= 2;
                                903                 :                :             dependentObjects = (ObjectAddressAndFlags *)
                                904                 :              9 :                 repalloc(dependentObjects,
                                905                 :                :                          maxDependentObjects * sizeof(ObjectAddressAndFlags));
                                906                 :                :         }
                                907                 :                : 
                                908                 :         109004 :         dependentObjects[numDependentObjects].obj = otherObject;
                                909                 :         109004 :         dependentObjects[numDependentObjects].subflags = subflags;
                                910                 :         109004 :         numDependentObjects++;
                                911                 :                :     }
                                912                 :                : 
                                913                 :         104623 :     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         [ +  + ]:         104623 :     if (numDependentObjects > 1)
  942 peter@eisentraut.org      921                 :          22497 :         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                 :                :      */
 2420 tgl@sss.pgh.pa.us         929                 :         104623 :     mystack.object = object;    /* set up a new stack level */
                                930                 :         104623 :     mystack.flags = objflags;
                                931                 :         104623 :     mystack.next = stack;
                                932                 :                : 
                                933         [ +  + ]:         213627 :     for (int i = 0; i < numDependentObjects; i++)
                                934                 :                :     {
                                935                 :         109004 :         ObjectAddressAndFlags *depObj = dependentObjects + i;
                                936                 :                : 
                                937                 :         109004 :         findDependentObjects(&depObj->obj,
                                938                 :                :                              depObj->subflags,
                                939                 :                :                              flags,
                                940                 :                :                              &mystack,
                                941                 :                :                              targetObjects,
                                942                 :                :                              pendingObjects,
                                943                 :                :                              depRel);
                                944                 :                :     }
                                945                 :                : 
                                946                 :         104623 :     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                 :                :      */
 6299                           956                 :         104623 :     extra.flags = mystack.flags;
 2399                           957         [ +  + ]:         104623 :     if (extra.flags & DEPFLAG_IS_PART)
                                958                 :           2473 :         extra.dependee = partitionObject;
                                959         [ +  + ]:         102150 :     else if (stack)
 6299                           960                 :          83481 :         extra.dependee = *stack->object;
                                961                 :                :     else
                                962                 :          18669 :         memset(&extra.dependee, 0, sizeof(extra.dependee));
                                963                 :         104623 :     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                 :          16229 : reportDependentObjects(const ObjectAddresses *targetObjects,
                                981                 :                :                        DropBehavior behavior,
                                982                 :                :                        int flags,
                                983                 :                :                        const ObjectAddress *origObject)
                                984                 :                : {
 3200                           985         [ +  + ]:          16229 :     int         msglevel = (flags & PERFORM_DELETION_QUIETLY) ? DEBUG2 : NOTICE;
 6299                           986                 :          16229 :     bool        ok = true;
                                987                 :                :     StringInfoData clientdetail;
                                988                 :                :     StringInfoData logdetail;
 6296                           989                 :          16229 :     int         numReportedClient = 0;
                                990                 :          16229 :     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                 :                :      */
 2399                          1003         [ +  + ]:         120837 :     for (i = 0; i < targetObjects->numrefs; i++)
                               1004                 :                :     {
                               1005                 :         104623 :         const ObjectAddressExtra *extra = &targetObjects->extras[i];
                               1006                 :                : 
                               1007         [ +  + ]:         104623 :         if ((extra->flags & DEPFLAG_IS_PART) &&
                               1008         [ +  + ]:           2473 :             !(extra->flags & DEPFLAG_PARTITION))
                               1009                 :                :         {
                               1010                 :             15 :             const ObjectAddress *object = &targetObjects->refs[i];
 1879 michael@paquier.xyz      1011                 :             15 :             char       *otherObjDesc = getObjectDescription(&extra->dependee,
                               1012                 :                :                                                             false);
                               1013                 :                : 
 2399 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                 :                :      */
 6296                          1027         [ +  + ]:          16214 :     if (behavior == DROP_CASCADE &&
 1748                          1028         [ +  + ]:           1677 :         !message_level_is_interesting(msglevel))
 6296                          1029                 :            460 :         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                 :          15754 :     initStringInfo(&clientdetail);
                               1039                 :          15754 :     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                 :                :      */
 6299                          1045         [ +  + ]:         114225 :     for (i = targetObjects->numrefs - 1; i >= 0; i--)
                               1046                 :                :     {
                               1047                 :          98471 :         const ObjectAddress *obj = &targetObjects->refs[i];
                               1048                 :          98471 :         const ObjectAddressExtra *extra = &targetObjects->extras[i];
                               1049                 :                :         char       *objDesc;
                               1050                 :                : 
                               1051                 :                :         /* Ignore the original deletion target(s) */
                               1052         [ +  + ]:          98471 :         if (extra->flags & DEPFLAG_ORIGINAL)
                               1053                 :          18613 :             continue;
                               1054                 :                : 
                               1055                 :                :         /* Also ignore sub-objects; we'll report the whole object elsewhere */
 2423                          1056         [ -  + ]:          79858 :         if (extra->flags & DEPFLAG_SUBOBJECT)
 2423 tgl@sss.pgh.pa.us        1057                 :UBC           0 :             continue;
                               1058                 :                : 
 1879 michael@paquier.xyz      1059                 :CBC       79858 :         objDesc = getObjectDescription(obj, false);
                               1060                 :                : 
                               1061                 :                :         /* An object being dropped concurrently doesn't need to be reported */
 1401 alvherre@alvh.no-ip.     1062         [ -  + ]:          79858 :         if (objDesc == NULL)
 1401 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                 :                :          */
 5324 tgl@sss.pgh.pa.us        1070         [ +  + ]:CBC       79858 :         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                 :                :              */
 6299                          1081         [ +  + ]:          77372 :             ereport(DEBUG2,
                               1082                 :                :                     (errmsg_internal("drop auto-cascades to %s",
                               1083                 :                :                                      objDesc)));
                               1084                 :                :         }
                               1085         [ +  + ]:           2486 :         else if (behavior == DROP_RESTRICT)
                               1086                 :                :         {
 1879 michael@paquier.xyz      1087                 :            257 :             char       *otherDesc = getObjectDescription(&extra->dependee,
                               1088                 :                :                                                          false);
                               1089                 :                : 
 1401 alvherre@alvh.no-ip.     1090         [ +  - ]:            257 :             if (otherDesc)
                               1091                 :                :             {
                               1092         [ +  - ]:            257 :                 if (numReportedClient < MAX_REPORTED_DEPS)
                               1093                 :                :                 {
                               1094                 :                :                     /* separate entries with a newline */
                               1095         [ +  + ]:            257 :                     if (clientdetail.len != 0)
                               1096                 :            103 :                         appendStringInfoChar(&clientdetail, '\n');
                               1097                 :            257 :                     appendStringInfo(&clientdetail, _("%s depends on %s"),
                               1098                 :                :                                      objDesc, otherDesc);
                               1099                 :            257 :                     numReportedClient++;
                               1100                 :                :                 }
                               1101                 :                :                 else
 1401 alvherre@alvh.no-ip.     1102                 :UBC           0 :                     numNotReportedClient++;
                               1103                 :                :                 /* separate entries with a newline */
 1401 alvherre@alvh.no-ip.     1104         [ +  + ]:CBC         257 :                 if (logdetail.len != 0)
                               1105                 :            103 :                     appendStringInfoChar(&logdetail, '\n');
                               1106                 :            257 :                 appendStringInfo(&logdetail, _("%s depends on %s"),
                               1107                 :                :                                  objDesc, otherDesc);
                               1108                 :            257 :                 pfree(otherDesc);
                               1109                 :                :             }
                               1110                 :                :             else
 6296 tgl@sss.pgh.pa.us        1111                 :UBC           0 :                 numNotReportedClient++;
 6299 tgl@sss.pgh.pa.us        1112                 :CBC         257 :             ok = false;
                               1113                 :                :         }
                               1114                 :                :         else
                               1115                 :                :         {
 6296                          1116         [ +  - ]:           2229 :             if (numReportedClient < MAX_REPORTED_DEPS)
                               1117                 :                :             {
                               1118                 :                :                 /* separate entries with a newline */
                               1119         [ +  + ]:           2229 :                 if (clientdetail.len != 0)
                               1120                 :           1511 :                     appendStringInfoChar(&clientdetail, '\n');
                               1121                 :           2229 :                 appendStringInfo(&clientdetail, _("drop cascades to %s"),
                               1122                 :                :                                  objDesc);
                               1123                 :           2229 :                 numReportedClient++;
                               1124                 :                :             }
                               1125                 :                :             else
 6296 tgl@sss.pgh.pa.us        1126                 :UBC           0 :                 numNotReportedClient++;
                               1127                 :                :             /* separate entries with a newline */
 6296 tgl@sss.pgh.pa.us        1128         [ +  + ]:CBC        2229 :             if (logdetail.len != 0)
                               1129                 :           1511 :                 appendStringInfoChar(&logdetail, '\n');
                               1130                 :           2229 :             appendStringInfo(&logdetail, _("drop cascades to %s"),
                               1131                 :                :                              objDesc);
                               1132                 :                :         }
                               1133                 :                : 
                               1134                 :          79858 :         pfree(objDesc);
                               1135                 :                :     }
                               1136                 :                : 
                               1137         [ -  + ]:          15754 :     if (numNotReportedClient > 0)
 6008 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                 :                : 
 6299 tgl@sss.pgh.pa.us        1145         [ +  + ]:CBC       15754 :     if (!ok)
                               1146                 :                :     {
                               1147         [ +  + ]:            154 :         if (origObject)
                               1148         [ +  - ]:            151 :             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                 :                :     }
 6296                          1163         [ +  + ]:          15600 :     else if (numReportedClient > 1)
                               1164                 :                :     {
                               1165         [ +  - ]:            331 :         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         [ +  + ]:          15269 :     else if (numReportedClient == 1)
                               1174                 :                :     {
                               1175                 :                :         /* we just use the single item as-is */
                               1176         [ +  - ]:            387 :         ereport(msglevel,
                               1177                 :                :                 (errmsg_internal("%s", clientdetail.data)));
                               1178                 :                :     }
                               1179                 :                : 
                               1180                 :          15600 :     pfree(clientdetail.data);
                               1181                 :          15600 :     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
 1915 peter@eisentraut.org     1189                 :           3051 : DropObjectById(const ObjectAddress *object)
                               1190                 :                : {
                               1191                 :                :     int         cacheId;
                               1192                 :                :     Relation    rel;
                               1193                 :                :     HeapTuple   tup;
                               1194                 :                : 
                               1195                 :           3051 :     cacheId = get_object_catcache_oid(object->classId);
                               1196                 :                : 
                               1197                 :           3051 :     rel = table_open(object->classId, RowExclusiveLock);
                               1198                 :                : 
                               1199                 :                :     /*
                               1200                 :                :      * Use the system cache for the oid column, if one exists.
                               1201                 :                :      */
                               1202         [ +  + ]:           3051 :     if (cacheId >= 0)
                               1203                 :                :     {
                               1204                 :           1047 :         tup = SearchSysCache1(cacheId, ObjectIdGetDatum(object->objectId));
                               1205         [ -  + ]:           1047 :         if (!HeapTupleIsValid(tup))
 1915 peter@eisentraut.org     1206         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for %s %u",
                               1207                 :                :                  get_object_class_descr(object->classId), object->objectId);
                               1208                 :                : 
 1915 peter@eisentraut.org     1209                 :CBC        1047 :         CatalogTupleDelete(rel, &tup->t_self);
                               1210                 :                : 
                               1211                 :           1047 :         ReleaseSysCache(tup);
                               1212                 :                :     }
                               1213                 :                :     else
                               1214                 :                :     {
                               1215                 :                :         ScanKeyData skey[1];
                               1216                 :                :         SysScanDesc scan;
                               1217                 :                : 
                               1218                 :           2004 :         ScanKeyInit(&skey[0],
                               1219                 :           2004 :                     get_object_attnum_oid(object->classId),
                               1220                 :                :                     BTEqualStrategyNumber, F_OIDEQ,
                               1221                 :           2004 :                     ObjectIdGetDatum(object->objectId));
                               1222                 :                : 
                               1223                 :           2004 :         scan = systable_beginscan(rel, get_object_oid_index(object->classId), true,
                               1224                 :                :                                   NULL, 1, skey);
                               1225                 :                : 
                               1226                 :                :         /* we expect exactly one match */
                               1227                 :           2004 :         tup = systable_getnext(scan);
                               1228         [ -  + ]:           2004 :         if (!HeapTupleIsValid(tup))
 1915 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                 :                : 
 1915 peter@eisentraut.org     1232                 :CBC        2004 :         CatalogTupleDelete(rel, &tup->t_self);
                               1233                 :                : 
                               1234                 :           2004 :         systable_endscan(scan);
                               1235                 :                :     }
                               1236                 :                : 
                               1237                 :           3051 :     table_close(rel, RowExclusiveLock);
                               1238                 :           3051 : }
                               1239                 :                : 
                               1240                 :                : /*
                               1241                 :                :  * deleteOneObject: delete a single object for performDeletion.
                               1242                 :                :  *
                               1243                 :                :  * *depRel is the already-open pg_depend relation.
                               1244                 :                :  */
                               1245                 :                : static void
 4658 tgl@sss.pgh.pa.us        1246                 :         102091 : 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 */
 4567 rhaas@postgresql.org     1254         [ +  + ]:         102091 :     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                 :                :      */
 4705 simon@2ndQuadrant.co     1262         [ +  + ]:         102091 :     if (flags & PERFORM_DELETION_CONCURRENTLY)
 2420 andres@anarazel.de       1263                 :             54 :         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                 :                :      */
 4705 simon@2ndQuadrant.co     1274                 :         102091 :     doDeletion(object, flags);
                               1275                 :                : 
                               1276                 :                :     /*
                               1277                 :                :      * Reopen depRel if we closed it above
                               1278                 :                :      */
                               1279         [ +  + ]:         102086 :     if (flags & PERFORM_DELETION_CONCURRENTLY)
 2420 andres@anarazel.de       1280                 :             54 :         *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                 :                :      */
 6299 tgl@sss.pgh.pa.us        1289                 :         102086 :     ScanKeyInit(&key[0],
                               1290                 :                :                 Anum_pg_depend_classid,
                               1291                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                               1292                 :         102086 :                 ObjectIdGetDatum(object->classId));
                               1293                 :         102086 :     ScanKeyInit(&key[1],
                               1294                 :                :                 Anum_pg_depend_objid,
                               1295                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                               1296                 :         102086 :                 ObjectIdGetDatum(object->objectId));
                               1297         [ +  + ]:         102086 :     if (object->objectSubId != 0)
                               1298                 :                :     {
                               1299                 :           1037 :         ScanKeyInit(&key[2],
                               1300                 :                :                     Anum_pg_depend_objsubid,
                               1301                 :                :                     BTEqualStrategyNumber, F_INT4EQ,
                               1302                 :           1037 :                     Int32GetDatum(object->objectSubId));
                               1303                 :           1037 :         nkeys = 3;
                               1304                 :                :     }
                               1305                 :                :     else
                               1306                 :         101049 :         nkeys = 2;
                               1307                 :                : 
 4658                          1308                 :         102086 :     scan = systable_beginscan(*depRel, DependDependerIndexId, true,
                               1309                 :                :                               NULL, nkeys, key);
                               1310                 :                : 
 6299                          1311         [ +  + ]:         244324 :     while (HeapTupleIsValid(tup = systable_getnext(scan)))
                               1312                 :                :     {
 3139                          1313                 :         142238 :         CatalogTupleDelete(*depRel, &tup->t_self);
                               1314                 :                :     }
                               1315                 :                : 
 6299                          1316                 :         102086 :     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                 :                :      */
 6071                          1322                 :         102086 :     deleteSharedDependencyRecordsFor(object->classId, object->objectId,
                               1323                 :         102086 :                                      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.)  As above, all
                               1330                 :                :      * these functions must remove records for sub-objects too if the subid is
                               1331                 :                :      * zero.
                               1332                 :                :      */
 6299                          1333                 :         102086 :     DeleteComments(object->objectId, object->classId, object->objectSubId);
 5458 rhaas@postgresql.org     1334                 :         102086 :     DeleteSecurityLabel(object);
 3440 sfrost@snowman.net       1335                 :         102086 :     DeleteInitPrivs(object);
                               1336                 :                : 
                               1337                 :                :     /*
                               1338                 :                :      * CommandCounterIncrement here to ensure that preceding changes are all
                               1339                 :                :      * visible to the next deletion step.
                               1340                 :                :      */
 6299 tgl@sss.pgh.pa.us        1341                 :         102086 :     CommandCounterIncrement();
                               1342                 :                : 
                               1343                 :                :     /*
                               1344                 :                :      * And we're done!
                               1345                 :                :      */
                               1346                 :         102086 : }
                               1347                 :                : 
                               1348                 :                : /*
                               1349                 :                :  * doDeletion: actually delete a single object
                               1350                 :                :  */
                               1351                 :                : static void
 4901 simon@2ndQuadrant.co     1352                 :         102091 : doDeletion(const ObjectAddress *object, int flags)
                               1353                 :                : {
  529 peter@eisentraut.org     1354   [ +  +  +  +  :         102091 :     switch (object->classId)
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                        +  +  +  -  
                                                 - ]
                               1355                 :                :     {
                               1356                 :          35068 :         case RelationRelationId:
                               1357                 :                :             {
 8388 tgl@sss.pgh.pa.us        1358                 :          35068 :                 char        relKind = get_rel_relkind(object->objectId);
                               1359                 :                : 
 2787 alvherre@alvh.no-ip.     1360   [ +  +  +  + ]:          35068 :                 if (relKind == RELKIND_INDEX ||
                               1361                 :                :                     relKind == RELKIND_PARTITIONED_INDEX)
 8403 bruce@momjian.us         1362                 :          11891 :                 {
 3200 tgl@sss.pgh.pa.us        1363                 :          11891 :                     bool        concurrent = ((flags & PERFORM_DELETION_CONCURRENTLY) != 0);
 2353 peter@eisentraut.org     1364                 :          11891 :                     bool        concurrent_lock_mode = ((flags & PERFORM_DELETION_CONCURRENT_LOCK) != 0);
                               1365                 :                : 
 8403 bruce@momjian.us         1366         [ -  + ]:          11891 :                     Assert(object->objectSubId == 0);
 2353 peter@eisentraut.org     1367                 :          11891 :                     index_drop(object->objectId, concurrent, concurrent_lock_mode);
                               1368                 :                :                 }
                               1369                 :                :                 else
                               1370                 :                :                 {
 8403 bruce@momjian.us         1371         [ +  + ]:          23177 :                     if (object->objectSubId != 0)
                               1372                 :           1037 :                         RemoveAttributeById(object->objectId,
                               1373                 :           1037 :                                             object->objectSubId);
                               1374                 :                :                     else
                               1375                 :          22140 :                         heap_drop_with_catalog(object->objectId);
                               1376                 :                :                 }
                               1377                 :                : 
                               1378                 :                :                 /*
                               1379                 :                :                  * for a sequence, in addition to dropping the heap, also
                               1380                 :                :                  * delete pg_sequence tuple
                               1381                 :                :                  */
 3182 peter_e@gmx.net          1382         [ +  + ]:          35065 :                 if (relKind == RELKIND_SEQUENCE)
                               1383                 :            466 :                     DeleteSequenceTuple(object->objectId);
 8403 bruce@momjian.us         1384                 :          35065 :                 break;
                               1385                 :                :             }
                               1386                 :                : 
  529 peter@eisentraut.org     1387                 :           3859 :         case ProcedureRelationId:
 8457 tgl@sss.pgh.pa.us        1388                 :           3859 :             RemoveFunctionById(object->objectId);
                               1389                 :           3859 :             break;
                               1390                 :                : 
  529 peter@eisentraut.org     1391                 :          34419 :         case TypeRelationId:
 8457 tgl@sss.pgh.pa.us        1392                 :          34419 :             RemoveTypeById(object->objectId);
                               1393                 :          34419 :             break;
                               1394                 :                : 
  529 peter@eisentraut.org     1395                 :          13627 :         case ConstraintRelationId:
 8457 tgl@sss.pgh.pa.us        1396                 :          13627 :             RemoveConstraintById(object->objectId);
                               1397                 :          13626 :             break;
                               1398                 :                : 
  529 peter@eisentraut.org     1399                 :           1650 :         case AttrDefaultRelationId:
 8454 tgl@sss.pgh.pa.us        1400                 :           1650 :             RemoveAttrDefaultById(object->objectId);
                               1401                 :           1650 :             break;
                               1402                 :                : 
  529 peter@eisentraut.org     1403                 :             47 :         case LargeObjectRelationId:
 5748 itagaki.takahiro@gma     1404                 :             47 :             LargeObjectDrop(object->objectId);
                               1405                 :             47 :             break;
                               1406                 :                : 
  529 peter@eisentraut.org     1407                 :            394 :         case OperatorRelationId:
 8457 tgl@sss.pgh.pa.us        1408                 :            394 :             RemoveOperatorById(object->objectId);
                               1409                 :            394 :             break;
                               1410                 :                : 
  529 peter@eisentraut.org     1411                 :           1455 :         case RewriteRelationId:
 8457 tgl@sss.pgh.pa.us        1412                 :           1455 :             RemoveRewriteRuleById(object->objectId);
                               1413                 :           1454 :             break;
                               1414                 :                : 
  529 peter@eisentraut.org     1415                 :           7104 :         case TriggerRelationId:
 8457 tgl@sss.pgh.pa.us        1416                 :           7104 :             RemoveTriggerById(object->objectId);
                               1417                 :           7104 :             break;
                               1418                 :                : 
  529 peter@eisentraut.org     1419                 :            272 :         case StatisticExtRelationId:
 3037 tgl@sss.pgh.pa.us        1420                 :            272 :             RemoveStatisticsById(object->objectId);
                               1421                 :            272 :             break;
                               1422                 :                : 
  529 peter@eisentraut.org     1423                 :             24 :         case TSConfigRelationId:
 6591 tgl@sss.pgh.pa.us        1424                 :             24 :             RemoveTSConfigurationById(object->objectId);
                               1425                 :             24 :             break;
                               1426                 :                : 
  529 peter@eisentraut.org     1427                 :             59 :         case ExtensionRelationId:
 5324 tgl@sss.pgh.pa.us        1428                 :             59 :             RemoveExtensionById(object->objectId);
                               1429                 :             59 :             break;
                               1430                 :                : 
  529 peter@eisentraut.org     1431                 :            303 :         case PolicyRelationId:
 4005 sfrost@snowman.net       1432                 :            303 :             RemovePolicyById(object->objectId);
                               1433                 :            303 :             break;
                               1434                 :                : 
  529 peter@eisentraut.org     1435                 :             96 :         case PublicationNamespaceRelationId:
 1410 akapila@postgresql.o     1436                 :             96 :             RemovePublicationSchemaById(object->objectId);
                               1437                 :             96 :             break;
                               1438                 :                : 
  529 peter@eisentraut.org     1439                 :            422 :         case PublicationRelRelationId:
 3152 peter_e@gmx.net          1440                 :            422 :             RemovePublicationRelById(object->objectId);
                               1441                 :            422 :             break;
                               1442                 :                : 
  529 peter@eisentraut.org     1443                 :            241 :         case PublicationRelationId:
 1459 akapila@postgresql.o     1444                 :            241 :             RemovePublicationById(object->objectId);
                               1445                 :            241 :             break;
                               1446                 :                : 
  529 peter@eisentraut.org     1447                 :           3051 :         case CastRelationId:
                               1448                 :                :         case CollationRelationId:
                               1449                 :                :         case ConversionRelationId:
                               1450                 :                :         case LanguageRelationId:
                               1451                 :                :         case OperatorClassRelationId:
                               1452                 :                :         case OperatorFamilyRelationId:
                               1453                 :                :         case AccessMethodRelationId:
                               1454                 :                :         case AccessMethodOperatorRelationId:
                               1455                 :                :         case AccessMethodProcedureRelationId:
                               1456                 :                :         case NamespaceRelationId:
                               1457                 :                :         case TSParserRelationId:
                               1458                 :                :         case TSDictionaryRelationId:
                               1459                 :                :         case TSTemplateRelationId:
                               1460                 :                :         case ForeignDataWrapperRelationId:
                               1461                 :                :         case ForeignServerRelationId:
                               1462                 :                :         case UserMappingRelationId:
                               1463                 :                :         case DefaultAclRelationId:
                               1464                 :                :         case EventTriggerRelationId:
                               1465                 :                :         case TransformRelationId:
                               1466                 :                :         case AuthMemRelationId:
 1915                          1467                 :           3051 :             DropObjectById(object);
 3786 peter_e@gmx.net          1468                 :           3051 :             break;
                               1469                 :                : 
                               1470                 :                :             /*
                               1471                 :                :              * These global object types are not supported here.
                               1472                 :                :              */
  529 peter@eisentraut.org     1473                 :UBC           0 :         case AuthIdRelationId:
                               1474                 :                :         case DatabaseRelationId:
                               1475                 :                :         case TableSpaceRelationId:
                               1476                 :                :         case SubscriptionRelationId:
                               1477                 :                :         case ParameterAclRelationId:
 3037 tgl@sss.pgh.pa.us        1478         [ #  # ]:              0 :             elog(ERROR, "global objects cannot be deleted by doDeletion");
                               1479                 :                :             break;
                               1480                 :                : 
  529 peter@eisentraut.org     1481                 :              0 :         default:
                               1482         [ #  # ]:              0 :             elog(ERROR, "unsupported object class: %u", object->classId);
                               1483                 :                :     }
 8457 tgl@sss.pgh.pa.us        1484                 :CBC      102086 : }
                               1485                 :                : 
                               1486                 :                : /*
                               1487                 :                :  * AcquireDeletionLock - acquire a suitable lock for deleting an object
                               1488                 :                :  *
                               1489                 :                :  * Accepts the same flags as performDeletion (though currently only
                               1490                 :                :  * PERFORM_DELETION_CONCURRENTLY does anything).
                               1491                 :                :  *
                               1492                 :                :  * We use LockRelation for relations, and otherwise LockSharedObject or
                               1493                 :                :  * LockDatabaseObject as appropriate for the object type.
                               1494                 :                :  */
                               1495                 :                : void
 4901 simon@2ndQuadrant.co     1496                 :         129178 : AcquireDeletionLock(const ObjectAddress *object, int flags)
                               1497                 :                : {
 6299 tgl@sss.pgh.pa.us        1498         [ +  + ]:         129178 :     if (object->classId == RelationRelationId)
                               1499                 :                :     {
                               1500                 :                :         /*
                               1501                 :                :          * In DROP INDEX CONCURRENTLY, take only ShareUpdateExclusiveLock on
                               1502                 :                :          * the index for the moment.  index_drop() will promote the lock once
                               1503                 :                :          * it's safe to do so.  In all other cases we need full exclusive
                               1504                 :                :          * lock.
                               1505                 :                :          */
 4705 simon@2ndQuadrant.co     1506         [ +  + ]:          44631 :         if (flags & PERFORM_DELETION_CONCURRENTLY)
 4901                          1507                 :             54 :             LockRelationOid(object->objectId, ShareUpdateExclusiveLock);
                               1508                 :                :         else
                               1509                 :          44577 :             LockRelationOid(object->objectId, AccessExclusiveLock);
                               1510                 :                :     }
 1115 rhaas@postgresql.org     1511         [ +  + ]:          84547 :     else if (object->classId == AuthMemRelationId)
                               1512                 :             42 :         LockSharedObject(object->classId, object->objectId, 0,
                               1513                 :                :                          AccessExclusiveLock);
                               1514                 :                :     else
                               1515                 :                :     {
                               1516                 :                :         /* assume we should lock the whole object not a sub-object */
 6299 tgl@sss.pgh.pa.us        1517                 :          84505 :         LockDatabaseObject(object->classId, object->objectId, 0,
                               1518                 :                :                            AccessExclusiveLock);
                               1519                 :                :     }
                               1520                 :         129178 : }
                               1521                 :                : 
                               1522                 :                : /*
                               1523                 :                :  * ReleaseDeletionLock - release an object deletion lock
                               1524                 :                :  *
                               1525                 :                :  * Companion to AcquireDeletionLock.
                               1526                 :                :  */
                               1527                 :                : void
                               1528                 :            857 : ReleaseDeletionLock(const ObjectAddress *object)
                               1529                 :                : {
                               1530         [ +  + ]:            857 :     if (object->classId == RelationRelationId)
                               1531                 :             28 :         UnlockRelationOid(object->objectId, AccessExclusiveLock);
                               1532                 :                :     else
                               1533                 :                :         /* assume we should lock the whole object not a sub-object */
                               1534                 :            829 :         UnlockDatabaseObject(object->classId, object->objectId, 0,
                               1535                 :                :                              AccessExclusiveLock);
                               1536                 :            857 : }
                               1537                 :                : 
                               1538                 :                : /*
                               1539                 :                :  * recordDependencyOnExpr - find expression dependencies
                               1540                 :                :  *
                               1541                 :                :  * This is used to find the dependencies of rules, constraint expressions,
                               1542                 :                :  * etc.
                               1543                 :                :  *
                               1544                 :                :  * Given an expression or query in node-tree form, find all the objects
                               1545                 :                :  * it refers to (tables, columns, operators, functions, etc).  Record
                               1546                 :                :  * a dependency of the specified type from the given depender object
                               1547                 :                :  * to each object mentioned in the expression.
                               1548                 :                :  *
                               1549                 :                :  * rtable is the rangetable to be used to interpret Vars with varlevelsup=0.
                               1550                 :                :  * It can be NIL if no such variables are expected.
                               1551                 :                :  */
                               1552                 :                : void
 8453                          1553                 :          14960 : recordDependencyOnExpr(const ObjectAddress *depender,
                               1554                 :                :                        Node *expr, List *rtable,
                               1555                 :                :                        DependencyType behavior)
                               1556                 :                : {
                               1557                 :                :     find_expr_references_context context;
                               1558                 :                : 
 6957 alvherre@alvh.no-ip.     1559                 :          14960 :     context.addrs = new_object_addresses();
                               1560                 :                : 
                               1561                 :                :     /* Set up interpretation for Vars at varlevelsup = 0 */
 7773 neilc@samurai.com        1562                 :          14960 :     context.rtables = list_make1(rtable);
                               1563                 :                : 
                               1564                 :                :     /* Scan the expression tree for referenceable objects */
 8453 tgl@sss.pgh.pa.us        1565                 :          14960 :     find_expr_references_walker(expr, &context);
                               1566                 :                : 
                               1567                 :                :     /* Remove any duplicates */
 6957 alvherre@alvh.no-ip.     1568                 :          14957 :     eliminate_duplicate_dependencies(context.addrs);
                               1569                 :                : 
                               1570                 :                :     /* And record 'em */
 8137 tgl@sss.pgh.pa.us        1571                 :          14957 :     recordMultipleDependencies(depender,
 1583 tmunro@postgresql.or     1572                 :          14957 :                                context.addrs->refs, context.addrs->numrefs,
                               1573                 :                :                                behavior);
                               1574                 :                : 
 6957 alvherre@alvh.no-ip.     1575                 :          14957 :     free_object_addresses(context.addrs);
 8137 tgl@sss.pgh.pa.us        1576                 :          14957 : }
                               1577                 :                : 
                               1578                 :                : /*
                               1579                 :                :  * recordDependencyOnSingleRelExpr - find expression dependencies
                               1580                 :                :  *
                               1581                 :                :  * As above, but only one relation is expected to be referenced (with
                               1582                 :                :  * varno = 1 and varlevelsup = 0).  Pass the relation OID instead of a
                               1583                 :                :  * range table.  An additional frammish is that dependencies on that
                               1584                 :                :  * relation's component columns will be marked with 'self_behavior',
                               1585                 :                :  * whereas 'behavior' is used for everything else; also, if 'reverse_self'
                               1586                 :                :  * is true, those dependencies are reversed so that the columns are made
                               1587                 :                :  * to depend on the table not vice versa.
                               1588                 :                :  *
                               1589                 :                :  * NOTE: the caller should ensure that a whole-table dependency on the
                               1590                 :                :  * specified relation is created separately, if one is needed.  In particular,
                               1591                 :                :  * a whole-row Var "relation.*" will not cause this routine to emit any
                               1592                 :                :  * dependency item.  This is appropriate behavior for subexpressions of an
                               1593                 :                :  * ordinary query, so other cases need to cope as necessary.
                               1594                 :                :  */
                               1595                 :                : void
                               1596                 :           5161 : recordDependencyOnSingleRelExpr(const ObjectAddress *depender,
                               1597                 :                :                                 Node *expr, Oid relId,
                               1598                 :                :                                 DependencyType behavior,
                               1599                 :                :                                 DependencyType self_behavior,
                               1600                 :                :                                 bool reverse_self)
                               1601                 :                : {
                               1602                 :                :     find_expr_references_context context;
 1148 peter@eisentraut.org     1603                 :           5161 :     RangeTblEntry rte = {0};
                               1604                 :                : 
 6957 alvherre@alvh.no-ip.     1605                 :           5161 :     context.addrs = new_object_addresses();
                               1606                 :                : 
                               1607                 :                :     /* We gin up a rather bogus rangetable list to handle Vars */
 8137 tgl@sss.pgh.pa.us        1608                 :           5161 :     rte.type = T_RangeTblEntry;
                               1609                 :           5161 :     rte.rtekind = RTE_RELATION;
                               1610                 :           5161 :     rte.relid = relId;
 2999                          1611                 :           5161 :     rte.relkind = RELKIND_RELATION; /* no need for exactness here */
 2533                          1612                 :           5161 :     rte.rellockmode = AccessShareLock;
                               1613                 :                : 
 7773 neilc@samurai.com        1614                 :           5161 :     context.rtables = list_make1(list_make1(&rte));
                               1615                 :                : 
                               1616                 :                :     /* Scan the expression tree for referenceable objects */
 8137 tgl@sss.pgh.pa.us        1617                 :           5161 :     find_expr_references_walker(expr, &context);
                               1618                 :                : 
                               1619                 :                :     /* Remove any duplicates */
 6957 alvherre@alvh.no-ip.     1620                 :           5155 :     eliminate_duplicate_dependencies(context.addrs);
                               1621                 :                : 
                               1622                 :                :     /* Separate self-dependencies if necessary */
 2238 tgl@sss.pgh.pa.us        1623   [ +  +  -  + ]:           5155 :     if ((behavior != self_behavior || reverse_self) &&
                               1624         [ +  + ]:            903 :         context.addrs->numrefs > 0)
                               1625                 :                :     {
                               1626                 :                :         ObjectAddresses *self_addrs;
                               1627                 :                :         ObjectAddress *outobj;
                               1628                 :                :         int         oldref,
                               1629                 :                :                     outrefs;
                               1630                 :                : 
 6957 alvherre@alvh.no-ip.     1631                 :            900 :         self_addrs = new_object_addresses();
                               1632                 :                : 
                               1633                 :            900 :         outobj = context.addrs->refs;
 8137 tgl@sss.pgh.pa.us        1634                 :            900 :         outrefs = 0;
 6957 alvherre@alvh.no-ip.     1635         [ +  + ]:           3703 :         for (oldref = 0; oldref < context.addrs->numrefs; oldref++)
                               1636                 :                :         {
                               1637                 :           2803 :             ObjectAddress *thisobj = context.addrs->refs + oldref;
                               1638                 :                : 
 7450 tgl@sss.pgh.pa.us        1639         [ +  + ]:           2803 :             if (thisobj->classId == RelationRelationId &&
 8137                          1640         [ +  + ]:           1148 :                 thisobj->objectId == relId)
                               1641                 :                :             {
                               1642                 :                :                 /* Move this ref into self_addrs */
 6299                          1643                 :           1112 :                 add_exact_object_address(thisobj, self_addrs);
                               1644                 :                :             }
                               1645                 :                :             else
                               1646                 :                :             {
                               1647                 :                :                 /* Keep it in context.addrs */
                               1648                 :           1691 :                 *outobj = *thisobj;
 8137                          1649                 :           1691 :                 outobj++;
                               1650                 :           1691 :                 outrefs++;
                               1651                 :                :             }
                               1652                 :                :         }
 6957 alvherre@alvh.no-ip.     1653                 :            900 :         context.addrs->numrefs = outrefs;
                               1654                 :                : 
                               1655                 :                :         /* Record the self-dependencies with the appropriate direction */
 2238 tgl@sss.pgh.pa.us        1656         [ +  + ]:            900 :         if (!reverse_self)
 3195 rhaas@postgresql.org     1657                 :            796 :             recordMultipleDependencies(depender,
 1583 tmunro@postgresql.or     1658                 :            796 :                                        self_addrs->refs, self_addrs->numrefs,
                               1659                 :                :                                        self_behavior);
                               1660                 :                :         else
                               1661                 :                :         {
                               1662                 :                :             /* Can't use recordMultipleDependencies, so do it the hard way */
                               1663                 :                :             int         selfref;
                               1664                 :                : 
 2238 tgl@sss.pgh.pa.us        1665         [ +  + ]:            249 :             for (selfref = 0; selfref < self_addrs->numrefs; selfref++)
                               1666                 :                :             {
                               1667                 :            145 :                 ObjectAddress *thisobj = self_addrs->refs + selfref;
                               1668                 :                : 
                               1669                 :            145 :                 recordDependencyOn(thisobj, depender, self_behavior);
                               1670                 :                :             }
                               1671                 :                :         }
                               1672                 :                : 
 6957 alvherre@alvh.no-ip.     1673                 :            900 :         free_object_addresses(self_addrs);
                               1674                 :                :     }
                               1675                 :                : 
                               1676                 :                :     /* Record the external dependencies */
 8453 tgl@sss.pgh.pa.us        1677                 :           5155 :     recordMultipleDependencies(depender,
 1583 tmunro@postgresql.or     1678                 :           5155 :                                context.addrs->refs, context.addrs->numrefs,
                               1679                 :                :                                behavior);
                               1680                 :                : 
 6957 alvherre@alvh.no-ip.     1681                 :           5155 :     free_object_addresses(context.addrs);
 8453 tgl@sss.pgh.pa.us        1682                 :           5155 : }
                               1683                 :                : 
                               1684                 :                : /*
                               1685                 :                :  * Recursively search an expression tree for object references.
                               1686                 :                :  *
                               1687                 :                :  * Note: in many cases we do not need to create dependencies on the datatypes
                               1688                 :                :  * involved in an expression, because we'll have an indirect dependency via
                               1689                 :                :  * some other object.  For instance Var nodes depend on a column which depends
                               1690                 :                :  * on the datatype, and OpExpr nodes depend on the operator which depends on
                               1691                 :                :  * the datatype.  However we do need a type dependency if there is no such
                               1692                 :                :  * indirect dependency, as for example in Const and CoerceToDomain nodes.
                               1693                 :                :  *
                               1694                 :                :  * Similarly, we don't need to create dependencies on collations except where
                               1695                 :                :  * the collation is being freshly introduced to the expression.
                               1696                 :                :  */
                               1697                 :                : static bool
                               1698                 :        1011976 : find_expr_references_walker(Node *node,
                               1699                 :                :                             find_expr_references_context *context)
                               1700                 :                : {
                               1701         [ +  + ]:        1011976 :     if (node == NULL)
                               1702                 :         348019 :         return false;
                               1703         [ +  + ]:         663957 :     if (IsA(node, Var))
                               1704                 :                :     {
                               1705                 :         163194 :         Var        *var = (Var *) node;
                               1706                 :                :         List       *rtable;
                               1707                 :                :         RangeTblEntry *rte;
                               1708                 :                : 
                               1709                 :                :         /* Find matching rtable entry, or complain if not found */
 7773 neilc@samurai.com        1710         [ -  + ]:         163194 :         if (var->varlevelsup >= list_length(context->rtables))
 8083 tgl@sss.pgh.pa.us        1711         [ #  # ]:UBC           0 :             elog(ERROR, "invalid varlevelsup %d", var->varlevelsup);
 7773 neilc@samurai.com        1712                 :CBC      163194 :         rtable = (List *) list_nth(context->rtables, var->varlevelsup);
                               1713   [ +  -  -  + ]:         163194 :         if (var->varno <= 0 || var->varno > list_length(rtable))
 8083 tgl@sss.pgh.pa.us        1714         [ #  # ]:UBC           0 :             elog(ERROR, "invalid varno %d", var->varno);
 8453 tgl@sss.pgh.pa.us        1715                 :CBC      163194 :         rte = rt_fetch(var->varno, rtable);
                               1716                 :                : 
                               1717                 :                :         /*
                               1718                 :                :          * A whole-row Var references no specific columns, so adds no new
                               1719                 :                :          * dependency.  (We assume that there is a whole-table dependency
                               1720                 :                :          * arising from each underlying rangetable entry.  While we could
                               1721                 :                :          * record such a dependency when finding a whole-row Var that
                               1722                 :                :          * references a relation directly, it's quite unclear how to extend
                               1723                 :                :          * that to whole-row Vars for JOINs, so it seems better to leave the
                               1724                 :                :          * responsibility with the range table.  Note that this poses some
                               1725                 :                :          * risks for identifying dependencies of stand-alone expressions:
                               1726                 :                :          * whole-table references may need to be created separately.)
                               1727                 :                :          */
 7688                          1728         [ +  + ]:         163194 :         if (var->varattno == InvalidAttrNumber)
                               1729                 :           2799 :             return false;
 8453                          1730         [ +  + ]:         160395 :         if (rte->rtekind == RTE_RELATION)
                               1731                 :                :         {
                               1732                 :                :             /* If it's a plain relation, reference this column */
  557 michael@paquier.xyz      1733                 :         115663 :             add_object_address(RelationRelationId, rte->relid, var->varattno,
                               1734                 :                :                                context->addrs);
                               1735                 :                :         }
 1142 tgl@sss.pgh.pa.us        1736         [ +  + ]:          44732 :         else if (rte->rtekind == RTE_FUNCTION)
                               1737                 :                :         {
                               1738                 :                :             /* Might need to add a dependency on a composite type's column */
                               1739                 :                :             /* (done out of line, because it's a bit bulky) */
                               1740                 :          23062 :             process_function_rte_ref(rte, var->varattno, context);
                               1741                 :                :         }
                               1742                 :                : 
                               1743                 :                :         /*
                               1744                 :                :          * Vars referencing other RTE types require no additional work.  In
                               1745                 :                :          * particular, a join alias Var can be ignored, because it must
                               1746                 :                :          * reference a merged USING column.  The relevant join input columns
                               1747                 :                :          * will also be referenced in the join qual, and any type coercion
                               1748                 :                :          * functions involved in the alias expression will be dealt with when
                               1749                 :                :          * we scan the RTE itself.
                               1750                 :                :          */
 8453                          1751                 :         160395 :         return false;
                               1752                 :                :     }
 6244                          1753         [ +  + ]:         500763 :     else if (IsA(node, Const))
                               1754                 :                :     {
 7279                          1755                 :          82028 :         Const      *con = (Const *) node;
                               1756                 :                :         Oid         objoid;
                               1757                 :                : 
                               1758                 :                :         /* A constant must depend on the constant's datatype */
  557 michael@paquier.xyz      1759                 :          82028 :         add_object_address(TypeRelationId, con->consttype, 0,
                               1760                 :                :                            context->addrs);
                               1761                 :                : 
                               1762                 :                :         /*
                               1763                 :                :          * We must also depend on the constant's collation: it could be
                               1764                 :                :          * different from the datatype's, if a CollateExpr was const-folded to
                               1765                 :                :          * a simple constant.  However we can save work in the most common
                               1766                 :                :          * case where the collation is "default", since we know that's pinned.
                               1767                 :                :          */
 1583 tmunro@postgresql.or     1768         [ +  + ]:          82028 :         if (OidIsValid(con->constcollid) &&
                               1769         [ +  + ]:          31892 :             con->constcollid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      1770                 :           7873 :             add_object_address(CollationRelationId, con->constcollid, 0,
                               1771                 :                :                                context->addrs);
                               1772                 :                : 
                               1773                 :                :         /*
                               1774                 :                :          * If it's a regclass or similar literal referring to an existing
                               1775                 :                :          * object, add a reference to that object.  (Currently, only the
                               1776                 :                :          * regclass and regconfig cases have any likely use, but we may as
                               1777                 :                :          * well handle all the OID-alias datatypes consistently.)
                               1778                 :                :          */
 7279 tgl@sss.pgh.pa.us        1779         [ +  + ]:          82028 :         if (!con->constisnull)
                               1780                 :                :         {
                               1781   [ -  -  +  -  :          69545 :             switch (con->consttype)
                                     -  -  -  +  +  
                                              +  + ]
                               1782                 :                :             {
 7279 tgl@sss.pgh.pa.us        1783                 :UBC           0 :                 case REGPROCOID:
                               1784                 :                :                 case REGPROCEDUREOID:
                               1785                 :              0 :                     objoid = DatumGetObjectId(con->constvalue);
 5683 rhaas@postgresql.org     1786         [ #  # ]:              0 :                     if (SearchSysCacheExists1(PROCOID,
                               1787                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1788                 :              0 :                         add_object_address(ProcedureRelationId, objoid, 0,
                               1789                 :                :                                            context->addrs);
 7279 tgl@sss.pgh.pa.us        1790                 :              0 :                     break;
                               1791                 :              0 :                 case REGOPEROID:
                               1792                 :                :                 case REGOPERATOROID:
                               1793                 :              0 :                     objoid = DatumGetObjectId(con->constvalue);
 5683 rhaas@postgresql.org     1794         [ #  # ]:              0 :                     if (SearchSysCacheExists1(OPEROID,
                               1795                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1796                 :              0 :                         add_object_address(OperatorRelationId, objoid, 0,
                               1797                 :                :                                            context->addrs);
 7279 tgl@sss.pgh.pa.us        1798                 :              0 :                     break;
 7279 tgl@sss.pgh.pa.us        1799                 :CBC        2194 :                 case REGCLASSOID:
                               1800                 :           2194 :                     objoid = DatumGetObjectId(con->constvalue);
 5683 rhaas@postgresql.org     1801         [ +  - ]:           2194 :                     if (SearchSysCacheExists1(RELOID,
                               1802                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1803                 :           2194 :                         add_object_address(RelationRelationId, objoid, 0,
                               1804                 :                :                                            context->addrs);
 7279 tgl@sss.pgh.pa.us        1805                 :           2194 :                     break;
 7279 tgl@sss.pgh.pa.us        1806                 :UBC           0 :                 case REGTYPEOID:
                               1807                 :              0 :                     objoid = DatumGetObjectId(con->constvalue);
 5683 rhaas@postgresql.org     1808         [ #  # ]:              0 :                     if (SearchSysCacheExists1(TYPEOID,
                               1809                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1810                 :              0 :                         add_object_address(TypeRelationId, objoid, 0,
                               1811                 :                :                                            context->addrs);
 7279 tgl@sss.pgh.pa.us        1812                 :              0 :                     break;
 1147                          1813                 :              0 :                 case REGCOLLATIONOID:
                               1814                 :              0 :                     objoid = DatumGetObjectId(con->constvalue);
                               1815         [ #  # ]:              0 :                     if (SearchSysCacheExists1(COLLOID,
                               1816                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1817                 :              0 :                         add_object_address(CollationRelationId, objoid, 0,
                               1818                 :                :                                            context->addrs);
 1147 tgl@sss.pgh.pa.us        1819                 :              0 :                     break;
 6591                          1820                 :              0 :                 case REGCONFIGOID:
                               1821                 :              0 :                     objoid = DatumGetObjectId(con->constvalue);
 5683 rhaas@postgresql.org     1822         [ #  # ]:              0 :                     if (SearchSysCacheExists1(TSCONFIGOID,
                               1823                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1824                 :              0 :                         add_object_address(TSConfigRelationId, objoid, 0,
                               1825                 :                :                                            context->addrs);
 6591 tgl@sss.pgh.pa.us        1826                 :              0 :                     break;
                               1827                 :              0 :                 case REGDICTIONARYOID:
                               1828                 :              0 :                     objoid = DatumGetObjectId(con->constvalue);
 5683 rhaas@postgresql.org     1829         [ #  # ]:              0 :                     if (SearchSysCacheExists1(TSDICTOID,
                               1830                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1831                 :              0 :                         add_object_address(TSDictionaryRelationId, objoid, 0,
                               1832                 :                :                                            context->addrs);
 6591 tgl@sss.pgh.pa.us        1833                 :              0 :                     break;
                               1834                 :                : 
 3773 andrew@dunslane.net      1835                 :CBC          96 :                 case REGNAMESPACEOID:
                               1836                 :             96 :                     objoid = DatumGetObjectId(con->constvalue);
                               1837         [ +  - ]:             96 :                     if (SearchSysCacheExists1(NAMESPACEOID,
                               1838                 :                :                                               ObjectIdGetDatum(objoid)))
  557 michael@paquier.xyz      1839                 :             96 :                         add_object_address(NamespaceRelationId, objoid, 0,
                               1840                 :                :                                            context->addrs);
 3773 andrew@dunslane.net      1841                 :             96 :                     break;
                               1842                 :                : 
                               1843                 :                :                     /*
                               1844                 :                :                      * Dependencies for regrole should be shared among all
                               1845                 :                :                      * databases, so explicitly inhibit to have dependencies.
                               1846                 :                :                      */
 3773 andrew@dunslane.net      1847                 :GBC           3 :                 case REGROLEOID:
                               1848         [ +  - ]:              3 :                     ereport(ERROR,
                               1849                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1850                 :                :                              errmsg("constant of the type %s cannot be used here",
                               1851                 :                :                                     "regrole")));
                               1852                 :                :                     break;
                               1853                 :                : 
                               1854                 :                :                     /*
                               1855                 :                :                      * Dependencies for regdatabase should be shared among all
                               1856                 :                :                      * databases, so explicitly inhibit to have dependencies.
                               1857                 :                :                      */
   68 nathan@postgresql.or     1858                 :GNC           3 :                 case REGDATABASEOID:
                               1859         [ +  - ]:              3 :                     ereport(ERROR,
                               1860                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1861                 :                :                              errmsg("constant of the type %s cannot be used here",
                               1862                 :                :                                     "regdatabase")));
                               1863                 :                :                     break;
                               1864                 :                :             }
                               1865                 :                :         }
 7279 tgl@sss.pgh.pa.us        1866                 :CBC       82022 :         return false;
                               1867                 :                :     }
 6244                          1868         [ +  + ]:         418735 :     else if (IsA(node, Param))
                               1869                 :                :     {
 7114                          1870                 :           8089 :         Param      *param = (Param *) node;
                               1871                 :                : 
                               1872                 :                :         /* A parameter must depend on the parameter's datatype */
  557 michael@paquier.xyz      1873                 :           8089 :         add_object_address(TypeRelationId, param->paramtype, 0,
                               1874                 :                :                            context->addrs);
                               1875                 :                :         /* and its collation, just as for Consts */
 1583 tmunro@postgresql.or     1876         [ +  + ]:           8089 :         if (OidIsValid(param->paramcollid) &&
                               1877         [ +  + ]:           1426 :             param->paramcollid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      1878                 :            768 :             add_object_address(CollationRelationId, param->paramcollid, 0,
                               1879                 :                :                                context->addrs);
                               1880                 :                :     }
 6244 tgl@sss.pgh.pa.us        1881         [ +  + ]:         410646 :     else if (IsA(node, FuncExpr))
                               1882                 :                :     {
 8304                          1883                 :          40500 :         FuncExpr   *funcexpr = (FuncExpr *) node;
                               1884                 :                : 
  557 michael@paquier.xyz      1885                 :          40500 :         add_object_address(ProcedureRelationId, funcexpr->funcid, 0,
                               1886                 :                :                            context->addrs);
                               1887                 :                :         /* fall through to examine arguments */
                               1888                 :                :     }
 6244 tgl@sss.pgh.pa.us        1889         [ +  + ]:         370146 :     else if (IsA(node, OpExpr))
                               1890                 :                :     {
 8069 bruce@momjian.us         1891                 :          46456 :         OpExpr     *opexpr = (OpExpr *) node;
                               1892                 :                : 
  557 michael@paquier.xyz      1893                 :          46456 :         add_object_address(OperatorRelationId, opexpr->opno, 0,
                               1894                 :                :                            context->addrs);
                               1895                 :                :         /* fall through to examine arguments */
                               1896                 :                :     }
 6244 tgl@sss.pgh.pa.us        1897         [ +  + ]:         323690 :     else if (IsA(node, DistinctExpr))
                               1898                 :                :     {
 8069 bruce@momjian.us         1899                 :              6 :         DistinctExpr *distinctexpr = (DistinctExpr *) node;
                               1900                 :                : 
  557 michael@paquier.xyz      1901                 :              6 :         add_object_address(OperatorRelationId, distinctexpr->opno, 0,
                               1902                 :                :                            context->addrs);
                               1903                 :                :         /* fall through to examine arguments */
                               1904                 :                :     }
 5285 tgl@sss.pgh.pa.us        1905         [ +  + ]:         323684 :     else if (IsA(node, NullIfExpr))
                               1906                 :                :     {
                               1907                 :            181 :         NullIfExpr *nullifexpr = (NullIfExpr *) node;
                               1908                 :                : 
  557 michael@paquier.xyz      1909                 :            181 :         add_object_address(OperatorRelationId, nullifexpr->opno, 0,
                               1910                 :                :                            context->addrs);
                               1911                 :                :         /* fall through to examine arguments */
                               1912                 :                :     }
 5285 tgl@sss.pgh.pa.us        1913         [ +  + ]:         323503 :     else if (IsA(node, ScalarArrayOpExpr))
                               1914                 :                :     {
                               1915                 :           3354 :         ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
                               1916                 :                : 
  557 michael@paquier.xyz      1917                 :           3354 :         add_object_address(OperatorRelationId, opexpr->opno, 0,
                               1918                 :                :                            context->addrs);
                               1919                 :                :         /* fall through to examine arguments */
                               1920                 :                :     }
 6244 tgl@sss.pgh.pa.us        1921         [ +  + ]:         320149 :     else if (IsA(node, Aggref))
                               1922                 :                :     {
 8453                          1923                 :            904 :         Aggref     *aggref = (Aggref *) node;
                               1924                 :                : 
  557 michael@paquier.xyz      1925                 :            904 :         add_object_address(ProcedureRelationId, aggref->aggfnoid, 0,
                               1926                 :                :                            context->addrs);
                               1927                 :                :         /* fall through to examine arguments */
                               1928                 :                :     }
 6096 tgl@sss.pgh.pa.us        1929         [ +  + ]:         319245 :     else if (IsA(node, WindowFunc))
                               1930                 :                :     {
                               1931                 :             78 :         WindowFunc *wfunc = (WindowFunc *) node;
                               1932                 :                : 
  557 michael@paquier.xyz      1933                 :             78 :         add_object_address(ProcedureRelationId, wfunc->winfnoid, 0,
                               1934                 :                :                            context->addrs);
                               1935                 :                :         /* fall through to examine arguments */
                               1936                 :                :     }
 1732 tgl@sss.pgh.pa.us        1937         [ +  + ]:         319167 :     else if (IsA(node, SubscriptingRef))
                               1938                 :                :     {
                               1939                 :           1321 :         SubscriptingRef *sbsref = (SubscriptingRef *) node;
                               1940                 :                : 
                               1941                 :                :         /*
                               1942                 :                :          * The refexpr should provide adequate dependency on refcontainertype,
                               1943                 :                :          * and that type in turn depends on refelemtype.  However, a custom
                               1944                 :                :          * subscripting handler might set refrestype to something different
                               1945                 :                :          * from either of those, in which case we'd better record it.
                               1946                 :                :          */
                               1947         [ +  + ]:           1321 :         if (sbsref->refrestype != sbsref->refcontainertype &&
                               1948         [ -  + ]:           1258 :             sbsref->refrestype != sbsref->refelemtype)
  557 michael@paquier.xyz      1949                 :UBC           0 :             add_object_address(TypeRelationId, sbsref->refrestype, 0,
                               1950                 :                :                                context->addrs);
                               1951                 :                :         /* fall through to examine arguments */
                               1952                 :                :     }
 6224 tgl@sss.pgh.pa.us        1953         [ -  + ]:CBC      317846 :     else if (IsA(node, SubPlan))
                               1954                 :                :     {
                               1955                 :                :         /* Extra work needed here if we ever need this case */
 7192 tgl@sss.pgh.pa.us        1956         [ #  # ]:UBC           0 :         elog(ERROR, "already-planned subqueries not supported");
                               1957                 :                :     }
 2875 tgl@sss.pgh.pa.us        1958         [ +  + ]:CBC      317846 :     else if (IsA(node, FieldSelect))
                               1959                 :                :     {
                               1960                 :           6975 :         FieldSelect *fselect = (FieldSelect *) node;
 2871                          1961                 :           6975 :         Oid         argtype = getBaseType(exprType((Node *) fselect->arg));
                               1962                 :           6975 :         Oid         reltype = get_typ_typrelid(argtype);
                               1963                 :                : 
                               1964                 :                :         /*
                               1965                 :                :          * We need a dependency on the specific column named in FieldSelect,
                               1966                 :                :          * assuming we can identify the pg_class OID for it.  (Probably we
                               1967                 :                :          * always can at the moment, but in future it might be possible for
                               1968                 :                :          * argtype to be RECORDOID.)  If we can make a column dependency then
                               1969                 :                :          * we shouldn't need a dependency on the column's type; but if we
                               1970                 :                :          * can't, make a dependency on the type, as it might not appear
                               1971                 :                :          * anywhere else in the expression.
                               1972                 :                :          */
                               1973         [ +  + ]:           6975 :         if (OidIsValid(reltype))
  557 michael@paquier.xyz      1974                 :           3933 :             add_object_address(RelationRelationId, reltype, fselect->fieldnum,
                               1975                 :                :                                context->addrs);
                               1976                 :                :         else
                               1977                 :           3042 :             add_object_address(TypeRelationId, fselect->resulttype, 0,
                               1978                 :                :                                context->addrs);
                               1979                 :                :         /* the collation might not be referenced anywhere else, either */
 1583 tmunro@postgresql.or     1980         [ +  + ]:           6975 :         if (OidIsValid(fselect->resultcollid) &&
                               1981         [ -  + ]:            854 :             fselect->resultcollid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      1982                 :UBC           0 :             add_object_address(CollationRelationId, fselect->resultcollid, 0,
                               1983                 :                :                                context->addrs);
                               1984                 :                :     }
 2875 tgl@sss.pgh.pa.us        1985         [ +  + ]:CBC      310871 :     else if (IsA(node, FieldStore))
                               1986                 :                :     {
                               1987                 :             48 :         FieldStore *fstore = (FieldStore *) node;
 2871                          1988                 :             48 :         Oid         reltype = get_typ_typrelid(fstore->resulttype);
                               1989                 :                : 
                               1990                 :                :         /* similar considerations to FieldSelect, but multiple column(s) */
                               1991         [ +  - ]:             48 :         if (OidIsValid(reltype))
                               1992                 :                :         {
                               1993                 :                :             ListCell   *l;
                               1994                 :                : 
                               1995   [ +  -  +  +  :             96 :             foreach(l, fstore->fieldnums)
                                              +  + ]
  557 michael@paquier.xyz      1996                 :             48 :                 add_object_address(RelationRelationId, reltype, lfirst_int(l),
                               1997                 :                :                                    context->addrs);
                               1998                 :                :         }
                               1999                 :                :         else
  557 michael@paquier.xyz      2000                 :UBC           0 :             add_object_address(TypeRelationId, fstore->resulttype, 0,
                               2001                 :                :                                context->addrs);
                               2002                 :                :     }
 6244 tgl@sss.pgh.pa.us        2003         [ +  + ]:CBC      310823 :     else if (IsA(node, RelabelType))
                               2004                 :                :     {
 6912 bruce@momjian.us         2005                 :           7033 :         RelabelType *relab = (RelabelType *) node;
                               2006                 :                : 
                               2007                 :                :         /* since there is no function dependency, need to depend on type */
  557 michael@paquier.xyz      2008                 :           7033 :         add_object_address(TypeRelationId, relab->resulttype, 0,
                               2009                 :                :                            context->addrs);
                               2010                 :                :         /* the collation might not be referenced anywhere else, either */
 1583 tmunro@postgresql.or     2011         [ +  + ]:           7033 :         if (OidIsValid(relab->resultcollid) &&
                               2012         [ +  + ]:           1607 :             relab->resultcollid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      2013                 :           1392 :             add_object_address(CollationRelationId, relab->resultcollid, 0,
                               2014                 :                :                                context->addrs);
                               2015                 :                :     }
 6244 tgl@sss.pgh.pa.us        2016         [ +  + ]:         303790 :     else if (IsA(node, CoerceViaIO))
                               2017                 :                :     {
 6668                          2018                 :           1116 :         CoerceViaIO *iocoerce = (CoerceViaIO *) node;
                               2019                 :                : 
                               2020                 :                :         /* since there is no exposed function, need to depend on type */
  557 michael@paquier.xyz      2021                 :           1116 :         add_object_address(TypeRelationId, iocoerce->resulttype, 0,
                               2022                 :                :                            context->addrs);
                               2023                 :                :         /* the collation might not be referenced anywhere else, either */
 1583 tmunro@postgresql.or     2024         [ +  + ]:           1116 :         if (OidIsValid(iocoerce->resultcollid) &&
                               2025         [ +  + ]:            894 :             iocoerce->resultcollid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      2026                 :            336 :             add_object_address(CollationRelationId, iocoerce->resultcollid, 0,
                               2027                 :                :                                context->addrs);
                               2028                 :                :     }
 6244 tgl@sss.pgh.pa.us        2029         [ +  + ]:         302674 :     else if (IsA(node, ArrayCoerceExpr))
                               2030                 :                :     {
 6738                          2031                 :            243 :         ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
                               2032                 :                : 
                               2033                 :                :         /* as above, depend on type */
  557 michael@paquier.xyz      2034                 :            243 :         add_object_address(TypeRelationId, acoerce->resulttype, 0,
                               2035                 :                :                            context->addrs);
                               2036                 :                :         /* the collation might not be referenced anywhere else, either */
 1583 tmunro@postgresql.or     2037         [ +  + ]:            243 :         if (OidIsValid(acoerce->resultcollid) &&
                               2038         [ +  + ]:             99 :             acoerce->resultcollid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      2039                 :             48 :             add_object_address(CollationRelationId, acoerce->resultcollid, 0,
                               2040                 :                :                                context->addrs);
                               2041                 :                :         /* fall through to examine arguments */
                               2042                 :                :     }
 6244 tgl@sss.pgh.pa.us        2043         [ -  + ]:         302431 :     else if (IsA(node, ConvertRowtypeExpr))
                               2044                 :                :     {
 7114 tgl@sss.pgh.pa.us        2045                 :UBC           0 :         ConvertRowtypeExpr *cvt = (ConvertRowtypeExpr *) node;
                               2046                 :                : 
                               2047                 :                :         /* since there is no function dependency, need to depend on type */
  557 michael@paquier.xyz      2048                 :              0 :         add_object_address(TypeRelationId, cvt->resulttype, 0,
                               2049                 :                :                            context->addrs);
                               2050                 :                :     }
 5293 tgl@sss.pgh.pa.us        2051         [ +  + ]:CBC      302431 :     else if (IsA(node, CollateExpr))
                               2052                 :                :     {
                               2053                 :             45 :         CollateExpr *coll = (CollateExpr *) node;
                               2054                 :                : 
  557 michael@paquier.xyz      2055                 :             45 :         add_object_address(CollationRelationId, coll->collOid, 0,
                               2056                 :                :                            context->addrs);
                               2057                 :                :     }
 6244 tgl@sss.pgh.pa.us        2058         [ +  + ]:         302386 :     else if (IsA(node, RowExpr))
                               2059                 :                :     {
 6912 bruce@momjian.us         2060                 :             30 :         RowExpr    *rowexpr = (RowExpr *) node;
                               2061                 :                : 
  557 michael@paquier.xyz      2062                 :             30 :         add_object_address(TypeRelationId, rowexpr->row_typeid, 0,
                               2063                 :                :                            context->addrs);
                               2064                 :                :     }
 6244 tgl@sss.pgh.pa.us        2065         [ +  + ]:         302356 :     else if (IsA(node, RowCompareExpr))
                               2066                 :                :     {
 7192                          2067                 :              9 :         RowCompareExpr *rcexpr = (RowCompareExpr *) node;
                               2068                 :                :         ListCell   *l;
                               2069                 :                : 
                               2070   [ +  -  +  +  :             27 :         foreach(l, rcexpr->opnos)
                                              +  + ]
                               2071                 :                :         {
  557 michael@paquier.xyz      2072                 :             18 :             add_object_address(OperatorRelationId, lfirst_oid(l), 0,
                               2073                 :                :                                context->addrs);
                               2074                 :                :         }
 6832 tgl@sss.pgh.pa.us        2075   [ +  -  +  +  :             27 :         foreach(l, rcexpr->opfamilies)
                                              +  + ]
                               2076                 :                :         {
  557 michael@paquier.xyz      2077                 :             18 :             add_object_address(OperatorFamilyRelationId, lfirst_oid(l), 0,
                               2078                 :                :                                context->addrs);
                               2079                 :                :         }
                               2080                 :                :         /* fall through to examine arguments */
                               2081                 :                :     }
 6244 tgl@sss.pgh.pa.us        2082         [ +  + ]:         302347 :     else if (IsA(node, CoerceToDomain))
                               2083                 :                :     {
 7114                          2084                 :          30360 :         CoerceToDomain *cd = (CoerceToDomain *) node;
                               2085                 :                : 
  557 michael@paquier.xyz      2086                 :          30360 :         add_object_address(TypeRelationId, cd->resulttype, 0,
                               2087                 :                :                            context->addrs);
                               2088                 :                :     }
 2976 tgl@sss.pgh.pa.us        2089         [ -  + ]:         271987 :     else if (IsA(node, NextValueExpr))
                               2090                 :                :     {
 2976 tgl@sss.pgh.pa.us        2091                 :UBC           0 :         NextValueExpr *nve = (NextValueExpr *) node;
                               2092                 :                : 
  557 michael@paquier.xyz      2093                 :              0 :         add_object_address(RelationRelationId, nve->seqid, 0,
                               2094                 :                :                            context->addrs);
                               2095                 :                :     }
 3405 tgl@sss.pgh.pa.us        2096         [ +  + ]:CBC      271987 :     else if (IsA(node, OnConflictExpr))
                               2097                 :                :     {
                               2098                 :              9 :         OnConflictExpr *onconflict = (OnConflictExpr *) node;
                               2099                 :                : 
                               2100         [ -  + ]:              9 :         if (OidIsValid(onconflict->constraint))
  557 michael@paquier.xyz      2101                 :UBC           0 :             add_object_address(ConstraintRelationId, onconflict->constraint, 0,
                               2102                 :                :                                context->addrs);
                               2103                 :                :         /* fall through to examine arguments */
                               2104                 :                :     }
 6244 tgl@sss.pgh.pa.us        2105         [ +  + ]:CBC      271978 :     else if (IsA(node, SortGroupClause))
                               2106                 :                :     {
                               2107                 :           6201 :         SortGroupClause *sgc = (SortGroupClause *) node;
                               2108                 :                : 
  557 michael@paquier.xyz      2109                 :           6201 :         add_object_address(OperatorRelationId, sgc->eqop, 0,
                               2110                 :                :                            context->addrs);
 6244 tgl@sss.pgh.pa.us        2111         [ +  - ]:           6201 :         if (OidIsValid(sgc->sortop))
  557 michael@paquier.xyz      2112                 :           6201 :             add_object_address(OperatorRelationId, sgc->sortop, 0,
                               2113                 :                :                                context->addrs);
 6244 tgl@sss.pgh.pa.us        2114                 :           6201 :         return false;
                               2115                 :                :     }
 2768                          2116         [ +  + ]:         265777 :     else if (IsA(node, WindowClause))
                               2117                 :                :     {
                               2118                 :             78 :         WindowClause *wc = (WindowClause *) node;
                               2119                 :                : 
                               2120         [ +  + ]:             78 :         if (OidIsValid(wc->startInRangeFunc))
  557 michael@paquier.xyz      2121                 :              3 :             add_object_address(ProcedureRelationId, wc->startInRangeFunc, 0,
                               2122                 :                :                                context->addrs);
 2768 tgl@sss.pgh.pa.us        2123         [ +  + ]:             78 :         if (OidIsValid(wc->endInRangeFunc))
  557 michael@paquier.xyz      2124                 :              3 :             add_object_address(ProcedureRelationId, wc->endInRangeFunc, 0,
                               2125                 :                :                                context->addrs);
 1583 tmunro@postgresql.or     2126         [ -  + ]:             78 :         if (OidIsValid(wc->inRangeColl) &&
 1583 tmunro@postgresql.or     2127         [ #  # ]:UBC           0 :             wc->inRangeColl != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      2128                 :              0 :             add_object_address(CollationRelationId, wc->inRangeColl, 0,
                               2129                 :                :                                context->addrs);
                               2130                 :                :         /* fall through to examine substructure */
                               2131                 :                :     }
 1678 peter@eisentraut.org     2132         [ +  + ]:CBC      265699 :     else if (IsA(node, CTECycleClause))
                               2133                 :                :     {
                               2134                 :              6 :         CTECycleClause *cc = (CTECycleClause *) node;
                               2135                 :                : 
                               2136         [ +  - ]:              6 :         if (OidIsValid(cc->cycle_mark_type))
  557 michael@paquier.xyz      2137                 :              6 :             add_object_address(TypeRelationId, cc->cycle_mark_type, 0,
                               2138                 :                :                                context->addrs);
 1678 peter@eisentraut.org     2139         [ +  + ]:              6 :         if (OidIsValid(cc->cycle_mark_collation))
  557 michael@paquier.xyz      2140                 :              3 :             add_object_address(CollationRelationId, cc->cycle_mark_collation, 0,
                               2141                 :                :                                context->addrs);
 1678 peter@eisentraut.org     2142         [ +  - ]:              6 :         if (OidIsValid(cc->cycle_mark_neop))
  557 michael@paquier.xyz      2143                 :              6 :             add_object_address(OperatorRelationId, cc->cycle_mark_neop, 0,
                               2144                 :                :                                context->addrs);
                               2145                 :                :         /* fall through to examine substructure */
                               2146                 :                :     }
 6244 tgl@sss.pgh.pa.us        2147         [ +  + ]:         265693 :     else if (IsA(node, Query))
                               2148                 :                :     {
                               2149                 :                :         /* Recurse into RTE subquery or not-yet-planned sublink subquery */
 8453                          2150                 :          18731 :         Query      *query = (Query *) node;
                               2151                 :                :         ListCell   *lc;
                               2152                 :                :         bool        result;
                               2153                 :                : 
                               2154                 :                :         /*
                               2155                 :                :          * Add whole-relation refs for each plain relation mentioned in the
                               2156                 :                :          * subquery's rtable, and ensure we add refs for any type-coercion
                               2157                 :                :          * functions used in join alias lists.
                               2158                 :                :          *
                               2159                 :                :          * Note: query_tree_walker takes care of recursing into RTE_FUNCTION
                               2160                 :                :          * RTEs, subqueries, etc, so no need to do that here.  But we must
                               2161                 :                :          * tell it not to visit join alias lists, or we'll add refs for join
                               2162                 :                :          * input columns whether or not they are actually used in our query.
                               2163                 :                :          *
                               2164                 :                :          * Note: we don't need to worry about collations mentioned in
                               2165                 :                :          * RTE_VALUES or RTE_CTE RTEs, because those must just duplicate
                               2166                 :                :          * collations referenced in other parts of the Query.  We do have to
                               2167                 :                :          * worry about collations mentioned in RTE_FUNCTION, but we take care
                               2168                 :                :          * of those when we recurse to the RangeTblFunction node(s).
                               2169                 :                :          */
 5509                          2170   [ +  +  +  +  :          58918 :         foreach(lc, query->rtable)
                                              +  + ]
                               2171                 :                :         {
                               2172                 :          40190 :             RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
                               2173                 :                : 
 7114                          2174   [ +  +  +  + ]:          40190 :             switch (rte->rtekind)
                               2175                 :                :             {
                               2176                 :          25128 :                 case RTE_RELATION:
  557 michael@paquier.xyz      2177                 :          25128 :                     add_object_address(RelationRelationId, rte->relid, 0,
                               2178                 :                :                                        context->addrs);
 7114 tgl@sss.pgh.pa.us        2179                 :          25128 :                     break;
 2067                          2180                 :           7273 :                 case RTE_JOIN:
                               2181                 :                : 
                               2182                 :                :                     /*
                               2183                 :                :                      * Examine joinaliasvars entries only for merged JOIN
                               2184                 :                :                      * USING columns.  Only those entries could contain
                               2185                 :                :                      * type-coercion functions.  Also, their join input
                               2186                 :                :                      * columns must be referenced in the join quals, so this
                               2187                 :                :                      * won't accidentally add refs to otherwise-unused join
                               2188                 :                :                      * input columns.  (We want to ref the type coercion
                               2189                 :                :                      * functions even if the merged column isn't explicitly
                               2190                 :                :                      * used anywhere, to protect possible expansion of the
                               2191                 :                :                      * join RTE as a whole-row var, and because it seems like
                               2192                 :                :                      * a bad idea to allow dropping a function that's present
                               2193                 :                :                      * in our query tree, whether or not it could get called.)
                               2194                 :                :                      */
                               2195                 :           7273 :                     context->rtables = lcons(query->rtable, context->rtables);
                               2196         [ +  + ]:           7373 :                     for (int i = 0; i < rte->joinmergedcols; i++)
                               2197                 :                :                     {
                               2198                 :            100 :                         Node       *aliasvar = list_nth(rte->joinaliasvars, i);
                               2199                 :                : 
                               2200         [ +  + ]:            100 :                         if (!IsA(aliasvar, Var))
                               2201                 :             24 :                             find_expr_references_walker(aliasvar, context);
                               2202                 :                :                     }
                               2203                 :           7273 :                     context->rtables = list_delete_first(context->rtables);
                               2204                 :           7273 :                     break;
  241                          2205                 :              3 :                 case RTE_NAMEDTUPLESTORE:
                               2206                 :                : 
                               2207                 :                :                     /*
                               2208                 :                :                      * Cataloged objects cannot depend on tuplestores, because
                               2209                 :                :                      * those have no cataloged representation.  For now we can
                               2210                 :                :                      * call the tuplestore a "transition table" because that's
                               2211                 :                :                      * the only kind exposed to SQL, but someday we might have
                               2212                 :                :                      * to work harder.
                               2213                 :                :                      */
                               2214         [ +  - ]:              3 :                     ereport(ERROR,
                               2215                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2216                 :                :                              errmsg("transition table \"%s\" cannot be referenced in a persistent object",
                               2217                 :                :                                     rte->eref->aliasname)));
                               2218                 :                :                     break;
 7114                          2219                 :           7786 :                 default:
                               2220                 :                :                     /* Other RTE types can be ignored here */
                               2221                 :           7786 :                     break;
                               2222                 :                :             }
                               2223                 :                :         }
                               2224                 :                : 
                               2225                 :                :         /*
                               2226                 :                :          * If the query is an INSERT or UPDATE, we should create a dependency
                               2227                 :                :          * on each target column, to prevent the specific target column from
                               2228                 :                :          * being dropped.  Although we will visit the TargetEntry nodes again
                               2229                 :                :          * during query_tree_walker, we won't have enough context to do this
                               2230                 :                :          * conveniently, so do it here.
                               2231                 :                :          */
 4927                          2232         [ +  + ]:          18728 :         if (query->commandType == CMD_INSERT ||
                               2233         [ +  + ]:          18500 :             query->commandType == CMD_UPDATE)
                               2234                 :                :         {
                               2235                 :                :             RangeTblEntry *rte;
                               2236                 :                : 
                               2237   [ +  -  -  + ]:            686 :             if (query->resultRelation <= 0 ||
                               2238                 :            343 :                 query->resultRelation > list_length(query->rtable))
 4927 tgl@sss.pgh.pa.us        2239         [ #  # ]:UBC           0 :                 elog(ERROR, "invalid resultRelation %d",
                               2240                 :                :                      query->resultRelation);
 4927 tgl@sss.pgh.pa.us        2241                 :CBC         343 :             rte = rt_fetch(query->resultRelation, query->rtable);
                               2242         [ +  - ]:            343 :             if (rte->rtekind == RTE_RELATION)
                               2243                 :                :             {
                               2244   [ +  +  +  +  :           1041 :                 foreach(lc, query->targetList)
                                              +  + ]
                               2245                 :                :                 {
                               2246                 :            698 :                     TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               2247                 :                : 
                               2248         [ +  + ]:            698 :                     if (tle->resjunk)
 2999                          2249                 :              3 :                         continue;   /* ignore junk tlist items */
  557 michael@paquier.xyz      2250                 :            695 :                     add_object_address(RelationRelationId, rte->relid, tle->resno,
                               2251                 :                :                                        context->addrs);
                               2252                 :                :                 }
                               2253                 :                :             }
                               2254                 :                :         }
                               2255                 :                : 
                               2256                 :                :         /*
                               2257                 :                :          * Add dependencies on constraints listed in query's constraintDeps
                               2258                 :                :          */
 5509 tgl@sss.pgh.pa.us        2259   [ +  +  +  +  :          18756 :         foreach(lc, query->constraintDeps)
                                              +  + ]
                               2260                 :                :         {
  557 michael@paquier.xyz      2261                 :             28 :             add_object_address(ConstraintRelationId, lfirst_oid(lc), 0,
                               2262                 :                :                                context->addrs);
                               2263                 :                :         }
                               2264                 :                : 
                               2265                 :                :         /* Examine substructure of query */
 8453 tgl@sss.pgh.pa.us        2266                 :          18728 :         context->rtables = lcons(query->rtable, context->rtables);
                               2267                 :          18728 :         result = query_tree_walker(query,
                               2268                 :                :                                    find_expr_references_walker,
                               2269                 :                :                                    context,
                               2270                 :                :                                    QTW_IGNORE_JOINALIASES |
                               2271                 :                :                                    QTW_EXAMINE_SORTGROUP);
 7773 neilc@samurai.com        2272                 :          18728 :         context->rtables = list_delete_first(context->rtables);
 8453 tgl@sss.pgh.pa.us        2273                 :          18728 :         return result;
                               2274                 :                :     }
 6239                          2275         [ +  + ]:         246962 :     else if (IsA(node, SetOperationStmt))
                               2276                 :                :     {
                               2277                 :           1926 :         SetOperationStmt *setop = (SetOperationStmt *) node;
                               2278                 :                : 
                               2279                 :                :         /* we need to look at the groupClauses for operator references */
                               2280                 :           1926 :         find_expr_references_walker((Node *) setop->groupClauses, context);
                               2281                 :                :         /* fall through to examine child nodes */
                               2282                 :                :     }
 4307                          2283         [ +  + ]:         245036 :     else if (IsA(node, RangeTblFunction))
                               2284                 :                :     {
                               2285                 :           2690 :         RangeTblFunction *rtfunc = (RangeTblFunction *) node;
                               2286                 :                :         ListCell   *ct;
                               2287                 :                : 
                               2288                 :                :         /*
                               2289                 :                :          * Add refs for any datatypes and collations used in a column
                               2290                 :                :          * definition list for a RECORD function.  (For other cases, it should
                               2291                 :                :          * be enough to depend on the function itself.)
                               2292                 :                :          */
                               2293   [ +  +  +  +  :           2747 :         foreach(ct, rtfunc->funccoltypes)
                                              +  + ]
                               2294                 :                :         {
  557 michael@paquier.xyz      2295                 :             57 :             add_object_address(TypeRelationId, lfirst_oid(ct), 0,
                               2296                 :                :                                context->addrs);
                               2297                 :                :         }
 4307 tgl@sss.pgh.pa.us        2298   [ +  +  +  +  :           2747 :         foreach(ct, rtfunc->funccolcollations)
                                              +  + ]
                               2299                 :                :         {
                               2300                 :             57 :             Oid         collid = lfirst_oid(ct);
                               2301                 :                : 
 1583 tmunro@postgresql.or     2302   [ +  +  -  + ]:             57 :             if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      2303                 :UBC           0 :                 add_object_address(CollationRelationId, collid, 0,
                               2304                 :                :                                    context->addrs);
                               2305                 :                :         }
                               2306                 :                :     }
 2124 tgl@sss.pgh.pa.us        2307         [ +  + ]:CBC      242346 :     else if (IsA(node, TableFunc))
                               2308                 :                :     {
                               2309                 :             41 :         TableFunc  *tf = (TableFunc *) node;
                               2310                 :                :         ListCell   *ct;
                               2311                 :                : 
                               2312                 :                :         /*
                               2313                 :                :          * Add refs for the datatypes and collations used in the TableFunc.
                               2314                 :                :          */
                               2315   [ +  -  +  +  :            194 :         foreach(ct, tf->coltypes)
                                              +  + ]
                               2316                 :                :         {
  557 michael@paquier.xyz      2317                 :            153 :             add_object_address(TypeRelationId, lfirst_oid(ct), 0,
                               2318                 :                :                                context->addrs);
                               2319                 :                :         }
 2124 tgl@sss.pgh.pa.us        2320   [ +  -  +  +  :            194 :         foreach(ct, tf->colcollations)
                                              +  + ]
                               2321                 :                :         {
                               2322                 :            153 :             Oid         collid = lfirst_oid(ct);
                               2323                 :                : 
 1583 tmunro@postgresql.or     2324   [ +  +  -  + ]:            153 :             if (OidIsValid(collid) && collid != DEFAULT_COLLATION_OID)
  557 michael@paquier.xyz      2325                 :UBC           0 :                 add_object_address(CollationRelationId, collid, 0,
                               2326                 :                :                                    context->addrs);
                               2327                 :                :         }
                               2328                 :                :     }
 3696 tgl@sss.pgh.pa.us        2329         [ +  + ]:CBC      242305 :     else if (IsA(node, TableSampleClause))
                               2330                 :                :     {
                               2331                 :             10 :         TableSampleClause *tsc = (TableSampleClause *) node;
                               2332                 :                : 
  557 michael@paquier.xyz      2333                 :             10 :         add_object_address(ProcedureRelationId, tsc->tsmhandler, 0,
                               2334                 :                :                            context->addrs);
                               2335                 :                :         /* fall through to examine arguments */
                               2336                 :                :     }
                               2337                 :                : 
 8453 tgl@sss.pgh.pa.us        2338                 :         393803 :     return expression_tree_walker(node, find_expr_references_walker,
                               2339                 :                :                                   context);
                               2340                 :                : }
                               2341                 :                : 
                               2342                 :                : /*
                               2343                 :                :  * find_expr_references_walker subroutine: handle a Var reference
                               2344                 :                :  * to an RTE_FUNCTION RTE
                               2345                 :                :  */
                               2346                 :                : static void
 1142                          2347                 :          23062 : process_function_rte_ref(RangeTblEntry *rte, AttrNumber attnum,
                               2348                 :                :                          find_expr_references_context *context)
                               2349                 :                : {
                               2350                 :          23062 :     int         atts_done = 0;
                               2351                 :                :     ListCell   *lc;
                               2352                 :                : 
                               2353                 :                :     /*
                               2354                 :                :      * Identify which RangeTblFunction produces this attnum, and see if it
                               2355                 :                :      * returns a composite type.  If so, we'd better make a dependency on the
                               2356                 :                :      * referenced column of the composite type (or actually, of its associated
                               2357                 :                :      * relation).
                               2358                 :                :      */
                               2359   [ +  -  +  +  :          23173 :     foreach(lc, rte->functions)
                                              +  + ]
                               2360                 :                :     {
                               2361                 :          23128 :         RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                               2362                 :                : 
                               2363         [ +  - ]:          23128 :         if (attnum > atts_done &&
                               2364         [ +  + ]:          23128 :             attnum <= atts_done + rtfunc->funccolcount)
                               2365                 :                :         {
                               2366                 :                :             TupleDesc   tupdesc;
                               2367                 :                : 
                               2368                 :                :             /* If it has a coldeflist, it certainly returns RECORD */
  509                          2369         [ +  + ]:          23017 :             if (rtfunc->funccolnames != NIL)
                               2370                 :             57 :                 tupdesc = NULL; /* no need to work hard */
                               2371                 :                :             else
                               2372                 :          22960 :                 tupdesc = get_expr_result_tupdesc(rtfunc->funcexpr, true);
 1142                          2373   [ +  +  +  + ]:          23017 :             if (tupdesc && tupdesc->tdtypeid != RECORDOID)
                               2374                 :                :             {
                               2375                 :                :                 /*
                               2376                 :                :                  * Named composite type, so individual columns could get
                               2377                 :                :                  * dropped.  Make a dependency on this specific column.
                               2378                 :                :                  */
                               2379                 :            111 :                 Oid         reltype = get_typ_typrelid(tupdesc->tdtypeid);
                               2380                 :                : 
                               2381         [ -  + ]:            111 :                 Assert(attnum - atts_done <= tupdesc->natts);
                               2382         [ +  - ]:            111 :                 if (OidIsValid(reltype))    /* can this fail? */
  557 michael@paquier.xyz      2383                 :            111 :                     add_object_address(RelationRelationId, reltype,
                               2384                 :                :                                        attnum - atts_done,
                               2385                 :                :                                        context->addrs);
 1142 tgl@sss.pgh.pa.us        2386                 :          23017 :                 return;
                               2387                 :                :             }
                               2388                 :                :             /* Nothing to do; function's result type is handled elsewhere */
                               2389                 :          22906 :             return;
                               2390                 :                :         }
                               2391                 :            111 :         atts_done += rtfunc->funccolcount;
                               2392                 :                :     }
                               2393                 :                : 
                               2394                 :                :     /* If we get here, must be looking for the ordinality column */
                               2395   [ +  -  +  - ]:             45 :     if (rte->funcordinality && attnum == atts_done + 1)
                               2396                 :             45 :         return;
                               2397                 :                : 
                               2398                 :                :     /* this probably can't happen ... */
 1142 tgl@sss.pgh.pa.us        2399         [ #  # ]:UBC           0 :     ereport(ERROR,
                               2400                 :                :             (errcode(ERRCODE_UNDEFINED_COLUMN),
                               2401                 :                :              errmsg("column %d of relation \"%s\" does not exist",
                               2402                 :                :                     attnum, rte->eref->aliasname)));
                               2403                 :                : }
                               2404                 :                : 
                               2405                 :                : /*
                               2406                 :                :  * Given an array of dependency references, eliminate any duplicates.
                               2407                 :                :  */
                               2408                 :                : static void
 8453 tgl@sss.pgh.pa.us        2409                 :CBC      215485 : eliminate_duplicate_dependencies(ObjectAddresses *addrs)
                               2410                 :                : {
                               2411                 :                :     ObjectAddress *priorobj;
                               2412                 :                :     int         oldref,
                               2413                 :                :                 newrefs;
                               2414                 :                : 
                               2415                 :                :     /*
                               2416                 :                :      * We can't sort if the array has "extra" data, because there's no way to
                               2417                 :                :      * keep it in sync.  Fortunately that combination of features is not
                               2418                 :                :      * needed.
                               2419                 :                :      */
 6299                          2420         [ -  + ]:         215485 :     Assert(!addrs->extras);
                               2421                 :                : 
 8453                          2422         [ +  + ]:         215485 :     if (addrs->numrefs <= 1)
                               2423                 :          76058 :         return;                 /* nothing to do */
                               2424                 :                : 
                               2425                 :                :     /* Sort the refs so that duplicates are adjacent */
  942 peter@eisentraut.org     2426                 :         139427 :     qsort(addrs->refs, addrs->numrefs, sizeof(ObjectAddress),
                               2427                 :                :           object_address_comparator);
                               2428                 :                : 
                               2429                 :                :     /* Remove dups */
 8453 tgl@sss.pgh.pa.us        2430                 :         139427 :     priorobj = addrs->refs;
                               2431                 :         139427 :     newrefs = 1;
                               2432         [ +  + ]:         944180 :     for (oldref = 1; oldref < addrs->numrefs; oldref++)
                               2433                 :                :     {
 8403 bruce@momjian.us         2434                 :         804753 :         ObjectAddress *thisobj = addrs->refs + oldref;
                               2435                 :                : 
 8453 tgl@sss.pgh.pa.us        2436         [ +  + ]:         804753 :         if (priorobj->classId == thisobj->classId &&
                               2437         [ +  + ]:         689490 :             priorobj->objectId == thisobj->objectId)
                               2438                 :                :         {
                               2439         [ +  + ]:         371152 :             if (priorobj->objectSubId == thisobj->objectSubId)
                               2440                 :         286314 :                 continue;       /* identical, so drop thisobj */
                               2441                 :                : 
                               2442                 :                :             /*
                               2443                 :                :              * If we have a whole-object reference and a reference to a part
                               2444                 :                :              * of the same object, we don't need the whole-object reference
                               2445                 :                :              * (for example, we don't need to reference both table foo and
                               2446                 :                :              * column foo.bar).  The whole-object reference will always appear
                               2447                 :                :              * first in the sorted list.
                               2448                 :                :              */
                               2449         [ +  + ]:          84838 :             if (priorobj->objectSubId == 0)
                               2450                 :                :             {
                               2451                 :                :                 /* replace whole ref with partial */
                               2452                 :          18443 :                 priorobj->objectSubId = thisobj->objectSubId;
                               2453                 :          18443 :                 continue;
                               2454                 :                :             }
                               2455                 :                :         }
                               2456                 :                :         /* Not identical, so add thisobj to output set */
                               2457                 :         499996 :         priorobj++;
 6299                          2458                 :         499996 :         *priorobj = *thisobj;
 8453                          2459                 :         499996 :         newrefs++;
                               2460                 :                :     }
                               2461                 :                : 
                               2462                 :         139427 :     addrs->numrefs = newrefs;
                               2463                 :                : }
                               2464                 :                : 
                               2465                 :                : /*
                               2466                 :                :  * qsort comparator for ObjectAddress items
                               2467                 :                :  */
                               2468                 :                : static int
                               2469                 :        2788008 : object_address_comparator(const void *a, const void *b)
                               2470                 :                : {
                               2471                 :        2788008 :     const ObjectAddress *obja = (const ObjectAddress *) a;
                               2472                 :        2788008 :     const ObjectAddress *objb = (const ObjectAddress *) b;
                               2473                 :                : 
                               2474                 :                :     /*
                               2475                 :                :      * Primary sort key is OID descending.  Most of the time, this will result
                               2476                 :                :      * in putting newer objects before older ones, which is likely to be the
                               2477                 :                :      * right order to delete in.
                               2478                 :                :      */
 2420                          2479         [ +  + ]:        2788008 :     if (obja->objectId > objb->objectId)
 8453                          2480                 :         675542 :         return -1;
                               2481         [ +  + ]:        2112466 :     if (obja->objectId < objb->objectId)
 2420                          2482                 :        1463213 :         return 1;
                               2483                 :                : 
                               2484                 :                :     /*
                               2485                 :                :      * Next sort on catalog ID, in case identical OIDs appear in different
                               2486                 :                :      * catalogs.  Sort direction is pretty arbitrary here.
                               2487                 :                :      */
                               2488         [ -  + ]:         649253 :     if (obja->classId < objb->classId)
 8453 tgl@sss.pgh.pa.us        2489                 :UBC           0 :         return -1;
 2420 tgl@sss.pgh.pa.us        2490         [ -  + ]:CBC      649253 :     if (obja->classId > objb->classId)
 8453 tgl@sss.pgh.pa.us        2491                 :UBC           0 :         return 1;
                               2492                 :                : 
                               2493                 :                :     /*
                               2494                 :                :      * Last, sort on object subId.
                               2495                 :                :      *
                               2496                 :                :      * We sort the subId as an unsigned int so that 0 (the whole object) will
                               2497                 :                :      * come first.  This is essential for eliminate_duplicate_dependencies,
                               2498                 :                :      * and is also the best order for findDependentObjects.
                               2499                 :                :      */
 8453 tgl@sss.pgh.pa.us        2500         [ +  + ]:CBC      649253 :     if ((unsigned int) obja->objectSubId < (unsigned int) objb->objectSubId)
                               2501                 :         158682 :         return -1;
                               2502         [ +  + ]:         490571 :     if ((unsigned int) obja->objectSubId > (unsigned int) objb->objectSubId)
                               2503                 :         140190 :         return 1;
                               2504                 :         350381 :     return 0;
                               2505                 :                : }
                               2506                 :                : 
                               2507                 :                : /*
                               2508                 :                :  * Routines for handling an expansible array of ObjectAddress items.
                               2509                 :                :  *
                               2510                 :                :  * new_object_addresses: create a new ObjectAddresses array.
                               2511                 :                :  */
                               2512                 :                : ObjectAddresses *
 6957 alvherre@alvh.no-ip.     2513                 :         247551 : new_object_addresses(void)
                               2514                 :                : {
                               2515                 :                :     ObjectAddresses *addrs;
                               2516                 :                : 
                               2517                 :         247551 :     addrs = palloc(sizeof(ObjectAddresses));
                               2518                 :                : 
 8453 tgl@sss.pgh.pa.us        2519                 :         247551 :     addrs->numrefs = 0;
 6957 alvherre@alvh.no-ip.     2520                 :         247551 :     addrs->maxrefs = 32;
 8453 tgl@sss.pgh.pa.us        2521                 :         247551 :     addrs->refs = (ObjectAddress *)
                               2522                 :         247551 :         palloc(addrs->maxrefs * sizeof(ObjectAddress));
 6299                          2523                 :         247551 :     addrs->extras = NULL;        /* until/unless needed */
                               2524                 :                : 
 6957 alvherre@alvh.no-ip.     2525                 :         247551 :     return addrs;
                               2526                 :                : }
                               2527                 :                : 
                               2528                 :                : /*
                               2529                 :                :  * Add an entry to an ObjectAddresses array.
                               2530                 :                :  */
                               2531                 :                : static void
  557 michael@paquier.xyz      2532                 :         394457 : add_object_address(Oid classId, Oid objectId, int32 subId,
                               2533                 :                :                    ObjectAddresses *addrs)
                               2534                 :                : {
                               2535                 :                :     ObjectAddress *item;
                               2536                 :                : 
                               2537                 :                :     /* enlarge array if needed */
 8453 tgl@sss.pgh.pa.us        2538         [ +  + ]:         394457 :     if (addrs->numrefs >= addrs->maxrefs)
                               2539                 :                :     {
                               2540                 :           5250 :         addrs->maxrefs *= 2;
                               2541                 :           5250 :         addrs->refs = (ObjectAddress *)
                               2542                 :           5250 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
 6299                          2543         [ -  + ]:           5250 :         Assert(!addrs->extras);
                               2544                 :                :     }
                               2545                 :                :     /* record this item */
 8453                          2546                 :         394457 :     item = addrs->refs + addrs->numrefs;
  557 michael@paquier.xyz      2547                 :         394457 :     item->classId = classId;
 8453 tgl@sss.pgh.pa.us        2548                 :         394457 :     item->objectId = objectId;
                               2549                 :         394457 :     item->objectSubId = subId;
                               2550                 :         394457 :     addrs->numrefs++;
                               2551                 :         394457 : }
                               2552                 :                : 
                               2553                 :                : /*
                               2554                 :                :  * Add an entry to an ObjectAddresses array.
                               2555                 :                :  *
                               2556                 :                :  * As above, but specify entry exactly.
                               2557                 :                :  */
                               2558                 :                : void
                               2559                 :         618346 : add_exact_object_address(const ObjectAddress *object,
                               2560                 :                :                          ObjectAddresses *addrs)
                               2561                 :                : {
                               2562                 :                :     ObjectAddress *item;
                               2563                 :                : 
                               2564                 :                :     /* enlarge array if needed */
                               2565         [ +  + ]:         618346 :     if (addrs->numrefs >= addrs->maxrefs)
                               2566                 :                :     {
                               2567                 :             37 :         addrs->maxrefs *= 2;
                               2568                 :             37 :         addrs->refs = (ObjectAddress *)
                               2569                 :             37 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
 6299                          2570         [ -  + ]:             37 :         Assert(!addrs->extras);
                               2571                 :                :     }
                               2572                 :                :     /* record this item */
 8453                          2573                 :         618346 :     item = addrs->refs + addrs->numrefs;
                               2574                 :         618346 :     *item = *object;
                               2575                 :         618346 :     addrs->numrefs++;
                               2576                 :         618346 : }
                               2577                 :                : 
                               2578                 :                : /*
                               2579                 :                :  * Add an entry to an ObjectAddresses array.
                               2580                 :                :  *
                               2581                 :                :  * As above, but specify entry exactly and provide some "extra" data too.
                               2582                 :                :  */
                               2583                 :                : static void
 6299                          2584                 :         104623 : add_exact_object_address_extra(const ObjectAddress *object,
                               2585                 :                :                                const ObjectAddressExtra *extra,
                               2586                 :                :                                ObjectAddresses *addrs)
                               2587                 :                : {
                               2588                 :                :     ObjectAddress *item;
                               2589                 :                :     ObjectAddressExtra *itemextra;
                               2590                 :                : 
                               2591                 :                :     /* allocate extra space if first time */
                               2592         [ +  + ]:         104623 :     if (!addrs->extras)
                               2593                 :          16229 :         addrs->extras = (ObjectAddressExtra *)
                               2594                 :          16229 :             palloc(addrs->maxrefs * sizeof(ObjectAddressExtra));
                               2595                 :                : 
                               2596                 :                :     /* enlarge array if needed */
                               2597         [ +  + ]:         104623 :     if (addrs->numrefs >= addrs->maxrefs)
                               2598                 :                :     {
                               2599                 :            398 :         addrs->maxrefs *= 2;
                               2600                 :            398 :         addrs->refs = (ObjectAddress *)
                               2601                 :            398 :             repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));
                               2602                 :            398 :         addrs->extras = (ObjectAddressExtra *)
 5931 bruce@momjian.us         2603                 :            398 :             repalloc(addrs->extras, addrs->maxrefs * sizeof(ObjectAddressExtra));
                               2604                 :                :     }
                               2605                 :                :     /* record this item */
 6299 tgl@sss.pgh.pa.us        2606                 :         104623 :     item = addrs->refs + addrs->numrefs;
                               2607                 :         104623 :     *item = *object;
                               2608                 :         104623 :     itemextra = addrs->extras + addrs->numrefs;
                               2609                 :         104623 :     *itemextra = *extra;
                               2610                 :         104623 :     addrs->numrefs++;
                               2611                 :         104623 : }
                               2612                 :                : 
                               2613                 :                : /*
                               2614                 :                :  * Test whether an object is present in an ObjectAddresses array.
                               2615                 :                :  *
                               2616                 :                :  * We return "true" if object is a subobject of something in the array, too.
                               2617                 :                :  */
                               2618                 :                : bool
 8385                          2619                 :            349 : object_address_present(const ObjectAddress *object,
                               2620                 :                :                        const ObjectAddresses *addrs)
                               2621                 :                : {
                               2622                 :                :     int         i;
                               2623                 :                : 
                               2624         [ +  + ]:           1299 :     for (i = addrs->numrefs - 1; i >= 0; i--)
                               2625                 :                :     {
 6299                          2626                 :            950 :         const ObjectAddress *thisobj = addrs->refs + i;
                               2627                 :                : 
 8385                          2628         [ +  + ]:            950 :         if (object->classId == thisobj->classId &&
                               2629         [ -  + ]:            265 :             object->objectId == thisobj->objectId)
                               2630                 :                :         {
 8385 tgl@sss.pgh.pa.us        2631         [ #  # ]:UBC           0 :             if (object->objectSubId == thisobj->objectSubId ||
                               2632         [ #  # ]:              0 :                 thisobj->objectSubId == 0)
                               2633                 :              0 :                 return true;
                               2634                 :                :         }
                               2635                 :                :     }
                               2636                 :                : 
 8385 tgl@sss.pgh.pa.us        2637                 :CBC         349 :     return false;
                               2638                 :                : }
                               2639                 :                : 
                               2640                 :                : /*
                               2641                 :                :  * As above, except that if the object is present then also OR the given
                               2642                 :                :  * flags into its associated extra data (which must exist).
                               2643                 :                :  */
                               2644                 :                : static bool
 6299                          2645                 :         129660 : object_address_present_add_flags(const ObjectAddress *object,
                               2646                 :                :                                  int flags,
                               2647                 :                :                                  ObjectAddresses *addrs)
                               2648                 :                : {
 3952                          2649                 :         129660 :     bool        result = false;
                               2650                 :                :     int         i;
                               2651                 :                : 
 6299                          2652         [ +  + ]:        4082791 :     for (i = addrs->numrefs - 1; i >= 0; i--)
                               2653                 :                :     {
                               2654                 :        3953131 :         ObjectAddress *thisobj = addrs->refs + i;
                               2655                 :                : 
                               2656         [ +  + ]:        3953131 :         if (object->classId == thisobj->classId &&
                               2657         [ +  + ]:        1259688 :             object->objectId == thisobj->objectId)
                               2658                 :                :         {
                               2659         [ +  + ]:          24183 :             if (object->objectSubId == thisobj->objectSubId)
                               2660                 :                :             {
                               2661                 :          23955 :                 ObjectAddressExtra *thisextra = addrs->extras + i;
                               2662                 :                : 
                               2663                 :          23955 :                 thisextra->flags |= flags;
 3952                          2664                 :          23955 :                 result = true;
                               2665                 :                :             }
                               2666         [ +  + ]:            228 :             else if (thisobj->objectSubId == 0)
                               2667                 :                :             {
                               2668                 :                :                 /*
                               2669                 :                :                  * We get here if we find a need to delete a column after
                               2670                 :                :                  * having already decided to drop its whole table.  Obviously
                               2671                 :                :                  * we no longer need to drop the subobject, so report that we
                               2672                 :                :                  * found the subobject in the array.  But don't plaster its
                               2673                 :                :                  * flags on the whole object.
                               2674                 :                :                  */
                               2675                 :            204 :                 result = true;
                               2676                 :                :             }
                               2677         [ +  + ]:             24 :             else if (object->objectSubId == 0)
                               2678                 :                :             {
                               2679                 :                :                 /*
                               2680                 :                :                  * We get here if we find a need to delete a whole table after
                               2681                 :                :                  * having already decided to drop one of its columns.  We
                               2682                 :                :                  * can't report that the whole object is in the array, but we
                               2683                 :                :                  * should mark the subobject with the whole object's flags.
                               2684                 :                :                  *
                               2685                 :                :                  * It might seem attractive to physically delete the column's
                               2686                 :                :                  * array entry, or at least mark it as no longer needing
                               2687                 :                :                  * separate deletion.  But that could lead to, e.g., dropping
                               2688                 :                :                  * the column's datatype before we drop the table, which does
                               2689                 :                :                  * not seem like a good idea.  This is a very rare situation
                               2690                 :                :                  * in practice, so we just take the hit of doing a separate
                               2691                 :                :                  * DROP COLUMN action even though we know we're gonna delete
                               2692                 :                :                  * the table later.
                               2693                 :                :                  *
                               2694                 :                :                  * What we can do, though, is mark this as a subobject so that
                               2695                 :                :                  * we don't report it separately, which is confusing because
                               2696                 :                :                  * it's unpredictable whether it happens or not.  But do so
                               2697                 :                :                  * only if flags != 0 (flags == 0 is a read-only probe).
                               2698                 :                :                  *
                               2699                 :                :                  * Because there could be other subobjects of this object in
                               2700                 :                :                  * the array, this case means we always have to loop through
                               2701                 :                :                  * the whole array; we cannot exit early on a match.
                               2702                 :                :                  */
                               2703                 :             18 :                 ObjectAddressExtra *thisextra = addrs->extras + i;
                               2704                 :                : 
 2423                          2705         [ +  - ]:             18 :                 if (flags)
                               2706                 :             18 :                     thisextra->flags |= (flags | DEPFLAG_SUBOBJECT);
                               2707                 :                :             }
                               2708                 :                :         }
                               2709                 :                :     }
                               2710                 :                : 
 3952                          2711                 :         129660 :     return result;
                               2712                 :                : }
                               2713                 :                : 
                               2714                 :                : /*
                               2715                 :                :  * Similar to above, except we search an ObjectAddressStack.
                               2716                 :                :  */
                               2717                 :                : static bool
 5127                          2718                 :         184496 : stack_address_present_add_flags(const ObjectAddress *object,
                               2719                 :                :                                 int flags,
                               2720                 :                :                                 ObjectAddressStack *stack)
                               2721                 :                : {
 3952                          2722                 :         184496 :     bool        result = false;
                               2723                 :                :     ObjectAddressStack *stackptr;
                               2724                 :                : 
 5127                          2725         [ +  + ]:         489798 :     for (stackptr = stack; stackptr; stackptr = stackptr->next)
                               2726                 :                :     {
                               2727                 :         305302 :         const ObjectAddress *thisobj = stackptr->object;
                               2728                 :                : 
                               2729         [ +  + ]:         305302 :         if (object->classId == thisobj->classId &&
                               2730         [ +  + ]:         135203 :             object->objectId == thisobj->objectId)
                               2731                 :                :         {
                               2732         [ +  + ]:          54872 :             if (object->objectSubId == thisobj->objectSubId)
                               2733                 :                :             {
                               2734                 :          54403 :                 stackptr->flags |= flags;
 3952                          2735                 :          54403 :                 result = true;
                               2736                 :                :             }
                               2737         [ +  + ]:            469 :             else if (thisobj->objectSubId == 0)
                               2738                 :                :             {
                               2739                 :                :                 /*
                               2740                 :                :                  * We're visiting a column with whole table already on stack.
                               2741                 :                :                  * As in object_address_present_add_flags(), we can skip
                               2742                 :                :                  * further processing of the subobject, but we don't want to
                               2743                 :                :                  * propagate flags for the subobject to the whole object.
                               2744                 :                :                  */
                               2745                 :            433 :                 result = true;
                               2746                 :                :             }
                               2747         [ -  + ]:             36 :             else if (object->objectSubId == 0)
                               2748                 :                :             {
                               2749                 :                :                 /*
                               2750                 :                :                  * We're visiting a table with column already on stack.  As in
                               2751                 :                :                  * object_address_present_add_flags(), we should propagate
                               2752                 :                :                  * flags for the whole object to each of its subobjects.
                               2753                 :                :                  */
 2423 tgl@sss.pgh.pa.us        2754         [ #  # ]:UBC           0 :                 if (flags)
                               2755                 :              0 :                     stackptr->flags |= (flags | DEPFLAG_SUBOBJECT);
                               2756                 :                :             }
                               2757                 :                :         }
                               2758                 :                :     }
                               2759                 :                : 
 3952 tgl@sss.pgh.pa.us        2760                 :CBC      184496 :     return result;
                               2761                 :                : }
                               2762                 :                : 
                               2763                 :                : /*
                               2764                 :                :  * Record multiple dependencies from an ObjectAddresses array, after first
                               2765                 :                :  * removing any duplicates.
                               2766                 :                :  */
                               2767                 :                : void
 6591                          2768                 :         195373 : record_object_address_dependencies(const ObjectAddress *depender,
                               2769                 :                :                                    ObjectAddresses *referenced,
                               2770                 :                :                                    DependencyType behavior)
                               2771                 :                : {
                               2772                 :         195373 :     eliminate_duplicate_dependencies(referenced);
                               2773                 :         195373 :     recordMultipleDependencies(depender,
 1769 tmunro@postgresql.or     2774                 :         195373 :                                referenced->refs, referenced->numrefs,
                               2775                 :                :                                behavior);
 6591 tgl@sss.pgh.pa.us        2776                 :         195373 : }
                               2777                 :                : 
                               2778                 :                : /*
                               2779                 :                :  * Sort the items in an ObjectAddresses array.
                               2780                 :                :  *
                               2781                 :                :  * The major sort key is OID-descending, so that newer objects will be listed
                               2782                 :                :  * first in most cases.  This is primarily useful for ensuring stable outputs
                               2783                 :                :  * from regression tests; it's not recommended if the order of the objects is
                               2784                 :                :  * determined by user input, such as the order of targets in a DROP command.
                               2785                 :                :  */
                               2786                 :                : void
 2362                          2787                 :             71 : sort_object_addresses(ObjectAddresses *addrs)
                               2788                 :                : {
                               2789         [ +  + ]:             71 :     if (addrs->numrefs > 1)
  942 peter@eisentraut.org     2790                 :             44 :         qsort(addrs->refs, addrs->numrefs,
                               2791                 :                :               sizeof(ObjectAddress),
                               2792                 :                :               object_address_comparator);
 2362 tgl@sss.pgh.pa.us        2793                 :             71 : }
                               2794                 :                : 
                               2795                 :                : /*
                               2796                 :                :  * Clean up when done with an ObjectAddresses array.
                               2797                 :                :  */
                               2798                 :                : void
 6957 alvherre@alvh.no-ip.     2799                 :         246515 : free_object_addresses(ObjectAddresses *addrs)
                               2800                 :                : {
 8453 tgl@sss.pgh.pa.us        2801                 :         246515 :     pfree(addrs->refs);
 6299                          2802         [ +  + ]:         246515 :     if (addrs->extras)
                               2803                 :          16055 :         pfree(addrs->extras);
 6957 alvherre@alvh.no-ip.     2804                 :         246515 :     pfree(addrs);
 8453 tgl@sss.pgh.pa.us        2805                 :         246515 : }
                               2806                 :                : 
                               2807                 :                : /*
                               2808                 :                :  * delete initial ACL for extension objects
                               2809                 :                :  */
                               2810                 :                : static void
 3440 sfrost@snowman.net       2811                 :         102086 : DeleteInitPrivs(const ObjectAddress *object)
                               2812                 :                : {
                               2813                 :                :     Relation    relation;
                               2814                 :                :     ScanKeyData key[3];
                               2815                 :                :     int         nkeys;
                               2816                 :                :     SysScanDesc scan;
                               2817                 :                :     HeapTuple   oldtuple;
                               2818                 :                : 
 2420 andres@anarazel.de       2819                 :         102086 :     relation = table_open(InitPrivsRelationId, RowExclusiveLock);
                               2820                 :                : 
 3440 sfrost@snowman.net       2821                 :         102086 :     ScanKeyInit(&key[0],
                               2822                 :                :                 Anum_pg_init_privs_objoid,
                               2823                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                               2824                 :         102086 :                 ObjectIdGetDatum(object->objectId));
                               2825                 :         102086 :     ScanKeyInit(&key[1],
                               2826                 :                :                 Anum_pg_init_privs_classoid,
                               2827                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                               2828                 :         102086 :                 ObjectIdGetDatum(object->classId));
  449 tgl@sss.pgh.pa.us        2829         [ +  + ]:         102086 :     if (object->objectSubId != 0)
                               2830                 :                :     {
                               2831                 :           1037 :         ScanKeyInit(&key[2],
                               2832                 :                :                     Anum_pg_init_privs_objsubid,
                               2833                 :                :                     BTEqualStrategyNumber, F_INT4EQ,
                               2834                 :           1037 :                     Int32GetDatum(object->objectSubId));
                               2835                 :           1037 :         nkeys = 3;
                               2836                 :                :     }
                               2837                 :                :     else
                               2838                 :         101049 :         nkeys = 2;
                               2839                 :                : 
 3440 sfrost@snowman.net       2840                 :         102086 :     scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
                               2841                 :                :                               NULL, nkeys, key);
                               2842                 :                : 
                               2843         [ +  + ]:         102148 :     while (HeapTupleIsValid(oldtuple = systable_getnext(scan)))
 3139 tgl@sss.pgh.pa.us        2844                 :             62 :         CatalogTupleDelete(relation, &oldtuple->t_self);
                               2845                 :                : 
 3440 sfrost@snowman.net       2846                 :         102086 :     systable_endscan(scan);
                               2847                 :                : 
 2420 andres@anarazel.de       2848                 :         102086 :     table_close(relation, RowExclusiveLock);
 3440 sfrost@snowman.net       2849                 :         102086 : }
        

Generated by: LCOV version 2.4-beta