【问题标题】:Multiple update statements in postgres CTEpostgres CTE 中的多个更新语句
【发布时间】:2019-08-08 11:19:20
【问题描述】:

我遇到了一个包含多个更新语句的 CTE 表达式的奇怪问题。

我可以使用以下 SQL 重现:-

DROP TABLE IF EXISTS foo;
DROP TABLE IF EXISTS baa;
CREATE TABLE foo(id BIGSERIAL, attributes JSONB);
CREATE TABLE baa(id BIGSERIAL, attributes JSON );
INSERT  INTO foo(attributes) SELECT jsonb_build_object('foo', 'baa');

WITH STEP_ONE AS (

    UPDATE foo 
    SET attributes = attributes ||jsonb_build_object('foo2', 'baa2')
    WHERE id = 1
    RETURNING attributes->>'foo' AS foo_att,id

), STEP_TWO AS (
    INSERT INTO baa(attributes)
    SELECT json_build_object('foo', id)
    FROM STEP_ONE
    RETURNING id as baa_id
) 
UPDATE foo
  SET attributes = attributes ||jsonb_build_object('baa', baa_id)
  FROM STEP_TWO
  WHERE id = 1

这不会更新表 foo。但是用“SELECT * FROM STEP_TWO”替换最终更新显示记录存在。

这是 postgresql 中的错误吗?还是我在文档中遗漏了有关在 CTE 中多次更新单个表的内容?

Postgres 版本:

PostgreSQL 11.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit

【问题讨论】:

    标签: postgresql common-table-expression


    【解决方案1】:

    是的,这个is documented

    所有语句都使用相同的快照执行(参见Chapter 13),因此它们无法“看到”彼此对目标表的影响。

    您要么必须重写查询,以便每个表在语句中只修改一次,要么必须运行两个单独的语句。

    【讨论】:

    • 谢谢,我读过这篇文章,但将“子语句”误解为包含在 AS() 中的任何内容,显然最终语句也很重要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    相关资源
    最近更新 更多