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 1kcDnj-00079W-So for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Nov 2020 20:30:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcDni-0004Lq-BA for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Nov 2020 20:30:06 +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 1kcDni-0004Lj-4l for pgsql-bugs@lists.postgresql.org; Mon, 09 Nov 2020 20:30:06 +0000 Received: from dd27502.kasserver.com ([85.13.146.49]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kcDnf-0008HW-KM for pgsql-bugs@lists.postgresql.org; Mon, 09 Nov 2020 20:30:05 +0000 Received: from [192.168.0.52] (ip-37-201-154-176.hsi13.unitymediagroup.de [37.201.154.176]) by dd27502.kasserver.com (Postfix) with ESMTPSA id 31A6060C3817 for ; Mon, 9 Nov 2020 21:29:59 +0100 (CET) Subject: Re: BUG #16706: insert into on conflict(pk) do update error violates not-null constraint To: pgsql-bugs@lists.postgresql.org References: <16706-08837c0cdaf4ce57@postgresql.org> <1974489.1604935011@sss.pgh.pa.us> <1996438.1604952901@sss.pgh.pa.us> From: Wolfgang Walther Message-ID: <3b3a3d4d-3510-4dfb-580a-11f9b0d00863@technowledgy.de> Date: Mon, 9 Nov 2020 21:29:51 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 MIME-Version: 1.0 In-Reply-To: <1996438.1604952901@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Tom Lane: >> I think the issue is the not null constraint is evaluated before the ON >> CONFLICT and so there is no chance to "heal" the row by coalescing the >> old and new values. > > Ah. Well, ON CONFLICT is for resolving duplicate-key errors; > it's not a get-out-of-jail-free card for every sort of error. > > I think the right way to handle this example would be with a > before-update trigger, which IIRC can modify the row before > we apply any table constraint checks. I don't think that would work, because the not null constraint is checked before the INSERT, not the UPDATE. But a before-insert trigger would not be able replace NULL with the "old" value, because there is none (yet). The whole approach to this query seems a bit strange to me: When you have the NOT NULL constraint and insert a NULL value like this, you must expect the query to fail for a regular INSERT (when the on conflict does not trigger) anyway. So the only way to succeed with this query is by going through the ON CONFLICT. But if you know before, that you will only be successful with an UPDATE, why not make the whole query an UPDATE from the start? Best Wolfgang