zoukankan      html  css  js  c++  java
  • PostgreSQL事务特性之嵌套事务

    嵌套事务的实现是基于SAVEPOINTROLLBACK TO SAVEPOINTRELEASE SAVEPOINT的,也就是设置一个保存点,可以回滚到保存点和释放保存点。

    测试表的初始状态如下:

    postgres=# d test 
         Table "public.test"
     Column |  Type   | Modifiers 
    --------+---------+-----------
     id     | integer | 
     name   | text    | 
    
    postgres=# select * from test ;
     id | name 
    ----+------
    (0 rows)

    开始测试

    postgres=# begin ;
    BEGIN
    postgres=# insert into test values (1, 'a');
    INSERT 0 1
    postgres=# savepoint insert_a;
    SAVEPOINT
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
    (1 row)
    
    postgres=# insert into test values (2, 'b');
    INSERT 0 1
    postgres=# savepoint insert_b;
    SAVEPOINT
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# insert into test values (3, 'c');
    INSERT 0 1
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
      3 | c
    (3 rows)

    现在定义了两个SAVEPOINT,并且插入了3条数据,现在测试ROLLBACK TO SAVEPOINT

    postgres=# rollback to insert_b;
    ROLLBACK
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# rollback to insert_a;
    ROLLBACK
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
    (1 row)

    可见回滚到前面定义的保存点成功了。

    如果回滚到前面的保存点,后面的更改就丢失了,包括保存点,比如回滚到insert_a,那么在insert_a之后的数据就没有了,insert_b这个保存点也不存在了。

    postgres=# rollback to insert_a;
    ROLLBACK
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
    (1 row)
    
    postgres=# rollback to insert_b;
    ERROR:  no such savepoint

    测试RELEASE SAVEPOINT

    postgres=# select * from test ;
     id | name 
    ----+------
    (0 rows)
    
    postgres=# begin ;             
    BEGIN
    postgres=# insert into test values (1, 'a');
    INSERT 0 1
    postgres=# savepoint insert_a;              
    SAVEPOINT
    postgres=# select * from test ; 
     id | name 
    ----+------
      1 | a
    (1 row)
    
    postgres=# insert into test values (2, 'b');
    INSERT 0 1
    postgres=# savepoint insert_b;              
    SAVEPOINT
    postgres=# select * from test ;             
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# release insert_a;
    RELEASE
    postgres=# select * from test ;
     id | name 
    ----+------
      1 | a
      2 | b
    (2 rows)
    
    postgres=# rollback to insert_a;
    ERROR:  no such savepoint

    保存点被释放后就不能再回滚到该保存点了。

  • 相关阅读:
    【学相伴】Nginx最新教程通俗易懂-狂神说
    Linux基础知识总结(命令行)
    CentOS7 运维
    Linux 的基础知识回顾(安装vmware) ---- No.1 后面都以Centos8 为例
    Linux sudo权限提升漏洞(CVE-2021-3156)
    Flutter开发指南之理论篇:Dart语法05(单线程模型,事件循环模型,Isolate)
    矩阵的范数
    函数导出在kvm_intel.ko,kvm.ko不共享
    python 调用内部类的两种方法
    python3 字符串方法
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/11242016.html
Copyright © 2011-2022 走看看