Enabling `ON_ERROR_STOP` doesn't work with `RAISE` statement in PostgreSQL

87 views Asked by At

First, I enabled ON_ERROR_STOP to stop the execution immediately after error as shown below:

\set ON_ERROR_STOP on

Because the doc about ON_ERROR_STOP says below:

By default, command processing continues after an error. When this variable is set to on, processing will instead stop immediately

So, I created test table with num column, then inserted the row whose num is 0 as shown below:

CREATE TABLE test (
  num INTEGER
);

INSERT INTO test (num) VALUES (0);

Then, I ran the script which raises INFO error with RAISE statement then, increments num in test table by 1 as shown below. *I also tried RAISE statement with DEBUG, LOG, NOTICE, WARNING, EXCEPTION or nothing:

DO LANGUAGE plpgsql $$
BEGIN
  RAISE INFO 'A custom error!';
  UPDATE test SET num = num + 1;
END
$$;

But, num was incremented to 1 as shown below. *num was not incremented to 1 only with EXCEPTION or nothing:

postgres=# SELECT num FROM test;
 num
-----
   1
(1 row)

So, how can I make ON_ERROR_STOP work with RAISE statement properly?

0

There are 0 answers