From: Wolfgang Walther <walther@technowledgy.de>
To: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16706: insert into on conflict(pk) do update error violates not-null constraint
Date: Mon, 9 Nov 2020 21:29:51 +0100
Message-ID: <3b3a3d4d-3510-4dfb-580a-11f9b0d00863@technowledgy.de> (raw)
In-Reply-To: <1996438.1604952901@sss.pgh.pa.us>
References: <16706-08837c0cdaf4ce57@postgresql.org>
<1974489.1604935011@sss.pgh.pa.us>
<de53948f-ff64-95dd-222e-cd0233d3399f@postgresfriends.org>
<1996438.1604952901@sss.pgh.pa.us>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: walther@technowledgy.de
Subject: Re: BUG #16706: insert into on conflict(pk) do update error violates not-null constraint
In-Reply-To: <3b3a3d4d-3510-4dfb-580a-11f9b0d00863@technowledgy.de>
References: <16706-08837c0cdaf4ce57@postgresql.org> <1974489.1604935011@sss.pgh.pa.us> <de53948f-ff64-95dd-222e-cd0233d3399f@postgresfriends.org> <1996438.1604952901@sss.pgh.pa.us> <3b3a3d4d-3510-4dfb-580a-11f9b0d00863@technowledgy.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox