PostgreSQL UNIQUE constraint violated without any error

41 Views Asked by At

I have the following table:

\d+ my_table;

                                                      Table "public.my_table"
 Column |       Type        | Collation | Nullable |           Default            | Storage  | Compression | Stats target | Description 
--------+-------------------+-----------+----------+------------------------------+----------+-------------+--------------+-------------
 pkey   | integer           |           | not null | generated always as identity | plain    |             |              | 
 name   | character varying |           |          |                              | extended |             |              | 

Indexes:
    "my_table_pkey" PRIMARY KEY, btree (pkey)
    "my_table_name_key" UNIQUE CONSTRAINT, btree (name)
Access method: heap

How is it possible then, that I can get:

select name, count(*) from my_table group by name having count(*) > 1;

   name     | count 
------------+-------
 some_value |     2
(1 row)

The value is not null.

Tried inserting a duplicate manually:

INSERT INTO my_table("name") VALUES('some_value');
ERROR:  duplicate key value violates unique constraint "my_table_name_key"
DETAIL:  Key (name)=(some_value) already exists.

INSERT INTO my_table("name") VALUES('some_value') ON CONFLICT DO NOTHING;
INSERT 0 0

which works as expected.

I'm also tracking all statements that PostgreSQL gets (I set `log_statement = 'all'), but I can't see anything useful in there either.

0

There are 0 best solutions below