Re: Add ON CONFLICT DO RETURN clause - Mailing list pgsql-hackers

From Peter Geoghegan
Subject Re: Add ON CONFLICT DO RETURN clause
Date
Msg-id CAH2-WzkDnN7jDbT4waRZhnhsvk8pv74LP_ue2ow4LofKCNDZTQ@mail.gmail.com
Whole thread Raw
In response to Add ON CONFLICT DO RETURN clause  (Wolfgang Walther <walther@technowledgy.de>)
Responses Re: Add ON CONFLICT DO RETURN clause
List pgsql-hackers
On Sun, Sep 25, 2022 at 8:55 AM Wolfgang Walther
<walther@technowledgy.de> wrote:
> The attached patch adds a DO RETURN clause to be able to do this:
>
> INSERT INTO x (id) VALUES (1)
>    ON CONFLICT DO RETURN
>    RETURNING created_at;
>
> Much simpler. This will either insert or do nothing - but in both cases
> return a row.

How can you tell which it was, though?

I don't see why this statement should ever perform steps for any row
that are equivalent to DO NOTHING processing -- it should at least
lock each and every affected row, if only to conclusively determine
that there really must be a conflict.

In general ON CONFLICT DO UPDATE allows the user to add a WHERE clause
to back out of updating a row based on an arbitrary predicate. DO
NOTHING has no such WHERE clause. So DO NOTHING quite literally does
nothing for any rows that had conflicts, unlike DO UPDATE, which will
at the very least lock the row (with or without an explicit WHERE
clause).

The READ COMMITTED behavior for DO NOTHING is a bit iffy, even
compared to DO UPDATE, but the advantages in bulk loading scenarios
can be decisive. Or at least they were before we had MERGE.

-- 
Peter Geoghegan



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: tweak to a few index tests to hits ambuildempty() routine.
Next
From: James Coleman
Date:
Subject: Re: Allow foreign keys to reference a superset of unique columns