0
0
PostgreSQLquery~10 mins

AFTER trigger behavior in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - AFTER trigger behavior
DML Statement Executed
Row(s) Modified in Table
AFTER Trigger Fires
Trigger Function Executes
Transaction Continues or Commits
After a data change (INSERT, UPDATE, DELETE), the AFTER trigger runs its function once the row modification is done.
Execution Sample
PostgreSQL
CREATE TRIGGER trg_after_insert
AFTER INSERT ON employees
FOR EACH ROW
EXECUTE FUNCTION log_insert();
This trigger runs the log_insert() function after each new row is inserted into employees.
Execution Table
StepActionTrigger TimingTrigger Fired?Trigger Function Result
1INSERT new employee rowBefore row changeNoNo trigger yet
2Row inserted into employees tableRow change doneNoRow data inserted
3AFTER INSERT trigger firesAfter row changeYeslog_insert() runs, logs insertion
4Trigger function completesAfter triggerYesLog entry created
5Transaction continuesAfter triggerNoReady for commit or next operation
💡 Trigger fires only after the row is inserted, ensuring the row modification is complete before trigger logic runs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
new_rownullContains new employee dataInserted in tableAvailable to trigger functionUnchanged after trigger
log_tableemptyemptyemptyOne new log entry addedContains log entry
Key Moments - 2 Insights
Why does the AFTER trigger run only after the row is inserted?
Because AFTER triggers execute only once the data modification is complete, as shown in execution_table step 3, ensuring the trigger sees the final data state.
Can the AFTER trigger modify the row that was just inserted?
No, AFTER triggers cannot change the row data that caused them because the row is already written; this is clear from step 2 and 3 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the AFTER trigger function run?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Check the 'Trigger Fired?' column in execution_table rows.
According to variable_tracker, what is the state of new_row after the trigger runs?
Anull
BAvailable and unchanged
CModified by trigger
DDeleted
💡 Hint
Look at the 'new_row' values after Step 3 and Final in variable_tracker.
If the trigger was BEFORE instead of AFTER, when would it run?
AAfter the row is inserted
BAfter transaction commit
CBefore the row is inserted
DNever
💡 Hint
Compare the 'Trigger Timing' column in execution_table for BEFORE vs AFTER triggers.
Concept Snapshot
AFTER triggers run after the data change is done.
They see the final row state.
They cannot modify the triggering row.
Useful for logging or cascading actions.
Defined with AFTER keyword in CREATE TRIGGER.
Fires once per row or statement after change.
Full Transcript
When you perform an INSERT, UPDATE, or DELETE in PostgreSQL, an AFTER trigger runs only after the row has been changed in the table. This means the trigger function sees the final data state. For example, an AFTER INSERT trigger runs after the new row is inserted. It cannot change the row that caused it because the data is already saved. This behavior is useful for logging or other actions that depend on the completed change. The trigger fires once per affected row or once per statement, depending on how it is defined.