Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHtvz-0005w6-6o for pgsql-general@arkaria.postgresql.org; Thu, 04 Mar 2021 19:46:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lHtvy-00076B-5A for pgsql-general@arkaria.postgresql.org; Thu, 04 Mar 2021 19:46:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHtvx-000764-Tz for pgsql-general@lists.postgresql.org; Thu, 04 Mar 2021 19:46:53 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lHtvu-0007Br-Qf for pgsql-general@postgresql.org; Thu, 04 Mar 2021 19:46:52 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 3F0D91D2B; Thu, 4 Mar 2021 21:46:43 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 20ymQUFOI2v4; Thu, 4 Mar 2021 21:46:41 +0200 (EET) Received: from mail.hot.ee (hot-relayhost2.estpak.ee [213.180.2.80]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id F33F61C75; Thu, 4 Mar 2021 21:46:40 +0200 (EET) Subject: Re: Duplicate key error To: Michael Lewis Cc: Adrian Klaver , pgsql-general References: <5b68b4b3-b957-5009-cf3e-4bbc9dcbd0d9@aklaver.com> <97577cda-b3fa-4edb-1c8a-2b39afad65c6@hot.ee> <6a9dcf88-7b28-3b8f-0a1a-8a163f45a2a8@hot.ee> <6cdec299-a677-03c4-2c8e-36560e052c9e@aklaver.com> <8bd1ce20-a369-7adf-2bb5-20fb6742c232@hot.ee> <65ad98da-fae5-83e1-6cf2-abe6fc513961@hot.ee> From: Andrus Message-ID: <516197f2-545a-db36-3f5c-caa766c63a0c@hot.ee> Date: Thu, 4 Mar 2021 21:46:32 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------FB2F3301D440B11BEF32988C" Content-Language: et List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------FB2F3301D440B11BEF32988C Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi! >I just meant a regular update (which might impact 0 rows) and then insert (not exists) like you are doing already. This causes duplicate key exception if other process adds same row to table at same time. > > >--transaction still ways. Should manual locking used or is there > better method. > >I don't follow what you mean. Allow access to this table for single transaction only. Wait for exclusive lock , do update/insert, release exclusive lock. Andrus. --------------FB2F3301D440B11BEF32988C Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

Hi!

>I just meant a regular update (which might impact 0 rows) and then insert (not exists) like you are doing already.

This causes duplicate key exception if other process adds same row to table at same time.


>--transaction still ways. Should manual locking used or is there better method.
>I don't follow what you mean.

Allow access to this table for single transaction only. Wait for exclusive lock , do update/insert, release exclusive lock.

Andrus.

--------------FB2F3301D440B11BEF32988C--