Sub topic 8
What the return value does
This chapter assumes you can write a basic PL/pgSQL trigger function and attach it with CREATE TRIGGER. You should also know the difference between BEFORE, AFTER and INSTEAD OF timing, and between row-level and statement-level triggers.
A trigger function must end by returning something, and PostgreSQL reads that value differently depending on the trigger's timing and granularity. Getting it wrong either fails with an error or, worse, silently skips the operation.
The core rule
A trigger function is declared RETURNS trigger. For a row-level trigger it returns either a row of the table's type or NULL. For a statement-level trigger the return value is ignored.
| Trigger kind | Return `NULL` | Return a row |
|---|---|---|
| BEFORE, row-level | Skips this row's operation. Later triggers and AFTER triggers do not fire for that row. | The operation proceeds. For INSERT and UPDATE, the returned row is the one written. |
| AFTER, row-level | Ignored | Ignored |
| INSTEAD OF, row-level (views) | Reports the row as not handled | Reports the row as handled |
| BEFORE or AFTER, statement-level | Ignored | Ignored |
The convention for ignored cases is RETURN NULL. It tells the reader that the value has no effect.
Returning a modified row
In a BEFORE INSERT or BEFORE UPDATE trigger, you can assign to fields of NEW. Whatever row you return is what gets stored. The function below cleans an email address, and it skips blank ones.
CREATE TABLE signups (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL
);
CREATE FUNCTION clean_signup() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF btrim(NEW.email) = '' THEN
RETURN NULL; -- skip this row silently
END IF;
NEW.email := lower(btrim(NEW.email));
RETURN NEW; -- store the modified row
END;
$$;
CREATE TRIGGER signups_clean
BEFORE INSERT OR UPDATE OF email ON signups
FOR EACH ROW
EXECUTE FUNCTION clean_signup();
INSERT INTO signups (email) VALUES (' [email protected] '), (' ');
SELECT email FROM signups;The INSERT reports INSERT 0 1, not INSERT 0 2. The blank row was skipped without an error, and the command tag counts only rows that were actually written. The SELECT returns [email protected].
The returned row must have the table's row type. You can return OLD from a BEFORE UPDATE trigger to discard every change the statement made to that row. You can also build a different row of the same type and return that.
Why DELETE needs special care
A DELETE has no new row, so NEW is null in a DELETE trigger. For a BEFORE DELETE trigger, any non-null return value means "go ahead". Return OLD to allow the delete.
CREATE FUNCTION protect_admin() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF OLD.email = '[email protected]' THEN
RETURN NULL; -- cancel the delete for this row
END IF;
RETURN OLD; -- allow the delete
END;
$$;
CREATE TRIGGER signups_protect_admin
BEFORE DELETE ON signups
FOR EACH ROW
EXECUTE FUNCTION protect_admin();Skipping versus raising
Returning NULL and raising an exception both stop a row from being written, but they behave differently.
RETURN NULLskips only the current row. The statement continues with other rows and the transaction stays healthy. The caller sees a smaller row count and no message.RAISE EXCEPTIONaborts the whole statement and puts the transaction into an error state. Every row the statement already changed is rolled back.
Use RETURN NULL when skipping is a deliberate feature, such as ignoring duplicates or implementing soft deletes. Use RAISE EXCEPTION for validation, where the caller must know the data was rejected. Silent skipping is hard to notice, so document it where the trigger is created.
Chained BEFORE triggers
When several BEFORE row triggers exist on one table, they run in name order. The row returned by one becomes NEW for the next. Two consequences follow.
- If an earlier trigger changes a column, later triggers see the changed value.
- If any BEFORE trigger returns
NULL, the remaining triggers for that row are not called. AFTER triggers for the row are also skipped, because the operation never happened.
If a trigger returns a row with a value you did not intend, the next trigger works from it. Always return the NEW variable, or a row you deliberately built from it.
INSTEAD OF triggers
INSTEAD OF row triggers exist only on views, and the view has no storage of its own. The trigger function performs the real work against the underlying tables. The return value then tells PostgreSQL whether the row was handled.
- A non-null return counts the row in the command's affected-row total.
NULLmeans nothing was done, and the row is not counted.
For INSERT and UPDATE, return NEW. For DELETE, return OLD. If your function returns NULL by accident, the statement runs but reports 0 rows, and INSERT ... RETURNING on the view gives nothing back.
Falling off the end
A PL/pgSQL trigger function that reaches END without a RETURN fails with the error control reached end of trigger procedure without RETURN. This applies to AFTER and statement-level triggers too, even though the value is ignored. Always end with an explicit RETURN.
Summary of failure modes
- A BEFORE trigger returns
NULLby accident. Rows vanish silently, and you see a low row count. RETURN NEWin a BEFORE DELETE trigger. No rows are ever deleted.- Modifying
NEWin an AFTER trigger. The change is ignored, because the row is already written. - No
RETURNat all. The statement fails with an error.
Exercise
A BEFORE UPDATE row trigger on signups contains IF NEW.email = OLD.email THEN RETURN NULL; END IF; RETURN NEW;. You run UPDATE signups SET email = email; on a table with 10 rows. What is the command tag, and do any AFTER UPDATE triggers fire? Then explain what would change if the trigger were declared AFTER UPDATE instead.
Show answer
Every row has an unchanged email, so the trigger returns NULL for all 10 rows. Each update is skipped and the command tag is UPDATE 0. No AFTER UPDATE triggers fire, because the operation never happened for those rows. If the trigger were AFTER UPDATE, the returned NULL would be ignored. The rows would already be updated, the tag would be UPDATE 10, and the check would have no effect.