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