NEW and OLD let you see data before and after a change in a table. This helps you check or use the changed data inside triggers.
NEW and OLD record access in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PostgreSQL
CREATE TRIGGER trigger_name BEFORE|AFTER INSERT|UPDATE|DELETE ON table_name FOR EACH ROW EXECUTE FUNCTION function_name(); -- Inside the trigger function: -- Use NEW.column_name to access new data (for INSERT or UPDATE) -- Use OLD.column_name to access old data (for UPDATE or DELETE)
NEW is available in INSERT and UPDATE triggers to see the new row data.
OLD is available in UPDATE and DELETE triggers to see the old row data.
Examples
PostgreSQL
CREATE TRIGGER log_update AFTER UPDATE ON employees FOR EACH ROW EXECUTE FUNCTION log_employee_changes();
PostgreSQL
CREATE OR REPLACE FUNCTION log_employee_changes() RETURNS trigger AS $$ BEGIN RAISE NOTICE 'Old salary: %, New salary: %', OLD.salary, NEW.salary; RETURN NEW; END; $$ LANGUAGE plpgsql;
Sample Program
This example creates a table and a trigger that prints a message when an employee's salary changes.
PostgreSQL
CREATE TABLE employees ( id SERIAL PRIMARY KEY, name TEXT, salary INT ); CREATE OR REPLACE FUNCTION log_salary_change() RETURNS trigger AS $$ BEGIN RAISE NOTICE 'Salary changed from % to % for employee %', OLD.salary, NEW.salary, NEW.name; RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER salary_change_trigger AFTER UPDATE ON employees FOR EACH ROW EXECUTE FUNCTION log_salary_change(); INSERT INTO employees (name, salary) VALUES ('Alice', 50000); UPDATE employees SET salary = 55000 WHERE name = 'Alice';
Important Notes
Triggers using NEW and OLD must be row-level (FOR EACH ROW) to access individual rows.
In INSERT triggers, OLD is not available because there is no old row.
In DELETE triggers, NEW is not available because the row is being removed.
Summary
NEW and OLD let you access data before and after changes inside triggers.
Use NEW for new data in INSERT and UPDATE triggers.
Use OLD for old data in UPDATE and DELETE triggers.
Practice
1. In a PostgreSQL trigger function, which record variable would you use to access the new row data after an
INSERT operation?easy
Solution
Step 1: Understand trigger timing for INSERT
For an INSERT operation, the new row is being added, so the trigger can access the new data using theNEWrecord.Step 2: Identify correct record variable
TheOLDrecord is not available for INSERT because there is no previous row. Therefore,NEWis used to access the inserted row.Final Answer:
NEW -> Option BQuick Check:
INSERT uses NEW = D [OK]
Hint: Use NEW for inserted or updated rows, OLD for deleted or old rows [OK]
Common Mistakes:
- Using OLD in INSERT triggers
- Confusing NEW and OLD for UPDATE
- Assuming CURRENT or PREVIOUS exist
2. Which of the following is the correct syntax to access the old value of a column named
price inside a DELETE trigger in PostgreSQL?easy
Solution
Step 1: Identify record variable for DELETE
In a DELETE trigger, the row is being removed, so the old data is accessible viaOLD.Step 2: Use correct syntax for column access
PostgreSQL uses dot notation to access columns in record variables, soOLD.priceis correct.Final Answer:
OLD.price -> Option AQuick Check:
DELETE uses OLD.column = A [OK]
Hint: Use dot notation with OLD for deleted row columns [OK]
Common Mistakes:
- Using NEW in DELETE triggers
- Using arrow (->) instead of dot for record access
- Confusing syntax for JSON operators
3. Consider this trigger function snippet for an UPDATE operation:
What will be the output if the old quantity was 10 and the new quantity is 7?
IF NEW.quantity < OLD.quantity THEN RAISE NOTICE 'Quantity decreased from % to %', OLD.quantity, NEW.quantity; END IF;
What will be the output if the old quantity was 10 and the new quantity is 7?
medium
Solution
Step 1: Understand the condition in the IF statement
The condition checks if the new quantity is less than the old quantity. Here, 7 < 10 is true.Step 2: Analyze the RAISE NOTICE output
The message prints the old quantity first, then the new quantity, so it will output: 'Quantity decreased from 10 to 7'.Final Answer:
Quantity decreased from 10 to 7 -> Option AQuick Check:
NEW < OLD triggers notice = A [OK]
Hint: Compare NEW and OLD values carefully in UPDATE triggers [OK]
Common Mistakes:
- Mixing up NEW and OLD values in output
- Assuming no output when condition is true
- Confusing syntax of RAISE NOTICE
4. You wrote this trigger function for DELETE:
What is the error in this function?
CREATE FUNCTION trg_delete_check() RETURNS trigger AS $$
BEGIN
IF NEW.id IS NULL THEN
RAISE EXCEPTION 'ID cannot be null';
END IF;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;What is the error in this function?
medium
Solution
Step 1: Check record variables in DELETE triggers
In DELETE triggers, theNEWrecord is not available because no new row is inserted or updated.Step 2: Identify incorrect usage of NEW
The function incorrectly usesNEW.id, which causes an error. It should useOLD.idinstead.Final Answer:
Using NEW in a DELETE trigger where only OLD is available -> Option CQuick Check:
DELETE triggers have OLD, not NEW = C [OK]
Hint: Use OLD in DELETE triggers; NEW is unavailable [OK]
Common Mistakes:
- Using NEW in DELETE triggers
- Returning OLD incorrectly
- Misunderstanding trigger return types
5. You want to create a trigger that logs changes to a
salary column only when the salary is updated to a higher value. Which trigger condition and record access correctly implements this in PostgreSQL?hard
Solution
Step 1: Understand the condition for logging
The trigger should log only when the new salary is greater than the old salary, so the condition isNEW.salary > OLD.salary.Step 2: Use correct record variables for UPDATE
The new salary and id come fromNEWbecause the row is updated with new values.Final Answer:
IF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF; -> Option DQuick Check:
Log when NEW > OLD salary = B [OK]
Hint: Compare NEW and OLD to detect increases, then log NEW data [OK]
Common Mistakes:
- Reversing NEW and OLD in condition
- Logging when salary decreases
- Using equality instead of greater than
