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

Necessary to close a returned refcursor from a stored plpgsql function or not?

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

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

这是我在强制事务中使用的 returns 游标。我是这样使用的:

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 用于 releasing resources earlier than end of transaction,因此如果您不关闭光标并且 endcommit 您的交易那么它应该无关紧要全部.