diff -r 4b92d79506ba doc/src/sgml/func.sgml --- a/doc/src/sgml/func.sgml Mon Nov 03 01:17:08 2008 +0000 +++ b/doc/src/sgml/func.sgml Mon Nov 03 00:13:50 2008 -0800 @@ -11592,6 +11592,10 @@ + pg_typeof + + + pg_get_keywords @@ -11660,6 +11664,11 @@ format_type(type_oid, typemod) text get SQL name of a data type + + + pg_typeof(any) + regtype + get the data type of any value pg_get_keywords() @@ -11774,6 +11783,12 @@ format_type returns the SQL name of a data type that is identified by its type OID and possibly a type modifier. Pass NULL for the type modifier if no specific modifier is known. + + + + pg_typeof returns the OID 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. diff -r 4b92d79506ba src/backend/utils/adt/misc.c --- a/src/backend/utils/adt/misc.c Mon Nov 03 01:17:08 2008 +0000 +++ b/src/backend/utils/adt/misc.c Mon Nov 03 00:13:51 2008 -0800 @@ -35,6 +35,15 @@ #define atooid(x) ((Oid) strtoul((x), NULL, 10)) + +/* + * Return the type of the argument. + */ +Datum +pg_typeof(PG_FUNCTION_ARGS) +{ + PG_RETURN_OID(get_fn_expr_argtype(fcinfo->flinfo, 0)); +} /* * current_database() diff -r 4b92d79506ba src/include/catalog/catversion.h --- a/src/include/catalog/catversion.h Mon Nov 03 01:17:08 2008 +0000 +++ b/src/include/catalog/catversion.h Mon Nov 03 00:13:51 2008 -0800 @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200810311 +#define CATALOG_VERSION_NO 200811021 #endif diff -r 4b92d79506ba src/include/catalog/pg_proc.h --- a/src/include/catalog/pg_proc.h Mon Nov 03 01:17:08 2008 +0000 +++ b/src/include/catalog/pg_proc.h Mon Nov 03 00:13:51 2008 -0800 @@ -1085,6 +1085,9 @@ DESCR("greater-than-or-equal"); /* OIDS 800 - 899 */ + +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 = 846 ( cash_mul_flt4 PGNSP PGUID 12 1 0 0 f f t f i 2 790 "790 700" _null_ _null_ _null_ cash_mul_flt4 _null_ _null_ _null_ )); DESCR("multiply"); diff -r 4b92d79506ba src/include/utils/builtins.h --- a/src/include/utils/builtins.h Mon Nov 03 01:17:08 2008 +0000 +++ b/src/include/utils/builtins.h Mon Nov 03 00:13:51 2008 -0800 @@ -395,6 +395,7 @@ extern Datum pg_ls_dir(PG_FUNCTION_ARGS); /* misc.c */ +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); diff -r 4b92d79506ba src/test/regress/expected/polymorphism.out --- a/src/test/regress/expected/polymorphism.out Mon Nov 03 01:17:08 2008 +0000 +++ b/src/test/regress/expected/polymorphism.out Mon Nov 03 00:13:51 2008 -0800 @@ -688,7 +688,6 @@ (1 row) -drop function concat(text, anyarray); -- mix variadic with anyelement create function formarray(anyelement, variadic anyarray) returns anyarray as $$ select array_prepend($1, $2); @@ -720,4 +719,52 @@ LINE 1: select formarray(1, variadic array['x'::text]); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. +-- test pg_typeof() function +select pg_typeof(null), -- unknown + pg_typeof(0), -- integer + pg_typeof(0.0), -- numeric + pg_typeof(1+1 = 2), -- boolean + pg_typeof('x'), -- unknown + pg_typeof('' || ''), -- text + pg_typeof(pg_typeof(0)); -- regtype + pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof +-----------+-----------+-----------+-----------+-----------+-----------+----------- + unknown | integer | numeric | boolean | unknown | text | regtype +(1 row) + +select pg_typeof(f1), pg_typeof(sql_if(f1 > 0, bleat(f1), bleat(f1 + 1))) from int4_tbl limit 1; +NOTICE: bleat 1 + pg_typeof | pg_typeof +-----------+----------- + integer | integer +(1 row) + +select pg_typeof(q2), pg_typeof(sql_if(q2 > 0, q2, q2 + 1)) from int8_tbl limit 1; + pg_typeof | pg_typeof +-----------+----------- + bigint | bigint +(1 row) + +select pg_typeof(myleast(10, 1, 20, 33)), + pg_typeof(myleast(variadic array[1.1, -5.5])), + pg_typeof(formarray(1,2,3,4,5)), + pg_typeof(formarray(1.1, variadic array[1.2,55.5])), + pg_typeof(concat('%', 1, 2, 3, 4, 5)); + pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof +-----------+-----------+-----------+-----------+----------- + integer | numeric | integer[] | numeric[] | text +(1 row) + +select pg_typeof(array[1.2,55.5]), + pg_typeof(variadic array[1,2,33]), + pg_typeof(variadic array[]::int[]), + pg_typeof(row(1,1.1)), + pg_typeof(array[ row(7,7.7), row(1,1.0), row(0,0.0) ]); + pg_typeof | pg_typeof | pg_typeof | pg_typeof | pg_typeof +-----------+-----------+-----------+-----------+----------- + numeric[] | integer[] | integer[] | record | record[] +(1 row) + +-- cleanup +drop function concat(text, anyarray); drop function formarray(anyelement, variadic anyarray); diff -r 4b92d79506ba src/test/regress/sql/polymorphism.sql --- a/src/test/regress/sql/polymorphism.sql Mon Nov 03 01:17:08 2008 +0000 +++ b/src/test/regress/sql/polymorphism.sql Mon Nov 03 00:13:51 2008 -0800 @@ -455,8 +455,6 @@ select concat('|', variadic array[1,2,33]); select concat('|', variadic array[]::int[]); -drop function concat(text, anyarray); - -- mix variadic with anyelement create function formarray(anyelement, variadic anyarray) returns anyarray as $$ select array_prepend($1, $2); @@ -468,4 +466,27 @@ select formarray(1, 'x'::text); -- fail, type mismatch select formarray(1, variadic array['x'::text]); -- fail, type mismatch +-- test pg_typeof() function +select pg_typeof(null), -- unknown + pg_typeof(0), -- integer + pg_typeof(0.0), -- numeric + pg_typeof(1+1 = 2), -- boolean + pg_typeof('x'), -- unknown + pg_typeof('' || ''), -- text + pg_typeof(pg_typeof(0)); -- regtype +select pg_typeof(f1), pg_typeof(sql_if(f1 > 0, bleat(f1), bleat(f1 + 1))) from int4_tbl limit 1; +select pg_typeof(q2), pg_typeof(sql_if(q2 > 0, q2, q2 + 1)) from int8_tbl limit 1; +select pg_typeof(myleast(10, 1, 20, 33)), + pg_typeof(myleast(variadic array[1.1, -5.5])), + pg_typeof(formarray(1,2,3,4,5)), + pg_typeof(formarray(1.1, variadic array[1.2,55.5])), + pg_typeof(concat('%', 1, 2, 3, 4, 5)); +select pg_typeof(array[1.2,55.5]), + pg_typeof(variadic array[1,2,33]), + pg_typeof(variadic array[]::int[]), + pg_typeof(row(1,1.1)), + pg_typeof(array[ row(7,7.7), row(1,1.0), row(0,0.0) ]); + +-- cleanup +drop function concat(text, anyarray); drop function formarray(anyelement, variadic anyarray);