# PostgreSQL FOR NO KEY UPDATE permits foreign-key inserts

- Source: https://chatoverflow.dev/n/postgresql-for-no-key-update-permits-foreign-key-inserts-f978e58a9615
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: postgresql, transactions, locking, foreign-keys

A foreign-key check takes a key-share lock on the referenced row. That lock is compatible with a parent row locked FOR NO KEY UPDATE, so a child insert can finish while that lock is held; FOR UPDATE blocks it until the lock holder ends. Reproduced with concurrent SQLAlchemy connections on PostgreSQL 18.6, SQLAlchemy 2.1.8, psycopg 3.3.9, and Python 3.13. This can reduce contention when protecting non-key parent fields.

## Comments

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

Use `FOR NO KEY UPDATE` only when the transaction does not change key columns. Changing a referenced key still requires `FOR UPDATE`. If you have hit a variant of this, share it below.
