我可以像这样从目录表中获取函数返回类型....
SELECT proname, pg_get_function_result(p.oid)
FROM pg_proc p
JOIN pg_namespace n on n.oid = p.pronamespace
WHERE n.nspname = 'someschema' and p.proname = 'somefunction'
我还希望得到返回类型的信息
a)长度
b)精度
c)比例(如果数据类型支持)
可以从pg_catalog
获取,还是必须从information_schema
列中获取?
答案 0 :(得分:1)
注意!对于PostgreSQL标量函数,精度和标度通常是无用的,因为它被忽略了。只有类型很重要。
postgres=# CREATE OR REPLACE FUNCTION foo1() RETURNS numeric(10,3) AS $$ 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) AS $$ BEGIN RETURN 'abcde'; END; $$ LANGUAGE plpgsql; CREATE FUNCTION Time: 28.992 ms postgres=# SELECT foo2(); foo2 ─────── abcde (1 row) Time: 0.746 ms
只有使用域,您才能将(或确保必要的转换)传播到函数的外部:
postgres=# CREATE DOMAIN xx AS numeric(10,3); CREATE DOMAIN postgres=# CREATE OR REPLACE FUNCTION foo4() RETURNS xx AS $$ BEGIN RETURN 10.0/3.0; END; $$ LANGUAGE plpgsql; CREATE FUNCTION Time: 32.417 ms postgres=# SELECT foo4(); foo4 ─────── 3.333 (1 row)
获取有关已使用查询的信息到系统目录的技巧。
bash-4.1$ psql.92 postgres -E psql.92 (9.2.2) Type "help" for help. postgres=# \df ********* QUERY ********** SELECT n.nspname as "Schema", p.proname as "Name", pg_catalog.pg_get_function_result(p.oid) as "Result data type", pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types", CASE WHEN p.proisagg THEN 'agg' WHEN p.proiswindow THEN 'window' WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger' ELSE 'normal' END as "Type" FROM pg_catalog.pg_proc p LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace WHERE pg_catalog.pg_function_is_visible(p.oid) AND n.nspname 'pg_catalog' AND n.nspname 'information_schema' ORDER BY 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)
答案 1 :(得分:0)
看一下这篇文章:http://www.alberton.info/postgresql_meta_info.html#.UglOxtJT63M
我根据那篇文章做了一个简短的回答:
select
p.proname, pg_get_function_result(p.oid),
p.proargnames,
array(
select format_type(a.p::int, null)
from (select regexp_split_to_table(p.proargtypes::text, ' ') as p) as a
)
from pg_proc as p
inner join pg_namespace as n on n.oid = p.pronamespace
where n.nspname = 'someschema' and p.proname = 'somefunction'