PostgreSQL 9.2.1中具有可串行化隔离的谓词锁定 [英] Predicate locking in PostgreSQL 9.2.1 with Serializable isolation
问题描述
我已经仔细阅读有关事务隔离的postgres文档在其他问题中建议,但我仍无法理解谓词锁定"的东西.
I have been reading thoroughly the postgres documentation on transaction isolation suggested in other of my questions but I have not still managed to understand the "predicate locking" stuff.
我希望有人能启发我:-)
I hope somebody can enlighten me :-)
根据文档: PostgreSQL中的谓词锁(与大多数其他数据库系统一样)基于事务实际访问的数据
听起来不错,那为什么会发生以下情况?
That sounds good, then why is the following happening?
CREATE TABLE mycustomer(cid integer PRIMARY KEY, licenses integer);
CREATE TABLE mydevice(id integer PRIMARY KEY, cid integer REFERENCES
mycustomer (cid), status varchar(10));
INSERT INTO mycustomer(cid, licenses) VALUES (1, 5);
INSERT INTO mycustomer(cid, licenses) VALUES (2, 5);
Request 1 Request2
BEGIN TRANSACTION ISOLATION
LEVEL SERIALIZABLE;
BEGIN TRANSACTION ISOLATION
LEVEL SERIALIZABLE;
SELECT * from mydevice where cid = 1;
SELECT * from mydevice where cid = 2;
INSERT INTO mydevice(id, cid, status)
VALUES (1, 1, 'ok');
INSERT INTO mydevice(id, cid, status)
VALUES (2, 2, 'ok');
commit;
(=ok)
commit;
(=rollback)
我知道请求1和请求2中的插入与先前的读操作没有冲突,因此不应出现任何错误.为什么会出现错误:由于事务之间的读/写依赖性而无法序列化访问".
I understand that the inserts from request 1 and request 2 are not conflicting with the previous reads and thus there should not be any error launched. Why am I getting a "ERROR: could not serialize access due to read/write dependencies among transactions".
您可以想象,由于每个并发请求都将被回滚而不考虑其详细信息,因此我无法发生上述行为.在我的业务场景中,我希望并发请求仅在它们为同一个客户插入数据时(根据示例设备)才回滚.
As you can imagine I cannot have the aforementioned behavior happening since every concurrent request would be roll-backed regardless of its details. In my business scenario I would like concurrent requests to be only roll-backed when they were inserting data (as per the example devices) for the same single customer.
这些操作是从Java应用程序执行的,原则上,我正在考虑创建一个锁定表以满足我的需要.有什么想法吗?
These operations are performed from a Java application and In principle I am thinking about creating a locking table to satisfy my needs. Any ideas?
非常感谢!