Skip to the solution
MediumDSA13 min read

What are COMMIT, ROLLBACK, and SAVEPOINT?

1.1k views

The problem

Question presented to candidate: "Walk me through COMMIT, ROLLBACK and SAVEPOINT. What exactly does each one do, and when would you use a savepoint instead of rolling back the whole transaction β€” for example, importing a batch of customers where a few rows are duplicates?"

What a strong answer should cover:

  • COMMIT makes every change of the transaction permanent and visible to others; ROLLBACK discards all of them and ends the transaction
  • SAVEPOINT name marks a point inside a transaction; ROLLBACK TO SAVEPOINT name undoes only the work after it and keeps the transaction (and the savepoint) alive; RELEASE SAVEPOINT name forgets the mark but keeps the work
  • Nothing is permanent until the outer COMMIT: rolling back the outer transaction also discards released savepoints
  • PostgreSQL aborts the whole transaction after any error ("current transaction is aborted"); a savepoint before the risky statement is how you recover and continue; PL/pgSQL EXCEPTION blocks and many ORMs' "nested transactions" are savepoints under the hood
  • Engine differences: SQLite allows SAVEPOINT outside BEGIN (it starts a transaction), PostgreSQL rejects it; COMMIT with no open transaction is an error in SQLite and a warning in PostgreSQL

Code / implementation expected: Yes β€” a transaction with a savepoint that rolls back one step and commits the rest.

Schema used by the examples:

  • transactions

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

13 min read Β· 12 sections

Run the code

Edit the query and run it against the tables for this question.

SQLCOMMIT keeps a transaction, ROLLBACK throws one away
SQLSAVEPOINT + ROLLBACK TO undoes one step, the rest is committed
SQLSQLite: nested savepoints, rolling back to an outer one, RELEASE of the outermost commits
PostgreSQL: a PL/pgSQL EXCEPTION block (an implicit savepoint) skips duplicate rows in an import
DO $$
DECLARE
  r record;
BEGIN
  FOR r IN
    SELECT * FROM (VALUES
      (4, 'Dara',    'dara@example.com'),
      (5, 'Alice B', 'alice@example.com'),
      (6, 'Farid',   'farid@example.com')
    ) AS v(id, name, email)
    ORDER BY id
  LOOP
    BEGIN
      INSERT INTO customers (id, name, email) VALUES (r.id, r.name, r.email);
    EXCEPTION WHEN unique_violation THEN
      RAISE NOTICE 'skipped %', r.email;
    END;
  END LOOP;
END
$$;
SELECT id, name, email FROM customers ORDER BY id;

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 25 of 82 in SQL

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All SQL questions
Up nextMedium

How do you aggregate values into a single string?

Continue