简体   繁体   English

PostgreSQL 事务级别保证只读事务中的外键一致性

[英]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 .childparent有一个可选的外键。

I've had trouble with the following access pattern:我在使用以下访问模式时遇到了问题:

  • A : BEGIN TRANSACTION A : BEGIN TRANSACTION
  • A : SELECT * FROM parent A : SELECT * FROM parent
  • B : BEGIN TRANSACTION B : BEGIN TRANSACTION
  • B : INSERT INTO parent BINSERT INTO parent
  • B : INSERT INTO child -- has foreign key to inserted parent B : INSERT INTO child -- has foreign key to inserted parent
  • B : COMMIT BCOMMIT
  • A : SELECT * FROM child -- I do not want to receive the inserted child here A : 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.据我了解,出于同样的原因,这也不被视为幻读。

  • Does SERIALIZABLE guarantee that A does not read the new child row? SERIALIZABLE是否保证A不读取新的child行?
  • Do I need to resort to application logic in A to discard child rows with invalid references to parent ?我是否需要求助于A中的应用程序逻辑来丢弃对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.

相关问题 PostgreSQL:交易和外键问题 - PostgreSQL : Transaction and foreign key problem Codeigniter Postgresql:具有外键死锁的事务 - Codeigniter Postgresql: Transaction with foreign key deadlock PostgreSQL 外键违规内部交易 - PostgreSQL foreign key violation inside transaction PostgreSQL 行级安全涉及其他表的外键 - PostgreSQL row-level security involving foreign key with other table PostgreSQL是否针对只读事务运行一些性能优化 - Does PostgreSQL run some performance optimizations for read-only transactions 只读事务中的序列 nextval - postgresql 9 - sequence nextval in read-only transactions- postgresql 9 如果事务处于 Postgresql 中的可重复读隔离级别,事务是否会看到来自另一个并发事务的插入? - Will a transaction see inserts from another concurrent transaction if they are in Repeatable Read isolation level in Postgresql? Postgres:为单个表或单个读取选择较弱的事务隔离保证? - Postgres: Opting in to weaker transaction isolation guarantees for a single table or a single read? 如何在Django中访问远程PostgreSQL数据库服务器(只读事务)? - How to access a remote PostgreSQL database server (Read only transaction) in Django? 错误:无法在只读事务中执行nextval()-PostgreSQL - ERROR: cannot execute nextval() in a read-only transaction - PostgreSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM