capypad
0 day streak
sql / expert
Snippet

Granular Transaction Recovery with Savepoints

Savepoints provide sub-transaction control. They allow developers to mark points within a transaction to which they can selectively roll back, preserving the work done before the savepoint while discarding failed sub-operations.

snippet.sql
sql
1
2
3
4
SAVEPOINT pre_batch;
UPDATE inventory SET qty = qty - 5 WHERE id = 10;
-- Conditional logic follows
ROLLBACK TO SAVEPOINT pre_batch;
Breakdown
1
SAVEPOINT pre_batch
Creates a named marker in the current transaction stream.
2
ROLLBACK TO SAVEPOINT pre_batch
Reverts all changes made after the marker without terminating the overall transaction.