TxTransactions Lab
GitHub
I · Build a transaction

Reserve the last item.

Two customers see one bicycle. Move the availability decision into the same atomic operation as the stock change.

You will: combine a conditional update, a reservation row and database constraints. Prerequisites: atomicity and rollback. Runtime: real PostgreSQL, one connection. The lost-update investigation controls competing customers; the next MVCC lab explains which row version a reader sees.

Concept diagram · before the experiment

Put the availability test inside the protected update

With one bicycle, a separate read can let two clients both decide yes. A conditional update holds write protection while checking stock and decrementing it. Only the request that updates a row creates a reservation, in the same transaction.
Read the diagram: A CHECK on stock prevents negative values; it does not connect stock to a particular reservation. The conditional write and reservation insert must share a transaction. Under contention, follow the database’s isolation rules and retry a rejected whole decision. Open full diagram

Predict

If both requests read stock = 1 before either writes, why might both applications decide they can reserve it? Can a CHECK on nonnegative stock enforce the identity and existence of every reservation?

Inspect the fixture

The page prepares one bicycle in stock and an empty reservations table. Select Run SQL in SQL 01 and inspect the bike’s stock and the reservation row count.

Preparing PostgreSQL…
SQL 01
Ctrl / ⌘ + Enter

The CHECK enforces nonnegative stock. The reservation primary key identifies a request, and its foreign key requires an existing product. These are complementary rules.

Commit the reservation and stock together

Select Run SQL in SQL 02. Compare the row returned by RETURNING with the bicycle’s current stock.

SQL 02
Ctrl / ⌘ + Enter

The returned row is the evidence that this request obtained stock. An empty RETURNING result means no reservation was made; it is not a successful checkout.

Try the second customer

Select Run SQL in SQL 03 on the same database. Check how many rows RETURNING gives request-b and inspect inventory_conserved.

SQL 03
Ctrl / ⌘ + Enter

The second request changes no row. This sequential run establishes the conditional statement's behavior, not contention. Use the shared-server exercise to observe two clients updating the same row.

Change one thing

Select Reset database above SQL 01 and wait for Ready. In SQL 02 and SQL 03, delete AND stock > 0. Select Run SQL in SQL 02, then SQL 03. The second attempt should show a CHECK error. Run the unchanged SQL 01 again to see the remaining stock and which request obtained a reservation. Compare this failure with the original zero-row result.

Hint 1 · Check the returned row

Do not infer success just because SQL execution completed. Count the reservation rows and inspect the UPDATE's RETURNING result.

Hint 2 · Think about retries

A stable request ID prevents a duplicate reservation row, but an application must return the original result on retry. It must not decrement stock and then silently ignore a duplicate INSERT.

Why this result occurs

The data-modifying CTE passes a product to INSERT only when a decrement succeeds. Both effects share the transaction. Exactly one of the sequential requests obtains the bicycle. Under PostgreSQL Read Committed, a competing updater waits and reevaluates the predicate against the updated row; other isolation levels can require a transaction retry. A cross-row invariant needs more than this row-local predicate.

Under the hood: one predicate, one protected update

The UPDATE does not grant a reservation just because an application previously read stock = 1. It locates a candidate tuple, obtains the needed row protection and applies the availability predicate as part of the write operation. At PostgreSQL Read Committed, an updater that waited for another updater can recheck its predicate against that transaction's updated row version. A snapshot/serializable execution can instead need a retry.

The RETURNING row connects the successful decrement to the reservation INSERT inside the same transaction. The reservation's unique index enforces request identity; the foreign-key path enforces a referenced product. A retry that decrements stock and then ignores a duplicate reservation INSERT would violate the intended accounting even though uniqueness itself held.

Inspect version visibility, implement lock compatibility, then explain how these mechanisms support the statement-level behavior observed here.

Transfer the idea

Reserve a bicycle and helmet as one bundle. Describe how you will undo a partial reservation if the helmet is unavailable. Then specify a consistent lock order and a stable request identity. Run the real-server extension.

Success criteria: use RETURNING and reservation rows to identify the winning request; verify stock + reservations = 1; distinguish an empty result from a constraint error and state which isolation failures require a whole-transaction retry.

Primary references: Read Committed and data-modifying WITH.

Your design record

Explain what happened and cite the run or SQL result that supports it.

Write your design notes

Download notes and evidence

Downloads include current editor text. Run evidence stays on this page until you leave.