如何获取 return 类型的 table returning 函数的列定义列表

How can I get a list of column definitions of the return type of a table returning function

我正在使用 postgres 12。我定义了一些函数,它们具有 TABLE(...) 作为 return 值。我知道如何查询 information_schema and/or pg_proc 以获取给定名称和架构的函数的参数列表。当 return 类型是 TABLE 时,我想做类似的事情,即找到一个查询,当函数 return 是一组记录时,将 return return table.

的列的序号位置和 oid(或名称)的列表

这可能吗?

编辑:我知道 pg_get_function_result() 其中 return 是 return 定义的文本,因为它写在函数定义中,但我必须解析它并且不知道有没有更简单的方法

pg_proc.prorettype 将包含 table 类型的 OID,因此您可以使用:

select prorettype::regtype::text
from pg_proc
where proname = 'your_function';

获取类型的名称 - 这也是 table 的名称。

要获取所有列,您可以将 pg_proc 与 pg_class 连接起来以获取 table 的 oid,然后使用它来查找列。

select col.attname, col.attnum, format_type(col.atttypid, col.atttypmod) as data_type
from pg_attribute col
where not attisdropped
  and attnum > 0
  and attrelid in (select tbl.oid
                   from pg_class tbl
                      join pg_proc p on p.prorettype = tbl.reltype
                   where p.proname = 'your_function_name_here')
order by col.attnum

如果您需要获取由定义为 returns table() 的函数返回的列名,它们可在数组 pg_proc.proargnames 中用于那些通过 [=16] 定义为“out”参数的列名=]

select t.column_name, t.arg_type::regtype::text, t.col_num
from pg_proc p
  cross join unnest(proargnames, proargmodes, proallargtypes) with ordinality as t(column_name, arg_mode, arg_type, col_num)
where p.proname = 'your_function_name_here'
  and t.arg_mode = 't'
order by t.col_num;