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 1sctct-00BEsr-Mp for pgsql-general@arkaria.postgresql.org; Sat, 10 Aug 2024 21:27:52 +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 1sctcs-00AYfO-2W for pgsql-general@arkaria.postgresql.org; Sat, 10 Aug 2024 21:27:50 +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 1sctcq-00AYfG-Ct for pgsql-general@lists.postgresql.org; Sat, 10 Aug 2024 21:27:49 +0000 Received: from fhigh8-smtp.messagingengine.com ([103.168.172.159]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sctcj-0045u5-27 for pgsql-general@lists.postgresql.org; Sat, 10 Aug 2024 21:27:47 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 38F3D1151BA7; Sat, 10 Aug 2024 17:27:39 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Sat, 10 Aug 2024 17:27:39 -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=1723325259; x=1723411659; bh=7YGbFdOrf/T3rYXfQ7seY0RJzxoGY10rs7DNis1ZVTQ=; b= ofhaBf3TVPok9wOw0L0nKE1W8ZrYagDCkHpb5BWZPnO1gX7iKTlBpv120ozmoNI3 1pD7HHlz+UcAI3M3BfI8Dji1WIidB/0/Ug1beOkmRuiRpPb3yiLmx8AC/SSQkpxm 8eRRbAWU0oKrFTRhnnwozEVNgMQMfh9Q6KwOvV9FxNxnTw62M2LTvqpvAQ50YimG 7yTzR7orSK46cFtq5DshkLyyHIGw39604AaZcIvt0bYlZcTlpFRDzxi4Lho4h6IJ GswlgsBEZXe9PlpEiZvYTNiFmj30fOZoYyTQZlhD4j3w+er5oU0PmVhIn+toU6w1 a2AzIK8JerGnTEuujeZX5w== 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=1723325259; x= 1723411659; bh=7YGbFdOrf/T3rYXfQ7seY0RJzxoGY10rs7DNis1ZVTQ=; b=P e9E6mIEgztZwgaQp2HfiWJbGryHzHpct3d06BxOF9aP/qCdON769UT638kgXpm9f uKEj6C9Yp9/8vi+MS+hJL3SYEdiTcrpjdBNFe14UBXTZQeSiLLznO0exiLguf53y sgSl5AY3iVXnFa3vOke/7RLkBlm6PViqH22iACpvW3wP1XZxcpwSkxGv6c86wHx+ 9Ct7gXAcf5N7kmBIGC/cWAx/3Bds2DLfI37hfuYG7aOLsSng8H/uTf/NmqLcin51 uP4ThUpCLxTCCiVlpaw/w64B1/3j1e33zYOKRYoXEqSRJry7IqW4+HCdgJTiusAq BDEXqErT9X3OuMEfOFMuA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrleeigdduiedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddtvdej necuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrh esrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepieduiefhvedugfefffev fefghffhtefhteeutdfhteegudeigfffueduhfffvedvnecuffhomhgrihhnpehtrghrgh gvthdrihgupdhsohhurhgtvgdrihgunecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homhdpnhgspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohep lhgvrghrnhgvrhgurghtrggsrghsvgelleesghhmrghilhdrtghomhdprhgtphhtthhope gurghvihgurdhgrdhjohhhnhhsthhonhesghhmrghilhdrtghomhdprhgtphhtthhopehp ghhsqhhlqdhgvghnvghrrghlsehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 10 Aug 2024 17:27:38 -0400 (EDT) Message-ID: Date: Sat, 10 Aug 2024 14:27:37 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Insert works but fails for merge To: 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/10/24 13:23, yudhi s wrote: > > > On Sat, Aug 10, 2024 at 8:22 PM Adrian Klaver > wrote: > > > > 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); > > > > Actually , as per the business logic , we need to merge on a column > which is not unique or having any unique index on it. It's the leading > column of a composite unique key though. 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. Alright it's official I am confused. You started with: WITH source_data (id, mid, txn_timestamp, cre_ts) AS ( VALUES ('5efd4c91-ef93-4477-840c-a723ae212d66', 123, '2024-08-09T11:33:49.402585600Z', '2024-08-09T11:33:49.402585600Z') ) INSERT INTO tab1 (id, mid, txn_timestamp, cre_ts) SELECT id, mid, txn_timestamp, cre_ts FROM source_data ON CONFLICT (id) DO UPDATE SET mid = EXCLUDED.mid, txn_timestamp = EXCLUDED.txn_timestamp, cre_ts = EXCLUDED.cre_ts; That implied that id was unique in of itself. As side note you called it a merge, which it is not as in MERGE. At this point I got off track thinking of MERGE. Then you went to the below which is a 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); The question I have now is if id is part of a composite UNIQUE index on this: CREATE TABLE tab1 ( id varchar(100) , mid INT, txn_timestamp TIMESTAMPTZ NOT NULL, cre_ts TIMESTAMPTZ NOT NULL ) PARTITION BY RANGE (txn_timestamp); Then what is the other column in the UNIQUE index? -- Adrian Klaver adrian.klaver@aklaver.com