Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdAEk-00DcTb-P9 for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2024 15:12:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sdAEh-00C8lk-EH for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2024 15:11:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdAEf-00C8lb-SU for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2024 15:11:59 +0000 Received: from fout8-smtp.messagingengine.com ([103.168.172.151]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdAEc-004IYA-08 for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2024 15:11:57 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 54231138E595; Sun, 11 Aug 2024 11:11:51 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sun, 11 Aug 2024 11:11:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1723389111; x=1723475511; bh=rGATITOr9JoxaPqRzAZt/dXCzo4sFkZfEo9EWWGzUNQ=; b= JopRoJMSgp5+y0vEDxgV4FqykMz5rtdR+OdDo1hwII96POa24/SPkzvjZsBbhTOS mtEfP4ydSG1gaZnbv1YjCtux+Lf+Oa5J2zb70hujfvjE2HBKvD0bBPLtK1CDYorj z4rIrA3b1SQxIzgesYrhwatL6ZTOC/f3oEGgpy4GBzNREHvxEcd/uAIUFRDlxPjF LSQ4oms6I6ai06Q9HwSVwgWn/n3KsK/7dUlFQUWFmjkYVozH4VyyHJTCq/F7oWJ4 WtMUz57qm8/cZ+vFj4Gxm59ilN8410K909eEgNfz/4oz+Bo6hONlBJEG3yH281C9 vbwJhWv5qanlCel6Gr/ogg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1723389111; x= 1723475511; bh=rGATITOr9JoxaPqRzAZt/dXCzo4sFkZfEo9EWWGzUNQ=; b=P s8lZsWSEWGTqvgnquYXMZ3FlU7dq47fy60GklKTLG0KjYRgiAK3YjwBmaKWFsqKe eOktKCXLVVvjtkscLboNjVYsTq+eoAD4Dt4qMLgbFKTt1SBEUD6DCOSVq3CJ7WCo X0T9CdyrGTPwG6CLr7ZRW4kz47Z4ZZMyxGkBh5eZef4KdSp1WtUp7lTE8ZBhOvkF EpV8VEJwOyYiqTWdoDCRgKzKxJCHliwrsxZ5ThQS9jtL3n8x4D1BL3+xyIO7B5gr ju2Ar+ufMaN2yTdjx3+/qrzph4WJaxsMDb0Cvx29waGtknTKsilECLT+8wa8AEZd llVhBt7uH20mThwdgA/fQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrleekgdekiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddvjeen ucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrse grkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeeiudeihfevudfgfeffveef gffhhfethfetuedthfetgeduiefgffeuudfhffevvdenucffohhmrghinhepthgrrhhgvg htrdhiugdpshhouhhrtggvrdhiugenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmpdhnsggprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehh rghrrghmrhgrvgesghhmrghilhdrtghomhdprhgtphhtthhopehlvggrrhhnvghruggrth grsggrshgvleelsehgmhgrihhlrdgtohhmpdhrtghpthhtohepuggrvhhiugdrghdrjhho hhhnshhtohhnsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrh grlheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 11 Aug 2024 11:11:50 -0400 (EDT) Message-ID: Date: Sun, 11 Aug 2024 08:11:49 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Insert works but fails for merge To: Alban Hertroys , yudhi s Cc: David G Johnston , pgsql-general References: <37e09717-f121-4192-b152-18df17713414@aklaver.com> <9dc702ca-8bb3-442e-bd2b-12abe81a84d0@aklaver.com> Content-Language: en-US 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 8/11/24 03:09, Alban Hertroys wrote: > >> On 10 Aug 2024, at 22:23, yudhi s wrote: >> On Sat, Aug 10, 2024 at 8:22 PM Adrian Klaver wrote: >>> MERGE INTO tab1 AS target >>> USING (VALUES ('5efd4c91-ef93-4477-840c-a723ae212d99', 123, >>> '2024-08-09T11:33:49.402585600Z','2024-08-09T11:33:49.402585600Z')) AS >>> source(id, mid,txn_timestamp, cre_ts) >>> ON target.id = source.id >>> WHEN MATCHED THEN >>> UPDATE SET mid = source.mid >>> WHEN NOT MATCHED THEN >>> INSERT (id, mid, txn_timestamp, cre_ts) >>> VALUES (source.id ,source.mid, >>> source.txn_timestamp, source.cre_ts); >> >> Actually , as per the business logic , we need to merge on a column which is not unique or having any unique index on it. > > Then how is the database supposed to determine which of those duplicate rows it should update? In the best case, it would update all of the duplicates with the same values, which usually is not what you want. > >> It's the leading column of a composite unique key though. > > Which could be unique of itself, I suppose that isn’t the case here? > > In that case, IMHO your best course of action is to do something about those duplicates first. > >> And in such scenarios the "INSERT.... ON CONFLICT" will give an error. So we are opting for a merge statement here, which will work fine with the column being having duplicate values in it. > > I’m not so sure about that claim… > > At least on MSSQL, MERGE has this requirement: "A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times.”. I’ve seen that as an error message on occasion. > > The MERGE documentation for PostgreSQL says this: "You should ensure that the join produces at most one candidate change row for each target row.”, which also seems to imply that you shouldn’t have duplicates. The next sentence says: "In other words, a target row shouldn't join to more than one data source row." In this case the OP's data source is a single VALUES(). As it is written I don't it tripping that rule, though it would not take much to change that. > > Alban Hertroys > -- > If you can't see the forest for the trees, > cut the trees and you'll find there is no forest. > -- Adrian Klaver adrian.klaver@aklaver.com