Sub topic 7
Reading `NEW`, `OLD`, and `TG_*` Variables
Prerequisites: basic INSERT, UPDATE, and DELETE, PostgreSQL transactions, and simple PL/pgSQL syntax. This chapter assumes you already know how to create a trigger function and attach it with CREATE TRIGGER.
A trigger function receives information about the operation that caused it to run. PostgreSQL exposes the affected row through NEW and OLD. It exposes details about the trigger itself through variables whose names begin with TG_.
`NEW` and `OLD`
NEW is the row after the operation. OLD is the row before the operation.
Their availability depends on the event:
| Event | `OLD` | `NEW` |
|---|---|---|
| `INSERT` | Not available | The inserted row |
| `UPDATE` | The previous row | The modified row |
| `DELETE` | The deleted row | Not available |
These are record variables. You can read their columns with dot notation, such as NEW.email or OLD.balance.
A row-level BEFORE trigger can change fields in NEW. A trigger that only reads data, such as an audit trigger, can compare OLD and NEW.
`TG_*` Variables
PostgreSQL supplies metadata about the current trigger invocation.
| Variable | Meaning |
|---|---|
| `TG_NAME` | Name of the trigger |
| `TG_OP` | Operation: `INSERT`, `UPDATE`, `DELETE`, or `TRUNCATE` |
| `TG_WHEN` | `BEFORE`, `AFTER`, or `INSTEAD OF` |
| `TG_LEVEL` | `ROW` or `STATEMENT` |
| `TG_TABLE_NAME` | Unqualified table name |
| `TG_TABLE_SCHEMA` | Table schema |
| `TG_RELID` | Object identifier of the table |
| `TG_NARGS` | Number of arguments supplied to the trigger |
| `TG_ARGV` | Text array containing trigger arguments |
TG_ARGV uses zero-based indexes. Therefore, the first argument is TG_ARGV[0].
A Function That Inspects the Context
This example logs the operation and the affected values. It uses TG_OP to avoid reading NEW during a delete, where NEW does not exist.
CREATE TABLE trigger_demo (
id integer PRIMARY KEY,
email text NOT NULL
);
CREATE FUNCTION describe_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE NOTICE 'trigger=% table=%.% operation=% level=% label=%',
TG_NAME,
TG_TABLE_SCHEMA,
TG_TABLE_NAME,
TG_OP,
TG_LEVEL,
TG_ARGV[0];
IF TG_OP = 'INSERT' THEN
RAISE NOTICE 'new email=%', NEW.email;
ELSIF TG_OP = 'UPDATE' THEN
RAISE NOTICE 'email changed from % to %', OLD.email, NEW.email;
ELSIF TG_OP = 'DELETE' THEN
RAISE NOTICE 'old email=%', OLD.email;
END IF;
RETURN NULL;
END;
$$;
CREATE TRIGGER trigger_demo_description
AFTER INSERT OR UPDATE OR DELETE ON trigger_demo
FOR EACH ROW
EXECUTE FUNCTION describe_change('audit');
INSERT INTO trigger_demo VALUES (1, '[email protected]');
UPDATE trigger_demo SET email = '[email protected]' WHERE id = 1;
DELETE FROM trigger_demo WHERE id = 1;For a statement-level trigger, TG_LEVEL is STATEMENT, and there is no individual row. Do not use NEW or OLD there. A transition table can provide the complete set of affected rows when the trigger supports one.
Exercise
In an UPDATE trigger, which variables contain the email before and after the change, and what is the index of the first trigger argument?
Show answer
OLD.email contains the previous value, NEW.email contains the modified value, and the first trigger argument is TG_ARGV[0].