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 1jG7TR-00084I-Ma for pgsql-general@arkaria.postgresql.org; Sun, 22 Mar 2020 20:45:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jG7SR-0008F6-WB for pgsql-general@arkaria.postgresql.org; Sun, 22 Mar 2020 20:44:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jG7SQ-0008BP-0U for pgsql-general@lists.postgresql.org; Sun, 22 Mar 2020 20:44:31 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jG7SJ-0000LT-TM for pgsql-general@lists.postgresql.org; Sun, 22 Mar 2020 20:44:29 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id DDBE95C0135; Sun, 22 Mar 2020 16:44:20 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sun, 22 Mar 2020 16:44:20 -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=W lAJjfPh/y7EA1ACoessGXXg0pD/SGsLWXLEc658ifk=; b=UEvXT2h4vlJ0F5T7p zIngO0UBvN4+7AoDvQWyXtfvV5elaZcqPigEoyKKJdIrYRr5PJyb19EMwJO23Hgn 9xiVMddokcB0WwFm6TDsauZhBR3LWW0kBKWToizRiRGoSevcXM/44uFX4RFCEg3s rYAcPkCGv01Rwx+0XcnayC7Xf3YH3ZZYk4oXRZ+uTWmImHE0mpqZYfSSVlx2XonI 1WZTjm3JaXq6xctVay7UryBFRmVT4otr3mItjw2iudAnvWsbY2FXFqas0c/d8KXG VNZZgRzmxpefvPIURIQzu9HRw2RNRhG7aezT5U5MqOPdNGcQ65UtbAuUIceiSPcQ 9PsTg== 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=WlAJjfPh/y7EA1ACoessGXXg0pD/SGsLWXLEc658i fk=; b=ZdtHFg0okoh5Jv1X3svJgM+UW6gm2VAhIY3A039nl8FvmVqF20B2VcG7/ k44Vo/++aiR+V1yutL4pDgfhjxlxXdmOo2eotCCfbes5TQLf12Blg3+j6HfdTUH6 u1qAikufpM4G1n2l48W5s+8hhbpCy1zp6a0kq4aC6NxGxPyBW1HsOtMIxN2wZyZ2 65VlU+cqcKz9ZV/d5g7VDhmCjIpc143Y+kd8AI4bMCeygM9vWXyPy6za630iSa72 8fxWxdJQaS+QJaIGLbAbT/2Za1JGDD/4/ZaFTOOKhgfSHM8aax/t5Lfy1ID/wt3b AFKvLRV/Abs5zUfF7eDI7ZJkfx50g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudegiedgudegvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurddvuddvrdduvdelrd duleefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 1AB223060FE7; Sun, 22 Mar 2020 16:44:20 -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> <162ae0d1-be71-f200-2b58-80b4b955fea8@aklaver.com> From: Adrian Klaver Message-ID: <80cdd870-e46c-147a-d063-03acfb30c892@aklaver.com> Date: Sun, 22 Mar 2020 13:44:19 -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/21/20 2:45 PM, Matt Magoffin wrote: > >> On 22/03/2020, at 8:11 AM, Adrian Klaver > > 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. Well if the combination of values do not need to be UNIQUE then imposing UNIQUE on them, in my mind, is an unnecessary constraint. Though it would be good to know why the constraint is being tripped. Is there a chance the BEFORE trigger functions are doing something that could be leading to the error? In the error log is there a line with the actual values that failed? > > — m@ -- Adrian Klaver adrian.klaver@aklaver.com