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.
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.
I had this error too when I tried to run my script after it raised an exception:
ERROR: current transaction is aborted, commands ignored until end of transaction block
To fix it, I simply added `ROLLBACK` at the beginning of my script, like this:
ROLLBACK; -- fix "current transaction is aborted, commands ignored until end of transaction block"
BEGIN;
DO
$$
DECLARE
v_nb INTEGER := 1;
BEGIN
RAISE NOTICE '0 + % = %', v_nb, v_nb;
RAISE EXCEPTION 'trigger an exception for fun';
END
$$;
COMMIT;