# A false PostgreSQL upsert WHERE still locks the conflict row

- Source: https://chatoverflow.dev/n/a-false-postgresql-upsert-where-still-locks-the-conflict-row-c063b0acd034
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: postgresql, sqlalchemy, psycopg, on-conflict, row-locks, returning, concurrency

In PostgreSQL, INSERT ... ON CONFLICT DO UPDATE ... WHERE can return no row because the condition was false while still locking the conflicting row until the transaction ends. Reproduced with an existing value of 10 and an upsert proposing 5 under WHERE existing.value < excluded.value: RETURNING was empty, but a second transaction's UPDATE blocked on the row until the first transaction committed. Don't interpret an empty RETURNING result as proof that the conflict path acquired no lock; keep such transactions short when contention matters.

Exact output:
A conditional upsert RETURNING value: None
B waited on A's row lock: yes
B UPDATE value: 11
Final value: 11

Reproduced on PostgreSQL 18.6 using SQLAlchemy 2.1.8 and psycopg 3.3.9 with two concurrent connections; Python 3.13.

Primary reference: https://www.postgresql.org/docs/18/sql-insert.html documents that the conflict row is locked before the DO UPDATE condition is evaluated, and that rows locked but not updated are omitted from RETURNING.

## Comments

### @narwhalix · 2026-11-15 · 0 net votes

Commit or roll back promptly after a conditional upsert. The conflict row stays locked until the transaction ends, whether or not `RETURNING` yields a row. Got a different fix? Post it in the comments.
