Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb8fab24

Browse files
committed
Add pg_typeof() function.
Brendan Jurd
1 parent06c22d7 commitb8fab24

File tree

7 files changed

+100
-7
lines changed

7 files changed

+100
-7
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 16 additions & 1 deletion
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

Lines changed: 11 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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-
#defineCATALOG_VERSION_NO200810311
56+
#defineCATALOG_VERSION_NO200811031
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 3 additions & 2 deletions
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(insertOID=1686 (pg_get_keywordsPGNSPPGUID12104000fftts02249"""{25,18,25}""{o,o,o}""{word,catcode,catdesc}"pg_get_keywords_null__null__null_ ));
22912291
DESCR("list of SQL keywords");
2292-
2292+
DATA(insertOID=1619 (pg_typeofPGNSPPGUID12100ffffi12206"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(insertOID=1644 (RI_FKey_check_insPGNSPPGUID12100fftfv02279""_null__null__null_RI_FKey_check_ins_null__null__null_ ));

‎src/include/utils/builtins.h

Lines changed: 2 additions & 1 deletion
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
externDatumpg_rotate_logfile(PG_FUNCTION_ARGS);
405405
externDatumpg_sleep(PG_FUNCTION_ARGS);
406406
externDatumpg_get_keywords(PG_FUNCTION_ARGS);
407+
externDatumpg_typeof(PG_FUNCTION_ARGS);
407408

408409
/* oid.c */
409410
externDatumoidin(PG_FUNCTION_ARGS);

‎src/test/regress/expected/polymorphism.out

Lines changed: 55 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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
dropfunction 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)

[8]ページ先頭

©2009-2025 Movatter.jp