1

I want to run a script inside a transaction in PostgreSQL. So I surround the SQL code with begin and commit statements. But I want to rollback on error. I don't see how to do that.

BEGIN;
UPDATE public.tablename
SET blah = 'xxx'
WHERE thing= '123';

COMMIT;

2 Answers 2

1

I found the answer. The rollback will happen when an error occurs without me doing anything.

0

You set a savepoint and after the comand a Rollback

CREATE tABLE tablename (blah varchar(3), thing varchar(3))
BEGIN;
SAVEPOINT my_savepoint;
   UPDATE tablename
   SET blah = 'xxx'
   WHERE thing= '123';
ROLLBACK TO my_savepoint;
COMMIT;

db<>fiddle here

1
  • Yes: That's nice, I can test my script that way, Thank you. Commented Dec 1, 2020 at 16:05

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.