相关疑难解决方法(0)

存储过程是否在Postgres中的数据库事务中运行?

如果存储过程在中间失败,那么从SP开始的那一点的更改是否隐式回滚,或者我们是否必须编写任何显式代码以确保SP仅在数据库事务中运行?

database postgresql stored-procedures transactions relational-database

12
推荐指数
2
解决办法
6719
查看次数

如何在函数内部强制 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万
查看次数

如何从 PostgreSQL 存储过程中获取结果集?

我在 PostgreSQL 11 中创建了一个存储过程来执行 CRUD 操作,它适用于 1. 创建 2. 更新 3. 删除,但是当我通过传递Condition = 4选择结果集来运行读取命令时,出现以下错误。

我已经使用 PostgreSQL 函数来获取对我有用的结果集,但是我需要使用 PostgreSQL 存储过程来获取结果。

这是我的存储过程代码:

CREATE OR REPLACE PROCEDURE public.testSpCrud(
    fnam text,
    lnam text,
    id integer,
    condition integer)
LANGUAGE 'plpgsql'

AS $BODY$
declare
    countOfDisc int; 
BEGIN
if condition=1 then

INSERT INTO public.employee(
    employeeid, lname, fname, securitylevel, employeepassword, hphonearea, hphone, cphonearea, cphone, street, city, state, zipcode, extzip, name, email, groomerid, type, commission, inactive, carrierid, notoallemployees, languageid, isdogwalker, ispetsitter, ismobilegroomer, ssma_timestamp)
    VALUES (4,  'Test', 'Test', …
Run Code Online (Sandbox Code Playgroud)

postgresql stored-procedures function postgresql-11

6
推荐指数
2
解决办法
9899
查看次数