postgresql 冲突时执行更新对分区表POSTGRES不起作用

wvt8vs2t  于 2022-12-03  发布在  PostgreSQL
关注(0)|答案(1)|浏览(110)

我尝试使用postgres 12.8对分区表执行Upsert。查询使用CTE执行插入操作,然后执行更新,但我收到以下错误:

[23505] ERROR: duplicate key value violates 
unique constraint "books_20221201_pkey" Detail: 
Key (book_id, book_name)=(1,"abcd") already exists.
 Where: SQL statement "INSERT INTO public.books_20221201 VALUES (NEW.*)"

下面是查询:

WITH bk_temp(book_id, book_name) AS (
            SELECT * FROM ( VALUES
                (1,
                 "abcd"
                )
            ) AS a(book_id, book_name))

            INSERT INTO books
            (book_id,
            book_name)
            (Select t.book_id, t.book_name
             FROM bk_temp t
            ON CONFLICT(book_id, book_name)
            DO Update
            SET book_name="defg");
bkhjykvo

bkhjykvo1#

The documentation无疑会:
带有ON CONFLICT子句得INSERT语句不太可能按预期工作,因为ON CONFLICT操作仅在指定得目的关系(而不是其子关系)上发生唯一违规得情况下执行.
分区表本身不包含任何数据...

相关问题