简体   繁体   English

如何控制 function 中的事务

[英]How to control transaction in the function

I am calling the test functions through wrappertest.psql file.我通过 wrappertest.psql 文件调用测试函数。 wrappertest.psql包装测试.psql

select paymentfailedtest();       
select paymentsuccesstest();

Test functions are placed in the testfunctions.psql file.测试函数放在 testfunctions.psql 文件中。

In Postgresql, Functions does not allow to rollback/commit transaction.在 Postgresql 中,函数不允许回滚/提交事务。 You need to use stored procedure avail the functionality of rollback & commit.您需要使用存储过程来利用回滚和提交的功能。

CREATE PROCEDURE transaction_test1()
LANGUAGE plpgsql
AS $$
BEGIN
    FOR i IN 0..9 LOOP
        INSERT INTO test1 (a) VALUES (i);
        IF i % 2 = 0 THEN
            COMMIT;
        ELSE
            ROLLBACK;
        END IF;
    END LOOP;
END
$$;

CALL transaction_test1();

refer https://www.postgresql.org/docs/11/plpgsql-transactions.html for more info.有关更多信息,请参阅https://www.postgresql.org/docs/11/plpgsql-transactions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM