Bird
Raised Fist0
PostgreSQLquery~10 mins

NEW and OLD record access in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - NEW and OLD record access
Trigger Event Occurs
Trigger Function Starts
Access OLD Record?
Access NEW Record?
Perform Logic Using OLD/NEW
Finish Trigger Function
Continue with Original Operation
When a trigger fires, you can access the OLD record (before change) or NEW record (after change) to perform logic inside the trigger function.
Execution Sample
PostgreSQL
CREATE FUNCTION trg_func() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'OLD name: %, NEW name: %', OLD.name, NEW.name;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
This trigger function prints the OLD and NEW 'name' values when a row is updated.
Execution Table
StepTrigger EventOLD RecordNEW RecordActionOutput
1UPDATE on table{id:1, name:'Alice'}{id:1, name:'Alicia'}Trigger fires, accesses OLD and NEWPrints: OLD name: Alice, NEW name: Alicia
2Trigger function runsOLD.name = 'Alice'NEW.name = 'Alicia'RAISE NOTICE executedMessage shown to client
3Trigger function endsOLD unchangedNEW unchangedRETURN NEWUpdate proceeds with NEW record
4Operation completesN/AN/ARow updated in tableRow now has name 'Alicia'
💡 Trigger completes and returns NEW record, allowing update to proceed with new data.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
OLD.nameN/AAliceAliceAlice
NEW.nameN/AAliciaAliciaAlicia
Key Moments - 2 Insights
Why can OLD be NULL during an INSERT trigger?
Because during INSERT, there is no existing row before the operation, so OLD does not exist. See execution_table step 1 where OLD is only available on UPDATE or DELETE.
What happens if the trigger returns OLD instead of NEW on an UPDATE?
Returning OLD cancels the update and keeps the old row unchanged. The execution_table step 3 shows returning NEW allows the update to proceed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the OLD.name value?
ANULL
BAlicia
CAlice
DNot accessible
💡 Hint
Check the OLD Record column in execution_table row 1.
At which step does the trigger function return the NEW record?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Action column in execution_table row 3.
If this trigger was fired on an INSERT, what would OLD be?
ANULL or not available
BThe inserted row
CThe previous row with same id
DSame as NEW
💡 Hint
Refer to key_moments about OLD being NULL during INSERT.
Concept Snapshot
NEW and OLD records are special variables in triggers.
OLD holds the row before change (available in UPDATE, DELETE).
NEW holds the row after change (available in INSERT, UPDATE).
Use OLD and NEW to compare or modify data in trigger functions.
Returning NEW continues the operation with new data.
Returning OLD can cancel or revert changes.
Full Transcript
When a database trigger runs, it can access two special records: OLD and NEW. OLD is the data before the change, and NEW is the data after the change. For example, during an UPDATE, OLD contains the original row, and NEW contains the updated row. In an INSERT, OLD is not available because there was no previous row. The trigger function can use these records to perform checks or modify data. Returning NEW from the trigger allows the change to proceed, while returning OLD can cancel or revert the change. This visual trace shows a trigger on UPDATE accessing OLD and NEW, printing their 'name' fields, and returning NEW to complete the update.

Practice

(1/5)
1. In a PostgreSQL trigger function, which record variable would you use to access the new row data after an INSERT operation?
easy
A. PREVIOUS
B. NEW
C. CURRENT
D. OLD

Solution

  1. 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 the NEW record.
  2. Step 2: Identify correct record variable

    The OLD record is not available for INSERT because there is no previous row. Therefore, NEW is used to access the inserted row.
  3. Final Answer:

    NEW -> Option B
  4. Quick 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
A. OLD.price
B. NEW.price
C. OLD->price
D. NEW->price

Solution

  1. Step 1: Identify record variable for DELETE

    In a DELETE trigger, the row is being removed, so the old data is accessible via OLD.
  2. Step 2: Use correct syntax for column access

    PostgreSQL uses dot notation to access columns in record variables, so OLD.price is correct.
  3. Final Answer:

    OLD.price -> Option A
  4. Quick 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:
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
A. Quantity decreased from 10 to 7
B. Quantity decreased from 7 to 10
C. No output
D. Syntax error

Solution

  1. 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.
  2. 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'.
  3. Final Answer:

    Quantity decreased from 10 to 7 -> Option A
  4. Quick 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:
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
A. RAISE EXCEPTION syntax is wrong
B. RETURN OLD is invalid in DELETE triggers
C. Using NEW in a DELETE trigger where only OLD is available
D. Function must return VOID, not trigger

Solution

  1. Step 1: Check record variables in DELETE triggers

    In DELETE triggers, the NEW record is not available because no new row is inserted or updated.
  2. Step 2: Identify incorrect usage of NEW

    The function incorrectly uses NEW.id, which causes an error. It should use OLD.id instead.
  3. Final Answer:

    Using NEW in a DELETE trigger where only OLD is available -> Option C
  4. Quick 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
A. IF OLD.salary > NEW.salary THEN INSERT INTO log_table VALUES (OLD.id, OLD.salary); END IF;
B. IF NEW.salary < OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
C. IF NEW.salary = OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
D. IF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;

Solution

  1. 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 is NEW.salary > OLD.salary.
  2. Step 2: Use correct record variables for UPDATE

    The new salary and id come from NEW because the row is updated with new values.
  3. Final Answer:

    IF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF; -> Option D
  4. Quick 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