Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jFiVd-0000i5-T4 for pgsql-general@arkaria.postgresql.org; Sat, 21 Mar 2020 18:06:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jFiVc-000835-DW for pgsql-general@arkaria.postgresql.org; Sat, 21 Mar 2020 18:06:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jFiVa-00082j-Gz for pgsql-general@lists.postgresql.org; Sat, 21 Mar 2020 18:06:08 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jFiVU-0006WT-Vj for pgsql-general@lists.postgresql.org; Sat, 21 Mar 2020 18:06:05 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 5CF0A6DC; Sat, 21 Mar 2020 14:05:59 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sat, 21 Mar 2020 14:05:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=h wnB9dNkFC0HYJqNac9HO9mhRif1LGiQvtpH4c8Y1Hk=; b=EPkg4Oq1IHbzQPwV+ 0QX87UyQhQ2WHhbj1Qcd0xTuL0ize5/Rym306XS67d3LytA01hrT26wOw2AnkFyu zNZIRNWsfC9joeDlZEOeyo8HlxkA5r5iXfxj9QGFS5/CyXIGcIIU5HU8iHwzwoUS SWouwPzWyXK2Ctp3bBoNnAzY2g+an75XW+oaF1xcfy4wFb5bj8XVCRzwhz9OKX1h v9FZUlD0xbJBQ4kfADGGOwW8WA3hyXzr/8DWek9JSzZlMRnG9X6zc6GQ1CFLBORD el1XixNvvAa21raciOm3pL0ZAqR6gm3aASBhkD4u+iS5NLIvmG7jX8EQ+a58/l7n TJ2mg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=hwnB9dNkFC0HYJqNac9HO9mhRif1LGiQvtpH4c8Y1 Hk=; b=m/vmX3geFqO4/3Dw569D/ngJyCJRT+C7yX4Qh7A3s5vIJOItHHsLpUAbO ZsNqUmaYn2aUf17Cwe5WVhqbw2vuoR9964xRj+h8xhsWAIJFIL1q7BSU0nsoKEmC LhT7lcCNrg9quM1z8TQibOBXjcVX9uJV6+kaeCnKYN4eNCiA8xFlc/qjXVwnqR6a MAq8Prq4gotFiPcLUW9jXM5OyNGC/ghZbkOsCCs7c2hwMaraK+6bfBr/iZR6ydM0 WNZr3LXgev4N7IhOxOgQ6F5u2xBdYBsqhsVu9cSqg3GJmX/NBrvhgc1jdlxFTK4D 79uDBSyH/uemssxHo5YkxacAwzwuA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudegfedguddutdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhoshhtghhrvg hsqhhlrdhorhhgnecukfhppeejuddrvdduvddruddvledrudelfeenucevlhhushhtvghr ufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvg hrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 44421328005D; Sat, 21 Mar 2020 14:05:58 -0400 (EDT) Subject: Re: Duplicate key violation on upsert To: Matt Magoffin Cc: pgsql-general@lists.postgresql.org 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> From: Adrian Klaver Message-ID: Date: Sat, 21 Mar 2020 11:05:57 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/20/20 1:32 PM, Matt Magoffin wrote: > >> On 21/03/2020, at 8:10 AM, Adrian Klaver > > wrote: >> >>> The _hyper_1_1931_chunk_da_datum_x_acc_idx index has the same >>> definition as the da_datum_x_acc_idx above (it is defined on a child >>> table). That is, they are both essentially: >>> UNIQUE, btree (node_id, source_id, ts DESC, jdata_a) WHERE jdata_a IS >>> NOT NULL >>> The da_datum_pkey index is what the ON CONFLICT cause refers to, so >>> (node_id, ts, source_id) is UNIQUE as well. >> >> Hmm, wonder if you are getting bit by this?: >> >> https://www.postgresql.org/docs/12/sql-insert.html#SQL-ON-CONFLICT >> >> "INSERT with an ON CONFLICT DO UPDATE clause is a “deterministic” >> statement. This means that the command will not be allowed to affect >> any single existing row more than once; a cardinality violation error >> will be raised when this situation arises. Rows proposed for insertion >> should not duplicate each other in terms of attributes constrained by >> an arbiter index or constraint.” > > I’m not sure I’m wrapping my head around this. The INSERT affects 1 row > as the unique values (node_id, ts, source_id) are specified in the > statement. Is it possible that da_datum_x_acc_idx is used as the arbiter > index in this situation, rather than da_datum_pkey (that I intended), > and you’re saying that the jdata_a column is getting updated twice, > first in the INSERT and second in the DO UPDATE, triggering the > duplicate key violation? 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. 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 > > — m@ > -- Adrian Klaver adrian.klaver@aklaver.com