LCOV - code coverage report
Current view: top level - src/backend/catalog - objectaddress.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 1632 1908 85.5 %
Date: 2019-11-22 07:06:56 Functions: 47 48 97.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * objectaddress.c
       4             :  *    functions for working with ObjectAddresses
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/catalog/objectaddress.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : 
      16             : #include "postgres.h"
      17             : 
      18             : #include "access/genam.h"
      19             : #include "access/htup_details.h"
      20             : #include "access/relation.h"
      21             : #include "access/sysattr.h"
      22             : #include "access/table.h"
      23             : #include "catalog/catalog.h"
      24             : #include "catalog/indexing.h"
      25             : #include "catalog/objectaddress.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_cast.h"
      32             : #include "catalog/pg_collation.h"
      33             : #include "catalog/pg_constraint.h"
      34             : #include "catalog/pg_conversion.h"
      35             : #include "catalog/pg_database.h"
      36             : #include "catalog/pg_default_acl.h"
      37             : #include "catalog/pg_enum.h"
      38             : #include "catalog/pg_event_trigger.h"
      39             : #include "catalog/pg_extension.h"
      40             : #include "catalog/pg_foreign_data_wrapper.h"
      41             : #include "catalog/pg_foreign_server.h"
      42             : #include "catalog/pg_language.h"
      43             : #include "catalog/pg_largeobject.h"
      44             : #include "catalog/pg_largeobject_metadata.h"
      45             : #include "catalog/pg_namespace.h"
      46             : #include "catalog/pg_opclass.h"
      47             : #include "catalog/pg_operator.h"
      48             : #include "catalog/pg_opfamily.h"
      49             : #include "catalog/pg_policy.h"
      50             : #include "catalog/pg_proc.h"
      51             : #include "catalog/pg_publication.h"
      52             : #include "catalog/pg_publication_rel.h"
      53             : #include "catalog/pg_rewrite.h"
      54             : #include "catalog/pg_statistic_ext.h"
      55             : #include "catalog/pg_subscription.h"
      56             : #include "catalog/pg_tablespace.h"
      57             : #include "catalog/pg_transform.h"
      58             : #include "catalog/pg_trigger.h"
      59             : #include "catalog/pg_ts_config.h"
      60             : #include "catalog/pg_ts_dict.h"
      61             : #include "catalog/pg_ts_parser.h"
      62             : #include "catalog/pg_ts_template.h"
      63             : #include "catalog/pg_type.h"
      64             : #include "catalog/pg_user_mapping.h"
      65             : #include "commands/dbcommands.h"
      66             : #include "commands/defrem.h"
      67             : #include "commands/event_trigger.h"
      68             : #include "commands/extension.h"
      69             : #include "commands/policy.h"
      70             : #include "commands/proclang.h"
      71             : #include "commands/tablespace.h"
      72             : #include "commands/trigger.h"
      73             : #include "foreign/foreign.h"
      74             : #include "funcapi.h"
      75             : #include "miscadmin.h"
      76             : #include "nodes/makefuncs.h"
      77             : #include "parser/parse_func.h"
      78             : #include "parser/parse_oper.h"
      79             : #include "parser/parse_type.h"
      80             : #include "rewrite/rewriteSupport.h"
      81             : #include "storage/large_object.h"
      82             : #include "storage/lmgr.h"
      83             : #include "storage/sinval.h"
      84             : #include "utils/builtins.h"
      85             : #include "utils/fmgroids.h"
      86             : #include "utils/lsyscache.h"
      87             : #include "utils/memutils.h"
      88             : #include "utils/regproc.h"
      89             : #include "utils/syscache.h"
      90             : 
      91             : /*
      92             :  * ObjectProperty
      93             :  *
      94             :  * This array provides a common part of system object structure; to help
      95             :  * consolidate routines to handle various kind of object classes.
      96             :  */
      97             : typedef struct
      98             : {
      99             :     Oid         class_oid;      /* oid of catalog */
     100             :     Oid         oid_index_oid;  /* oid of index on system oid column */
     101             :     int         oid_catcache_id;    /* id of catcache on system oid column  */
     102             :     int         name_catcache_id;   /* id of catcache on (name,namespace), or
     103             :                                      * (name) if the object does not live in a
     104             :                                      * namespace */
     105             :     AttrNumber  attnum_oid;     /* attribute number of oid column */
     106             :     AttrNumber  attnum_name;    /* attnum of name field */
     107             :     AttrNumber  attnum_namespace;   /* attnum of namespace field */
     108             :     AttrNumber  attnum_owner;   /* attnum of owner field */
     109             :     AttrNumber  attnum_acl;     /* attnum of acl field */
     110             :     ObjectType  objtype;        /* OBJECT_* of this object type */
     111             :     bool        is_nsp_name_unique; /* can the nsp/name combination (or name
     112             :                                      * alone, if there's no namespace) be
     113             :                                      * considered a unique identifier for an
     114             :                                      * object of this class? */
     115             : } ObjectPropertyType;
     116             : 
     117             : static const ObjectPropertyType ObjectProperty[] =
     118             : {
     119             :     {
     120             :         AccessMethodRelationId,
     121             :         AmOidIndexId,
     122             :         AMOID,
     123             :         AMNAME,
     124             :         Anum_pg_am_oid,
     125             :         Anum_pg_am_amname,
     126             :         InvalidAttrNumber,
     127             :         InvalidAttrNumber,
     128             :         InvalidAttrNumber,
     129             :         -1,
     130             :         true
     131             :     },
     132             :     {
     133             :         CastRelationId,
     134             :         CastOidIndexId,
     135             :         -1,
     136             :         -1,
     137             :         Anum_pg_cast_oid,
     138             :         InvalidAttrNumber,
     139             :         InvalidAttrNumber,
     140             :         InvalidAttrNumber,
     141             :         InvalidAttrNumber,
     142             :         -1,
     143             :         false
     144             :     },
     145             :     {
     146             :         CollationRelationId,
     147             :         CollationOidIndexId,
     148             :         COLLOID,
     149             :         -1,                     /* COLLNAMEENCNSP also takes encoding */
     150             :         Anum_pg_collation_oid,
     151             :         Anum_pg_collation_collname,
     152             :         Anum_pg_collation_collnamespace,
     153             :         Anum_pg_collation_collowner,
     154             :         InvalidAttrNumber,
     155             :         OBJECT_COLLATION,
     156             :         true
     157             :     },
     158             :     {
     159             :         ConstraintRelationId,
     160             :         ConstraintOidIndexId,
     161             :         CONSTROID,
     162             :         -1,
     163             :         Anum_pg_constraint_oid,
     164             :         Anum_pg_constraint_conname,
     165             :         Anum_pg_constraint_connamespace,
     166             :         InvalidAttrNumber,
     167             :         InvalidAttrNumber,
     168             :         -1,
     169             :         false
     170             :     },
     171             :     {
     172             :         ConversionRelationId,
     173             :         ConversionOidIndexId,
     174             :         CONVOID,
     175             :         CONNAMENSP,
     176             :         Anum_pg_conversion_oid,
     177             :         Anum_pg_conversion_conname,
     178             :         Anum_pg_conversion_connamespace,
     179             :         Anum_pg_conversion_conowner,
     180             :         InvalidAttrNumber,
     181             :         OBJECT_CONVERSION,
     182             :         true
     183             :     },
     184             :     {
     185             :         DatabaseRelationId,
     186             :         DatabaseOidIndexId,
     187             :         DATABASEOID,
     188             :         -1,
     189             :         Anum_pg_database_oid,
     190             :         Anum_pg_database_datname,
     191             :         InvalidAttrNumber,
     192             :         Anum_pg_database_datdba,
     193             :         Anum_pg_database_datacl,
     194             :         OBJECT_DATABASE,
     195             :         true
     196             :     },
     197             :     {
     198             :         ExtensionRelationId,
     199             :         ExtensionOidIndexId,
     200             :         -1,
     201             :         -1,
     202             :         Anum_pg_extension_oid,
     203             :         Anum_pg_extension_extname,
     204             :         InvalidAttrNumber,      /* extension doesn't belong to extnamespace */
     205             :         Anum_pg_extension_extowner,
     206             :         InvalidAttrNumber,
     207             :         OBJECT_EXTENSION,
     208             :         true
     209             :     },
     210             :     {
     211             :         ForeignDataWrapperRelationId,
     212             :         ForeignDataWrapperOidIndexId,
     213             :         FOREIGNDATAWRAPPEROID,
     214             :         FOREIGNDATAWRAPPERNAME,
     215             :         Anum_pg_foreign_data_wrapper_oid,
     216             :         Anum_pg_foreign_data_wrapper_fdwname,
     217             :         InvalidAttrNumber,
     218             :         Anum_pg_foreign_data_wrapper_fdwowner,
     219             :         Anum_pg_foreign_data_wrapper_fdwacl,
     220             :         OBJECT_FDW,
     221             :         true
     222             :     },
     223             :     {
     224             :         ForeignServerRelationId,
     225             :         ForeignServerOidIndexId,
     226             :         FOREIGNSERVEROID,
     227             :         FOREIGNSERVERNAME,
     228             :         Anum_pg_foreign_server_oid,
     229             :         Anum_pg_foreign_server_srvname,
     230             :         InvalidAttrNumber,
     231             :         Anum_pg_foreign_server_srvowner,
     232             :         Anum_pg_foreign_server_srvacl,
     233             :         OBJECT_FOREIGN_SERVER,
     234             :         true
     235             :     },
     236             :     {
     237             :         ProcedureRelationId,
     238             :         ProcedureOidIndexId,
     239             :         PROCOID,
     240             :         -1,                     /* PROCNAMEARGSNSP also takes argument types */
     241             :         Anum_pg_proc_oid,
     242             :         Anum_pg_proc_proname,
     243             :         Anum_pg_proc_pronamespace,
     244             :         Anum_pg_proc_proowner,
     245             :         Anum_pg_proc_proacl,
     246             :         OBJECT_FUNCTION,
     247             :         false
     248             :     },
     249             :     {
     250             :         LanguageRelationId,
     251             :         LanguageOidIndexId,
     252             :         LANGOID,
     253             :         LANGNAME,
     254             :         Anum_pg_language_oid,
     255             :         Anum_pg_language_lanname,
     256             :         InvalidAttrNumber,
     257             :         Anum_pg_language_lanowner,
     258             :         Anum_pg_language_lanacl,
     259             :         OBJECT_LANGUAGE,
     260             :         true
     261             :     },
     262             :     {
     263             :         LargeObjectMetadataRelationId,
     264             :         LargeObjectMetadataOidIndexId,
     265             :         -1,
     266             :         -1,
     267             :         Anum_pg_largeobject_metadata_oid,
     268             :         InvalidAttrNumber,
     269             :         InvalidAttrNumber,
     270             :         Anum_pg_largeobject_metadata_lomowner,
     271             :         Anum_pg_largeobject_metadata_lomacl,
     272             :         OBJECT_LARGEOBJECT,
     273             :         false
     274             :     },
     275             :     {
     276             :         OperatorClassRelationId,
     277             :         OpclassOidIndexId,
     278             :         CLAOID,
     279             :         -1,                     /* CLAAMNAMENSP also takes opcmethod */
     280             :         Anum_pg_opclass_oid,
     281             :         Anum_pg_opclass_opcname,
     282             :         Anum_pg_opclass_opcnamespace,
     283             :         Anum_pg_opclass_opcowner,
     284             :         InvalidAttrNumber,
     285             :         OBJECT_OPCLASS,
     286             :         true
     287             :     },
     288             :     {
     289             :         OperatorRelationId,
     290             :         OperatorOidIndexId,
     291             :         OPEROID,
     292             :         -1,                     /* OPERNAMENSP also takes left and right type */
     293             :         Anum_pg_operator_oid,
     294             :         Anum_pg_operator_oprname,
     295             :         Anum_pg_operator_oprnamespace,
     296             :         Anum_pg_operator_oprowner,
     297             :         InvalidAttrNumber,
     298             :         OBJECT_OPERATOR,
     299             :         false
     300             :     },
     301             :     {
     302             :         OperatorFamilyRelationId,
     303             :         OpfamilyOidIndexId,
     304             :         OPFAMILYOID,
     305             :         -1,                     /* OPFAMILYAMNAMENSP also takes opfmethod */
     306             :         Anum_pg_opfamily_oid,
     307             :         Anum_pg_opfamily_opfname,
     308             :         Anum_pg_opfamily_opfnamespace,
     309             :         Anum_pg_opfamily_opfowner,
     310             :         InvalidAttrNumber,
     311             :         OBJECT_OPFAMILY,
     312             :         true
     313             :     },
     314             :     {
     315             :         AuthIdRelationId,
     316             :         AuthIdOidIndexId,
     317             :         AUTHOID,
     318             :         AUTHNAME,
     319             :         Anum_pg_authid_oid,
     320             :         Anum_pg_authid_rolname,
     321             :         InvalidAttrNumber,
     322             :         InvalidAttrNumber,
     323             :         InvalidAttrNumber,
     324             :         -1,
     325             :         true
     326             :     },
     327             :     {
     328             :         RewriteRelationId,
     329             :         RewriteOidIndexId,
     330             :         -1,
     331             :         -1,
     332             :         Anum_pg_rewrite_oid,
     333             :         Anum_pg_rewrite_rulename,
     334             :         InvalidAttrNumber,
     335             :         InvalidAttrNumber,
     336             :         InvalidAttrNumber,
     337             :         -1,
     338             :         false
     339             :     },
     340             :     {
     341             :         NamespaceRelationId,
     342             :         NamespaceOidIndexId,
     343             :         NAMESPACEOID,
     344             :         NAMESPACENAME,
     345             :         Anum_pg_namespace_oid,
     346             :         Anum_pg_namespace_nspname,
     347             :         InvalidAttrNumber,
     348             :         Anum_pg_namespace_nspowner,
     349             :         Anum_pg_namespace_nspacl,
     350             :         OBJECT_SCHEMA,
     351             :         true
     352             :     },
     353             :     {
     354             :         RelationRelationId,
     355             :         ClassOidIndexId,
     356             :         RELOID,
     357             :         RELNAMENSP,
     358             :         Anum_pg_class_oid,
     359             :         Anum_pg_class_relname,
     360             :         Anum_pg_class_relnamespace,
     361             :         Anum_pg_class_relowner,
     362             :         Anum_pg_class_relacl,
     363             :         OBJECT_TABLE,
     364             :         true
     365             :     },
     366             :     {
     367             :         TableSpaceRelationId,
     368             :         TablespaceOidIndexId,
     369             :         TABLESPACEOID,
     370             :         -1,
     371             :         Anum_pg_tablespace_oid,
     372             :         Anum_pg_tablespace_spcname,
     373             :         InvalidAttrNumber,
     374             :         Anum_pg_tablespace_spcowner,
     375             :         Anum_pg_tablespace_spcacl,
     376             :         OBJECT_TABLESPACE,
     377             :         true
     378             :     },
     379             :     {
     380             :         TransformRelationId,
     381             :         TransformOidIndexId,
     382             :         TRFOID,
     383             :         InvalidAttrNumber,
     384             :         Anum_pg_transform_oid
     385             :     },
     386             :     {
     387             :         TriggerRelationId,
     388             :         TriggerOidIndexId,
     389             :         -1,
     390             :         -1,
     391             :         Anum_pg_trigger_oid,
     392             :         Anum_pg_trigger_tgname,
     393             :         InvalidAttrNumber,
     394             :         InvalidAttrNumber,
     395             :         InvalidAttrNumber,
     396             :         -1,
     397             :         false
     398             :     },
     399             :     {
     400             :         PolicyRelationId,
     401             :         PolicyOidIndexId,
     402             :         -1,
     403             :         -1,
     404             :         Anum_pg_policy_oid,
     405             :         Anum_pg_policy_polname,
     406             :         InvalidAttrNumber,
     407             :         InvalidAttrNumber,
     408             :         InvalidAttrNumber,
     409             :         -1,
     410             :         false
     411             :     },
     412             :     {
     413             :         EventTriggerRelationId,
     414             :         EventTriggerOidIndexId,
     415             :         EVENTTRIGGEROID,
     416             :         EVENTTRIGGERNAME,
     417             :         Anum_pg_event_trigger_oid,
     418             :         Anum_pg_event_trigger_evtname,
     419             :         InvalidAttrNumber,
     420             :         Anum_pg_event_trigger_evtowner,
     421             :         InvalidAttrNumber,
     422             :         OBJECT_EVENT_TRIGGER,
     423             :         true
     424             :     },
     425             :     {
     426             :         TSConfigRelationId,
     427             :         TSConfigOidIndexId,
     428             :         TSCONFIGOID,
     429             :         TSCONFIGNAMENSP,
     430             :         Anum_pg_ts_config_oid,
     431             :         Anum_pg_ts_config_cfgname,
     432             :         Anum_pg_ts_config_cfgnamespace,
     433             :         Anum_pg_ts_config_cfgowner,
     434             :         InvalidAttrNumber,
     435             :         OBJECT_TSCONFIGURATION,
     436             :         true
     437             :     },
     438             :     {
     439             :         TSDictionaryRelationId,
     440             :         TSDictionaryOidIndexId,
     441             :         TSDICTOID,
     442             :         TSDICTNAMENSP,
     443             :         Anum_pg_ts_dict_oid,
     444             :         Anum_pg_ts_dict_dictname,
     445             :         Anum_pg_ts_dict_dictnamespace,
     446             :         Anum_pg_ts_dict_dictowner,
     447             :         InvalidAttrNumber,
     448             :         OBJECT_TSDICTIONARY,
     449             :         true
     450             :     },
     451             :     {
     452             :         TSParserRelationId,
     453             :         TSParserOidIndexId,
     454             :         TSPARSEROID,
     455             :         TSPARSERNAMENSP,
     456             :         Anum_pg_ts_parser_oid,
     457             :         Anum_pg_ts_parser_prsname,
     458             :         Anum_pg_ts_parser_prsnamespace,
     459             :         InvalidAttrNumber,
     460             :         InvalidAttrNumber,
     461             :         -1,
     462             :         true
     463             :     },
     464             :     {
     465             :         TSTemplateRelationId,
     466             :         TSTemplateOidIndexId,
     467             :         TSTEMPLATEOID,
     468             :         TSTEMPLATENAMENSP,
     469             :         Anum_pg_ts_template_oid,
     470             :         Anum_pg_ts_template_tmplname,
     471             :         Anum_pg_ts_template_tmplnamespace,
     472             :         InvalidAttrNumber,
     473             :         InvalidAttrNumber,
     474             :         -1,
     475             :         true,
     476             :     },
     477             :     {
     478             :         TypeRelationId,
     479             :         TypeOidIndexId,
     480             :         TYPEOID,
     481             :         TYPENAMENSP,
     482             :         Anum_pg_type_oid,
     483             :         Anum_pg_type_typname,
     484             :         Anum_pg_type_typnamespace,
     485             :         Anum_pg_type_typowner,
     486             :         Anum_pg_type_typacl,
     487             :         OBJECT_TYPE,
     488             :         true
     489             :     },
     490             :     {
     491             :         PublicationRelationId,
     492             :         PublicationObjectIndexId,
     493             :         PUBLICATIONOID,
     494             :         PUBLICATIONNAME,
     495             :         Anum_pg_publication_oid,
     496             :         Anum_pg_publication_pubname,
     497             :         InvalidAttrNumber,
     498             :         Anum_pg_publication_pubowner,
     499             :         InvalidAttrNumber,
     500             :         OBJECT_PUBLICATION,
     501             :         true
     502             :     },
     503             :     {
     504             :         SubscriptionRelationId,
     505             :         SubscriptionObjectIndexId,
     506             :         SUBSCRIPTIONOID,
     507             :         SUBSCRIPTIONNAME,
     508             :         Anum_pg_subscription_oid,
     509             :         Anum_pg_subscription_subname,
     510             :         InvalidAttrNumber,
     511             :         Anum_pg_subscription_subowner,
     512             :         InvalidAttrNumber,
     513             :         OBJECT_SUBSCRIPTION,
     514             :         true
     515             :     },
     516             :     {
     517             :         StatisticExtRelationId,
     518             :         StatisticExtOidIndexId,
     519             :         STATEXTOID,
     520             :         STATEXTNAMENSP,
     521             :         Anum_pg_statistic_ext_oid,
     522             :         Anum_pg_statistic_ext_stxname,
     523             :         Anum_pg_statistic_ext_stxnamespace,
     524             :         Anum_pg_statistic_ext_stxowner,
     525             :         InvalidAttrNumber,      /* no ACL (same as relation) */
     526             :         OBJECT_STATISTIC_EXT,
     527             :         true
     528             :     }
     529             : };
     530             : 
     531             : /*
     532             :  * This struct maps the string object types as returned by
     533             :  * getObjectTypeDescription into ObjectType enum values.  Note that some enum
     534             :  * values can be obtained by different names, and that some string object types
     535             :  * do not have corresponding values in the output enum.  The user of this map
     536             :  * must be careful to test for invalid values being returned.
     537             :  *
     538             :  * To ease maintenance, this follows the order of getObjectTypeDescription.
     539             :  */
     540             : static const struct object_type_map
     541             : {
     542             :     const char *tm_name;
     543             :     ObjectType  tm_type;
     544             : }
     545             : 
     546             :             ObjectTypeMap[] =
     547             : {
     548             :     /* OCLASS_CLASS, all kinds of relations */
     549             :     {
     550             :         "table", OBJECT_TABLE
     551             :     },
     552             :     {
     553             :         "index", OBJECT_INDEX
     554             :     },
     555             :     {
     556             :         "sequence", OBJECT_SEQUENCE
     557             :     },
     558             :     {
     559             :         "toast table", -1
     560             :     },                          /* unmapped */
     561             :     {
     562             :         "view", OBJECT_VIEW
     563             :     },
     564             :     {
     565             :         "materialized view", OBJECT_MATVIEW
     566             :     },
     567             :     {
     568             :         "composite type", -1
     569             :     },                          /* unmapped */
     570             :     {
     571             :         "foreign table", OBJECT_FOREIGN_TABLE
     572             :     },
     573             :     {
     574             :         "table column", OBJECT_COLUMN
     575             :     },
     576             :     {
     577             :         "index column", -1
     578             :     },                          /* unmapped */
     579             :     {
     580             :         "sequence column", -1
     581             :     },                          /* unmapped */
     582             :     {
     583             :         "toast table column", -1
     584             :     },                          /* unmapped */
     585             :     {
     586             :         "view column", -1
     587             :     },                          /* unmapped */
     588             :     {
     589             :         "materialized view column", -1
     590             :     },                          /* unmapped */
     591             :     {
     592             :         "composite type column", -1
     593             :     },                          /* unmapped */
     594             :     {
     595             :         "foreign table column", OBJECT_COLUMN
     596             :     },
     597             :     /* OCLASS_PROC */
     598             :     {
     599             :         "aggregate", OBJECT_AGGREGATE
     600             :     },
     601             :     {
     602             :         "function", OBJECT_FUNCTION
     603             :     },
     604             :     {
     605             :         "procedure", OBJECT_PROCEDURE
     606             :     },
     607             :     /* OCLASS_TYPE */
     608             :     {
     609             :         "type", OBJECT_TYPE
     610             :     },
     611             :     /* OCLASS_CAST */
     612             :     {
     613             :         "cast", OBJECT_CAST
     614             :     },
     615             :     /* OCLASS_COLLATION */
     616             :     {
     617             :         "collation", OBJECT_COLLATION
     618             :     },
     619             :     /* OCLASS_CONSTRAINT */
     620             :     {
     621             :         "table constraint", OBJECT_TABCONSTRAINT
     622             :     },
     623             :     {
     624             :         "domain constraint", OBJECT_DOMCONSTRAINT
     625             :     },
     626             :     /* OCLASS_CONVERSION */
     627             :     {
     628             :         "conversion", OBJECT_CONVERSION
     629             :     },
     630             :     /* OCLASS_DEFAULT */
     631             :     {
     632             :         "default value", OBJECT_DEFAULT
     633             :     },
     634             :     /* OCLASS_LANGUAGE */
     635             :     {
     636             :         "language", OBJECT_LANGUAGE
     637             :     },
     638             :     /* OCLASS_LARGEOBJECT */
     639             :     {
     640             :         "large object", OBJECT_LARGEOBJECT
     641             :     },
     642             :     /* OCLASS_OPERATOR */
     643             :     {
     644             :         "operator", OBJECT_OPERATOR
     645             :     },
     646             :     /* OCLASS_OPCLASS */
     647             :     {
     648             :         "operator class", OBJECT_OPCLASS
     649             :     },
     650             :     /* OCLASS_OPFAMILY */
     651             :     {
     652             :         "operator family", OBJECT_OPFAMILY
     653             :     },
     654             :     /* OCLASS_AM */
     655             :     {
     656             :         "access method", OBJECT_ACCESS_METHOD
     657             :     },
     658             :     /* OCLASS_AMOP */
     659             :     {
     660             :         "operator of access method", OBJECT_AMOP
     661             :     },
     662             :     /* OCLASS_AMPROC */
     663             :     {
     664             :         "function of access method", OBJECT_AMPROC
     665             :     },
     666             :     /* OCLASS_REWRITE */
     667             :     {
     668             :         "rule", OBJECT_RULE
     669             :     },
     670             :     /* OCLASS_TRIGGER */
     671             :     {
     672             :         "trigger", OBJECT_TRIGGER
     673             :     },
     674             :     /* OCLASS_SCHEMA */
     675             :     {
     676             :         "schema", OBJECT_SCHEMA
     677             :     },
     678             :     /* OCLASS_TSPARSER */
     679             :     {
     680             :         "text search parser", OBJECT_TSPARSER
     681             :     },
     682             :     /* OCLASS_TSDICT */
     683             :     {
     684             :         "text search dictionary", OBJECT_TSDICTIONARY
     685             :     },
     686             :     /* OCLASS_TSTEMPLATE */
     687             :     {
     688             :         "text search template", OBJECT_TSTEMPLATE
     689             :     },
     690             :     /* OCLASS_TSCONFIG */
     691             :     {
     692             :         "text search configuration", OBJECT_TSCONFIGURATION
     693             :     },
     694             :     /* OCLASS_ROLE */
     695             :     {
     696             :         "role", OBJECT_ROLE
     697             :     },
     698             :     /* OCLASS_DATABASE */
     699             :     {
     700             :         "database", OBJECT_DATABASE
     701             :     },
     702             :     /* OCLASS_TBLSPACE */
     703             :     {
     704             :         "tablespace", OBJECT_TABLESPACE
     705             :     },
     706             :     /* OCLASS_FDW */
     707             :     {
     708             :         "foreign-data wrapper", OBJECT_FDW
     709             :     },
     710             :     /* OCLASS_FOREIGN_SERVER */
     711             :     {
     712             :         "server", OBJECT_FOREIGN_SERVER
     713             :     },
     714             :     /* OCLASS_USER_MAPPING */
     715             :     {
     716             :         "user mapping", OBJECT_USER_MAPPING
     717             :     },
     718             :     /* OCLASS_DEFACL */
     719             :     {
     720             :         "default acl", OBJECT_DEFACL
     721             :     },
     722             :     /* OCLASS_EXTENSION */
     723             :     {
     724             :         "extension", OBJECT_EXTENSION
     725             :     },
     726             :     /* OCLASS_EVENT_TRIGGER */
     727             :     {
     728             :         "event trigger", OBJECT_EVENT_TRIGGER
     729             :     },
     730             :     /* OCLASS_POLICY */
     731             :     {
     732             :         "policy", OBJECT_POLICY
     733             :     },
     734             :     /* OCLASS_PUBLICATION */
     735             :     {
     736             :         "publication", OBJECT_PUBLICATION
     737             :     },
     738             :     /* OCLASS_PUBLICATION_REL */
     739             :     {
     740             :         "publication relation", OBJECT_PUBLICATION_REL
     741             :     },
     742             :     /* OCLASS_SUBSCRIPTION */
     743             :     {
     744             :         "subscription", OBJECT_SUBSCRIPTION
     745             :     },
     746             :     /* OCLASS_TRANSFORM */
     747             :     {
     748             :         "transform", OBJECT_TRANSFORM
     749             :     },
     750             :     /* OBJECT_STATISTIC_EXT */
     751             :     {
     752             :         "statistics object", OBJECT_STATISTIC_EXT
     753             :     }
     754             : };
     755             : 
     756             : const ObjectAddress InvalidObjectAddress =
     757             : {
     758             :     InvalidOid,
     759             :     InvalidOid,
     760             :     0
     761             : };
     762             : 
     763             : static ObjectAddress get_object_address_unqualified(ObjectType objtype,
     764             :                                                     Value *strval, bool missing_ok);
     765             : static ObjectAddress get_relation_by_qualified_name(ObjectType objtype,
     766             :                                                     List *object, Relation *relp,
     767             :                                                     LOCKMODE lockmode, bool missing_ok);
     768             : static ObjectAddress get_object_address_relobject(ObjectType objtype,
     769             :                                                   List *object, Relation *relp, bool missing_ok);
     770             : static ObjectAddress get_object_address_attribute(ObjectType objtype,
     771             :                                                   List *object, Relation *relp,
     772             :                                                   LOCKMODE lockmode, bool missing_ok);
     773             : static ObjectAddress get_object_address_attrdef(ObjectType objtype,
     774             :                                                 List *object, Relation *relp, LOCKMODE lockmode,
     775             :                                                 bool missing_ok);
     776             : static ObjectAddress get_object_address_type(ObjectType objtype,
     777             :                                              TypeName *typename, bool missing_ok);
     778             : static ObjectAddress get_object_address_opcf(ObjectType objtype, List *object,
     779             :                                              bool missing_ok);
     780             : static ObjectAddress get_object_address_opf_member(ObjectType objtype,
     781             :                                                    List *object, bool missing_ok);
     782             : 
     783             : static ObjectAddress get_object_address_usermapping(List *object,
     784             :                                                     bool missing_ok);
     785             : static ObjectAddress get_object_address_publication_rel(List *object,
     786             :                                                         Relation *relp,
     787             :                                                         bool missing_ok);
     788             : static ObjectAddress get_object_address_defacl(List *object,
     789             :                                                bool missing_ok);
     790             : static const ObjectPropertyType *get_object_property_data(Oid class_id);
     791             : 
     792             : static void getRelationDescription(StringInfo buffer, Oid relid);
     793             : static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
     794             : static void getRelationTypeDescription(StringInfo buffer, Oid relid,
     795             :                                        int32 objectSubId);
     796             : static void getProcedureTypeDescription(StringInfo buffer, Oid procid);
     797             : static void getConstraintTypeDescription(StringInfo buffer, Oid constroid);
     798             : static void getOpFamilyIdentity(StringInfo buffer, Oid opfid, List **object);
     799             : static void getRelationIdentity(StringInfo buffer, Oid relid, List **object);
     800             : 
     801             : /*
     802             :  * Translate an object name and arguments (as passed by the parser) to an
     803             :  * ObjectAddress.
     804             :  *
     805             :  * The returned object will be locked using the specified lockmode.  If a
     806             :  * sub-object is looked up, the parent object will be locked instead.
     807             :  *
     808             :  * If the object is a relation or a child object of a relation (e.g. an
     809             :  * attribute or constraint), the relation is also opened and *relp receives
     810             :  * the open relcache entry pointer; otherwise, *relp is set to NULL.  This
     811             :  * is a bit grotty but it makes life simpler, since the caller will
     812             :  * typically need the relcache entry too.  Caller must close the relcache
     813             :  * entry when done with it.  The relation is locked with the specified lockmode
     814             :  * if the target object is the relation itself or an attribute, but for other
     815             :  * child objects, only AccessShareLock is acquired on the relation.
     816             :  *
     817             :  * If the object is not found, an error is thrown, unless missing_ok is
     818             :  * true.  In this case, no lock is acquired, relp is set to NULL, and the
     819             :  * returned address has objectId set to InvalidOid.
     820             :  *
     821             :  * We don't currently provide a function to release the locks acquired here;
     822             :  * typically, the lock must be held until commit to guard against a concurrent
     823             :  * drop operation.
     824             :  *
     825             :  * Note: If the object is not found, we don't give any indication of the
     826             :  * reason.  (It might have been a missing schema if the name was qualified, or
     827             :  * a nonexistent type name in case of a cast, function or operator; etc).
     828             :  * Currently there is only one caller that might be interested in such info, so
     829             :  * we don't spend much effort here.  If more callers start to care, it might be
     830             :  * better to add some support for that in this function.
     831             :  */
     832             : ObjectAddress
     833       23598 : get_object_address(ObjectType objtype, Node *object,
     834             :                    Relation *relp, LOCKMODE lockmode, bool missing_ok)
     835             : {
     836             :     ObjectAddress address;
     837       23598 :     ObjectAddress old_address = {InvalidOid, InvalidOid, 0};
     838       23598 :     Relation    relation = NULL;
     839             :     uint64      inval_count;
     840             : 
     841             :     /* Some kind of lock must be taken. */
     842             :     Assert(lockmode != NoLock);
     843             : 
     844             :     for (;;)
     845             :     {
     846             :         /*
     847             :          * Remember this value, so that, after looking up the object name and
     848             :          * locking it, we can check whether any invalidation messages have
     849             :          * been processed that might require a do-over.
     850             :          */
     851       23738 :         inval_count = SharedInvalidMessageCounter;
     852             : 
     853             :         /* Look up object address. */
     854       23668 :         switch (objtype)
     855             :         {
     856             :             case OBJECT_INDEX:
     857             :             case OBJECT_SEQUENCE:
     858             :             case OBJECT_TABLE:
     859             :             case OBJECT_VIEW:
     860             :             case OBJECT_MATVIEW:
     861             :             case OBJECT_FOREIGN_TABLE:
     862         382 :                 address =
     863         382 :                     get_relation_by_qualified_name(objtype, castNode(List, object),
     864             :                                                    &relation, lockmode,
     865             :                                                    missing_ok);
     866         220 :                 break;
     867             :             case OBJECT_COLUMN:
     868         180 :                 address =
     869         180 :                     get_object_address_attribute(objtype, castNode(List, object),
     870             :                                                  &relation, lockmode,
     871             :                                                  missing_ok);
     872         118 :                 break;
     873             :             case OBJECT_DEFAULT:
     874          32 :                 address =
     875          32 :                     get_object_address_attrdef(objtype, castNode(List, object),
     876             :                                                &relation, lockmode,
     877             :                                                missing_ok);
     878           8 :                 break;
     879             :             case OBJECT_RULE:
     880             :             case OBJECT_TRIGGER:
     881             :             case OBJECT_TABCONSTRAINT:
     882             :             case OBJECT_POLICY:
     883         992 :                 address = get_object_address_relobject(objtype, castNode(List, object),
     884             :                                                        &relation, missing_ok);
     885         844 :                 break;
     886             :             case OBJECT_DOMCONSTRAINT:
     887             :                 {
     888             :                     List       *objlist;
     889             :                     ObjectAddress domaddr;
     890             :                     char       *constrname;
     891             : 
     892          42 :                     objlist = castNode(List, object);
     893          84 :                     domaddr = get_object_address_type(OBJECT_DOMAIN,
     894          42 :                                                       linitial_node(TypeName, objlist),
     895             :                                                       missing_ok);
     896          34 :                     constrname = strVal(lsecond(objlist));
     897             : 
     898          34 :                     address.classId = ConstraintRelationId;
     899          34 :                     address.objectId = get_domain_constraint_oid(domaddr.objectId,
     900             :                                                                  constrname, missing_ok);
     901          30 :                     address.objectSubId = 0;
     902             : 
     903             :                 }
     904          30 :                 break;
     905             :             case OBJECT_DATABASE:
     906             :             case OBJECT_EXTENSION:
     907             :             case OBJECT_TABLESPACE:
     908             :             case OBJECT_ROLE:
     909             :             case OBJECT_SCHEMA:
     910             :             case OBJECT_LANGUAGE:
     911             :             case OBJECT_FDW:
     912             :             case OBJECT_FOREIGN_SERVER:
     913             :             case OBJECT_EVENT_TRIGGER:
     914             :             case OBJECT_ACCESS_METHOD:
     915             :             case OBJECT_PUBLICATION:
     916             :             case OBJECT_SUBSCRIPTION:
     917        1904 :                 address = get_object_address_unqualified(objtype,
     918             :                                                          (Value *) object, missing_ok);
     919        1814 :                 break;
     920             :             case OBJECT_TYPE:
     921             :             case OBJECT_DOMAIN:
     922         886 :                 address = get_object_address_type(objtype, castNode(TypeName, object), missing_ok);
     923         842 :                 break;
     924             :             case OBJECT_AGGREGATE:
     925             :             case OBJECT_FUNCTION:
     926             :             case OBJECT_PROCEDURE:
     927             :             case OBJECT_ROUTINE:
     928        3478 :                 address.classId = ProcedureRelationId;
     929        3478 :                 address.objectId = LookupFuncWithArgs(objtype, castNode(ObjectWithArgs, object), missing_ok);
     930        3294 :                 address.objectSubId = 0;
     931        3294 :                 break;
     932             :             case OBJECT_OPERATOR:
     933         162 :                 address.classId = OperatorRelationId;
     934         162 :                 address.objectId = LookupOperWithArgs(castNode(ObjectWithArgs, object), missing_ok);
     935         142 :                 address.objectSubId = 0;
     936         142 :                 break;
     937             :             case OBJECT_COLLATION:
     938          28 :                 address.classId = CollationRelationId;
     939          28 :                 address.objectId = get_collation_oid(castNode(List, object), missing_ok);
     940          28 :                 address.objectSubId = 0;
     941          28 :                 break;
     942             :             case OBJECT_CONVERSION:
     943         118 :                 address.classId = ConversionRelationId;
     944         118 :                 address.objectId = get_conversion_oid(castNode(List, object), missing_ok);
     945          86 :                 address.objectSubId = 0;
     946          86 :                 break;
     947             :             case OBJECT_OPCLASS:
     948             :             case OBJECT_OPFAMILY:
     949         296 :                 address = get_object_address_opcf(objtype, castNode(List, object), missing_ok);
     950         240 :                 break;
     951             :             case OBJECT_AMOP:
     952             :             case OBJECT_AMPROC:
     953          32 :                 address = get_object_address_opf_member(objtype, castNode(List, object), missing_ok);
     954          16 :                 break;
     955             :             case OBJECT_LARGEOBJECT:
     956          32 :                 address.classId = LargeObjectRelationId;
     957          32 :                 address.objectId = oidparse(object);
     958          28 :                 address.objectSubId = 0;
     959          28 :                 if (!LargeObjectExists(address.objectId))
     960             :                 {
     961           4 :                     if (!missing_ok)
     962           4 :                         ereport(ERROR,
     963             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
     964             :                                  errmsg("large object %u does not exist",
     965             :                                         address.objectId)));
     966             :                 }
     967          24 :                 break;
     968             :             case OBJECT_CAST:
     969             :                 {
     970          52 :                     TypeName   *sourcetype = linitial_node(TypeName, castNode(List, object));
     971          52 :                     TypeName   *targettype = lsecond_node(TypeName, castNode(List, object));
     972             :                     Oid         sourcetypeid;
     973             :                     Oid         targettypeid;
     974             : 
     975          52 :                     sourcetypeid = LookupTypeNameOid(NULL, sourcetype, missing_ok);
     976          48 :                     targettypeid = LookupTypeNameOid(NULL, targettype, missing_ok);
     977          48 :                     address.classId = CastRelationId;
     978          44 :                     address.objectId =
     979          48 :                         get_cast_oid(sourcetypeid, targettypeid, missing_ok);
     980          44 :                     address.objectSubId = 0;
     981             :                 }
     982          44 :                 break;
     983             :             case OBJECT_TRANSFORM:
     984             :                 {
     985          44 :                     TypeName   *typename = linitial_node(TypeName, castNode(List, object));
     986          44 :                     char       *langname = strVal(lsecond(castNode(List, object)));
     987          44 :                     Oid         type_id = LookupTypeNameOid(NULL, typename, missing_ok);
     988          38 :                     Oid         lang_id = get_language_oid(langname, missing_ok);
     989             : 
     990          36 :                     address.classId = TransformRelationId;
     991          36 :                     address.objectId =
     992          36 :                         get_transform_oid(type_id, lang_id, missing_ok);
     993          36 :                     address.objectSubId = 0;
     994             :                 }
     995          36 :                 break;
     996             :             case OBJECT_TSPARSER:
     997          66 :                 address.classId = TSParserRelationId;
     998          66 :                 address.objectId = get_ts_parser_oid(castNode(List, object), missing_ok);
     999          38 :                 address.objectSubId = 0;
    1000          38 :                 break;
    1001             :             case OBJECT_TSDICTIONARY:
    1002        7204 :                 address.classId = TSDictionaryRelationId;
    1003        7204 :                 address.objectId = get_ts_dict_oid(castNode(List, object), missing_ok);
    1004        7176 :                 address.objectSubId = 0;
    1005        7176 :                 break;
    1006             :             case OBJECT_TSTEMPLATE:
    1007         388 :                 address.classId = TSTemplateRelationId;
    1008         388 :                 address.objectId = get_ts_template_oid(castNode(List, object), missing_ok);
    1009         360 :                 address.objectSubId = 0;
    1010         360 :                 break;
    1011             :             case OBJECT_TSCONFIGURATION:
    1012        7190 :                 address.classId = TSConfigRelationId;
    1013        7190 :                 address.objectId = get_ts_config_oid(castNode(List, object), missing_ok);
    1014        7162 :                 address.objectSubId = 0;
    1015        7162 :                 break;
    1016             :             case OBJECT_USER_MAPPING:
    1017          20 :                 address = get_object_address_usermapping(castNode(List, object),
    1018             :                                                          missing_ok);
    1019           8 :                 break;
    1020             :             case OBJECT_PUBLICATION_REL:
    1021          20 :                 address = get_object_address_publication_rel(castNode(List, object),
    1022             :                                                              &relation,
    1023             :                                                              missing_ok);
    1024           8 :                 break;
    1025             :             case OBJECT_DEFACL:
    1026          28 :                 address = get_object_address_defacl(castNode(List, object),
    1027             :                                                     missing_ok);
    1028          16 :                 break;
    1029             :             case OBJECT_STATISTIC_EXT:
    1030          92 :                 address.classId = StatisticExtRelationId;
    1031          92 :                 address.objectId = get_statistics_object_oid(castNode(List, object),
    1032             :                                                              missing_ok);
    1033          92 :                 address.objectSubId = 0;
    1034          92 :                 break;
    1035             :             default:
    1036           0 :                 elog(ERROR, "unrecognized objtype: %d", (int) objtype);
    1037             :                 /* placate compiler, in case it thinks elog might return */
    1038             :                 address.classId = InvalidOid;
    1039             :                 address.objectId = InvalidOid;
    1040             :                 address.objectSubId = 0;
    1041             :         }
    1042             : 
    1043             :         /*
    1044             :          * If we could not find the supplied object, return without locking.
    1045             :          */
    1046       22646 :         if (!OidIsValid(address.objectId))
    1047             :         {
    1048             :             Assert(missing_ok);
    1049         248 :             return address;
    1050             :         }
    1051             : 
    1052             :         /*
    1053             :          * If we're retrying, see if we got the same answer as last time.  If
    1054             :          * so, we're done; if not, we locked the wrong thing, so give up our
    1055             :          * lock.
    1056             :          */
    1057       22398 :         if (OidIsValid(old_address.classId))
    1058             :         {
    1059          70 :             if (old_address.classId == address.classId
    1060          70 :                 && old_address.objectId == address.objectId
    1061          70 :                 && old_address.objectSubId == address.objectSubId)
    1062          70 :                 break;
    1063           0 :             if (old_address.classId != RelationRelationId)
    1064             :             {
    1065           0 :                 if (IsSharedRelation(old_address.classId))
    1066           0 :                     UnlockSharedObject(old_address.classId,
    1067             :                                        old_address.objectId,
    1068             :                                        0, lockmode);
    1069             :                 else
    1070           0 :                     UnlockDatabaseObject(old_address.classId,
    1071             :                                          old_address.objectId,
    1072             :                                          0, lockmode);
    1073             :             }
    1074             :         }
    1075             : 
    1076             :         /*
    1077             :          * If we're dealing with a relation or attribute, then the relation is
    1078             :          * already locked.  Otherwise, we lock it now.
    1079             :          */
    1080       22328 :         if (address.classId != RelationRelationId)
    1081             :         {
    1082       21990 :             if (IsSharedRelation(address.classId))
    1083         702 :                 LockSharedObject(address.classId, address.objectId, 0,
    1084             :                                  lockmode);
    1085             :             else
    1086       21288 :                 LockDatabaseObject(address.classId, address.objectId, 0,
    1087             :                                    lockmode);
    1088             :         }
    1089             : 
    1090             :         /*
    1091             :          * At this point, we've resolved the name to an OID and locked the
    1092             :          * corresponding database object.  However, it's possible that by the
    1093             :          * time we acquire the lock on the object, concurrent DDL has modified
    1094             :          * the database in such a way that the name we originally looked up no
    1095             :          * longer resolves to that OID.
    1096             :          *
    1097             :          * We can be certain that this isn't an issue if (a) no shared
    1098             :          * invalidation messages have been processed or (b) we've locked a
    1099             :          * relation somewhere along the line.  All the relation name lookups
    1100             :          * in this module ultimately use RangeVarGetRelid() to acquire a
    1101             :          * relation lock, and that function protects against the same kinds of
    1102             :          * races we're worried about here.  Even when operating on a
    1103             :          * constraint, rule, or trigger, we still acquire AccessShareLock on
    1104             :          * the relation, which is enough to freeze out any concurrent DDL.
    1105             :          *
    1106             :          * In all other cases, however, it's possible that the name we looked
    1107             :          * up no longer refers to the object we locked, so we retry the lookup
    1108             :          * and see whether we get the same answer.
    1109             :          */
    1110       22328 :         if (inval_count == SharedInvalidMessageCounter || relation != NULL)
    1111             :             break;
    1112          70 :         old_address = address;
    1113             :     }
    1114             : 
    1115             :     /* Return the object address and the relation. */
    1116       22328 :     *relp = relation;
    1117       22328 :     return address;
    1118             : }
    1119             : 
    1120             : /*
    1121             :  * Return an ObjectAddress based on a RangeVar and an object name. The
    1122             :  * name of the relation identified by the RangeVar is prepended to the
    1123             :  * (possibly empty) list passed in as object. This is useful to find
    1124             :  * the ObjectAddress of objects that depend on a relation. All other
    1125             :  * considerations are exactly as for get_object_address above.
    1126             :  */
    1127             : ObjectAddress
    1128          24 : get_object_address_rv(ObjectType objtype, RangeVar *rel, List *object,
    1129             :                       Relation *relp, LOCKMODE lockmode,
    1130             :                       bool missing_ok)
    1131             : {
    1132          24 :     if (rel)
    1133             :     {
    1134          18 :         object = lcons(makeString(rel->relname), object);
    1135          18 :         if (rel->schemaname)
    1136           0 :             object = lcons(makeString(rel->schemaname), object);
    1137          18 :         if (rel->catalogname)
    1138           0 :             object = lcons(makeString(rel->catalogname), object);
    1139             :     }
    1140             : 
    1141          24 :     return get_object_address(objtype, (Node *) object,
    1142             :                               relp, lockmode, missing_ok);
    1143             : }
    1144             : 
    1145             : /*
    1146             :  * Find an ObjectAddress for a type of object that is identified by an
    1147             :  * unqualified name.
    1148             :  */
    1149             : static ObjectAddress
    1150        1904 : get_object_address_unqualified(ObjectType objtype,
    1151             :                                Value *strval, bool missing_ok)
    1152             : {
    1153             :     const char *name;
    1154             :     ObjectAddress address;
    1155             : 
    1156        1904 :     name = strVal(strval);
    1157             : 
    1158             :     /* Translate name to OID. */
    1159        1904 :     switch (objtype)
    1160             :     {
    1161             :         case OBJECT_ACCESS_METHOD:
    1162          44 :             address.classId = AccessMethodRelationId;
    1163          44 :             address.objectId = get_am_oid(name, missing_ok);
    1164          36 :             address.objectSubId = 0;
    1165          36 :             break;
    1166             :         case OBJECT_DATABASE:
    1167         652 :             address.classId = DatabaseRelationId;
    1168         652 :             address.objectId = get_database_oid(name, missing_ok);
    1169         648 :             address.objectSubId = 0;
    1170         648 :             break;
    1171             :         case OBJECT_EXTENSION:
    1172          98 :             address.classId = ExtensionRelationId;
    1173          98 :             address.objectId = get_extension_oid(name, missing_ok);
    1174          90 :             address.objectSubId = 0;
    1175          90 :             break;
    1176             :         case OBJECT_TABLESPACE:
    1177           8 :             address.classId = TableSpaceRelationId;
    1178           8 :             address.objectId = get_tablespace_oid(name, missing_ok);
    1179           4 :             address.objectSubId = 0;
    1180           4 :             break;
    1181             :         case OBJECT_ROLE:
    1182          26 :             address.classId = AuthIdRelationId;
    1183          26 :             address.objectId = get_role_oid(name, missing_ok);
    1184          20 :             address.objectSubId = 0;
    1185          20 :             break;
    1186             :         case OBJECT_SCHEMA:
    1187         240 :             address.classId = NamespaceRelationId;
    1188         240 :             address.objectId = get_namespace_oid(name, missing_ok);
    1189         228 :             address.objectSubId = 0;
    1190         228 :             break;
    1191             :         case OBJECT_LANGUAGE:
    1192         414 :             address.classId = LanguageRelationId;
    1193         414 :             address.objectId = get_language_oid(name, missing_ok);
    1194         406 :             address.objectSubId = 0;
    1195         406 :             break;
    1196             :         case OBJECT_FDW:
    1197         126 :             address.classId = ForeignDataWrapperRelationId;
    1198         126 :             address.objectId = get_foreign_data_wrapper_oid(name, missing_ok);
    1199         114 :             address.objectSubId = 0;
    1200         114 :             break;
    1201             :         case OBJECT_FOREIGN_SERVER:
    1202         110 :             address.classId = ForeignServerRelationId;
    1203         110 :             address.objectId = get_foreign_server_oid(name, missing_ok);
    1204          98 :             address.objectSubId = 0;
    1205          98 :             break;
    1206             :         case OBJECT_EVENT_TRIGGER:
    1207          84 :             address.classId = EventTriggerRelationId;
    1208          84 :             address.objectId = get_event_trigger_oid(name, missing_ok);
    1209          76 :             address.objectSubId = 0;
    1210          76 :             break;
    1211             :         case OBJECT_PUBLICATION:
    1212          68 :             address.classId = PublicationRelationId;
    1213          68 :             address.objectId = get_publication_oid(name, missing_ok);
    1214          64 :             address.objectSubId = 0;
    1215          64 :             break;
    1216             :         case OBJECT_SUBSCRIPTION:
    1217          34 :             address.classId = SubscriptionRelationId;
    1218          34 :             address.objectId = get_subscription_oid(name, missing_ok);
    1219          30 :             address.objectSubId = 0;
    1220          30 :             break;
    1221             :         default:
    1222           0 :             elog(ERROR, "unrecognized objtype: %d", (int) objtype);
    1223             :             /* placate compiler, which doesn't know elog won't return */
    1224             :             address.classId = InvalidOid;
    1225             :             address.objectId = InvalidOid;
    1226             :             address.objectSubId = 0;
    1227             :     }
    1228             : 
    1229        1814 :     return address;
    1230             : }
    1231             : 
    1232             : /*
    1233             :  * Locate a relation by qualified name.
    1234             :  */
    1235             : static ObjectAddress
    1236         382 : get_relation_by_qualified_name(ObjectType objtype, List *object,
    1237             :                                Relation *relp, LOCKMODE lockmode,
    1238             :                                bool missing_ok)
    1239             : {
    1240             :     Relation    relation;
    1241             :     ObjectAddress address;
    1242             : 
    1243         382 :     address.classId = RelationRelationId;
    1244         382 :     address.objectId = InvalidOid;
    1245         382 :     address.objectSubId = 0;
    1246             : 
    1247         382 :     relation = relation_openrv_extended(makeRangeVarFromNameList(object),
    1248             :                                         lockmode, missing_ok);
    1249         220 :     if (!relation)
    1250           0 :         return address;
    1251             : 
    1252         220 :     switch (objtype)
    1253             :     {
    1254             :         case OBJECT_INDEX:
    1255          84 :             if (relation->rd_rel->relkind != RELKIND_INDEX &&
    1256          12 :                 relation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
    1257           0 :                 ereport(ERROR,
    1258             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1259             :                          errmsg("\"%s\" is not an index",
    1260             :                                 RelationGetRelationName(relation))));
    1261          72 :             break;
    1262             :         case OBJECT_SEQUENCE:
    1263          20 :             if (relation->rd_rel->relkind != RELKIND_SEQUENCE)
    1264           0 :                 ereport(ERROR,
    1265             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1266             :                          errmsg("\"%s\" is not a sequence",
    1267             :                                 RelationGetRelationName(relation))));
    1268          20 :             break;
    1269             :         case OBJECT_TABLE:
    1270          74 :             if (relation->rd_rel->relkind != RELKIND_RELATION &&
    1271          12 :                 relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
    1272           0 :                 ereport(ERROR,
    1273             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1274             :                          errmsg("\"%s\" is not a table",
    1275             :                                 RelationGetRelationName(relation))));
    1276          62 :             break;
    1277             :         case OBJECT_VIEW:
    1278          24 :             if (relation->rd_rel->relkind != RELKIND_VIEW)
    1279           0 :                 ereport(ERROR,
    1280             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1281             :                          errmsg("\"%s\" is not a view",
    1282             :                                 RelationGetRelationName(relation))));
    1283          24 :             break;
    1284             :         case OBJECT_MATVIEW:
    1285          18 :             if (relation->rd_rel->relkind != RELKIND_MATVIEW)
    1286           0 :                 ereport(ERROR,
    1287             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1288             :                          errmsg("\"%s\" is not a materialized view",
    1289             :                                 RelationGetRelationName(relation))));
    1290          18 :             break;
    1291             :         case OBJECT_FOREIGN_TABLE:
    1292          24 :             if (relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE)
    1293           0 :                 ereport(ERROR,
    1294             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1295             :                          errmsg("\"%s\" is not a foreign table",
    1296             :                                 RelationGetRelationName(relation))));
    1297          24 :             break;
    1298             :         default:
    1299           0 :             elog(ERROR, "unrecognized objtype: %d", (int) objtype);
    1300             :             break;
    1301             :     }
    1302             : 
    1303             :     /* Done. */
    1304         220 :     address.objectId = RelationGetRelid(relation);
    1305         220 :     *relp = relation;
    1306             : 
    1307         220 :     return address;
    1308             : }
    1309             : 
    1310             : /*
    1311             :  * Find object address for an object that is attached to a relation.
    1312             :  *
    1313             :  * Note that we take only an AccessShareLock on the relation.  We need not
    1314             :  * pass down the LOCKMODE from get_object_address(), because that is the lock
    1315             :  * mode for the object itself, not the relation to which it is attached.
    1316             :  */
    1317             : static ObjectAddress
    1318         992 : get_object_address_relobject(ObjectType objtype, List *object,
    1319             :                              Relation *relp, bool missing_ok)
    1320             : {
    1321             :     ObjectAddress address;
    1322         992 :     Relation    relation = NULL;
    1323             :     int         nnames;
    1324             :     const char *depname;
    1325             :     List       *relname;
    1326             :     Oid         reloid;
    1327             : 
    1328             :     /* Extract name of dependent object. */
    1329         992 :     depname = strVal(llast(object));
    1330             : 
    1331             :     /* Separate relation name from dependent object name. */
    1332         992 :     nnames = list_length(object);
    1333         992 :     if (nnames < 2)
    1334          32 :         ereport(ERROR,
    1335             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1336             :                  errmsg("must specify relation and object name")));
    1337             : 
    1338             :     /* Extract relation name and open relation. */
    1339         960 :     relname = list_truncate(list_copy(object), nnames - 1);
    1340         960 :     relation = table_openrv_extended(makeRangeVarFromNameList(relname),
    1341             :                                      AccessShareLock,
    1342             :                                      missing_ok);
    1343             : 
    1344         872 :     reloid = relation ? RelationGetRelid(relation) : InvalidOid;
    1345             : 
    1346         872 :     switch (objtype)
    1347             :     {
    1348             :         case OBJECT_RULE:
    1349         138 :             address.classId = RewriteRelationId;
    1350         130 :             address.objectId = relation ?
    1351         138 :                 get_rewrite_oid(reloid, depname, missing_ok) : InvalidOid;
    1352         130 :             address.objectSubId = 0;
    1353         130 :             break;
    1354             :         case OBJECT_TRIGGER:
    1355         526 :             address.classId = TriggerRelationId;
    1356         518 :             address.objectId = relation ?
    1357         526 :                 get_trigger_oid(reloid, depname, missing_ok) : InvalidOid;
    1358         518 :             address.objectSubId = 0;
    1359         518 :             break;
    1360             :         case OBJECT_TABCONSTRAINT:
    1361         118 :             address.classId = ConstraintRelationId;
    1362         110 :             address.objectId = relation ?
    1363         118 :                 get_relation_constraint_oid(reloid, depname, missing_ok) :
    1364             :                 InvalidOid;
    1365         110 :             address.objectSubId = 0;
    1366         110 :             break;
    1367             :         case OBJECT_POLICY:
    1368          90 :             address.classId = PolicyRelationId;
    1369          86 :             address.objectId = relation ?
    1370          90 :                 get_relation_policy_oid(reloid, depname, missing_ok) :
    1371             :                 InvalidOid;
    1372          86 :             address.objectSubId = 0;
    1373          86 :             break;
    1374             :         default:
    1375           0 :             elog(ERROR, "unrecognized objtype: %d", (int) objtype);
    1376             :     }
    1377             : 
    1378             :     /* Avoid relcache leak when object not found. */
    1379         844 :     if (!OidIsValid(address.objectId))
    1380             :     {
    1381          32 :         if (relation != NULL)
    1382           8 :             table_close(relation, AccessShareLock);
    1383             : 
    1384          32 :         relation = NULL;        /* department of accident prevention */
    1385          32 :         return address;
    1386             :     }
    1387             : 
    1388             :     /* Done. */
    1389         812 :     *relp = relation;
    1390         812 :     return address;
    1391             : }
    1392             : 
    1393             : /*
    1394             :  * Find the ObjectAddress for an attribute.
    1395             :  */
    1396             : static ObjectAddress
    1397         180 : get_object_address_attribute(ObjectType objtype, List *object,
    1398             :                              Relation *relp, LOCKMODE lockmode,
    1399             :                              bool missing_ok)
    1400             : {
    1401             :     ObjectAddress address;
    1402             :     List       *relname;
    1403             :     Oid         reloid;
    1404             :     Relation    relation;
    1405             :     const char *attname;
    1406             :     AttrNumber  attnum;
    1407             : 
    1408             :     /* Extract relation name and open relation. */
    1409         180 :     if (list_length(object) < 2)
    1410          18 :         ereport(ERROR,
    1411             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1412             :                  errmsg("column name must be qualified")));
    1413         162 :     attname = strVal(lfirst(list_tail(object)));
    1414         162 :     relname = list_truncate(list_copy(object), list_length(object) - 1);
    1415             :     /* XXX no missing_ok support here */
    1416         162 :     relation = relation_openrv(makeRangeVarFromNameList(relname), lockmode);
    1417         130 :     reloid = RelationGetRelid(relation);
    1418             : 
    1419             :     /* Look up attribute and construct return value. */
    1420         130 :     attnum = get_attnum(reloid, attname);
    1421         130 :     if (attnum == InvalidAttrNumber)
    1422             :     {
    1423          12 :         if (!missing_ok)
    1424          12 :             ereport(ERROR,
    1425             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
    1426             :                      errmsg("column \"%s\" of relation \"%s\" does not exist",
    1427             :                             attname, NameListToString(relname))));
    1428             : 
    1429           0 :         address.classId = RelationRelationId;
    1430           0 :         address.objectId = InvalidOid;
    1431           0 :         address.objectSubId = InvalidAttrNumber;
    1432           0 :         relation_close(relation, lockmode);
    1433           0 :         return address;
    1434             :     }
    1435             : 
    1436         118 :     address.classId = RelationRelationId;
    1437         118 :     address.objectId = reloid;
    1438         118 :     address.objectSubId = attnum;
    1439             : 
    1440         118 :     *relp = relation;
    1441         118 :     return address;
    1442             : }
    1443             : 
    1444             : /*
    1445             :  * Find the ObjectAddress for an attribute's default value.
    1446             :  */
    1447             : static ObjectAddress
    1448          32 : get_object_address_attrdef(ObjectType objtype, List *object,
    1449             :                            Relation *relp, LOCKMODE lockmode,
    1450             :                            bool missing_ok)
    1451             : {
    1452             :     ObjectAddress address;
    1453             :     List       *relname;
    1454             :     Oid         reloid;
    1455             :     Relation    relation;
    1456             :     const char *attname;
    1457             :     AttrNumber  attnum;
    1458             :     TupleDesc   tupdesc;
    1459             :     Oid         defoid;
    1460             : 
    1461             :     /* Extract relation name and open relation. */
    1462          32 :     if (list_length(object) < 2)
    1463           8 :         ereport(ERROR,
    1464             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    1465             :                  errmsg("column name must be qualified")));
    1466          24 :     attname = strVal(llast(object));
    1467          24 :     relname = list_truncate(list_copy(object), list_length(object) - 1);
    1468             :     /* XXX no missing_ok support here */
    1469          24 :     relation = relation_openrv(makeRangeVarFromNameList(relname), lockmode);
    1470           8 :     reloid = RelationGetRelid(relation);
    1471             : 
    1472           8 :     tupdesc = RelationGetDescr(relation);
    1473             : 
    1474             :     /* Look up attribute number and scan pg_attrdef to find its tuple */
    1475           8 :     attnum = get_attnum(reloid, attname);
    1476           8 :     defoid = InvalidOid;
    1477           8 :     if (attnum != InvalidAttrNumber && tupdesc->constr != NULL)
    1478             :     {
    1479             :         Relation    attrdef;
    1480             :         ScanKeyData keys[2];
    1481             :         SysScanDesc scan;
    1482             :         HeapTuple   tup;
    1483             : 
    1484           8 :         attrdef = relation_open(AttrDefaultRelationId, AccessShareLock);
    1485           8 :         ScanKeyInit(&keys[0],
    1486             :                     Anum_pg_attrdef_adrelid,
    1487             :                     BTEqualStrategyNumber,
    1488             :                     F_OIDEQ,
    1489             :                     ObjectIdGetDatum(reloid));
    1490           8 :         ScanKeyInit(&keys[1],
    1491             :                     Anum_pg_attrdef_adnum,
    1492             :                     BTEqualStrategyNumber,
    1493             :                     F_INT2EQ,
    1494             :                     Int16GetDatum(attnum));
    1495           8 :         scan = systable_beginscan(attrdef, AttrDefaultIndexId, true,
    1496             :                                   NULL, 2, keys);
    1497           8 :         if (HeapTupleIsValid(tup = systable_getnext(scan)))
    1498             :         {
    1499           8 :             Form_pg_attrdef atdform = (Form_pg_attrdef) GETSTRUCT(tup);
    1500             : 
    1501           8 :             defoid = atdform->oid;
    1502             :         }
    1503             : 
    1504           8 :         systable_endscan(scan);
    1505           8 :         relation_close(attrdef, AccessShareLock);
    1506             :     }
    1507           8 :     if (!OidIsValid(defoid))
    1508             :     {
    1509           0 :         if (!missing_ok)
    1510           0 :             ereport(ERROR,
    1511             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
    1512             :                      errmsg("default value for column \"%s\" of relation \"%s\" does not exist",
    1513             :                             attname, NameListToString(relname))));
    1514             : 
    1515           0 :         address.classId = AttrDefaultRelationId;
    1516           0 :         address.objectId = InvalidOid;
    1517           0 :         address.objectSubId = InvalidAttrNumber;
    1518           0 :         relation_close(relation, lockmode);
    1519           0 :         return address;
    1520             :     }
    1521             : 
    1522           8 :     address.classId = AttrDefaultRelationId;
    1523           8 :     address.objectId = defoid;
    1524           8 :     address.objectSubId = 0;
    1525             : 
    1526           8 :     *relp = relation;
    1527           8 :     return address;
    1528             : }
    1529             : 
    1530             : /*
    1531             :  * Find the ObjectAddress for a type or domain
    1532             :  */
    1533             : static ObjectAddress
    1534         992 : get_object_address_type(ObjectType objtype, TypeName *typename, bool missing_ok)
    1535             : {
    1536             :     ObjectAddress address;
    1537             :     Type        tup;
    1538             : 
    1539         992 :     address.classId = TypeRelationId;
    1540         992 :     address.objectId = InvalidOid;
    1541         992 :     address.objectSubId = 0;
    1542             : 
    1543         992 :     tup = LookupTypeName(NULL, typename, NULL, missing_ok);
    1544         992 :     if (!HeapTupleIsValid(tup))
    1545             :     {
    1546          70 :         if (!missing_ok)
    1547          52 :             ereport(ERROR,
    1548             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    1549             :                      errmsg("type \"%s\" does not exist",
    1550             :                             TypeNameToString(typename))));
    1551          18 :         return address;
    1552             :     }
    1553         922 :     address.objectId = typeTypeId(tup);
    1554             : 
    1555         922 :     if (objtype == OBJECT_DOMAIN)
    1556             :     {
    1557         264 :         if (((Form_pg_type) GETSTRUCT(tup))->typtype != TYPTYPE_DOMAIN)
    1558           0 :             ereport(ERROR,
    1559             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1560             :                      errmsg("\"%s\" is not a domain",
    1561             :                             TypeNameToString(typename))));
    1562             :     }
    1563             : 
    1564         922 :     ReleaseSysCache(tup);
    1565             : 
    1566         922 :     return address;
    1567             : }
    1568             : 
    1569             : /*
    1570             :  * Find the ObjectAddress for an opclass or opfamily.
    1571             :  */
    1572             : static ObjectAddress
    1573         328 : get_object_address_opcf(ObjectType objtype, List *object, bool missing_ok)
    1574             : {
    1575             :     Oid         amoid;
    1576             :     ObjectAddress address;
    1577             : 
    1578             :     /* XXX no missing_ok support here */
    1579         328 :     amoid = get_index_am_oid(strVal(linitial(object)), false);
    1580         280 :     object = list_copy_tail(object, 1);
    1581             : 
    1582         280 :     switch (objtype)
    1583             :     {
    1584             :         case OBJECT_OPCLASS:
    1585         108 :             address.classId = OperatorClassRelationId;
    1586         108 :             address.objectId = get_opclass_oid(amoid, object, missing_ok);
    1587         104 :             address.objectSubId = 0;
    1588         104 :             break;
    1589             :         case OBJECT_OPFAMILY:
    1590         172 :             address.classId = OperatorFamilyRelationId;
    1591         172 :             address.objectId = get_opfamily_oid(amoid, object, missing_ok);
    1592         168 :             address.objectSubId = 0;
    1593         168 :             break;
    1594             :         default:
    1595           0 :             elog(ERROR, "unrecognized objtype: %d", (int) objtype);
    1596             :             /* placate compiler, which doesn't know elog won't return */
    1597             :             address.classId = InvalidOid;
    1598             :             address.objectId = InvalidOid;
    1599             :             address.objectSubId = 0;
    1600             :     }
    1601             : 
    1602         272 :     return address;
    1603             : }
    1604             : 
    1605             : /*
    1606             :  * Find the ObjectAddress for an opclass/opfamily member.
    1607             :  *
    1608             :  * (The returned address corresponds to a pg_amop/pg_amproc object).
    1609             :  */
    1610             : static ObjectAddress
    1611          32 : get_object_address_opf_member(ObjectType objtype,
    1612             :                               List *object, bool missing_ok)
    1613             : {
    1614             :     ObjectAddress famaddr;
    1615             :     ObjectAddress address;
    1616             :     ListCell   *cell;
    1617             :     List       *copy;
    1618             :     TypeName   *typenames[2];
    1619             :     Oid         typeoids[2];
    1620             :     int         membernum;
    1621             :     int         i;
    1622             : 
    1623             :     /*
    1624             :      * The last element of the object list contains the strategy or procedure
    1625             :      * number.  We need to strip that out before getting the opclass/family
    1626             :      * address.  The rest can be used directly by get_object_address_opcf().
    1627             :      */
    1628          32 :     membernum = atoi(strVal(llast(linitial(object))));
    1629          32 :     copy = list_truncate(list_copy(linitial(object)), list_length(linitial(object)) - 1);
    1630             : 
    1631             :     /* no missing_ok support here */
    1632          32 :     famaddr = get_object_address_opcf(OBJECT_OPFAMILY, copy, false);
    1633             : 
    1634             :     /* find out left/right type names and OIDs */
    1635          32 :     typenames[0] = typenames[1] = NULL;
    1636          32 :     typeoids[0] = typeoids[1] = InvalidOid;
    1637          32 :     i = 0;
    1638          64 :     foreach(cell, lsecond(object))
    1639             :     {
    1640             :         ObjectAddress typaddr;
    1641             : 
    1642          64 :         typenames[i] = lfirst_node(TypeName, cell);
    1643          64 :         typaddr = get_object_address_type(OBJECT_TYPE, typenames[i], missing_ok);
    1644          64 :         typeoids[i] = typaddr.objectId;
    1645          64 :         if (++i >= 2)
    1646          32 :             break;
    1647             :     }
    1648             : 
    1649          32 :     switch (objtype)
    1650             :     {
    1651             :         case OBJECT_AMOP:
    1652             :             {
    1653             :                 HeapTuple   tp;
    1654             : 
    1655          16 :                 ObjectAddressSet(address, AccessMethodOperatorRelationId,
    1656             :                                  InvalidOid);
    1657             : 
    1658          64 :                 tp = SearchSysCache4(AMOPSTRATEGY,
    1659          16 :                                      ObjectIdGetDatum(famaddr.objectId),
    1660          16 :                                      ObjectIdGetDatum(typeoids[0]),
    1661          16 :                                      ObjectIdGetDatum(typeoids[1]),
    1662             :                                      Int16GetDatum(membernum));
    1663          16 :                 if (!HeapTupleIsValid(tp))
    1664             :                 {
    1665           8 :                     if (!missing_ok)
    1666           8 :                         ereport(ERROR,
    1667             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    1668             :                                  errmsg("operator %d (%s, %s) of %s does not exist",
    1669             :                                         membernum,
    1670             :                                         TypeNameToString(typenames[0]),
    1671             :                                         TypeNameToString(typenames[1]),
    1672             :                                         getObjectDescription(&famaddr))));
    1673             :                 }
    1674             :                 else
    1675             :                 {
    1676           8 :                     address.objectId = ((Form_pg_amop) GETSTRUCT(tp))->oid;
    1677           8 :                     ReleaseSysCache(tp);
    1678             :                 }
    1679             :             }
    1680           8 :             break;
    1681             : 
    1682             :         case OBJECT_AMPROC:
    1683             :             {
    1684             :                 HeapTuple   tp;
    1685             : 
    1686          16 :                 ObjectAddressSet(address, AccessMethodProcedureRelationId,
    1687             :                                  InvalidOid);
    1688             : 
    1689          64 :                 tp = SearchSysCache4(AMPROCNUM,
    1690          16 :                                      ObjectIdGetDatum(famaddr.objectId),
    1691          16 :                                      ObjectIdGetDatum(typeoids[0]),
    1692          16 :                                      ObjectIdGetDatum(typeoids[1]),
    1693             :                                      Int16GetDatum(membernum));
    1694          16 :                 if (!HeapTupleIsValid(tp))
    1695             :                 {
    1696           8 :                     if (!missing_ok)
    1697           8 :                         ereport(ERROR,
    1698             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    1699             :                                  errmsg("function %d (%s, %s) of %s does not exist",
    1700             :                                         membernum,
    1701             :                                         TypeNameToString(typenames[0]),
    1702             :                                         TypeNameToString(typenames[1]),
    1703             :                                         getObjectDescription(&famaddr))));
    1704             :                 }
    1705             :                 else
    1706             :                 {
    1707           8 :                     address.objectId = ((Form_pg_amproc) GETSTRUCT(tp))->oid;
    1708           8 :                     ReleaseSysCache(tp);
    1709             :                 }
    1710             :             }
    1711           8 :             break;
    1712             :         default:
    1713           0 :             elog(ERROR, "unrecognized objtype: %d", (int) objtype);
    1714             :     }
    1715             : 
    1716          16 :     return address;
    1717             : }
    1718             : 
    1719             : /*
    1720             :  * Find the ObjectAddress for a user mapping.
    1721             :  */
    1722             : static ObjectAddress
    1723          20 : get_object_address_usermapping(List *object, bool missing_ok)
    1724             : {
    1725             :     ObjectAddress address;
    1726             :     Oid         userid;
    1727             :     char       *username;
    1728             :     char       *servername;
    1729             :     ForeignServer *server;
    1730             :     HeapTuple   tp;
    1731             : 
    1732          20 :     ObjectAddressSet(address, UserMappingRelationId, InvalidOid);
    1733             : 
    1734             :     /* fetch string names from input lists, for error messages */
    1735          20 :     username = strVal(linitial(object));
    1736          20 :     servername = strVal(lsecond(object));
    1737             : 
    1738             :     /* look up pg_authid OID of mapped user; InvalidOid if PUBLIC */
    1739          20 :     if (strcmp(username, "public") == 0)
    1740           0 :         userid = InvalidOid;
    1741             :     else
    1742             :     {
    1743          20 :         tp = SearchSysCache1(AUTHNAME,
    1744             :                              CStringGetDatum(username));
    1745          20 :         if (!HeapTupleIsValid(tp))
    1746             :         {
    1747          12 :             if (!missing_ok)
    1748          12 :                 ereport(ERROR,
    1749             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    1750             :                          errmsg("user mapping for user \"%s\" on server \"%s\" does not exist",
    1751             :                                 username, servername)));
    1752           0 :             return address;
    1753             :         }
    1754           8 :         userid = ((Form_pg_authid) GETSTRUCT(tp))->oid;
    1755           8 :         ReleaseSysCache(tp);
    1756             :     }
    1757             : 
    1758             :     /* Now look up the pg_user_mapping tuple */
    1759           8 :     server = GetForeignServerByName(servername, true);
    1760           8 :     if (!server)
    1761             :     {
    1762           0 :         if (!missing_ok)
    1763           0 :             ereport(ERROR,
    1764             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    1765             :                      errmsg("server \"%s\" does not exist", servername)));
    1766           0 :         return address;
    1767             :     }
    1768           8 :     tp = SearchSysCache2(USERMAPPINGUSERSERVER,
    1769             :                          ObjectIdGetDatum(userid),
    1770           8 :                          ObjectIdGetDatum(server->serverid));
    1771           8 :     if (!HeapTupleIsValid(tp))
    1772             :     {
    1773           0 :         if (!missing_ok)
    1774           0 :             ereport(ERROR,
    1775             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    1776             :                      errmsg("user mapping for user \"%s\" on server \"%s\" does not exist",
    1777             :                             username, servername)));
    1778           0 :         return address;
    1779             :     }
    1780             : 
    1781           8 :     address.objectId = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid;
    1782             : 
    1783           8 :     ReleaseSysCache(tp);
    1784             : 
    1785           8 :     return address;
    1786             : }
    1787             : 
    1788             : /*
    1789             :  * Find the ObjectAddress for a publication relation.  The first element of
    1790             :  * the object parameter is the relation name, the second is the
    1791             :  * publication name.
    1792             :  */
    1793             : static ObjectAddress
    1794          20 : get_object_address_publication_rel(List *object,
    1795             :                                    Relation *relp, bool missing_ok)
    1796             : {
    1797             :     ObjectAddress address;
    1798             :     Relation    relation;
    1799             :     List       *relname;
    1800             :     char       *pubname;
    1801             :     Publication *pub;
    1802             : 
    1803          20 :     ObjectAddressSet(address, PublicationRelRelationId, InvalidOid);
    1804             : 
    1805          20 :     relname = linitial(object);
    1806          20 :     relation = relation_openrv_extended(makeRangeVarFromNameList(relname),
    1807             :                                         AccessShareLock, missing_ok);
    1808           8 :     if (!relation)
    1809           0 :         return address;
    1810             : 
    1811             :     /* fetch publication name from input list */
    1812           8 :     pubname = strVal(lsecond(object));
    1813             : 
    1814             :     /* Now look up the pg_publication tuple */
    1815           8 :     pub = GetPublicationByName(pubname, missing_ok);
    1816           8 :     if (!pub)
    1817             :     {
    1818           0 :         relation_close(relation, AccessShareLock);
    1819           0 :         return address;
    1820             :     }
    1821             : 
    1822             :     /* Find the publication relation mapping in syscache. */
    1823           8 :     address.objectId =
    1824           8 :         GetSysCacheOid2(PUBLICATIONRELMAP, Anum_pg_publication_rel_oid,
    1825             :                         ObjectIdGetDatum(RelationGetRelid(relation)),
    1826             :                         ObjectIdGetDatum(pub->oid));
    1827           8 :     if (!OidIsValid(address.objectId))
    1828             :     {
    1829           0 :         if (!missing_ok)
    1830           0 :             ereport(ERROR,
    1831             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    1832             :                      errmsg("publication relation \"%s\" in publication \"%s\" does not exist",
    1833             :                             RelationGetRelationName(relation), pubname)));
    1834           0 :         relation_close(relation, AccessShareLock);
    1835           0 :         return address;
    1836             :     }
    1837             : 
    1838           8 :     *relp = relation;
    1839           8 :     return address;
    1840             : }
    1841             : 
    1842             : /*
    1843             :  * Find the ObjectAddress for a default ACL.
    1844             :  */
    1845             : static ObjectAddress
    1846          28 : get_object_address_defacl(List *object, bool missing_ok)
    1847             : {
    1848             :     HeapTuple   tp;
    1849             :     Oid         userid;
    1850             :     Oid         schemaid;
    1851             :     char       *username;
    1852             :     char       *schema;
    1853             :     char        objtype;
    1854             :     char       *objtype_str;
    1855             :     ObjectAddress address;
    1856             : 
    1857          28 :     ObjectAddressSet(address, DefaultAclRelationId, InvalidOid);
    1858             : 
    1859             :     /*
    1860             :      * First figure out the textual attributes so that they can be used for
    1861             :      * error reporting.
    1862             :      */
    1863          28 :     username = strVal(lsecond(object));
    1864          28 :     if (list_length(object) >= 3)
    1865          16 :         schema = (char *) strVal(lthird(object));
    1866             :     else
    1867          12 :         schema = NULL;
    1868             : 
    1869             :     /*
    1870             :      * Decode defaclobjtype.  Only first char is considered; the rest of the
    1871             :      * string, if any, is blissfully ignored.
    1872             :      */
    1873          28 :     objtype = ((char *) strVal(linitial(object)))[0];
    1874          28 :     switch (objtype)
    1875             :     {
    1876             :         case DEFACLOBJ_RELATION:
    1877          16 :             objtype_str = "tables";
    1878          16 :             break;
    1879             :         case DEFACLOBJ_SEQUENCE:
    1880           0 :             objtype_str = "sequences";
    1881           0 :             break;
    1882             :         case DEFACLOBJ_FUNCTION:
    1883           0 :             objtype_str = "functions";
    1884           0 :             break;
    1885             :         case DEFACLOBJ_TYPE:
    1886           0 :             objtype_str = "types";
    1887           0 :             break;
    1888             :         case DEFACLOBJ_NAMESPACE:
    1889           0 :             objtype_str = "schemas";
    1890           0 :             break;
    1891             :         default:
    1892          12 :             ereport(ERROR,
    1893             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1894             :                      errmsg("unrecognized default ACL object type \"%c\"", objtype),
    1895             :                      errhint("Valid object types are \"%c\", \"%c\", \"%c\", \"%c\", \"%c\".",
    1896             :                              DEFACLOBJ_RELATION,
    1897             :                              DEFACLOBJ_SEQUENCE,
    1898             :                              DEFACLOBJ_FUNCTION,
    1899             :                              DEFACLOBJ_TYPE,
    1900             :                              DEFACLOBJ_NAMESPACE)));
    1901             :     }
    1902             : 
    1903             :     /*
    1904             :      * Look up user ID.  Behave as "default ACL not found" if the user doesn't
    1905             :      * exist.
    1906             :      */
    1907          16 :     tp = SearchSysCache1(AUTHNAME,
    1908             :                          CStringGetDatum(username));
    1909          16 :     if (!HeapTupleIsValid(tp))
    1910           0 :         goto not_found;
    1911          16 :     userid = ((Form_pg_authid) GETSTRUCT(tp))->oid;
    1912          16 :     ReleaseSysCache(tp);
    1913             : 
    1914             :     /*
    1915             :      * If a schema name was given, look up its OID.  If it doesn't exist,
    1916             :      * behave as "default ACL not found".
    1917             :      */
    1918          16 :     if (schema)
    1919             :     {
    1920           8 :         schemaid = get_namespace_oid(schema, true);
    1921           8 :         if (schemaid == InvalidOid)
    1922           0 :             goto not_found;
    1923             :     }
    1924             :     else
    1925           8 :         schemaid = InvalidOid;
    1926             : 
    1927             :     /* Finally, look up the pg_default_acl object */
    1928          16 :     tp = SearchSysCache3(DEFACLROLENSPOBJ,
    1929             :                          ObjectIdGetDatum(userid),
    1930             :                          ObjectIdGetDatum(schemaid),
    1931             :                          CharGetDatum(objtype));
    1932          16 :     if (!HeapTupleIsValid(tp))
    1933           0 :         goto not_found;
    1934             : 
    1935          16 :     address.objectId = ((Form_pg_default_acl) GETSTRUCT(tp))->oid;
    1936          16 :     ReleaseSysCache(tp);
    1937             : 
    1938          16 :     return address;
    1939             : 
    1940             : not_found:
    1941           0 :     if (!missing_ok)
    1942             :     {
    1943           0 :         if (schema)
    1944           0 :             ereport(ERROR,
    1945             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    1946             :                      errmsg("default ACL for user \"%s\" in schema \"%s\" on %s does not exist",
    1947             :                             username, schema, objtype_str)));
    1948             :         else
    1949           0 :             ereport(ERROR,
    1950             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    1951             :                      errmsg("default ACL for user \"%s\" on %s does not exist",
    1952             :                             username, objtype_str)));
    1953             :     }
    1954           0 :     return address;
    1955             : }
    1956             : 
    1957             : /*
    1958             :  * Convert an array of TEXT into a List of string Values, as emitted by the
    1959             :  * parser, which is what get_object_address uses as input.
    1960             :  */
    1961             : static List *
    1962        2168 : textarray_to_strvaluelist(ArrayType *arr)
    1963             : {
    1964             :     Datum      *elems;
    1965             :     bool       *nulls;
    1966             :     int         nelems;
    1967        2168 :     List       *list = NIL;
    1968             :     int         i;
    1969             : 
    1970        2168 :     deconstruct_array(arr, TEXTOID, -1, false, 'i',
    1971             :                       &elems, &nulls, &nelems);
    1972             : 
    1973        4744 :     for (i = 0; i < nelems; i++)
    1974             :     {
    1975        2580 :         if (nulls[i])
    1976           4 :             ereport(ERROR,
    1977             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1978             :                      errmsg("name or argument lists may not contain nulls")));
    1979        2576 :         list = lappend(list, makeString(TextDatumGetCString(elems[i])));
    1980             :     }
    1981             : 
    1982        2164 :     return list;
    1983             : }
    1984             : 
    1985             : /*
    1986             :  * SQL-callable version of get_object_address
    1987             :  */
    1988             : Datum
    1989        1344 : pg_get_object_address(PG_FUNCTION_ARGS)
    1990             : {
    1991        1344 :     char       *ttype = TextDatumGetCString(PG_GETARG_DATUM(0));
    1992        1344 :     ArrayType  *namearr = PG_GETARG_ARRAYTYPE_P(1);
    1993        1344 :     ArrayType  *argsarr = PG_GETARG_ARRAYTYPE_P(2);
    1994             :     int         itype;
    1995             :     ObjectType  type;
    1996        1344 :     List       *name = NIL;
    1997        1344 :     TypeName   *typename = NULL;
    1998        1344 :     List       *args = NIL;
    1999        1344 :     Node       *objnode = NULL;
    2000             :     ObjectAddress addr;
    2001             :     TupleDesc   tupdesc;
    2002             :     Datum       values[3];
    2003             :     bool        nulls[3];
    2004             :     HeapTuple   htup;
    2005             :     Relation    relation;
    2006             : 
    2007             :     /* Decode object type, raise error if unknown */
    2008        1344 :     itype = read_objtype_from_string(ttype);
    2009        1340 :     if (itype < 0)
    2010          24 :         ereport(ERROR,
    2011             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2012             :                  errmsg("unsupported object type \"%s\"", ttype)));
    2013        1316 :     type = (ObjectType) itype;
    2014             : 
    2015             :     /*
    2016             :      * Convert the text array to the representation appropriate for the given
    2017             :      * object type.  Most use a simple string Values list, but there are some
    2018             :      * exceptions.
    2019             :      */
    2020        1316 :     if (type == OBJECT_TYPE || type == OBJECT_DOMAIN || type == OBJECT_CAST ||
    2021        1196 :         type == OBJECT_TRANSFORM || type == OBJECT_DOMCONSTRAINT)
    2022          88 :     {
    2023             :         Datum      *elems;
    2024             :         bool       *nulls;
    2025             :         int         nelems;
    2026             : 
    2027         152 :         deconstruct_array(namearr, TEXTOID, -1, false, 'i',
    2028             :                           &elems, &nulls, &nelems);
    2029         152 :         if (nelems != 1)
    2030          64 :             ereport(ERROR,
    2031             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2032             :                      errmsg("name list length must be exactly %d", 1)));
    2033          88 :         if (nulls[0])
    2034           0 :             ereport(ERROR,
    2035             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2036             :                      errmsg("name or argument lists may not contain nulls")));
    2037          88 :         typename = typeStringToTypeName(TextDatumGetCString(elems[0]));
    2038             :     }
    2039        1164 :     else if (type == OBJECT_LARGEOBJECT)
    2040             :     {
    2041             :         Datum      *elems;
    2042             :         bool       *nulls;
    2043             :         int         nelems;
    2044             : 
    2045          12 :         deconstruct_array(namearr, TEXTOID, -1, false, 'i',
    2046             :                           &elems, &nulls, &nelems);
    2047          12 :         if (nelems != 1)
    2048           4 :             ereport(ERROR,
    2049             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2050             :                      errmsg("name list length must be exactly %d", 1)));
    2051           8 :         if (nulls[0])
    2052           0 :             ereport(ERROR,
    2053             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2054             :                      errmsg("large object OID may not be null")));
    2055           8 :         objnode = (Node *) makeFloat(TextDatumGetCString(elems[0]));
    2056             :     }
    2057             :     else
    2058             :     {
    2059        1152 :         name = textarray_to_strvaluelist(namearr);
    2060        1148 :         if (list_length(name) < 1)
    2061           4 :             ereport(ERROR,
    2062             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2063             :                      errmsg("name list length must be at least %d", 1)));
    2064             :     }
    2065             : 
    2066             :     /*
    2067             :      * If args are given, decode them according to the object type.
    2068             :      */
    2069        1240 :     if (type == OBJECT_AGGREGATE ||
    2070        1176 :         type == OBJECT_FUNCTION ||
    2071        1144 :         type == OBJECT_PROCEDURE ||
    2072        1144 :         type == OBJECT_ROUTINE ||
    2073        1112 :         type == OBJECT_OPERATOR ||
    2074        1096 :         type == OBJECT_CAST ||
    2075        1056 :         type == OBJECT_AMOP ||
    2076             :         type == OBJECT_AMPROC)
    2077         224 :     {
    2078             :         /* in these cases, the args list must be of TypeName */
    2079             :         Datum      *elems;
    2080             :         bool       *nulls;
    2081             :         int         nelems;
    2082             :         int         i;
    2083             : 
    2084         224 :         deconstruct_array(argsarr, TEXTOID, -1, false, 'i',
    2085             :                           &elems, &nulls, &nelems);
    2086             : 
    2087         224 :         args = NIL;
    2088         428 :         for (i = 0; i < nelems; i++)
    2089             :         {
    2090         204 :             if (nulls[i])
    2091           0 :                 ereport(ERROR,
    2092             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2093             :                          errmsg("name or argument lists may not contain nulls")));
    2094         204 :             args = lappend(args,
    2095         204 :                            typeStringToTypeName(TextDatumGetCString(elems[i])));
    2096             :         }
    2097             :     }
    2098             :     else
    2099             :     {
    2100             :         /* For all other object types, use string Values */
    2101        1016 :         args = textarray_to_strvaluelist(argsarr);
    2102             :     }
    2103             : 
    2104             :     /*
    2105             :      * get_object_address is pretty sensitive to the length of its input
    2106             :      * lists; check that they're what it wants.
    2107             :      */
    2108        1240 :     switch (type)
    2109             :     {
    2110             :         case OBJECT_DOMCONSTRAINT:
    2111             :         case OBJECT_CAST:
    2112             :         case OBJECT_USER_MAPPING:
    2113             :         case OBJECT_PUBLICATION_REL:
    2114             :         case OBJECT_DEFACL:
    2115             :         case OBJECT_TRANSFORM:
    2116         152 :             if (list_length(args) != 1)
    2117          48 :                 ereport(ERROR,
    2118             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2119             :                          errmsg("argument list length must be exactly %d", 1)));
    2120         104 :             break;
    2121             :         case OBJECT_OPFAMILY:
    2122             :         case OBJECT_OPCLASS:
    2123          64 :             if (list_length(name) < 2)
    2124          16 :                 ereport(ERROR,
    2125             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2126             :                          errmsg("name list length must be at least %d", 2)));
    2127          48 :             break;
    2128             :         case OBJECT_AMOP:
    2129             :         case OBJECT_AMPROC:
    2130          80 :             if (list_length(name) < 3)
    2131          32 :                 ereport(ERROR,
    2132             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2133             :                          errmsg("name list length must be at least %d", 3)));
    2134             :             /* fall through to check args length */
    2135             :             /* FALLTHROUGH */
    2136             :         case OBJECT_OPERATOR:
    2137          80 :             if (list_length(args) != 2)
    2138          40 :                 ereport(ERROR,
    2139             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2140             :                          errmsg("argument list length must be exactly %d", 2)));
    2141          40 :             break;
    2142             :         default:
    2143         912 :             break;
    2144             :     }
    2145             : 
    2146             :     /*
    2147             :      * Now build the Node type that get_object_address() expects for the given
    2148             :      * type.
    2149             :      */
    2150        1104 :     switch (type)
    2151             :     {
    2152             :         case OBJECT_TABLE:
    2153             :         case OBJECT_SEQUENCE:
    2154             :         case OBJECT_VIEW:
    2155             :         case OBJECT_MATVIEW:
    2156             :         case OBJECT_INDEX:
    2157             :         case OBJECT_FOREIGN_TABLE:
    2158             :         case OBJECT_COLUMN:
    2159             :         case OBJECT_ATTRIBUTE:
    2160             :         case OBJECT_COLLATION:
    2161             :         case OBJECT_CONVERSION:
    2162             :         case OBJECT_STATISTIC_EXT:
    2163             :         case OBJECT_TSPARSER:
    2164             :         case OBJECT_TSDICTIONARY:
    2165             :         case OBJECT_TSTEMPLATE:
    2166             :         case OBJECT_TSCONFIGURATION:
    2167             :         case OBJECT_DEFAULT:
    2168             :         case OBJECT_POLICY:
    2169             :         case OBJECT_RULE:
    2170             :         case OBJECT_TRIGGER:
    2171             :         case OBJECT_TABCONSTRAINT:
    2172             :         case OBJECT_OPCLASS:
    2173             :         case OBJECT_OPFAMILY:
    2174         656 :             objnode = (Node *) name;
    2175         656 :             break;
    2176             :         case OBJECT_ACCESS_METHOD:
    2177             :         case OBJECT_DATABASE:
    2178             :         case OBJECT_EVENT_TRIGGER:
    2179             :         case OBJECT_EXTENSION:
    2180             :         case OBJECT_FDW:
    2181             :         case OBJECT_FOREIGN_SERVER:
    2182             :         case OBJECT_LANGUAGE:
    2183             :         case OBJECT_PUBLICATION:
    2184             :         case OBJECT_ROLE:
    2185             :         case OBJECT_SCHEMA:
    2186             :         case OBJECT_SUBSCRIPTION:
    2187             :         case OBJECT_TABLESPACE:
    2188         160 :             if (list_length(name) != 1)
    2189          48 :                 ereport(ERROR,
    2190             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2191             :                          errmsg("name list length must be exactly %d", 1)));
    2192         112 :             objnode = linitial(name);
    2193         112 :             break;
    2194             :         case OBJECT_TYPE:
    2195             :         case OBJECT_DOMAIN:
    2196          40 :             objnode = (Node *) typename;
    2197          40 :             break;
    2198             :         case OBJECT_CAST:
    2199             :         case OBJECT_DOMCONSTRAINT:
    2200             :         case OBJECT_TRANSFORM:
    2201          36 :             objnode = (Node *) list_make2(typename, linitial(args));
    2202          36 :             break;
    2203             :         case OBJECT_PUBLICATION_REL:
    2204          20 :             objnode = (Node *) list_make2(name, linitial(args));
    2205          20 :             break;
    2206             :         case OBJECT_USER_MAPPING:
    2207          20 :             objnode = (Node *) list_make2(linitial(name), linitial(args));
    2208          20 :             break;
    2209             :         case OBJECT_DEFACL:
    2210          28 :             objnode = (Node *) lcons(linitial(args), name);
    2211          28 :             break;
    2212             :         case OBJECT_AMOP:
    2213             :         case OBJECT_AMPROC:
    2214          32 :             objnode = (Node *) list_make2(name, args);
    2215          32 :             break;
    2216             :         case OBJECT_FUNCTION:
    2217             :         case OBJECT_PROCEDURE:
    2218             :         case OBJECT_ROUTINE:
    2219             :         case OBJECT_AGGREGATE:
    2220             :         case OBJECT_OPERATOR:
    2221             :             {
    2222         104 :                 ObjectWithArgs *owa = makeNode(ObjectWithArgs);
    2223             : 
    2224         104 :                 owa->objname = name;
    2225         104 :                 owa->objargs = args;
    2226         104 :                 objnode = (Node *) owa;
    2227         104 :                 break;
    2228             :             }
    2229             :         case OBJECT_LARGEOBJECT:
    2230             :             /* already handled above */
    2231           8 :             break;
    2232             :             /* no default, to let compiler warn about missing case */
    2233             :     }
    2234             : 
    2235        1056 :     if (objnode == NULL)
    2236           0 :         elog(ERROR, "unrecognized object type: %d", type);
    2237             : 
    2238        1056 :     addr = get_object_address(type, objnode,
    2239             :                               &relation, AccessShareLock, false);
    2240             : 
    2241             :     /* We don't need the relcache entry, thank you very much */
    2242         392 :     if (relation)
    2243         128 :         relation_close(relation, AccessShareLock);
    2244             : 
    2245         392 :     tupdesc = CreateTemplateTupleDesc(3);
    2246         392 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "classid",
    2247             :                        OIDOID, -1, 0);
    2248         392 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2, "objid",
    2249             :                        OIDOID, -1, 0);
    2250         392 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3, "objsubid",
    2251             :                        INT4OID, -1, 0);
    2252         392 :     tupdesc = BlessTupleDesc(tupdesc);
    2253             : 
    2254         392 :     values[0] = ObjectIdGetDatum(addr.classId);
    2255         392 :     values[1] = ObjectIdGetDatum(addr.objectId);
    2256         392 :     values[2] = Int32GetDatum(addr.objectSubId);
    2257         392 :     nulls[0] = false;
    2258         392 :     nulls[1] = false;
    2259         392 :     nulls[2] = false;
    2260             : 
    2261         392 :     htup = heap_form_tuple(tupdesc, values, nulls);
    2262             : 
    2263         392 :     PG_RETURN_DATUM(HeapTupleGetDatum(htup));
    2264             : }
    2265             : 
    2266             : /*
    2267             :  * Check ownership of an object previously identified by get_object_address.
    2268             :  */
    2269             : void
    2270       18180 : check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address,
    2271             :                        Node *object, Relation relation)
    2272             : {
    2273       18180 :     switch (objtype)
    2274             :     {
    2275             :         case OBJECT_INDEX:
    2276             :         case OBJECT_SEQUENCE:
    2277             :         case OBJECT_TABLE:
    2278             :         case OBJECT_VIEW:
    2279             :         case OBJECT_MATVIEW:
    2280             :         case OBJECT_FOREIGN_TABLE:
    2281             :         case OBJECT_COLUMN:
    2282             :         case OBJECT_RULE:
    2283             :         case OBJECT_TRIGGER:
    2284             :         case OBJECT_POLICY:
    2285             :         case OBJECT_TABCONSTRAINT:
    2286        1020 :             if (!pg_class_ownercheck(RelationGetRelid(relation), roleid))
    2287          16 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2288          16 :                                RelationGetRelationName(relation));
    2289        1004 :             break;
    2290             :         case OBJECT_DATABASE:
    2291         648 :             if (!pg_database_ownercheck(address.objectId, roleid))
    2292           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2293           0 :                                strVal((Value *) object));
    2294         648 :             break;
    2295             :         case OBJECT_TYPE:
    2296             :         case OBJECT_DOMAIN:
    2297             :         case OBJECT_ATTRIBUTE:
    2298          68 :             if (!pg_type_ownercheck(address.objectId, roleid))
    2299           0 :                 aclcheck_error_type(ACLCHECK_NOT_OWNER, address.objectId);
    2300          68 :             break;
    2301             :         case OBJECT_DOMCONSTRAINT:
    2302             :             {
    2303             :                 HeapTuple   tuple;
    2304             :                 Oid         contypid;
    2305             : 
    2306          20 :                 tuple = SearchSysCache1(CONSTROID,
    2307          20 :                                         ObjectIdGetDatum(address.objectId));
    2308          20 :                 if (!HeapTupleIsValid(tuple))
    2309           0 :                     elog(ERROR, "constraint with OID %u does not exist",
    2310             :                          address.objectId);
    2311             : 
    2312          20 :                 contypid = ((Form_pg_constraint) GETSTRUCT(tuple))->contypid;
    2313             : 
    2314          20 :                 ReleaseSysCache(tuple);
    2315             : 
    2316             :                 /*
    2317             :                  * Fallback to type ownership check in this case as this is
    2318             :                  * what domain constraints rely on.
    2319             :                  */
    2320          20 :                 if (!pg_type_ownercheck(contypid, roleid))
    2321           4 :                     aclcheck_error_type(ACLCHECK_NOT_OWNER, contypid);
    2322             :             }
    2323          16 :             break;
    2324             :         case OBJECT_AGGREGATE:
    2325             :         case OBJECT_FUNCTION:
    2326             :         case OBJECT_PROCEDURE:
    2327             :         case OBJECT_ROUTINE:
    2328         884 :             if (!pg_proc_ownercheck(address.objectId, roleid))
    2329          12 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2330          12 :                                NameListToString((castNode(ObjectWithArgs, object))->objname));
    2331         872 :             break;
    2332             :         case OBJECT_OPERATOR:
    2333          26 :             if (!pg_oper_ownercheck(address.objectId, roleid))
    2334           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2335           0 :                                NameListToString((castNode(ObjectWithArgs, object))->objname));
    2336          26 :             break;
    2337             :         case OBJECT_SCHEMA:
    2338         212 :             if (!pg_namespace_ownercheck(address.objectId, roleid))
    2339           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2340           0 :                                strVal((Value *) object));
    2341         212 :             break;
    2342             :         case OBJECT_COLLATION:
    2343           2 :             if (!pg_collation_ownercheck(address.objectId, roleid))
    2344           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2345           0 :                                NameListToString(castNode(List, object)));
    2346           2 :             break;
    2347             :         case OBJECT_CONVERSION:
    2348          18 :             if (!pg_conversion_ownercheck(address.objectId, roleid))
    2349           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2350           0 :                                NameListToString(castNode(List, object)));
    2351          18 :             break;
    2352             :         case OBJECT_EXTENSION:
    2353          56 :             if (!pg_extension_ownercheck(address.objectId, roleid))
    2354           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2355           0 :                                strVal((Value *) object));
    2356          56 :             break;
    2357             :         case OBJECT_FDW:
    2358          80 :             if (!pg_foreign_data_wrapper_ownercheck(address.objectId, roleid))
    2359          12 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2360          12 :                                strVal((Value *) object));
    2361          68 :             break;
    2362             :         case OBJECT_FOREIGN_SERVER:
    2363          66 :             if (!pg_foreign_server_ownercheck(address.objectId, roleid))
    2364          16 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2365          16 :                                strVal((Value *) object));
    2366          50 :             break;
    2367             :         case OBJECT_EVENT_TRIGGER:
    2368          64 :             if (!pg_event_trigger_ownercheck(address.objectId, roleid))
    2369           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2370           0 :                                strVal((Value *) object));
    2371          64 :             break;
    2372             :         case OBJECT_LANGUAGE:
    2373         362 :             if (!pg_language_ownercheck(address.objectId, roleid))
    2374           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2375           0 :                                strVal((Value *) object));
    2376         362 :             break;
    2377             :         case OBJECT_OPCLASS:
    2378           4 :             if (!pg_opclass_ownercheck(address.objectId, roleid))
    2379           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2380           0 :                                NameListToString(castNode(List, object)));
    2381           4 :             break;
    2382             :         case OBJECT_OPFAMILY:
    2383           4 :             if (!pg_opfamily_ownercheck(address.objectId, roleid))
    2384           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2385           0 :                                NameListToString(castNode(List, object)));
    2386           4 :             break;
    2387             :         case OBJECT_LARGEOBJECT:
    2388          28 :             if (!lo_compat_privileges &&
    2389          14 :                 !pg_largeobject_ownercheck(address.objectId, roleid))
    2390           0 :                 ereport(ERROR,
    2391             :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2392             :                          errmsg("must be owner of large object %u",
    2393             :                                 address.objectId)));
    2394          14 :             break;
    2395             :         case OBJECT_CAST:
    2396             :             {
    2397             :                 /* We can only check permissions on the source/target types */
    2398          16 :                 TypeName   *sourcetype = linitial_node(TypeName, castNode(List, object));
    2399          16 :                 TypeName   *targettype = lsecond_node(TypeName, castNode(List, object));
    2400          16 :                 Oid         sourcetypeid = typenameTypeId(NULL, sourcetype);
    2401          16 :                 Oid         targettypeid = typenameTypeId(NULL, targettype);
    2402             : 
    2403          16 :                 if (!pg_type_ownercheck(sourcetypeid, roleid)
    2404           0 :                     && !pg_type_ownercheck(targettypeid, roleid))
    2405           0 :                     ereport(ERROR,
    2406             :                             (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2407             :                              errmsg("must be owner of type %s or type %s",
    2408             :                                     format_type_be(sourcetypeid),
    2409             :                                     format_type_be(targettypeid))));
    2410             :             }
    2411          16 :             break;
    2412             :         case OBJECT_PUBLICATION:
    2413          44 :             if (!pg_publication_ownercheck(address.objectId, roleid))
    2414           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2415           0 :                                strVal((Value *) object));
    2416          44 :             break;
    2417             :         case OBJECT_SUBSCRIPTION:
    2418           8 :             if (!pg_subscription_ownercheck(address.objectId, roleid))
    2419           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2420           0 :                                strVal((Value *) object));
    2421           8 :             break;
    2422             :         case OBJECT_TRANSFORM:
    2423             :             {
    2424          22 :                 TypeName   *typename = linitial_node(TypeName, castNode(List, object));
    2425          22 :                 Oid         typeid = typenameTypeId(NULL, typename);
    2426             : 
    2427          22 :                 if (!pg_type_ownercheck(typeid, roleid))
    2428           0 :                     aclcheck_error_type(ACLCHECK_NOT_OWNER, typeid);
    2429             :             }
    2430          22 :             break;
    2431             :         case OBJECT_TABLESPACE:
    2432           0 :             if (!pg_tablespace_ownercheck(address.objectId, roleid))
    2433           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2434           0 :                                strVal((Value *) object));
    2435           0 :             break;
    2436             :         case OBJECT_TSDICTIONARY:
    2437        7090 :             if (!pg_ts_dict_ownercheck(address.objectId, roleid))
    2438           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2439           0 :                                NameListToString(castNode(List, object)));
    2440        7090 :             break;
    2441             :         case OBJECT_TSCONFIGURATION:
    2442        7086 :             if (!pg_ts_config_ownercheck(address.objectId, roleid))
    2443           0 :                 aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
    2444           0 :                                NameListToString(castNode(List, object)));
    2445        7086 :             break;
    2446             :         case OBJECT_ROLE:
    2447             : 
    2448             :             /*
    2449             :              * We treat roles as being "owned" by those with CREATEROLE priv,
    2450             :              * except that superusers are only owned by superusers.
    2451             :              */
    2452          12 :             if (superuser_arg(address.objectId))
    2453             :             {
    2454           0 :                 if (!superuser_arg(roleid))
    2455           0 :                     ereport(ERROR,
    2456             :                             (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2457             :                              errmsg("must be superuser")));
    2458             :             }
    2459             :             else
    2460             :             {
    2461          12 :                 if (!has_createrole_privilege(roleid))
    2462           2 :                     ereport(ERROR,
    2463             :                             (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2464             :                              errmsg("must have CREATEROLE privilege")));
    2465             :             }
    2466          10 :             break;
    2467             :         case OBJECT_TSPARSER:
    2468             :         case OBJECT_TSTEMPLATE:
    2469             :         case OBJECT_ACCESS_METHOD:
    2470             :             /* We treat these object types as being owned by superusers */
    2471         350 :             if (!superuser_arg(roleid))
    2472           0 :                 ereport(ERROR,
    2473             :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2474             :                          errmsg("must be superuser")));
    2475         350 :             break;
    2476             :         case OBJECT_STATISTIC_EXT:
    2477           4 :             if (!pg_statistics_object_ownercheck(address.objectId, roleid))
    2478           0 :                 aclcheck_error_type(ACLCHECK_NOT_OWNER, address.objectId);
    2479           4 :             break;
    2480             :         default:
    2481           0 :             elog(ERROR, "unrecognized object type: %d",
    2482             :                  (int) objtype);
    2483             :     }
    2484       18118 : }
    2485             : 
    2486             : /*
    2487             :  * get_object_namespace
    2488             :  *
    2489             :  * Find the schema containing the specified object.  For non-schema objects,
    2490             :  * this function returns InvalidOid.
    2491             :  */
    2492             : Oid
    2493        3726 : get_object_namespace(const ObjectAddress *address)
    2494             : {
    2495             :     int         cache;
    2496             :     HeapTuple   tuple;
    2497             :     bool        isnull;
    2498             :     Oid         oid;
    2499             :     const ObjectPropertyType *property;
    2500             : 
    2501             :     /* If not owned by a namespace, just return InvalidOid. */
    2502        3726 :     property = get_object_property_data(address->classId);
    2503        3726 :     if (property->attnum_namespace == InvalidAttrNumber)
    2504        1178 :         return InvalidOid;
    2505             : 
    2506             :     /* Currently, we can only handle object types with system caches. */
    2507        2548 :     cache = property->oid_catcache_id;
    2508             :     Assert(cache != -1);
    2509             : 
    2510             :     /* Fetch tuple from syscache and extract namespace attribute. */
    2511        2548 :     tuple = SearchSysCache1(cache, ObjectIdGetDatum(address->objectId));
    2512        2548 :     if (!HeapTupleIsValid(tuple))
    2513           0 :         elog(ERROR, "cache lookup failed for cache %d oid %u",
    2514             :              cache, address->objectId);
    2515        2548 :     oid = DatumGetObjectId(SysCacheGetAttr(cache,
    2516             :                                            tuple,
    2517             :                                            property->attnum_namespace,
    2518             :                                            &isnull));
    2519             :     Assert(!isnull);
    2520        2548 :     ReleaseSysCache(tuple);
    2521             : 
    2522        2548 :     return oid;
    2523             : }
    2524             : 
    2525             : /*
    2526             :  * Return ObjectType for the given object type as given by
    2527             :  * getObjectTypeDescription; if no valid ObjectType code exists, but it's a
    2528             :  * possible output type from getObjectTypeDescription, return -1.
    2529             :  * Otherwise, an error is thrown.
    2530             :  */
    2531             : int
    2532        1344 : read_objtype_from_string(const char *objtype)
    2533             : {
    2534             :     int         i;
    2535             : 
    2536       37980 :     for (i = 0; i < lengthof(ObjectTypeMap); i++)
    2537             :     {
    2538       37976 :         if (strcmp(ObjectTypeMap[i].tm_name, objtype) == 0)
    2539        1340 :             return ObjectTypeMap[i].tm_type;
    2540             :     }
    2541           4 :     ereport(ERROR,
    2542             :             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    2543             :              errmsg("unrecognized object type \"%s\"", objtype)));
    2544             : 
    2545             :     return -1;                  /* keep compiler quiet */
    2546             : }
    2547             : 
    2548             : /*
    2549             :  * Interfaces to reference fields of ObjectPropertyType
    2550             :  */
    2551             : Oid
    2552         382 : get_object_oid_index(Oid class_id)
    2553             : {
    2554         382 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2555             : 
    2556         382 :     return prop->oid_index_oid;
    2557             : }
    2558             : 
    2559             : int
    2560        4132 : get_object_catcache_oid(Oid class_id)
    2561             : {
    2562        4132 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2563             : 
    2564        4132 :     return prop->oid_catcache_id;
    2565             : }
    2566             : 
    2567             : int
    2568         412 : get_object_catcache_name(Oid class_id)
    2569             : {
    2570         412 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2571             : 
    2572         412 :     return prop->name_catcache_id;
    2573             : }
    2574             : 
    2575             : AttrNumber
    2576        3330 : get_object_attnum_oid(Oid class_id)
    2577             : {
    2578        3330 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2579             : 
    2580        3330 :     return prop->attnum_oid;
    2581             : }
    2582             : 
    2583             : AttrNumber
    2584        3044 : get_object_attnum_name(Oid class_id)
    2585             : {
    2586        3044 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2587             : 
    2588        3044 :     return prop->attnum_name;
    2589             : }
    2590             : 
    2591             : AttrNumber
    2592        3782 : get_object_attnum_namespace(Oid class_id)
    2593             : {
    2594        3782 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2595             : 
    2596        3782 :     return prop->attnum_namespace;
    2597             : }
    2598             : 
    2599             : AttrNumber
    2600        1252 : get_object_attnum_owner(Oid class_id)
    2601             : {
    2602        1252 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2603             : 
    2604        1252 :     return prop->attnum_owner;
    2605             : }
    2606             : 
    2607             : AttrNumber
    2608         840 : get_object_attnum_acl(Oid class_id)
    2609             : {
    2610         840 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2611             : 
    2612         840 :     return prop->attnum_acl;
    2613             : }
    2614             : 
    2615             : /*
    2616             :  * get_object_type
    2617             :  *
    2618             :  * Return the object type associated with a given object.  This routine
    2619             :  * is primarily used to determine the object type to mention in ACL check
    2620             :  * error messages, so it's desirable for it to avoid failing.
    2621             :  */
    2622             : ObjectType
    2623         120 : get_object_type(Oid class_id, Oid object_id)
    2624             : {
    2625         120 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2626             : 
    2627         120 :     if (prop->objtype == OBJECT_TABLE)
    2628             :     {
    2629             :         /*
    2630             :          * If the property data says it's a table, dig a little deeper to get
    2631             :          * the real relation kind, so that callers can produce more precise
    2632             :          * error messages.
    2633             :          */
    2634           0 :         return get_relkind_objtype(get_rel_relkind(object_id));
    2635             :     }
    2636             :     else
    2637         120 :         return prop->objtype;
    2638             : }
    2639             : 
    2640             : bool
    2641        2294 : get_object_namensp_unique(Oid class_id)
    2642             : {
    2643        2294 :     const ObjectPropertyType *prop = get_object_property_data(class_id);
    2644             : 
    2645        2294 :     return prop->is_nsp_name_unique;
    2646             : }
    2647             : 
    2648             : /*
    2649             :  * Return whether we have useful data for the given object class in the
    2650             :  * ObjectProperty table.
    2651             :  */
    2652             : bool
    2653        2926 : is_objectclass_supported(Oid class_id)
    2654             : {
    2655             :     int         index;
    2656             : 
    2657       63236 :     for (index = 0; index < lengthof(ObjectProperty); index++)
    2658             :     {
    2659       62840 :         if (ObjectProperty[index].class_oid == class_id)
    2660        2530 :             return true;
    2661             :     }
    2662             : 
    2663         396 :     return false;
    2664             : }
    2665             : 
    2666             : /*
    2667             :  * Find ObjectProperty structure by class_id.
    2668             :  */
    2669             : static const ObjectPropertyType *
    2670       23314 : get_object_property_data(Oid class_id)
    2671             : {
    2672             :     static const ObjectPropertyType *prop_last = NULL;
    2673             :     int         index;
    2674             : 
    2675             :     /*
    2676             :      * A shortcut to speed up multiple consecutive lookups of a particular
    2677             :      * object class.
    2678             :      */
    2679       23314 :     if (prop_last && prop_last->class_oid == class_id)
    2680       20982 :         return prop_last;
    2681             : 
    2682       41856 :     for (index = 0; index < lengthof(ObjectProperty); index++)
    2683             :     {
    2684       41856 :         if (ObjectProperty[index].class_oid == class_id)
    2685             :         {
    2686        2332 :             prop_last = &ObjectProperty[index];
    2687        2332 :             return &ObjectProperty[index];
    2688             :         }
    2689             :     }
    2690             : 
    2691           0 :     ereport(ERROR,
    2692             :             (errmsg_internal("unrecognized class ID: %u", class_id)));
    2693             : 
    2694             :     return NULL;                /* keep MSC compiler happy */
    2695             : }
    2696             : 
    2697             : /*
    2698             :  * Return a copy of the tuple for the object with the given object OID, from
    2699             :  * the given catalog (which must have been opened by the caller and suitably
    2700             :  * locked).  NULL is returned if the OID is not found.
    2701             :  *
    2702             :  * We try a syscache first, if available.
    2703             :  */
    2704             : HeapTuple
    2705        3720 : get_catalog_object_by_oid(Relation catalog, AttrNumber oidcol, Oid objectId)
    2706             : {
    2707             :     HeapTuple   tuple;
    2708        3720 :     Oid         classId = RelationGetRelid(catalog);
    2709        3720 :     int         oidCacheId = get_object_catcache_oid(classId);
    2710             : 
    2711        3720 :     if (oidCacheId > 0)
    2712             :     {
    2713        3338 :         tuple = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objectId));
    2714        3338 :         if (!HeapTupleIsValid(tuple))   /* should not happen */
    2715           0 :             return NULL;
    2716             :     }
    2717             :     else
    2718             :     {
    2719         382 :         Oid         oidIndexId = get_object_oid_index(classId);
    2720             :         SysScanDesc scan;
    2721             :         ScanKeyData skey;
    2722             : 
    2723             :         Assert(OidIsValid(oidIndexId));
    2724             : 
    2725         382 :         ScanKeyInit(&skey,
    2726             :                     oidcol,
    2727             :                     BTEqualStrategyNumber, F_OIDEQ,
    2728             :                     ObjectIdGetDatum(objectId));
    2729             : 
    2730         382 :         scan = systable_beginscan(catalog, oidIndexId, true,
    2731             :                                   NULL, 1, &skey);
    2732         382 :         tuple = systable_getnext(scan);
    2733         382 :         if (!HeapTupleIsValid(tuple))
    2734             :         {
    2735           0 :             systable_endscan(scan);
    2736           0 :             return NULL;
    2737             :         }
    2738         382 :         tuple = heap_copytuple(tuple);
    2739             : 
    2740         382 :         systable_endscan(scan);
    2741             :     }
    2742             : 
    2743        3720 :     return tuple;
    2744             : }
    2745             : 
    2746             : /*
    2747             :  * getObjectDescription: build an object description for messages
    2748             :  *
    2749             :  * The result is a palloc'd string.
    2750             :  */
    2751             : char *
    2752       81626 : getObjectDescription(const ObjectAddress *object)
    2753             : {
    2754             :     StringInfoData buffer;
    2755             : 
    2756       81626 :     initStringInfo(&buffer);
    2757             : 
    2758       81626 :     switch (getObjectClass(object))
    2759             :     {
    2760             :         case OCLASS_CLASS:
    2761       23786 :             if (object->objectSubId == 0)
    2762       22554 :                 getRelationDescription(&buffer, object->objectId);
    2763             :             else
    2764             :             {
    2765             :                 /* column, not whole relation */
    2766             :                 StringInfoData rel;
    2767             : 
    2768        1232 :                 initStringInfo(&rel);
    2769        1232 :                 getRelationDescription(&rel, object->objectId);
    2770             :                 /* translator: second %s is, e.g., "table %s" */
    2771        2464 :                 appendStringInfo(&buffer, _("column %s of %s"),
    2772             :                                  get_attname(object->objectId,
    2773        1232 :                                              object->objectSubId,
    2774             :                                              false),
    2775             :                                  rel.data);
    2776        1232 :                 pfree(rel.data);
    2777             :             }
    2778       23786 :             break;
    2779             : 
    2780             :         case OCLASS_PROC:
    2781        1800 :             appendStringInfo(&buffer, _("function %s"),
    2782             :                              format_procedure(object->objectId));
    2783        1800 :             break;
    2784             : 
    2785             :         case OCLASS_TYPE:
    2786       38564 :             appendStringInfo(&buffer, _("type %s"),
    2787             :                              format_type_be(object->objectId));
    2788       38564 :             break;
    2789             : 
    2790             :         case OCLASS_CAST:
    2791             :             {
    2792             :                 Relation    castDesc;
    2793             :                 ScanKeyData skey[1];
    2794             :                 SysScanDesc rcscan;
    2795             :                 HeapTuple   tup;
    2796             :                 Form_pg_cast castForm;
    2797             : 
    2798          94 :                 castDesc = table_open(CastRelationId, AccessShareLock);
    2799             : 
    2800          94 :                 ScanKeyInit(&skey[0],
    2801             :                             Anum_pg_cast_oid,
    2802             :                             BTEqualStrategyNumber, F_OIDEQ,
    2803          94 :                             ObjectIdGetDatum(object->objectId));
    2804             : 
    2805          94 :                 rcscan = systable_beginscan(castDesc, CastOidIndexId, true,
    2806             :                                             NULL, 1, skey);
    2807             : 
    2808          94 :                 tup = systable_getnext(rcscan);
    2809             : 
    2810          94 :                 if (!HeapTupleIsValid(tup))
    2811           0 :                     elog(ERROR, "could not find tuple for cast %u",
    2812             :                          object->objectId);
    2813             : 
    2814          94 :                 castForm = (Form_pg_cast) GETSTRUCT(tup);
    2815             : 
    2816          94 :                 appendStringInfo(&buffer, _("cast from %s to %s"),
    2817             :                                  format_type_be(castForm->castsource),
    2818             :                                  format_type_be(castForm->casttarget));
    2819             : 
    2820          94 :                 systable_endscan(rcscan);
    2821          94 :                 table_close(castDesc, AccessShareLock);
    2822          94 :                 break;
    2823             :             }
    2824             : 
    2825             :         case OCLASS_COLLATION:
    2826             :             {
    2827             :                 HeapTuple   collTup;
    2828             :                 Form_pg_collation coll;
    2829             :                 char       *nspname;
    2830             : 
    2831          12 :                 collTup = SearchSysCache1(COLLOID,
    2832          12 :                                           ObjectIdGetDatum(object->objectId));
    2833          12 :                 if (!HeapTupleIsValid(collTup))
    2834           0 :                     elog(ERROR, "cache lookup failed for collation %u",
    2835             :                          object->objectId);
    2836          12 :                 coll = (Form_pg_collation) GETSTRUCT(collTup);
    2837             : 
    2838             :                 /* Qualify the name if not visible in search path */
    2839          12 :                 if (CollationIsVisible(object->objectId))
    2840          12 :                     nspname = NULL;
    2841             :                 else
    2842           0 :                     nspname = get_namespace_name(coll->collnamespace);
    2843             : 
    2844          12 :                 appendStringInfo(&buffer, _("collation %s"),
    2845             :                                  quote_qualified_identifier(nspname,
    2846          12 :                                                             NameStr(coll->collname)));
    2847          12 :                 ReleaseSysCache(collTup);
    2848          12 :                 break;
    2849             :             }
    2850             : 
    2851             :         case OCLASS_CONSTRAINT:
    2852             :             {
    2853             :                 HeapTuple   conTup;
    2854             :                 Form_pg_constraint con;
    2855             : 
    2856        6548 :                 conTup = SearchSysCache1(CONSTROID,
    2857        6548 :                                          ObjectIdGetDatum(object->objectId));
    2858        6548 :                 if (!HeapTupleIsValid(conTup))
    2859           0 :                     elog(ERROR, "cache lookup failed for constraint %u",
    2860             :                          object->objectId);
    2861        6548 :                 con = (Form_pg_constraint) GETSTRUCT(conTup);
    2862             : 
    2863        6548 :                 if (OidIsValid(con->conrelid))
    2864             :                 {
    2865             :                     StringInfoData rel;
    2866             : 
    2867        6436 :                     initStringInfo(&rel);
    2868        6436 :                     getRelationDescription(&rel, con->conrelid);
    2869             :                     /* translator: second %s is, e.g., "table %s" */
    2870       12872 :                     appendStringInfo(&buffer, _("constraint %s on %s"),
    2871        6436 :                                      NameStr(con->conname), rel.data);
    2872        6436 :                     pfree(rel.data);
    2873             :                 }
    2874             :                 else
    2875             :                 {
    2876         112 :                     appendStringInfo(&buffer, _("constraint %s"),
    2877         112 :                                      NameStr(con->conname));
    2878             :                 }
    2879             : 
    2880        6548 :                 ReleaseSysCache(conTup);
    2881        6548 :                 break;
    2882             :             }
    2883             : 
    2884             :         case OCLASS_CONVERSION:
    2885             :             {
    2886             :                 HeapTuple   conTup;
    2887             :                 Form_pg_conversion conv;
    2888             :                 char       *nspname;
    2889             : 
    2890          20 :                 conTup = SearchSysCache1(CONVOID,
    2891          20 :                                          ObjectIdGetDatum(object->objectId));
    2892          20 :                 if (!HeapTupleIsValid(conTup))
    2893           0 :                     elog(ERROR, "cache lookup failed for conversion %u",
    2894             :                          object->objectId);
    2895          20 :                 conv = (Form_pg_conversion) GETSTRUCT(conTup);
    2896             : 
    2897             :                 /* Qualify the name if not visible in search path */
    2898          20 :                 if (ConversionIsVisible(object->objectId))
    2899          12 :                     nspname = NULL;
    2900             :                 else
    2901           8 :                     nspname = get_namespace_name(conv->connamespace);
    2902             : 
    2903          20 :                 appendStringInfo(&buffer, _("conversion %s"),
    2904             :                                  quote_qualified_identifier(nspname,
    2905          20 :                                                             NameStr(conv->conname)));
    2906          20 :                 ReleaseSysCache(conTup);
    2907          20 :                 break;
    2908             :             }
    2909             : 
    2910             :         case OCLASS_DEFAULT:
    2911             :             {
    2912             :                 Relation    attrdefDesc;
    2913             :                 ScanKeyData skey[1];
    2914             :                 SysScanDesc adscan;
    2915             :                 HeapTuple   tup;
    2916             :                 Form_pg_attrdef attrdef;
    2917             :                 ObjectAddress colobject;
    2918             : 
    2919        1082 :                 attrdefDesc = table_open(AttrDefaultRelationId, AccessShareLock);
    2920             : 
    2921        1082 :                 ScanKeyInit(&skey[0],
    2922             :                             Anum_pg_attrdef_oid,
    2923             :                             BTEqualStrategyNumber, F_OIDEQ,
    2924        1082 :                             ObjectIdGetDatum(object->objectId));
    2925             : 
    2926        1082 :                 adscan = systable_beginscan(attrdefDesc, AttrDefaultOidIndexId,
    2927             :                                             true, NULL, 1, skey);
    2928             : 
    2929        1082 :                 tup = systable_getnext(adscan);
    2930             : 
    2931        1082 :                 if (!HeapTupleIsValid(tup))
    2932           0 :                     elog(ERROR, "could not find tuple for attrdef %u",
    2933             :                          object->objectId);
    2934             : 
    2935        1082 :                 attrdef = (Form_pg_attrdef) GETSTRUCT(tup);
    2936             : 
    2937        1082 :                 colobject.classId = RelationRelationId;
    2938        1082 :                 colobject.objectId = attrdef->adrelid;
    2939        1082 :                 colobject.objectSubId = attrdef->adnum;
    2940             : 
    2941             :                 /* translator: %s is typically "column %s of table %s" */
    2942        1082 :                 appendStringInfo(&buffer, _("default value for %s"),
    2943             :                                  getObjectDescription(&colobject));
    2944             : 
    2945        1082 :                 systable_endscan(adscan);
    2946        1082 :                 table_close(attrdefDesc, AccessShareLock);
    2947        1082 :                 break;
    2948             :             }
    2949             : 
    2950             :         case OCLASS_LANGUAGE:
    2951          10 :             appendStringInfo(&buffer, _("language %s"),
    2952             :                              get_language_name(object->objectId, false));
    2953          10 :             break;
    2954             : 
    2955             :         case OCLASS_LARGEOBJECT:
    2956           0 :             appendStringInfo(&buffer, _("large object %u"),
    2957             :                              object->objectId);
    2958           0 :             break;
    2959             : 
    2960             :         case OCLASS_OPERATOR:
    2961         684 :             appendStringInfo(&buffer, _("operator %s"),
    2962             :                              format_operator(object->objectId));
    2963         684 :             break;
    2964             : 
    2965             :         case OCLASS_OPCLASS:
    2966             :             {
    2967             :                 HeapTuple   opcTup;
    2968             :                 Form_pg_opclass opcForm;
    2969             :                 HeapTuple   amTup;
    2970             :                 Form_pg_am  amForm;
    2971             :                 char       *nspname;
    2972             : 
    2973         112 :                 opcTup = SearchSysCache1(CLAOID,
    2974         112 :                                          ObjectIdGetDatum(object->objectId));
    2975         112 :                 if (!HeapTupleIsValid(opcTup))
    2976           0 :                     elog(ERROR, "cache lookup failed for opclass %u",
    2977             :                          object->objectId);
    2978         112 :                 opcForm = (Form_pg_opclass) GETSTRUCT(opcTup);
    2979             : 
    2980         112 :                 amTup = SearchSysCache1(AMOID,
    2981         112 :                                         ObjectIdGetDatum(opcForm->opcmethod));
    2982         112 :                 if (!HeapTupleIsValid(amTup))
    2983           0 :                     elog(ERROR, "cache lookup failed for access method %u",
    2984             :                          opcForm->opcmethod);
    2985         112 :                 amForm = (Form_pg_am) GETSTRUCT(amTup);
    2986             : 
    2987             :                 /* Qualify the name if not visible in search path */
    2988         112 :                 if (OpclassIsVisible(object->objectId))
    2989          92 :                     nspname = NULL;
    2990             :                 else
    2991          20 :                     nspname = get_namespace_name(opcForm->opcnamespace);
    2992             : 
    2993         112 :                 appendStringInfo(&buffer, _("operator class %s for access method %s"),
    2994             :                                  quote_qualified_identifier(nspname,
    2995         112 :                                                             NameStr(opcForm->opcname)),
    2996         112 :                                  NameStr(amForm->amname));
    2997             : 
    2998         112 :                 ReleaseSysCache(amTup);
    2999         112 :                 ReleaseSysCache(opcTup);
    3000         112 :                 break;
    3001             :             }
    3002             : 
    3003             :         case OCLASS_OPFAMILY:
    3004         104 :             getOpFamilyDescription(&buffer, object->objectId);
    3005         104 :             break;
    3006             : 
    3007             :         case OCLASS_AM:
    3008             :             {
    3009             :                 HeapTuple   tup;
    3010             : 
    3011          28 :                 tup = SearchSysCache1(AMOID,
    3012          28 :                                       ObjectIdGetDatum(object->objectId));
    3013          28 :                 if (!HeapTupleIsValid(tup))
    3014           0 :                     elog(ERROR, "cache lookup failed for access method %u",
    3015             :                          object->objectId);
    3016          28 :                 appendStringInfo(&buffer, _("access method %s"),
    3017          28 :                                  NameStr(((Form_pg_am) GETSTRUCT(tup))->amname));
    3018          28 :                 ReleaseSysCache(tup);
    3019          28 :                 break;
    3020             :             }
    3021             : 
    3022             :         case OCLASS_AMOP:
    3023             :             {
    3024             :                 Relation    amopDesc;
    3025             :                 HeapTuple   tup;
    3026             :                 ScanKeyData skey[1];
    3027             :                 SysScanDesc amscan;
    3028             :                 Form_pg_amop amopForm;
    3029             :                 StringInfoData opfam;
    3030             : 
    3031         828 :                 amopDesc = table_open(AccessMethodOperatorRelationId,
    3032             :                                       AccessShareLock);
    3033             : 
    3034         828 :                 ScanKeyInit(&skey[0],
    3035             :                             Anum_pg_amop_oid,
    3036             :                             BTEqualStrategyNumber, F_OIDEQ,
    3037         828 :                             ObjectIdGetDatum(object->objectId));
    3038             : 
    3039         828 :                 amscan = systable_beginscan(amopDesc, AccessMethodOperatorOidIndexId, true,
    3040             :                                             NULL, 1, skey);
    3041             : 
    3042         828 :                 tup = systable_getnext(amscan);
    3043             : 
    3044         828 :                 if (!HeapTupleIsValid(tup))
    3045           0 :                     elog(ERROR, "could not find tuple for amop entry %u",
    3046             :                          object->objectId);
    3047             : 
    3048         828 :                 amopForm = (Form_pg_amop) GETSTRUCT(tup);
    3049             : 
    3050         828 :                 initStringInfo(&opfam);
    3051         828 :                 getOpFamilyDescription(&opfam, amopForm->amopfamily);
    3052             : 
    3053             :                 /*------
    3054             :                    translator: %d is the operator strategy (a number), the
    3055             :                    first two %s's are data type names, the third %s is the
    3056             :                    description of the operator family, and the last %s is the
    3057             :                    textual form of the operator with arguments.  */
    3058        1656 :                 appendStringInfo(&buffer, _("operator %d (%s, %s) of %s: %s"),
    3059         828 :                                  amopForm->amopstrategy,
    3060             :                                  format_type_be(amopForm->amoplefttype),
    3061             :                                  format_type_be(amopForm->amoprighttype),
    3062             :                                  opfam.data,
    3063             :                                  format_operator(amopForm->amopopr));
    3064             : 
    3065         828 :                 pfree(opfam.data);
    3066             : 
    3067         828 :                 systable_endscan(amscan);
    3068         828 :                 table_close(amopDesc, AccessShareLock);
    3069         828 :                 break;
    3070             :             }
    3071             : 
    3072             :         case OCLASS_AMPROC:
    3073             :             {
    3074             :                 Relation    amprocDesc;
    3075             :                 ScanKeyData skey[1];
    3076             :                 SysScanDesc amscan;
    3077             :                 HeapTuple   tup;
    3078             :                 Form_pg_amproc amprocForm;
    3079             :                 StringInfoData opfam;
    3080             : 
    3081         302 :                 amprocDesc = table_open(AccessMethodProcedureRelationId,
    3082             :                                         AccessShareLock);
    3083             : 
    3084         302 :                 ScanKeyInit(&skey[0],
    3085             :                             Anum_pg_amproc_oid,
    3086             :                             BTEqualStrategyNumber, F_OIDEQ,
    3087         302 :                             ObjectIdGetDatum(object->objectId));
    3088             : 
    3089         302 :                 amscan = systable_beginscan(amprocDesc, AccessMethodProcedureOidIndexId, true,
    3090             :                                             NULL, 1, skey);
    3091             : 
    3092         302 :                 tup = systable_getnext(amscan);
    3093             : 
    3094         302 :                 if (!HeapTupleIsValid(tup))
    3095           0 :                     elog(ERROR, "could not find tuple for amproc entry %u",
    3096             :                          object->objectId);
    3097             : 
    3098         302 :                 amprocForm = (Form_pg_amproc) GETSTRUCT(tup);
    3099             : 
    3100         302 :                 initStringInfo(&opfam);
    3101         302 :                 getOpFamilyDescription(&opfam, amprocForm->amprocfamily);
    3102             : 
    3103             :                 /*------
    3104             :                    translator: %d is the function number, the first two %s's
    3105             :                    are data type names, the third %s is the description of the
    3106             :                    operator family, and the last %s is the textual form of the
    3107             :                    function with arguments.  */
    3108         604 :                 appendStringInfo(&buffer, _("function %d (%s, %s) of %s: %s"),
    3109         302 :                                  amprocForm->amprocnum,
    3110             :                                  format_type_be(amprocForm->amproclefttype),
    3111             :                                  format_type_be(amprocForm->amprocrighttype),
    3112             :                                  opfam.data,
    3113             :                                  format_procedure(amprocForm->amproc));
    3114             : 
    3115         302 :                 pfree(opfam.data);
    3116             : 
    3117         302 :                 systable_endscan(amscan);
    3118         302 :                 table_close(amprocDesc, AccessShareLock);
    3119         302 :                 break;
    3120             :             }
    3121             : 
    3122             :         case OCLASS_REWRITE:
    3123             :             {
    3124             :                 Relation    ruleDesc;
    3125             :                 ScanKeyData skey[1];
    3126             :                 SysScanDesc rcscan;
    3127             :                 HeapTuple   tup;
    3128             :                 Form_pg_rewrite rule;
    3129             :                 StringInfoData rel;
    3130             : 
    3131        1286 :                 ruleDesc = table_open(RewriteRelationId, AccessShareLock);
    3132             : 
    3133        1286 :                 ScanKeyInit(&skey[0],
    3134             :                             Anum_pg_rewrite_oid,
    3135             :                             BTEqualStrategyNumber, F_OIDEQ,
    3136        1286 :                             ObjectIdGetDatum(object->objectId));
    3137             : 
    3138        1286 :                 rcscan = systable_beginscan(ruleDesc, RewriteOidIndexId, true,
    3139             :                                             NULL, 1, skey);
    3140             : 
    3141        1286 :                 tup = systable_getnext(rcscan);
    3142             : 
    3143        1286 :                 if (!HeapTupleIsValid(tup))
    3144           0 :                     elog(ERROR, "could not find tuple for rule %u",
    3145             :                          object->objectId);
    3146        1286 :                 rule = (Form_pg_rewrite) GETSTRUCT(tup);
    3147             : 
    3148        1286 :                 initStringInfo(&rel);
    3149        1286 :                 getRelationDescription(&rel, rule->ev_class);
    3150             : 
    3151             :                 /* translator: second %s is, e.g., "table %s" */
    3152        2572 :                 appendStringInfo(&buffer, _("rule %s on %s"),
    3153        1286 :                                  NameStr(rule->rulename), rel.data);
    3154        1286 :                 pfree(rel.data);
    3155        1286 :                 systable_endscan(rcscan);
    3156        1286 :                 table_close(ruleDesc, AccessShareLock);
    3157        1286 :                 break;
    3158             :             }
    3159             : 
    3160             :         case OCLASS_TRIGGER:
    3161             :             {
    3162             :                 Relation    trigDesc;
    3163             :                 ScanKeyData skey[1];
    3164             :                 SysScanDesc tgscan;
    3165             :                 HeapTuple   tup;
    3166             :                 Form_pg_trigger trig;
    3167             :                 StringInfoData rel;
    3168             : 
    3169        5532 :                 trigDesc = table_open(TriggerRelationId, AccessShareLock);
    3170             : 
    3171        5532 :                 ScanKeyInit(&skey[0],
    3172             :                             Anum_pg_trigger_oid,
    3173             :                             BTEqualStrategyNumber, F_OIDEQ,
    3174        5532 :                             ObjectIdGetDatum(object->objectId));
    3175             : 
    3176        5532 :                 tgscan = systable_beginscan(trigDesc, TriggerOidIndexId, true,
    3177             :                                             NULL, 1, skey);
    3178             : 
    3179        5532 :                 tup = systable_getnext(tgscan);
    3180             : 
    3181        5532 :                 if (!HeapTupleIsValid(tup))
    3182           0 :                     elog(ERROR, "could not find tuple for trigger %u",
    3183             :                          object->objectId);
    3184        5532 :                 trig = (Form_pg_trigger) GETSTRUCT(tup);
    3185             : 
    3186        5532 :                 initStringInfo(&rel);
    3187        5532 :                 getRelationDescription(&rel, trig->tgrelid);
    3188             : 
    3189             :                 /* translator: second %s is, e.g., "table %s" */
    3190       11064 :                 appendStringInfo(&buffer, _("trigger %s on %s"),
    3191        5532 :                                  NameStr(trig->tgname), rel.data);
    3192        5532 :                 pfree(rel.data);
    3193        5532 :                 systable_endscan(tgscan);
    3194        5532 :                 table_close(trigDesc, AccessShareLock);
    3195        5532 :                 break;
    3196             :             }
    3197             : 
    3198             :         case OCLASS_SCHEMA:
    3199             :             {
    3200             :                 char       *nspname;
    3201             : 
    3202          80 :                 nspname = get_namespace_name(object->objectId);
    3203          80 :                 if (!nspname)
    3204           0 :                     elog(ERROR, "cache lookup failed for namespace %u",
    3205             :                          object->objectId);
    3206          80 :                 appendStringInfo(&buffer, _("schema %s"), nspname);
    3207          80 :                 break;
    3208             :             }
    3209             : 
    3210             :         case OCLASS_STATISTIC_EXT:
    3211             :             {
    3212             :                 HeapTuple   stxTup;
    3213             :                 Form_pg_statistic_ext stxForm;
    3214             :                 char       *nspname;
    3215             : 
    3216          72 :                 stxTup = SearchSysCache1(STATEXTOID,
    3217          72 :                                          ObjectIdGetDatum(object->objectId));
    3218          72 :                 if (!HeapTupleIsValid(stxTup))
    3219           0 :                     elog(ERROR, "could not find tuple for statistics object %u",
    3220             :                          object->objectId);
    3221          72 :                 stxForm = (Form_pg_statistic_ext) GETSTRUCT(stxTup);
    3222             : 
    3223             :                 /* Qualify the name if not visible in search path */
    3224          72 :                 if (StatisticsObjIsVisible(object->objectId))
    3225          44 :                     nspname = NULL;
    3226             :                 else
    3227          28 :                     nspname = get_namespace_name(stxForm->stxnamespace);
    3228             : 
    3229          72 :                 appendStringInfo(&buffer, _("statistics object %s"),
    3230             :                                  quote_qualified_identifier(nspname,
    3231          72 :                                                             NameStr(stxForm->stxname)));
    3232             : 
    3233          72 :                 ReleaseSysCache(stxTup);
    3234          72 :                 break;
    3235             :             }
    3236             : 
    3237             :         case OCLASS_TSPARSER:
    3238             :             {
    3239             :                 HeapTuple   tup;
    3240             :                 Form_pg_ts_parser prsForm;
    3241             :                 char       *nspname;
    3242             : 
    3243          20 :                 tup = SearchSysCache1(TSPARSEROID,
    3244          20 :                                       ObjectIdGetDatum(object->objectId));
    3245          20 :                 if (!HeapTupleIsValid(tup))
    3246           0 :                     elog(ERROR, "cache lookup failed for text search parser %u",
    3247             :                          object->objectId);
    3248          20 :                 prsForm = (Form_pg_ts_parser) GETSTRUCT(tup);
    3249             : 
    3250             :                 /* Qualify the name if not visible in search path */
    3251          20 :                 if (TSParserIsVisible(object->objectId))
    3252          12 :                     nspname = NULL;
    3253             :                 else
    3254           8 :                     nspname = get_namespace_name(prsForm->prsnamespace);
    3255             : 
    3256          20 :                 appendStringInfo(&buffer, _("text search parser %s"),
    3257             :                                  quote_qualified_identifier(nspname,
    3258          20 :                                                             NameStr(prsForm->prsname)));
    3259          20 :                 ReleaseSysCache(tup);
    3260          20 :                 break;
    3261             :             }
    3262             : 
    3263             :         case OCLASS_TSDICT:
    3264             :             {
    3265             :                 HeapTuple   tup;
    3266             :                 Form_pg_ts_dict dictForm;
    3267             :                 char       *nspname;
    3268             : 
    3269          24 :                 tup = SearchSysCache1(TSDICTOID,
    3270          24 :                                       ObjectIdGetDatum(object->objectId));
    3271          24 :                 if (!HeapTupleIsValid(tup))
    3272           0 :                     elog(ERROR, "cache lookup failed for text search dictionary %u",
    3273             :                          object->objectId);
    3274          24 :                 dictForm = (Form_pg_ts_dict) GETSTRUCT(tup);
    3275             : 
    3276             :                 /* Qualify the name if not visible in search path */
    3277          24 :                 if (TSDictionaryIsVisible(object->objectId))
    3278          16 :                     nspname = NULL;
    3279             :                 else
    3280           8 :                     nspname = get_namespace_name(dictForm->dictnamespace);
    3281             : 
    3282          24 :                 appendStringInfo(&buffer, _("text search dictionary %s"),
    3283             :                                  quote_qualified_identifier(nspname,
    3284          24 :                                                             NameStr(dictForm->dictname)));
    3285          24 :                 ReleaseSysCache(tup);
    3286          24 :                 break;
    3287             :             }
    3288             : 
    3289             :         case OCLASS_TSTEMPLATE:
    3290             :             {
    3291             :                 HeapTuple   tup;
    3292             :                 Form_pg_ts_template tmplForm;
    3293             :                 char       *nspname;
    3294             : 
    3295          20 :                 tup = SearchSysCache1(TSTEMPLATEOID,
    3296          20 :                                       ObjectIdGetDatum(object->objectId));
    3297          20 :                 if (!HeapTupleIsValid(tup))
    3298           0 :                     elog(ERROR, "cache lookup failed for text search template %u",
    3299             :                          object->objectId);
    3300          20 :                 tmplForm = (Form_pg_ts_template) GETSTRUCT(tup);
    3301             : 
    3302             :                 /* Qualify the name if not visible in search path */
    3303          20 :                 if (TSTemplateIsVisible(object->objectId))
    3304          12 :                     nspname = NULL;
    3305             :                 else
    3306           8 :                     nspname = get_namespace_name(tmplForm->tmplnamespace);
    3307             : 
    3308          20 :                 appendStringInfo(&buffer, _("text search template %s"),
    3309             :                                  quote_qualified_identifier(nspname,
    3310          20 :                                                             NameStr(tmplForm->tmplname)));
    3311          20 :                 ReleaseSysCache(tup);
    3312          20 :                 break;
    3313             :             }
    3314             : 
    3315             :         case OCLASS_TSCONFIG:
    3316             :             {
    3317             :                 HeapTuple   tup;
    3318             :                 Form_pg_ts_config cfgForm;
    3319             :                 char       *nspname;
    3320             : 
    3321          24 :                 tup = SearchSysCache1(TSCONFIGOID,
    3322          24 :                                       ObjectIdGetDatum(object->objectId));
    3323          24 :                 if (!HeapTupleIsValid(tup))
    3324           0 :                     elog(ERROR, "cache lookup failed for text search configuration %u",
    3325             :                          object->objectId);
    3326          24 :                 cfgForm = (Form_pg_ts_config) GETSTRUCT(tup);
    3327             : 
    3328             :                 /* Qualify the name if not visible in search path */
    3329          24 :                 if (TSConfigIsVisible(object->objectId))
    3330          16 :                     nspname = NULL;
    3331             :                 else
    3332           8 :                     nspname = get_namespace_name(cfgForm->cfgnamespace);
    3333             : 
    3334          24 :                 appendStringInfo(&buffer, _("text search configuration %s"),
    3335             :                                  quote_qualified_identifier(nspname,
    3336          24 :                                                             NameStr(cfgForm->cfgname)));
    3337          24 :                 ReleaseSysCache(tup);
    3338          24 :                 break;
    3339             :             }
    3340             : 
    3341             :         case OCLASS_ROLE:
    3342             :             {
    3343           0 :                 appendStringInfo(&buffer, _("role %s"),
    3344             :                                  GetUserNameFromId(object->objectId, false));
    3345           0 :                 break;
    3346             :             }
    3347             : 
    3348             :         case OCLASS_DATABASE:
    3349             :             {
    3350             :                 char       *datname;
    3351             : 
    3352           8 :                 datname = get_database_name(object->objectId);
    3353           8 :                 if (!datname)
    3354           0 :                     elog(ERROR, "cache lookup failed for database %u",
    3355             :                          object->objectId);
    3356           8 :                 appendStringInfo(&buffer, _("database %s"), datname);
    3357           8 :                 break;
    3358             :             }
    3359             : 
    3360             :         case OCLASS_TBLSPACE:
    3361             :             {
    3362             :                 char       *tblspace;
    3363             : 
    3364           0 :                 tblspace = get_tablespace_name(object->objectId);
    3365           0 :                 if (!tblspace)
    3366           0 :                     elog(ERROR, "cache lookup failed for tablespace %u",
    3367             :                          object->objectId);
    3368           0 :                 appendStringInfo(&buffer, _("tablespace %s"), tblspace);
    3369           0 :                 break;
    3370             :             }
    3371             : 
    3372             :         case OCLASS_FDW:
    3373             :             {
    3374             :                 ForeignDataWrapper *fdw;
    3375             : 
    3376          42 :                 fdw = GetForeignDataWrapper(object->objectId);
    3377          42 :                 appendStringInfo(&buffer, _("foreign-data wrapper %s"), fdw->fdwname);
    3378          42 :                 break;
    3379             :             }
    3380             : 
    3381             :         case OCLASS_FOREIGN_SERVER:
    3382             :             {
    3383             :                 ForeignServer *srv;
    3384             : 
    3385          78 :                 srv = GetForeignServer(object->objectId);
    3386          78 :                 appendStringInfo(&buffer, _("server %s"), srv->servername);
    3387          78 :                 break;
    3388             :             }
    3389             : 
    3390             :         case OCLASS_USER_MAPPING:
    3391             :             {
    3392             :                 HeapTuple   tup;
    3393             :                 Oid         useid;
    3394             :                 char       *usename;
    3395             :                 Form_pg_user_mapping umform;
    3396             :                 ForeignServer *srv;
    3397             : 
    3398          84 :                 tup = SearchSysCache1(USERMAPPINGOID,
    3399          84 :                                       ObjectIdGetDatum(object->objectId));
    3400          84 :                 if (!HeapTupleIsValid(tup))
    3401           0 :                     elog(ERROR, "cache lookup failed for user mapping %u",
    3402             :                          object->objectId);
    3403          84 :                 umform = (Form_pg_user_mapping) GETSTRUCT(tup);
    3404          84 :                 useid = umform->umuser;
    3405          84 :                 srv = GetForeignServer(umform->umserver);
    3406             : 
    3407          84 :                 ReleaseSysCache(tup);
    3408             : 
    3409          84 :                 if (OidIsValid(useid))
    3410          68 :                     usename = GetUserNameFromId(useid, false);
    3411             :                 else
    3412          16 :                     usename = "public";
    3413             : 
    3414          84 :                 appendStringInfo(&buffer, _("user mapping for %s on server %s"), usename,
    3415             :                                  srv->servername);
    3416          84 :                 break;
    3417             :             }
    3418             : 
    3419             :         case OCLASS_DEFACL:
    3420             :             {
    3421             :                 Relation    defaclrel;
    3422             :                 ScanKeyData skey[1];
    3423             :                 SysScanDesc rcscan;
    3424             :                 HeapTuple   tup;
    3425             :                 Form_pg_default_acl defacl;
    3426             :                 char       *rolename;
    3427             :                 char       *nspname;
    3428             : 
    3429          28 :                 defaclrel = table_open(DefaultAclRelationId, AccessShareLock);
    3430             : 
    3431          28 :                 ScanKeyInit(&skey[0],
    3432             :                             Anum_pg_default_acl_oid,
    3433             :                             BTEqualStrategyNumber, F_OIDEQ,
    3434          28 :                             ObjectIdGetDatum(object->objectId));
    3435             : 
    3436          28 :                 rcscan = systable_beginscan(defaclrel, DefaultAclOidIndexId,
    3437             :                                             true, NULL, 1, skey);
    3438             : 
    3439          28 :                 tup = systable_getnext(rcscan);
    3440             : 
    3441          28 :                 if (!HeapTupleIsValid(tup))
    3442           0 :                     elog(ERROR, "could not find tuple for default ACL %u",
    3443             :                          object->objectId);
    3444             : 
    3445          28 :                 defacl = (Form_pg_default_acl) GETSTRUCT(tup);
    3446             : 
    3447          28 :                 rolename = GetUserNameFromId(defacl->defaclrole, false);
    3448             : 
    3449          28 :                 if (OidIsValid(defacl->defaclnamespace))
    3450          20 :                     nspname = get_namespace_name(defacl->defaclnamespace);
    3451             :                 else
    3452           8 :                     nspname = NULL;
    3453             : 
    3454          28 :                 switch (defacl->defaclobjtype)
    3455             :                 {
    3456             :                     case DEFACLOBJ_RELATION:
    3457          20 :                         if (nspname)
    3458          12 :                             appendStringInfo(&buffer,
    3459          12 :                                              _("default privileges on new relations belonging to role %s in schema %s"),
    3460             :                                              rolename, nspname);
    3461             :                         else
    3462           8 :                             appendStringInfo(&buffer,
    3463           8 :                                              _("default privileges on new relations belonging to role %s"),
    3464             :                                              rolename);
    3465          20 :                         break;
    3466             :                     case DEFACLOBJ_SEQUENCE:
    3467           0 :                         if (nspname)
    3468           0 :                             appendStringInfo(&buffer,
    3469           0 :                                              _("default privileges on new sequences belonging to role %s in schema %s"),
    3470             :                                              rolename, nspname);
    3471             :                         else
    3472           0 :                             appendStringInfo(&buffer,
    3473           0 :                                              _("default privileges on new sequences belonging to role %s"),
    3474             :                                              rolename);
    3475           0 :                         break;
    3476             :                     case DEFACLOBJ_FUNCTION:
    3477           4 :                         if (nspname)
    3478           4 :                             appendStringInfo(&buffer,
    3479           4 :                                              _("default privileges on new functions belonging to role %s in schema %s"),
    3480             :                                              rolename, nspname);
    3481             :                         else
    3482           0 :                             appendStringInfo(&buffer,
    3483           0 :                                              _("default privileges on new functions belonging to role %s"),
    3484             :                                              rolename);
    3485           4 :                         break;
    3486             :                     case DEFACLOBJ_TYPE:
    3487           4 :                         if (nspname)
    3488           4 :                             appendStringInfo(&buffer,
    3489           4 :                                              _("default privileges on new types belonging to role %s in schema %s"),
    3490             :                                              rolename, nspname);
    3491             :                         else
    3492           0 :                             appendStringInfo(&buffer,
    3493           0 :                                              _("default privileges on new types belonging to role %s"),
    3494             :                                              rolename);
    3495           4 :                         break;
    3496             :                     case DEFACLOBJ_NAMESPACE:
    3497             :                         Assert(!nspname);
    3498           0 :                         appendStringInfo(&buffer,
    3499           0 :                                          _("default privileges on new schemas belonging to role %s"),
    3500             :                                          rolename);
    3501           0 :                         break;
    3502             :                     default:
    3503             :                         /* shouldn't get here */
    3504           0 :                         if (nspname)
    3505           0 :                             appendStringInfo(&buffer,
    3506           0 :                                              _("default privileges belonging to role %s in schema %s"),
    3507             :                                              rolename, nspname);
    3508             :                         else
    3509           0 :                             appendStringInfo(&buffer,
    3510           0 :                                              _("default privileges belonging to role %s"),
    3511             :                                              rolename);
    3512           0 :                         break;
    3513             :                 }
    3514             : 
    3515          28 :                 systable_endscan(rcscan);
    3516          28 :                 table_close(defaclrel, AccessShareLock);
    3517          28 :                 break;
    3518             :             }
    3519             : 
    3520             :         case OCLASS_EXTENSION:
    3521             :             {
    3522             :                 char       *extname;
    3523             : 
    3524          36 :                 extname = get_extension_name(object->objectId);
    3525          36 :                 if (!extname)
    3526           0 :                     elog(ERROR, "cache lookup failed for extension %u",
    3527             :                          object->objectId);
    3528          36 :                 appendStringInfo(&buffer, _("extension %s"), extname);
    3529          36 :                 break;
    3530             :             }
    3531             : 
    3532             :         case OCLASS_EVENT_TRIGGER:
    3533             :             {
    3534             :                 HeapTuple   tup;
    3535             : 
    3536           8 :                 tup = SearchSysCache1(EVENTTRIGGEROID,
    3537           8 :                                       ObjectIdGetDatum(object->objectId));
    3538           8 :                 if (!HeapTupleIsValid(tup))
    3539           0 :                     elog(ERROR, "cache lookup failed for event trigger %u",
    3540             :                          object->objectId);
    3541           8 :                 appendStringInfo(&buffer, _("event trigger %s"),
    3542           8 :                                  NameStr(((Form_pg_event_trigger) GETSTRUCT(tup))->evtname));
    3543           8 :                 ReleaseSysCache(tup);
    3544           8 :                 break;
    3545             :             }
    3546             : 
    3547             :         case OCLASS_POLICY:
    3548             :             {
    3549             :                 Relation    policy_rel;
    3550             :                 ScanKeyData skey[1];
    3551             :                 SysScanDesc sscan;
    3552             :                 HeapTuple   tuple;
    3553             :                 Form_pg_policy form_policy;
    3554             :                 StringInfoData rel;
    3555             : 
    3556         258 :                 policy_rel = table_open(PolicyRelationId, AccessShareLock);
    3557             : 
    3558         258 :                 ScanKeyInit(&skey[0],
    3559             :                             Anum_pg_policy_oid,
    3560             :                             BTEqualStrategyNumber, F_OIDEQ,
    3561         258 :                             ObjectIdGetDatum(object->objectId));
    3562             : 
    3563         258 :                 sscan = systable_beginscan(policy_rel, PolicyOidIndexId,
    3564             :                                            true, NULL, 1, skey);
    3565             : 
    3566         258 :                 tuple = systable_getnext(sscan);
    3567             : 
    3568         258 :                 if (!HeapTupleIsValid(tuple))
    3569           0 :                     elog(ERROR, "could not find tuple for policy %u",
    3570             :                          object->objectId);
    3571         258 :                 form_policy = (Form_pg_policy) GETSTRUCT(tuple);
    3572             : 
    3573         258 :                 initStringInfo(&rel);
    3574         258 :                 getRelationDescription(&rel, form_policy->polrelid);
    3575             : 
    3576             :                 /* translator: second %s is, e.g., "table %s" */
    3577         516 :                 appendStringInfo(&buffer, _("policy %s on %s"),
    3578         258 :                                  NameStr(form_policy->polname), rel.data);
    3579         258 :                 pfree(rel.data);
    3580         258 :                 systable_endscan(sscan);
    3581         258 :                 table_close(policy_rel, AccessShareLock);
    3582         258 :                 break;
    3583             :             }
    3584             : 
    3585             :         case OCLASS_PUBLICATION:
    3586             :             {
    3587           0 :                 appendStringInfo(&buffer, _("publication %s"),
    3588             :                                  get_publication_name(object->objectId,
    3589             :                                                       false));
    3590           0 :                 break;
    3591             :             }
    3592             : 
    3593             :         case OCLASS_PUBLICATION_REL:
    3594             :             {
    3595             :                 HeapTuple   tup;
    3596             :                 char       *pubname;
    3597             :                 Form_pg_publication_rel prform;
    3598             :                 StringInfoData rel;
    3599             : 
    3600          38 :                 tup = SearchSysCache1(PUBLICATIONREL,
    3601          38 :                                       ObjectIdGetDatum(object->objectId));
    3602          38 :                 if (!HeapTupleIsValid(tup))
    3603           0 :                     elog(ERROR, "cache lookup failed for publication table %u",
    3604             :                          object->objectId);
    3605             : 
    3606          38 :                 prform = (Form_pg_publication_rel) GETSTRUCT(tup);
    3607          38 :                 pubname = get_publication_name(prform->prpubid, false);
    3608             : 
    3609          38 :                 initStringInfo(&rel);
    3610          38 :                 getRelationDescription(&rel, prform->prrelid);
    3611             : 
    3612             :                 /* translator: first %s is, e.g., "table %s" */
    3613          38 :                 appendStringInfo(&buffer, _("publication of %s in publication %s"),
    3614             :                                  rel.data, pubname);
    3615          38 :                 pfree(rel.data);
    3616          38 :                 ReleaseSysCache(tup);
    3617          38 :                 break;
    3618             :             }
    3619             : 
    3620             :         case OCLASS_SUBSCRIPTION:
    3621             :             {
    3622           0 :                 appendStringInfo(&buffer, _("subscription %s"),
    3623             :                                  get_subscription_name(object->objectId,
    3624             :                                                        false));
    3625           0 :                 break;
    3626             :             }
    3627             : 
    3628             :         case OCLASS_TRANSFORM:
    3629             :             {
    3630             :                 HeapTuple   trfTup;
    3631             :                 Form_pg_transform trfForm;
    3632             : 
    3633          14 :                 trfTup = SearchSysCache1(TRFOID,
    3634          14 :                                          ObjectIdGetDatum(object->objectId));
    3635          14 :                 if (!HeapTupleIsValid(trfTup))
    3636           0 :                     elog(ERROR, "could not find tuple for transform %u",
    3637             :                          object->objectId);
    3638             : 
    3639          14 :                 trfForm = (Form_pg_transform) GETSTRUCT(trfTup);
    3640             : 
    3641          14 :                 appendStringInfo(&buffer, _("transform for %s language %s"),
    3642             :                                  format_type_be(trfForm->trftype),
    3643             :                                  get_language_name(trfForm->trflang, false));
    3644             : 
    3645          14 :                 ReleaseSysCache(trfTup);
    3646          14 :                 break;
    3647             :             }
    3648             : 
    3649             :             /*
    3650             :              * There's intentionally no default: case here; we want the
    3651             :              * compiler to warn if a new OCLASS hasn't been handled above.
    3652             :              */
    3653             :     }
    3654             : 
    3655       81626 :     return buffer.data;
    3656             : }
    3657             : 
    3658             : /*
    3659             :  * getObjectDescriptionOids: as above, except the object is specified by Oids
    3660             :  */
    3661             : char *
    3662           0 : getObjectDescriptionOids(Oid classid, Oid objid)
    3663             : {
    3664             :     ObjectAddress address;
    3665             : 
    3666           0 :     address.classId = classid;
    3667           0 :     address.objectId = objid;
    3668           0 :     address.objectSubId = 0;
    3669             : 
    3670           0 :     return getObjectDescription(&address);
    3671             : }
    3672             : 
    3673             : /*
    3674             :  * subroutine for getObjectDescription: describe a relation
    3675             :  *
    3676             :  * The result is appended to "buffer".
    3677             :  */
    3678             : static void
    3679       37336 : getRelationDescription(StringInfo buffer, Oid relid)
    3680             : {
    3681             :     HeapTuple   relTup;
    3682             :     Form_pg_class relForm;
    3683             :     char       *nspname;
    3684             :     char       *relname;
    3685             : 
    3686       37336 :     relTup = SearchSysCache1(RELOID,
    3687             :                              ObjectIdGetDatum(relid));
    3688       37336 :     if (!HeapTupleIsValid(relTup))
    3689           0 :         elog(ERROR, "cache lookup failed for relation %u", relid);
    3690       37336 :     relForm = (Form_pg_class) GETSTRUCT(relTup);
    3691             : 
    3692             :     /* Qualify the name if not visible in search path */
    3693       37336 :     if (RelationIsVisible(relid))
    3694       25876 :         nspname = NULL;
    3695             :     else
    3696       11460 :         nspname = get_namespace_name(relForm->relnamespace);
    3697             : 
    3698       37336 :     relname = quote_qualified_identifier(nspname, NameStr(relForm->relname));
    3699             : 
    3700       37336 :     switch (relForm->relkind)
    3701             :     {
    3702             :         case RELKIND_RELATION:
    3703             :         case RELKIND_PARTITIONED_TABLE:
    3704       17670 :             appendStringInfo(buffer, _("table %s"),
    3705             :                              relname);
    3706       17670 :             break;
    3707             :         case RELKIND_INDEX:
    3708             :         case RELKIND_PARTITIONED_INDEX:
    3709       11618 :             appendStringInfo(buffer, _("index %s"),
    3710             :                              relname);
    3711       11618 :             break;
    3712             :         case RELKIND_SEQUENCE:
    3713         434 :             appendStringInfo(buffer, _("sequence %s"),
    3714             :                              relname);
    3715         434 :             break;
    3716             :         case RELKIND_TOASTVALUE:
    3717        5134 :             appendStringInfo(buffer, _("toast table %s"),
    3718             :                              relname);
    3719        5134 :             break;
    3720             :         case RELKIND_VIEW:
    3721        1830 :             appendStringInfo(buffer, _("view %s"),
    3722             :                              relname);
    3723        1830 :             break;
    3724             :         case RELKIND_MATVIEW:
    3725         284 :             appendStringInfo(buffer, _("materialized view %s"),
    3726             :                              relname);
    3727         284 :             break;
    3728             :         case RELKIND_COMPOSITE_TYPE:
    3729         228 :             appendStringInfo(buffer, _("composite type %s"),
    3730             :                              relname);
    3731         228 :             break;
    3732             :         case RELKIND_FOREIGN_TABLE:
    3733         138 :             appendStringInfo(buffer, _("foreign table %s"),
    3734             :                              relname);
    3735         138 :             break;
    3736             :         default:
    3737             :             /* shouldn't get here */
    3738           0 :             appendStringInfo(buffer, _("relation %s"),
    3739             :                              relname);
    3740           0 :             break;
    3741             :     }
    3742             : 
    3743       37336 :     ReleaseSysCache(relTup);
    3744       37336 : }
    3745             : 
    3746             : /*
    3747             :  * subroutine for getObjectDescription: describe an operator family
    3748             :  */
    3749             : static void
    3750        1234 : getOpFamilyDescription(StringInfo buffer, Oid opfid)
    3751             : {
    3752             :     HeapTuple   opfTup;
    3753             :     Form_pg_opfamily opfForm;
    3754             :     HeapTuple   amTup;
    3755             :     Form_pg_am  amForm;
    3756             :     char       *nspname;
    3757             : 
    3758        1234 :     opfTup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
    3759        1234 :     if (!HeapTupleIsValid(opfTup))
    3760           0 :         elog(ERROR, "cache lookup failed for opfamily %u", opfid);
    3761        1234 :     opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
    3762             : 
    3763        1234 :     amTup = SearchSysCache1(AMOID, ObjectIdGetDatum(opfForm->opfmethod));
    3764        1234 :     if (!HeapTupleIsValid(amTup))
    3765           0 :         elog(ERROR, "cache lookup failed for access method %u",
    3766             :              opfForm->opfmethod);
    3767        1234 :     amForm = (Form_pg_am) GETSTRUCT(amTup);
    3768             : 
    3769             :     /* Qualify the name if not visible in search path */
    3770        1234 :     if (OpfamilyIsVisible(opfid))
    3771        1078 :         nspname = NULL;
    3772             :     else
    3773         156 :         nspname = get_namespace_name(opfForm->opfnamespace);
    3774             : 
    3775        1234 :     appendStringInfo(buffer, _("operator family %s for access method %s"),
    3776             :                      quote_qualified_identifier(nspname,
    3777        1234 :                                                 NameStr(opfForm->opfname)),
    3778        1234 :                      NameStr(amForm->amname));
    3779             : 
    3780        1234 :     ReleaseSysCache(amTup);
    3781        1234 :     ReleaseSysCache(opfTup);
    3782        1234 : }
    3783             : 
    3784             : /*
    3785             :  * SQL-level callable version of getObjectDescription
    3786             :  */
    3787             : Datum
    3788         658 : pg_describe_object(PG_FUNCTION_ARGS)
    3789             : {
    3790         658 :     Oid         classid = PG_GETARG_OID(0);
    3791         658 :     Oid         objid = PG_GETARG_OID(1);
    3792         658 :     int32       objsubid = PG_GETARG_INT32(2);
    3793             :     char       *description;
    3794             :     ObjectAddress address;
    3795             : 
    3796             :     /* for "pinned" items in pg_depend, return null */
    3797         658 :     if (!OidIsValid(classid) && !OidIsValid(objid))
    3798           0 :         PG_RETURN_NULL();
    3799             : 
    3800         658 :     address.classId = classid;
    3801         658 :     address.objectId = objid;
    3802         658 :     address.objectSubId = objsubid;
    3803             : 
    3804         658 :     description = getObjectDescription(&address);
    3805         658 :     PG_RETURN_TEXT_P(cstring_to_text(description));
    3806             : }
    3807             : 
    3808             : /*
    3809             :  * SQL-level callable function to obtain object type + identity
    3810             :  */
    3811             : Datum
    3812        1186 : pg_identify_object(PG_FUNCTION_ARGS)
    3813             : {
    3814        1186 :     Oid         classid = PG_GETARG_OID(0);
    3815        1186 :     Oid         objid = PG_GETARG_OID(1);
    3816        1186 :     int32       objsubid = PG_GETARG_INT32(2);
    3817        1186 :     Oid         schema_oid = InvalidOid;
    3818        1186 :     const char *objname = NULL;
    3819             :     ObjectAddress address;
    3820             :     Datum       values[4];
    3821             :     bool        nulls[4];
    3822             :     TupleDesc   tupdesc;
    3823             :     HeapTuple   htup;
    3824             : 
    3825        1186 :     address.classId = classid;
    3826        1186 :     address.objectId = objid;
    3827        1186 :     address.objectSubId = objsubid;
    3828             : 
    3829             :     /*
    3830             :      * Construct a tuple descriptor for the result row.  This must match this
    3831             :      * function's pg_proc entry!
    3832             :      */
    3833        1186 :     tupdesc = CreateTemplateTupleDesc(4);
    3834        1186 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "type",
    3835             :                        TEXTOID, -1, 0);
    3836        1186 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2, "schema",
    3837             :                        TEXTOID, -1, 0);
    3838        1186 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3, "name",
    3839             :                        TEXTOID, -1, 0);
    3840        1186 :     TupleDescInitEntry(tupdesc, (AttrNumber) 4, "identity",
    3841             :                        TEXTOID, -1, 0);
    3842             : 
    3843        1186 :     tupdesc = BlessTupleDesc(tupdesc);
    3844             : 
    3845        1186 :     if (is_objectclass_supported(address.classId))
    3846             :     {
    3847             :         HeapTuple   objtup;
    3848        1018 :         Relation    catalog = table_open(address.classId, AccessShareLock);
    3849             : 
    3850        2036 :         objtup = get_catalog_object_by_oid(catalog,
    3851        1018 :                                            get_object_attnum_oid(address.classId),
    3852             :                                            address.objectId);
    3853        1018 :         if (objtup != NULL)
    3854             :         {
    3855             :             bool        isnull;
    3856             :             AttrNumber  nspAttnum;
    3857             :             AttrNumber  nameAttnum;
    3858             : 
    3859        1018 :             nspAttnum = get_object_attnum_namespace(address.classId);
    3860        1018 :             if (nspAttnum != InvalidAttrNumber)
    3861             :             {
    3862         704 :                 schema_oid = heap_getattr(objtup, nspAttnum,
    3863             :                                           RelationGetDescr(catalog), &isnull);
    3864         704 :                 if (isnull)
    3865           0 :                     elog(ERROR, "invalid null namespace in object %u/%u/%d",
    3866             :                          address.classId, address.objectId, address.objectSubId);
    3867             :             }
    3868             : 
    3869             :             /*
    3870             :              * We only return the object name if it can be used (together with
    3871             :              * the schema name, if any) as a unique identifier.
    3872             :              */
    3873        1018 :             if (get_object_namensp_unique(address.classId))
    3874             :             {
    3875         748 :                 nameAttnum = get_object_attnum_name(address.classId);
    3876         748 :                 if (nameAttnum != InvalidAttrNumber)
    3877             :                 {
    3878             :                     Datum       nameDatum;
    3879             : 
    3880         748 :                     nameDatum = heap_getattr(objtup, nameAttnum,
    3881             :                                              RelationGetDescr(catalog), &isnull);
    3882         748 :                     if (isnull)
    3883           0 :                         elog(ERROR, "invalid null name in object %u/%u/%d",
    3884             :                              address.classId, address.objectId, address.objectSubId);
    3885         748 :                     objname = quote_identifier(NameStr(*(DatumGetName(nameDatum))));
    3886             :                 }
    3887             :             }
    3888             :         }
    3889             : 
    3890        1018 :         table_close(catalog, AccessShareLock);
    3891             :     }
    3892             : 
    3893             :     /* object type */
    3894        1186 :     values[0] = CStringGetTextDatum(getObjectTypeDescription(&address));
    3895        1186 :     nulls[0] = false;
    3896             : 
    3897             :     /* schema name */
    3898        1186 :     if (OidIsValid(schema_oid))
    3899             :     {
    3900         704 :         const char *schema = quote_identifier(get_namespace_name(schema_oid));
    3901             : 
    3902         704 :         values[1] = CStringGetTextDatum(schema);
    3903         704 :         nulls[1] = false;
    3904             :     }
    3905             :     else
    3906         482 :         nulls[1] = true;
    3907             : 
    3908             :     /* object name */
    3909        1186 :     if (objname)
    3910             :     {
    3911         748 :         values[2] = CStringGetTextDatum(objname);
    3912         748 :         nulls[2] = false;
    3913             :     }
    3914             :     else
    3915         438 :         nulls[2] = true;
    3916             : 
    3917             :     /* object identity */
    3918        1186 :     values[3] = CStringGetTextDatum(getObjectIdentity(&address));
    3919        1186 :     nulls[3] = false;
    3920             : 
    3921        1186 :     htup = heap_form_tuple(tupdesc, values, nulls);
    3922             : 
    3923        1186 :     PG_RETURN_DATUM(HeapTupleGetDatum(htup));
    3924             : }
    3925             : 
    3926             : /*
    3927             :  * SQL-level callable function to obtain object type + identity
    3928             :  */
    3929             : Datum
    3930         196 : pg_identify_object_as_address(PG_FUNCTION_ARGS)
    3931             : {
    3932         196 :     Oid         classid = PG_GETARG_OID(0);
    3933         196 :     Oid         objid = PG_GETARG_OID(1);
    3934         196 :     int32       objsubid = PG_GETARG_INT32(2);
    3935             :     ObjectAddress address;
    3936             :     char       *identity;
    3937             :     List       *names;
    3938             :     List       *args;
    3939             :     Datum       values[3];
    3940             :     bool        nulls[3];
    3941             :     TupleDesc   tupdesc;
    3942             :     HeapTuple   htup;
    3943             : 
    3944         196 :     address.classId = classid;
    3945         196 :     address.objectId = objid;
    3946         196 :     address.objectSubId = objsubid;
    3947             : 
    3948             :     /*
    3949             :      * Construct a tuple descriptor for the result row.  This must match this
    3950             :      * function's pg_proc entry!
    3951             :      */
    3952         196 :     tupdesc = CreateTemplateTupleDesc(3);
    3953         196 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1, "type",
    3954             :                        TEXTOID, -1, 0);
    3955         196 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2, "object_names",
    3956             :                        TEXTARRAYOID, -1, 0);
    3957         196 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3, "object_args",
    3958             :                        TEXTARRAYOID, -1, 0);
    3959             : 
    3960         196 :     tupdesc = BlessTupleDesc(tupdesc);
    3961             : 
    3962             :     /* object type */
    3963         196 :     values[0] = CStringGetTextDatum(getObjectTypeDescription(&address));
    3964         196 :     nulls[0] = false;
    3965             : 
    3966             :     /* object identity */
    3967         196 :     identity = getObjectIdentityParts(&address, &names, &args);
    3968         196 :     pfree(identity);
    3969             : 
    3970             :     /* object_names */
    3971         196 :     if (names != NIL)
    3972         196 :         values[1] = PointerGetDatum(strlist_to_textarray(names));
    3973             :     else
    3974           0 :         values[1] = PointerGetDatum(construct_empty_array(TEXTOID));
    3975         196 :     nulls[1] = false;
    3976             : 
    3977             :     /* object_args */
    3978         196 :     if (args)
    3979          52 :         values[2] = PointerGetDatum(strlist_to_textarray(args));
    3980             :     else
    3981         144 :         values[2] = PointerGetDatum(construct_empty_array(TEXTOID));
    3982         196 :     nulls[2] = false;
    3983             : 
    3984         196 :     htup = heap_form_tuple(tupdesc, values, nulls);
    3985             : 
    3986         196 :     PG_RETURN_DATUM(HeapTupleGetDatum(htup));
    3987             : }
    3988             : 
    3989             : /*
    3990             :  * Return a palloc'ed string that describes the type of object that the
    3991             :  * passed address is for.
    3992             :  *
    3993             :  * Keep ObjectTypeMap in sync with this.
    3994             :  */
    3995             : char *
    3996        3122 : getObjectTypeDescription(const ObjectAddress *object)
    3997             : {
    3998             :     StringInfoData buffer;
    3999             : 
    4000        3122 :     initStringInfo(&buffer);
    4001             : 
    4002        3122 :     switch (getObjectClass(object))
    4003             :     {
    4004             :         case OCLASS_CLASS:
    4005         888 :             getRelationTypeDescription(&buffer, object->objectId,
    4006             :                                        object->objectSubId);
    4007         888 :             break;
    4008             : 
    4009             :         case OCLASS_PROC:
    4010         170 :             getProcedureTypeDescription(&buffer, object->objectId);
    4011         170 :             break;
    4012             : 
    4013             :         case OCLASS_TYPE:
    4014         686 :             appendStringInfoString(&buffer, "type");
    4015         686 :             break;
    4016             : 
    4017             :         case OCLASS_CAST:
    4018          36 :             appendStringInfoString(&buffer, "cast");
    4019          36 :             break;
    4020             : 
    4021             :         case OCLASS_COLLATION:
    4022          28 :             appendStringInfoString(&buffer, "collation");
    4023          28 :             break;
    4024             : 
    4025             :         case OCLASS_CONSTRAINT:
    4026         152 :             getConstraintTypeDescription(&buffer, object->objectId);
    4027         152 :             break;
    4028             : 
    4029             :         case OCLASS_CONVERSION:
    4030          30 :             appendStringInfoString(&buffer, "conversion");
    4031          30 :             break;
    4032             : 
    4033             :         case OCLASS_DEFAULT:
    4034         248 :             appendStringInfoString(&buffer, "default value");
    4035         248 :             break;
    4036             : 
    4037             :         case OCLASS_LANGUAGE:
    4038          28 :             appendStringInfoString(&buffer, "language");
    4039          28 :             break;
    4040             : 
    4041             :         case OCLASS_LARGEOBJECT:
    4042           0 :             appendStringInfoString(&buffer, "large object");
    4043           0 :             break;
    4044             : 
    4045             :         case OCLASS_OPERATOR:
    4046          32 :             appendStringInfoString(&buffer, "operator");
    4047          32 :             break;
    4048             : 
    4049             :         case OCLASS_OPCLASS:
    4050          30 :             appendStringInfoString(&buffer, "operator class");
    4051          30 :             break;
    4052             : 
    4053             :         case OCLASS_OPFAMILY:
    4054          30 :             appendStringInfoString(&buffer, "operator family");
    4055          30 :             break;
    4056             : 
    4057             :         case OCLASS_AM:
    4058          28 :             appendStringInfoString(&buffer, "access method");
    4059          28 :             break;
    4060             : 
    4061             :         case OCLASS_AMOP:
    4062          28 :             appendStringInfoString(&buffer, "operator of access method");
    4063          28 :             break;
    4064             : 
    4065             :         case OCLASS_AMPROC:
    4066          28 :             appendStringInfoString(&buffer, "function of access method");
    4067          28 :             break;
    4068             : 
    4069             :         case OCLASS_REWRITE:
    4070          40 :             appendStringInfoString(&buffer, "rule");
    4071          40 :             break;
    4072             : 
    4073             :         case OCLASS_TRIGGER:
    4074          98 :             appendStringInfoString(&buffer, "trigger");
    4075          98 :             break;
    4076             : 
    4077             :         case OCLASS_SCHEMA:
    4078          70 :             appendStringInfoString(&buffer, "schema");
    4079          70 :             break;
    4080             : 
    4081             :         case OCLASS_STATISTIC_EXT:
    4082          28 :             appendStringInfoString(&buffer, "statistics object");
    4083          28 :             break;
    4084             : 
    4085             :         case OCLASS_TSPARSER:
    4086          28 :             appendStringInfoString(&buffer, "text search parser");
    4087          28 :             break;
    4088             : 
    4089             :         case OCLASS_TSDICT:
    4090          28 :             appendStringInfoString(&buffer, "text search dictionary");
    4091          28 :             break;
    4092             : 
    4093             :         case OCLASS_TSTEMPLATE:
    4094          28 :             appendStringInfoString(&buffer, "text search template");
    4095          28 :             break;
    4096             : 
    4097             :         case OCLASS_TSCONFIG:
    4098          32 :             appendStringInfoString(&buffer, "text search configuration");
    4099          32 :             break;
    4100             : 
    4101             :         case OCLASS_ROLE:
    4102          28 :             appendStringInfoString(&buffer, "role");
    4103          28 :             break;
    4104             : 
    4105             :         case OCLASS_DATABASE:
    4106           0 :             appendStringInfoString(&buffer, "database");
    4107           0 :             break;
    4108             : 
    4109             :         case OCLASS_TBLSPACE:
    4110           0 :             appendStringInfoString(&buffer, "tablespace");
    4111           0 :             break;
    4112             : 
    4113             :         case OCLASS_FDW:
    4114          28 :             appendStringInfoString(&buffer, "foreign-data wrapper");
    4115          28 :             break;
    4116             : 
    4117             :         case OCLASS_FOREIGN_SERVER:
    4118          28 :             appendStringInfoString(&buffer, "server");
    4119          28 :             break;
    4120             : 
    4121             :         case OCLASS_USER_MAPPING:
    4122          32 :             appendStringInfoString(&buffer, "user mapping");
    4123          32 :             break;
    4124             : 
    4125             :         case OCLASS_DEFACL:
    4126          60 :             appendStringInfoString(&buffer, "default acl");
    4127          60 :             break;
    4128             : 
    4129             :         case OCLASS_EXTENSION:
    4130           4 :             appendStringInfoString(&buffer, "extension");
    4131           4 :             break;
    4132             : 
    4133             :         case OCLASS_EVENT_TRIGGER:
    4134           0 :             appendStringInfoString(&buffer, "event trigger");
    4135           0 :             break;
    4136             : 
    4137             :         case OCLASS_POLICY:
    4138          32 :             appendStringInfoString(&buffer, "policy");
    4139          32 :             break;
    4140             : 
    4141             :         case OCLASS_PUBLICATION:
    4142          28 :             appendStringInfoString(&buffer, "publication");
    4143          28 :             break;
    4144             : 
    4145             :         case OCLASS_PUBLICATION_REL:
    4146          28 :             appendStringInfoString(&buffer, "publication relation");
    4147          28 :             break;
    4148             : 
    4149             :         case OCLASS_SUBSCRIPTION:
    4150          28 :             appendStringInfoString(&buffer, "subscription");
    4151          28 :             break;
    4152             : 
    4153             :         case OCLASS_TRANSFORM:
    4154          32 :             appendStringInfoString(&buffer, "transform");
    4155          32 :             break;
    4156             : 
    4157             :             /*
    4158             :              * There's intentionally no default: case here; we want the
    4159             :              * compiler to warn if a new OCLASS hasn't been handled above.
    4160             :              */
    4161             :     }
    4162             : 
    4163        3122 :     return buffer.data;
    4164             : }
    4165             : 
    4166             : /*
    4167             :  * subroutine for getObjectTypeDescription: describe a relation type
    4168             :  */
    4169             : static void
    4170         888 : getRelationTypeDescription(StringInfo buffer, Oid relid, int32 objectSubId)
    4171             : {
    4172             :     HeapTuple   relTup;
    4173             :     Form_pg_class relForm;
    4174             : 
    4175         888 :     relTup = SearchSysCache1(RELOID,
    4176             :                              ObjectIdGetDatum(relid));
    4177         888 :     if (!HeapTupleIsValid(relTup))
    4178           0 :         elog(ERROR, "cache lookup failed for relation %u", relid);
    4179         888 :     relForm = (Form_pg_class) GETSTRUCT(relTup);
    4180             : 
    4181         888 :     switch (relForm->relkind)
    4182             :     {
    4183             :         case RELKIND_RELATION:
    4184             :         case RELKIND_PARTITIONED_TABLE:
    4185         374 :             appendStringInfoString(buffer, "table");
    4186         374 :             break;
    4187             :         case RELKIND_INDEX:
    4188             :         case RELKIND_PARTITIONED_INDEX:
    4189         230 :             appendStringInfoString(buffer, "index");
    4190         230 :             break;
    4191             :         case RELKIND_SEQUENCE:
    4192          94 :             appendStringInfoString(buffer, "sequence");
    4193          94 :             break;
    4194             :         case RELKIND_TOASTVALUE:
    4195          56 :             appendStringInfoString(buffer, "toast table");
    4196          56 :             break;
    4197             :         case RELKIND_VIEW:
    4198          44 :             appendStringInfoString(buffer, "view");
    4199          44 :             break;
    4200             :         case RELKIND_MATVIEW:
    4201          34 :             appendStringInfoString(buffer, "materialized view");
    4202          34 :             break;
    4203             :         case RELKIND_COMPOSITE_TYPE:
    4204           0 :             appendStringInfoString(buffer, "composite type");
    4205           0 :             break;
    4206             :         case RELKIND_FOREIGN_TABLE:
    4207          56 :             appendStringInfoString(buffer, "foreign table");
    4208          56 :             break;
    4209             :         default:
    4210             :             /* shouldn't get here */
    4211           0 :             appendStringInfoString(buffer, "relation");
    4212           0 :             break;
    4213             :     }
    4214             : 
    4215         888 :     if (objectSubId != 0)
    4216          70 :         appendStringInfoString(buffer, " column");
    4217             : 
    4218         888 :     ReleaseSysCache(relTup);
    4219         888 : }
    4220             : 
    4221             : /*
    4222             :  * subroutine for getObjectTypeDescription: describe a constraint type
    4223             :  */
    4224             : static void
    4225         152 : getConstraintTypeDescription(StringInfo buffer, Oid constroid)
    4226             : {
    4227             :     Relation    constrRel;
    4228             :     HeapTuple   constrTup;
    4229             :     Form_pg_constraint constrForm;
    4230             : 
    4231         152 :     constrRel = table_open(ConstraintRelationId, AccessShareLock);
    4232         152 :     constrTup = get_catalog_object_by_oid(constrRel, Anum_pg_constraint_oid,
    4233             :                                           constroid);
    4234         152 :     if (!HeapTupleIsValid(constrTup))
    4235           0 :         elog(ERROR, "cache lookup failed for constraint %u", constroid);
    4236             : 
    4237         152 :     constrForm = (Form_pg_constraint) GETSTRUCT(constrTup);
    4238             : 
    4239         152 :     if (OidIsValid(constrForm->conrelid))
    4240         124 :         appendStringInfoString(buffer, "table constraint");
    4241          28 :     else if (OidIsValid(constrForm->contypid))
    4242          28 :         appendStringInfoString(buffer, "domain constraint");
    4243             :     else
    4244           0 :         elog(ERROR, "invalid constraint %u", constrForm->oid);
    4245             : 
    4246         152 :     table_close(constrRel, AccessShareLock);
    4247         152 : }
    4248             : 
    4249             : /*
    4250             :  * subroutine for getObjectTypeDescription: describe a procedure type
    4251             :  */
    4252             : static void
    4253         170 : getProcedureTypeDescription(StringInfo buffer, Oid procid)
    4254             : {
    4255             :     HeapTuple   procTup;
    4256             :     Form_pg_proc procForm;
    4257             : 
    4258         170 :     procTup = SearchSysCache1(PROCOID,
    4259             :                               ObjectIdGetDatum(procid));
    4260         170 :     if (!HeapTupleIsValid(procTup))
    4261           0 :         elog(ERROR, "cache lookup failed for procedure %u", procid);
    4262         170 :     procForm = (Form_pg_proc) GETSTRUCT(procTup);
    4263             : 
    4264         170 :     if (procForm->prokind == PROKIND_AGGREGATE)
    4265          40 :         appendStringInfoString(buffer, "aggregate");
    4266         130 :     else if (procForm->prokind == PROKIND_PROCEDURE)
    4267          32 :         appendStringInfoString(buffer, "procedure");
    4268             :     else                        /* function or window function */
    4269          98 :         appendStringInfoString(buffer, "function");
    4270             : 
    4271         170 :     ReleaseSysCache(procTup);
    4272         170 : }
    4273             : 
    4274             : /*
    4275             :  * Obtain a given object's identity, as a palloc'ed string.
    4276             :  *
    4277             :  * This is for machine consumption, so it's not translated.  All elements are
    4278             :  * schema-qualified when appropriate.
    4279             :  */
    4280             : char *
    4281        1422 : getObjectIdentity(const ObjectAddress *object)
    4282             : {
    4283        1422 :     return getObjectIdentityParts(object, NULL, NULL);
    4284             : }
    4285             : 
    4286             : /*
    4287             :  * As above, but more detailed.
    4288             :  *
    4289             :  * There are two sets of return values: the identity itself as a palloc'd
    4290             :  * string is returned.  objname and objargs, if not NULL, are output parameters
    4291             :  * that receive lists of C-strings that are useful to give back to
    4292             :  * get_object_address() to reconstruct the ObjectAddress.
    4293             :  */
    4294             : char *
    4295        3398 : getObjectIdentityParts(const ObjectAddress *object,
    4296             :                        List **objname, List **objargs)
    4297             : {
    4298             :     StringInfoData buffer;
    4299             : 
    4300        3398 :     initStringInfo(&buffer);
    4301             : 
    4302             :     /*
    4303             :      * Make sure that both objname and objargs were passed, or none was; and
    4304             :      * initialize them to empty lists.  For objname this is useless because it
    4305             :      * will be initialized in all cases inside the switch; but we do it anyway
    4306             :      * so that we can test below that no branch leaves it unset.
    4307             :      */
    4308             :     Assert(PointerIsValid(objname) == PointerIsValid(objargs));
    4309        3398 :     if (objname)
    4310             :     {
    4311        1928 :         *objname = NIL;
    4312        1928 :         *objargs = NIL;
    4313             :     }
    4314             : 
    4315        3398 :     switch (getObjectClass(object))
    4316             :     {
    4317             :         case OCLASS_CLASS:
    4318        1136 :             getRelationIdentity(&buffer, object->objectId, objname);
    4319        1136 :             if (object->objectSubId != 0)
    4320             :             {
    4321             :                 char       *attr;
    4322             : 
    4323         318 :                 attr = get_attname(object->objectId, object->objectSubId,
    4324             :                                    false);
    4325         318 :                 appendStringInfo(&buffer, ".%s", quote_identifier(attr));
    4326         318 :                 if (objname)
    4327         244 :                     *objname = lappend(*objname, attr);
    4328             :             }
    4329        1136 :             break;
    4330             : 
    4331             :         case OCLASS_PROC:
    4332         170 :             appendStringInfoString(&buffer,
    4333         170 :                                    format_procedure_qualified(object->objectId));
    4334         170 :             if (objname)
    4335          66 :                 format_procedure_parts(object->objectId, objname, objargs);
    4336         170 :             break;
    4337             : 
    4338             :         case OCLASS_TYPE:
    4339             :             {
    4340             :                 char       *typeout;
    4341             : 
    4342         714 :                 typeout = format_type_be_qualified(object->objectId);
    4343         714 :                 appendStringInfoString(&buffer, typeout);
    4344         714 :                 if (objname)
    4345         564 :                     *objname = list_make1(typeout);
    4346             :             }
    4347         714 :             break;
    4348             : 
    4349             :         case OCLASS_CAST:
    4350             :             {
    4351             :                 Relation    castRel;
    4352             :                 HeapTuple   tup;
    4353             :                 Form_pg_cast castForm;
    4354             : 
    4355          36 :                 castRel = table_open(CastRelationId, AccessShareLock);
    4356             : 
    4357          36 :                 tup = get_catalog_object_by_oid(castRel, Anum_pg_cast_oid,
    4358             :                                                 object->objectId);
    4359             : 
    4360          36 :                 if (!HeapTupleIsValid(tup))
    4361           0 :                     elog(ERROR, "could not find tuple for cast %u",
    4362             :                          object->objectId);
    4363             : 
    4364          36 :                 castForm = (Form_pg_cast) GETSTRUCT(tup);
    4365             : 
    4366          36 :                 appendStringInfo(&buffer, "(%s AS %s)",
    4367             :                                  format_type_be_qualified(castForm->castsource),
    4368             :                                  format_type_be_qualified(castForm->casttarget));
    4369             : 
    4370          36 :                 if (objname)
    4371             :                 {
    4372           4 :                     *objname = list_make1(format_type_be_qualified(castForm->castsource));
    4373           4 :                     *objargs = list_make1(format_type_be_qualified(castForm->casttarget));
    4374             :                 }
    4375             : 
    4376          36 :                 table_close(castRel, AccessShareLock);
    4377          36 :                 break;
    4378             :             }
    4379             : 
    4380             :         case OCLASS_COLLATION:
    4381             :             {
    4382             :                 HeapTuple   collTup;
    4383             :                 Form_pg_collation coll;
    4384             :                 char       *schema;
    4385             : 
    4386          28 :                 collTup = SearchSysCache1(COLLOID,
    4387          28 :                                           ObjectIdGetDatum(object->objectId));
    4388          28 :                 if (!HeapTupleIsValid(collTup))
    4389           0 :                     elog(ERROR, "cache lookup failed for collation %u",
    4390             :                          object->objectId);
    4391          28 :                 coll = (Form_pg_collation) GETSTRUCT(collTup);
    4392          28 :                 schema = get_namespace_name_or_temp(coll->collnamespace);
    4393          28 :                 appendStringInfoString(&buffer,
    4394          28 :                                        quote_qualified_identifier(schema,
    4395          28 :                                                                   NameStr(coll->collname)));
    4396          28 :                 if (objname)
    4397           4 :                     *objname = list_make2(schema,
    4398             :                                           pstrdup(NameStr(coll->collname)));
    4399          28 :                 ReleaseSysCache(collTup);
    4400          28 :                 break;
    4401             :             }
    4402             : 
    4403             :         case OCLASS_CONSTRAINT:
    4404             :             {
    4405             :                 HeapTuple   conTup;
    4406             :                 Form_pg_constraint con;
    4407             : 
    4408         152 :                 conTup = SearchSysCache1(CONSTROID,
    4409         152 :                                          ObjectIdGetDatum(object->objectId));
    4410         152 :                 if (!HeapTupleIsValid(conTup))
    4411           0 :                     elog(ERROR, "cache lookup failed for constraint %u",
    4412             :                          object->objectId);
    4413         152 :                 con = (Form_pg_constraint) GETSTRUCT(conTup);
    4414             : 
    4415         152 :                 if (OidIsValid(con->conrelid))
    4416             :                 {
    4417         124 :                     appendStringInfo(&buffer, "%s on ",
    4418         124 :                                      quote_identifier(NameStr(con->conname)));
    4419         124 :                     getRelationIdentity(&buffer, con->conrelid, objname);
    4420         124 :                     if (objname)
    4421         100 :                         *objname = lappend(*objname, pstrdup(NameStr(con->conname)));
    4422             :                 }
    4423             :                 else
    4424             :                 {
    4425             :                     ObjectAddress domain;
    4426             : 
    4427             :                     Assert(OidIsValid(con->contypid));
    4428          28 :                     domain.classId = TypeRelationId;
    4429          28 :                     domain.objectId = con->contypid;
    4430          28 :                     domain.objectSubId = 0;
    4431             : 
    4432          56 :                     appendStringInfo(&buffer, "%s on %s",
    4433          28 :                                      quote_identifier(NameStr(con->conname)),
    4434             :                                      getObjectIdentityParts(&domain, objname, objargs));
    4435             : 
    4436          28 :                     if (objname)
    4437           4 :                         *objargs = lappend(*objargs, pstrdup(NameStr(con->conname)));
    4438             :                 }
    4439             : 
    4440         152 :                 ReleaseSysCache(conTup);
    4441         152 :                 break;
    4442             :             }
    4443             : 
    4444             :         case OCLASS_CONVERSION:
    4445             :             {
    4446             :                 HeapTuple   conTup;
    4447             :                 Form_pg_conversion conForm;
    4448             :                 char       *schema;
    4449             : 
    4450          30 :                 conTup = SearchSysCache1(CONVOID,
    4451          30 :                                          ObjectIdGetDatum(object->objectId));
    4452          30 :                 if (!HeapTupleIsValid(conTup))
    4453           0 :                     elog(ERROR, "cache lookup failed for conversion %u",
    4454             :                          object->objectId);
    4455          30 :                 conForm = (Form_pg_conversion) GETSTRUCT(conTup);
    4456          30 :                 schema = get_namespace_name_or_temp(conForm->connamespace);
    4457          30 :                 appendStringInfoString(&buffer,
    4458          30 :                                        quote_qualified_identifier(schema,
    4459          30 :                                                                   NameStr(conForm->conname)));
    4460          30 :                 if (objname)
    4461           4 :                     *objname = list_make2(schema,
    4462             :                                           pstrdup(NameStr(conForm->conname)));
    4463          30 :                 ReleaseSysCache(conTup);
    4464          30 :                 break;
    4465             :             }
    4466             : 
    4467             :         case OCLASS_DEFAULT:
    4468             :             {
    4469             :                 Relation    attrdefDesc;
    4470             :                 ScanKeyData skey[1];
    4471             :                 SysScanDesc adscan;
    4472             : 
    4473             :                 HeapTuple   tup;
    4474             :                 Form_pg_attrdef attrdef;
    4475             :                 ObjectAddress colobject;
    4476             : 
    4477         248 :                 attrdefDesc = table_open(AttrDefaultRelationId, AccessShareLock);
    4478             : 
    4479         248 :                 ScanKeyInit(&skey[0],
    4480             :                             Anum_pg_attrdef_oid,
    4481             :                             BTEqualStrategyNumber, F_OIDEQ,
    4482         248 :                             ObjectIdGetDatum(object->objectId));
    4483             : 
    4484         248 :                 adscan = systable_beginscan(attrdefDesc, AttrDefaultOidIndexId,
    4485             :                                             true, NULL, 1, skey);
    4486             : 
    4487         248 :                 tup = systable_getnext(adscan);
    4488             : 
    4489         248 :                 if (!HeapTupleIsValid(tup))
    4490           0 :                     elog(ERROR, "could not find tuple for attrdef %u",
    4491             :                          object->objectId);
    4492             : 
    4493         248 :                 attrdef = (Form_pg_attrdef) GETSTRUCT(tup);
    4494             : 
    4495         248 :                 colobject.classId = RelationRelationId;
    4496         248 :                 colobject.objectId = attrdef->adrelid;
    4497         248 :                 colobject.objectSubId = attrdef->adnum;
    4498             : 
    4499         248 :                 appendStringInfo(&buffer, "for %s",
    4500             :                                  getObjectIdentityParts(&colobject,
    4501             :                                                         objname, objargs));
    4502             : 
    4503         248 :                 systable_endscan(adscan);
    4504         248 :                 table_close(attrdefDesc, AccessShareLock);
    4505         248 :                 break;
    4506             :             }
    4507             : 
    4508             :         case OCLASS_LANGUAGE:
    4509             :             {
    4510             :                 HeapTuple   langTup;
    4511             :                 Form_pg_language langForm;
    4512             : 
    4513          28 :                 langTup = SearchSysCache1(LANGOID,
    4514          28 :                                           ObjectIdGetDatum(object->objectId));
    4515          28 :                 if (!HeapTupleIsValid(langTup))
    4516           0 :                     elog(ERROR, "cache lookup failed for language %u",
    4517             :                          object->objectId);
    4518          28 :                 langForm = (Form_pg_language) GETSTRUCT(langTup);
    4519          28 :                 appendStringInfoString(&buffer,
    4520          28 :                                        quote_identifier(NameStr(langForm->lanname)));
    4521          28 :                 if (objname)
    4522           4 :                     *objname = list_make1(pstrdup(NameStr(langForm->lanname)));
    4523          28 :                 ReleaseSysCache(langTup);
    4524          28 :                 break;
    4525             :             }
    4526             :         case OCLASS_LARGEOBJECT:
    4527           0 :             appendStringInfo(&buffer, "%u",
    4528             :                              object->objectId);
    4529           0 :             if (objname)
    4530           0 :                 *objname = list_make1(psprintf("%u", object->objectId));
    4531           0 :             break;
    4532             : 
    4533             :         case OCLASS_OPERATOR:
    4534          32 :             appendStringInfoString(&buffer,
    4535          32 :                                    format_operator_qualified(object->objectId));
    4536          32 :             if (objname)
    4537           4 :                 format_operator_parts(object->objectId, objname, objargs);
    4538          32 :             break;
    4539             : 
    4540             :         case OCLASS_OPCLASS:
    4541             :             {
    4542             :                 HeapTuple   opcTup;
    4543             :                 Form_pg_opclass opcForm;
    4544             :                 HeapTuple   amTup;
    4545             :                 Form_pg_am  amForm;
    4546             :                 char       *schema;
    4547             : 
    4548          30 :                 opcTup = SearchSysCache1(CLAOID,
    4549          30 :                                          ObjectIdGetDatum(object->objectId));
    4550          30 :                 if (!HeapTupleIsValid(opcTup))
    4551           0 :                     elog(ERROR, "cache lookup failed for opclass %u",
    4552             :                          object->objectId);
    4553          30 :                 opcForm = (Form_pg_opclass) GETSTRUCT(opcTup);
    4554          30 :                 schema = get_namespace_name_or_temp(opcForm->opcnamespace);
    4555             : 
    4556          30 :                 amTup = SearchSysCache1(AMOID,
    4557          30 :                                         ObjectIdGetDatum(opcForm->opcmethod));
    4558          30 :                 if (!HeapTupleIsValid(amTup))
    4559           0 :                     elog(ERROR, "cache lookup failed for access method %u",
    4560             :                          opcForm->opcmethod);
    4561          30 :                 amForm = (Form_pg_am) GETSTRUCT(amTup);
    4562             : 
    4563          60 :                 appendStringInfo(&buffer, "%s USING %s",
    4564             :                                  quote_qualified_identifier(schema,
    4565          30 :                                                             NameStr(opcForm->opcname)),
    4566          30 :                                  quote_identifier(NameStr(amForm->amname)));
    4567          30 :                 if (objname)
    4568           4 :                     *objname = list_make3(pstrdup(NameStr(amForm->amname)),
    4569             :                                           schema,
    4570             :                                           pstrdup(NameStr(opcForm->opcname)));
    4571             : 
    4572          30 :                 ReleaseSysCache(amTup);
    4573          30 :                 ReleaseSysCache(opcTup);
    4574          30 :                 break;
    4575             :             }
    4576             : 
    4577             :         case OCLASS_OPFAMILY:
    4578          30 :             getOpFamilyIdentity(&buffer, object->objectId, objname);
    4579          30 :             break;
    4580             : 
    4581             :         case OCLASS_AM:
    4582             :             {
    4583             :                 char       *amname;
    4584             : 
    4585          28 :                 amname = get_am_name(object->objectId);
    4586          28 :                 if (!amname)
    4587           0 :                     elog(ERROR, "cache lookup failed for access method %u",
    4588             :                          object->objectId);
    4589          28 :                 appendStringInfoString(&buffer, quote_identifier(amname));
    4590          28 :                 if (objname)
    4591           4 :                     *objname = list_make1(amname);
    4592             :             }
    4593          28 :             break;
    4594             : 
    4595             :         case OCLASS_AMOP:
    4596             :             {
    4597             :                 Relation    amopDesc;
    4598             :                 HeapTuple   tup;
    4599             :                 ScanKeyData skey[1];
    4600             :                 SysScanDesc amscan;
    4601             :                 Form_pg_amop amopForm;
    4602             :                 StringInfoData opfam;
    4603             :                 char       *ltype;
    4604             :                 char       *rtype;
    4605             : 
    4606          28 :                 amopDesc = table_open(AccessMethodOperatorRelationId,
    4607             :                                       AccessShareLock);
    4608             : 
    4609          28 :                 ScanKeyInit(&skey[0],
    4610             :                             Anum_pg_amop_oid,
    4611             :                             BTEqualStrategyNumber, F_OIDEQ,
    4612          28 :                             ObjectIdGetDatum(object->objectId));
    4613             : 
    4614          28 :                 amscan = systable_beginscan(amopDesc, AccessMethodOperatorOidIndexId, true,
    4615             :                                             NULL, 1, skey);
    4616             : 
    4617          28 :                 tup = systable_getnext(amscan);
    4618             : 
    4619          28 :                 if (!HeapTupleIsValid(tup))
    4620           0 :                     elog(ERROR, "could not find tuple for amop entry %u",
    4621             :                          object->objectId);
    4622             : 
    4623          28 :                 amopForm = (Form_pg_amop) GETSTRUCT(tup);
    4624             : 
    4625          28 :                 initStringInfo(&opfam);
    4626          28 :                 getOpFamilyIdentity(&opfam, amopForm->amopfamily, objname);
    4627             : 
    4628          28 :                 ltype = format_type_be_qualified(amopForm->amoplefttype);
    4629          28 :                 rtype = format_type_be_qualified(amopForm->amoprighttype);
    4630             : 
    4631          28 :                 if (objname)
    4632             :                 {
    4633           4 :                     *objname = lappend(*objname,
    4634           4 :                                        psprintf("%d", amopForm->amopstrategy));
    4635           4 :                     *objargs = list_make2(ltype, rtype);
    4636             :                 }
    4637             : 
    4638          56 :                 appendStringInfo(&buffer, "operator %d (%s, %s) of %s",
    4639          28 :                                  amopForm->amopstrategy,
    4640             :                                  ltype, rtype, opfam.data);
    4641             : 
    4642          28 :                 pfree(opfam.data);
    4643             : 
    4644          28 :                 systable_endscan(amscan);
    4645          28 :                 table_close(amopDesc, AccessShareLock);
    4646          28 :                 break;
    4647             :             }
    4648             : 
    4649             :         case OCLASS_AMPROC:
    4650             :             {
    4651             :                 Relation    amprocDesc;
    4652             :                 ScanKeyData skey[1];
    4653             :                 SysScanDesc amscan;
    4654             :                 HeapTuple   tup;
    4655             :                 Form_pg_amproc amprocForm;
    4656             :                 StringInfoData opfam;
    4657             :                 char       *ltype;
    4658             :                 char       *rtype;
    4659             : 
    4660          28 :                 amprocDesc = table_open(AccessMethodProcedureRelationId,
    4661             :                                         AccessShareLock);
    4662             : 
    4663          28 :                 ScanKeyInit(&skey[0],
    4664             :                             Anum_pg_amproc_oid,
    4665             :                             BTEqualStrategyNumber, F_OIDEQ,
    4666          28 :                             ObjectIdGetDatum(object->objectId));
    4667             : 
    4668          28 :                 amscan = systable_beginscan(amprocDesc, AccessMethodProcedureOidIndexId, true,
    4669             :                                             NULL, 1, skey);
    4670             : 
    4671          28 :                 tup = systable_getnext(amscan);
    4672             : 
    4673          28 :                 if (!HeapTupleIsValid(tup))
    4674           0 :                     elog(ERROR, "could not find tuple for amproc entry %u",
    4675             :                          object->objectId);
    4676             : 
    4677          28 :                 amprocForm = (Form_pg_amproc) GETSTRUCT(tup);
    4678             : 
    4679          28 :                 initStringInfo(&opfam);
    4680          28 :                 getOpFamilyIdentity(&opfam, amprocForm->amprocfamily, objname);
    4681             : 
    4682          28 :                 ltype = format_type_be_qualified(amprocForm->amproclefttype);
    4683          28 :                 rtype = format_type_be_qualified(amprocForm->amprocrighttype);
    4684             : 
    4685          28 :                 if (objname)
    4686             :                 {
    4687           4 :                     *objname = lappend(*objname,
    4688           4 :                                        psprintf("%d", amprocForm->amprocnum));
    4689           4 :                     *objargs = list_make2(ltype, rtype);
    4690             :                 }
    4691             : 
    4692          56 :                 appendStringInfo(&buffer, "function %d (%s, %s) of %s",
    4693          28 :                                  amprocForm->amprocnum,
    4694             :                                  ltype, rtype, opfam.data);
    4695             : 
    4696          28 :                 pfree(opfam.data);
    4697             : 
    4698          28 :                 systable_endscan(amscan);
    4699          28 :                 table_close(amprocDesc, AccessShareLock);
    4700          28 :                 break;
    4701             :             }
    4702             : 
    4703             :         case OCLASS_REWRITE:
    4704             :             {
    4705             :                 Relation    ruleDesc;
    4706             :                 HeapTuple   tup;
    4707             :                 Form_pg_rewrite rule;
    4708             : 
    4709          40 :                 ruleDesc = table_open(RewriteRelationId, AccessShareLock);
    4710             : 
    4711          40 :                 tup = get_catalog_object_by_oid(ruleDesc, Anum_pg_rewrite_oid,
    4712             :                                                 object->objectId);
    4713             : 
    4714          40 :                 if (!HeapTupleIsValid(tup))
    4715           0 :                     elog(ERROR, "could not find tuple for rule %u",
    4716             :                          object->objectId);
    4717             : 
    4718          40 :                 rule = (Form_pg_rewrite) GETSTRUCT(tup);
    4719             : 
    4720          40 :                 appendStringInfo(&buffer, "%s on ",
    4721          40 :                                  quote_identifier(NameStr(rule->rulename)));
    4722          40 :                 getRelationIdentity(&buffer, rule->ev_class, objname);
    4723          40 :                 if (objname)
    4724           4 :                     *objname = lappend(*objname, pstrdup(NameStr(rule->rulename)));
    4725             : 
    4726          40 :                 table_close(ruleDesc, AccessShareLock);
    4727          40 :                 break;
    4728             :             }
    4729             : 
    4730             :         case OCLASS_TRIGGER:
    4731             :             {
    4732             :                 Relation    trigDesc;
    4733             :                 HeapTuple   tup;
    4734             :                 Form_pg_trigger trig;
    4735             : 
    4736          98 :                 trigDesc = table_open(TriggerRelationId, AccessShareLock);
    4737             : 
    4738          98 :                 tup = get_catalog_object_by_oid(trigDesc, Anum_pg_trigger_oid,
    4739             :                                                 object->objectId);
    4740             : 
    4741          98 :                 if (!HeapTupleIsValid(tup))
    4742           0 :                     elog(ERROR, "could not find tuple for trigger %u",
    4743             :                          object->objectId);
    4744             : 
    4745          98 :                 trig = (Form_pg_trigger) GETSTRUCT(tup);
    4746             : 
    4747          98 :                 appendStringInfo(&buffer, "%s on ",
    4748          98 :                                  quote_identifier(NameStr(trig->tgname)));
    4749          98 :                 getRelationIdentity(&buffer, trig->tgrelid, objname);
    4750          98 :                 if (objname)
    4751          68 :                     *objname = lappend(*objname, pstrdup(NameStr(trig->tgname)));
    4752             : 
    4753          98 :                 table_close(trigDesc, AccessShareLock);
    4754          98 :                 break;
    4755             :             }
    4756             : 
    4757             :         case OCLASS_SCHEMA:
    4758             :             {
    4759             :                 char       *nspname;
    4760             : 
    4761          70 :                 nspname = get_namespace_name_or_temp(object->objectId);
    4762          70 :                 if (!nspname)
    4763           0 :                     elog(ERROR, "cache lookup failed for namespace %u",
    4764             :                          object->objectId);
    4765          70 :                 appendStringInfoString(&buffer,
    4766             :                                        quote_identifier(nspname));
    4767          70 :                 if (objname)
    4768          36 :                     *objname = list_make1(nspname);
    4769          70 :                 break;
    4770             :             }
    4771             : 
    4772             :         case OCLASS_STATISTIC_EXT:
    4773             :             {
    4774             :                 HeapTuple   tup;
    4775             :                 Form_pg_statistic_ext formStatistic;
    4776             :                 char       *schema;
    4777             : 
    4778          28 :                 tup = SearchSysCache1(STATEXTOID,
    4779          28 :                                       ObjectIdGetDatum(object->objectId));
    4780          28 :                 if (!HeapTupleIsValid(tup))
    4781           0 :                     elog(ERROR, "cache lookup failed for statistics object %u",
    4782             :                          object->objectId);
    4783          28 :                 formStatistic = (Form_pg_statistic_ext) GETSTRUCT(tup);
    4784          28 :                 schema = get_namespace_name_or_temp(formStatistic->stxnamespace);
    4785          28 :                 appendStringInfoString(&buffer,
    4786          28 :                                        quote_qualified_identifier(schema,
    4787          28 :                                                                   NameStr(formStatistic->stxname)));
    4788          28 :                 if (objname)
    4789           4 :                     *objname = list_make2(schema,
    4790             :                                           pstrdup(NameStr(formStatistic->stxname)));
    4791          28 :                 ReleaseSysCache(tup);
    4792             :             }
    4793          28 :             break;
    4794             : 
    4795             :         case OCLASS_TSPARSER:
    4796             :             {
    4797             :                 HeapTuple   tup;
    4798             :                 Form_pg_ts_parser formParser;
    4799             :                 char       *schema;
    4800             : 
    4801          28 :                 tup = SearchSysCache1(TSPARSEROID,
    4802          28 :                                       ObjectIdGetDatum(object->objectId));
    4803          28 :                 if (!HeapTupleIsValid(tup))
    4804           0 :                     elog(ERROR, "cache lookup failed for text search parser %u",
    4805             :                          object->objectId);
    4806          28 :                 formParser = (Form_pg_ts_parser) GETSTRUCT(tup);
    4807          28 :                 schema = get_namespace_name_or_temp(formParser->prsnamespace);
    4808          28 :                 appendStringInfoString(&buffer,
    4809          28 :                                        quote_qualified_identifier(schema,
    4810          28 :                                                                   NameStr(formParser->prsname)));
    4811          28 :                 if (objname)
    4812           4 :                     *objname = list_make2(schema,
    4813             :                                           pstrdup(NameStr(formParser->prsname)));
    4814          28 :                 ReleaseSysCache(tup);
    4815          28 :                 break;
    4816             :             }
    4817             : 
    4818             :         case OCLASS_TSDICT:
    4819             :             {
    4820             :                 HeapTuple   tup;
    4821             :                 Form_pg_ts_dict formDict;
    4822             :                 char       *schema;
    4823             : 
    4824          28 :                 tup = SearchSysCache1(TSDICTOID,
    4825          28 :                                       ObjectIdGetDatum(object->objectId));
    4826          28 :                 if (!HeapTupleIsValid(tup))
    4827           0 :                     elog(ERROR, "cache lookup failed for text search dictionary %u",
    4828             :                          object->objectId);
    4829          28 :                 formDict = (Form_pg_ts_dict) GETSTRUCT(tup);
    4830          28 :                 schema = get_namespace_name_or_temp(formDict->dictnamespace);
    4831          28 :                 appendStringInfoString(&buffer,
    4832          28 :                                        quote_qualified_identifier(schema,
    4833          28 :                                                                   NameStr(formDict->dictname)));
    4834          28 :                 if (objname)
    4835           4 :                     *objname = list_make2(schema,
    4836             :                                           pstrdup(NameStr(formDict->dictname)));
    4837          28 :                 ReleaseSysCache(tup);
    4838          28 :                 break;
    4839             :             }
    4840             : 
    4841             :         case OCLASS_TSTEMPLATE:
    4842             :             {
    4843             :                 HeapTuple   tup;
    4844             :                 Form_pg_ts_template formTmpl;
    4845             :                 char       *schema;
    4846             : 
    4847          28 :                 tup = SearchSysCache1(TSTEMPLATEOID,
    4848          28 :                                       ObjectIdGetDatum(object->objectId));
    4849          28 :                 if (!HeapTupleIsValid(tup))
    4850           0 :                     elog(ERROR, "cache lookup failed for text search template %u",
    4851             :                          object->objectId);
    4852          28 :                 formTmpl = (Form_pg_ts_template) GETSTRUCT(tup);
    4853          28 :                 schema = get_namespace_name_or_temp(formTmpl->tmplnamespace);
    4854          28 :                 appendStringInfoString(&buffer,
    4855          28 :                                        quote_qualified_identifier(schema,
    4856          28 :                                                                   NameStr(formTmpl->tmplname)));
    4857          28 :                 if (objname)
    4858           4 :                     *objname = list_make2(schema,
    4859             :                                           pstrdup(NameStr(formTmpl->tmplname)));
    4860          28 :                 ReleaseSysCache(tup);
    4861          28 :                 break;
    4862             :             }
    4863             : 
    4864             :         case OCLASS_TSCONFIG:
    4865             :             {
    4866             :                 HeapTuple   tup;
    4867             :                 Form_pg_ts_config formCfg;
    4868             :                 char       *schema;
    4869             : 
    4870          32 :                 tup = SearchSysCache1(TSCONFIGOID,
    4871          32 :                                       ObjectIdGetDatum(object->objectId));
    4872          32 :                 if (!HeapTupleIsValid(tup))
    4873           0 :                     elog(ERROR, "cache lookup failed for text search configuration %u",
    4874             :                          object->objectId);
    4875          32 :                 formCfg = (Form_pg_ts_config) GETSTRUCT(tup);
    4876          32 :                 schema = get_namespace_name_or_temp(formCfg->cfgnamespace);
    4877          32 :                 appendStringInfoString(&buffer,
    4878          32 :                                        quote_qualified_identifier(schema,
    4879          32 :                                                                   NameStr(formCfg->cfgname)));
    4880          32 :                 if (objname)
    4881           4 :                     *objname = list_make2(schema,
    4882             :                                           pstrdup(NameStr(formCfg->cfgname)));
    4883          32 :                 ReleaseSysCache(tup);
    4884          32 :                 break;
    4885             :             }
    4886             : 
    4887             :         case OCLASS_ROLE:
    4888             :             {
    4889             :                 char       *username;
    4890             : 
    4891          28 :                 username = GetUserNameFromId(object->objectId, false);
    4892          28 :                 if (objname)
    4893           4 :                     *objname = list_make1(username);
    4894          28 :                 appendStringInfoString(&buffer,
    4895             :                                        quote_identifier(username));
    4896          28 :                 break;
    4897             :             }
    4898             : 
    4899             :         case OCLASS_DATABASE:
    4900             :             {
    4901             :                 char       *datname;
    4902             : 
    4903           0 :                 datname = get_database_name(object->objectId);
    4904           0 :                 if (!datname)
    4905           0 :                     elog(ERROR, "cache lookup failed for database %u",
    4906             :                          object->objectId);
    4907           0 :                 if (objname)
    4908           0 :                     *objname = list_make1(datname);
    4909           0 :                 appendStringInfoString(&buffer,
    4910             :                                        quote_identifier(datname));
    4911           0 :                 break;
    4912             :             }
    4913             : 
    4914             :         case OCLASS_TBLSPACE:
    4915             :             {
    4916             :                 char       *tblspace;
    4917             : 
    4918           0 :                 tblspace = get_tablespace_name(object->objectId);
    4919           0 :                 if (!tblspace)
    4920           0 :                     elog(ERROR, "cache lookup failed for tablespace %u",
    4921             :                          object->objectId);
    4922           0 :                 if (objname)
    4923           0 :                     *objname = list_make1(tblspace);
    4924           0 :                 appendStringInfoString(&buffer,
    4925             :                                        quote_identifier(tblspace));
    4926           0 :                 break;
    4927             :             }
    4928             : 
    4929             :         case OCLASS_FDW:
    4930             :             {
    4931             :                 ForeignDataWrapper *fdw;
    4932             : 
    4933          28 :                 fdw = GetForeignDataWrapper(object->objectId);
    4934          28 :                 appendStringInfoString(&buffer, quote_identifier(fdw->fdwname));
    4935          28 :                 if (objname)
    4936           4 :                     *objname = list_make1(pstrdup(fdw->fdwname));
    4937          28 :                 break;
    4938             :             }
    4939             : 
    4940             :         case OCLASS_FOREIGN_SERVER:
    4941             :             {
    4942             :                 ForeignServer *srv;
    4943             : 
    4944          28 :                 srv = GetForeignServer(object->objectId);
    4945          28 :                 appendStringInfoString(&buffer,
    4946          28 :                                        quote_identifier(srv->servername));
    4947          28 :                 if (objname)
    4948           4 :                     *objname = list_make1(pstrdup(srv->servername));
    4949          28 :                 break;
    4950             :             }
    4951             : 
    4952             :         case OCLASS_USER_MAPPING:
    4953             :             {
    4954             :                 HeapTuple   tup;
    4955             :                 Oid         useid;
    4956             :                 Form_pg_user_mapping umform;
    4957             :                 ForeignServer *srv;
    4958             :                 const char *usename;
    4959             : 
    4960          32 :                 tup = SearchSysCache1(USERMAPPINGOID,
    4961          32 :                                       ObjectIdGetDatum(object->objectId));
    4962          32 :                 if (!HeapTupleIsValid(tup))
    4963           0 :                     elog(ERROR, "cache lookup failed for user mapping %u",
    4964             :                          object->objectId);
    4965          32 :                 umform = (Form_pg_user_mapping) GETSTRUCT(tup);
    4966          32 :                 useid = umform->umuser;
    4967          32 :                 srv = GetForeignServer(umform->umserver);
    4968             : 
    4969          32 :                 ReleaseSysCache(tup);
    4970             : 
    4971          32 :                 if (OidIsValid(useid))
    4972          32 :                     usename = GetUserNameFromId(useid, false);
    4973             :                 else
    4974           0 :                     usename = "public";
    4975             : 
    4976          32 :                 if (objname)
    4977             :                 {
    4978           8 :                     *objname = list_make1(pstrdup(usename));
    4979           8 :                     *objargs = list_make1(pstrdup(srv->servername));
    4980             :                 }
    4981             : 
    4982          32 :                 appendStringInfo(&buffer, "%s on server %s",
    4983             :                                  quote_identifier(usename),
    4984             :                                  srv->servername);
    4985          32 :                 break;
    4986             :             }
    4987             : 
    4988             :         case OCLASS_DEFACL:
    4989             :             {
    4990             :                 Relation    defaclrel;
    4991             :                 ScanKeyData skey[1];
    4992             :                 SysScanDesc rcscan;
    4993             :                 HeapTuple   tup;
    4994             :                 Form_pg_default_acl defacl;
    4995             :                 char       *schema;
    4996             :                 char       *username;
    4997             : 
    4998          60 :                 defaclrel = table_open(DefaultAclRelationId, AccessShareLock);
    4999             : 
    5000          60 :                 ScanKeyInit(&skey[0],
    5001             :                             Anum_pg_default_acl_oid,
    5002             :                             BTEqualStrategyNumber, F_OIDEQ,
    5003          60 :                             ObjectIdGetDatum(object->objectId));
    5004             : 
    5005          60 :                 rcscan = systable_beginscan(defaclrel, DefaultAclOidIndexId,
    5006             :                                             true, NULL, 1, skey);
    5007             : 
    5008          60 :                 tup = systable_getnext(rcscan);
    5009             : 
    5010          60 :                 if (!HeapTupleIsValid(tup))
    5011           0 :                     elog(ERROR, "could not find tuple for default ACL %u",
    5012             :                          object->objectId);
    5013             : 
    5014          60 :                 defacl = (Form_pg_default_acl) GETSTRUCT(tup);
    5015             : 
    5016          60 :                 username = GetUserNameFromId(defacl->defaclrole, false);
    5017          60 :                 appendStringInfo(&buffer,
    5018             :                                  "for role %s",
    5019             :                                  quote_identifier(username));
    5020             : 
    5021          60 :                 if (OidIsValid(defacl->defaclnamespace))
    5022             :                 {
    5023          28 :                     schema = get_namespace_name_or_temp(defacl->defaclnamespace);
    5024          28 :                     appendStringInfo(&buffer,
    5025             :                                      " in schema %s",
    5026             :                                      quote_identifier(schema));
    5027             :                 }
    5028             :                 else
    5029          32 :                     schema = NULL;
    5030             : 
    5031          60 :                 switch (defacl->defaclobjtype)
    5032             :                 {
    5033             :                     case DEFACLOBJ_RELATION:
    5034          60 :                         appendStringInfoString(&buffer,
    5035             :                                                " on tables");
    5036          60 :                         break;
    5037             :                     case DEFACLOBJ_SEQUENCE:
    5038           0 :                         appendStringInfoString(&buffer,
    5039             :                                                " on sequences");
    5040           0 :                         break;
    5041             :                     case DEFACLOBJ_FUNCTION:
    5042           0 :                         appendStringInfoString(&buffer,
    5043             :                                                " on functions");
    5044           0 :                         break;
    5045             :                     case DEFACLOBJ_TYPE:
    5046           0 :                         appendStringInfoString(&buffer,
    5047             :                                                " on types");
    5048           0 :                         break;
    5049             :                     case DEFACLOBJ_NAMESPACE:
    5050           0 :                         appendStringInfoString(&buffer,
    5051             :                                                " on schemas");
    5052           0 :                         break;
    5053             :                 }
    5054             : 
    5055          60 :                 if (objname)
    5056             :                 {
    5057          12 :                     *objname = list_make1(username);
    5058          12 :                     if (schema)
    5059           4 :                         *objname = lappend(*objname, schema);
    5060          12 :                     *objargs = list_make1(psprintf("%c", defacl->defaclobjtype));
    5061             :                 }
    5062             : 
    5063          60 :                 systable_endscan(rcscan);
    5064          60 :                 table_close(defaclrel, AccessShareLock);
    5065          60 :                 break;
    5066             :             }
    5067             : 
    5068             :         case OCLASS_EXTENSION:
    5069             :             {
    5070             :                 char       *extname;
    5071             : 
    5072           4 :                 extname = get_extension_name(object->objectId);
    5073           4 :                 if (!extname)
    5074           0 :                     elog(ERROR, "cache lookup failed for extension %u",
    5075             :                          object->objectId);
    5076           4 :                 appendStringInfoString(&buffer, quote_identifier(extname));
    5077           4 :                 if (objname)
    5078           0 :                     *objname = list_make1(extname);
    5079           4 :                 break;
    5080             :             }
    5081             : 
    5082             :         case OCLASS_EVENT_TRIGGER:
    5083             :             {
    5084             :                 HeapTuple   tup;
    5085             :                 Form_pg_event_trigger trigForm;
    5086             : 
    5087             :                 /* no objname support here */
    5088           0 :                 if (objname)
    5089           0 :                     *objname = NIL;
    5090             : 
    5091           0 :                 tup = SearchSysCache1(EVENTTRIGGEROID,
    5092           0 :                                       ObjectIdGetDatum(object->objectId));
    5093           0 :                 if (!HeapTupleIsValid(tup))
    5094           0 :                     elog(ERROR, "cache lookup failed for event trigger %u",
    5095             :                          object->objectId);
    5096           0 :                 trigForm = (Form_pg_event_trigger) GETSTRUCT(tup);
    5097           0 :                 appendStringInfoString(&buffer,
    5098           0 :                                        quote_identifier(NameStr(trigForm->evtname)));
    5099           0 :                 ReleaseSysCache(tup);
    5100           0 :                 break;
    5101             :             }
    5102             : 
    5103             :         case OCLASS_POLICY:
    5104             :             {
    5105             :                 Relation    polDesc;
    5106             :                 HeapTuple   tup;
    5107             :                 Form_pg_policy policy;
    5108             : 
    5109          32 :                 polDesc = table_open(PolicyRelationId, AccessShareLock);
    5110             : 
    5111          32 :                 tup = get_catalog_object_by_oid(polDesc, Anum_pg_policy_oid,
    5112             :                                                 object->objectId);
    5113             : 
    5114          32 :                 if (!HeapTupleIsValid(tup))
    5115           0 :                     elog(ERROR, "could not find tuple for policy %u",
    5116             :                          object->objectId);
    5117             : 
    5118          32 :                 policy = (Form_pg_policy) GETSTRUCT(tup);
    5119             : 
    5120          32 :                 appendStringInfo(&buffer, "%s on ",
    5121          32 :                                  quote_identifier(NameStr(policy->polname)));
    5122          32 :                 getRelationIdentity(&buffer, policy->polrelid, objname);
    5123          32 :                 if (objname)
    5124           8 :                     *objname = lappend(*objname, pstrdup(NameStr(policy->polname)));
    5125             : 
    5126          32 :                 table_close(polDesc, AccessShareLock);
    5127          32 :                 break;
    5128             :             }
    5129             : 
    5130             :         case OCLASS_PUBLICATION:
    5131             :             {
    5132             :                 char       *pubname;
    5133             : 
    5134          28 :                 pubname = get_publication_name(object->objectId, false);
    5135          28 :                 appendStringInfoString(&buffer,
    5136             :                                        quote_identifier(pubname));
    5137          28 :                 if (objname)
    5138           4 :                     *objname = list_make1(pubname);
    5139          28 :                 break;
    5140             :             }
    5141             : 
    5142             :         case OCLASS_PUBLICATION_REL:
    5143             :             {
    5144             :                 HeapTuple   tup;
    5145             :                 char       *pubname;
    5146             :                 Form_pg_publication_rel prform;
    5147             : 
    5148          28 :                 tup = SearchSysCache1(PUBLICATIONREL,
    5149          28 :                                       ObjectIdGetDatum(object->objectId));
    5150          28 :                 if (!HeapTupleIsValid(tup))
    5151           0 :                     elog(ERROR, "cache lookup failed for publication table %u",
    5152             :                          object->objectId);
    5153             : 
    5154          28 :                 prform = (Form_pg_publication_rel) GETSTRUCT(tup);
    5155          28 :                 pubname = get_publication_name(prform->prpubid, false);
    5156             : 
    5157          28 :                 getRelationIdentity(&buffer, prform->prrelid, objname);
    5158          28 :                 appendStringInfo(&buffer, " in publication %s", pubname);
    5159             : 
    5160          28 :                 if (objargs)
    5161           4 :                     *objargs = list_make1(pubname);
    5162             : 
    5163          28 :                 ReleaseSysCache(tup);
    5164          28 :                 break;
    5165             :             }
    5166             : 
    5167             :         case OCLASS_SUBSCRIPTION:
    5168             :             {
    5169             :                 char       *subname;
    5170             : 
    5171          28 :                 subname = get_subscription_name(object->objectId, false);
    5172          28 :                 appendStringInfoString(&buffer,
    5173             :                                        quote_identifier(subname));
    5174          28 :                 if (objname)
    5175           4 :                     *objname = list_make1(subname);
    5176          28 :                 break;
    5177             :             }
    5178             : 
    5179             :         case OCLASS_TRANSFORM:
    5180             :             {
    5181             :                 Relation    transformDesc;
    5182             :                 HeapTuple   tup;
    5183             :                 Form_pg_transform transform;
    5184             :                 char       *transformLang;
    5185             :                 char       *transformType;
    5186             : 
    5187          32 :                 transformDesc = table_open(TransformRelationId, AccessShareLock);
    5188             : 
    5189          32 :                 tup = get_catalog_object_by_oid(transformDesc,
    5190             :                                                 Anum_pg_transform_oid,
    5191             :                                                 object->objectId);
    5192             : 
    5193          32 :                 if (!HeapTupleIsValid(tup))
    5194           0 :                     elog(ERROR, "could not find tuple for transform %u",
    5195             :                          object->objectId);
    5196             : 
    5197          32 :                 transform = (Form_pg_transform) GETSTRUCT(tup);
    5198             : 
    5199          32 :                 transformType = format_type_be_qualified(transform->trftype);
    5200          32 :                 transformLang = get_language_name(transform->trflang, false);
    5201             : 
    5202          32 :                 appendStringInfo(&buffer, "for %s on language %s",
    5203             :                                  transformType,
    5204             :                                  transformLang);
    5205          32 :                 if (objname)
    5206             :                 {
    5207           6 :                     *objname = list_make1(transformType);
    5208           6 :                     *objargs = list_make1(pstrdup(transformLang));
    5209             :                 }
    5210             : 
    5211          32 :                 table_close(transformDesc, AccessShareLock);
    5212             :             }
    5213          32 :             break;
    5214             : 
    5215             :             /*
    5216             :              * There's intentionally no default: case here; we want the
    5217             :              * compiler to warn if a new OCLASS hasn't been handled above.
    5218             :              */
    5219             :     }
    5220             : 
    5221             :     /*
    5222             :      * If a get_object_address representation was requested, make sure we are
    5223             :      * providing one.  We don't check objargs, because many of the cases above
    5224             :      * leave it as NIL.
    5225             :      */
    5226        3398 :     if (objname && *objname == NIL)
    5227           0 :         elog(ERROR, "requested object address for unsupported object class %d: text result \"%s\"",
    5228             :              (int) getObjectClass(object), buffer.data);
    5229             : 
    5230        3398 :     return buffer.data;
    5231             : }
    5232             : 
    5233             : static void
    5234          86 : getOpFamilyIdentity(StringInfo buffer, Oid opfid, List **object)
    5235             : {
    5236             :     HeapTuple   opfTup;
    5237             :     Form_pg_opfamily opfForm;
    5238             :     HeapTuple   amTup;
    5239             :     Form_pg_am  amForm;
    5240             :     char       *schema;
    5241             : 
    5242          86 :     opfTup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
    5243          86 :     if (!HeapTupleIsValid(opfTup))
    5244           0 :         elog(ERROR, "cache lookup failed for opfamily %u", opfid);
    5245          86 :     opfForm = (Form_pg_opfamily) GETSTRUCT(opfTup);
    5246             : 
    5247          86 :     amTup = SearchSysCache1(AMOID, ObjectIdGetDatum(opfForm->opfmethod));
    5248          86 :     if (!HeapTupleIsValid(amTup))
    5249           0 :         elog(ERROR, "cache lookup failed for access method %u",
    5250             :              opfForm->opfmethod);
    5251          86 :     amForm = (Form_pg_am) GETSTRUCT(amTup);
    5252             : 
    5253          86 :     schema = get_namespace_name_or_temp(opfForm->opfnamespace);
    5254          86 :     appendStringInfo(buffer, "%s USING %s",
    5255             :                      quote_qualified_identifier(schema,
    5256          86 :                                                 NameStr(opfForm->opfname)),
    5257          86 :                      NameStr(amForm->amname));
    5258             : 
    5259          86 :     if (object)
    5260          12 :         *object = list_make3(pstrdup(NameStr(amForm->amname)),
    5261             :                              pstrdup(schema),
    5262             :                              pstrdup(NameStr(opfForm->opfname)));
    5263             : 
    5264          86 :     ReleaseSysCache(amTup);
    5265          86 :     ReleaseSysCache(opfTup);
    5266          86 : }
    5267             : 
    5268             : /*
    5269             :  * Append the relation identity (quoted qualified name) to the given
    5270             :  * StringInfo.
    5271             :  */
    5272             : static void
    5273        1458 : getRelationIdentity(StringInfo buffer, Oid relid, List **object)
    5274             : {
    5275             :     HeapTuple   relTup;
    5276             :     Form_pg_class relForm;
    5277             :     char       *schema;
    5278             : 
    5279        1458 :     relTup = SearchSysCache1(RELOID,
    5280             :                              ObjectIdGetDatum(relid));
    5281        1458 :     if (!HeapTupleIsValid(relTup))
    5282           0 :         elog(ERROR, "cache lookup failed for relation %u", relid);
    5283        1458 :     relForm = (Form_pg_class) GETSTRUCT(relTup);
    5284             : 
    5285        1458 :     schema = get_namespace_name_or_temp(relForm->relnamespace);
    5286        1458 :     appendStringInfoString(buffer,
    5287        1458 :                            quote_qualified_identifier(schema,
    5288        1458 :                                                       NameStr(relForm->relname)));
    5289        1458 :     if (object)
    5290         928 :         *object = list_make2(schema, pstrdup(NameStr(relForm->relname)));
    5291             : 
    5292        1458 :     ReleaseSysCache(relTup);
    5293        1458 : }
    5294             : 
    5295             : /*
    5296             :  * Auxiliary function to build a TEXT array out of a list of C-strings.
    5297             :  */
    5298             : ArrayType *
    5299        1064 : strlist_to_textarray(List *list)
    5300             : {
    5301             :     ArrayType  *arr;
    5302             :     Datum      *datums;
    5303             :     bool       *nulls;
    5304        1064 :     int         j = 0;
    5305             :     ListCell   *cell;
    5306             :     MemoryContext memcxt;
    5307             :     MemoryContext oldcxt;
    5308             :     int         lb[1];
    5309             : 
    5310             :     /* Work in a temp context; easier than individually pfree'ing the Datums */
    5311        1064 :     memcxt = AllocSetContextCreate(CurrentMemoryContext,
    5312             :                                    "strlist to array",
    5313             :                                    ALLOCSET_DEFAULT_SIZES);
    5314        1064 :     oldcxt = MemoryContextSwitchTo(memcxt);
    5315             : 
    5316        1064 :     datums = (Datum *) palloc(sizeof(Datum) * list_length(list));
    5317        1064 :     nulls = palloc(sizeof(bool) * list_length(list));
    5318             : 
    5319        2776 :     foreach(cell, list)
    5320             :     {
    5321        1712 :         char       *name = lfirst(cell);
    5322             : 
    5323        1712 :         if (name)
    5324             :         {
    5325        1712 :             nulls[j] = false;
    5326        1712 :             datums[j++] = CStringGetTextDatum(name);
    5327             :         }
    5328             :         else
    5329           0 :             nulls[j] = true;
    5330             :     }
    5331             : 
    5332        1064 :     MemoryContextSwitchTo(oldcxt);
    5333             : 
    5334        1064 :     lb[0] = 1;
    5335        1064 :     arr = construct_md_array(datums, nulls, 1, &j,
    5336             :                              lb, TEXTOID, -1, false, 'i');
    5337             : 
    5338        1064 :     MemoryContextDelete(memcxt);
    5339             : 
    5340        1064 :     return arr;
    5341             : }
    5342             : 
    5343             : /*
    5344             :  * get_relkind_objtype
    5345             :  *
    5346             :  * Return the object type for the relkind given by the caller.
    5347             :  *
    5348             :  * If an unexpected relkind is passed, we say OBJECT_TABLE rather than
    5349             :  * failing.  That's because this is mostly used for generating error messages
    5350             :  * for failed ACL checks on relations, and we'd rather produce a generic
    5351             :  * message saying "table" than fail entirely.
    5352             :  */
    5353             : ObjectType
    5354         586 : get_relkind_objtype(char relkind)
    5355             : {
    5356         586 :     switch (relkind)
    5357             :     {
    5358             :         case RELKIND_RELATION:
    5359             :         case RELKIND_PARTITIONED_TABLE:
    5360         464 :             return OBJECT_TABLE;
    5361             :         case RELKIND_INDEX:
    5362             :         case RELKIND_PARTITIONED_INDEX:
    5363          12 :             return OBJECT_INDEX;
    5364             :         case RELKIND_SEQUENCE:
    5365           4 :             return OBJECT_SEQUENCE;
    5366             :         case RELKIND_VIEW:
    5367         100 :             return OBJECT_VIEW;
    5368             :         case RELKIND_MATVIEW:
    5369           0 :             return OBJECT_MATVIEW;
    5370             :         case RELKIND_FOREIGN_TABLE:
    5371           2 :             return OBJECT_FOREIGN_TABLE;
    5372             :         case RELKIND_TOASTVALUE:
    5373           4 :             return OBJECT_TABLE;
    5374             :         default:
    5375             :             /* Per above, don't raise an error */
    5376           0 :             return OBJECT_TABLE;
    5377             :     }
    5378             : }

Generated by: LCOV version 1.13