Bird
Raised Fist0
PostgreSQLquery~10 mins

RAISE for notices and exceptions in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - RAISE for notices and exceptions
Start PL/pgSQL block
Execute statements
RAISE statement encountered?
NoContinue execution
Yes
Check RAISE level: NOTICE, WARNING, EXCEPTION
NOTICE/WARNING: Show message, continue
EXCEPTION: Show error, stop execution
End block or handle exception
The flow shows how a RAISE statement in PostgreSQL PL/pgSQL triggers messages or errors, either continuing or stopping execution.
Execution Sample
PostgreSQL
DO $$
BEGIN
  RAISE NOTICE 'Step 1: Starting';
  IF TRUE THEN
    RAISE EXCEPTION 'Error occurred';
  END IF;
END $$;
This code raises a notice message, then raises an exception which stops execution.
Execution Table
StepActionRAISE LevelMessageExecution Continues?
1Start blockN/AN/AYes
2RAISE NOTICE 'Step 1: Starting'NOTICEStep 1: StartingYes
3Check IF condition TRUEN/AN/AYes
4RAISE EXCEPTION 'Error occurred'EXCEPTIONError occurredNo
5Block ends due to exceptionN/AN/ANo
💡 Execution stops at step 4 due to RAISE EXCEPTION.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Execution StateNot startedNotice shownCondition checkedException raisedStopped
Key Moments - 2 Insights
Why does execution stop after RAISE EXCEPTION but not after RAISE NOTICE?
RAISE NOTICE only shows a message and lets execution continue (see step 2 and 5 in execution_table). RAISE EXCEPTION raises an error that stops execution immediately (step 4).
Can RAISE WARNING stop execution like EXCEPTION?
No, RAISE WARNING behaves like NOTICE by showing a message but continuing execution. Only EXCEPTION stops execution as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what message is shown at step 2?
AError occurred
BStep 1: Starting
CCondition checked
DBlock ends
💡 Hint
Check the 'Message' column at step 2 in execution_table.
At which step does execution stop due to an error?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Execution Continues?' column in execution_table.
If we replace RAISE EXCEPTION with RAISE WARNING at step 4, what changes?
ANo message is shown
BExecution stops earlier
CExecution continues after step 4
DAn error is raised
💡 Hint
RAISE WARNING behaves like NOTICE and does not stop execution (see explanation in key_moments).
Concept Snapshot
RAISE statement syntax:
RAISE level 'message';
Levels: NOTICE, WARNING, EXCEPTION
NOTICE and WARNING show messages and continue execution.
EXCEPTION raises error and stops execution.
Use RAISE to debug or handle errors in PL/pgSQL.
Full Transcript
This visual execution trace shows how the RAISE statement works in PostgreSQL PL/pgSQL. When the block starts, it executes normally. At step 2, RAISE NOTICE outputs a message but lets execution continue. At step 4, RAISE EXCEPTION raises an error message and stops the block immediately. The variable tracker shows the execution state changing from started to stopped after the exception. Key moments clarify that only EXCEPTION stops execution, while NOTICE and WARNING do not. The quiz tests understanding of message output and execution flow. The snapshot summarizes syntax and behavior of RAISE levels.

Practice

(1/5)
1. What does the RAISE NOTICE statement do in PostgreSQL?
easy
A. It silently logs a message without showing it to the user.
B. It stops the code execution and returns an error.
C. It sends a message to the client but continues running the code.
D. It restarts the current transaction automatically.

Solution

  1. Step 1: Understand RAISE NOTICE behavior

    RAISE NOTICE sends an informational message to the client but does not stop the execution.
  2. Step 2: Compare with other RAISE levels

    Unlike EXCEPTION, which stops execution, NOTICE only shows a message and continues.
  3. Final Answer:

    It sends a message to the client but continues running the code. -> Option C
  4. Quick Check:

    RAISE NOTICE = message without stopping [OK]
Hint: NOTICE shows messages but never stops code [OK]
Common Mistakes:
  • Confusing NOTICE with EXCEPTION stopping code
  • Thinking NOTICE hides messages
  • Assuming NOTICE restarts transactions
2. Which of the following is the correct syntax to raise a warning message in a PostgreSQL function?
easy
A. RAISE WARNING 'This is a warning';
B. RAISE WARN 'This is a warning';
C. RAISE WARNNING 'This is a warning';
D. RAISE WARN_MSG 'This is a warning';

Solution

  1. Step 1: Recall correct RAISE syntax for warnings

    The correct keyword is WARNING, not WARN or other variants.
  2. Step 2: Validate syntax correctness

    RAISE WARNING 'message'; is the valid syntax to raise a warning message.
  3. Final Answer:

    RAISE WARNING 'This is a warning'; -> Option A
  4. Quick Check:

    RAISE WARNING = correct syntax [OK]
Hint: Use full word WARNING, not abbreviations [OK]
Common Mistakes:
  • Using 'WARN' instead of 'WARNING'
  • Misspelling WARNING as WARNNING
  • Inventing unsupported keywords like WARN_MSG
3. Consider this PostgreSQL PL/pgSQL block:
DO $$
BEGIN
  RAISE NOTICE 'Step 1';
  RAISE WARNING 'Step 2';
  RAISE EXCEPTION 'Step 3';
  RAISE NOTICE 'Step 4';
END $$;

What will be the output?
medium
A. Step 1 and Step 4 notices print, but warning and exception are ignored.
B. All four messages print: Step 1, Step 2, Step 3, Step 4.
C. Only Step 3 error message is shown, others are ignored.
D. Step 1 (notice), Step 2 (warning), then error 'Step 3', and stops before Step 4.

Solution

  1. Step 1: Trace the RAISE statements in order

    RAISE NOTICE 'Step 1' prints a message and continues.
  2. Step 2: Understand RAISE WARNING and EXCEPTION effects

    RAISE WARNING 'Step 2' prints a warning and continues. RAISE EXCEPTION 'Step 3' raises an error and stops execution immediately.
  3. Step 3: Check if Step 4 runs

    Since EXCEPTION stops execution, RAISE NOTICE 'Step 4' does not run.
  4. Final Answer:

    Step 1 (notice), Step 2 (warning), then error 'Step 3', and stops before Step 4. -> Option D
  5. Quick Check:

    EXCEPTION stops code after Step 3 [OK]
Hint: EXCEPTION stops code; notices/warnings do not [OK]
Common Mistakes:
  • Assuming all messages print despite EXCEPTION
  • Thinking WARNING stops execution
  • Ignoring that EXCEPTION halts code immediately
4. You wrote this code inside a PostgreSQL function:
RAISE EXCEPTION 'Error: %', message;

But it causes an error: "ERROR: column "message" does not exist". What is the likely problem?
medium
A. The variable 'message' is not declared or out of scope.
B. RAISE EXCEPTION cannot use placeholders like '%'.
C. You must use RAISE WARNING instead of EXCEPTION for variables.
D. The syntax requires double quotes around 'message'.

Solution

  1. Step 1: Understand the error message

    The error says column "message" does not exist, meaning 'message' is treated as a column or variable but is undefined.
  2. Step 2: Check variable declaration and scope

    In PL/pgSQL, variables must be declared before use. If 'message' is not declared or out of scope, this error occurs.
  3. Final Answer:

    The variable 'message' is not declared or out of scope. -> Option A
  4. Quick Check:

    Undefined variable causes this error [OK]
Hint: Declare variables before using in RAISE [OK]
Common Mistakes:
  • Thinking placeholders '%' are invalid
  • Using RAISE WARNING instead of EXCEPTION for variables
  • Adding quotes around variable names incorrectly
5. You want to write a PostgreSQL function that checks if a user age is below 18 and raises an exception with a custom message including the age. Which code snippet correctly does this?
hard
A. IF age < 18 THEN RAISE EXCEPTION 'User age % is too young' || age; END IF;
B. IF age < 18 THEN RAISE EXCEPTION 'User age % is too young', age; END IF;
C. IF age < 18 THEN RAISE EXCEPTION MESSAGE = 'User age ' || age || ' is too young'; END IF;
D. IF age < 18 THEN RAISE EXCEPTION 'User age % is too young', 'age'; END IF;

Solution

  1. Step 1: Understand RAISE EXCEPTION with placeholders

    RAISE EXCEPTION supports printf-style placeholders like % and variables as arguments.
  2. Step 2: Check each option's correctness

    IF age < 18 THEN RAISE EXCEPTION 'User age % is too young', age; END IF; correctly uses the % placeholder followed by the variable age as an argument.
    IF age < 18 THEN RAISE EXCEPTION 'User age % is too young' || age; END IF; incorrectly concatenates the variable to the format string, resulting in an unreplaced % and a runtime format error.
    IF age < 18 THEN RAISE EXCEPTION MESSAGE = 'User age ' || age || ' is too young'; END IF; omits the required USING keyword before MESSAGE=, causing a syntax error.
    IF age < 18 THEN RAISE EXCEPTION 'User age % is too young', 'age'; END IF; passes the string literal 'age' instead of the variable.
  3. Final Answer:

    IF age < 18 THEN RAISE EXCEPTION 'User age % is too young', age; END IF; -> Option B
  4. Quick Check:

    Use % and variable for dynamic messages [OK]
Hint: Use % placeholder with variable, not string 'age' [OK]
Common Mistakes:
  • Concatenating strings instead of using placeholders
  • Passing variable name as string instead of variable
  • Using incorrect RAISE EXCEPTION syntax