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

openGauss每日一练第16天 | 学习笔记

原创 手机用户2634 2021-12-30
400

第十六课 openGauss事务控制

学习目标

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

课程学习

连接数据库

#第一次进入等待15秒
#数据库启动中…
su - omm
gsql -r

###学习内容如下
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

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

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

4.清理数据

show transaction_isolation; START TRANSACTION; show transaction_isolation; SET local TRANSACTION ISOLATION LEVEL repeatable read; show transaction_isolation; rollback; START TRANSACTION ISOLATION LEVEL read committed READ WRITE; CREATE TABLE t1(a int); show transaction_read_only; SET LOCAL TRANSACTION READ ONLY; show transaction_read_only; insert into t1 values(1); rollback; CREATE TABLE t2(a int); START TRANSACTION; INSERT INTO t2 VALUES (1); SAVEPOINT my_savepoint1; INSERT INTO t2 VALUES (2); ROLLBACK TO SAVEPOINT my_savepoint1; RELEASE SAVEPOINT my_savepoint1; INSERT INTO t2 VALUES (3); COMMIT; SELECT * FROM t2; drop table t1; drop table t2;

课后作业记录

omm=# omm=# omm=# START TRANSACTION; START TRANSACTION omm=# select * from pg_class limit 1; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | reltoastidxid | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relhasindex | relisshared | relpersistence | relkind | relnatts | re lchecks | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassubclass | relcmprs | relhas clusterkey | relrowmovement | parttype | 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; COMMIT omm=# 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 | relpages | reltuples | relallvisible | reltoastrelid | reltoastidxid | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relhasindex | relisshared | relpersistence | relkind | relnatts | re lchecks | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassubclass | relcmprs | relhas clusterkey | relrowmovement | parttype | 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=# create schema tpcds10; ERROR: cannot execute CREATE SCHEMA in a read-only transaction omm=# commit; ROLLBACK omm=# omm=# omm=# 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; relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | reltoastidxid | reldeltarelid | reldeltaidx | relcudescrelid | relcudescidx | relhasindex | relisshared | relpersistence | relkind | relnatts | re lchecks | relhasoids | relhaspkey | relhasrules | relhastriggers | relhassubclass | relcmprs | relhas clusterkey | relrowmovement | parttype | relfrozenxid | relacl | reloptions | relreplident | relfrozenxid64 | relbucket | relbucketkey --------------+--------------+---------+-----------+----------+-------+-------------+---------------+ ----------+-----------+---------------+---------------+---------------+---------------+-------------+ ----------------+--------------+-------------+-------------+----------------+---------+----------+--- --------+------------+------------+-------------+----------------+----------------+----------+------- -----------+----------------+----------+--------------+-------------------+------------+------------- -+----------------+-----------+-------------- pg_statistic | 11 | 11334 | 0 | 10 | 0 | 13689 | 0 | | f | n | 0 | {omm=arwdDxt/omm} | | n | 7891 | | (1 row) 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; CREATE SCHEMA omm=# rollback; ROLLBACK omm=# omm=# \dn+ tpcds10; List of schemas Name | Owner | Access privileges | Description ------+-------+-------------------+------------- (0 rows) omm=# omm=# CREATE TABLE table1(a int); CREATE TABLE omm=# START TRANSACTION; START TRANSACTION omm=# INSERT INTO table1 VALUES (1); INSERT 0 1 omm=# omm=# SAVEPOINT my_savepoint; SAVEPOINT omm=# INSERT INTO table1 VALUES (2); INSERT 0 1 omm=# omm=# ROLLBACK TO SAVEPOINT my_savepoint; ROLLBACK omm=# omm=# select * from table1; a --- 1 (1 row) omm=# RELEASE SAVEPOINT my_savepoint; omm=# INSERT INTO table1 VALUES (3); RELEASE INSERT 0 1 omm=# COMMIT; COMMIT omm=# omm=# SELECT * FROM table1; a --- 1 3 (2 rows) omm=# omm=# omm=# show transaction_isolation; transaction_isolation ----------------------- read committed (1 row) omm=# START TRANSACTION; START TRANSACTION omm=# omm=# show transaction_isolation; transaction_isolation ----------------------- read committed (1 row) omm=# SET local TRANSACTION ISOLATION LEVEL repeatable read; SET omm=# show transaction_isolation; (1 row) omm=# transaction_isolation ----------------------- repeatable read omm=# rollback; ROLLBACK omm=# omm=# omm=# omm=# omm=# show transaction_isolation; transaction_isolation ----------------------- read committed (1 row) omm=# START TRANSACTION ISOLATION READ WRITE; ERROR: syntax error at or near "READ" LINE 1: START TRANSACTION ISOLATION READ WRITE; ^ omm=# CREATE TABLE t1(a int); CREATE TABLE omm=# omm=# dtop table t1; ERROR: syntax error at or near "dtop" LINE 1: dtop table t1; ^ omm=# omm=# drop table t1; DROP TABLE omm=# omm=# omm=# omm=# omm=# START TRANSACTION ISOLATION LEVEL read committed READ WRITE; START TRANSACTION omm=# CREATE TABLE t1(a int); CREATE TABLE omm=# omm=# show transaction_read_only; transaction_read_only ----------------------- off (1 row) omm=# SET LOCAL TRANSACTION READ ONLY; SET omm=# omm=# show transaction_read_only; transaction_read_only ----------------------- on (1 row) omm=# insert into t1 values(1); ERROR: cannot execute INSERT in a read-only transaction omm=# omm=# rollback; ROLLBACK omm=# omm=# omm=# omm=# CREATE TABLE t2(a int); omm=# CREATE TABLE omm=# START TRANSACTION; START TRANSACTION omm=# INSERT INTO t2 VALUES (1); INSERT 0 1 omm=# omm=# omm=# omm=# SAVEPOINT my_savepoint1; SAVEPOINT omm=# INSERT INTO t2 VALUES (2); omm=# INSERT 0 1 omm=# omm=# ROLLBACK TO SAVEPOINT my_savepoint1; ROLLBACK omm=# omm=# omm=# select * from t2; a --- 1 (1 row) omm=# omm=# RELEASE SAVEPOINT my_savepoint1; RELEASE omm=# INSERT INTO t2 VALUES (3); COMMIT; INSERT 0 1 omm=# COMMIT omm=# omm=# SELECT * FROM t2; a --- 1 3 (2 rows) omm=# omm=# omm=# drop table t1; drop table t2; ERROR: table "t1" does not exist omm=# DROP TABLE omm=# omm=# omm=# \d List of relations Schema | Name | Type | Owner | Storage --------+--------+-------+-------+---------------------------------- public | item | table | omm | {orientation=row,compression=no} public | table1 | table | omm | {orientation=row,compression=no} (2 rows) omm=# omm=#
最后修改时间:2021-12-30 21:42:43
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

目录
  • 第十六课 openGauss事务控制
    • 学习目标
    • 课程学习
      • 连接数据库
    • 课程作业
    • 课后作业记录