postgres:INSERT in with子句返回没有行?
问题描述:
运行的Postgres 10,下面的代码返回0行:postgres:INSERT in with子句返回没有行?
drop table if exists foo;
create table foo(t text);
with x as (
insert into foo values ('t') returning 't' as t)
select foo.* from foo join x on foo.t = x.t;
我希望它返回1行。有人可以解释发生了什么吗?
答
表foo
为空,因为(完整)命令完成时插入的行将可见。
见the documentation这种说法(以及下面的例子):
与子语句彼此之间以及与主查询并发执行。因此,在WITH中使用数据修改语句时,指定更新实际发生的顺序是不可预知的。所有的语句都使用相同的快照执行(见第13章),所以它们不能在目标表上“看到”对方的影响。
有趣的行为,它显示行计数1,如果我执行查询两次。并从总行数中连续显示一行。 – WannaBeSQLExpert
'select * from x' –