0

i'm writing a postgresql function implementing a transaction. this how the code looks like:

BEGIN TRANSACTION REPEATABLE READ
INSERT INTO table1 VALUES(value1, value2);
INSERT INTO table2 VALUES(value3);
COMMIT
END;

Now i need to check if one of the insert query return some kind of error for insert rollback. How can i do it?

4
  • 3
    You do not need to check for errors. Transaction will automatically rollback, if one of the commands throws error. Commented Jul 30, 2013 at 8:03
  • So i can call rollback just in a explicit case (i.e i call a select query and return value is greater than a constant)? Commented Jul 30, 2013 at 8:11
  • Yes. But explicit rollback is rarely used. Most of the time it is better to throw an error, that will cause rollback. Such error will clearly state the cause of rollback. Commented Jul 30, 2013 at 8:35
  • Transaction control cannot be used inside a postgresql function. See among others: Commit, savepoint, rollback to in PostgreSQL? Commented Jul 30, 2013 at 11:16

1 Answer 1

0

Checking for, and handling errors, is good. Do it consistently. While transactions will automatically roll back, you may want to alert the user log the problem, or take other actions. This being said the transaction will roll back on its own if there is a database error.

On the other hand, if you need to roll back a transaction because of an application error (outside the database), you issue a ROLLBACK command.

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.