gpt4 book ai didi

postgresql - 是否需要关闭存储的 plpgsql 函数返回的 refcursor?

转载 作者:行者123 更新时间:2023-11-29 14:30:17 24 4
gpt4 key购买 nike

我使用 PostgreSQL 使用以下结构存储函数:

create or replace function myfunc() returns refcursor as $$
declare rc refcursor := 'mycursor';
-- ...
begin;
-- ...
open rc for select ...;
return rc;
end; $$ language plpgsql;

这会返回一个我在强制事务中使用的游标。我正在这样使用:

begin;
select myfunc();
fetch all in mycursor;
close mycursor;
commit;

但在大多数示例和教程中,语句 close mycursor; 被简单地省略了。我知道您需要在函数内部使用它时关闭它,但是当它返回时,commit; 可能会自动关闭所有打开的游标?那么,关闭游标真的有必要吗?

不确定如何在 commit; 之后检查它是否仍然打开,因为游标不再在范围内。

最佳答案

作为mention in the docs

CLOSE closes the portal underlying an open cursor. This can be used to release resources earlier than end of transaction, or to free up the cursor variable to be opened again.

您可以使用close cursor_name在事务结束之前释放资源,因此如果您不关闭游标并end提交您的交易,那么它根本不重要。

关于postgresql - 是否需要关闭存储的 plpgsql 函数返回的 refcursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52914479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com