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
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
-
Yes: That's nice, I can test my script that way, Thank you.Peter Marshall– Peter Marshall2020年12月01日 16:05:32 +00:00Commented Dec 1, 2020 at 16:05
lang-sql