postgresql - How to get function return type's length, precision and scale? -
i function return type catalog table this....
select proname, pg_get_function_result(p.oid) pg_proc p join pg_namespace n on n.oid = p.pronamespace n.nspname = 'someschema' , p.proname = 'somefunction'
i return type's
a) length
b) precision
c) scale (if supported data type)
is possible pg_catalog
or have take information_schema
columns?
attention! precision , scale useless postgresql scalar functions, because ignored. type important.
postgres=# create or replace function foo1() returns numeric(10,3) $$ begin return 10.0/3.0; end; $$ language plpgsql; create function time: 39.511 ms postgres=# select foo1(); foo1 ──────────────────── 3.3333333333333333 (1 row) time: 0.910 ms postgres=# create or replace function foo2() returns varchar(2) $$ begin return 'abcde'; end; $$ language plpgsql; create function time: 28.992 ms postgres=# select foo2(); foo2 ─────── abcde (1 row) time: 0.746 ms
only domain, can propagate (or ensure necessary casting) outer side of function:
postgres=# create domain xx numeric(10,3); create domain postgres=# create or replace function foo4() returns xx $$ begin return 10.0/3.0; end; $$ language plpgsql; create function time: 32.417 ms postgres=# select foo4(); foo4 ─────── 3.333 (1 row)
trick getting info used queries system catalog.
- run psql parameter -e
- use related \d* command
bash-4.1$ psql.92 postgres -e psql.92 (9.2.2) type "help" help. postgres=# \df ********* query ********** select n.nspname "schema", p.proname "name", pg_catalog.pg_get_function_result(p.oid) "result data type", pg_catalog.pg_get_function_arguments(p.oid) "argument data types", case when p.proisagg 'agg' when p.proiswindow 'window' when p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype 'trigger' else 'normal' end "type" pg_catalog.pg_proc p left join pg_catalog.pg_namespace n on n.oid = p.pronamespace pg_catalog.pg_function_is_visible(p.oid) , n.nspname 'pg_catalog' , n.nspname 'information_schema' order 1, 2, 4; ************************** list of functions schema │ name │ result data type │ argument data types │ type ────────┼────────────────────────────┼─────────────────────────────┼─────────────────────┼──────── public │ f1 │ void │ │ normal public │ foo1 │ numeric │ │ normal public │ foo2 │ character varying │ │ normal public │ foo3 │ numeric │ out result numeric │ normal public │ foo4 │ xx │ │ normal public │ to_timestamp_ignore_errors │ timestamp without time zone │ text │ normal public │ xavg │ bigint │ integer │ normal (7 rows)
Comments
Post a Comment