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