简体   繁体   English

如何从postgres函数返回临时表?

[英]how to return temp table from postgres function?

I have below query running fine independently, but showing many issues inside a postgres function 我在下面的查询中独立运行良好,但是在postgres函数中显示了许多问题

CREATE TEMP TABLE tbl (h ltree, pathid int) ; 
CREATE TEMP TABLE temp_res (pathid int, res_count int) ; 
insert into tbl select l_tree,pathid from tblinfo where parentid in (880);
insert into temp_res select T.pathid pathid from tblinfo p1, tbl T where index(p1.l_tree,T.h ) != -1 GROUP BY T.pathid order by T.pathid;
select p.pathid pathid, p.name name, p.PBS PBS,p.parentid parentid,p.resid resid from tblinfo p, temp_res t where t.pathid = p.pathid;

i just need a function like 我只需要一个像

CREATE OR REPLACE FUNCTION getresourceinfo(opened_path int,tablename varchar) returns TABLE (pathid int,name varchar,pbs varchar, parentid varchar, resid int) AS $BODY$ 

just need to use two variables opened_path and tablename for 880 and tblinfo respectively. 只需分别为880和tblinfo使用两个变量open_path和tablename。 I know there many posts about returning tables but I am asking after trying many of them to my basic postgres knowledge any suggestions would be of great help. 我知道有很多关于返回表的帖子,但是在尝试了许多关于Postgres的基础知识之后,我想问一下任何建议对您有很大帮助。 If you feel my query is clumsy please just help me with one function that takes 2 arguments a number n and tablename. 如果您觉得我的查询很笨拙,请为我提供一个函数,该函数需要2个参数,分别是数字n和表名。 Assume there are 10 columns and one of them is serial number now function should return all rows >n and not all but 2 or 3 columns of tablename. 假设有10列,其中之一是序列号,现在函数应该返回所有大于n的行,而不是返回表名的2或3列。

Temporary table 临时表

To answer your question in the title: 在标题中回答您的问题:
One cannot "return a temp table from postgres function". 一个人不能 “从postgres函数返回一个临时表”。 Temporary tables are created and automatically visible to the same user within the same session. 临时表已创建,并且在同一会话中对同一用户自动可见。 They are dropped automatically at the end of the session (or sooner). 在会话结束时(或更早),它们会自动删除。

Table function 表功能

But a set-returning function (aka "table function") can be used just like a table: 但是可以像表一样使用返回集合的函数(也称为“表函数”):

CREATE OR REPLACE FUNCTION getresourceinfo(tablename regclass, opened_path int)
  RETURNS TABLE (pathid int, name varchar, pbs varchar
               , parentid varchar, resid int) AS
$func$ 
BEGIN

RETURN QUERY EXECUTE format(
  'SELECT t.pathid, t.name, t.pbs, t.parentid, t.resid
   FROM  ' || tablename || ' t
   WHERE  t.opened_path = $1'
   )
USING opened_path;

END
$func$ LANGUAGE plpgsql;

Would only make sense for a bunch of tables that all share the hard coded column names with the same data type. 只有一堆都共享具有相同数据类型的硬编码列名的表才有意义。
Call (just like selecting from a table): 致电(就像从表格中选择一样):

SELECT * FROM getresourceinfo(1, 'my_schema.my_tbl')

Why the data type regclass for the table parameter? 为什么表参数的数据类型为regclass
Table name as a PostgreSQL function parameter 表名作为PostgreSQL函数参数

Cursor 光标

For completeness: One can return a CURSOR , which would be a very similar concept as what you ask for. 为了完整性:可以返回CURSOR ,这与您要求的概念非常相似。 Details in the manual here. 手册中的详细信息在这里。
But I hardly ever use cursors. 但是我几乎从不使用游标。 Table functions are more practical most of the time. 在大多数情况下,表函数更实用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM