I have tableA called Order
It has a PK id several columns and a bool column named active and a column tableid. Two orders cant be active at the same time for a tableid that is ensured with a two column unique contraint (&& active=true) that works like an index and finding active orders is pretty fast.
The problem is that there is another table orderitems. I want active to be true unless all orderitems for the order-id are marked as paid=true..
Using serialization transactions this can be achieved i think in payment code by setting an update query if all items are paid. I think that this wont work always.Because if they run concurrently they might both see that there are unpaid items(due to old snapshot) but when commit they would pay all items,but not update active column.(different) .
Adding new items and payment transaction tries to set active=false wont be a problem with serial transactions because one of them would fail..
I think triggers are a solution but i dont know what to do exactly.. Thank you for reading
SERIALIZABLEmode I think it might be possible if you're meticulous about always doing aSELECT ... FOR UPDATEon the Order whenINSERTing a new order item. You have to do theSELECT ... FOR UPDATEbecause there's still no predicate locking; it can't know that the newly inserted order item breaks serializability when another transaction is looking at other orderitems with the same order ID. For that reason, I'd recommend doing it in a trigger.