43.9.1. Reporting Errors and Messages
Use the RAISE
statement to report messages and raise errors.
RAISE [level
] 'format
' [,expression
[, ... ]] [ USINGoption
=expression
[, ... ] ]; RAISE [level
]condition_name
[ USINGoption
=expression
[, ... ] ]; RAISE [level
] SQLSTATE 'sqlstate
' [ USINGoption
=expression
[, ... ] ]; RAISE [level
] USINGoption
=expression
[, ... ]; RAISE ;
The level
option specifies the error severity. Allowed levels are DEBUG
, LOG
, INFO
, NOTICE
, WARNING
, and EXCEPTION
, with EXCEPTION
being the default. EXCEPTION
raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels. Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the log_min_messages and client_min_messages configuration variables. See Chapter 20 for more information.
After level
if any, you can specify a format
string (which must be a simple string literal, not an expression). The format string specifies the error message text to be reported. The format string can be followed by optional argument expressions to be inserted into the message. Inside the format string, %
is replaced by the string representation of the next optional argument’s value. Write %%
to emit a literal %
. The number of arguments must match the number of %
placeholders in the format string, or an error is raised during the compilation of the function.
In this example, the value of v_job_id
will replace the %
in the string:
RAISE NOTICE 'Calling cs_create_job(%)', v_job_id;
You can attach additional information to the error report by writing USING
followed by option
= expression
items. Each expression
can be any string-valued expression. The allowed option
key words are:
MESSAGE
-
Sets the error message text. This option can’t be used in the form of
RAISE
that includes a format string beforeUSING
. DETAIL
-
Supplies an error detail message.
HINT
-
Supplies a hint message.
ERRCODE
-
Specifies the error code (SQLSTATE) to report, either by condition name, as shown in Appendix A, or directly as a five-character SQLSTATE code.
COLUMN
CONSTRAINT
DATATYPE
TABLE
SCHEMA
-
Supplies the name of a related object.
This example will abort the transaction with the given error message and hint:
RAISE EXCEPTION 'Nonexistent ID --> %', user_id USING HINT = 'Please check your user ID';
These two examples show equivalent ways of setting the SQLSTATE:
RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation'; RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
There is a second RAISE
syntax in which the main argument is the condition name or SQLSTATE to be reported, for example:
RAISE division_by_zero; RAISE SQLSTATE '22012';
In this syntax, USING
can be used to supply a custom error message, detail, or hint. Another way to do the earlier example is
RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
Still another variant is to write RAISE USING
or RAISE
and put everything else into the level
USINGUSING
list.
The last variant of RAISE
has no parameters at all. This form can only be used inside a BEGIN
block’s EXCEPTION
clause; it causes the error currently being handled to be re-thrown.
Note
Before PostgreSQL 9.1, RAISE
without parameters was interpreted as re-throwing the error from the block containing the active exception handler. Thus an EXCEPTION
clause nested within that handler could not catch it, even if the RAISE
was within the nested EXCEPTION
clause’s block. This was deemed surprising as well as being incompatible with Oracle’s PL/SQL.
If no condition name nor SQLSTATE is specified in a RAISE EXCEPTION
command, the default is to use ERRCODE_RAISE_EXCEPTION
(P0001
). If no message text is specified, the default is to use the condition name or SQLSTATE as message text.
Note
When specifying an error code by SQLSTATE code, you are not limited to the predefined error codes, but can select any error code consisting of five digits and/or upper-case ASCII letters, other than 00000
. It is recommended that you avoid throwing error codes that end in three zeroes, because these are category codes and can only be trapped by trapping the whole category.
43.9.2. Checking Assertions
The ASSERT
statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions.
ASSERTcondition
[ ,message
];
The condition
is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT
statement does nothing further. If the result is false or null, then an ASSERT_FAILURE
exception is raised. (If an error occurs while evaluating the condition
, it is reported as a normal error.)
If the optional message
is provided, it is an expression whose result (if not null) replaces the default error message text “assertion failed”, should the condition
fail. The message
expression is not evaluated in the normal case where the assertion succeeds.
Testing of assertions can be enabled or disabled via the configuration parameter plpgsql.check_asserts
, which takes a Boolean value; the default is on
. If this parameter is off
then ASSERT
statements do nothing.
Note that ASSERT
is meant for detecting program bugs, not for reporting ordinary error conditions. Use the RAISE
statement, described above, for that.
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.
Syntax:
Given below is the syntax:
RAISE [LEVEL] (Level which we have used with raise exception statement.) [FORMAT]
OR
RAISE [ LEVEL] USING option (Raise statement using option) = expression
OR
RAISE;
Parameters Description:
- 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:
- Notice
- Log
- Debug
- Warning
- Info
- Exception
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:
Example #1
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.
Code:
DO $$
BEGIN
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();
END $$;
Output:
In the above example, only info, warning, notice and exception will display the information. But debug and log will not display the output information.
Example #2
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.
Code:
DO $$
DECLARE
personal_email varchar(100) := 'raise@exceptions.com';
BEGIN
-- 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';
END $$;
Output:
Example #3
Raise exception by creating function.
- The below example shows that raise exception in PostgreSQL by creating the function.
Code:
create or replace function test_exp() returns void as
$$
begin
raise exception using message = 'S 167', detail = 'D 167', hint = 'H 167', errcode = 'P3333';
end;
$$ language plpgsql
;
Output:
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.
Conclusion
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.
Recommended Articles
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 –
- PostgreSQL ARRAY_AGG()
- PostgreSQL IF Statement
- PostgreSQL Auto Increment
- PostgreSQL replace
Summary: in this tutorial, you will learn how to report messages and raise errors using the raise
statement. In addition, you will learn how to use the assert
statement to insert debugging checks into PL/pgSQL blocks.
Reporting messages
To raise a message, you use the raise
statement as follows:
Code language: SQL (Structured Query Language) (sql)
raise level format;
Let’s examine the raise
statement in more detail.
Level
Following the raise
statement is the level
option that specifies the error severity.
PostgreSQL provides the following levels:
-
debug
-
log
-
notice
-
info
-
warning
-
exception
If you don’t specify the level
, by default, the raise
statement will use exception
level that raises an error and stops the current transaction. We will discuss the raise exception
later in the next section.
Format
The format
is a string that specifies the message. The format
uses percentage ( %
) placeholders that will be substituted by the arguments.
The number of placeholders must be the same as the number of arguments, otherwise, PostgreSQL will issue an error:
[Err] ERROR: too many parameters specified for raise
Code language: CSS (css)
The following example illustrates the raise
statement that reports different messages at the current time.
Code language: SQL (Structured Query Language) (sql)
do $$ begin raise info 'information message %', now() ; raise log 'log message %', now(); raise debug 'debug message %', now(); raise warning 'warning message %', now(); raise notice 'notice message %', now(); end $$;
Output:
Code language: SQL (Structured Query Language) (sql)
info: information message 2015-09-10 21:17:39.398+07 warning: warning message 2015-09-10 21:17:39.398+07 notice: notice message 2015-09-10 21:17:39.398+07
Notice that not all messages are reported back to the client. PostgreSQL only reports the info
, warning
, and notice
level messages back to the client. This is controlled by client_min_messages
and log_min_messages
configuration parameters.
Raising errors
To raise an error, you use the exception
level after the raise
statement. Note that raise
statement uses the exception
level by default.
Besides raising an error, you can add more information by using the following additional clause:
Code language: SQL (Structured Query Language) (sql)
using option = expression
The option
can be:
message
: set error messagehint
: provide the hint message so that the root cause of the error is easier to be discovered.detail
: give detailed information about the error.errcode
: identify the error code, which can be either by condition name or directly five-characterSQLSTATE
code. Please refer to the table of error codes and condition names.
The expression
is a string-valued expression. The following example raises a duplicate email error message:
Code language: SQL (Structured Query Language) (sql)
do $$ declare email varchar(255) := 'info@postgresqltutorial.com'; begin -- check email for duplicate -- ... -- report duplicate email raise exception 'duplicate email: %', email using hint = 'check the email again'; end $$;
Code language: SQL (Structured Query Language) (sql)
[Err] ERROR: Duplicate email: info@postgresqltutorial.com HINT: Check the email again
The following examples illustrate how to raise an SQLSTATE
and its corresponding condition:
Code language: SQL (Structured Query Language) (sql)
do $$ begin --... raise sqlstate '2210b'; end $$;
Code language: SQL (Structured Query Language) (sql)
do $$ begin --... raise invalid_regular_expression; end $$;
Now you can use raise
statement to either raise a message or report an error.
Was this tutorial helpful ?
In this article, we will look into the Errors in that are inbuilt in PostgreSQL and the process of raising an error in PostgreSQL through RAISE statement and to use the ASSERT statement to insert debugging checks into PL/pgSQL blocks.
To raise an error message user can implement the RAISE statement as follows:
Syntax: RAISE level format;
Let’s explore into the raise statement a bit more. Following the RAISE statement is the level option that specifies the error severity. PostgreSQL provides the following levels:
- DEBUG
- LOG
- NOTICE
- INFO
- WARNING
- EXCEPTION
If users don’t specify the level, by default, the RAISE statement will use the EXCEPTION level that raises an error and stops the current transaction. We will discuss the RAISE EXCEPTION later in the next section.
The format is a string that specifies the message. The format uses percentage ( %) placeholders that will be substituted by the next arguments. The number of placeholders must match the number of arguments, otherwise, PostgreSQL will report the following error message:
[Err] ERROR: too many parameters specified for RAISE
Example:
The following example illustrates the RAISE statement that reports different messages at the current time.
DO $$ BEGIN RAISE INFO 'information message %', now() ; RAISE LOG 'log message %', now(); RAISE DEBUG 'debug message %', now(); RAISE WARNING 'warning message %', now(); RAISE NOTICE 'notice message %', now(); END $$;
Output:
Note: Not all messages are reported back to the client, only INFO, WARNING, and NOTICE level messages are reported to the client. This is controlled by the client_min_messages and log_min_messages configuration parameters.
Raising Errors:
To raise errors, you use the EXCEPTION level after the RAISE statement. Note that the RAISE statement uses the EXCEPTION level by default. Besides raising an error, you can add more detailed information by using the following clause with the RAISE statement:
USING option = expression
The options can be any one of the below:
- MESSAGE: set error message text
- HINT: provide the hint message so that the root cause of the error is easier to be discovered.
- DETAIL: give detailed information about the error.
- ERRCODE: identify the error code, which can be either by condition name or directly five-character SQLSTATE code.
Example 1:
DO $$ DECLARE email varchar(255) := 'raju@geeksforgeeks.org'; BEGIN -- check email for duplicate -- ... -- report duplicate email RAISE EXCEPTION 'Duplicate email: %', email USING HINT = 'Check the email again'; END $$;
Output:
Example 2:
The following examples illustrate how to raise an SQLSTATE and its corresponding condition:
DO $$ BEGIN --... RAISE SQLSTATE '2210B'; END $$; DO $$ BEGIN --... RAISE invalid_regular_expression; END $$;
Output:
Summary: in this tutorial, you will learn how to report messages and raise errors using the raise
statement. In addition, you will learn how to use the assert
statement to insert debugging checks into PL/pgSQL blocks.
Reporting messages
To raise a message, you use the raise
statement as follows:
raise level format;
Let’s examine the raise
statement in more detail.
Level
Following the raise
statement is the level
option that specifies the error severity.
PostgreSQL provides the following levels:
-
debug
-
log
-
notice
-
info
-
warning
-
exception
If you don’t specify the level
, by default, the raise
statement will use exception
level that raises an error and stops the current transaction. We will discuss the raise exception
later in the next section.
Format
The format
is a string that specifies the message. The format
uses percentage ( %
) placeholders that will be substituted by the arguments.
The number of placeholders must be the same as the number of arguments, otherwise, PostgreSQL will issue an error:
[Err] ERROR: too many parameters specified for raise
The following example illustrates the raise
statement that reports different messages at the current time.
do $$
begin
raise info 'information message %', now() ;
raise log 'log message %', now();
raise debug 'debug message %', now();
raise warning 'warning message %', now();
raise notice 'notice message %', now();
end $$;
Output:
info: information message 2015-09-10 21:17:39.398+07
warning: warning message 2015-09-10 21:17:39.398+07
notice: notice message 2015-09-10 21:17:39.398+07
Notice that not all messages are reported back to the client. PostgreSQL only reports the info
, warning
, and notice
level messages back to the client. This is controlled by client_min_messages
and log_min_messages
configuration parameters.
Raising errors
To raise an error, you use the exception
level after the raise
statement. Note that raise
statement uses the exception
level by default.
Besides raising an error, you can add more information by using the following additional clause:
using option = expression
The option
can be:
message
: set error messagehint
: provide the hint message so that the root cause of the error is easier to be discovered.detail
: give detailed information about the error.errcode
: identify the error code, which can be either by condition name or directly five-characterSQLSTATE
code. Please refer to the table of error codes and condition names.
The expression
is a string-valued expression. The following example raises a duplicate email error message:
do $$
declare
email varchar(255) := 'info@reconshell.com';
begin
-- check email for duplicate
-- ...
-- report duplicate email
raise exception 'duplicate email: %', email
using hint = 'check the email again';
end $$;
[Err] ERROR: Duplicate email: info@reconshell.com
HINT: Check the email again
The following examples illustrate how to raise an SQLSTATE
and its corresponding condition:
do $$
begin
--...
raise sqlstate '2210b';
end $$;
do $$
begin
--...
raise invalid_regular_expression;
end $$;
Now you can use raise
statement to either raise a message or report an error.
This article is half-done without your Comment! *** Please share your thoughts via Comment ***
You can use the RAISE Statements for the report messages and raise errors.
Different level of RAISE statements are INFO, NOTICE, and EXCEPTION.
By default, NOTICE is always returning to the client only. We should use RAISE INFO for our internal query or function debugging.
We should break down our code into smaller parts and add RAISE statement with clock_timestamp().
We can compare the execution time difference between the code blocks and can find a slow running code block.
We can also add parameter and variable to the RAISE statement. We can print the current value of our parameter and variable.
Few examples:
RAISE INFO ‘Hello World !’; RAISE NOTICE ‘%’, variable_name; RAISE NOTICE ‘Current value of parameter (%)’, my_var; RAISE EXCEPTION ‘% cannot have null salary’, EmpName; |
One Practical Demonstration:
Create a table with Sample Data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
CREATE TABLE Employee ( EmpID SERIAL ,EmpName CHARACTER VARYING(50) ,Gender CHAR(1) ,AGE SMALLINT ); INSERT INTO Employee ( EmpName ,Gender ,AGE ) VALUES (‘Anvesh’,’M’,27) ,(‘Mohan’,’M’,30) ,(‘Roy’,’M’,31) ,(‘Meera’,’F’,27) ,(‘Richa’,’F’,26) ,(‘Martin’,’M’,35) ,(‘Mahesh’,’M’,38) ,(‘Paresh’,’M’,22) ,(‘Alina’,’F’,21) ,(‘Alex’,’M’,24); |
Sample function for Custome Paging with the use of RAISE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
CREATE OR REPLACE FUNCTION fn_GetEmployeeData ( Paging_PageSize INTEGER = NULL ,Paging_PageNumber INTEGER = NULL ) RETURNS TABLE ( outEmpID INTEGER ,outEmpName CHARACTER VARYING ,outGender CHAR(1) ,outAge SMALLINT ) AS $BODY$ DECLARE PageNumber BIGINT; DECLARE TempINT INTEGER; BEGIN /* *************************************************************** Construct Custom paging parameter… **************************************************************** */ IF (paging_pagesize IS NOT NULL AND paging_pagenumber IS NOT NULL) THEN PageNumber := (Paging_PageSize * (Paging_PageNumber-1)); END IF; RAISE INFO ‘%’,’Construction of Custom Paging Parameter — DONE ‘|| clock_timestamp(); /* ************************************************ Custome paging SQL Query construction……. ************************************************ */ TempINT := 10000; WHILE (TempINT > 0) LOOP TempINT = TempINT — 1; RAISE INFO ‘%’,’The current value of TempINT ‘ || TempINT; END LOOP; RETURN QUERY SELECT EmpID ,EmpName ,Gender ,Age FROM public.Employee ORDER BY EmpID LIMIT Paging_PageSize OFFSET PageNumber; RAISE INFO ‘%’,’Final result set of main query — DONE ‘ || clock_timestamp(); EXCEPTION WHEN OTHERS THEN RAISE; END; $BODY$ LANGUAGE ‘plpgsql’; |
Execute this function and check the query message window:
You will find list of RAISE INFO messages that we mentioned in above function.
SELECT *FROM public.fn_GetEmployeeData(4,2); |
Apr 14, 2018
42.8.1. Reporting Errors and Messages
Use the RAISE
statement to report messages and raise errors.
RAISE [level
] 'format
' [,expression
[, ... ]] [ USINGoption
=expression
[, ... ] ]; RAISE [level
]condition_name
[ USINGoption
=expression
[, ... ] ]; RAISE [level
] SQLSTATE 'sqlstate
' [ USINGoption
=expression
[, ... ] ]; RAISE [level
] USINGoption
=expression
[, ... ]; RAISE ;
The level
option specifies the error severity. Allowed levels are DEBUG
, LOG
, INFO
, NOTICE
, WARNING
, and EXCEPTION
, with EXCEPTION
being the default. EXCEPTION
raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels. Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the log_min_messages and client_min_messages configuration variables. See Chapter 19 for more information.
After level
if any, you can write a format
(which must be a simple string literal, not an expression). The format string specifies the error message text to be reported. The format string can be followed by optional argument expressions to be inserted into the message. Inside the format string, %
is replaced by the string representation of the next optional argument’s value. Write %%
to emit a literal %
. The number of arguments must match the number of %
placeholders in the format string, or an error is raised during the compilation of the function.
In this example, the value of v_job_id
will replace the %
in the string:
RAISE NOTICE 'Calling cs_create_job(%)', v_job_id;
You can attach additional information to the error report by writing USING
followed by option
= expression
items. Each expression
can be any string-valued expression. The allowed option
key words are:
-
MESSAGE
-
Sets the error message text. This option can’t be used in the form of
RAISE
that includes a format string beforeUSING
. -
DETAIL
-
Supplies an error detail message.
-
HINT
-
Supplies a hint message.
-
ERRCODE
-
Specifies the error code (SQLSTATE) to report, either by condition name, as shown in Appendix A, or directly as a five-character SQLSTATE code.
-
COLUMN
CONSTRAINT
DATATYPE
TABLE
SCHEMA
-
Supplies the name of a related object.
This example will abort the transaction with the given error message and hint:
RAISE EXCEPTION 'Nonexistent ID --> %', user_id USING HINT = 'Please check your user ID';
These two examples show equivalent ways of setting the SQLSTATE:
RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation'; RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
There is a second RAISE
syntax in which the main argument is the condition name or SQLSTATE to be reported, for example:
RAISE division_by_zero; RAISE SQLSTATE '22012';
In this syntax, USING
can be used to supply a custom error message, detail, or hint. Another way to do the earlier example is
RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
Still another variant is to write RAISE USING
or RAISE
and put everything else into the level
USINGUSING
list.
The last variant of RAISE
has no parameters at all. This form can only be used inside a BEGIN
block’s EXCEPTION
clause; it causes the error currently being handled to be re-thrown.
Note
Before PostgreSQL 9.1, RAISE
without parameters was interpreted as re-throwing the error from the block containing the active exception handler. Thus an EXCEPTION
clause nested within that handler could not catch it, even if the RAISE
was within the nested EXCEPTION
clause’s block. This was deemed surprising as well as being incompatible with Oracle’s PL/SQL.
If no condition name nor SQLSTATE is specified in a RAISE EXCEPTION
command, the default is to use ERRCODE_RAISE_EXCEPTION
(P0001
). If no message text is specified, the default is to use the condition name or SQLSTATE as message text.
Note
When specifying an error code by SQLSTATE code, you are not limited to the predefined error codes, but can select any error code consisting of five digits and/or upper-case ASCII letters, other than 00000
. It is recommended that you avoid throwing error codes that end in three zeroes, because these are category codes and can only be trapped by trapping the whole category.
42.8.2. Checking Assertions
The ASSERT
statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions.
ASSERTcondition
[ ,message
];
The condition
is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT
statement does nothing further. If the result is false or null, then an ASSERT_FAILURE
exception is raised. (If an error occurs while evaluating the condition
, it is reported as a normal error.)
If the optional message
is provided, it is an expression whose result (if not null) replaces the default error message text “assertion failed”, should the condition
fail. The message
expression is not evaluated in the normal case where the assertion succeeds.
Testing of assertions can be enabled or disabled via the configuration parameter plpgsql.check_asserts
, which takes a Boolean value; the default is on
. If this parameter is off
then ASSERT
statements do nothing.
Note that ASSERT
is meant for detecting program bugs, not for reporting ordinary error conditions. Use the RAISE
statement, described above, for that.