pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matt Magoffin <postgresql.org@msqr.us>
To: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Duplicate key violation on upsert
Date: Sun, 22 Mar 2020 10:45:39 +1300
Message-ID: <EB9192B4-9EAC-4376-BC8B-5D9CEF56F5FE@msqr.us> (raw)
In-Reply-To: <162ae0d1-be71-f200-2b58-80b4b955fea8@aklaver.com>
References: <0E4E069D-40B1-46C1-9BA0-5A5E737F753B@msqr.us>
	<604644e7-6881-3fdb-8767-897fc40559ac@aklaver.com>
	<03EC7E36-EA7F-452A-B0A9-2F0FBB73E914@msqr.us>
	<00a020ca-f2db-2dd7-2aa6-e3ef78e7ba68@aklaver.com>
	<C6A3AA75-38FA-4A8E-9FBD-A9121AE4EE91@msqr.us>
	<a7aecaad-5bb5-3daf-95e8-2c815039f66c@aklaver.com>
	<162ae0d1-be71-f200-2b58-80b4b955fea8@aklaver.com>


> On 22/03/2020, at 8:11 AM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> 
>> I was thinking more about this:
>> "INSERT INTO solardatum.da_datum(ts, node_id, source_id, posted, jdata_i, jdata_a, jdata_s, jdata_t)
>>     VALUES (…) ..."
>> from your OP. Namely whether it was:
>> VALUES (), (), (), ...
>> and if so there were values in the (),(),() that duplicated each other.
>> As to the second part of your response, ON CONFLICT does one of either INSERT or UPDATE. If:
>> 1) There is no conflict for ON CONFLICT (node_id, ts, source_id) then the INSERT proceeds.
>> 2) If there is a conflict then an UPDATE occurs using the SET values.
>> Now just me working through this:
>> da_datum_pkey       = (node_id, ts, source_id)
>> da_datum_x_acc_idx  = (node_id, source_id, ts DESC, jdata_a)
>> If 1) from above applies then da_datum_x_acc_idx will not be tripped as the only way that could happen is if the node_id, ts, source_id was the same as an existing row and that can't be true because the PK over the same values passed.
> 
> Well the below is complete rot. If you are UPDATEing then you are not creating a duplicate row, just overwriting a value with itself.
> 
>> If 2) from above happened then you are trying to UPDATE a row with matching PK values(node_id, ts, source_id). Now it is entirely possible that since you are not testing for constraint violation on (node_id, source_id, ts DESC, jdata_a) that you be doing SET jdata_a = EXCLUDED.jdata_a, using a value that would trip da_datum_x_acc_idx

Sorry for the vagueness in my OP, I was trying to make it easier to read. The VALUES are for individual single column values, so a single possible row to insert/update.

So what you’ve outlined is basically what I thought should be happening. Namely, there can be only one row that will be inserted/updated. I am wondering if I should re-create the da_datum_x_acc_idx index without UNIQUE? I had it as UNIQUE to optimise the type of queries that make use of that index… but I did a little bit of testing using a non-UNIQUE index and those queries appear to execute around the same time as with the UNIQUE index. I just wasn’t sure if that would just be masking some other problem in my setup.

— m@=

view thread (15+ messages)  latest in thread

Message-ID: <EB9192B4-9EAC-4376-BC8B-5D9CEF56F5FE@msqr.us>
Permalink:  ../EB9192B4-9EAC-4376-BC8B-5D9CEF56F5FE@msqr.us/
Also on:    postgresql.org/message-id/EB9192B4-9EAC-4376-BC8B-5D9CEF56F5FE@msqr.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: postgresql.org@msqr.us, adrian.klaver@aklaver.com
  Subject: Re: Duplicate key violation on upsert
  In-Reply-To: <EB9192B4-9EAC-4376-BC8B-5D9CEF56F5FE@msqr.us>
  References: <0E4E069D-40B1-46C1-9BA0-5A5E737F753B@msqr.us> <604644e7-6881-3fdb-8767-897fc40559ac@aklaver.com> <03EC7E36-EA7F-452A-B0A9-2F0FBB73E914@msqr.us> <00a020ca-f2db-2dd7-2aa6-e3ef78e7ba68@aklaver.com> <C6A3AA75-38FA-4A8E-9FBD-A9121AE4EE91@msqr.us> <a7aecaad-5bb5-3daf-95e8-2c815039f66c@aklaver.com> <162ae0d1-be71-f200-2b58-80b4b955fea8@aklaver.com> <EB9192B4-9EAC-4376-BC8B-5D9CEF56F5FE@msqr.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox