Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nHtDI-0005mR-ST for pgsql-general@arkaria.postgresql.org; Wed, 09 Feb 2022 20:05:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nHtDH-0003Dp-Ol for pgsql-general@arkaria.postgresql.org; Wed, 09 Feb 2022 20:05:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nHtDF-0003Dc-Pe for pgsql-general@lists.postgresql.org; Wed, 09 Feb 2022 20:05:15 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nHtD8-0002sW-Kg for pgsql-general@lists.postgresql.org; Wed, 09 Feb 2022 20:05:12 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 717FE5C01D1; Wed, 9 Feb 2022 15:05:05 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 09 Feb 2022 15:05:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; bh=SxzgKSszPAFbNw ksaw2Dg3izQTgw0bZWD9ACNal/Zc0=; b=EOrpB6CcQPUJCn5lI4l8P+zfbF8aJR y6DlBXOnLsJ1uNffMVtXE30OQOVMcTqHUeGUpdwz9mKWbzBKCk+DSUPIhhbKmK4P W9lbhZoDp9zDkXzoRJaUANzuPELia67B921OrVfCbCTwvkNz8kqI2qU9+40LiQT0 jpYTr+f6ACiUX8FqbGdbvfc2MiICJh4I9M7JIzoxdlDdxlkQ9aYARB7PwgmSDNYX 6Rah4Kz3Kft75y14WppOknCqD/DpGyYAlHyDHlf66XQQg/pL+5nSW0iuZpz4cb/V 1RoLwKnhYLZLnyfZuNN0JoVAhVxjm4fUuZWVfbSnZTUEo5SQ8Tx5HA/w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:sender:subject:subject:to:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=SxzgKSszPAFbNwksaw2Dg3izQTgw0bZWD9ACNal/Zc0=; b=ajlu+G+O wJMbMkOS5c3YQVixSGolGe7XmYeL4IGIniV9WFZQ5MJlLYS0MwnFSWIzZ6TKfP6k Ycu4042WI3xmsR8csbabFP/zD5y1j/8511r4u3RVqqLyHtjkGTHxKJvGRtRnTWZS haSf9z6EqsMoLf0kmVjABcayeuaUQ7Ka8U6OKwDmdIC8eY56nagDzm6o9cMYDK7A 2O5pmWR2nX9cSBBTEzO2VOO+8KaXePIgbt/qoC2O4CglnP8e96JggODxbROaT0kH 42fR3ulU5Orcn4xqJz06XVujz3oCpDU++k1rgtDCv+be/zXNBAxPyecBB7zDAWgi 1Nrn3/vfP69jRw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrheelgddufedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpedugfeuleegveffhfegleejhefhtedtgeeileeggfeh fefffeevgeejtdeutefftdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 9 Feb 2022 15:05:04 -0500 (EST) Message-ID: <2d136d22-e82d-c691-775b-c12aa29b079c@aklaver.com> Date: Wed, 9 Feb 2022 12:05:03 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.1 Subject: Re: DELETING then INSERTING record with same PK in the same TRANSACTION Content-Language: en-US To: Brent Wood , Andrew Hardy , "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/9/22 10:34, Brent Wood wrote: > My take on this... > > Because both statements are in the transaction, the delete is not fully > actioned until the commit. So it still exists in the table when you try > to insert the record with the duplicate key. No: \d animals Table "public.animals" Column | Type | Collation | Nullable | Default --------+------------------------+-----------+----------+--------- id | integer | | not null | cond | character varying(200) | | not null | animal | character varying(200) | | not null | Indexes: "animals_pkey" PRIMARY KEY, btree (id) delete from animals where id = 1; DELETE 1 insert into animals values (1, 'great', 'opossum'); INSERT 0 1 > > Check if the error is generated during the transaction or at the commit > stage, run it without the commit, rollback instead to check this. > > I don't see how you can do this within a transaction, someone else might? > > > > Brent Wood > > Principal Technician, Fisheries > NIWA > DDI:  +64 (4) 3860529 > -- Adrian Klaver adrian.klaver@aklaver.com