Introduction to PostgreSQL RAISE EXCEPTION
PostgreSQL raise exception is used to raise the statement for reporting the warnings, errors and other type of reported message within function or stored procedure. We are raising the exception in function and stored procedures in PostgreSQL, there are different level available of raise exception i.e. info, notice, warning, debug, log and notice. We can basically use raise exception statement for raise errors and report the messages, by default exception level is used in raise exception. We can also add the parameter and variable in the raise exception statement, also we can print the value of our variable.
Given below is the syntax:
RAISE [LEVEL] (Level which we have used with raise exception statement.) [FORMAT]
RAISE [ LEVEL] USING option (Raise statement using option) = expression
- RAISE: This is defined as exception statement which was used to raise the exception in PostgreSQL. We have basically pass two parameter with raise exception i.e. level and format. There are various parameter available to raise an error.
- LEVEL: Level in raise exception is defined as define the error severity. We can use level in raise exception i.e. log, notice, warning, info, debug and exception. Every level generates detailed information of error or warning message based on priority of levels. By default exception level is used with raise statement in PostgreSQL, log_min_messages and client_min_messages parameter will used to control the database server logging.
- FORMAT: This is defined as error message which we want to display. If our message needs some variable value then we need to use % sign. This sign acts as placeholder which replace the variable value by given command.
- expression: We can use multiple expression with raise expression statement in it. Expression is optional parameter which was used with raise expression statement.
- option: We can use options with raise exception statement in PostgreSQL.
How RAISE EXCEPTION work in PostgreSQL?
We can raise the exception by violating the data integrity constraints in PostgreSQL. Raise exception is basically used raise the error and report the messages.
Given below shows the raise statement level options which was specifying the error severity in PostgreSQL:
If we need to raise an error we need to use exception level after using the raise statement in it.
We can also add the more detailed information about the error by using following clause with the raise exception statement in it.
USING option = expression
We can also provide hint with error message which is used to find the root cause of error easier and it is possible to discover the error. Exception gives error in error code, we will identify error using the error code, it will define either a condition of SQL State code. When raise statement is not specifying the level then it will specify the printed message as error. After printing the error message currently running transaction is aborted and the next raise statement is not executed.
It is used in various parameters or options to produce the error message is more informative and readable. When we are not specifying any level then by default exception level is used in raise statement. Exception level is abort the current transaction with raise statement in it. Exception level is very useful and important in raise statement to abort the current transaction in it.
Examples of PostgreSQL RAISE EXCEPTION
Given below are the examples mentioned:
Raise exception statement which report different messages.
- Below example shows that raise exception statement which report different messages.
- Below example shows that raise statement which was used to report the different messages at current time stamp.
RAISE INFO 'Print the message of information %', now() ;
RAISE LOG 'Print the message of log %', now();
RAISE DEBUG 'Print the message of debug %', now();
RAISE WARNING 'Print the message of warning %', now();
RAISE NOTICE 'Print the message of notice %', now();
RAISE EXCEPTION 'Print the message of exception %', now();
In above example only info, warning, notice and exception will display the information. But debug and log will not display the output information.
Raise error using raise exception.
- Below example shows that raise the error using raise exception in PostgreSQL.
- We have added more detailed information by adding exception.
personal_email varchar(100) := 'email@example.com';
-- First check the user email id is correct as well as duplicate or not.
-- If user email id is duplicate then report mail as duplicate.
RAISE EXCEPTION 'Enter email is duplicate: %', personal_email
USING HINT = 'Check email and enter correct email ID of user';
Raise exception by creating function.
- Below example shows that raise exception in PostgreSQL by creating the function.
create or replace function test_exp() returns void as
raise exception using message = 'S 167', detail = 'D 167', hint = 'H 167', errcode = 'P3333';
$$ language plpgsql
Advantages of PostgreSQL RAISE EXCEPTION
Below are the advantages:
- The main advantage of raise exception is to raise the statement for reporting the warnings.
- We have used raise exception in various parameters.
- Raise exception have multiple level to raise the error and warning.
- Raise statement is used the level of exception to show warnings and error.
RAISE EXCEPTION in PostgreSQL is basically used to raise the warning and error message. It is very useful and important. There are six levels of raise exception is available in PostgreSQL i.e. notice, log, debug, warning info and exception. It is used in various parameters.
This is a guide to PostgreSQL RAISE EXCEPTION. Here we discuss how RAISE EXCEPTION work in PostgreSQL, advantages and examples. You may also have a look at the following articles to learn more –