I am seeing some odd locking behaviour when deleting a parent record (Postgres 9.4.4).
Setup:create table pk_rel (id int primary key);
create table fk_rel (pk_id int references pk_rel (id), x text unique);
insert into pk_rel values (1), (2);
insert into fk_rel values (1, 'a');
This example works as I expected.
Session 1:
=>begin;
=>update fk_rel set pk_id = 2;
Session 2:
=>delete from pk_rel where id = 1;
=>begin;
=>update fk_rel set pk_id = 2;
Session 2:
=>delete from pk_rel where id = 1;
[Fails with FK violation]
But the following case, I do not understand.
=>begin;
=>update fk_rel set x = 'b';
=>delete from pk_rel where id = 1;
=>insert into fk_rel values (1, 'a');