Sub topic 5
FOR EACH ROW vs FOR EACH STATEMENT
Assume you know basic INSERT, UPDATE, DELETE, and transactions. This chapter focuses on how often PostgreSQL runs a trigger.
A row-level trigger runs once for every row affected by a statement. A statement-level trigger runs once for the whole SQL statement. The choice affects performance, available data, and what the trigger function should do.
Row-level triggers
Use FOR EACH ROW when the function must inspect or change individual records. Row-level functions can read NEW and OLD.
For an UPDATE affecting 100 rows, PostgreSQL calls the function 100 times. A BEFORE row trigger can change NEW, or return NULL to skip the current row. An AFTER row trigger can record the row, but its return value is ignored.
CREATE TABLE accounts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
balance numeric(12, 2) NOT NULL
);
CREATE FUNCTION reject_negative_balance() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF NEW.balance < 0 THEN
RAISE EXCEPTION 'balance cannot be negative';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER accounts_check_balance
BEFORE INSERT OR UPDATE OF balance ON accounts
FOR EACH ROW
EXECUTE FUNCTION reject_negative_balance();The UPDATE OF balance condition means the trigger fires only when the statement names balance. It can still fire when a value is assigned to the same value. Add a WHEN condition if you need to compare the old and new values.
Statement-level triggers
Use FOR EACH STATEMENT when the work applies to the operation as a whole. The function runs once even if the statement changes many rows. A statement-level trigger also runs when the statement affects zero rows.
Statement-level triggers cannot use a row-specific NEW or OLD. Their return value is ignored. They are useful for counting, refreshing summary data, or starting set-based work.
CREATE FUNCTION announce_update() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE NOTICE 'An UPDATE statement finished on %', TG_TABLE_NAME;
RETURN NULL;
END;
$$;
CREATE TRIGGER accounts_update_notice
AFTER UPDATE ON accounts
FOR EACH STATEMENT
EXECUTE FUNCTION announce_update();Transition tables
An AFTER statement-level trigger can use a transition table. It exposes all rows changed by one statement, allowing one set-based query instead of one function call per row.
CREATE FUNCTION count_changed_accounts() RETURNS trigger
LANGUAGE plpgsql AS $$
DECLARE
changed_count integer;
BEGIN
SELECT count(*) INTO changed_count FROM changed_rows;
RAISE NOTICE '% accounts changed', changed_count;
RETURN NULL;
END;
$$;
CREATE TRIGGER accounts_count_changes
AFTER UPDATE ON accounts
REFERENCING NEW TABLE AS changed_rows
FOR EACH STATEMENT
EXECUTE FUNCTION count_changed_accounts();| Need | Suitable granularity |
|---|---|
| Validate or derive each row | `FOR EACH ROW` |
| Process all changed rows together | `FOR EACH STATEMENT` |
Exercise
An UPDATE changes 500 rows. How many times does each trigger function run if one trigger is FOR EACH ROW and another is FOR EACH STATEMENT?
Show answer
The row-level function runs 500 times. The statement-level function runs once.