Bird
Raised Fist0
PostgreSQLquery~20 mins

Trigger execution order in PostgreSQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Trigger Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Order of BEFORE triggers on INSERT
Consider a table with two BEFORE INSERT triggers named trigger_a and trigger_b. If both triggers are set to fire BEFORE INSERT, in what order will they execute by default?
PostgreSQL
CREATE TABLE test_order(id SERIAL PRIMARY KEY, val TEXT);

CREATE FUNCTION trigger_a_func() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'Trigger A fired';
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION trigger_b_func() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'Trigger B fired';
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_a BEFORE INSERT ON test_order FOR EACH ROW EXECUTE FUNCTION trigger_a_func();
CREATE TRIGGER trigger_b BEFORE INSERT ON test_order FOR EACH ROW EXECUTE FUNCTION trigger_b_func();

INSERT INTO test_order(val) VALUES ('test');
Atrigger_a fires first, then trigger_b
Btrigger_b fires first, then trigger_a
CBoth triggers fire simultaneously
DThe order is random and cannot be predicted
Attempts:
2 left
💡 Hint
PostgreSQL executes triggers in alphabetical order by trigger name when no explicit ordering is set.
query_result
intermediate
2:00remaining
Execution order of AFTER triggers on UPDATE
A table has two AFTER UPDATE triggers: update_first and update_second. Both are row-level triggers. Which trigger executes first after an UPDATE statement?
PostgreSQL
CREATE TABLE update_test(id SERIAL PRIMARY KEY, data TEXT);
INSERT INTO update_test(data) VALUES ('initial');

CREATE FUNCTION update_first_func() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'update_first fired';
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE FUNCTION update_second_func() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'update_second fired';
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_first AFTER UPDATE ON update_test FOR EACH ROW EXECUTE FUNCTION update_first_func();
CREATE TRIGGER update_second AFTER UPDATE ON update_test FOR EACH ROW EXECUTE FUNCTION update_second_func();

UPDATE update_test SET data = 'changed' WHERE id = 1;
Aupdate_second fires before update_first
Bupdate_first fires before update_second
CBoth triggers fire simultaneously
DThe order depends on the order of UPDATE statement execution
Attempts:
2 left
💡 Hint
Trigger execution order is alphabetical by trigger name if no other ordering is specified.
🧠 Conceptual
advanced
2:30remaining
Effect of trigger firing order on data modification
If two BEFORE UPDATE triggers modify the same column in a row, how does the firing order affect the final value stored in that column?
AThe last trigger to fire determines the final column value
BThe first trigger to fire determines the final column value
CBoth triggers' changes are merged automatically
DThe database throws an error due to conflict
Attempts:
2 left
💡 Hint
Each trigger modifies the row data passed to the next trigger.
📝 Syntax
advanced
2:30remaining
Correct syntax to specify trigger firing order
Which of the following statements correctly sets the firing order of two triggers so that trigger_b fires before trigger_a on INSERT?
ACREATE TRIGGER trigger_b BEFORE INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_b(); CREATE TRIGGER trigger_a BEFORE INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_a();
BALTER TRIGGER trigger_b ON table_name ORDER BEFORE trigger_a;
CPostgreSQL does not support explicit trigger firing order; order depends on trigger name alphabetical order.
DCREATE TRIGGER trigger_b BEFORE INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_b() WHEN (TRUE); CREATE TRIGGER trigger_a BEFORE INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION func_a();
Attempts:
2 left
💡 Hint
Check PostgreSQL documentation on trigger ordering capabilities.
🔧 Debug
expert
3:00remaining
Diagnosing unexpected trigger execution order
A developer created two BEFORE INSERT triggers named 'a_trigger' and 'b_trigger'. They expect 'b_trigger' to fire first, but 'a_trigger' fires first instead. What is the most likely cause?
ATriggers always fire in creation order, so the first created fires first
BThe database is misconfigured and triggers are firing incorrectly
CThe trigger firing order is random and cannot be controlled
D'a_trigger' comes before 'b_trigger' alphabetically, so it fires first
Attempts:
2 left
💡 Hint
Consider how PostgreSQL determines trigger firing order by default.

Practice

(1/5)
1. In PostgreSQL, when multiple triggers are set for the same event and timing on a table, how does the database decide the order in which to execute them?
easy
A. It executes triggers based on the size of the table.
B. It executes triggers randomly.
C. It executes triggers in the order they were created.
D. It executes triggers in alphabetical order by their trigger names.

Solution

  1. Step 1: Understand trigger execution timing

    PostgreSQL runs triggers automatically on table events like INSERT, UPDATE, or DELETE.
  2. Step 2: Identify execution order rule

    When multiple triggers exist for the same event and timing, PostgreSQL executes them in the order they were created.
  3. Final Answer:

    It executes triggers in the order they were created. -> Option C
  4. Quick Check:

    Trigger order = creation order [OK]
Hint: Remember: PostgreSQL orders triggers by creation order [OK]
Common Mistakes:
  • Assuming triggers run in alphabetical order
  • Thinking triggers run randomly
  • Believing trigger order depends on table size
2. Which of the following is the correct syntax to create a BEFORE INSERT trigger named alpha_trigger on a table users in PostgreSQL?
easy
A. CREATE TRIGGER alpha_trigger BEFORE INSERT ON users FOR EACH ROW EXECUTE FUNCTION func_name();
B. CREATE TRIGGER alpha_trigger ON users BEFORE INSERT EXECUTE FUNCTION func_name();
C. CREATE TRIGGER alpha_trigger BEFORE INSERT EXECUTE FUNCTION func_name() ON users;
D. CREATE TRIGGER alpha_trigger BEFORE INSERT ON users EXECUTE PROCEDURE func_name();

Solution

  1. Step 1: Recall correct CREATE TRIGGER syntax

    The correct syntax is: CREATE TRIGGER name BEFORE event ON table FOR EACH ROW EXECUTE FUNCTION function_name();
  2. Step 2: Check each option

    CREATE TRIGGER alpha_trigger BEFORE INSERT ON users FOR EACH ROW EXECUTE FUNCTION func_name(); matches the correct syntax exactly. CREATE TRIGGER alpha_trigger BEFORE INSERT ON users EXECUTE PROCEDURE func_name(); uses EXECUTE PROCEDURE which is deprecated in modern PostgreSQL versions.
  3. Final Answer:

    CREATE TRIGGER alpha_trigger BEFORE INSERT ON users FOR EACH ROW EXECUTE FUNCTION func_name(); -> Option A
  4. Quick Check:

    Correct syntax uses EXECUTE FUNCTION and ON table [OK]
Hint: Use EXECUTE FUNCTION and ON table in CREATE TRIGGER [OK]
Common Mistakes:
  • Using EXECUTE PROCEDURE instead of EXECUTE FUNCTION
  • Placing ON table after EXECUTE FUNCTION
  • Omitting FOR EACH ROW clause
3. Given two triggers on table orders: alpha_trigger and beta_trigger, both BEFORE INSERT triggers. Which trigger runs first when a new row is inserted?
medium
A. alpha_trigger runs first because 'a' comes before 'b' alphabetically.
B. beta_trigger runs first because 'b' comes before 'a' alphabetically.
C. Both triggers run simultaneously.
D. The trigger created last runs first.

Solution

  1. Step 1: Identify trigger names and order

    Triggers are named alpha_trigger and beta_trigger. Alphabetically, 'alpha' comes before 'beta'.
  2. Step 2: Apply PostgreSQL trigger execution order

    PostgreSQL executes triggers in the order they were created for the same event and timing, not alphabetical order.
  3. Final Answer:

    beta_trigger runs first because it was created before alpha_trigger. -> Option B
  4. Quick Check:

    Trigger order = creation order [OK]
Hint: Trigger execution order depends on creation order, not alphabetical [OK]
Common Mistakes:
  • Assuming alphabetical order matters
  • Thinking triggers run simultaneously
  • Confusing creation order
4. You have two AFTER UPDATE triggers on table products: zeta_trigger and alpha_trigger. You want zeta_trigger to run before alpha_trigger. What is the problem with this setup?
medium
A. Triggers run in reverse alphabetical order, so zeta_trigger runs first as desired.
B. PostgreSQL does not support multiple triggers on the same event.
C. You must rename triggers to numbers to control order.
D. PostgreSQL runs triggers alphabetically, so alpha_trigger runs before zeta_trigger regardless of creation order.

Solution

  1. Step 1: Understand trigger execution order

    PostgreSQL executes multiple triggers for the same event and timing in the order they were created.
  2. Step 2: Analyze the trigger names

    Trigger names do not affect execution order; creation order does. So the trigger created first runs first.
  3. Final Answer:

    PostgreSQL runs triggers in creation order, so alpha_trigger runs before zeta_trigger if created first. -> Option D
  4. Quick Check:

    Trigger order = creation order [OK]
Hint: Creation order controls trigger execution, not alphabetical order [OK]
Common Mistakes:
  • Assuming creation order controls execution
  • Believing reverse alphabetical order applies
  • Thinking trigger renaming is unnecessary
5. You have three BEFORE DELETE triggers on table customers: clean_up, archive, and notify. You want archive to run first, then clean_up, then notify. How should you rename the triggers to ensure this execution order?
hard
A. No renaming needed; PostgreSQL runs triggers in creation order.
B. Rename triggers to notify, clean_up, archive.
C. Rename triggers to a_archive, b_clean_up, c_notify.
D. Rename triggers to 1_archive, 2_clean_up, 3_notify.

Solution

  1. Step 1: Understand trigger execution order

    PostgreSQL executes triggers in the order they were created for the same event and timing.
  2. Step 2: Renaming triggers does not affect execution order

    Renaming triggers does not change execution order; only creation order matters.
  3. Final Answer:

    No renaming needed; PostgreSQL runs triggers in creation order. -> Option A
  4. Quick Check:

    Trigger order = creation order [OK]
Hint: Trigger execution order depends on creation order, not name [OK]
Common Mistakes:
  • Assuming alphabetical order controls execution
  • Believing numeric or alphabetical prefixes affect order
  • Not understanding creation order importance