首页 > > > MySQL与PostgreSQL在事务隔离上的差异
前言:主流的开源数据源产品如MySQL、PostgreSQL虽然都各自实现了Repeatable Read、Serializable 事务隔离级别,不过他们在概念上或者实现方式上还是存在一定差异,本文旨在通过实际测验来对比两块数据库产品在实现上的差异。
在《A Critique of ANSI SQL Isolation Levels》论文中描述了事务不同隔离级别下异常现象,总结出的结论如图:

这里主要对比P3、P4、A5B现象表现上存在差异
论文中对A5B、P3、P4的定义如下:
A5B Write Skew Suppose T1 reads x and y, which are consistent with C(), and then a T2 reads x and y, writes x, and commits. Then T1 writes y. If there were a constraint between x and y, it might be violated. In terms of histories: A5B: r1[x]...r2[y]...w1[y]...w2[x]...(c1 and c2 occur) (Write Skew)
P3: SQL-transaction T1 reads the set of rows N that satisfy some <search condition>. SQL-transaction T2 then executes SQL-statements that generate one or more rows that satisfy the <search condition> used by SQL-transaction T1. If SQL-transaction T1 then repeats the initial read with the same <search condition>, it obtains a different collection of rows.
r1[P]...w2[y in P]...((c1 or a1) and (c2 or a2) any order)
P4 (Lost Update): The lost update anomaly occurs when transaction T1 reads a data item and then T2 updates the data item (possibly based on a previous read), then T1 (based on its earlier read value) updates the data item and commits. In terms of histories, this is:
P4: r1[x]...w2[x]...w1[x]...c1 (Lost Update)
下面分别测试MySQL、PostgreSQL 在Repeatable Read 、Serializable 隔离级别下A5B、P3现象的差异
MySQL版本5.7

MySQL Repeatable Read 下能够避免A5B

MySQL Repeatable Read 下不能避免P3

MySQL Repeatable Read 下不能避免P4
PostgreSQL版本10

PostgreSQL Repeatable Read 下不能够避免A5B

PostgreSQL Repeatable Read 下能避免P3

PostgreSQL Repeatable Read 下能避免P4
MySQL版本5.7

MySQL Serializable 下能避免P3
PostgreSQL版本10

PostgreSQL Serializable下能够避免A5B
参考:
A Critique of ANSI SQL Isolation Levels13.2. Transaction IsolationSerializable Snapshot Isolation in PostgreSQL来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3122939/ ,如需转载,请注明出处,否则将追究法律责任。