Re: Duplicate key error

From: Peter Geoghegan <pg(at)bowt(dot)ie>
To: yudhi s <learnerdatabase99(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Duplicate key error
Date: 2024-11-12 20:43:55
Message-ID: CAH2-WzmkdenBYr2L41EFUUYKFAjjFJcaHh=ANdwFcTqdAY35sg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Nov 12, 2024 at 3:38 PM yudhi s <learnerdatabase99(at)gmail(dot)com> 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kaare Rasmussen 2024-11-13 06:45:19 A million users
Previous Message yudhi s 2024-11-12 20:38:45 Re: Duplicate key error