From: Brendan Jurd <direvus@gmail.com>
To: pgsql-patches@postgresql.org, "Tom Lane" <tgl@sss.pgh.pa.us>
To: David E. Wheeler <david@kineticode.com>
To: Neil Conway <neilc@samurai.com>
Subject: pg_typeof() (was: Mysterious Bus Error with get_fn_expr_argtype())
Date: Wed, 3 Sep 2008 17:15:36 +1000
Message-ID: <37ed240d0809030015u73b65b66v70fd76741317c6f5@mail.gmail.com> (raw)
In-Reply-To: <37ed240d0809021957r6291d2aej3fc87020a3f7a299@mail.gmail.com>
References: <37ed240d0809021957r6291d2aej3fc87020a3f7a299@mail.gmail.com>
Hi folks,
As discussed on -hackers [1], 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(!)
[1] http://archives.postgresql.org/message-id/37ed240d0809012135k1a7621d5x2acf18b31c7f9281@mail.gmail.co...Attachments:pg_typeof.diff (application/octet-stream, 3.5K) download
*** 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);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-patches@postgresql.org
Cc: direvus@gmail.com, tgl@sss.pgh.pa.us, david@kineticode.com, neilc@samurai.com
Subject: Re: pg_typeof() (was: Mysterious Bus Error with get_fn_expr_argtype())
In-Reply-To: <37ed240d0809030015u73b65b66v70fd76741317c6f5@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox