小编Kof*_*fiB的帖子

如何在函数内部强制 COMMIT 以便其他会话可以看到更新的行?

在 Postgres 12 数据库中,我在一个函数中有多个查询(SELECTUPDATE、 ...),总共需要大约 20 分钟才能完成。我在顶部有一个检查,它执行UPDATEifstatus未运行:

create or replace function aaa.fnc_work() returns varchar as 
$body$
    begin
        if (select count(*) from aaa.monitor where id='invoicing' and status='running')=0 then
           return 'running';
        else
           update aaa.monitor set status='running' where id='invoicing';
        end if;
        --- rest of code ---
        --finally
        update aaa.monitor set status='idle' where id='invoicing';
        return '';
    exception when others then
         return SQLERRM::varchar;
    end
$body$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)

这个想法是为了防止其他用户执行直到--- rest of code ---空闲status

然而,其他人(调用相同的函数)似乎看不到更新的状态,他们也继续并开始执行 …

postgresql stored-procedures transactions plpgsql task-queue

9
推荐指数
1
解决办法
1万
查看次数