An AFTER trigger runs after a database action like insert, update, or delete finishes. It helps you do extra work once the main change is done.
AFTER trigger behavior in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
CREATE TRIGGER trigger_name AFTER INSERT OR UPDATE OR DELETE ON table_name FOR EACH ROW EXECUTE FUNCTION function_name();
AFTER triggers run after the main action completes successfully.
You can define AFTER triggers for INSERT, UPDATE, or DELETE actions.
CREATE TRIGGER log_after_insert AFTER INSERT ON employees FOR EACH ROW EXECUTE FUNCTION log_employee_insert();
CREATE TRIGGER audit_after_update AFTER UPDATE ON orders FOR EACH ROW EXECUTE FUNCTION audit_order_update();
CREATE TRIGGER notify_after_delete AFTER DELETE ON products FOR EACH ROW EXECUTE FUNCTION notify_product_delete();
This example creates two tables: employees and employee_logs. It defines an AFTER INSERT trigger on employees that adds a log entry after a new employee is added. Then it inserts one employee and shows the log.
CREATE TABLE employees ( id SERIAL PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE employee_logs ( log_id SERIAL PRIMARY KEY, employee_id INT, action TEXT, log_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE OR REPLACE FUNCTION log_employee_insert() RETURNS TRIGGER AS $$ BEGIN INSERT INTO employee_logs(employee_id, action) VALUES (NEW.id, 'INSERT'); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER after_employee_insert AFTER INSERT ON employees FOR EACH ROW EXECUTE FUNCTION log_employee_insert(); INSERT INTO employees(name) VALUES ('Alice'); SELECT * FROM employee_logs;
AFTER triggers cannot modify the row being inserted or updated because the main action is already done.
If the AFTER trigger fails, the whole transaction rolls back, so use them carefully.
AFTER triggers run after the main database action completes successfully.
They are useful for logging, notifications, and related updates.
You define them with CREATE TRIGGER and link to a function that runs after insert, update, or delete.
Practice
AFTER trigger in PostgreSQL?Solution
Step 1: Understand the timing of AFTER triggers
AFTER triggers run only after the main database action (INSERT, UPDATE, DELETE) has completed successfully.Step 2: Identify the purpose of AFTER triggers
They are used to perform actions like logging or notifications after the main operation finishes.Final Answer:
To execute a function after the main database operation completes successfully -> Option AQuick Check:
AFTER trigger = runs after operation [OK]
- Confusing AFTER with BEFORE triggers
- Thinking AFTER triggers can stop the main operation
- Assuming AFTER triggers run before the operation
Solution
Step 1: Recall the correct CREATE TRIGGER syntax
PostgreSQL requires specifying FOR EACH ROW or FOR EACH STATEMENT for triggers.Step 2: Identify the correct syntax for AFTER INSERT
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_name(); correctly uses AFTER INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_name();Final Answer:
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_name(); -> Option AQuick Check:
AFTER INSERT + FOR EACH ROW + EXECUTE FUNCTION = CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_name(); [OK]
- Omitting FOR EACH ROW or FOR EACH STATEMENT
- Using BEFORE instead of AFTER
- Using EXECUTE PROCEDURE instead of EXECUTE FUNCTION in modern PostgreSQL
CREATE TABLE users(id SERIAL PRIMARY KEY, name TEXT);
CREATE FUNCTION log_insert() RETURNS trigger AS $$
BEGIN
RAISE NOTICE 'Inserted user: %', NEW.name;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER after_user_insert AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION log_insert();
INSERT INTO users(name) VALUES ('Alice');Solution
Step 1: Understand the trigger function behavior
The function raises a NOTICE with the inserted user's name after insert.Step 2: Recognize AFTER trigger effects
AFTER triggers run after the insert, so the notice will be shown, and the insert completes successfully.Final Answer:
A notice message: 'Inserted user: Alice' -> Option DQuick Check:
AFTER trigger raises notice = A notice message: 'Inserted user: Alice' [OK]
- Thinking RETURN NEW is invalid in AFTER triggers
- Expecting no output from the trigger
- Assuming the insert is rolled back
Solution
Step 1: Understand when AFTER UPDATE triggers fire
AFTER UPDATE triggers fire only if the UPDATE actually changes data.Step 2: Analyze the cause of no trigger firing
If the UPDATE sets columns to their existing values, no actual change occurs, so the trigger does not fire.Final Answer:
The UPDATE statement does not change any column values -> Option BQuick Check:
UPDATE with no change = no AFTER trigger fire [OK]
- Assuming triggers fire even if no data changes
- Confusing BEFORE and AFTER triggers
- Not matching trigger function with trigger type
Solution
Step 1: Understand the requirement for conditional logging after delete
We want to log only after the delete happens and only for rows with status 'active'.Step 2: Choose the correct trigger timing and condition
AFTER DELETE trigger can access OLD row data and conditionally log if OLD.status = 'active'.Final Answer:
Create an AFTER DELETE trigger that checks if OLD.status = 'active' inside the trigger function before logging -> Option CQuick Check:
Conditional logging after delete = AFTER trigger with check [OK]
- Using BEFORE trigger which runs before deletion
- Logging all deletes without condition
- Trying to prevent deletion instead of logging
