typestar

Savepoints in SQL

A savepoint is a transaction inside a transaction, so one step can fail alone.

BEGIN;

INSERT INTO tags (name)
VALUES ('imported');

SAVEPOINT risky_part;

UPDATE accounts
SET balance = balance - 100
WHERE id = 1;

ROLLBACK TO risky_part;

RELEASE risky_part;

COMMIT;

How it works

  1. SAVEPOINT name marks a point you can roll back to.
  2. ROLLBACK TO name undoes the work since the mark and keeps the transaction.
  3. RELEASE name accepts that work and drops the mark.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
189
Tokens
38
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 24 seconds.

Type this snippet

Step 2 of 5 in Transactions, step 12 of 17 in Plans & performance.

← Previous Next →