I'm trying to clearly understand how foreign key constraints work. I still need some help.
The PostgreSQL documentation says:
So if my foreign key constraint is: table A b_id references b(id) and if table B already has an try for id = 5 and I do an insert into table A with b_id of 5 how does the database ensure that the entry in table B will still be there by the time the transaction ends? e.g. if there is an insert into A and a delete from b of id = 5, if the delete happens first, then the insert should fail. If the insert happens first, then the delete should fail. But how is this accomplished? Looking at the documentation above, I would expect the insert into A to get a Row exclusive lock for table A. And, I'm guessing it would get an ACCESS SHARE lock for table B. But this would not prevent the delete from B from happening at the same time (if I am reading this correctly). Can someone help me out here? Thank you, Perry |