Introduction to PostgreSQL RAISE EXCEPTION
PostgreSQL raises an exception is used to raise the statement for reporting the warnings, errors and other type of reported message within a 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 the raise exception statement to raise errors and report the messages; by default, the exception level is used in the raise exception. We can also add the parameter and variable in the raise exception statement; also, we can print our variable’s value.
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 an exception statement that was used to raise the exception in PostgreSQL. We have basically pass two parameters with raise exception, i.e. level and format. There is various parameter available to raise an error.
- LEVEL: Level in raise exception is defined as defining the error severity. We can use level in raise exception, i.e. log, notice, warning, info, debug and exception. Every level generates detailed information about the error or warning message based on the priority of levels. By default, the exception level is used with raise statement in PostgreSQL, log_min_messages and client_min_messages parameter will be used to control the database server logging.
- FORMAT: This is defined as an error message which we want to display. If our message needs some variable value, then we need to use the % sign. This sign acts as a placeholder that replaces the variable value with a given command.
- expression: We can use multiple expression with raise expression statement in it. The expression is an optional parameter that was used with the 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 to 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 the exception level after using the raise statement in it.
We can also add more detailed information about the error by using the following clause with the raise exception statement in it.
USING option = expression
We can also provide an error message that is used to find the root cause of the error easier, and it is possible to discover the error. The exception gives an error in the error code; we will identify the error using the error code; it will define either a SQL State code condition. When the raise statement does not specify the level, it will specify the printed message as an 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 an error message that is more informative and readable. When we are not specifying any level, then by default, the exception level is used in the raise statement. The exception level is aborted the current transaction with a raise statement in it. The exception level is very useful and important to raise the statement to abort the current transaction in it.
Examples of PostgreSQL RAISE EXCEPTION
Given below are the examples mentioned:
Raise an exception statement, which reports different messages.
- The below example shows the raise exception statement, which reports different messages.
- The below example shows the raise statement, which was used to report the different messages at the 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 the 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.
- The below example shows that raise the error using raise exception in PostgreSQL.
- We have added more detailed information by adding an exception.
personal_email varchar(100) := '[email protected]';
-- 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.
- The 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 to have multiple levels 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 to RAISE EXCEPTION work in PostgreSQL, its advantages and examples. You may also have a look at the following articles to learn more –