小编Ami*_*ron的帖子

PostgreSQL锁定机制中的错误或对机制的误解

我们遇到了PostgreSQL 9.0.12锁定机制的问题.

这是我们重现问题的最小代码:

脚本

Transaction 1      Transaction 2
BEGIN              BEGIN
......             select trees for update;                
update apples;      
--passes
update apples;    
-- stuck!      
Run Code Online (Sandbox Code Playgroud)

重现代码:如果你想在你的PostgreSQL中尝试它 - 这是一个你可以复制/粘贴的代码.

我有一个以下数据库架构:

CREATE TABLE trees (
    id       integer primary key
);

create table apples (
    id       integer primary key,
    tree_id  integer references trees(id)
);

insert into trees values(1);
insert into apples values(1,1);
Run Code Online (Sandbox Code Playgroud)

打开两个psql shell:

在shell 1上:

BEGIN;
    SELECT id FROM trees WHERE id = 1 FOR UPDATE;    
Run Code Online (Sandbox Code Playgroud)

在shell 2上:

BEGIN;
UPDATE apples SET id = id …
Run Code Online (Sandbox Code Playgroud)

postgresql locking transactions

8
推荐指数
1
解决办法
708
查看次数

标签 统计

locking ×1

postgresql ×1

transactions ×1