Goal: prove commit, rollback, and partial rollback with visible before/after values.
Connect as beer_db. This lesson’s separate warehouses fixture starts with 350 kegs across two warehouses.
In the connection command, -U beer_db selects the SQL role and -d beer_db selects the database. Expect the psql welcome message and a beer_db=> prompt; run the following SQL there.
Goal: record the starting stock before changing it.
Look at the warehouses table to see the starting keg counts before any transfers.
Expect Amber Brewery 150 and Golden Brewery 200, totaling 350. The lab supplies this separate fixture; brewery names identify the two rows to update.
Goal: observe a temporary write and then undo the whole transaction.
Start a transaction, update a keg count, verify the change within your session, then ROLLBACK to undo it. Verify the count returned to its original value.
BEGIN starts a transaction; your own session sees its writes before commit. Expect Golden 150 inside the transaction, then Golden 200 after ROLLBACK, while Amber stays 150. UPDATE 1 is an affected-row count, not proof the write was committed.
Goal: commit both halves of one transfer together.
Move 50 kegs from Golden to Amber between BEGIN and COMMIT. Check both values before and after COMMIT: Golden = 150, Amber = 200, total = 350. The live probe uses a separate connection, so completion also confirms the committed values are visible there.
COMMIT makes both writes durable for subsequent connections. Each update should report UPDATE 1; removing stock and adding stock belong to the same transaction so the total stays 350.
Goal: recover from a failed transaction without retaining its earlier write.
After the successful transfer, try moving 10 more kegs. Deliberately set Amber to -1 in the second write: the fixture rejects negative stock. A SELECT then reports an aborted transaction. ROLLBACK restores both committed values: Golden 150 and Amber 200.
The first update succeeds within the transaction, but the negative-stock CHECK fails the second. Until ROLLBACK, PostgreSQL rejects further ordinary commands, including the deliberate SELECT. Both errors are expected. After rollback, Golden must be 150, not 140; that proves the earlier subtraction was undone too.
Goal: undo only work performed after a named savepoint.
Inside a transaction, insert a test style, set a SAVEPOINT, insert a second test style, then ROLLBACK TO SAVEPOINT to undo only the second insert. COMMIT to save only the first style, then verify and clean up.
after_a is a checkpoint label inside this transaction, not a saved database copy. ROLLBACK TO SAVEPOINT after_a removes B while leaving A available to commit. The verification must show only TX Style A; cleanup must return that name and DELETE 1. numrange supplies the required numeric ranges for the test styles.