Nested transactions in postgresql 8.2?

后端 未结 2 601
执念已碎
执念已碎 2021-01-04 12:09

I\'m working on scripts that apply database schema updates. I\'ve setup all my SQL update scripts using start transaction/commit. I pass these scripts to psql on the command

2条回答
  •  半阙折子戏
    2021-01-04 12:41

    Well you have the possibility to use nested transactions inside postgresql using SavePoints.

    Take this code example:

    CREATE TABLE t1 (a integer PRIMARY KEY);
    
    CREATE FUNCTION test_exception() RETURNS boolean LANGUAGE plpgsql AS
    $$BEGIN
       INSERT INTO t1 (a) VALUES (1);
       INSERT INTO t1 (a) VALUES (2);
       INSERT INTO t1 (a) VALUES (1);
       INSERT INTO t1 (a) VALUES (3);
       RETURN TRUE;
    EXCEPTION
       WHEN integrity_constraint_violation THEN
          RAISE NOTICE 'Rollback to savepoint';
          RETURN FALSE;
    END;$$;
    
    BEGIN;
    
    SELECT test_exception();
    NOTICE:  Rollback to savepoint
     test_exception 
    ----------------
     f
    (1 row)
    
    COMMIT;
    
    SELECT count(*) FROM t1;
     count 
    -------
         0
    (1 row)
    

    Maybe this will help you out a little bit.

提交回复
热议问题