- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Oracle 和 PostgreSQL 中都注意到了以下情况。
考虑到我们有以下数据库模式:
create table post (
id int8 not null,
title varchar(255),
version int4 not null,
primary key (id));
create table post_comment (
id int8 not null,
review varchar(255),
version int4 not null,
post_id int8,
primary key (id));
alter table post_comment
add constraint FKna4y825fdc5hw8aow65ijexm0
foreign key (post_id) references post;
具有以下数据:
insert into post (title, version, id) values ('Transactions', 0, 1);
insert into post_comment (post_id, review, version, id)
values (1, 'Post comment 1', 459, 0);
insert into post_comment (post_id, review, version, id)
values (1, 'Post comment 2', 537, 1);
insert into post_comment (post_id, review, version, id)
values (1, 'Post comment 3', 689, 2);
如果我打开两个独立的 SQL 控制台并执行以下语句:
TX1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
TX2: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
TX1: SELECT COUNT(*) FROM post_comment where post_id = 1;
TX1: > 3
TX1: UPDATE post_comment SET version = 100 WHERE post_id = 1;
TX2: INSERT INTO post_comment (post_id, review, version, id) VALUES (1, 'Phantom', 0, 1000);
TX2: COMMIT;
TX1: SELECT COUNT(*) FROM post_comment where post_id = 1;
TX1: > 3
TX1: COMMIT;
TX3: SELECT * from post_comment;
> 0;"Post comment 0";100;1
1;"Post comment 1";100;1
2;"Post comment 2";100;1
1000;"Phantom";0;1
正如预期的那样,SERIALIZABLE
隔离级别保留了 TX1 事务开始时的快照数据,并且 TX1 只看到 3 个 post_comment
记录。
由于 Oracle 和 PostgreSQL 中的 MVCC 模型,TX2 允许插入一条新记录并提交。
为什么允许 TX1 提交?因为这是写倾斜异常,所以我期望看到 TX1 会回滚并出现“序列化失败异常”或类似的东西。
PostgreSQL 和 Oracle 中的 MVCC Serializable 模型是否只提供快照隔离保证而没有 Write Skew 异常检测?
我什至更改了 Tx1 以发出 UPDATE 语句,更改属于同一 post
的所有 post_comment
记录的 version
列。
这样,Tx2 创建一条新记录,而 Tx1 将在不知道已添加满足 UPDATE 过滤条件的新记录的情况下提交。
实际上,让它在 PostgreSQL 上失败的唯一方法是在插入幻像记录之前在 Tx2 中执行以下 COUNT 查询:
Tx2: SELECT COUNT(*) FROM post_comment where post_id = 1 and version = 0
TX2: INSERT INTO post_comment (post_id, review, version, id) VALUES (1, 'Phantom', 0, 1000);
TX2: COMMIT;
然后 Tx1 将被回滚:
org.postgresql.util.PSQLException: ERROR: could not serialize access due to read/write dependencies among transactions
Detail: Reason code: Canceled on identification as a pivot, during conflict out checking.
Hint: The transaction might succeed if retried.
很可能是写入偏移异常预防机制检测到此更改并回滚了事务。
有趣的是,Oracle 似乎并没有被这种异常所困扰,因此 Tx1 只是成功提交。由于 Oracle 不会阻止写入偏移的发生,因此 Tx1 提交正常。
顺便说一下,您可以自己运行所有这些示例,因为它们在 GitHub 上.
最佳答案
在 1995 年的论文中,A Critique of ANSI SQL Isolation Levels , Jim Gray 和同事将 Phantom Read 描述为:
P3: r1[P]...w2[y in P]...(c1 or a1) (Phantom)
One important note is that ANSI SQL P3 only prohibits inserts (and updates, according to some interpretations) to a predicate whereas the definition of P3 above prohibits any write satisfying the predicate once the predicate has been read — the write could be an insert, update, or delete.
因此,幻读并不意味着您可以简单地返回当前正在运行的事务开始时的快照,并假装为查询提供相同的结果将保护您免受实际的幻读异常。
在最初的 SQL Server 2PL(两阶段锁定)实现中,为查询返回相同的结果隐含谓词锁。
MVCC(多版本并发控制)快照隔离(在 Oracle 中错误地命名为 Serializable)实际上并不能阻止其他事务插入/删除与已执行并返回结果集的查询匹配相同过滤条件的行我们当前正在运行的交易。
为此,我们可以设想如下场景,我们希望对所有员工进行加薪:
SELECT SUM(salary) FROM employee where company_id = 1;
INSERT INTO employee (id, name, company_id, salary)
VALUES (100, '李四', 1, 100000);
UPDATE employee SET salary = salary * 1.1;
提交;
提交:
在这种情况下,CEO 运行第一笔交易 (Tx1),因此:
轰! CEO 已根据旧快照做出决定,提供当前更新的薪资预算可能无法维持的加薪。
您可以在 the following post 中查看此用例的详细说明(包含大量图表) .
这是幻读还是 Write Skew ?
根据 Jim Gray and co ,这是幻读,因为写偏斜定义为:
A5B Write Skew Suppose T1 reads x and y, which are consistent with C(), and then a T2 reads x and y, writes x, and commits. Then T1 writes y. If there were a constraint between x and y, it might be violated. In terms of histories:
A5B: r1[x]...r2[y]...w1[y]...w2[x]...(c1 and c2 occur)
在 Oracle 中,事务管理器可能会或可能不会检测到上述异常,因为它不使用谓词锁或 index range locks (next-key locks) ,比如 MySQL。
只有当 Bob 对员工表发出读取时,PostgreSQL 才设法捕获此异常,否则,无法阻止该现象。
最初,我假设可序列化性也意味着时间顺序。然而,作为very well explained by Peter Bailis ,挂钟排序或线性化仅假定为严格可序列化。
因此,我的假设是针对 Strict Serializable 系统的。但这不是 Serializable 应该提供的。 Serializable 隔离模型不保证时间,并且允许重新排序操作,只要它们等同于一些串行执行。
因此,根据Serializable的定义,如果第二个事务不发出任何读取,就会发生这种幻读。但是,在 2PL 提供的 Strict Serializable 模型中,即使第二个事务没有针对我们试图防止幻读的相同条目发出读取,幻读也会被阻止。
关于oracle - Oracle和PostgreSQL中Write Skew异常不回滚事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39567266/
Oracle 即时客户端和 Oracle 客户端有什么区别?你能给我解释一下吗?谢谢 最佳答案 Oracle 客户端带有安装程序和许多可执行文件,例如 sqlplus,tnsping,它是完整而庞大的
我正在寻找一个Delphi组件来直接连接到ORACLE数据库服务器,而无需安装oracle客户端。 我知道Oracle Data Access (ODAC)来自DevArt 。还有其他组件具有此功能吗
如何编写 Oracle 存储过程,以表 (X) 作为输入参数,并在过程内部使用表 X 来与另一个表 Y 联接? 表 X 将包含数千条记录。 不希望将表名作为 varchar 传递,然后使用动态 SQL
如何编写 Oracle 存储过程,以表 (X) 作为输入参数,并在过程内部使用表 X 来与另一个表 Y 联接? 表 X 将包含数千条记录。 不希望将表名作为 varchar 传递,然后使用动态 SQL
我要过滤COMMENTS属性为空的记录 我试过了 SELECT TABLE_NAME, COMMENTS FROM (SELECT TABLE_NAME, COMMENTS FROM (sel
我要下载 Oracle Instant Client for Linux x86-64 (64-bit)现在有一段时间了。 现在我注意到该网站在过去几个月中一直遇到技术问题。 要从 Oracle 下载
有什么方法可以将我的 Delphi 应用程序 (FireDac) 直接连接到 Oracle 数据库? 目前可以连接,但需要安装Oracle Client 在 embarcadero 站点 ( http
我有一张表,其中日期列的数据格式如下:“7/25/2014 12:14:27 AM”。我需要通过放入 where 子句来获取此日期。有人可以建议我该怎么做吗? 最佳答案 日期(存储在表中)是 repr
如果两个事务试图同时修改同一行会发生什么?通常,一旦行被修改,另一个事务等待直到第一个事务执行提交或回滚。但是,如果他们恰好在同一时刻发送更新请求怎么办? 最佳答案 答案是否定的。两个事务不能同时修改
我想知道为什么我不能在 Oracle 模式中有两个同名的索引?它抛出一个错误,指出该名称已被使用。我的印象是,由于索引在一个特定的表上,这应该不会导致任何错误,除非我们对同一个表上的两个不同列使用相同
我需要构建一个查询来按成员和到期日期检索信息组,但我需要为每个成员提供一个序列号.. 例如: 如果成员“A”有 3 条记录要过期,“B”只有 1 条,“C”有 2 条,我需要这样的结果: Number
独立程序 create procedure proc1 ( begin end; ) 存储过程 create package pkg1 ( procedure proc2 begin end; ) 最
在 Oracle 9i 中声明 FK 时遇到问题。我在这里查看了许多关于 SO 和一些在线文档(例如 http://www.techonthenet.com/oracle/foreign_keys/f
我和我的同事维护的应用程序在后端有一个 Oracle 数据库。我们正在考虑偶尔以“受限”模式运行应用程序,其中一个数据库表空间设置为只读。我们可以轻松地将必要的表和索引移动到单独的表空间,这些表空间将
我想实现一个自定义的回归聚合函数,类似于现有的 REGR_SLOPE . 我要定义的函数需要获取两列作为参数,例如 select T.EMPLOYEE_ID, CUSTOM_REGR_SL
我已经尝试解决这个问题一段时间了,我认为是时候寻求帮助了。我正在构建一个架构配置脚本,我想添加一些脚本输出和错误处理。这个想法是脚本输出窗口只会向我显示关键消息而没有所有噪音。 Create Temp
在旧的 Oracle 服务器(我被告知是 8i)上使用 JDBC 时,我遇到了一个非常令人困惑和奇怪的问题。我在那里准备了一个表,其中包含大约 10 列、数字、varchars、一个 raw(255)
我有一张 table Customer_Chronics在 Oracle 11g 中。 该表具有三个关键列,如下所示: 分支代码 客户 ID 期 我已按 branch_code 列表按表分区,现在我进
是否有存储用户自定义异常的oracle表? 最佳答案 没有。 与其他变量一样,用户定义的异常在 PL/SQL block 中定义,并且具有 PL/SQL 变量将具有的任何范围。所以,例如 DECLAR
在 oracle 中使用序列并使用 Before insert trigger 自动递增列或使用标识列是否更好,因为它在 Oracle 12 c 中可用? 最佳答案 无论哪种方式,您都将使用序列。 1
我是一名优秀的程序员,十分优秀!