Bird
Raised Fist0
Supabasecloud~20 mins

Database triggers for automation in Supabase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Database Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What happens when a Supabase trigger fires on INSERT?

Consider a Supabase database table with a trigger that runs a function after an INSERT operation. What is the expected behavior of this trigger?

Supabase
CREATE FUNCTION log_insert() RETURNS trigger AS $$ BEGIN INSERT INTO audit_log(table_name, operation) VALUES (TG_TABLE_NAME, 'INSERT'); RETURN NEW; END; $$ LANGUAGE plpgsql;

CREATE TRIGGER after_insert_trigger AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION log_insert();
AThe trigger modifies the inserted row before it is saved to the users table.
BThe trigger prevents the insert operation from completing and rolls back the transaction.
CThe trigger logs the insert operation into audit_log table after a new row is added to users.
DThe trigger runs before the insert and can change the data being inserted.
Attempts:
2 left
💡 Hint

Think about when AFTER INSERT triggers run and what RETURN NEW means.

❓ Architecture
intermediate
2:00remaining
Which trigger type is best for validating data before insert?

You want to ensure data integrity by checking values before they are saved in a Supabase table. Which trigger timing should you use?

AAFTER INSERT trigger
BAFTER UPDATE trigger
CINSTEAD OF INSERT trigger
DBEFORE INSERT trigger
Attempts:
2 left
💡 Hint

Consider when you want to stop bad data from entering the table.

❓ security
advanced
2:00remaining
What security risk can arise from triggers that call external APIs?

A Supabase trigger calls an external API to notify another system after data changes. What is a potential security risk?

AThe trigger can cause data leaks if API keys are exposed in the function code.
BThe trigger will always fail because triggers cannot call external APIs.
CThe trigger will slow down the database but has no security risks.
DThe trigger automatically encrypts all data sent to the API.
Attempts:
2 left
💡 Hint

Think about sensitive information in trigger code.

❓ Configuration
advanced
2:00remaining
What error occurs if a trigger function returns NULL in a BEFORE INSERT trigger?

Given a BEFORE INSERT trigger function in Supabase that returns NULL, what happens to the insert operation?

Supabase
CREATE FUNCTION block_insert() RETURNS trigger AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE plpgsql;

CREATE TRIGGER before_insert_block BEFORE INSERT ON orders FOR EACH ROW EXECUTE FUNCTION block_insert();
AThe insert operation inserts a NULL row into the table.
BThe insert operation is canceled and the row is not inserted.
CA runtime error occurs and the transaction is rolled back.
DThe insert operation proceeds normally with the original row.
Attempts:
2 left
💡 Hint

Check what RETURN NULL means in BEFORE triggers.

✅ Best Practice
expert
2:00remaining
Which approach best avoids performance issues with triggers in Supabase?

You have a high-traffic Supabase table with triggers that perform complex calculations. What is the best way to avoid slowing down database operations?

AMove complex logic to asynchronous background jobs instead of triggers.
BAdd more triggers to split the logic into smaller parts.
CUse AFTER triggers to run logic synchronously after each row insert.
DDisable all triggers during peak traffic hours.
Attempts:
2 left
💡 Hint

Think about how to keep database operations fast and responsive.

Practice

(1/5)
1. What is the main purpose of a database trigger in Supabase?
easy
A. To manually update data in the database
B. To automatically run a function when data changes in a table
C. To create a backup of the database
D. To increase the database storage size

Solution

  1. Step 1: Understand what triggers do

    Triggers run automatically when data changes like insert, update, or delete.
  2. Step 2: Identify the purpose in Supabase

    Supabase triggers automate running functions on data changes without manual intervention.
  3. Final Answer:

    To automatically run a function when data changes in a table -> Option B
  4. Quick Check:

    Trigger = auto-run function on data change [OK]
Hint: Triggers run code automatically on data changes [OK]
Common Mistakes:
  • Thinking triggers require manual activation
  • Confusing triggers with backups
  • Assuming triggers increase storage
2. Which of the following is the correct syntax to create a trigger in Supabase SQL?
easy
A. CREATE TRIGGER trigger_name WHEN INSERT ON table_name EXECUTE function_name();
B. CREATE TRIGGER trigger_name BEFORE UPDATE table_name CALL function_name();
C. CREATE TRIGGER trigger_name ON table_name AFTER DELETE RUN function_name();
D. CREATE TRIGGER trigger_name AFTER INSERT ON table_name EXECUTE FUNCTION function_name();

Solution

  1. Step 1: Recall correct trigger syntax in PostgreSQL (used by Supabase)

    The correct syntax uses CREATE TRIGGER, timing (AFTER/BEFORE), event (INSERT/UPDATE/DELETE), ON table, and EXECUTE FUNCTION.
  2. Step 2: Match options to syntax

    CREATE TRIGGER trigger_name AFTER INSERT ON table_name EXECUTE FUNCTION function_name(); matches the correct syntax: CREATE TRIGGER name AFTER INSERT ON table EXECUTE FUNCTION func(); others use wrong keywords like CALL, RUN, or WHEN.
  3. Final Answer:

    CREATE TRIGGER trigger_name AFTER INSERT ON table_name EXECUTE FUNCTION function_name(); -> Option D
  4. Quick Check:

    CREATE TRIGGER ... EXECUTE FUNCTION ... [OK]
Hint: Use EXECUTE FUNCTION, not CALL or RUN in triggers [OK]
Common Mistakes:
  • Using CALL or RUN instead of EXECUTE FUNCTION
  • Missing ON keyword before table name
  • Using WHEN instead of BEFORE or AFTER
3. Given this trigger function in Supabase SQL:
CREATE FUNCTION log_insert() RETURNS trigger AS $$ BEGIN INSERT INTO logs(table_name, action) VALUES (TG_TABLE_NAME, 'insert'); RETURN NEW; END; $$ LANGUAGE plpgsql;

And this trigger:
CREATE TRIGGER after_insert_log AFTER INSERT ON users EXECUTE FUNCTION log_insert();

What happens when a new row is inserted into the users table?
medium
A. A new row is added to the logs table recording the insert action on users
B. The insert into users is blocked and logs table is updated
C. Nothing happens because the trigger is AFTER INSERT
D. The users table row is deleted after insert

Solution

  1. Step 1: Understand the trigger function behavior

    The function inserts a row into logs with the table name and action 'insert' whenever triggered.
  2. Step 2: Understand the trigger timing and event

    The trigger runs AFTER INSERT on users, so after a new user row is added, the function runs and logs the action.
  3. Final Answer:

    A new row is added to the logs table recording the insert action on users -> Option A
  4. Quick Check:

    AFTER INSERT triggers log insert action [OK]
Hint: AFTER INSERT triggers run after data is added [OK]
Common Mistakes:
  • Thinking AFTER INSERT blocks the insert
  • Assuming trigger does nothing
  • Confusing trigger timing with BEFORE
4. You wrote this trigger function:
CREATE FUNCTION update_timestamp() RETURNS trigger AS $$ BEGIN NEW.updated_at = NOW(); RETURN NEW; END; $$ LANGUAGE plpgsql;

And this trigger:
CREATE TRIGGER update_time BEFORE UPDATE ON orders EXECUTE FUNCTION update_timestamp();

But the updated_at column is not changing on updates. What is the likely problem?
medium
A. The trigger is AFTER UPDATE instead of BEFORE UPDATE
B. The trigger function is missing RETURN OLD
C. The trigger function does not modify NEW row correctly
D. The trigger function is not marked as VOLATILE

Solution

  1. Step 1: Check how NEW row is modified in trigger

    The function sets NEW.updated_at = NOW() but does not assign it back properly; in PL/pgSQL, you must assign NEW.updated_at and then RETURN NEW.
  2. Step 2: Confirm trigger timing and return

    The trigger is BEFORE UPDATE and returns NEW, which is correct. The problem is likely the function does not properly update the NEW record.
  3. Final Answer:

    The trigger function does not modify NEW row correctly -> Option C
  4. Quick Check:

    Modify NEW row correctly in BEFORE UPDATE triggers [OK]
Hint: Always RETURN NEW after modifying it in BEFORE UPDATE triggers [OK]
Common Mistakes:
  • Not returning NEW after changes
  • Using AFTER UPDATE instead of BEFORE UPDATE
  • Returning OLD instead of NEW
5. You want to automatically archive deleted rows from the products table into products_archive using a trigger. Which approach correctly achieves this in Supabase?
hard
A. Create an AFTER DELETE trigger on products that inserts OLD row into products_archive
B. Create a BEFORE DELETE trigger on products that deletes from products_archive
C. Create an AFTER INSERT trigger on products_archive that copies rows to products
D. Create a BEFORE UPDATE trigger on products that moves rows to products_archive

Solution

  1. Step 1: Understand the goal

    You want to save deleted rows from products into products_archive automatically.
  2. Step 2: Choose correct trigger timing and action

    AFTER DELETE trigger on products can access OLD row data and insert it into products_archive. BEFORE DELETE is too early to archive after deletion. Other options do not archive deleted rows.
  3. Final Answer:

    Create an AFTER DELETE trigger on products that inserts OLD row into products_archive -> Option A
  4. Quick Check:

    AFTER DELETE triggers archive OLD row [OK]
Hint: Use AFTER DELETE triggers to archive deleted rows [OK]
Common Mistakes:
  • Using BEFORE DELETE to archive after deletion
  • Confusing insert and delete triggers
  • Trying to archive on update instead of delete
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard