Reverter até um savepoint

Transações e Tratamento de Erros no PostgreSQL

Jason Myers

Principal Engineer

Sem savepoints

BEGIN TRANSACTION;

UPDATE cost = 2.33 WHERE name = 'Linga';

UPDATE cost = 500 WHERE name = 'Macaron';

ROLLBACK;
Transações e Tratamento de Erros no PostgreSQL

Usando savepoints e rollbacks direcionados

BEGIN TRANSACTION;

UPDATE cost = 2.33 WHERE name = 'Linga';

SAVEPOINT oops;
UPDATE cost = 500 WHERE name = 'Macaron';
ROLLBACK TO oops;
Transações e Tratamento de Erros no PostgreSQL

Liberando um savepoint quando terminar

BEGIN TRANSACTION;

UPDATE cost = 2.33 WHERE name = 'Linga';
UPDATE cost = 2.33 WHERE name = 'Petit-Beurre';
UPDATE cost = 2.33 WHERE name = 'Rosette';

SAVEPOINT oops;

UPDATE cost = 5.00 WHERE name = 'Macaron';
UPDATE cost = 3.50 WHERE name = 'Panellets';
RELEASE SAVEPOINT oops;
Transações e Tratamento de Erros no PostgreSQL

Dois pontos críticos sobre rollbacks e savepoints

  • ROLLBACK sem TO reverte toda a transação
  • ROLLBACK TO ___, onde ___ não é um savepoint válido, gera erro
Transações e Tratamento de Erros no PostgreSQL

Vamos praticar!

Transações e Tratamento de Erros no PostgreSQL

Preparing Video For Download...