Re: INSERT ... ON CONFLICT UPDATE/IGNORE 4.0

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Peter Geoghegan <pg(at)heroku(dot)com>, hlinnaka <hlinnaka(at)iki(dot)fi>, Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, Stephen Frost <sfrost(at)snowman(dot)net>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: INSERT ... ON CONFLICT UPDATE/IGNORE 4.0
Date: 2015-05-01 14:39:35
Message-ID: CA+TgmoYozhG9dcwpVwQ+cpOs+5jakx6MUuXy9zVoXcDSViYqZg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-hackers

On Fri, May 1, 2015 at 10:24 AM, Andres Freund <andres(at)anarazel(dot)de> wrote:
>> > Well, it's a BEFORE INSERT trigger, not a BEFORE UPDATE, that's why I'm
>> > not so sure that argument applies.
>>
>> Would the BEFORE UPDATE trigger even fire in this case?
>
> BEFORE UPDATE triggers fire for INSERT ... ON CONFLICT UPDATE iff
> there's a conflict, yes.
>
>> The thing is, suppose somebody puts a BEFORE INSERT trigger and a
>> BEFORE UPDATE trigger on the table, and each of those triggers does
>> this:
>>
>> NEW.last_updated_time = clock_timestamp();
>> return NEW;
>>
>> That should work, and should cover all cases, even if you're using UPSERT.
>
> The BEFORE UPDATE would catch things in this case.

OK. In that case, I'm a lot less sure what the right decision is. It
seems weird for both the BEFORE INSERT and BEFORE UPDATE triggers to
get a crack at the same tuple, so your way might be better after all.
But on the other hand, the BEFORE INSERT trigger might have had side
effects, so we can't just pretend it didn't happen.

One idea is to decide that an INSERT with an ON CONFLICT UPDATE
handler is still an INSERT. Period. So the INSERT triggers run, the
UPDATE triggers don't, and that's it.

Not sure.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Heikki Linnakangas 2015-05-01 14:47:48 Re: INSERT ... ON CONFLICT UPDATE/IGNORE 4.0
Previous Message Andres Freund 2015-05-01 14:24:03 Re: INSERT ... ON CONFLICT UPDATE/IGNORE 4.0

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2015-05-01 14:40:08 Re: initdb start server recommendation
Previous Message Stephen Frost 2015-05-01 14:30:58 Re: pg_rewind test race condition..?