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 1scnSU-00AXbb-3a for pgsql-general@arkaria.postgresql.org; Sat, 10 Aug 2024 14:52:42 +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 1scnSS-009J2X-Lh for pgsql-general@arkaria.postgresql.org; Sat, 10 Aug 2024 14:52:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scnSR-009J2O-7K for pgsql-general@lists.postgresql.org; Sat, 10 Aug 2024 14:52:40 +0000 Received: from fhigh3-smtp.messagingengine.com ([103.168.172.154]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scnSM-0043K9-VU for pgsql-general@lists.postgresql.org; Sat, 10 Aug 2024 14:52:37 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 1D1381151D3C; Sat, 10 Aug 2024 10:52:33 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 10 Aug 2024 10:52:33 -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=1723301553; x=1723387953; bh=KYVAJx4woQ6+eFcewtTU4+Ol7y1KHawDzaudrq2GFJA=; b= JOEIXud6m8li9o5g7atVA+wM5+sTYavEAw8oCeF0Fv2BXPb6dGKFXYq+1ncez/Dq CcLdFZxNRl0poGd80IeE0ML5Q0hK46WYxTZODRPitQzl+R7hrVT4oSfHAHd7FxJo C97s4uiaVBkkfsjpd4d24yRqJDsBgr1DmUaGhXvclRt5lpDOkGKmQjy0nSHosch2 13/jOC+3s10M4cSv+f7JaB0vG+9lH6w4gVMYvUf+qW4sws98ljMRHNOTpkk7eWCk oYHqgknfJq6rr5OsZwNed6YZv/xAAyww0Y5cGmA7yJvwctWshH9gs6HZEro14LZB C4w2KV85Tam065vkb8i8/g== 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=1723301553; x= 1723387953; bh=KYVAJx4woQ6+eFcewtTU4+Ol7y1KHawDzaudrq2GFJA=; b=J 7eXoOrJJNMPS5sY8c0j0kgEGwM4x++enFEXLFE34AUkTClUe4wZv3jy0w05gXn4Q Q4dQo5tNC73GG49F7cZVl18NenofWxzWXXGXc7kikkj3lg+7E1mqspZfgvYNPpYG lDil30U3XhBk+hjoOdH6a2ePF1U1KbzMgcWR+Kog2v4AqGPd4DPbi4Uw5HY+l1p9 50mQGKnHo140wYRKAnoHmO2QLmE+4D7NXvK0+92RTE2bW+ynWNkLv7Z3Dc04uyq6 /WiF5/Q+CYjMwz9skoZ0eUBBA85AwdhjUyrL+hX798sXAuG6x89G/tNMmqF/hG5O 80sTNTGWsSvh42o/I16yA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrleeigdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddvjeen ucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrse grkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeeiudeihfevudfgfeffveef gffhhfethfetuedthfetgeduiefgffeuudfhffevvdenucffohhmrghinhepthgrrhhgvg htrdhiugdpshhouhhrtggvrdhiugenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmpdhnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehl vggrrhhnvghruggrthgrsggrshgvleelsehgmhgrihhlrdgtohhmpdhrtghpthhtohepug grvhhiugdrghdrjhhohhhnshhtohhnsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhg shhqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 10 Aug 2024 10:52:32 -0400 (EDT) Message-ID: <9dc702ca-8bb3-442e-bd2b-12abe81a84d0@aklaver.com> Date: Sat, 10 Aug 2024 07:52:31 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Insert works but fails for merge To: yudhi s , "David G. Johnston" Cc: pgsql-general References: <37e09717-f121-4192-b152-18df17713414@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/10/24 05:07, yudhi s wrote: > > > Thank You Adrian and David. > > Even converting the merge avoiding the WITH clause/CTE as below , is > still making it fail with the same error. So it seems , only > direct "insert into values" query can be auto converted/casted but not > the other queries. > > In our case , we were using this merge query in application code(in > Java) as a framework to dynamically take these values as bind values and > do the merge of input data/message. But it seems we have to now cast > each and every field which we get from the incoming message to make > this merge work in a correct way. I am wondering if the only way now is > to get the data types from information_schema.columns and then use the > cast function to write the values of the merge query dynamically > casted/converted for each of the fields in the application code. Please > correct me if my understanding is wrong. Why not use INSERT ... ON CONFLICT instead of MERGE? > > 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); -- Adrian Klaver adrian.klaver@aklaver.com