Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b8fab24

Browse files
committed
Add pg_typeof() function.
Brendan Jurd
1 parent 06c22d7 commit b8fab24

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

doc/src/sgml/func.sgml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.451 2008/10/27 09:37:46 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.452 2008/11/03 17:51:12 tgl Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -11643,6 +11643,10 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1164311643
<primary>pg_tablespace_databases</primary>
1164411644
</indexterm>
1164511645

11646+
<indexterm>
11647+
<primary>pg_typeof</primary>
11648+
</indexterm>
11649+
1164611650
<para>
1164711651
<xref linkend="functions-info-catalog-table"> lists functions that
1164811652
extract information from the system catalogs.
@@ -11766,6 +11770,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1176611770
<entry><type>setof oid</type></entry>
1176711771
<entry>get the set of database OIDs that have objects in the tablespace</entry>
1176811772
</row>
11773+
<row>
11774+
<entry><literal><function>pg_typeof</function>(<parameter>any</parameter>)</literal></entry>
11775+
<entry><type>regtype</type></entry>
11776+
<entry>get the data type of any value</entry>
11777+
</row>
1176911778
</tbody>
1177011779
</tgroup>
1177111780
</table>
@@ -11848,6 +11857,12 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1184811857
<structname>pg_class</> catalogs.
1184911858
</para>
1185011859

11860+
<para>
11861+
<function>pg_typeof</function> returns the OID of the data type of the
11862+
value that is passed to it. This can be helpful for troubleshooting or
11863+
dynamically constructing SQL queries.
11864+
</para>
11865+
1185111866
<indexterm>
1185211867
<primary>col_description</primary>
1185311868
</indexterm>

src/backend/utils/adt/misc.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.64 2008/10/05 17:33:16 petere Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.65 2008/11/03 17:51:13 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -371,3 +371,13 @@ pg_get_keywords(PG_FUNCTION_ARGS)
371371

372372
SRF_RETURN_DONE(funcctx);
373373
}
374+
375+
376+
/*
377+
* Return the type of the argument.
378+
*/
379+
Datum
380+
pg_typeof(PG_FUNCTION_ARGS)
381+
{
382+
PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0));
383+
}

src/include/catalog/catversion.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.500 2008/10/31 08:39:22 heikki Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.501 2008/11/03 17:51:13 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200810311
56+
#define CATALOG_VERSION_NO 200811031
5757

5858
#endif

src/include/catalog/pg_proc.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.520 2008/10/14 17:12:33 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.521 2008/11/03 17:51:13 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2289,7 +2289,8 @@ DESCR("result type of a function");
22892289

22902290
DATA(insert OID = 1686 ( pg_get_keywords PGNSP PGUID 12 10 400 0 f f t t s 0 2249 "" "{25,18,25}" "{o,o,o}" "{word,catcode,catdesc}" pg_get_keywords _null_ _null_ _null_ ));
22912291
DESCR("list of SQL keywords");
2292-
2292+
DATA(insert OID = 1619 ( pg_typeof PGNSP PGUID 12 1 0 0 f f f f i 1 2206 "2276" _null_ _null_ _null_ pg_typeof _null_ _null_ _null_ ));
2293+
DESCR("returns the type of the argument");
22932294

22942295
/* Generic referential integrity constraint triggers */
22952296
DATA(insert OID = 1644 ( RI_FKey_check_ins PGNSP PGUID 12 1 0 0 f f t f v 0 2279 "" _null_ _null_ _null_ RI_FKey_check_ins _null_ _null_ _null_ ));

src/include/utils/builtins.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.324 2008/10/13 16:25:20 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.325 2008/11/03 17:51:13 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -404,6 +404,7 @@ extern Datum pg_tablespace_databases(PG_FUNCTION_ARGS);
404404
extern Datum pg_rotate_logfile(PG_FUNCTION_ARGS);
405405
extern Datum pg_sleep(PG_FUNCTION_ARGS);
406406
extern Datum pg_get_keywords(PG_FUNCTION_ARGS);
407+
extern Datum pg_typeof(PG_FUNCTION_ARGS);
407408

408409
/* oid.c */
409410
extern Datum oidin(PG_FUNCTION_ARGS);

src/test/regress/expected/polymorphism.out

+55
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,58 @@ LINE 1: select formarray(1, variadic array['x'::text]);
721721
^
722722
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
723723
drop function formarray(anyelement, variadic anyarray);
724+
-- test pg_typeof() function
725+
select pg_typeof(null); -- unknown
726+
pg_typeof
727+
-----------
728+
unknown
729+
(1 row)
730+
731+
select pg_typeof(0); -- integer
732+
pg_typeof
733+
-----------
734+
integer
735+
(1 row)
736+
737+
select pg_typeof(0.0); -- numeric
738+
pg_typeof
739+
-----------
740+
numeric
741+
(1 row)
742+
743+
select pg_typeof(1+1 = 2); -- boolean
744+
pg_typeof
745+
-----------
746+
boolean
747+
(1 row)
748+
749+
select pg_typeof('x'); -- unknown
750+
pg_typeof
751+
-----------
752+
unknown
753+
(1 row)
754+
755+
select pg_typeof('' || ''); -- text
756+
pg_typeof
757+
-----------
758+
text
759+
(1 row)
760+
761+
select pg_typeof(pg_typeof(0)); -- regtype
762+
pg_typeof
763+
-----------
764+
regtype
765+
(1 row)
766+
767+
select pg_typeof(array[1.2,55.5]); -- numeric[]
768+
pg_typeof
769+
-----------
770+
numeric[]
771+
(1 row)
772+
773+
select pg_typeof(myleast(10, 1, 20, 33)); -- polymorphic input
774+
pg_typeof
775+
-----------
776+
integer
777+
(1 row)
778+

src/test/regress/sql/polymorphism.sql

+11
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,14 @@ select formarray(1, 'x'::text); -- fail, type mismatch
469469
select formarray(1, variadic array['x'::text]); -- fail, type mismatch
470470

471471
drop function formarray(anyelement, variadic anyarray);
472+
473+
-- test pg_typeof() function
474+
select pg_typeof(null); -- unknown
475+
select pg_typeof(0); -- integer
476+
select pg_typeof(0.0); -- numeric
477+
select pg_typeof(1+1 = 2); -- boolean
478+
select pg_typeof('x'); -- unknown
479+
select pg_typeof('' || ''); -- text
480+
select pg_typeof(pg_typeof(0)); -- regtype
481+
select pg_typeof(array[1.2,55.5]); -- numeric[]
482+
select pg_typeof(myleast(10, 1, 20, 33)); -- polymorphic input

0 commit comments

Comments
 (0)