如何从pg_catalog或information_schema

时间:2016-01-19 17:18:54

标签: postgresql information-schema

我想读取并处理函数返回表类型的列定义...并检测函数返回类型是否为表。

我在information_schema.parameters中找到了列,但我无法区分它是OUT标记的参数还是返回表列定义。

在pg来源中,我注意到了一个" argmode"或者" PROARGMODE"但我不知道如何在information_schemapg_catalog内找到此信息。

我知道有人可以解析pg_catalog.pg_get_function_result()的结果。

第9.4页

1 个答案:

答案 0 :(得分:1)

pg_proc catalog中有两列:proargtypesproallargtypes。 第一个包含函数输入参数的数据类型,第二个包含所有参数的数据类型。 要仅获取输出参数的类型,您应该获得proallargtypes跳过proargtypes元素的片段:

create or replace function function_return_types(p pg_proc)
returns oid[] language sql as $$
    select p.proallargtypes[array_length(p.proargtypes, 1)+ 1 : array_length(p.proallargtypes, 1)]
$$;

示例函数和检索其返回类型的查询:

create or replace function test_function (int, date, text)
returns table (i int, d date, t text)
language sql as $$
    select $1, $2, $3;
$$;

select proname, function_return_types(p)
from pg_proc p
where proname = 'test_function';

    proname    | function_return_types 
---------------+-----------------------
 test_function | {23,1082,25}
(1 row) 

上面的函数返回oids数组。您可以使用它来获取类型名称:

create or replace function function_return_type_names(p pg_proc)
returns name[] language sql as $$
    select array_agg(typname)
    from (
        select typname
            from unnest(function_return_types(p)) with ordinality u
            join pg_type t on t.oid = u
            order by ordinality
        ) s
$$;

select proname, function_return_type_names(p)
from pg_proc p
where proname = 'test_function';

    proname    | function_return_type_names 
---------------+----------------------------
 test_function | {int4,date,text}
(1 row)