Re: Duplicate key violation on upsert

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Matt Magoffin <postgresql(dot)org(at)msqr(dot)us>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Duplicate key violation on upsert
Date: 2020-03-20 15:00:15
Message-ID: 604644e7-6881-3fdb-8767-897fc40559ac@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 3/20/20 2:17 AM, Matt Magoffin wrote:
> Hello,
>
> I am experiencing a duplicate key violation in Postgres 9.6 on occasion
> for one particular query, and I’m wondering where I’m going wrong. My
> table looks like this:
>
>                       Table "solardatum.da_datum"
>   Column   |           Type           | Collation | Nullable | Default
> -----------+--------------------------+-----------+----------+---------
>  ts        | timestamp with time zone |           | not null |
>  node_id   | bigint                   |           | not null |
>  source_id | character varying(64)    |           | not null |
>  posted    | timestamp with time zone |           | not null |
>  jdata_i   | jsonb                    |           |          |
>  jdata_a   | jsonb                    |           |          |
>  jdata_s   | jsonb                    |           |          |
>  jdata_t   | text[]                   |           |          |
> Indexes:
>     "da_datum_pkey" UNIQUE, btree (node_id, ts, source_id) CLUSTER,
> tablespace "solarindex"
>     "da_datum_reverse_pkey" UNIQUE, btree (node_id, ts DESC,
> source_id), tablespace "solarindex"
>     "da_datum_x_acc_idx" UNIQUE, btree (node_id, source_id, ts DESC,
> jdata_a) WHERE jdata_a IS NOT NULL, tablespace "solarindex"
> Triggers:
>     aa_agg_stale_datum BEFORE INSERT OR DELETE OR UPDATE ON
> solardatum.da_datum FOR EACH ROW EXECUTE
> PROCEDURE solardatum.trigger_agg_stale_datum()
>     ts_insert_blocker BEFORE INSERT ON solardatum.da_datum FOR EACH ROW
> EXECUTE PROCEDURE _timescaledb_internal.insert_blocker()
>
> The error/query looks like:
>
> ERROR: duplicate key value violates unique constraint
> “_hyper_1_1931_chunk_da_datum_x_acc_idx"

What is the above index UNIQUE over?

What is da_datum_x_acc_idx index below indexed over?

>   Where: SQL statement "INSERT INTO solardatum.da_datum(ts, node_id,
> source_id, posted, jdata_i, jdata_a, jdata_s, jdata_t)
> VALUES (…)
> ON CONFLICT (node_id, ts, source_id) DO UPDATE
> SET jdata_i = EXCLUDED.jdata_i,
> jdata_a = EXCLUDED.jdata_a,
> jdata_s = EXCLUDED.jdata_s,
> jdata_t = EXCLUDED.jdata_t,
> posted = EXCLUDED.posted
> RETURNING (xmax = 0)"
>
> I am using the TimescaleDB extension so there are child tables
> inheriting from this main table and that’s why the reported index name
> differs from the definition shown above. I’m not sure if the extension
> is the problem, so I thought I’d start here to see if I’ve configured
> something wrong or my expectations on how the upsert should work is
> wrong. My expectation was that basically the insert would never fail
> from a duplicate key violation.
>
> The error always references the da_datum_x_acc_idx index, which is a
> partial index with jdata_a added as a covering column… that is, it’s
> only in the index so I can get some index-only results with that column.
> Is the partial index possibly an issue in this configuration?
>
> Thanks for any insight,
> Matt

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2020-03-20 16:12:16 Re: How to get RAISE INFO in JDBC
Previous Message Ravi Krishna 2020-03-20 14:48:43 How to get RAISE INFO in JDBC