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 1kdFsj-0001IP-7G for pgsql-general@arkaria.postgresql.org; Thu, 12 Nov 2020 16:55:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kdFsh-0004jK-St for pgsql-general@arkaria.postgresql.org; Thu, 12 Nov 2020 16:55:31 +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 1kdFsh-0004ho-J9 for pgsql-general@lists.postgresql.org; Thu, 12 Nov 2020 16:55:31 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kdFsf-0008Ef-B0 for pgsql-general@lists.postgresql.org; Thu, 12 Nov 2020 16:55:30 +0000 Received: by mail-ed1-x52d.google.com with SMTP id y4so1278132edy.5 for ; Thu, 12 Nov 2020 08:55:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=96ZmsJ9agqgyJuvfWLqVmQZcGRo73t37WFoQ9RavJZY=; b=OSBFYLWiPMY6lwmlSJ7ca1AKAbgXmnnUW9UZU1GIZwdgyrGJzEkP/gnOsScR2TgPA/ abh0NO+DYTrmSU9zQ0tYtavajjP95S3ZgndQ1zHSOo7c9ZXWeEf4/f1f/STmULJXh5Qn VhiK2eGtWhvzfR0xxQTDitdanAoT2x3eVaBk5ol/YyuCiid5x9WRaNLjSnOhPbS8mtWL Ob9MHOklf0mIGUchEUteZT4/j6rwgz/YFpsbQXB6oOVRZ1riJH/ANCcfWe/GYLvF8Aiv HaHo/izl1nI3nLj41qn2C0gGx3/CYyQKn7kzJVo5x5rbDDtbtZ08SGHjDfwzGIGZcWGG M8mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=96ZmsJ9agqgyJuvfWLqVmQZcGRo73t37WFoQ9RavJZY=; b=EJB4BUHwEFuYCAz0YRqbGQsNaD2tx1yqCY0mc3MhNILbPxQZhZvbXK+enJIfzhHZsZ d1a/+tqbq5aShUWIAGyHrqSkUesRWwz/D1CDtYQnteEcvYRBUhulWpTW5ce6FE8beeR6 zcAirzaneirXh2Hy1wMicYHMW1BFC6WI73b6kRRmMuup3NkaEiCODSzdXCTE7l7+kp7g ejWWj00AI4NDBGkp+vR/LVs0d14oe1oNpwb3LWKxspGUoS1HF3E6arMPDRXzk7Ix1pAM 0HWG8SyRna1mQShVSDCncGrr6+pLX0W1YCZ3/Hh8BM9Wei4FpphAhAOa1n1UFnpamC5o H6+A== X-Gm-Message-State: AOAM533tVXUt5EPomdoNOt/CENZ10g2pJMqDi24z0vPkzgNvfgP09khV lu+Ocehpw0TzaenlmyLAQak= X-Google-Smtp-Source: ABdhPJwMZZ9mFpajWGS7VkRObomjF8e6jOmo0mh6O6SNqCnAXZmpaCTLBpXu2BxGcnVNW5MvOZB7Dw== X-Received: by 2002:aa7:cc0e:: with SMTP id q14mr710897edt.181.1605200127635; Thu, 12 Nov 2020 08:55:27 -0800 (PST) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id n1sm2649233edt.66.2020.11.12.08.55.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Nov 2020 08:55:26 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: Is it possible to write a generic UPSERT? From: Alban Hertroys In-Reply-To: <138f88db-9d18-dc06-3f55-bf0ae36541aa@emmenlauer.de> Date: Thu, 12 Nov 2020 17:55:25 +0100 Cc: PostgreSQL General Content-Transfer-Encoding: quoted-printable Message-Id: <068070B9-38F7-4476-A053-B424A4A0A1D3@gmail.com> References: <138f88db-9d18-dc06-3f55-bf0ae36541aa@emmenlauer.de> To: Mario Emmenlauer X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 12 Nov 2020, at 14:58, Mario Emmenlauer = wrote: (=E2=80=A6) > 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. >=20 > 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 >=20 > In my (naiive) thinking, such a construct would cover 99% of our > use cases. Or did other people make very different experiences? (=E2=80=A6) > 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. We do this in plpgsql, but that=E2=80=99s mostly because this code is = part of our ETL process and it has to perform some other logic on the = same data anyway. I could look up our code for you tomorrow, but that = looks to be a busy day, so I can=E2=80=99t promise. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.