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