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 1sd5WV-00D2PC-Ko for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2024 10:10: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 1sd5WU-00BaPy-0L for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2024 10:10:02 +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 1sd5WT-00BaPp-LV for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2024 10:10:01 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sd5WP-004Ayl-T7 for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2024 10:10:00 +0000 Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-5bd1a9bdce4so1332224a12.3 for ; Sun, 11 Aug 2024 03:09:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723370996; x=1723975796; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=cjsIKlmZ0992FGgVHW1rMLo8QnWriGPDviIi5ALe6bc=; b=ksBZcUHL5m9ZIEsRZQf8t80MI7pAgexknyBV38lSwgiUqxMdPegAhjLuvuQ1qzd/tu tHjsXTxXsq9l04SHya4+OnrF8c7iwa1vQZttSU/TGbgmU1Lb66cNTw3zTCJbn3I0fqWw 3/0Xd/kmFpR1GDnbvslUttZ2yNaXTnV/A2GC4SqhfzgO+qQKbL748SryHCPYf9/kIxC1 81fcdU3o8c31uJyXGrnOEPMy57N55V8QHLe1ZEhPcRuzFpH5Wv+5MaHjjJs6CKBLaBnh 70Yn71w1xPPPr4g22v8kBXdPLjxYvvLU7CyFcssZK5CvY9kp/sEqwku/YsSIeT8E2B53 nTSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723370996; x=1723975796; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cjsIKlmZ0992FGgVHW1rMLo8QnWriGPDviIi5ALe6bc=; b=vMeCWxjBJtVygweNHYdFxQH/7e6f0PjYrwSbUk9RbEN/ixjof7hvnj3nzHbkygVCdj oWqY0B8mk/NoTDQPfcBfbBvDaNh0/xToLdIbVfGkO+HbwnU0GPa+OX4LtDYptdeOJdnH sBkDbbQY9z0v+TAkNKHJ0YP1DAXqVbEfABByKVakkqdveVv5Y3+4OkFaZA333ApyazQE Hy3jSvtbOVa2mB5KHCnEtoTYr9kD/dfzJnLKQwYxVElntfLrZA4CIPTG2HfzOJ5qaeva kehq/6oRzTzK6p1fA4EsFNdLas6jOM2Ux6qHiKJlBgU8tfG849l/69oZ4DVd0X6CW/98 T1Jg== X-Forwarded-Encrypted: i=1; AJvYcCXGugM3/3z5jmgXbQsBTZBDUqhBDbRoGOHlRujn5XeVElwQTYJd4hZ/nGBcmqtdSGb5jlzGlXVb6V4SlVlp@lists.postgresql.org X-Gm-Message-State: AOJu0Yxc3YUtKNGn+GKuy5Ha3mQFpwLft7WtMvX2NnppyCPrXh32mZFa U/76aj1ippIpovOmkNN3YICSyfpCeFAgxJIVi3p1Svu9RgIoK+j5 X-Google-Smtp-Source: AGHT+IEl8LdxHGHcP2n7Pfe8Tzmq6YApyNq+FrLWfPeLeCwj1TQ3/el3bVhrdN7u2g16Eom3pBvDAw== X-Received: by 2002:a17:907:c7c8:b0:a7a:9f0f:ab18 with SMTP id a640c23a62f3a-a80aa598664mr485597466b.20.1723370995955; Sun, 11 Aug 2024 03:09:55 -0700 (PDT) Received: from smtpclient.apple ([188.212.112.125]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80bb0e1b29sm137364066b.68.2024.08.11.03.09.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 03:09:55 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) Subject: Re: Insert works but fails for merge From: Alban Hertroys In-Reply-To: Date: Sun, 11 Aug 2024 12:09:44 +0200 Cc: Adrian Klaver , David G Johnston , pgsql-general Content-Transfer-Encoding: quoted-printable Message-Id: References: <37e09717-f121-4192-b152-18df17713414@aklaver.com> <9dc702ca-8bb3-442e-bd2b-12abe81a84d0@aklaver.com> To: yudhi s X-Mailer: Apple Mail (2.3776.700.51) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 10 Aug 2024, at 22:23, yudhi s wrote: > On Sat, Aug 10, 2024 at 8:22=E2=80=AFPM Adrian Klaver = wrote: > > MERGE INTO tab1 AS target > > USING (VALUES ('5efd4c91-ef93-4477-840c-a723ae212d99', 123,=20 > > '2024-08-09T11:33:49.402585600Z','2024-08-09T11:33:49.402585600Z')) = AS=20 > > source(id, mid,txn_timestamp, cre_ts) > > ON target.id =3D source.id > > WHEN MATCHED THEN > > UPDATE SET mid =3D source.mid > > WHEN NOT MATCHED THEN > > INSERT (id, mid, txn_timestamp, cre_ts) > > VALUES (source.id ,source.mid,=20 > > source.txn_timestamp, source.cre_ts); >=20 > 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=E2=80=99t 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=E2=80=99m not so sure about that claim=E2=80=A6 At least on MSSQL, MERGE has this requirement: "A MERGE statement cannot = UPDATE/DELETE the same row of the target table multiple times.=E2=80=9D. = I=E2=80=99ve 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.=E2=80=9D, which also seems to imply that you shouldn=E2=80=99t have = duplicates. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.