Missed in PostgreSQL 19 : Add support for INSERT … ON CONFLICT DO SELECT.

On 12th of February 2026, Dean Rasheed committed patch:

Add support for INSERT ... ON CONFLICT DO SELECT.
 
This adds a new ON CONFLICT action DO SELECT [FOR UPDATE/SHARE], which
returns the pre-existing rows when conflicts are detected. The INSERT
statement must have a RETURNING clause, when DO SELECT is specified.
 
The optional FOR UPDATE/SHARE clause allows the rows to be locked
before they are are returned. As with a DO UPDATE conflict action, an
optional WHERE clause may be used to prevent rows from being selected
for return (but as with a DO UPDATE action, rows filtered out by the
WHERE clause are still locked).
 
Bumps catversion as stored rules change.
 
Author: Andreas Karlsson <andreas@proxel.se>
Author: Marko Tiikkaja <marko@joh.to>
Author: Viktor Holmberg <v@viktorh.net>
Reviewed-by: Joel Jacobson <joel@compiler.org>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Discussion: https://postgr.es/m/d631b406-13b7-433e-8c0b-c6040c4b4663@Spark
Discussion: https://postgr.es/m/5fca222d-62ae-4a2f-9fcb-0eca56277094@Spark
Discussion: https://postgr.es/m/2b5db2e6-8ece-44d0-9890-f256fdca9f7e@proxel.se
Discussion: https://postgr.es/m/CAL9smLCdV-v3KgOJX3mU19FYK82N7yzqJj2HAwWX70E=P98kgQ@mail.gmail.com

I'm really surprised I missed this one, as it solves a problem that I've seen asked about on IRC / Slack / Discord more times than I can count.

The problem is: “I want to insert a row, and if it already exists, I want to get its id". Typical “get or create" thing.

Let's make simple table:

=$ create table users (
    id int8 generated always as identity primary key,
    username text not null unique,
    created timestamptz not null default now()
);
CREATE TABLE
 
=$ insert into users (username) values ('depesz'), ('magnus');
INSERT 0 2
 
=$ select * from users;
 id │ username │            created
────┼──────────┼───────────────────────────────
  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
  2 │ magnus   │ 2026-12-02 11:52:12.181456+01
(2 rows)

Now, the naive approach is to use on conflict do nothing, but then you get nothing back:

=$ insert into users (username) values ('depesz') on conflict (username) do nothing returning *;
 id │ username │ created
────┼──────────┼─────────
(0 rows)
INSERT 0 0

So people were doing either separate select afterwards (with all the race conditions that come with it), CTE with union of insert and select, or the “fake update" trick: on conflict … do update set username = excluded.username returning *. This last one works, but it does an actual update. Let's see what that means:

=$ select ctid, xmin, * from users;
 ctid  │ xmin │ id │ username │            created
───────┼──────┼────┼──────────┼───────────────────────────────
 (0,1) │  848 │  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
 (0,2) │  848 │  2 │ magnus   │ 2026-12-02 11:52:12.181456+01
(2 rows)
 
=$ insert into users (username) values ('depesz'), ('tom')
    on conflict (username) do update set username = excluded.username
    returning ctid, xmin, *;
 ctid  │ xmin │ id │ username │            created
───────┼──────┼────┼──────────┼───────────────────────────────
 (0,3) │  849 │  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
 (0,4) │  849 │  5 │ tom      │ 2026-12-02 11:52:12.185946+01
(2 rows)
INSERT 0 2

Row for depesz got new ctid, and new xmin. Which means: new row version was written, old one is now dead, and will have to be vacuumed. Plus WAL, plus update triggers fire, and so on.

Now, with Pg 19, we can do:

=$ insert into users (username) values ('depesz'), ('bruce')
    on conflict (username) do select
    returning ctid, xmin, *;
 ctid  │ xmin │ id │ username │            created
───────┼──────┼────┼──────────┼───────────────────────────────
 (0,3) │  849 │  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
 (0,5) │  850 │  7 │ bruce    │ 2026-12-02 11:52:12.188243+01
(2 rows)
INSERT 0 2

depesz row is returned as it was – same ctid, same xmin. Nothing was written for it. And bruce got inserted normally.

Please note the command tag: INSERT 0 2, even though only one row was really inserted. With do select the count is number of returned rows, not inserted ones. So, if your app checks number of inserted rows based on command tag – it will be misled.

Of course, without returning it doesn't make any sense, and PostgreSQL tells us about it:

=$ insert into users (username) values ('depesz') on conflict (username) do select;
ERROR:  ON CONFLICT DO SELECT requires a RETURNING clause
LINE 1: insert into users (username) values ('depesz') on conflict (...
                                                       ^

Nice thing is that we can easily tell which rows were inserted, and which already existed. Since Pg 18 we have old and new in returning, and with do select old is filled only for pre-existing rows:

=$ insert into users (username) values ('depesz'), ('robert')
    on conflict (username) do select
    returning old.*, new.*;
   id   │ username │            created            │ id │ username │            created
────────┼──────────┼───────────────────────────────┼────┼──────────┼───────────────────────────────
      1 │ depesz   │ 2026-12-02 11:52:12.181456+01 │  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
 [null] │ [null]   │ [null]                        │  9 │ robert   │ 2026-12-02 11:52:12.19137+01
(2 rows)
INSERT 0 2

So, something like this works great:

=$ insert into users (username) values ('depesz'), ('andres')
    on conflict (username) do select
    returning old.id is null as was_inserted, new.*;
 was_inserted │ id │ username │            created
──────────────┼────┼──────────┼───────────────────────────────
 f            │  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
 t            │ 11 │ andres   │ 2026-12-02 11:52:12.193043+01
(2 rows)
INSERT 0 2

If you need to lock the returned row, for example because you will be updating it later in the transaction, you can add for update (or for share, for no key update, for key share):

=$ insert into users (username) values ('depesz')
    on conflict (username) do select for update
    returning *;
 id │ username │            created
────┼──────────┼───────────────────────────────
  1 │ depesz   │ 2026-12-02 11:52:12.181456+01
(1 row)
INSERT 0 1

There is also optional where clause, that can filter which conflicting rows get returned:

=$ insert into users (username) values ('tom'), ('peter')
    on conflict (username) do select where users.id < 3
    returning *;
 id │ username │            created
────┼──────────┼───────────────────────────────
 14 │ peter    │ 2026-12-02 11:52:12.194098+01
(1 row)
INSERT 0 1

tom already existed, with id 5, so it was filtered out. peter didn't exist, so got inserted. Please note, as commit message says, that the rows filtered out by where are still locked, if you used for update/share.

Side note: that took me a while to wrap my head around it. WHERE id < 3 returned row with id 14? In case you also wondered, it's because the where applies to select, which happen on conflict. So if the conflict happened on row with id < 5, for example for depesz, it would have been returned:

=$ insert into users (username) values ('depesz'), ('peter')
    on conflict (username) do select where users.id < 3
    returning *;
 id │ username │            created
────┼──────────┼───────────────────────────────
  1 │ depesz   │ 2026-12-02 11:52:12.586665+01
(1 row)

One more thing, that is not new, but it's good to remember: sequence (identity) values are consumed even for rows that end up being conflicts. That's why ids in my table jump:

=$ select id, username from users order by id;
 id │ username
────┼──────────
  1 │ depesz
  2 │ magnus
  5 │ tom
  7 │ bruce
  9 │ robert
 11 │ andres
 14 │ peter
(7 rows)

So don't expect them to be gap-less.

This is really cool. “Get or create" in single, simple, and race-free statement, without generating dead rows. Thanks a lot to all involved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.