0
0
PostgreSQLquery~20 mins

BEFORE trigger behavior in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BEFORE Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of BEFORE INSERT trigger modifying NEW row

Consider a PostgreSQL table users with columns id (serial primary key) and username (text). A BEFORE INSERT trigger modifies the NEW.username to uppercase before insertion.

What will be the output of this query after inserting 'alice'?

INSERT INTO users (username) VALUES ('alice');
SELECT username FROM users WHERE id = currval(pg_get_serial_sequence('users','id'));
PostgreSQL
CREATE TABLE users (id SERIAL PRIMARY KEY, username TEXT);
CREATE OR REPLACE FUNCTION uppercase_username() RETURNS TRIGGER AS $$
BEGIN
  NEW.username := UPPER(NEW.username);
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER before_insert_uppercase
BEFORE INSERT ON users
FOR EACH ROW EXECUTE FUNCTION uppercase_username();
Aalice
BNULL
CALICE
DAlice
Attempts:
2 left
💡 Hint

BEFORE triggers can modify the NEW row before it is saved.

query_result
intermediate
2:00remaining
Effect of BEFORE UPDATE trigger returning NULL

In PostgreSQL, what happens if a BEFORE UPDATE trigger returns NULL?

Consider this trigger function:

CREATE OR REPLACE FUNCTION cancel_update() RETURNS TRIGGER AS $$
BEGIN
  RETURN NULL;
END;
$$ LANGUAGE plpgsql;

And the trigger is attached BEFORE UPDATE on a table. What is the effect on the UPDATE statement?

AThe UPDATE proceeds normally without changes.
BThe UPDATE is canceled; no row is updated.
CThe UPDATE causes a runtime error.
DThe UPDATE updates the row with NULL values.
Attempts:
2 left
💡 Hint

Returning NULL in a BEFORE trigger means the row is skipped.

📝 Syntax
advanced
2:00remaining
Correct syntax for BEFORE INSERT trigger function

Which of the following is the correct syntax for a PostgreSQL BEFORE INSERT trigger function that modifies the NEW row?

ACREATE FUNCTION trg_func() RETURNS TRIGGER AS $$ BEGIN NEW.col := 1; RETURN NEW; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION trg_func() RETURNS VOID AS $$ BEGIN NEW.col := 1; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION trg_func() RETURNS TRIGGER AS $$ BEGIN UPDATE table SET col = 1; RETURN NEW; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION trg_func() RETURNS TRIGGER AS $$ BEGIN NEW.col = 1; RETURN OLD; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint

BEFORE triggers must return the modified NEW row.

optimization
advanced
2:00remaining
Optimizing BEFORE INSERT trigger to avoid recursion

You have a BEFORE INSERT trigger on a table that modifies the NEW row and then inserts into the same table inside the trigger function. What problem can this cause?

How can you optimize or fix this behavior?

AIt causes deadlock; fix by adding explicit locks.
BIt causes no problem; triggers can safely insert into same table.
CIt causes syntax error; fix by changing trigger to AFTER INSERT.
DIt causes infinite recursion; fix by using a condition to skip trigger on internal inserts.
Attempts:
2 left
💡 Hint

Triggers firing themselves repeatedly cause recursion.

🧠 Conceptual
expert
2:00remaining
Behavior of BEFORE trigger with multiple rows in PostgreSQL

In PostgreSQL, if a BEFORE INSERT trigger is defined FOR EACH ROW, and an INSERT statement inserts multiple rows at once, how does the trigger behave?

Choose the correct statement.

AThe trigger fires once per row inserted, each time with that row's NEW data.
BThe trigger fires once for the entire statement with all rows in NEW.
CThe trigger fires only once for the first row inserted.
DThe trigger fires once per column of each row inserted.
Attempts:
2 left
💡 Hint

FOR EACH ROW triggers run once per row.