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