0
0
PostgreSQLquery~10 mins

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

Choose your learning style9 modes available
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.