Two sessions insert into a child table referencing the same parent row, neither one touches the parent's columns, and one of them blocks — sometimes for the duration of the other's whole transaction. No row in the parent table was ever written. The cause is the trigger a foreign key constraint silently creates, and the row-level lock it takes on the referenced row.
What REFERENCES actually installs
FOREIGN KEY (customer_id) REFERENCES customers(id) isn't just a constraint checked once; it's implemented as a pair of triggers — RI_FKey_check_ins on the child table (fires on insert/update) and triggers on the parent for ON DELETE/ON UPDATE actions. The check trigger, to guarantee the parent row still exists and isn't about to be deleted concurrently, needs more than a SELECT — it takes a FOR KEY SHARE row lock on the specific parent row being referenced, for the duration of the inserting transaction. pg_locks shows it:
SELECT pid, mode, relation::regclass, granted FROM pg_locks WHERE locktype = 'tuple'; pid | mode | relation | granted -------+-----------------+-----------+--------- 18422 | ForKeyShare | customers | t 18501 | ForKeyShare | customers | t
FOR KEY SHARE is the weakest row lock Postgres has — it conflicts only with locks that would change or delete the row's key columns, not with other FOR KEY SHARE locks. So two inserts referencing the same parent row, neither touching the parent, coexist fine; ForKeyShare is explicitly non-conflicting with itself.
Where it actually blocks
The conflict shows up when something else takes a stronger lock on that same parent row: an UPDATE on the parent that touches any column (not just the key) takes FOR NO KEY UPDATE by default, which still conflicts with FOR KEY SHARE if the update happens to touch the key, but more commonly the real-world case is simpler and more common than key updates: a parent-row UPDATE or DELETE run inside a transaction that hasn't committed yet, while a concurrent INSERT into the child table is waiting on that same row's FK check lock. The child insert isn't blocked by a lock conflict in the usual "two writers, same row" sense — it's blocked because the FK trigger's FOR KEY SHARE acquisition itself waits for the parent UPDATE's stronger lock to release, exactly like any other lock wait:
-- session A BEGIN; UPDATE customers SET tier = 'gold' WHERE id = 42; -- holds row lock, doesn't commit yet -- session B, blocks here until A commits or rolls back INSERT INTO orders (customer_id, total) VALUES (42, 99.00);
session B's insert isn't slow because of orders at all — pg_stat_activity.wait_event = 'Lock' with wait_event_type = 'transactionid' or a tuple wait, and the blocking pid traces back to session A's still-open update on customers, a table session B's INSERT statement never mentions.
Finding it in a real deadlock graph
SELECT blocked.pid AS blocked_pid, blocking.pid AS blocking_pid, blocked_act.query AS blocked_query, blocking_act.query AS blocking_query FROM pg_locks blocked JOIN pg_locks blocking ON blocked.locktype = blocking.locktype AND blocked.relation = blocking.relation AND blocked.page IS NOT DISTINCT FROM blocking.page AND blocked.tuple IS NOT DISTINCT FROM blocking.tuple AND blocked.pid != blocking.pid JOIN pg_stat_activity blocked_act ON blocked_act.pid = blocked.pid JOIN pg_stat_activity blocking_act ON blocking_act.pid = blocking.pid WHERE NOT blocked.granted AND blocking.granted;
The blocking_query column is the giveaway: it will show an UPDATE/DELETE against the parent table, while the thing that looks stuck is an INSERT into the child — easy to misdiagnose as a child-table contention problem when the actual holder never touched the child table at all.
The fix is transaction shape, not indexes
An index on the child's FK column avoids a different, worse problem (a full table scan to check for orphans on parent delete), but it does nothing for this lock wait — the lock is on the parent row, not the child index. The actual fixes: keep transactions that update parent rows as short as possible and commit before doing unrelated work in the same transaction, and avoid wrapping a parent UPDATE together with slow, unrelated application logic in one open transaction — every child insert referencing that row queues up behind it for as long as it stays open.
Rule of thumb: if an
INSERTinto a child table is stuck andpg_locksshows it waiting on a tuple lock against the parent table it references, look for an open, uncommitted transaction touching that exact parent row — the FK check trigger'sFOR KEY SHARElock is waiting on it, not on anything in the child table itself.