Re: unique constraint violation on multiple-rows update

From: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: unique constraint violation on multiple-rows update
Date: 2026-04-19 13:43:51
Message-ID: aeTcFzCm4Lz-ARZS@hermes.hilbert.loc
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Am Sun, Apr 19, 2026 at 03:17:07PM +0200 schrieb Karsten Hilbert:

> WITH cte AS (
> SELECT pk, list_position
> FROM clin.export_item
> WHERE
> list_position >= _target_position
> ORDER BY
> list_position DESC
> )
> UPDATE clin.export_item SET
> list_position = cte.list_position + 1
> FROM cte
> WHERE
> clin.export_item.pk = cte.pk;
>
> Running that does violate the (non-deferred) UNIQUE
> constraint on the table column, however.

Wait, should that UPDATE have been:

UPDATE clin.export_item SET
list_position = list_position + 1
FROM cte
WHERE
clin.export_item.pk = cte.pk;

(note the lack of "cte." on the "list_position + 1")

Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Saulo José Benvenutti 2026-04-19 13:46:30 ANN: storage_engine 1.0.7 – columnar + row-compressed Table Access Methods for PostgreSQL 16-18
Previous Message Karsten Hilbert 2026-04-19 13:17:07 unique constraint violation on multiple-rows update