CHAPTER 22
Intermediate
Triggers and Event Automation
Updated: May 16, 2026
7 min read
# CHAPTER 22
Triggers and Event Automation
1. Introduction
Imagine a security requirement: Every single time a user's salary is updated, you must log the old salary, the new salary, and the timestamp into anaudit_log table. You *could* write PHP code to do this. But what if a developer logs into pgAdmin and manually runs an UPDATE query? The PHP code is bypassed, and the change is never logged.
To guarantee an action occurs 100% of the time, we use Triggers. A Trigger is a tripwire set directly on a table. When an event (INSERT, UPDATE, DELETE) occurs, the Trigger fires automatically.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the concept of database Event Automation.
-
Differentiate between
BEFOREandAFTERtriggers.
- Create a special PL/pgSQL Trigger Function.
-
Access the
NEWandOLDdata payloads.
- Bind the Trigger Function to a physical table.
3. The Two Parts of a PostgreSQL Trigger
Unlike MySQL (where you write the logic directly into the trigger definition), PostgreSQL strictly separates the architecture into two steps:- 1. The Trigger Function: You write a PL/pgSQL function containing the logic.
- 2. The Trigger Binding: You attach that function to a specific table event (like "AFTER UPDATE on users").
4. Step 1: The Trigger Function (The Logic)
Let's build our HR Audit Logger. When a salary changes, we want to record it. In a Trigger Function, PostgreSQL magically provides two special variables:-
NEW: Contains the brand new data being inserted or updated.
-
OLD: Contains the previous data that is about to be overwritten or deleted.
sql
5. Step 2: The Trigger Binding (The Tripwire)
Now we must attach our tripwire to theemployees table. We want it to fire AFTER an UPDATE occurs.
sql
*(Now, no matter who updates the table—PHP, Python, or an admin in pgAdmin—the tripwire fires, and the audit log is perfectly maintained!)*
6. BEFORE vs AFTER Triggers
-
AFTERTriggers: Best for logging and auditing. They fire *after* the data has been safely saved to the hard drive.
-
BEFORETriggers: Best for validation and data modification. They fire *before* the data hits the hard drive. You can actually intercept theNEWdata, alter it, and pass it along!
7. Intercepting Data with a BEFORE Trigger
Suppose users keep signing up with messy lowercase/uppercase names like "jOhN". We want the database to automatically capitalize the first letter, silently, before saving it.
sql
8. Mini Project: Prevent Accidental Deletions
Let's build a trigger that makes it physically impossible toDELETE a user. If a DELETE is attempted, we intercept it, cancel the deletion, and instead mark the user as is_active = FALSE. (This is called a "Soft Delete").
sql
9. Common Mistakes
-
Infinite Loops: If you have an
AFTER UPDATEtrigger on theuserstable, and the logic inside that trigger executes anUPDATEon theuserstable... it fires the trigger again, which fires the trigger again! The database will freeze and crash. Never update the table that fired the trigger!
-
Forgetting
FOR EACH ROW: If you do not specifyFOR EACH ROW, the trigger becomes a "Statement-Level" trigger. If anUPDATEmodifies 50 rows, the trigger only fires ONE time for the entire batch (and you cannot use theNEW/OLDvariables!).
10. Best Practices
-
Triggers are "Hidden Magic": Use them sparingly. If a new developer joins the team and tests an
INSERTcommand, and suddenly 5 other tables are magically updated, they will be very confused because the Trigger is invisible to the application code. Always document your triggers extensively.
11. Exercises
-
1.
In a Trigger Function, what does the
OLDvariable contain during anINSERToperation? (Hint: Think logically).
-
2.
Write the syntax to physically remove the
triggerpreventdeletetrigger from theuserstable. (Hint: useDROP TRIGGER...)
12. SQL Challenges
Write aBEFORE INSERT trigger function for a products table. If the NEW.price is less than 0, the trigger should automatically force the NEW.price to be 0.00 before saving it.
sql
13. MCQ Quiz with Answers
Question 1
In PostgreSQL, creating a Trigger is a two-step process. What are the two steps?
Question 2
During an UPDATE operation, you want to compare the original data with the incoming modified data. Which two system variables allow you to do this inside the Trigger Function?
14. Interview Questions
-
Q: Explain the fundamental difference between a
BEFOREtrigger and anAFTERtrigger. In what scenario MUST you use aBEFOREtrigger?
-
Q: What is the difference between a Row-Level trigger (
FOR EACH ROW) and a Statement-Level trigger?
15. FAQs
Q: Can I manually execute a Trigger Function usingCALL?
A: No. A function defined as RETURNS TRIGGER can *only* be executed automatically by the PostgreSQL event system via a bound Trigger.
16. Summary
Triggers are the ultimate enforcement mechanism for business logic. By utilizingBEFORE and AFTER tripwires, you can automate complex audit logging, enforce dynamic data validation, and create self-healing tables that operate flawlessly regardless of how the frontend application behaves.