Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: duplicate key value violates unique constraint on UPDATE command

When doing an UPDATE query, we got the following error message:

ERROR:  duplicate key value violates unique constraint "tableA_pkey"
DETAIL:  Key (id)=(47470) already exists.

However, our UPDATE query does not affect the primary key. Here is a simplified version:

UPDATE tableA AS a
SET
    items = (
        SELECT array_to_string(
            array(
                SELECT b.value
                FROM tableB b
                WHERE b.a_id = b.id
                GROUP BY b.name
            ),
            ','
        )
    )
WHERE
    a.end_at BETWEEN now() AND  now() - interval '1 day';

We ensured the primary key sequence was already synced:

\d tableA_id_seq

Which produces:

    Column     |  Type   |          Value           
---------------+---------+--------------------------
 sequence_name | name    | tableA_id_seq
 last_value    | bigint  | 50364
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | t

Looking for maximum table index:

select max(id) from tableA;

We got a lower value:

  max  
-------
 50363
(1 row)

Have you any idea on why such a behavior? If we exclude the problematic id, it works.

Another strange point is that replacing the previous UPDATE by:

UPDATE tableA AS a
SET
    items = (
        SELECT array_to_string(
            array(
                SELECT b.value
                FROM tableB b
                WHERE b.a_id = b.id
                GROUP BY b.name
            ),
            ','
        )
    )
WHERE a.id = 47470;

It works well. Are we missing something?

EDIT: triggers

I have no user-defined triggers on this table:

SELECT t.tgname, c.relname
FROM pg_trigger t
JOIN pg_class c ON t.tgrelid = c.oid
WHERE
    c.relname = 'tableA'
    AND
    t.tgisinternal = false
;

Which returns no row.

Note: I am using psql (PostgreSQL) 9.3.4 version.

like image 866
Jonathan Petitcolas Avatar asked Jun 27 '14 09:06

Jonathan Petitcolas


People also ask

How to fix PostgreSQL error duplicate key violates unique constraint?

How to fix PostgreSQL error "duplicate key violates unique constraint" SELECT setval('the_primary_key_sequence', (SELECT MAX(the_primary_key) FROM the_table)+1); That will set the sequence to the next available value that's higher than any existing primary key in the sequence.

What is duplicate key value violates unique constraint?

The “duplicate key violates unique constraint” error notifies the caller that a retry is needed. This seems like an intuitive approach, but relying on this optimistic insert can quickly have a negative performance impact on your database.


1 Answers

Not really sure what was the cause. However, deleting the two (non vital) records corresponding to already existing ids (?) solved the issue.

like image 156
Jonathan Petitcolas Avatar answered Sep 21 '22 07:09

Jonathan Petitcolas