PostreSQL 中的 INSERT 和事务序列化
我有一个问题。事务隔离级别设置为可序列化。当一个用户打开事务并在“table1”中插入或更新数据,然后另一个用户打开事务并尝试将数据插入到同一个表中时,第二个用户是否需要等待第一个用户提交事务?
I have a question. Transaction isolation level is set to serializable. When the one user opens a transaction and INSERTs or UPDATEs data in "table1" and then another user opens a transaction and tries to INSERT data to the same table, does the second user need to wait 'til the first user commits the transaction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,不会。第二个事务只是插入,因此除非需要进行唯一索引检查或其他触发器,否则可以无条件插入数据。在唯一索引(包括主键)的情况下,如果两个事务都更新具有相同值的行,它将阻塞,例如:
在可能影响另一个事务插入的更新的情况下,事情不太明显。据我了解,在这种情况下,PostgreSQL 尚不支持“真正的”可串行性。我不知道其他 SQL 系统对它的支持有多普遍。
请参阅 http://www.postgresql.org/docs/current/interactive/mvcc .html
Generally, no. The second transaction is inserting only, so unless there is a unique index check or other trigger that needs to take place, the data can be inserted unconditionally. In the case of a unique index (including primary key), it will block if both transactions are updating rows with the same value, e.g.:
Things are less obvious in the case of updates that may affect insertions by the other transaction. I understand PostgreSQL does not yet support "true" serialisability in this case. I do not know how commonly supported it is by other SQL systems.
See http://www.postgresql.org/docs/current/interactive/mvcc.html
第二个用户将被阻止,直到第一个用户提交或回滚他/她的更改。
The second user will be blocked until the first user commits or rolls back his/her changes.