---
title: "Two requests, one duplicate row? PostgreSQL ON CONFLICT fixes it"
description: "INSERT with ON CONFLICT DO UPDATE in PostgreSQL inserts the row or updates the existing one in a single command, and the documentation guarantees an insert or update outcome even under high concurrency. It needs a unique index on the conflict column."
url: https://ihsanalapsi.dev/articles/postgresql-upsert-on-conflict
lang: en
date: 2026-11-25
updated: 2026-11-25
tags: ["Developer tools", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Two requests, one duplicate row? PostgreSQL ON CONFLICT fixes it

Two customers press "Sign up" in the same second, or one customer presses it twice. The code asks the database "does this email exist?", the answer is "no" for both requests, so both requests insert. By the end of this article you will know how to write the insert and the update as one command in PostgreSQL, without that race.

## What is ON CONFLICT?

ON CONFLICT is a part of the PostgreSQL `INSERT` command that says what happens if the new row conflicts with an existing row on a unique constraint: either do nothing, or update the existing row.

According to the [PostgreSQL documentation for INSERT](https://www.postgresql.org/docs/current/sql-insert.html), `ON CONFLICT DO NOTHING` simply avoids inserting the row, and `ON CONFLICT DO UPDATE` updates the existing row that conflicts with the new one. The documentation also calls the second one UPSERT, meaning "update or insert".

## Why does check-then-insert break under load?

Because there is a moment between the "does it exist?" question and the insert, and another request can ask the same question in that moment and get the same answer. Both requests see an empty table, and both insert.

This is the human mistake: the code makes sense if you read it line by line, because you picture one request. But the server is handling many requests at once. If there is a unique constraint, one of them fails with a duplicate error; if there is not, you get two rows for the same email.

ON CONFLICT DO UPDATE solves this because it is one command. The documentation says it guarantees an atomic outcome: either an insert or an update, even under high concurrency, as long as there is no independent error.

## How do you write an upsert correctly?

You write a normal `INSERT`, then `ON CONFLICT` with the column that has the unique index, then what you want to happen.

The documentation's example, where the `distributors` table has a unique index on `did`:

```sql
INSERT INTO distributors (did, dname)
    VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
    ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;
```

`EXCLUDED` is a special table that holds the row that was about to be inserted. So `dname = EXCLUDED.dname` means "put the new name in the existing row". And if you want to leave the existing row as it is:

```sql
INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')
    ON CONFLICT (did) DO NOTHING;
```

The values still come from your app as parameters like any query, because they are [your protection from SQL injection](/articles/sql-injection-parameterized-queries).

## What rules stop the command?

The first rule: there must be a unique index or unique constraint on the column you write after `ON CONFLICT`, because that is what defines a "conflict".

According to the same page:

1. With `DO UPDATE` you must name the column (or constraint) of the conflict. With `DO NOTHING` it is optional, and without it conflicts with any usable constraint or unique index are handled.
2. In all cases, only `NOT DEFERRABLE` constraints and unique indexes are accepted.
3. The command will not update the same existing row more than once. If the values you send hold two rows with the same value in the unique column, you get a cardinality violation error.

The third rule comes up a lot in bulk imports: a file with the same customer twice, so the whole command stops. Remove the duplicates from the values before you send them.

## Where do you start now?

The lesson for us: any check-then-insert in your code is a race between two requests, and the fix is to hand the decision to the database in one command backed by a unique constraint. Search your code for a `SELECT` followed by an `INSERT` on the same table, try replacing them with `ON CONFLICT`, and check the plan with [EXPLAIN ANALYZE](/articles/postgresql-explain-analyze) if the table is large. For more everyday tools, see the [developer tools articles](/articles?tag=developer-tools). Where did you first run into this race?

## FAQ

**What is the difference between DO NOTHING and DO UPDATE?**

DO NOTHING leaves the existing row as it is and skips the new one. DO UPDATE changes the existing row using the values of the row that was about to be inserted.

**Do I have to name the column after ON CONFLICT?**

With DO UPDATE, yes, it is required. With DO NOTHING it is optional, and without it conflicts with any usable constraint or unique index are handled.

**What does a cardinality violation error mean?**

It means one INSERT holds two new rows that conflict with the same existing row, and PostgreSQL will not update the same row twice in one command. Remove the duplicates from the values before you send them.
