0
0
PostgreSQLquery~20 mins

NEW and OLD record access in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of NEW and OLD record access
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of trigger function using OLD and NEW
Consider a PostgreSQL trigger function that logs changes on a table. What will be the output of the following function when an UPDATE occurs changing column price from 100 to 120?

CREATE OR REPLACE FUNCTION log_price_change() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'Price changed from % to %', OLD.price, NEW.price;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
APrice changed from 100 to 120
BPrice changed from 100 to NULL
CPrice changed from NULL to 120
DPrice changed from 120 to 100
Attempts:
2 left
💡 Hint
OLD contains the row before the update, NEW contains the row after.
🧠 Conceptual
intermediate
2:00remaining
Understanding OLD and NEW in DELETE triggers
In a DELETE trigger in PostgreSQL, which of the following statements is true about the availability of OLD and NEW records?
ABoth OLD and NEW contain the row being deleted
BNEW contains the row being deleted; OLD is NULL
COLD contains the row being deleted; NEW is NULL
DBoth OLD and NEW are NULL
Attempts:
2 left
💡 Hint
Think about what data exists before and after a DELETE.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in trigger function using OLD and NEW
Which option contains a syntax error in this PostgreSQL trigger function that tries to compare OLD and NEW values?

CREATE OR REPLACE FUNCTION check_update() RETURNS trigger AS $$
BEGIN
  IF NEW.value <> OLD.value THEN
    RAISE NOTICE 'Value changed';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
AIF (NEW.value <> OLD.value) THEN
BIF NEW.value <> OLD.value
CIF NEW.value <> OLD.value THEN
DIF NEW.value != OLD.value THEN
Attempts:
2 left
💡 Hint
Check the syntax of IF statements in PL/pgSQL.
optimization
advanced
2:00remaining
Optimizing trigger function to avoid unnecessary updates
You have a BEFORE UPDATE trigger that updates a timestamp column only if certain columns changed. Which approach is best to avoid unnecessary updates?

Options show different ways to compare OLD and NEW records.
A
IF NEW.col1 &lt;&gt; OLD.col1 OR NEW.col2 &lt;&gt; OLD.col2 THEN
  NEW.updated_at = now();
END IF;
BNEW.updated_at = now();
C
IF OLD IS DISTINCT FROM NEW THEN
  NEW.updated_at = now();
END IF;
D
IF NEW IS DISTINCT FROM OLD THEN
  NEW.updated_at = now();
END IF;
Attempts:
2 left
💡 Hint
PostgreSQL supports IS DISTINCT FROM for row comparisons.
🔧 Debug
expert
2:00remaining
Debugging a trigger function that causes infinite recursion
A BEFORE UPDATE trigger function updates the same table's row by setting NEW.updated_at = now(). However, this causes infinite recursion and stack overflow. What is the best fix?
AAdd a condition to update NEW.updated_at only if OLD.updated_at is different
BDisable triggers on the table temporarily during update
CChange the trigger to AFTER UPDATE instead of BEFORE UPDATE
DUse a separate function to update updated_at outside the trigger
Attempts:
2 left
💡 Hint
Think about what causes the trigger to fire repeatedly.