Skip to content

Commit 6cc2deb

Browse files
committed
Add pg_describe_object function
This function is useful to obtain textual descriptions of objects as stored in pg_depend.
1 parent 48c348f commit 6cc2deb

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

doc/src/sgml/func.sgml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12847,6 +12847,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1284712847
<entry><type>setof oid</type></entry>
1284812848
<entry>get the set of database OIDs that have objects in the tablespace</entry>
1284912849
</row>
12850+
<row>
12851+
<entry><literal><function>pg_describe_object(<parameter>catalog_id</parameter>, <parameter>object_id</parameter>, <parameter>object_sub_id</parameter>)</function>)</literal></entry>
12852+
<entry><type>text</type></entry>
12853+
<entry>get description of a database object</entry>
12854+
</row>
1285012855
<row>
1285112856
<entry><literal><function>pg_typeof(<parameter>any</parameter>)</function></literal></entry>
1285212857
<entry><type>regtype</type></entry>
@@ -12940,6 +12945,13 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1294012945
<structname>pg_class</> catalogs.
1294112946
</para>
1294212947

12948+
<para>
12949+
<function>pg_describe_object</function> returns a description of a database
12950+
object specified by catalog OID, object OID and a (possibly zero) sub-object ID.
12951+
This is useful to determine the identity of an object as stored in the
12952+
<structname>pg_depend</structname> catalog.
12953+
</para>
12954+
1294312955
<para>
1294412956
<function>pg_typeof</function> returns the OID of the data type of the
1294512957
value that is passed to it. This can be helpful for troubleshooting or

src/backend/catalog/dependency.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,118 +1982,97 @@ free_object_addresses(ObjectAddresses *addrs)
19821982
ObjectClass
19831983
getObjectClass(const ObjectAddress *object)
19841984
{
1985+
/* only pg_class entries can have nonzero objectSubId */
1986+
if (object->classId != RelationRelationId &&
1987+
object->objectSubId != 0)
1988+
elog(ERROR, "invalid objectSubId 0 for object class %u",
1989+
object->classId);
1990+
19851991
switch (object->classId)
19861992
{
19871993
case RelationRelationId:
19881994
/* caller must check objectSubId */
19891995
return OCLASS_CLASS;
19901996

19911997
case ProcedureRelationId:
1992-
Assert(object->objectSubId == 0);
19931998
return OCLASS_PROC;
19941999

19952000
case TypeRelationId:
1996-
Assert(object->objectSubId == 0);
19972001
return OCLASS_TYPE;
19982002

19992003
case CastRelationId:
2000-
Assert(object->objectSubId == 0);
20012004
return OCLASS_CAST;
20022005

20032006
case ConstraintRelationId:
2004-
Assert(object->objectSubId == 0);
20052007
return OCLASS_CONSTRAINT;
20062008

20072009
case ConversionRelationId:
2008-
Assert(object->objectSubId == 0);
20092010
return OCLASS_CONVERSION;
20102011

20112012
case AttrDefaultRelationId:
2012-
Assert(object->objectSubId == 0);
20132013
return OCLASS_DEFAULT;
20142014

20152015
case LanguageRelationId:
2016-
Assert(object->objectSubId == 0);
20172016
return OCLASS_LANGUAGE;
20182017

20192018
case LargeObjectRelationId:
2020-
Assert(object->objectSubId == 0);
20212019
return OCLASS_LARGEOBJECT;
20222020

20232021
case OperatorRelationId:
2024-
Assert(object->objectSubId == 0);
20252022
return OCLASS_OPERATOR;
20262023

20272024
case OperatorClassRelationId:
2028-
Assert(object->objectSubId == 0);
20292025
return OCLASS_OPCLASS;
20302026

20312027
case OperatorFamilyRelationId:
2032-
Assert(object->objectSubId == 0);
20332028
return OCLASS_OPFAMILY;
20342029

20352030
case AccessMethodOperatorRelationId:
2036-
Assert(object->objectSubId == 0);
20372031
return OCLASS_AMOP;
20382032

20392033
case AccessMethodProcedureRelationId:
2040-
Assert(object->objectSubId == 0);
20412034
return OCLASS_AMPROC;
20422035

20432036
case RewriteRelationId:
2044-
Assert(object->objectSubId == 0);
20452037
return OCLASS_REWRITE;
20462038

20472039
case TriggerRelationId:
2048-
Assert(object->objectSubId == 0);
20492040
return OCLASS_TRIGGER;
20502041

20512042
case NamespaceRelationId:
2052-
Assert(object->objectSubId == 0);
20532043
return OCLASS_SCHEMA;
20542044

20552045
case TSParserRelationId:
2056-
Assert(object->objectSubId == 0);
20572046
return OCLASS_TSPARSER;
20582047

20592048
case TSDictionaryRelationId:
2060-
Assert(object->objectSubId == 0);
20612049
return OCLASS_TSDICT;
20622050

20632051
case TSTemplateRelationId:
2064-
Assert(object->objectSubId == 0);
20652052
return OCLASS_TSTEMPLATE;
20662053

20672054
case TSConfigRelationId:
2068-
Assert(object->objectSubId == 0);
20692055
return OCLASS_TSCONFIG;
20702056

20712057
case AuthIdRelationId:
2072-
Assert(object->objectSubId == 0);
20732058
return OCLASS_ROLE;
20742059

20752060
case DatabaseRelationId:
2076-
Assert(object->objectSubId == 0);
20772061
return OCLASS_DATABASE;
20782062

20792063
case TableSpaceRelationId:
2080-
Assert(object->objectSubId == 0);
20812064
return OCLASS_TBLSPACE;
20822065

20832066
case ForeignDataWrapperRelationId:
2084-
Assert(object->objectSubId == 0);
20852067
return OCLASS_FDW;
20862068

20872069
case ForeignServerRelationId:
2088-
Assert(object->objectSubId == 0);
20892070
return OCLASS_FOREIGN_SERVER;
20902071

20912072
case UserMappingRelationId:
2092-
Assert(object->objectSubId == 0);
20932073
return OCLASS_USER_MAPPING;
20942074

20952075
case DefaultAclRelationId:
2096-
Assert(object->objectSubId == 0);
20972076
return OCLASS_DEFACL;
20982077
}
20992078

@@ -2807,3 +2786,27 @@ getOpFamilyDescription(StringInfo buffer, Oid opfid)
28072786
ReleaseSysCache(amTup);
28082787
ReleaseSysCache(opfTup);
28092788
}
2789+
2790+
/*
2791+
* SQL-level callable version of getObjectDescription
2792+
*/
2793+
Datum
2794+
pg_describe_object(PG_FUNCTION_ARGS)
2795+
{
2796+
Oid classid = PG_GETARG_OID(0);
2797+
Oid objid = PG_GETARG_OID(1);
2798+
int32 subobjid = PG_GETARG_INT32(2);
2799+
char *description = NULL;
2800+
ObjectAddress address;
2801+
2802+
/* for "pinned" items in pg_depend, return null */
2803+
if (!OidIsValid(classid) && !OidIsValid(objid))
2804+
PG_RETURN_NULL();
2805+
2806+
address.classId = classid;
2807+
address.objectId = objid;
2808+
address.objectSubId = subobjid;
2809+
2810+
description = getObjectDescription(&address);
2811+
PG_RETURN_TEXT_P(cstring_to_text(description));
2812+
}

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201011151
56+
#define CATALOG_VERSION_NO 201011181
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,6 +3333,8 @@ DESCR("view system lock information");
33333333
DATA(insert OID = 1065 ( pg_prepared_xact PGNSP PGUID 12 1 1000 0 f f f t t v 0 0 2249 "" "{28,25,1184,26,26}" "{o,o,o,o,o}" "{transaction,gid,prepared,ownerid,dbid}" _null_ pg_prepared_xact _null_ _null_ _null_ ));
33343334
DESCR("view two-phase transactions");
33353335

3336+
DATA(insert OID = 3537 ( pg_describe_object PGNSP PGUID 12 1 0 0 f f f t f s 3 0 25 "26 26 23" _null_ _null_ _null_ _null_ pg_describe_object _null_ _null_ _null_ ));
3337+
33363338
DATA(insert OID = 2079 ( pg_table_is_visible PGNSP PGUID 12 1 0 0 f f f t f s 1 0 16 "26" _null_ _null_ _null_ _null_ pg_table_is_visible _null_ _null_ _null_ ));
33373339
DESCR("is table visible in search path?");
33383340
DATA(insert OID = 2080 ( pg_type_is_visible PGNSP PGUID 12 1 0 0 f f f t f s 1 0 16 "26" _null_ _null_ _null_ _null_ pg_type_is_visible _null_ _null_ _null_ ));

src/include/utils/builtins.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,9 @@ extern Datum window_nth_value(PG_FUNCTION_ARGS);
10451045
/* access/transam/twophase.c */
10461046
extern Datum pg_prepared_xact(PG_FUNCTION_ARGS);
10471047

1048+
/* catalogs/dependency.c */
1049+
extern Datum pg_describe_object(PG_FUNCTION_ARGS);
1050+
10481051
/* commands/constraint.c */
10491052
extern Datum unique_key_recheck(PG_FUNCTION_ARGS);
10501053

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy