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 1tAZUY-00GVAi-Am for pgsql-general@arkaria.postgresql.org; Mon, 11 Nov 2024 18:50:25 +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 1tAZUV-00H2bz-Pr for pgsql-general@arkaria.postgresql.org; Mon, 11 Nov 2024 18:50:24 +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 1tAZUV-00H2bQ-Ed for pgsql-general@lists.postgresql.org; Mon, 11 Nov 2024 18:50:24 +0000 Received: from mail-oa1-x2e.google.com ([2001:4860:4864:20::2e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tAZUS-001PHM-Br for pgsql-general@lists.postgresql.org; Mon, 11 Nov 2024 18:50:23 +0000 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-2951f3af3ceso2524077fac.1 for ; Mon, 11 Nov 2024 10:50:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731351019; x=1731955819; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ANuaunfle9YVDn1BhZt1665PxrJwFd7PfyfinbY4ALg=; b=Ph1VJgJcRprPEyA0ZRNvX6nH74+v4Miy7x8KwzZTRFimTk6fyrcUHLmpZ3/0NUHiBd oe0JlJROKee5xwov2/EvtMxzZmKVS3oaZUO7jLfQTgSR0kqllu28PIc9cqpfFWDiMAFv KuF+B0kxsHTp6XVtfvFP/09VoMqFOXZKspZkrJiOH58yO0nl4HdtM1ucN/V4rbVRxZ2M XVacLZ0tzQr+FKE0y6Xo2ibdc5cXjZRptD7pFXVPRo+3tIVFAUFZ2Gb9Ybbo4vXw/FZh dva/M6yRwbgw6xxtlHH6YxHmVO8k8w/HX95DnWXtNL4+4FqeT2R4+gup63uBEowfnajo Xhlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731351019; x=1731955819; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ANuaunfle9YVDn1BhZt1665PxrJwFd7PfyfinbY4ALg=; b=WOqWcJArXftVttc24GcgC3UrvE3uevNnPNQ7JrvSsmsYvYLSPg0t9DZ011MCVWo0Oe rAaT6hUK/j2yNu9tNiov80k5Y/KgPjoYZdVc9DfwjPZ/72IucrCO2BFQXpszwsBKh6f1 wP36gum/jfT9Pt/73h+NDJ3Ell06xKkSht9J8KH7po7VgKlwFLY0JO2E58nSn1YlXl2E W1s88fvJwqfZiMKOhz/12eC5nnIf1hpWCtWzysmHZ7I/VG+wfSISlZCgbHnGyVuM5sER sd51pyLERzsMlwXhAjR2WKtP1lLRaWWepQk6PDtpXKuXxKLpVAxvzr5GKj++lRzY8aDu R25w== X-Gm-Message-State: AOJu0YyJjum8IBEhqCjAEKGuurEtr6ZczpZOkFUC7piWpuipHnYyXkZW iVfJ2QajUbcQ3WAbFX18MHCkS37voBbPGXweLSapyKsNtHAvI3uBOJbb6kHpMDtKc5TnG8ArJbE iN5Dp+594zQUBWKth2g6cTqiFoNKEbjIU X-Google-Smtp-Source: AGHT+IEtBmD87KJyruk7FYCW081sB+/Yvbqv9TZJ0q2VKltcrfrNs2nfofZ2k6RAyEvyzwEsxrxn0snhut23QVQeELI= X-Received: by 2002:a05:6870:2198:b0:277:c113:5b26 with SMTP id 586e51a60fabf-2955fffbcf7mr11094518fac.7.1731351019671; Mon, 11 Nov 2024 10:50:19 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: yudhi s Date: Tue, 12 Nov 2024 00:20:07 +0530 Message-ID: Subject: Re: Duplicate key error To: Achilleas Mantzios - cloud Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000008257ea0626a79219" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000008257ea0626a79219 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Nov 11, 2024 at 1:57=E2=80=AFPM Achilleas Mantzios - cloud < a.mantzios@cloud.gatewaynet.com> wrote: > > > WITH source_data (col1, col2, col3.....col29) AS (VALUES ($1, > > $2::date, $3::timestamptz, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, > > $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, > > $27::timestamptz, $28, $29::timestamptz)) MERGE INTO cpod.TAB1 AS > > target USING source_data AS source ON target.ID =3D source.ID WHEN > > MATCHED THEN UPDATE SET ....) WHEN NOT MATCHED THEN INSERT (...) > > VALUES (....); > > > > Error: > > ERROR: duplicate key value violates unique constraint "TAB1_pkey" > > Detail: Key (ID, part_date)=3D(XXXXXXXXX, 2024-11-04) already exists. > > You have to use the whole composite unique key (including part_date) > when matching against source_data . > > If you had uniqueness on "ID" and then added "part_name" to the key, > you'd still had uniqueness. > > BUT, reversely, if you have uniqueness on the pair (ID, part_date) there > is no guaranteed uniqueness on ID alone, hence your ERROR. > > > Actually the table is partitioned on column part_date which is why the unique key is composite i.e. on ID and part_date. So in that case even we merge on one of the column i.e. ID which is unique in itself, if any duplicate value ID tries to be merged it should satisfy the MATCH criteria and thus it should do a UPDATE on table TAB1 rather than going for NOMATCH or INSERT. So in that case it should not error out with "duplicate key violation". Is my understanding correct here? --0000000000008257ea0626a79219 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Mon, Nov 11, 2024 at 1:57=E2=80=AF= PM Achilleas Mantzios - cloud <a.mantzios@cloud.gatewaynet.com> wrote:

> WITH source_data (col1, col2, col3.....col29) AS (VALUES ($1,
> $2::date, $3::timestamptz, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13,=
> $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26,
> $27::timestamptz, $28, $29::timestamptz)) MERGE INTO cpod.TAB1 AS
> target USING source_data AS source ON target.ID =3D source.ID WHEN > MATCHED THEN UPDATE SET ....) WHEN NOT MATCHED THEN INSERT (...)
> VALUES (....);
>
> Error:
> ERROR: duplicate key value violates unique constraint "TAB1_pkey&= quot;
> Detail: Key (ID, part_date)=3D(XXXXXXXXX, 2024-11-04) already exists.<= br>
You have to use the whole composite unique key (including part_date)
when matching against source_data .

If you had uniqueness on "ID" and then added "part_name"= ; to the key,
you'd still had uniqueness.

BUT, reversely, if you have uniqueness on the pair (ID, part_date) there is no guaranteed uniqueness on ID alone, hence your ERROR.



Actually the table is partitioned = on column part_date which is why the unique key is composite i.e. on ID and= part_date. So in that case even we merge on one of the column i.e. ID whic= h is unique in itself, if any duplicate value ID tries to be merged it shou= ld satisfy the MATCH criteria and thus it should do a UPDATE on table=C2=A0= TAB1 rather than going for NOMATCH or INSERT. So in that case it=C2=A0shoul= d not error out with "duplicate key violation". Is my understandi= ng correct here?
--0000000000008257ea0626a79219--