Suppose I have this append-only
SQL table representing deposits/withdraws from/to accounts. The 3 rows below will render the balance of account A to be -$10
ID | Account | Credit |
---|---|---|
1 | A | $100 |
2 | A | -$50 |
3 | A | -$60 |
If the inserts are concurrent, how can I implement validation that prevents the total balance of accounts from becoming negative?
I understand that the validation depends on the aggregated values of the existing entries, and since none of the existing entries will be updated, the traditional transaction protection provided by the isolation levels no longer works. This seems to be a generic problem that affects all append-only tables. But, I could not find any discussions about the problem.
My questions are
- Is this a real problem?
- If it is, what are the generic solutions to this kind of problem.