public inbox for [email protected]  
help / color / mirror / Atom feed
From: Peter Geoghegan <[email protected]>
To: yudhi s <[email protected]>
Cc: pgsql-general <[email protected]>
Subject: Re: Duplicate key error
Date: Tue, 12 Nov 2024 15:43:55 -0500
Message-ID: <CAH2-WzmkdenBYr2L41EFUUYKFAjjFJcaHh=ANdwFcTqdAY35sg@mail.gmail.com> (raw)
In-Reply-To: <CAEzWdqcG8wFcVCcW-7YVsPWxb-9uzHcKNmyYOdLEZ6o1MHC8Aw@mail.gmail.com>
References: <CAEzWdqeKafRTLDSP-0Ffw8nijQPUXKpgaHxMXoqz0yEdad-2aA@mail.gmail.com>
	<CAH2-Wz=bSrxrEF_xsz+jLgiqQqE=n2_PZPe-GzRsSz6k6-Vr9A@mail.gmail.com>
	<CAEzWdqcG8wFcVCcW-7YVsPWxb-9uzHcKNmyYOdLEZ6o1MHC8Aw@mail.gmail.com>

On Tue, Nov 12, 2024 at 3:38 PM yudhi s <[email protected]> wrote:
> Can you share your thoughts on how exactly this merge query can possibly cause the duplicate key error?

MERGE doesn't take any special precautions to avoid such unique
violations. ON CONFLICT does. It really is that simple.

Your app will be prone to the same sorts of errors (at READ COMMITTED
isolation level) if you do inserts conditioned on the absence of an
existing/conflict row. You'd have to retry the statement to plug the
race condition, which is how Postgres users did upserts prior to the
introduction of ON CONFLICT DO UPDATE.

-- 
Peter Geoghegan






view thread (6+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Duplicate key error
  In-Reply-To: <CAH2-WzmkdenBYr2L41EFUUYKFAjjFJcaHh=ANdwFcTqdAY35sg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox