A Savepoint Inside a Transaction

A savepoint marks a position an application can roll back to while keeping earlier work inside the same uncommitted transaction.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an optional step
  2. Verify final commit separately
  3. Keep earlier valid work while undoing a local change
  4. Choose which failures permit local recovery

Trace an optional step

A transaction records a completed import summary, sets a savepoint, and tries attaching a supplementary record. On a supported rollback to that point it can keep the earlier work within the transaction. Partial success supplies the application decision.

Verify final commit separately

Earlier work still depends on the outer transaction committing. Beginning and committing describe that broader boundary.[1]

Keep earlier valid work while undoing a local change

A bulk operation can accept record A, set a savepoint, attempt record B and roll back to the savepoint if B fails. It can then process record C and decide whether to commit the enclosing transaction. The savepoint itself does not publish anything. See batch commit contracts.

Begin → accept A → set savepoint
B rejected → rollback to savepoint
Accept C → commit
Durable rows under chosen policy: A and C

Choose which failures permit local recovery

Some errors require rolling back the full transaction. Check database semantics after an error. Test one wanted recovery and an error that should stop everything.

References

  1. PostgreSQL Global Development Group, Concurrency Control ↩