INSERT and transaction serialization in PostreSQL

╄→尐↘猪︶ㄣ 提交于 2019-12-07 17:55:37

问题


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?


回答1:


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.:

-- Session 1                           -- Session 2
CREATE TABLE t (x INT PRIMARY KEY);
BEGIN;
INSERT INTO t VALUES (1);
                                       BEGIN;
                                       INSERT INTO t VALUES (1);  -- blocks here
COMMIT;
                                       -- finally completes with duplicate key error

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




回答2:


The second user will be blocked until the first user commits or rolls back his/her changes.



来源:https://stackoverflow.com/questions/3011260/insert-and-transaction-serialization-in-postresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!