0

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?

6
  • 1) From Reporting Errors: EXCEPTION raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels. 2) It works for me if I use RAISE EXCEPTION. Commented Jan 7, 2024 at 0:10
  • @AdrianKlaver So, enabling ON_ERROR_STOP doesn't affect to the behaviour of INFO, DEBUG, LOG, NOTICE and WARNING errors? I mean enabling ON_ERROR_STOP doesn't stop INFO, DEBUG, LOG, NOTICE and WARNING errors immediately after error? Commented Jan 7, 2024 at 0:20
  • Yes, because as the docs state they are not errors they are just messages. Commented Jan 7, 2024 at 0:23
  • @AdrianKlaver I thought INFO, DEBUG, LOG, NOTICE and WARNING were errors because the doc says The level option specifies the error severity. Commented Jan 7, 2024 at 0:31
  • Yes but EXCEPTION is the only one that actually raises an error. Commented Jan 7, 2024 at 0:36

0

Your Answer

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.