【问题标题】:Necessary to close a returned refcursor from a stored plpgsql function or not?是否需要从存储的 plpgsql 函数中关闭返回的 refcursor?
【发布时间】:2018-10-22 13:32:54
【问题描述】:

使用 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; 之后是否仍然打开。

【问题讨论】:

    标签: postgresql plpgsql database-cursor


    【解决方案1】:

    作为mention in the docs

    CLOSE 关闭打开光标下的门户。这可用于在事务结束之前释放资源,或释放游标变量以再次打开。

    您可以将close cursor_name 用于releasing resources earlier than end of transaction,因此,如果您不关闭光标并且您的事务不关闭endcommit,那么它根本不重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      相关资源
      最近更新 更多