You cannot use ROLLBACK in PL/pgSQL, except in certain limited cases inside procedures.

You don't need to explicitly roll back in your PL/pgSQL code. Just let the exception propagate out of the PL/pgSQL code, and it will cause an error, which will cause the whole transaction to be rolled back.

Your comments suggest that this code is called from an SQL script. Then the solution would be to have a COMMIT in that SQL script at some place after the PL/pgSQL code. That would end the transaction and roll it back.

Answer from Laurenz Albe on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › postgresql › postgresql-rollback
PostgreSQL - ROLLBACK - GeeksforGeeks
July 23, 2025 - Explanation: in this example, the changes are reverted to the original state after the ROLLBACK command. PostgreSQL ROLLBACK command is to undo changes made during a transaction.
🌐
PostgreSQL
postgresql.org › docs › 9.4 › sql-rollback.html
PostgreSQL: Documentation: 9.4: ROLLBACK
February 13, 2020 - This documentation is for an unsupported version of PostgreSQL. You may want to view the same page for the current version, or one of the other supported versions listed above instead. ... ROLLBACK rolls back the current transaction and causes all the updates made by the transaction to be discarded.
🌐
PostgreSQL
postgresql.org › docs › current › sql-rollback-to.html
PostgreSQL: Documentation: 18: ROLLBACK TO SAVEPOINT
May 14, 2026 - BEGIN; DECLARE foo CURSOR FOR SELECT 1 UNION SELECT 2; SAVEPOINT foo; FETCH 1 FROM foo; ?column? ---------- 1 ROLLBACK TO SAVEPOINT foo; FETCH 1 FROM foo; ?column? ---------- 2 COMMIT; The SQL standard specifies that the key word SAVEPOINT is mandatory, but PostgreSQL and Oracle allow it to be omitted.
🌐
Bytebase
bytebase.com › blog › guides › postgres rollback explained
Postgres Rollback Explained | Bytebase
September 4, 2025 - BEGIN; -- Step 1: safe operations ... 'Marketing'); -- Oops, Bob is actually in Sales -- Roll back only the risky step ROLLBACK TO SAVEPOINT sp_batch; -- Step 3: continue with corrected operation INSERT INTO employees (name, department) ...
🌐
GitHub
github.com › acakojic › postgresql-learning › blob › main › transactions › 1_4_postgresql_rollback_in_transaction.md
postgresql-learning/transactions/1_4_postgresql_rollback_in_transaction.md at main · acakojic/postgresql-learning
Now, let's improve this with error handling and ROLLBACK. DO $$ -- PostgreSQL anonymous code block DECLARE v_error BOOLEAN := FALSE; BEGIN -- Start the transaction BEGIN; -- Debit Account 1 UPDATE accounts SET balance = balance - 100 WHERE id = 1; IF NOT FOUND THEN RAISE 'Account 1 not found'; END IF; -- Credit Account 2 UPDATE accounts SET balance = balance + 100 WHERE id = 2; IF NOT FOUND THEN RAISE 'Account 2 not found'; END IF; -- If we reach this point, it means no error has occurred COMMIT; EXCEPTION WHEN OTHERS THEN -- An error occurred, so rollback the transaction ROLLBACK; v_error := TRUE; END; $$;
Author   acakojic
🌐
Heroku Dev Center
devcenter.heroku.com › articles › heroku-postgres-rollback
Heroku Postgres Rollback | Heroku Dev Center
May 27, 2026 - Before rolling back your database, check the earliest your can roll back to with data:pg:info and look for the Rollback field: $ heroku data:pg:info HEROKU_POSTGRESQL_COBALT === postgresql-rugged-24211 as HEROKU_POSTGRESQL_COBALT_URL on ⬢ example-app ...
🌐
PostgreSQL
postgresql.org › docs › current › tutorial-transactions.html
PostgreSQL: Documentation: 18: 3.4. Transactions
May 14, 2026 - = 'Bob'; -- oops ... forget that and use Wally's account ROLLBACK TO my_savepoint; UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Wally'; COMMIT; This example is, of course, oversimplified, but there's a lot of control possible ...
🌐
EnterpriseDB
enterprisedb.com › postgres-tutorials › how-work-postgresql-transactions
How to work with PostgreSQL transactions | EDB
postgres=# begin; BEGIN postgres=# create table my_table(n int); CREATE TABLE postgres=# insert into my_table values (1); INSERT 0 1 postgres=# savepoint my_savepoint; SAVEPOINT postgres=# insert into my_table values(2); INSERT 0 1 postgres=# insert into my_table values(3); INSERT 0 1 postgres=# select * from my_table; n --- 1 2 3 (3 rows) postgres=# rollback to my_savepoint; ROLLBACK postgres=# select * from my_table; n --- 1 (1 row) postgres=# commit; COMMIT postgres=#
Find elsewhere
🌐
Neon
neon.com › postgresql › postgresql-tutorial › postgresql-transaction
PostgreSQL Transaction
For example, the following example uses the ROLLBACK statement to roll back the changes made to the account 1: -- start a transaction BEGIN; UPDATE accounts SET balance = balance - 1000 WHERE id = 1; -- rollback the changes ROLLBACK; If you retrieve ...
🌐
OpenSourceDB
opensource-db.com › home › blog › mastering postgresql: rollback to savepoints !!
Mastering PostgreSQL: Rollback to Savepoints !! - OpenSourceDB
November 22, 2023 - To grasp how to implement rollback to savepoint in PostgreSQL, let’s consider a practical example with our favorite two tables “osdb_dept” and “osdb_emp“, with “osdb_dept” serving as the parent table and linked to “osdb_emp” child table, through the “dept_id” column.
🌐
Compile N Run
compilenrun.com › postgresql tutorial › postgresql transactions › postgresql rollback
PostgreSQL ROLLBACK | Compile N Run
... BEGIN; INSERT INTO customers (first_name, last_name, email) VALUES ('John', 'Doe', '[email protected]'); -- Oh wait, we made a mistake! ROLLBACK; In this example, the INSERT operation is cancelled and no new record is added to the customers ...
🌐
PostgreSQL
postgresql.org › docs › current › sql-rollback.html
ROLLBACK - PostgreSQL: Documentation: 18
May 14, 2026 - ROLLBACK ROLLBACK — abort the current transaction Synopsis ROLLBACK [ WORK | TRANSACTION ] [ AND [ NO ] CHAIN …
🌐
Medium
medium.com › nerd-for-tech › transaction-in-postgresql-rollback-savepoint-23987dc6c533
Transaction in PostgreSQL | ROLLBACK | SAVEPOINT | by Sohaib Anser | Nerd For Tech | Medium
July 1, 2021 - Now in the transaction, after 100 gems deduction from David, a savepoint will be marked. Actually, David’s gems will be transferred to Joe but accidentally transferred to John. Next, a Rollback will be established to the marked savepoint, ...
🌐
Devart
docs.devart.com › dotconnect › postgresql › Devart.Data.PostgreSql~Devart.Data.PostgreSql.PgSqlTransaction~Rollback().html
Rollback() Method - Documentation - Devart
public void RunPgSqlTransaction(string myConnString) { PgSqlConnection pgConnection = new PgSqlConnection(myConnString); pgConnection.Open(); PgSqlCommand pgCommand = new PgSqlCommand(); PgSqlTransaction myTrans; // Start a local transaction myTrans = pgConnection.BeginTransaction(); // Assign transaction object for a pending local transaction pgCommand.Transaction = myTrans; try { pgCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(50, 'DEVELOPMENT')"; pgCommand.ExecuteNonQuery(); pgCommand.CommandText = "INSERT INTO Test.Dept(DeptNo, DName) Values(60, 'PRODUCTION')"; pgCommand.ExecuteNonQuery(); myTrans.Commit(); Console.WriteLine("Both records are written to database."); } catch(Exception e) { myTrans.Rollback(); Console.WriteLine(e.ToString()); Console.WriteLine("Neither record was written to database."); } finally { pgConnection.Close(); } }
🌐
TutorialsPoint
tutorialspoint.com › postgresql › postgresql_rollback.htm
PostgreSQL - ROLLBACK
Suppose the payment fails, the stock should not be reduced. ROLLBACK ensures the stock is restored if the transaction is incomplete. Now, please connect to PostgreSQL − COMMIT chapters where we entered the values of one new user.
🌐
PostgreSQL
postgresql.org › docs › 9.1 › sql-rollback.html
PostgreSQL: Documentation: 9.1: ROLLBACK
October 27, 2016 - This documentation is for an unsupported version of PostgreSQL. You may want to view the same page for the current version, or one of the other supported versions listed above instead. ... ROLLBACK rolls back the current transaction and causes all the updates made by the transaction to be discarded.
🌐
TutorialsPoint
tutorialspoint.com › postgresql › postgresql_transactions.htm
PostgreSQL - TRANSACTIONS
But a transaction will also ROLLBACK if the database is closed or if an error occurs.
🌐
PostgreSQL
postgresql.org › docs › 9.4 › sql-rollback-to.html
PostgreSQL: Documentation: 9.4: ROLLBACK TO SAVEPOINT
February 13, 2020 - BEGIN; DECLARE foo CURSOR FOR SELECT 1 UNION SELECT 2; SAVEPOINT foo; FETCH 1 FROM foo; ?column? ---------- 1 ROLLBACK TO SAVEPOINT foo; FETCH 1 FROM foo; ?column? ---------- 2 COMMIT; The SQL standard specifies that the key word SAVEPOINT is mandatory, but PostgreSQL and Oracle allow it to be omitted.