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 1kdVNS-0003sA-Ql for pgsql-general@arkaria.postgresql.org; Fri, 13 Nov 2020 09:28:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kdVNR-0003Dc-Q6 for pgsql-general@arkaria.postgresql.org; Fri, 13 Nov 2020 09:28:17 +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 1kdVNR-0003DV-IC for pgsql-general@lists.postgresql.org; Fri, 13 Nov 2020 09:28:17 +0000 Received: from aldebaran.he1ix.org ([2a01:4f8:173:803:0:a1de:ba:a]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1kdVNJ-0007dt-Pu for pgsql-general@lists.postgresql.org; Fri, 13 Nov 2020 09:28:16 +0000 Received: from port-213-160-25-97.static.as20676.net ([213.160.25.97] helo=[192.168.33.110]) by aldebaran.he1ix.org with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1kdVNB-0006GG-VY; Fri, 13 Nov 2020 10:28:02 +0100 To: Michael Lewis Cc: PostgreSQL General References: <138f88db-9d18-dc06-3f55-bf0ae36541aa@emmenlauer.de> From: Mario Emmenlauer X-Tagtoolbar-Keys: D20201113102801593 Message-ID: <56e86aee-1541-ee4a-0c58-8a13c622b9cf@emmenlauer.de> Date: Fri, 13 Nov 2020 10:28:01 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 213.160.25.97 X-SA-Exim-Mail-From: mario@emmenlauer.de X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on aldebaran.he1ix.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00, NICE_REPLY_A,URIBL_BLOCKED autolearn=ham version=3.3.1 Subject: Re: Is it possible to write a generic UPSERT? X-SA-Exim-Version: 4.2.1 (built Sun, 08 Nov 2009 07:31:22 +0000) X-SA-Exim-Scanned: Yes (on aldebaran.he1ix.org) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12.11.20 18:34, Michael Lewis wrote: > On Thu, Nov 12, 2020 at 6:58 AM Mario Emmenlauer > wrote: > > I can see how "ON CONFLICT" is very powerful. But that power seems > often a burden for us. We would prefer something that is less manual > effort for the specific use case. Basically, we would like: >     INSERT if not exist, and >     UPDATE _all_ non-conflicting fields in case of _any_ conflict > > > If you do not have significant ratio of HOT (heap only tuple) updates that you want to preserve and you don't have sequences that are GENERATED AS ALWAYS > (rather than BY DEFAULT), you could consider just doing a DELETE where the keys exist, then insert all the rows. It should be trivial to figure out the primary > key or other unique index to match on. > > MERGE command is implemented for this use case in some DBMS, but not Postgres (yet?). Actually I'm quite happy you suggest this, because its the one thing that also came to my mind :) I was not really sure if this is a sane idea. But it seems this could be the "easiest" way out, because its short (like "easy to write") and works for all our use cases. The main potential problem for my use case may be cascading deletes, that I think we may want to use in the long run. All the best, Mario Emmenlauer -- BioDataAnalysis GmbH, Mario Emmenlauer Tel. Buero: +49-89-74677203 Balanstr. 43 mailto: memmenlauer * biodataanalysis.de D-81669 München http://www.biodataanalysis.de/