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 1tAxfG-0019rV-MC for pgsql-general@arkaria.postgresql.org; Tue, 12 Nov 2024 20:39:06 +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 1tAxfC-009CXI-AP for pgsql-general@arkaria.postgresql.org; Tue, 12 Nov 2024 20:39:02 +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 1tAxfB-009CX9-W1 for pgsql-general@lists.postgresql.org; Tue, 12 Nov 2024 20:39:02 +0000 Received: from mail-oi1-x231.google.com ([2607:f8b0:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tAxf8-001b2B-Vu for pgsql-general@lists.postgresql.org; Tue, 12 Nov 2024 20:39:01 +0000 Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3e5f9712991so2990931b6e.2 for ; Tue, 12 Nov 2024 12:38:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731443938; x=1732048738; 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=TepiXsS5bRqJ4wVt+++E4JtOfg2A8rsHu/8G5Di2CGg=; b=HDeFHfrfuTfS73qLPo9a2GrrhwGWZtRLu/+9H/4FCYxCSuo3e9ElXJ6zg6cn9oqlrR pHpJyPM5a3vjefMy6fz8Xgjif57vY8FHiXfukLKihpuFYSxFGAWPZRZj4/7eirWaTMog yiCzfA0TVMzqjqbPfvS17b9WnN2uWwFtZj9XJh9vEi559vX+dk7VPC8YSuRQASVf8Apk cqxDpCBTvgYYCT/1jrMPDNJbzRXmxxCSBgVwjdEcASglziTOGTPH2hAlFgzX5HrCZBx0 fLp26QNzjHntS2NKYFPalkQpp99P3XAZxiYl5Pg4ykGB7G+ht0DMGz24+bmFpnV3bMml 19vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731443938; x=1732048738; 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=TepiXsS5bRqJ4wVt+++E4JtOfg2A8rsHu/8G5Di2CGg=; b=L78EKWrkPDmE/5qTxT8BXxg/zZ0OPczuavb/OqrDVuSbcRzmBUNeu8lso6lO8m+XTT iP9yHqGMoKcPpNnQWzKkpe4ItfCBZDvgQR+2Gk/pV8A6yLg2VDmxKR/9qLzauvxN3NIA RcPVz1ZxgtOdEtGtYqhZctPXtz8vu3i6bJROe0KGx2gTcq0OS1PRMCS6W5ckXJt2QuuB UovhKkLOSUCvvlcTKXDKpk3v8S8vTMNtLCCIrdRGZJ3wQVbsnRl3Uak5JjTO+4TbGwI0 M5kjLg2ww+3jK/t4Vg+a3+ONCLMQk/l3V4A4XyFcelsZmfY7i+DqDXwj0Sp/0cGPFjeY TeCQ== X-Gm-Message-State: AOJu0YypTyrfxeYe34Bjr7h4LQPtIarn66YbfJKNXX7lQ/y6XFpyGg6S E16hysuz7KnRqCnjFGeNLwAeHU4EPaEE8TkRIUOei7alVpW/tcpigReIWHUDqVeiCmN/SIoLcy2 agM6iSprRm87RxON1WXJCubEUkpY7vMZi X-Google-Smtp-Source: AGHT+IHDT1ff+W6bgOMmzpKooz+4neDdtX3yzBd+H7/328TYG0aKZ8EepuNVyLQ2RZROvDSmgC0qqfYnGJ9wTxvYdKc= X-Received: by 2002:a05:6870:1f18:b0:278:8fe:6293 with SMTP id 586e51a60fabf-2956000760bmr13962114fac.1.1731443937578; Tue, 12 Nov 2024 12:38:57 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: yudhi s Date: Wed, 13 Nov 2024 02:08:45 +0530 Message-ID: Subject: Re: Duplicate key error To: Peter Geoghegan Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000d918980626bd3460" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000d918980626bd3460 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Nov 12, 2024 at 1:35=E2=80=AFAM Peter Geoghegan wrote: > On Mon, Nov 11, 2024 at 12:41=E2=80=AFAM yudhi s > wrote: > > So it means it will ensure no duplication happens for ID values, but > still we are seeing "duplicate key" error. So what is the possible reason > here or are we encountering any buggy behaviour here? > > MERGE doesn't actually make any promises about not getting unique > violations. Only ON CONFLICT DO UPDATE (and ON CONFLICT DO NOTHING) > make such a promise. That's the main reason why Postgres supports > both. > > Okay. But here in this Merge statement it should first compare the ON clause which is the value of ID column and if its exists in the target table then its a MATCH which means it will do the UPDATE and if its not available in the target table then its a NOT MATCH and it will do the INSERT, so i am wondering at what exact situation it will throw duplicate key error. Also the WITH clause will only pick one record at a time and run the MERGE, so it will only merge one record at a time and then commit. Can you share your thoughts on how exactly this merge query can possibly cause the duplicate key error? --000000000000d918980626bd3460 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Tue, Nov 12, 2024= at 1:35=E2=80=AFAM Peter Geoghegan <pg@bowt.ie> wrote:
On Mon, Nov 11, 2024 at 12:41=E2=80=AFAM yudhi s <= ;learnerda= tabase99@gmail.com> wrote:
> So it means it will ensure no duplication happens for ID values, but s= till we are seeing "duplicate key" error. So what is the possible= reason here or are we encountering any buggy behaviour here?

MERGE doesn't actually make any promises about not getting unique
violations. Only ON CONFLICT DO UPDATE (and ON CONFLICT DO NOTHING)
make such a promise. That's the main reason why Postgres supports
both.


Okay. But here in this Merge = statement it should first compare the ON clause which is the value of ID co= lumn and if its=C2=A0exists in the target=C2=A0table then its a MATCH which= means it will do the UPDATE and if its=C2=A0not available in the target ta= ble then its a NOT MATCH and it will do the INSERT, so i am wondering at wh= at exact situation it will throw duplicate key error. Also the WITH clause = will only pick one record at a time and run the MERGE, so it will only merg= e one record at a time and then commit. Can you share your thoughts on how = exactly=C2=A0this merge query can possibly cause the=C2=A0duplicate key err= or?
--000000000000d918980626bd3460--