postgresql模拟死锁

版权声明:本文为博主原创文章,但知识无界。欢迎关注本人公众号&小程序:馨成 https://blog.csdn.net/haohaizijhz/article/details/85077308

今天教你模拟postgresql实现死锁。

1准备

create table t1(id int primary key,info character varying(20));
insert into dss_pmart.t1 values(22,'hello');
insert into dss_pmart.t1 values(23,'world');

2模拟死锁

session1,在命令窗口A,输入以下信息:

--命令窗口A
begin;
select * from t1 where id =23 for update;

session2,在命令窗口B,输入以下信息:

--命令窗口B
begin;
select * from t1 where id in (22,23) for update;

session1,回到命令窗口A,继续输入以下信息:

--命令窗口A
select * from t1 where id =22 for update;

3模拟结果

ERROR: deadlock detected
SQL 状态: 40P01
详细:Process 3202 waits for ShareLock on transaction 19236551; blocked by process 3559.
Process 3559 waits for ShareLock on transaction 19236550; blocked by process 3202.
指导建议:See server log for query details.

猜你喜欢

转载自blog.csdn.net/haohaizijhz/article/details/85077308