I have a relation y(k, v)
where k is a key.
Having concurrent connections on the database, I run on each of them:
INSERT INTO y
SELECT 'k1', 'v1' WHERE NOT EXISTS (SELECT 1 FROM y WHERE k = 'k1');
sometimes I get the following error:
ERROR: duplicate key value violates unique constraint "y_pk"
Why does it happen? Can it be avoided?