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;
Erwin Brandstetter
186k28 gold badges463 silver badges636 bronze badges
asked Dec 1, 2020 at 15:49

2 Answers 2

1

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

armitage
1,4292 gold badges14 silver badges20 bronze badges
answered Dec 1, 2020 at 16:04
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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.