PostgreSQL - 在执行过程中发生的异常是否会执行先前操作的回滚?

PostgreSQL - Does the occurrence of an exception during the execution of a procedure perform a rollback of previous operations?

提问人:nietoperz21 提问时间:11/19/2022 最后编辑:nietoperz21 更新时间:11/19/2022 访问量:25

问:

当调用过程时发生异常时,其中以前的操作是否会在 PostgreSQL 中回滚?在MySQL中,为了进行ROLLBACK,我必须为SQLEXCEPTION添加一个处理程序。例如:

DELIMITER //
CREATE PROCEDURE insert_address (IN region VARCHAR(25), city VARCHAR(50), street VARCHAR(50), OUT last_id INT)
    BEGIN
        DECLARE region_id INT;
        DECLARE city_id INT;
        DECLARE errno INT;
        
        DECLARE EXIT HANDLER FOR SQLEXCEPTION 
        BEGIN
            SHOW ERRORS;
            ROLLBACK;
        END;
        
        START TRANSACTION;
        
        SET region_id = (SELECT r.id FROM region AS r WHERE r.name = region LIMIT 1);
        
        IF NOT EXISTS (SELECT c.id FROM city AS c WHERE c.name = city LIMIT 1) THEN
            CALL insert_city(city, city_id);
        ELSE
            SET city_id = (SELECT c.id FROM city AS c LIMIT 1);
        END IF;
    
        INSERT INTO address(region_id, city_id, street)
        VALUES(region_id, city_id, street);
        
        SET last_id = LAST_INSERT_ID();
        
        COMMIT;
    END

我想知道这在 PostgreSQL 中是如何工作的,它是自动发生的还是我还必须添加一些命令。

sql postgresql 异常 事务 plpgsql

评论

0赞 rveerd 11/19/2022
这回答了你的问题吗?PostgreSQL 函数是事务性的吗?

答: 暂无答案