Discover how triggers save you from tedious, error-filled manual updates!
Row-level vs statement-level triggers in PostgreSQL - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big spreadsheet where you need to check and update information every time someone changes a row. Doing this by hand means opening the sheet, looking at each row, and making changes one by one.
Manually checking each row is slow and easy to mess up. You might forget a row, make mistakes, or waste a lot of time repeating the same steps for every change.
Row-level and statement-level triggers automatically run your checks and updates in the database. Row-level triggers act on each row changed, while statement-level triggers act once per whole operation, saving time and avoiding errors.
for each row in table: if row changed: update related data
CREATE TRIGGER trg AFTER UPDATE ON table FOR EACH ROW EXECUTE FUNCTION update_related_data();
Triggers let your database react instantly and correctly to changes, keeping data accurate without extra work.
When a customer updates their address, a row-level trigger can update their shipping info immediately, while a statement-level trigger can log the whole batch update once.
Manual row-by-row updates are slow and error-prone.
Row-level triggers run for each changed row automatically.
Statement-level triggers run once per operation, improving efficiency.
Practice
Solution
Step 1: Understand trigger execution scope
Row-level triggers run once for every row affected by the SQL command, meaning if 10 rows are updated, the trigger runs 10 times.Step 2: Understand statement-level trigger behavior
Statement-level triggers run only once per SQL command, regardless of how many rows are affected.Final Answer:
Row-level triggers execute once for each affected row; statement-level triggers execute once per SQL statement. -> Option CQuick Check:
Row-level = per row, Statement-level = per statement [OK]
- Confusing which trigger runs per row vs per statement
- Thinking row-level triggers run only once per statement
- Assuming statement-level triggers run per row
- Believing trigger types depend on operation type (INSERT/UPDATE)
Solution
Step 1: Identify correct trigger syntax
The syntax for creating a row-level trigger requires the clause FOR EACH ROW to specify it runs per affected row.Step 2: Check full syntax correctness
CREATE TRIGGER trg AFTER INSERT ON table FOR EACH ROW EXECUTE FUNCTION func(); correctly includes AFTER INSERT, ON table, FOR EACH ROW, and EXECUTE FUNCTION func(); which is the proper syntax.Final Answer:
CREATE TRIGGER trg AFTER INSERT ON table FOR EACH ROW EXECUTE FUNCTION func(); -> Option AQuick Check:
Row-level triggers use FOR EACH ROW [OK]
- Omitting FOR EACH ROW for row-level triggers
- Using FOR EACH STATEMENT for row-level triggers
- Missing EXECUTE FUNCTION keyword
- Incorrect order of clauses
CREATE FUNCTION trg_func() RETURNS trigger AS $$ BEGIN RAISE NOTICE 'Triggered'; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW EXECUTE FUNCTION trg_func(); UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';What will be the output when the UPDATE affects 3 rows?
Solution
Step 1: Identify trigger type and execution count
The trigger is defined FOR EACH ROW, so it runs once for every row updated.Step 2: Calculate total trigger executions
Since 3 rows are updated, the trigger function runs 3 times, each raising the notice 'Triggered'.Final Answer:
The notice 'Triggered' will appear 3 times. -> Option AQuick Check:
Row-level trigger runs per row = 3 notices [OK]
- Assuming notice appears only once per statement
- Confusing FOR EACH ROW with FOR EACH STATEMENT
- Thinking AFTER UPDATE triggers don't raise notices
- Believing trigger runs multiple times per row
Solution
Step 1: Understand trigger definition impact
If a trigger runs multiple times per row update, it is likely defined as FOR EACH ROW, not FOR EACH STATEMENT.Step 2: Verify PostgreSQL trigger capabilities
PostgreSQL supports both row-level and statement-level triggers; statement-level triggers run once per statement.Final Answer:
You accidentally defined the trigger as FOR EACH ROW instead of FOR EACH STATEMENT. -> Option DQuick Check:
FOR EACH ROW triggers run per row, causing multiple executions [OK]
- Believing statement-level triggers run per row
- Ignoring trigger definition syntax
- Assuming PostgreSQL lacks statement-level triggers
- Blaming trigger function code without checking trigger type
Solution
Step 1: Determine trigger timing for logging after update
Logging after the update completes requires an AFTER trigger.Step 2: Choose trigger level for single summary message
To log once per statement regardless of rows, use a statement-level trigger (FOR EACH STATEMENT).Final Answer:
An AFTER UPDATE statement-level trigger -> Option BQuick Check:
Summary logging = AFTER + statement-level trigger [OK]
- Using row-level triggers causing multiple logs
- Using BEFORE triggers missing final state
- Confusing timing and level for logging
- Assuming row-level triggers can log once per statement
