Nested transactions in postgresql 8.2?

后端 未结 2 580
执念已碎
执念已碎 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.

    0 讨论(0)
  • 2021-01-04 12:43

    I've ended up 'solving' my problem out of band - I use a perl script to re-work the input scripts to eliminate their start transaction/commit calls, then push them all into one file, which gets it's own start transaction/commit.

    0 讨论(0)
提交回复
热议问题