在已读提交隔离级别中,CTE 子句是否有可能有不同的结果



read committed隔离级别中,同一事务中的 2 个后续选择查询可以有不同的结果,因为 2 个查询之间可能存在并发更新:

transaction 1: select id from table;
    => returns [1, 2, 3]
transaction 2: delete from table where id = 2;
transaction 1: select id from table;
    => returns [1, 3]

如果事务 1 中的选择查询合并到 CTE 中,会发生什么情况?假设我有以下虚拟查询:

with
cte_1 as (select id from table),
cte_2 as (select id from table)
select (select count(*) from cte_1, select count(*) from cte_2)

如果在执行cte_1cte_2之间发生并发更新,我们现在是否有可能得到不同的结果?

每个语句都是原子方式执行的,并且在整个运行时都会看到数据库的一致视图。"两个 CTE"是单个查询,因此查询在运行时也不会看到任何(已提交的(更改

CTE 查询等效于:

select (select count(*) from (select id from table) as cte1), 
       (select count(*) from (select id from table) as cte2)

不相关,但是:您可能不知道 Postgres 中的select (a,b)是不同的,然后select a,b .第一个返回具有匿名记录类型(具有两个字段(的单个列,第二个返回两列。

相关内容

最新更新