postgresql 如何确保所有过程都使用redshift.sql运行,即使单个过程失败

vm0i2vca  于 11个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(185)

我在一个手术中有3个手术

CREATE OR REPLACE PROCEDURE public.a()
LANGUAGE plpgsql
AS $$
BEGIN
call public.abc();
call public.abc1();
call public.abc2();
END;
$$;

字符串
如何确保即使一个单独的过程失败,所有的过程也能运行。
我尝试使用try/catch,但redshift不支持try/catch模式。

kq0g1dla

kq0g1dla1#

AmazonRedshift文档有一个很好的例子来解释处理这种情况的方法
请参考以下page

CREATE TABLE employee (firstname varchar, lastname varchar); 
INSERT INTO employee VALUES ('Tomas','Smith'); 
CREATE TABLE employee_error_log (message varchar);

CREATE OR REPLACE PROCEDURE update_employee_sp_3() NONATOMIC AS
$$
BEGIN
    BEGIN
        UPDATE employee SET firstname = 'Adam' WHERE lastname = 'Smith';
        EXECUTE 'select invalid1';
    EXCEPTION WHEN OTHERS THEN
        RAISE INFO 'An exception occurred in the first block.';
        INSERT INTO employee_error_log VALUES ('Error message: ' || SQLERRM);
    END;
    BEGIN
        INSERT INTO employee VALUES ('Edie','Robertson');
        EXECUTE 'select invalid2';
    EXCEPTION WHEN OTHERS THEN
        RAISE INFO 'An exception occurred in the second block.';
        INSERT INTO employee_error_log VALUES ('Error message: ' || SQLERRM);
    END;
END;
$$ 
LANGUAGE plpgsql;

CALL update_employee_sp_3();
INFO:  An exception occurred in the first block.
INFO:  An exception occurred in the second block.
CALL

SELECT * from employee;

 firstname | lastname  
-----------+-----------
 Adam      | Smith
 Edie      | Robertson
(2 rows)

SELECT * from employee_error_log;

                     message                     
-------------------------------------------------
 Error message: column "invalid1" does not exist
 Error message: column "invalid2" does not exist
(2 rows)

字符串

相关问题