MySQL 和 PostgreSQL 中可重复读取语义的差异
Difference in Repeatable Read Semantics in MySQL and PostgreSQL
我了解在MySQL
和PostgreSQL
中,REPEATABLE READ
隔离级别将使读取在事务开始时看到快照。但是在 https://dev.mysql.com/doc/refman/8.0/en/innodb-consistent-read.html 的 MySQL 文档中
下面的注释是用一个例子提到的
The snapshot of the database state applies to SELECT statements within
a transaction, not necessarily to DML statements. If you insert or
modify some rows and then commit that transaction, a DELETE or UPDATE
statement issued from another concurrent REPEATABLE READ transaction
could affect those just-committed rows, even though the session could
not query them. If a transaction does update or delete rows committed
by a different transaction, those changes do become visible to the
current transaction. For example, you might encounter a situation like
the following:
SELECT COUNT(c1) FROM t1 WHERE c1 = 'xyz';
-- Returns 0: no rows match.
DELETE FROM t1 WHERE c1 = 'xyz';
-- Deletes several rows recently committed by other transaction.
SELECT COUNT(c2) FROM t1 WHERE c2 = 'abc';
-- Returns 0: no rows match.
UPDATE t1 SET c2 = 'cba' WHERE c2 = 'abc';
-- Affects 10 rows: another txn just committed 10 rows with 'abc' values.
SELECT COUNT(c2) FROM t1 WHERE c2 = 'cba';
-- Returns 10: this txn can now see the rows it just updated.
同样的例子是否适用于 PostgreSQL,或者它不允许这样的行为?
这在 PostgreSQL 中不会发生。
如果REPEATABLE READ
事务A试图修改已被并发事务B修改的行在A的快照被拍摄之后,A将收到“序列化错误”。
我了解在MySQL
和PostgreSQL
中,REPEATABLE READ
隔离级别将使读取在事务开始时看到快照。但是在 https://dev.mysql.com/doc/refman/8.0/en/innodb-consistent-read.html 的 MySQL 文档中
下面的注释是用一个例子提到的
The snapshot of the database state applies to SELECT statements within a transaction, not necessarily to DML statements. If you insert or modify some rows and then commit that transaction, a DELETE or UPDATE statement issued from another concurrent REPEATABLE READ transaction could affect those just-committed rows, even though the session could not query them. If a transaction does update or delete rows committed by a different transaction, those changes do become visible to the current transaction. For example, you might encounter a situation like the following:
SELECT COUNT(c1) FROM t1 WHERE c1 = 'xyz';
-- Returns 0: no rows match.
DELETE FROM t1 WHERE c1 = 'xyz';
-- Deletes several rows recently committed by other transaction.
SELECT COUNT(c2) FROM t1 WHERE c2 = 'abc';
-- Returns 0: no rows match.
UPDATE t1 SET c2 = 'cba' WHERE c2 = 'abc';
-- Affects 10 rows: another txn just committed 10 rows with 'abc' values.
SELECT COUNT(c2) FROM t1 WHERE c2 = 'cba';
-- Returns 10: this txn can now see the rows it just updated.
同样的例子是否适用于 PostgreSQL,或者它不允许这样的行为?
这在 PostgreSQL 中不会发生。
如果REPEATABLE READ
事务A试图修改已被并发事务B修改的行在A的快照被拍摄之后,A将收到“序列化错误”。