Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we use rollback in sql explicitly?

Tags:

sql

postgresql

I'm using PostgreSQL 9.3

I have one misunderstanding about transactions and how they work. Suppose we wrapped some SQL operator within a transaction like the following:

BEGIN;
   insert into tbl (name, val) VALUES('John', 'Doe');
   insert into tbl (name, val) VALUES('John', 'Doee');
COMMIT;

If something goes wrong the transaction will automatically be rolled back. Taking that into account I can't get when should we use ROLLBACK explicitly? Could you get an example when it's necessary?

like image 340
St.Antario Avatar asked Dec 02 '14 08:12

St.Antario


People also ask

Why do we use ROLLBACK in SQL?

ROLLBACK is a transactional control language in SQL. It lets a user undo those transactions that aren't saved yet in the database. One can make use of this command if they wish to undo any changes or alterations since the execution of the last COMMIT.

When should ROLLBACK be used?

ROLLBACK in SQL is a transactional control language that is used to undo the transactions that have not been saved in the database. The command is only been used to undo changes since the last COMMIT.

What is ROLLBACK operation and why it is required?

A rollback is the operation of restoring a database to a previous state by canceling a specific transaction or transaction set. Rollbacks are either performed automatically by database systems or manually by users.

What is the purpose ROLLBACK?

Use the ROLLBACK statement to undo work done in the current transaction or to manually undo the work done by an in-doubt distributed transaction. Note: Oracle recommends that you explicitly end transactions in application programs using either a COMMIT or ROLLBACK statement.


2 Answers

In PostgreSQL the transaction is not automatically rolled back on error.

It is set to the aborted state, where further commands will fail with an error until you roll the transaction back.

Observe:

regress=> BEGIN;
BEGIN
regress=> LOCK TABLE nosuchtable;
ERROR:  relation "nosuchtable" does not exist
regress=> SELECT 1;
ERROR:  current transaction is aborted, commands ignored until end of transaction block
regress=> ROLLBACK;
ROLLBACK

This is important, because it prevents you from accidentally executing half a transaction. Imagine if PostgreSQL automatically rolled back, allowing new implicit transactions to occur, and you tried to run the following sequence of statements:

BEGIN;
INSERT INTO archive_table SELECT * FROM current_tabble;
DELETE FROM current_table;
COMMIT;

PostgreSQL will abort the transaction when it sees the typo current_tabble. So the DELETE will never happen - all statements get ignored after the error, and the COMMIT is treated as a ROLLBACK for an aborted transaction:

regress=> BEGIN;
BEGIN
regress=> SELECT typo;
ERROR:  column "typo" does not exist
regress=> COMMIT;
ROLLBACK

If it instead automatically rolled the transaction back, it'd be like you ran:

BEGIN;
INSERT INTO archive_table SELECT * FROM current_tabble;
ROLLBACK; -- automatic
BEGIN; -- automatic
DELETE FROM current_table;
COMMIT; -- automatic

... which, needless to say, would probably make you quite upset.

like image 96
Craig Ringer Avatar answered Nov 15 '22 09:11

Craig Ringer


Other uses for explicit ROLLBACK are manual modification and test cases:

  • Do some changes to the data (UPDATE, DELETE ...).
  • Run SELECT statements to check results of data modification.
  • Do ROLLBACK if results are not as expected.

In Postgres DB you can do this even with DDL statements (CREATE TABLE, ...)

like image 24
Ihor Romanchenko Avatar answered Nov 15 '22 08:11

Ihor Romanchenko