runtime
60 / 88

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_idavailablereserved in 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_idavailablereserved
141
211
390

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 · keycapacityintegeravailableintegerreservedinteger
1550
2220
3990

all 3 rows

market_allocation_feed3 columns · 4 rows
batchtext · keyitem_idinteger · keyquantityinteger
rejected12
rejected23
accepted11
accepted21

all 4 rows

market_allocation_audit2 columns · 1 rows
batchtext · keyunitsinteger
historical5

all 1 rows

solution.sql
 

Hints and the answer are always available. Using them counts the question as solved with help.