task Transactions · hard
Recover a failed stock allocation
The rejected batch cannot fit available stock: its item 2 request exceeds availability. Demonstrate recovery from a provisional reservation of 2 units of item 1 and a rejected audit entry, then apply only the accepted batch. Return item_id, available and reserved from the final stock table.
what to return
- Columns
item_idavailablereservedin this order- Rows
- 3 rows · in the order asked
- Watch for
- Use a savepoint to undo the provisional stock and audit work, then commit the accepted batch. Your answer is checked by looking at the resulting tables, without requiring particular SQL keywords.
- Every stock row starts with available=capacity and reserved=0. Preserve all capacities and the historical audit row (units=5).
- End with one accepted audit row whose units equal the accepted feed total, no rejected audit row, and each stock balance adjusted exactly once for its accepted quantity. Order by item_id.
- Skill
- Verify recovery by the final state of all affected tables, not transaction keywords. Refresher: Stage, validate & atomically promote a refresh →
expected result
Your query should return exactly these columns and values. Run it and compare.
expected result3 rows · in this order
| item_id | available | reserved |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 1 | 1 |
| 3 | 9 | 0 |
all 3 rows
data you can query
Sample rows from each table. Each column shows its type underneath; → marks a link to another table.
market_stock4 columns · 3 rows
| item_idinteger · key | capacityinteger | availableinteger | reservedinteger |
|---|---|---|---|
| 1 | 5 | 5 | 0 |
| 2 | 2 | 2 | 0 |
| 3 | 9 | 9 | 0 |
all 3 rows
market_allocation_feed3 columns · 4 rows
| batchtext · key | item_idinteger · key | quantityinteger |
|---|---|---|
| rejected | 1 | 2 |
| rejected | 2 | 3 |
| accepted | 1 | 1 |
| accepted | 2 | 1 |
all 4 rows
market_allocation_audit2 columns · 1 rows
| batchtext · key | unitsinteger |
|---|---|
| historical | 5 |
all 1 rows
solution.sql
Hints and the answer are always available. Using them counts the question as solved with help.