postgres=# create table t1(id int); CREATE TABLE postgres=# insert into t1 values(1); INSERT01 postgres=# insert into t1 values(2); INSERT01
postgres=# begin; BEGIN postgres=*# update t1 set id =99where id =1; ---第①步 UPDATE1 postgres=*# update t1 set id =100where id =2; ---第③步 UPDATE1
第二个会话
1 2 3 4 5 6 7 8 9 10 11 12
postgres=# begin; BEGIN postgres=*# update t1 set id =100where id =2; ---第②步 UPDATE1 postgres=*# update t1 set id =99where id =1; ---第④步 ERROR: deadlock detected DETAIL: Process 27023 waits for ShareLock on transaction 498461; blocked by process 26975. Process 26975 waits for ShareLock on transaction 498462; blocked by process 27023. HINT: See server log for query details. CONTEXT: while updating tuple (0,1) in relation "t1" postgres=!# commit; ---第⑤步 ROLLBACK
postgres=*# select*from options; ERROR: deadlock detected LINE 1: select*from options; ^ DETAIL: Process 27350 waits for AccessShareLock on relation 33622of database 5; blocked by process 27346. Process 27346 waits for AccessExclusiveLock on relation 33619of database 5; blocked by process 27350. HINT: See server log for query details.
postgres=# set enable_seqscan to off; SET postgres=# explain update t1 set salary = slow(salary) where salary >=100.00; QUERY PLAN ------------------------------------------------------------------------------------ Updateon t1 (cost=0.15..505.33rows=0 width=0) -> Index Scan using t1_salary_idx on t1 (cost=0.15..505.33rows=1810 width=38) Index Cond: (salary >=100.00) (3rows)
postgres=# update t1 set salary = slow(salary) where salary >=100.00; ERROR: deadlock detected DETAIL: Process 6070 waits for ShareLock on transaction 498525; blocked by process 6120. Process 6120 waits for ShareLock on transaction 498524; blocked by process 6070. HINT: See server log for query details. CONTEXT: while updating tuple (0,3) in relation "t1"