pg_typeof() (was: Mysterious Bus Error with get_fn_expr_argtype())
On Tue, Sep 2, 2008 at 3:31 PM, Brendan Jurd <direvus@gmail.com> wrote:
Barring any further comments/objections, I'll go ahead and prepare a
patch to add this to core.
Any opinions on where pg_typeof() should be defined? This function is
a little unusual and doesn't seem to slot nicely into any one of the
existing categories.
A few candidates:
* regproc.c - it contains functions for working with regtype.
* ruleutils.c - it contains a lot of other system information
functions, like pg_get_expr(), pg_get_userbyid(), et al.
* format_type.c - functions for presenting type information to the user.
* misc.c
Personally I'd be inclined to go with misc.c (if it's not clear where
it belongs, that probably makes it miscellany), but it's a weak
preference only.
Cheers,
BJ
Hi folks,
As discussed on -hackers [1]http://archives.postgresql.org/message-id/37ed240d0809012135k1a7621d5x2acf18b31c7f9281@mail.gmail.com, here is a patch to add a pg_typeof()
builtin function to core.
The function accepts one argument (type "any") and returns the regtype
of that argument. This can be helpful in various circumstances,
including troubleshooting cast/coercion behaviour in a query, or
constructing dynamic SQL statements.
It's declared in builtins.h and defined in utils/adt/misc.c.
The patch includes a small documentation update; I added pg_typeof()
to Table 9-47. System Catalog Information Functions, and a brief
descriptive paragraph underneath the table.
This didn't seem like it warranted any additional regression tests.
Added to the November commitfest.
Cheers,
BJ
doc/src/sgml/func.sgml | 15 +++++++++++++++
src/backend/utils/adt/misc.c | 9 +++++++++
src/include/catalog/catversion.h | 2 !!
src/include/catalog/pg_proc.h | 2 ++
src/include/utils/builtins.h | 1 +
5 files changed, 27 insertions(+), 2 modifications(!)
Attachments:
pg_typeof.diffapplication/octet-stream; name=pg_typeof.diffDownload
*** doc/src/sgml/func.sgml
--- doc/src/sgml/func.sgml
***************
*** 11551,11556 **** SELECT pg_type_is_visible('myschema.widget'::regtype);
--- 11551,11560 ----
</indexterm>
<indexterm>
+ <primary>pg_typeof</primary>
+ </indexterm>
+
+ <indexterm>
<primary>pg_get_keywords</primary>
</indexterm>
***************
*** 11617,11622 **** SELECT pg_type_is_visible('myschema.widget'::regtype);
--- 11621,11631 ----
<entry>get SQL name of a data type</entry>
</row>
<row>
+ <entry><literal><function>pg_typeof</function>(<parameter>any</parameter>)</literal></entry>
+ <entry><type>regtype</type></entry>
+ <entry>get the data type of any value</entry>
+ </row>
+ <row>
<entry><literal><function>pg_get_keywords</function>()</literal></entry>
<entry><type>setof record</type></entry>
<entry>get list of SQL keywords and their categories</entry>
***************
*** 11727,11732 **** SELECT pg_type_is_visible('myschema.widget'::regtype);
--- 11736,11747 ----
</para>
<para>
+ <function>pg_typeof</function> returns the SQL name of the data type of any
+ value which is passed to it as an argument. This can be helpful for
+ troubleshooting or dynamically constructing SQL queries.
+ </para>
+
+ <para>
<function>pg_get_keywords</function> returns a set of records describing
the SQL keywords recognized by the server. The <structfield>word</> column
contains the keyword. The <structfield>catcode</> column contains a
*** src/backend/utils/adt/misc.c
--- src/backend/utils/adt/misc.c
***************
*** 59,64 **** nonnullvalue(PG_FUNCTION_ARGS)
--- 59,73 ----
}
/*
+ * Return the type of the argument.
+ */
+ Datum
+ pg_typeof(PG_FUNCTION_ARGS)
+ {
+ PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
+ }
+
+ /*
* current_database()
* Expose the current database to the user
*/
*** src/include/catalog/catversion.h
--- src/include/catalog/catversion.h
***************
*** 53,58 ****
*/
/* yyyymmddN */
! #define CATALOG_VERSION_NO 200808311
#endif
--- 53,58 ----
*/
/* yyyymmddN */
! #define CATALOG_VERSION_NO 200809031
#endif
*** src/include/catalog/pg_proc.h
--- src/include/catalog/pg_proc.h
***************
*** 1121,1126 **** DESCR("does not match LIKE expression");
--- 1121,1128 ----
DATA(insert OID = 860 ( bpchar PGNSP PGUID 12 1 0 0 f f t f i 1 1042 "18" _null_ _null_ _null_ char_bpchar _null_ _null_ _null_ ));
DESCR("convert char to char()");
+ DATA(insert OID = 826 ( pg_typeof PGNSP PGUID 12 1 0 0 f f f f i 1 2206 "2276" _null_ _null_ _null_ pg_typeof _null_ _null_ _null_ ));
+ DESCR("returns the type of the argument");
DATA(insert OID = 861 ( current_database PGNSP PGUID 12 1 0 0 f f t f s 0 19 "" _null_ _null_ _null_ current_database _null_ _null_ _null_ ));
DESCR("returns the current database");
DATA(insert OID = 817 ( current_query PGNSP PGUID 12 1 0 0 f f f f v 0 25 "" _null_ _null_ _null_ current_query _null_ _null_ _null_ ));
*** src/include/utils/builtins.h
--- src/include/utils/builtins.h
***************
*** 403,408 **** extern Datum pg_ls_dir(PG_FUNCTION_ARGS);
--- 403,409 ----
/* misc.c */
extern Datum nullvalue(PG_FUNCTION_ARGS);
extern Datum nonnullvalue(PG_FUNCTION_ARGS);
+ extern Datum pg_typeof(PG_FUNCTION_ARGS);
extern Datum current_database(PG_FUNCTION_ARGS);
extern Datum current_query(PG_FUNCTION_ARGS);
extern Datum pg_cancel_backend(PG_FUNCTION_ARGS);