Module lessons (3/4)
Introduction to Triggers
Inspect incoming NEW and outgoing OLD rows with BEFORE and AFTER trigger functions.
What you'll learn in this lesson
- Write special signed Trigger Functions with no parameters that return
RETURNS TRIGGER. - Inspect and modify intercepted rows using
NEWandOLDcontextual records. - Block illegal changes from inside the engine by launching
RAISE EXCEPTION '...'.
Overview
A Trigger is a reactive rule that fires automatically when a table experiences a INSERT, UPDATE, or DELETE. However, before you can hook it to the table, PostgreSQL requires you to define a special Trigger Function in PL/pgSQL without formal parameters which returns RETURNS TRIGGER.
Each BEFORE or AFTER trigger runs in the same transaction and under the same row-lock as the DML command that activated it: if the trigger launches RAISE EXCEPTION, the entire statement and any changes to it are atomically canceled!
Sections covered in this lesson
- 11. Mental Model: What
NEW,OLDandRETURNContain - 22. Progressive & Annotated Examples
- 33. Interactive Laboratory: Atomicity and Lock Around Triggers
- 44. Diagnostic Verification (Quiz)
- 55. Progressive Practical Laboratory
Unlock "Introduction to Triggers" and the full SQL Course curriculum
Inspect incoming NEW and outgoing OLD rows with BEFORE and AFTER trigger functions.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in SQL Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.