I have a query that I've been successfully running since Postgres added upsert support, I believe that was 9.6:
INSERT INTO gitrefresh(projecttag, state, analysis_started, counter_requested, customer_id)
VALUES('npm@randombytes', 'Q', NOW(), 1, 0)
ON CONFLICT (projecttag) DO UPDATE SET state='Q';
Out of nowhere, I started getting this error, I'm on 14.5 now:
duplicate key value violates unique constraint "idx_projecttag"
DETAIL: Key (projecttag)=(npm@randombytes) already exists.
In the server log there's almost the same:
ERROR: duplicate key value violates unique constraint "idx_projecttag"
DETAIL: Key (projecttag)=(npm@randombytes) already exists.
STATEMENT: INSERT INTO gitrefresh(projecttag, state, analysis_started, counter_requested, customer_id) VALUES ('npm@randombytes', 'Q', NOW(), 1, 0) ON CONFLICT (projecttag) DO UPDATE SET state='Q'
I don't understand what's going on, it's acting as if it didn't use the ON CONFLICT part.
The table in question:
\d+ gitrefresh
+-------------------+-----------------------------+----------------------------------------------------------+----------+--------------+-------------+
| Column | Type | Modifiers | Storage | Stats target | Description |
|-------------------+-----------------------------+----------------------------------------------------------+----------+--------------+-------------|
| projecttag | text | | extended | <null> | <null> |
| state | character(1) | | extended | <null> | <null> |
| analysis_started | timestamp without time zone | | plain | <null> | <null> |
| analysis_ended | timestamp without time zone | | plain | <null> | <null> |
| counter_requested | integer | | plain | <null> | <null> |
| customer_id | integer | | plain | <null> | <null> |
| id | integer | not null default nextval('gitrefresh_id_seq'::regclass) | plain | <null> | <null> |
+-------------------+-----------------------------+----------------------------------------------------------+----------+--------------+-------------+
Indexes:
"pk_gitrefresh" PRIMARY KEY, btree (id)
"idx_projecttag" UNIQUE CONSTRAINT, btree (projecttag)
Has OIDs: no