What are PostgreSQL savepoints? Undo one part of a transaction
PostgreSQL savepoints mark a place inside an open transaction that you can return to while keeping earlier changes. An inventory reservation shows where to put the mark and what each rollback does to the rows.
Which reservation can the warehouse keep?
A warehouse has eight blue lamps and five red ones. A customer asks the warehouse to set aside two blue lamps and, if possible, three red ones. The blue pair can ship even if the customer revises the red quantity during checkout. The clerk still wants both final quantities recorded together. That last condition matters. Two separate transactions would allow the blue reservation to become permanent before the clerk knows which red quantity to enter.
Imagine a table called stock with one row per colour, a unique sku and an integer column called units. Start a transaction with BEGIN. Subtract two from units on the blue row, then issue SAVEPOINT try_red. The working quantities in this session are now six blue and five red. Nothing about issuing SAVEPOINT changes either number. Its position records a choice the warehouse has made about the order. Rolling back to this point will keep the blue reservation and undo any later red reservation.
Follow the two rows through a rollback
With try_red in place, run UPDATE stock SET units = units - 3 WHERE sku = 'red'. A query in the same session now shows six blue and two red. Suppose the customer calls back and asks for just one red lamp. Run ROLLBACK TO SAVEPOINT try_red and read the rows again. Blue still reads six because its update came before the mark. Red reads five because its update came after it. Subtract one from the red row and the two quantities become six and four.
The number to subtract on the second attempt is one. Subtracting the difference between the two requests would produce the wrong quantity because the rollback already restored all three red lamps. This is a useful place for a test assertion. Check both rows immediately after the rollback, before sending the replacement update. Otherwise a mistake in the placement of the savepoint can look like a mistake in the arithmetic.
Release the mark, then decide whether to commit
After the customer confirms one red lamp, RELEASE SAVEPOINT try_red removes the mark. The outstanding transaction still contains both reservations. A COMMIT at this point makes their final quantities visible together to a new transaction. A full ROLLBACK instead discards both reservations. If the original stock rows already existed in a committed transaction, a fresh read after that full rollback finds eight blue and five red again.
Releasing the mark is handy when the code that handles red lamps returns to a caller responsible for the whole order. The caller can still cancel an order with two perfectly valid reservation lines. It also explains why sending a confirmation to the customer immediately after RELEASE would be premature. The outer transaction still has a decision to make. If your test checks only what the current connection reads after RELEASE, it has checked the pending quantities. Open a new transaction after the actual commit to check the result a later reader would see.
Recover from a rejected quantity
Give units a check constraint that requires a value of zero or greater. With five red lamps on hand, an attempt to subtract seven violates the constraint. PostgreSQL reports an error, and the transaction needs recovery before it can process an ordinary query. If try_red was established before this update, ROLLBACK TO SAVEPOINT try_red lets the session continue with the earlier blue reservation intact. The target savepoint remains available, so a replacement attempt can use the same recovery point.
There is a different case worth testing with the same order form. A misspelled sku can make an UPDATE affect zero rows without violating the quantity constraint. The database has no reason to raise that particular error. Application code that requires one matching stock row needs to inspect the affected row count or use RETURNING to see which row it changed. A savepoint gives the program somewhere to return when it detects a bad result. The program still has to recognize that result.
Give each recovery point a job
A clerk who must record a reason for every quantity change might add a note after reserving the red lamps. Suppose the transaction creates a second savepoint called try_note just before inserting that note. Rolling back to try_note removes the attempted note and keeps the red reservation. Rolling back to the earlier try_red removes the red reservation, the note, and the newer try_note mark. A later attempt to refer to try_note will fail unless the program creates it again.
Names such as try_red and try_note make a log of SQL statements easier to read. The name describes which work can be discarded. For an order that the customer accepts only as a complete set, there may be no useful intermediate decision at all. Let the entire transaction roll back if either colour is unavailable. The flexibility in the first example comes from the customer's delivery preference. A database feature alone cannot tell the warehouse whether shipping only the blue lamps is acceptable.
In practice
Place the savepoint immediately before the first change you are prepared to undo. Read the rows on either side of that mark in the same transaction, then decide whether the remaining work belongs in a single commit.
Open-source projects that use PostgreSQL savepoints
No project in our catalogue matches this concept yet.
Sources
Was this useful?