Definitioncore
Savepoint
Also called partial rollback.
A named point inside a transaction to which it can roll back, undoing later updates while keeping the earlier ones.
A savepoint is a named point inside a transaction to which the transaction can roll back, undoing the updates made after the savepoint and keeping the ones made before it while the transaction stays active; this is called a partial rollback. In a log-based system a savepoint is just the LSN of the transaction's newest log record at the time it is set, and a partial rollback is the usual rollback with compensation log records that stops at that LSN. A savepoint is not a checkpoint: a checkpoint is taken by the system for all transactions to make restart shorter, and ARIES recovery after a crash still rolls back every unfinished transaction completely, whatever savepoints it had. A savepoint is also a flat, sequential form of nested transactions: it can undo a suffix of the work, but the part after it is not a separate transaction with its own commit.
Definition
Let be an active transaction, and let be the LSN of its newest log record, or 0 if it has none.
Setting a savepoint. Setting savepoint stores in the memory of . ARIES calls this value the SaveLSN. Nothing is written to the log.
Rollback to . Let be the head of 's undo chain: the next record to undo. Repeat while :
- if record is an update, undo it, write a CLR with undoNextLSN equal to the update's prevLSN, and set to that prevLSN;
- if record is a CLR, set to its undoNextLSN.
Then goes on as an active transaction. A total rollback is the case , followed by an END record.
What the rollback keeps. After the rollback, the updates of that are in effect are exactly the uncompensated updates with LSN at most , the ones in effect when was set. The proof is the chain argument of the CLR page: the walk meets the uncompensated updates newest first, and the loop stops before the first one at or before .
Several savepoints. Savepoints of one transaction form a stack ordered by their LSNs. Rolling back to makes every savepoint set after invalid, because the work it marked is gone, and keeps itself, so that the transaction can roll back to again. Releasing removes and the savepoints after it, and keeps all updates. PostgreSQL behaves in this way. In the SQL standard a new savepoint with the name of an old one replaces it; PostgreSQL keeps the old one, and the name then refers to the newer one until it is released.
Locks. A partial rollback undoes data changes but does not by itself undo lock requests. ARIES Section 5.2 reports that DB2 keeps all locks after a partial rollback, while System R releases locks after it. Keeping them is always safe under strict two-phase locking, since the locks are released at the end of the transaction in any case.
Examples and non-examples
A rollback to a savepoint, and a later abort. writes , sets savepoint , writes and , and rolls back to . So .
| LSN | record | prevLSN | undoNextLSN |
|---|---|---|---|
| 1 | update | 0 | |
| 2 | update | 1 | |
| 3 | update | 2 | |
| 4 | CLR for | 3 | 2 |
| 5 | CLR for | 4 | 1 |
| 6 | update | 5 |
The rollback starts at , writes CLR 4 and sets , writes CLR 5 and sets , and stops because . then writes , whose prevLSN is the CLR 5. If now aborts, it undoes (CLR 7, undoNextLSN 5), jumps from CLR 5 to LSN 1, undoes (CLR 8, undoNextLSN 0) and ends. It does not undo or a second time.
Statement atomicity. An SQL statement that changes many rows must have no effect if it fails half way, for example on a unique-key violation at the 500th row. DB2 sets a savepoint before each SQL statement that changes data, and on an error rolls back to it, so the transaction loses only that statement. ARIES Section 5.2 describes this use.
A retry in application code. In PostgreSQL:
BEGIN;
INSERT INTO orders VALUES (1, 'book');
SAVEPOINT before_stock;
UPDATE stock SET n = n - 1 WHERE item = 'book';
ROLLBACK TO SAVEPOINT before_stock;
UPDATE stock SET n = n - 1 WHERE item = 'pen';
COMMIT;
The commit keeps the insert and the second update. The first update has no effect.
Non-example: a checkpoint. A checkpoint writes the system's tables to the log and is not visible to transactions. No transaction can roll back to it.
Non-example: a crash. Savepoints are kept in memory. After a crash, restart rolls back each unfinished transaction totally; it does not stop at the transaction's newest savepoint.
Non-example: a nested subtransaction. In the nested transaction model a child can run in parallel with its siblings, commits to its parent, and can hold its own locks. The work after a savepoint has none of these; it is part of the one transaction, and only the order of the log records separates it.
Related definitions
- A compensation log record is written for each update that a partial rollback undoes.
- A log sequence number is the value that a savepoint stores.
- ARIES recovery was designed with partial rollback as a goal, and its restart treats the CLRs of a partial rollback like any other CLRs.
- Nested transactions generalize savepoints to a tree of subtransactions.
- Atomicity is the property that a total rollback gives, and statement atomicity is the same property for one statement, given by a savepoint.
References
- C. Mohan, Don Haderle, Bruce Lindsay, Hamid Pirahesh and Peter Schwarz, ARIES, a transaction recovery method supporting fine-granularity locking and partial rollbacks using write-ahead logging, ACM Transactions on Database Systems 17(1), 1992, pages 94 to 162, Section 5.2, Total or Partial Rollbacks (SaveLSN, a savepoint before each SQL statement in DB2, one CLR per undone record, Figure 3, and the release of locks after a partial rollback)
- PostgreSQL documentation, SAVEPOINT, the description and the compatibility notes on savepoints with the same name
- PostgreSQL documentation, ROLLBACK TO SAVEPOINT, the description (later savepoints are destroyed, the named one stays)
- PostgreSQL documentation, RELEASE SAVEPOINT, the description (the savepoint is removed and the changes after it are kept)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 0JQNYQ Edit this page History Report an error Markdown