wrkm.app
Search
Databases

Deepak Mahto: Exploring PostgreSQL 19: A Developer’s Guide to New Features – Part 1: INSERT ON CONFLICT DO SELECT

Read time: ~8 minutes PostgreSQL 19 finally lets INSERT hand back the row that already exists, no dummy update, no exception block, no second round trip. PostgreSQL 19 was released on November 19, 2026. With it, I’m starting a new series: what’s new in PostgreSQL 19 primarily for application developers, the people writing SQL and PL/pgSQL every day, or supporting migrations to PostgreSQL. Part 1 starts with the feature that removes a workaround almost every code base carrie

Editor
Nov 30, 2026 · 4 min read
Deepak Mahto: Exploring PostgreSQL 19: A Developer’s Guide to New Features – Part 1: INSERT ON CONFLICT DO SELECT

Read time: ~8 minutes

PostgreSQL 19 finally lets INSERT hand back the row that already exists, no dummy update, no exception block, no second round trip.

PostgreSQL 19 was released on November 19, 2026. With it, I’m starting a new series: what’s new in PostgreSQL 19 primarily for application developers, the people writing SQL and PL/pgSQL every day, or supporting migrations to PostgreSQL.

Part 1 starts with the feature that removes a workaround almost every code base carries: INSERT … ON CONFLICT DO SELECT.

The Pattern Every Application Writes

Every application has a get-or-create:

Register a user by email

Find or create a tag

Resolve a customer reference arriving from an upstream feed

The ask is always the same. Give me the row — whether it was already there or not.

Until PostgreSQL 19, INSERT … ON CONFLICT couldn’t say that. DO NOTHING skips the row and returns nothing. DO UPDATE returns the row, but only by writing it again.

Before PostgreSQL 19: Three Workarounds, Three Compromises

Let’s create a sample table:

CREATE TABLE app_users ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, email text NOT NULL UNIQUE, full_name text, created_at timestamptz NOT NULL DEFAULT now());INSERT INTO app_users (email, full_name)VALUES ('deepak@dcg.com', 'Deepak Mahto');

Workaround 1: DO NOTHING, then SELECT

postgres=# INSERT INTO app_users (email, full_name) VALUES ('deepak@dcg.com', 'Deepak M') ON CONFLICT (email) DO NOTHING RETURNING id, email, full_name; id | email | full_name----+-------+-----------(0 rows)INSERT 0 0

The row exists. RETURNING gives you nothing. So the application fires a second SELECT as two round trips, and a window between them where the row can change or disappear.

The one-statement version wraps it in a CTE:

WITH ins AS ( INSERT INTO app_users (email, full_name) VALUES ('deepak@dcg.com', 'Deepak M') ON CONFLICT (email) DO NOTHING RETURNING id, email, full_name)SELECT id, email, full_name FROM insUNION ALLSELECT id, email, full_name FROM app_usersWHERE email = 'deepak@dcg.com' AND NOT EXISTS (SELECT 1 FROM ins);

Looks perfect, right? Under READ COMMITTED, the outer SELECT reads from the statement’s snapshot. If another session inserts the same email and commits after our statement starts, DO NOTHING sees the conflict but the SELECT doesn’t see the row. Zero rows back. Rare, and almost impossible to reproduce in testing.

Workaround 2: The “Dummy” DO UPDATE

The popular fix is to update the row to itself, so RETURNING has something to return:

postgres=# SELECT ctid, xmin, id, email, full_name FROM app_users; ctid | xmin | id | email | full_name-------+------+----+--------------------+-------------- (0,1) | 732 | 1 | deepak@dcg.com | Deepak Mahto(1 row)postgres=# INSERT INTO app_users (email, full_name) VALUES ('deepak@dcg.com', 'Deepak M') ON CONFLICT (email) DO UPDATE SET email = EXCLUDED.email RETURNING id, email, full_name, (xmax = 0) AS inserted; id | email | full_name | inserted----+--------------------+--------------+---------- 1 | deepak@dcg.com | Deepak Mahto | f(1 row)INSERT 0 1postgres=# SELECT ctid, xmin, id, email, full_name FROM app_users; ctid | xmin | id | email | full_name-------+------+----+--------------------+-------------- (0,2) | 733 | 1 | deepak@dcg.com | Deepak Mahto(1 row)postgres=# SELECT n_tup_ins, n_tup_upd, n_dead_tup FROM pg_stat_user_tables WHERE relname = 'app_users'; n_tup_ins | n_tup_upd | n_dead_tup-----------+-----------+------------ 1 | 1 | 1(1 row)

Nothing changed. PostgreSQL still wrote a new row version — new ctid, new xmin, one dead tuple. At scale that is:

A dead tuple per lookup, and vacuum debt that grows with traffic

WAL for every “read”, shipped to replicas and backups

A row lock taken on every call

UPDATE triggers firing for a change that never happened – audit rows, updated_at stamps

Phantom UPDATE events in logical replication and CDC

Workaround 3: The Exception Block (Straight from Oracle)

This is the one I see in converted Oracle code. The PL/SQL source:

BEGIN INSERT INTO app_users (email, full_name) VALUES (p_email, p_name) RETURNING id INTO v_id;EXCEPTION WHEN DUP_VAL_ON_INDEX THEN SELECT id INTO v_id FROM app_users WHERE email = p_email;END;

It usually lands in PostgreSQL almost 1:1, with WHEN unique_violation. It works. But every EXCEPTION block runs as a subtransaction, and every duplicate leaves an aborted row behind.

We called that converted function 1,000 times for an email that already exists:

postgres=# SELECT n_tup_ins, n_dead_tup, pg_size_pretty(pg_relation_size('app_users')) AS heap_size FROM pg_stat_user_tables WHERE relname = 'app_users'; n_tup_ins | n_dead_tup | heap_size-----------+------------+----------- 1001 | 1000 | 72 kB(1 row)

One live row. A thousand dead ones. Functionally correct. Physically expensive.

Outputs above are from previous PostgreSQL versions (

Kaynak: Planet PostgreSQL

#PostgreSQL
Share

From other categories