暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第16天 openGauss事务控制

原创 Sally 2021-12-21
355

openGauss每日一练第16天 openGauss事务控制

1.事务控制
–通过START TRANSACTION和BEGIN语法启动事务
-以默认方式启动事务

START TRANSACTION;
select * from pg_class limit 1;

END;
–开启一个事务,设置事务的隔离级别为READ COMMITTED,访问模式为READ ONLY

BEGIN;
SET LOCAL TRANSACTION ISOLATION LEVEL READ COMMITTED READ ONLY;
show transaction_read_only;
select * from pg_class limit 1;
create schema tpcds10;
commit;

–以隔离级别为repeatable read,读/写方式启动事务

show transaction_isolation;
START TRANSACTION ISOLATION LEVEL repeatable read READ WRITE;
show transaction_isolation;
show transaction_read_only;
select * from pg_class limit 1;
create schema tpcds10;
rollback;

–事务回滚,schema没有创建成功

\dn+ tpcds10;
2.savepoint
–保存点是事务中的一个特殊记号,它允许将那些在它建立后执行的命令全部回滚,把事务的状态恢复到保存点所在的时刻

CREATE TABLE table1(a int);
START TRANSACTION;
INSERT INTO table1 VALUES (1);
–建立保存点

SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (2);
–回滚保存点

ROLLBACK TO SAVEPOINT my_savepoint;
–删除保存点

RELEASE SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (3);
COMMIT;
–查询表的内容,会同时看到1和3,不能看到2,因为2被回滚

SELECT * FROM table1;
课后作业
1.以默认方式启动事务1,修改事务隔离级别,查看transaction_isolation

omm=# start transaction;
START TRANSACTION
omm=# set local transaction isolation level repeatable read read write;
SET
omm=# show transaction_isolation;
(1 row)

omm=#  transaction_isolation 
-----------------------
 repeatable read

omm=# show transaction_read_only;
 transaction_read_only 
-----------------------
 off
(1 row)

omm=# end;
COMMIT

2.以读写方式启动事务2,创建新表,修改事务为只读事务,查看transaction_read_only,并向表中插入记录

omm=# start transaction isolation level read committed read write;
omm=# START TRANSACTION

omm=# create table product(product_id int,product_name char(30));
CREATE TABLE
omm=# 
omm=# set local transaction isolation level read committed read only;
SET
omm=# insert into product values(1,'phone');
ERROR:  cannot execute INSERT in a read-only transaction
omm=# end;
ROLLBACK

3.启动事务3,对表进行增删改查,并用到创建savepoint,回滚savepoint和删除savepoint

omm=# create table product(product_id int,product_name char(30));
omm=# CREATE TABLE

omm=# start transaction;
START TRANSACTION
omm=# show transaction_isolation;
 transaction_isolation 
-----------------------
 read committed
(1 row)

omm=# show transaction_read_only;
(1 row)

omm=#  transaction_read_only 
-----------------------
 off

omm=# insert into product values(1,'computer');
INSERT 0 1
omm=# select * from product;
 product_id |          product_name          
------------+--------------------------------
          1 | computer                      
(1 row)

omm=# update product set product_name='phone' where product_id = 1;
UPDATE 1
omm=# select * from product;
 product_id |          product_name          
------------+--------------------------------
          1 | phone                         
(1 row)

omm=# savepoint my_sp1;
omm=# SAVEPOINT

omm=# insert into product values(2,'light');
INSERT 0 1
omm=# select * from product;
 product_id |          product_name          
------------+--------------------------------
          1 | phone                         
          2 | light                         
(2 rows)

omm=# rollback to savepoint my_sp1;
ROLLBACK
omm=# select * from product;
(1 row)

omm=#  product_id |          product_name          
------------+--------------------------------
          1 | phone                         

omm=# release savepoint my_sp1;
omm=# RELEASE

omm=# insert into product values(3,'mouse');
INSERT 0 1
omm=# commit;
COMMIT
omm=# select * from product;
 product_id |          product_name          
------------+--------------------------------
          1 | phone                         
          3 | mouse                         
(2 rows)

4.清理数据

omm=# drop table product;
DROP TABLE

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论