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 1kdG0A-0001bW-NR for pgsql-general@arkaria.postgresql.org; Thu, 12 Nov 2020 17:03:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kdG09-00027V-Ku for pgsql-general@arkaria.postgresql.org; Thu, 12 Nov 2020 17:03:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kdG09-00027N-DZ for pgsql-general@lists.postgresql.org; Thu, 12 Nov 2020 17:03:13 +0000 Received: from aldebaran.he1ix.org ([2a01:4f8:173:803:0:a1de:ba:a]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1kdG06-0002vH-Kn for pgsql-general@lists.postgresql.org; Thu, 12 Nov 2020 17:03:12 +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 1kdG03-0003iM-RU; Thu, 12 Nov 2020 18:03:08 +0100 To: Alban Hertroys Cc: PostgreSQL General References: <138f88db-9d18-dc06-3f55-bf0ae36541aa@emmenlauer.de> <068070B9-38F7-4476-A053-B424A4A0A1D3@gmail.com> From: Mario Emmenlauer X-Tagtoolbar-Keys: D20201112180307477 Message-ID: <20b9e75e-e6ce-35cc-eb9e-36a14d9f470f@emmenlauer.de> Date: Thu, 12 Nov 2020 18:03:07 +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: <068070B9-38F7-4476-A053-B424A4A0A1D3@gmail.com> 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 17:55, Alban Hertroys wrote: >> On 12 Nov 2020, at 14:58, Mario Emmenlauer wrote: > > (…) > >> But the statement is slightly complex to type, and I find me and my >> colleagues often spend more time on this than I would hope. Our two >> main challenges are: >> (1) we have to look up the uniqueness constraints on the table, and >> (2) we have to duplicate the insert statement in the UPDATE section >> again, because virtually all fields should get overwritten >> (except for the conflicting ones). On long inserts this can be >> quite annoying and error-prone. >> >> 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 >> >> In my (naiive) thinking, such a construct would cover 99% of our >> use cases. Or did other people make very different experiences? > > (…) > >> Has anybody ever done something like this? Is there an SQL way to >> achieve this? Or another programmatic way? > > We generate the SQL @work based on the definitions in, IIRC, the information_schema. It has tables for both the column lists per table and the primary key definitions. > > With that, an SQL statement that returns the required SQL statement is easy to generate, after which you can execute it either from a plpgsql execute statement in a function or in a do-block. This is actually a very very interesting idea! I did not consider that we could completely generate the statements based on the actual table information from the information_schema. I need to give this a bit more thought but I very much like the idea, thanks for pushing me in a new direction! 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/