Cookbook recipe

Transactions, savepoints, and rollback

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

You want part of a transaction to fail and recover without aborting everything. Savepoints give you partial rollback. Diagnose it Use a savepoint inside a transaction: BEGIN; INSERT INTO t ...; SAVEPOINT sp1; INSERT INTO t…

Investigation Path

You want part of a transaction to fail and recover without aborting everything. Savepoints give you partial rollback.

Diagnose it

Use a savepoint inside a transaction:

BEGIN;
  INSERT INTO t ...;
  SAVEPOINT sp1;
  INSERT INTO t ...;  -- fails
  ROLLBACK TO sp1;   -- undo just that
COMMIT;

Why it happens

A transaction is all-or-nothing by default. A SAVEPOINT marks a point you can roll back to, undoing later statements while keeping earlier ones — useful for retry logic inside one transaction.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes