是否可以从 Postgres 函数中发现列类型?
我正在开发一个实用程序,该实用程序使用模板针对 Postgres 数据库生成数据访问层。作为其中的一部分,我试图动态发现存储过程的返回类型。在返回单个标准类型的简单情况下,这很容易,但是当它返回用户定义的类型时,我很困难。
如果有人可以提供必要的 SQL 来返回此数据,我将不胜感激。
谢谢 马克,
我很欣赏到目前为止我得到的答案,这些答案实际上归结为以下 SQL
SELECT p.proname, t.typname, p,proretset
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_type t ON p.prorettype = t.oid
WHERE n.nspname = 'public'
--and proname = 'foo'
ORDER BY proname;
这将返回返回类型的名称。然而,我仍然需要将类型分解为当它返回用户定义类型时组成它的属性。
在函数返回记录的情况下,我认为除了调用函数并检查其返回值之外,没有任何方法可以发现其返回结构。
I'm working on a utility that is using templates to generate a data access layer against a Postgres database. As part of this I'm trying to dynamically discover the return types of the stored procedures. This is easy enough in simple cases where a single standard type is returned, but I'm struggling when it comes to it returning a user defined type.
I'd appreciate if someone could provide the necessary SQL to return this data.
Thanks
Mark
I appreciate the answers that I have so far, which effectively boil to to the following SQL
SELECT p.proname, t.typname, p,proretset
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_type t ON p.prorettype = t.oid
WHERE n.nspname = 'public'
--and proname = 'foo'
ORDER BY proname;
This will return the name of the return types. However I still need to decompose the type into the properties that make it up when it returns a user defined type.
In the case that a function returns a record I don't think there is any way to discover its return structure other than calling the function and examining its return values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
psql
元命令是查找信息模式内容的简单快捷方式。尝试
test=# \d?
查找内省信息。然后 psql -E -c '\df' 将显示 show function 命令后面的 sql:
在您的情况下,这将是您想要的:
psql
meta commands are an easy shortcut to finding information schema stuff.try
test=# \d?
to find introspection info.then
psql -E -c '\df'
will show the sql behind the show function command:In your case the would be what you want:
此查询将列出带有类型的存储过程。
您始终可以按昵称进行过滤。
This query will list the stored procedures with the types.
You can always filter by proname.
刚开始:
Just to get started:
您在找这个吗?
Are you looking for this?
如果函数返回
记录
,则直到运行时才知道类型,如下所示:换句话说,在调用之后之前,您无法知道类型函数。调用该函数后,您可以将有关
记录
的信息传递到参考此处if the function returns a
record
then the type is not known until runtime, demonstrated with:in other words, you can't know the type until after you call the function. Once you have called the function, you could dynamically determine information about the
record
by passing it into a C-language function as referenced here感谢大家的帮助,我认为 JackPDouglas 是正确的,并且由于返回记录集的函数可以是多态的,因此无法找出返回类型定义。
然而,这是我正在寻找的用于获取返回复合类型的函数的定义的 SQL:
Thanks for the help guys, I'm think JackPDouglas is correct and that since functions that return record sets can be polymorphic that there's no way to find out the return type definition.
However here's the SQL I was looking for to get the definition of a function that returns a composite type: