0
0
PostgreSQLquery~20 mins

Trigger function creation in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trigger Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this trigger function execution?

Consider the following PostgreSQL trigger function and trigger on a table employees:

CREATE OR REPLACE FUNCTION update_last_modified()
RETURNS TRIGGER AS $$
BEGIN
  NEW.last_modified := NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_update_last_modified
BEFORE UPDATE ON employees
FOR EACH ROW
EXECUTE FUNCTION update_last_modified();

If a row in employees is updated, what will be the value of last_modified column after the update?

AThe <code>last_modified</code> column will remain unchanged from its previous value.
BThe <code>last_modified</code> column will be set to NULL.
CThe update will fail because the trigger function does not return OLD.
DThe <code>last_modified</code> column will be set to the current timestamp at update time.
Attempts:
2 left
💡 Hint

Think about what the trigger function does before returning the new row.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this trigger function definition

Which option contains the correct syntax for creating a trigger function in PostgreSQL?

PostgreSQL
CREATE OR REPLACE FUNCTION trg_func()
RETURNS TRIGGER AS $$
BEGIN
  -- some logic
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
ACREATE FUNCTION trg_func() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;
BCREATE OR REPLACE FUNCTION trg_func RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;
CCREATE OR REPLACE FUNCTION trg_func() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END; $$ LANGUAGE plpgsql;
DCREATE OR REPLACE FUNCTION trg_func() RETURNS TRIGGER AS $$ BEGIN RETURN NEW; END $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint

Check for missing parentheses, semicolons, and correct keywords.

optimization
advanced
3:00remaining
Optimize this trigger function to avoid unnecessary updates

Given this trigger function that updates a last_modified timestamp on every update:

CREATE OR REPLACE FUNCTION update_last_modified()
RETURNS TRIGGER AS $$
BEGIN
  NEW.last_modified := NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

How can you optimize it to only update last_modified if some column other than last_modified actually changed?

AAdd an IF statement comparing OLD and NEW rows for changes excluding last_modified, then update last_modified only if changed.
BAlways update last_modified regardless of changes to ensure consistency.
CRemove the trigger function and update last_modified manually in application code.
DUse a BEFORE INSERT trigger instead of BEFORE UPDATE to update last_modified.
Attempts:
2 left
💡 Hint

Think about how to detect changes between OLD and NEW rows.

🔧 Debug
advanced
3:00remaining
Why does this trigger function cause an infinite loop?

Consider this trigger function and trigger on table orders:

CREATE OR REPLACE FUNCTION trg_update_total()
RETURNS TRIGGER AS $$
BEGIN
  NEW.total := NEW.quantity * NEW.price;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_update_total
BEFORE UPDATE ON orders
FOR EACH ROW
EXECUTE FUNCTION trg_update_total();

Updating any row in orders causes an infinite loop. Why?

AThe table <code>orders</code> does not have columns <code>quantity</code> or <code>price</code>.
BThe trigger updates the row, which fires the trigger again, causing recursion without a stop condition.
CThe trigger is defined as AFTER UPDATE instead of BEFORE UPDATE.
DThe trigger function is missing a RETURN statement, causing an error.
Attempts:
2 left
💡 Hint

Think about what happens when a trigger modifies the row it is triggered on.

🧠 Conceptual
expert
2:30remaining
Which statement about trigger functions in PostgreSQL is true?

Choose the correct statement about trigger functions in PostgreSQL.

ATrigger functions can be written in any language supported by PostgreSQL, but must return type TRIGGER.
BTrigger functions must always return the NEW row for BEFORE triggers and the OLD row for AFTER triggers.
CTrigger functions can only be used with BEFORE INSERT triggers, not UPDATE or DELETE.
DTrigger functions cannot access the OLD row data in BEFORE INSERT triggers.
Attempts:
2 left
💡 Hint

Consider the flexibility of languages and return types for trigger functions.