[英]PostgreSQL transaction level guarantees for foreign key consistency in read only transactions
Application A (think exporter) needs to read all rows of all tables from a running PostgreSQL database.应用程序A (想想导出器)需要从正在运行的 PostgreSQL 数据库中读取所有表的所有行。 Meanwhile Application B (think web application) continues to do reads and writes.
同时应用程序B (想想 web 应用程序)继续进行读写。
Table child
has an optional foreign key to parent
.表
child
对parent
有一个可选的外键。
I've had trouble with the following access pattern:我在使用以下访问模式时遇到了问题:
BEGIN TRANSACTION
BEGIN TRANSACTION
SELECT * FROM parent
SELECT * FROM parent
BEGIN TRANSACTION
BEGIN TRANSACTION
INSERT INTO parent
INSERT INTO parent
级INSERT INTO child -- has foreign key to inserted parent
INSERT INTO child -- has foreign key to inserted parent
COMMIT
COMMIT
SELECT * FROM child -- I do not want to receive the inserted child here
SELECT * FROM child -- I do not want to receive the inserted child here
Application A breaks because it reads a child
for which it could not read the parent
.应用程序A中断,因为它读取了一个它无法读取
parent
级的child
级。 Therefore I do not want that A reads the child
row inserted by B .因此,我不希望A读取B插入的
child
行。
As far as I understand REPEATABLE_READ
does not give me any guarantees here, since I did not already read the child
table in this transaction.据我了解,
REPEATABLE_READ
在这里没有给我任何保证,因为我还没有在这个事务中读取child
表。 As far as I understand this is not considered a phantom read either for the same reason.据我了解,出于同样的原因,这也不被视为幻读。
SERIALIZABLE
guarantee that A does not read the new child
row? SERIALIZABLE
是否保证A不读取新的child
行?child
rows with invalid references to parent
?parent
无效引用的child
行?Start transaction A with开始交易A
START TRANSACTION READ ONLY ISOLATION LEVEL REPEATABLE READ;
Then all statements in that transaction will see the same state (snapshot) of the database, no matter what was modified by concurrent transactions.然后,该事务中的所有语句都将看到数据库的相同 state(快照),无论并发事务修改了什么。
I added the READ ONLY
only because you said that A was, it is not necessary for it to work.我添加
READ ONLY
只是因为您说 A 是,它没有必要工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.