0

I'm a bit new to transaction in postgresql. I read many tutorials in it but what i want to do is that... If i have many queries for example:

INSERT INTO a (id) VALUES (1)
INSERT INTO a (id) VALUES (2)
INSERT INTO a (id) VALUES (3)
INSERT INTO a (id) VALUES (4)
....

if it failed in the 3rd or any nth query because of some issue, what i want is to rollback all the other previous queries automatically.

My question is, is there any way to do that in postgresql?

1
  • What client tool/library are you using to connect to PostgreSQL? Different tools use different method. Commented May 22, 2014 at 16:41

1 Answer 1

3

If you have a transaction, PostgreSQL will automatically ROLLBACK the whole transaction if any query fails. It doesn't matter if you submit ROLLBACK or COMMIT, rollback will follow. Additionally, any query submited will end with SQL exception.

It's a strict view on transaction concept: all or nothing.

If you don't want such behaviour, you need to use savepoints or plpgsql blocks with EXCEPTION handling.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.