目录
修改配置文件postgresql.conf,设置默认的隔离级别:
四. 在事务A中, 修改同一行,造成可重复读的状态变化,查看postgres的处理
一. postgres对于可重复读的隔离级别,如果出现读取行的状态变化,则本事务将会提交失败并回滚
二. mysql的可重复读, 如果出现读取行的状态变化,则本事务将会覆盖先前的事务
摘要:
可重复读的隔离级别控制,在postgres和mysql中有不同的处理。
而可重复读,与读已提交,在隔离级别上, 又必须要具体的明白到底有哪些区别。
本文主要记录postgres的可重复读的处理。
postgres的可重复读说明:
postgres的可重复读的具体操作测试:
一. 开启会话级别的可重复读的隔离级别
修改配置文件postgresql.conf,设置默认的隔离级别:
vim postgresql.conf
default_transaction_isolation = ‘repeatable read’
重新加载配置:
pg_ctl -D /data/pg reload
查看默认的隔离级别:
show default_transaction_isolation;
postgres=# show default_transaction_isolation;
***(Single step mode: verify command)*******************************************
show default_transaction_isolation;
***(press return to proceed or enter x and return to cancel)********************
default_transaction_isolation
-------------------------------
repeatable read
(1 row)
二. 先开启事务A
务必先开启事务A, 然后在事务A开始后, 再开启事务B。
在事务B中执行完commit提交后, 再在事务A中修改同一行。
事务A操作:
test=# begin;
***(Single step mode: verify command)*******************************************
begin;
***(press return to proceed or enter x and return to cancel)********************
BEGIN
test=*# select * from company where id=11;
***(Single step mode: verify command)*******************************************
select * from company where id=11;
***(press return to proceed or enter x and return to cancel)********************
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
11 | Paul | 66 | California | 20000 | 2001-07-13
(1 row)
test=*# select * from company where id=11;
***(Single step mode: verify command)*******************************************
select * from company where id=11;
***(press return to proceed or enter x and return to cancel)********************
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
11 | Paul | 66 | California | 20000 | 2001-07-13
(1 row)
test=*# select * from company where id=11;
***(Single step mode: verify command)*******************************************
select * from company where id=11;
***(press return to proceed or enter x and return to cancel)********************
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
11 | Paul | 66 | California | 20000 | 2001-07-13
(1 row)
三. 开启事务B,修改特定行,并提交
test=# begin;
***(Single step mode: verify command)*******************************************
begin;
***(press return to proceed or enter x and return to cancel)********************
BEGIN
test=*#
test=*# select * from company where id=11;
***(Single step mode: verify command)*******************************************
select * from company where id=11;
***(press return to proceed or enter x and return to cancel)********************
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
11 | Paul | 66 | California | 20000 | 2001-07-13
(1 row)
test=*# update company set age=88 where id=11;
***(Single step mode: verify command)*******************************************
update company set age=88 where id=11;
***(press return to proceed or enter x and return to cancel)********************
UPDATE 1
test=*# select * from company where id=11;
***(Single step mode: verify command)*******************************************
select * from company where id=11;
***(press return to proceed or enter x and return to cancel)********************
id | name | age | address | salary | join_date
----+------+-----+----------------------------------------------------+--------+------------
11 | Paul | 88 | California | 20000 | 2001-07-13
(1 row)
test=*# commit;
***(Single step mode: verify command)*******************************************
commit;
***(press return to proceed or enter x and return to cancel)********************
四. 在事务A中, 修改同一行,造成可重复读的状态变化,查看postgres的处理
test=*# update company set age=99 where id=11;
***(Single step mode: verify command)*******************************************
update company set age=99 where id=11;
***(press return to proceed or enter x and return to cancel)********************
2022-05-24 12:50:53.649 EDT [443682] ERROR: could not serialize access due to concurrent update
2022-05-24 12:50:53.649 EDT [443682] STATEMENT: update company set age=99 where id=11;
ERROR: could not serialize access due to concurrent update