小编Dav*_*Bob的帖子

PostgreSQL 中并发 DELETE / INSERT 的锁定问题

这很简单,但我对 PG 所做的(v9.0)感到困惑。我们从一个简单的表开始:

CREATE TABLE test (id INT PRIMARY KEY);
Run Code Online (Sandbox Code Playgroud)

和几行:

INSERT INTO TEST VALUES (1);
INSERT INTO TEST VALUES (2);
Run Code Online (Sandbox Code Playgroud)

使用我最喜欢的 JDBC 查询工具 (ExecuteQuery),我将两个会话窗口连接到该表所在的数据库。它们都是事务性的(即 auto-commit=false)。我们称它们为 S1 和 S2。

每个相同的代码位:

1:DELETE FROM test WHERE id=1;
2:INSERT INTO test VALUES (1);
3:COMMIT;
Run Code Online (Sandbox Code Playgroud)

现在,以慢动作运行它,在窗口中一次执行一个。

S1-1 runs (1 row deleted)
S2-1 runs (but is blocked since S1 has a write lock)
S1-2 runs (1 row inserted)
S1-3 runs, releasing the write lock
S2-1 runs, now that it can get the lock. But reports …
Run Code Online (Sandbox Code Playgroud)

postgresql concurrency locking serialization

38
推荐指数
4
解决办法
3万
查看次数

Postgres 需要在异常后提交或回滚

只是想了解这里的想法......

错误说明对于其他数据库,您可以在发生 SQLException 后运行其他命令。它写于 2001 年,但从未接触过。我们正在使用 Postgres 和 SQL Server,所以现在我们遇到了这个问题。

假设你有:

start transaction;
create table test(id int primary key);
insert into test values (1);
commit;

-- Following statement throws a SQLException(duplicate key) in
-- PG, SS and ORacle
insert into test values (1);

-- Following statement behaves differently for different DBMS:
-- SS and OR: No error...statement runs fine
-- PG: Another SQLException thrown...must rollback or commit
insert into test values (99);
Run Code Online (Sandbox Code Playgroud)

谁能帮助我理解为什么 Postgres 不允许这样做?或者更好的是,是否有连接标志来改变行为?(我看了看,没有找到任何东西。)

我在上面添加了“开始事务”位以表明它不是自动提交的 tx。

这是一个 …

postgresql transaction error-handling

4
推荐指数
1
解决办法
3万
查看次数