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

openGauss每日一练第16天 | 学习openGauss事务控制-学习笔记

原创 cxb 2021-12-20
426

学习目标
学习openGauss事务控制
事务是用户定义的一个数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位

课程学习
连接数据库

root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=#

1.事务控制
–通过START TRANSACTION和BEGIN语法启动事务
-以默认方式启动事务
omm=# START TRANSACTION;
START TRANSACTION
omm=# select * from pg_class limit 1;
relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoa
strelid | reltoastidxid | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relhasindex | relisshared | relpersistence | relkind | reln
atts | relchecks | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassubclass | relcmprs | relhasclusterkey | relrowmovement | parttyp
e | relfrozenxid | relacl | reloptions | relreplident | relfrozenxid64 | relbucket | relbucketkey
--------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+-------
--+--------------+-------------------+------------+--------------+----------------+-----------+--------------
pg_statistic | 11 | 11334 | 0 | 10 | 0 | 13689 | 0 | 18 | 476 | 18 |
2840 | 0 | 0 | 0 | 0 | 0 | t | f | p | r |
--------+---------------+---------------+-------------+----------------+--------------+-------------+-------------+----------------+---------+-----
-----+-----------+------------+------------+-------------+----------------+----------------+----------+------------------+----------------+--------
29 | 0 | f | f | f | f | f | 0 | f | f | n

| 0 | {omm=arwdDxt/omm} | | n | 7891 | |
(1 row)
omm=# END;
omm=# COMMIT

–开启一个事务,设置事务的隔离级别为READ COMMITTED,访问模式为READ ONLY
omm=# BEGIN;
BEGIN
omm=# SET LOCAL TRANSACTION ISOLATION LEVEL READ COMMITTED READ ONLY;
SET
omm=# show transaction_read_only;
transaction_read_only
-----------------------
on
(1 row)

omm=# select * from pg_class limit 1;
relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | r
elpages | reltuples | relallvisible | reltoastrelid | reltoastidxid | reldeltarelid | reldeltaidx | rel
cudescrelid | relcudescidx | relhasindex | relisshared | relpersistence | relkind | relnatts | relcheck
s | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassubclass | relcmprs | relhasclusterk
ey | relrowmovement | parttype | relfrozenxid | relacl | reloptions | relreplident | relfroz
enxid64 | relbucket | relbucketkey
--------------+--------------+---------+-----------+----------+-------+-------------+---------------+--
--------+-----------+---------------+---------------+---------------+---------------+-------------+----
------------+--------------+-------------+-------------+----------------+---------+----------+---------
--+------------+------------+-------------+----------------+----------------+----------+---------------
---+----------------+----------+--------------+-------------------+------------+--------------+--------
--------+-----------+--------------
| f | n | 0 | {omm=arwdDxt/omm} | | n |
7891 | |
(1 row)

pg_statistic | 11 | 11334 | 0 | 10 | 0 | 13689 | 0 |
18 | 476 | 18 | 2840 | 0 | 0 | 0 |
0 | 0 | t | f | p | r | 29 |
0 | f | f | f | f | f | 0 | f
omm=# create schema tpcds10;
ERROR: cannot execute CREATE SCHEMA in a read-only transaction
omm=# commit;
ROLLBACK
–以隔离级别为repeatable read,读/写方式启动事务
omm=# show transaction_isolation;
transaction_isolation
-----------------------
read committed
(1 row)

omm=# START TRANSACTION ISOLATION LEVEL repeatable read READ WRITE;
START TRANSACTION

omm=# show transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)
omm=# show transaction_read_only;
transaction_read_only
-----------------------
off
(1 row)
omm=# select * from pg_class limit 1;
omm=# create schema tpcds10;
CREATE SCHEMA
omm=# rollback;
ROLLBACK
–事务回滚,schema没有创建成功
omm=# \dn+ tpcds10;
List of schemas
Name | Owner | Access privileges | Description
------+-------+-------------------+-------------
(0 rows)
2.savepoint
–保存点是事务中的一个特殊记号,它允许将那些在它建立后执行的命令全部回滚,把事务的状态恢复到保存点所在的时刻
omm=# CREATE TABLE table1(a int);
CREATE TABLE
omm=# START TRANSACTION;
START TRANSACTION
omm=# INSERT INTO table1 VALUES (1);
INSERT 0 1
–建立保存点
omm=# SAVEPOINT my_savepoint;
SAVEPOINT
omm=# INSERT INTO table1 VALUES (2);
INSERT 0 1
–回滚保存点
omm=# ROLLBACK TO SAVEPOINT my_savepoint;
ROLLBACK
–删除保存点
omm=# ROLLBACK TO SAVEPOINT my_savepoint;
ROLLBACK
omm=# RELEASE SAVEPOINT my_savepoint;
RELEASE
omm=# INSERT INTO table1 VALUES (3);
INSERT 0 1
omm=# COMMIT;
COMMIT
–查询表的内容,会同时看到1和3,不能看到2,因为2被回滚
omm=# SELECT * FROM table1;
a
---
1
3
(2 rows)

课程作业
1.以默认方式启动事务1,修改事务隔离级别,查看transaction_isolation
START TRANSACTION;
SET LOCAL TRANSACTION ISOLATION LEVEL repeatable read READ WRITE;
show transaction_isolation;
end;

omm=# START TRANSACTION;
START TRANSACTION
omm=# SET LOCAL TRANSACTION ISOLATION LEVEL repeatable read READ WRITE;
SET
omm=# show transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)

omm=# end;
COMMIT

2.以读写方式启动事务2,创建新表,修改事务为只读事务,查看transaction_read_only,并向表中插入记录
START TRANSACTION ISOLATION LEVEL repeatable read READ WRITE;
CREATE TABLE table1(a int);
show transaction_isolation;
commit;
begin;
SET LOCAL TRANSACTION ISOLATION LEVEL READ COMMITTED READ ONLY;
show transaction_read_only;
INSERT INTO table1 VALUES (1);
commit;

omm=# START TRANSACTION ISOLATION LEVEL repeatable read READ WRITE;
omm=# START TRANSACTION

omm=#
omm=# CREATE TABLE table1(a int);
CREATE TABLE

omm=# show transaction_isolation;

(1 row)

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

omm=# commit;

omm=# begin;
BEGIN
omm=# SET LOCAL TRANSACTION ISOLATION LEVEL READ COMMITTED READ ONLY;
SET
omm=# show transaction_read_only;
transaction_read_only
-----------------------
on
(1 row)

omm=# INSERT INTO table1 VALUES (1);
ERROR: cannot execute INSERT in a read-only transaction
omm=# commit;
ROLLBACK


3.启动事务3,对表进行增删改查,并用到创建savepoint,回滚savepoint和删除savepoint
BEGIN;
INSERT INTO table1 VALUES (2);
SAVEPOINT my_savepoint;
INSERT INTO table1 VALUES (3);
delete from table1 where a=2;
update table1 set a=6 where a=3;
ROLLBACK TO SAVEPOINT my_savepoint;
RELEASE SAVEPOINT my_savepoint;
commit;
select * from table1;
omm=# BEGIN;
BEGIN
omm=# INSERT INTO table1 VALUES (2);
INSERT 0 1
omm=# SAVEPOINT my_savepoint;
SAVEPOINT
omm=# INSERT INTO table1 VALUES (3);
INSERT 0 1
omm=# delete from table1 where a=2;
DELETE 1
omm=# update table1 set a=6 where a=3;
UPDATE 1
omm=# ROLLBACK TO SAVEPOINT my_savepoint;
ROLLBACK
omm=# RELEASE SAVEPOINT my_savepoint;
RELEASE
omm=# commit;
COMMIT
omm=# select * from table1;
omm=# a
---
2
(1 row)


4.清理数据
drop table table1;
omm=# drop table table1;
DROP TABLE

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

评论