Report messages and raise errors

Use the RAISE statement to report messages and raise errors.

The statement syntax follows:

RAISE level 'format' [, identifier [...]];
Inside format, the percent character (%) is used as a placeholder for a subsequent, comma-separated identifier. You can specify more than one % and identifier pair, as follows:
RAISE NOTICE 'Welcome % %', firstname, lastname;

In this example, the notice message substitutes the value of firstname for the first % character, and substitutes the value of lastname for the second % character.

The message levels are as follows:
  • DEBUG messages are written only to pg.log.
  • NOTICE messages are written to the database log and forwarded to the client application.
  • EXCEPTION messages are written to the database log, forwarded to the client application as non-fatal messages, and usually abort the transaction if they are not caught.
The following is an example of a RAISE statement:
RAISE NOTICE 'Calling cs_create_job(%)', job_id;
In the example, job_id replaces the % in the string and display the message to the client and in pg.log.
RAISE EXCEPTION 'Inexistent ID --> %', user_id;

This EXCEPTION statement aborts the transaction (if the exception is not caught) and writes the message to the database log.