两个并发语句写入数据库

时间:2017-12-21 17:19:57

标签: postgresql transactions

+---+---------+-----------+
|id |  title  |description| 
+---+---------------------+
| 1 | The King|  Jonh X   |
+---+---------------------+

两个并发语句:

update book set title = 'aaa', description = 'aaa' where id = 1

update book set title = 'bbb', description = 'bbb' where id = 1

理论上可能会产生以下结果吗?

+---+---------+-----------+
|id |  title  |description| 
+---+---------------------+
| 1 |   aaa   |   bbbb    |
+---+---------------------+
update book set title = 'aaa', description = 'aaa' where id = 1

select title, description from book -> (The King, aaa)?

这些陈述未包含在交易中

如SQL Server,Postgres等流行的数据库系统呢?

1 个答案:

答案 0 :(得分:1)

任何 ACID 兼容数据库中的

通常不可能
ACID代表 原子性,一致性,隔离性,持久性

特别是,Postgres在UPDATE之前对受影响的行进行写锁定,并且直到事务结束才释放它。 (每个 UPDATE在事务内部,隐式或显式地运行。)尝试写入同一行的并发事务必须等待并在释放锁后重新评估过滤器。然后,他们可能会再次更改行 - 如果过滤器不再适用,则可能会变空。