Database triggers for automation in Supabase - Time & Space Complexity
When using database triggers in Supabase, it's important to know how the work grows as more data changes.
We want to see how the number of trigger executions changes when many rows are affected.
Analyze the time complexity of this trigger setup.
-- Create a trigger function
CREATE FUNCTION log_update() RETURNS trigger AS $$
BEGIN
INSERT INTO audit_log(table_name, changed_at) VALUES (TG_TABLE_NAME, now());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Attach trigger to a table
CREATE TRIGGER after_update
AFTER UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION log_update();
This trigger logs an entry every time a row in the users table is updated.
Look at what repeats when updates happen.
- Primary operation: The trigger runs once for each updated row.
- How many times: Equal to the number of rows updated in the users table.
As more rows get updated, the trigger runs more times.
| Input Size (n) | Approx. Trigger Executions |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of trigger executions grows directly with the number of rows updated.
Time Complexity: O(n)
This means the work grows in a straight line with the number of rows updated.
[X] Wrong: "The trigger runs only once no matter how many rows change."
[OK] Correct: Each row update causes the trigger to run separately, so more rows mean more trigger runs.
Understanding how triggers scale helps you design efficient automation and avoid surprises in real projects.
"What if the trigger was set to run once per statement instead of once per row? How would the time complexity change?"
Practice
Solution
Step 1: Understand what triggers do
Triggers run automatically when data changes like insert, update, or delete.Step 2: Identify the purpose in Supabase
Supabase triggers automate running functions on data changes without manual intervention.Final Answer:
To automatically run a function when data changes in a table -> Option BQuick Check:
Trigger = auto-run function on data change [OK]
- Thinking triggers require manual activation
- Confusing triggers with backups
- Assuming triggers increase storage
Solution
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.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.Final Answer:
CREATE TRIGGER trigger_name AFTER INSERT ON table_name EXECUTE FUNCTION function_name(); -> Option DQuick Check:
CREATE TRIGGER ... EXECUTE FUNCTION ... [OK]
- Using CALL or RUN instead of EXECUTE FUNCTION
- Missing ON keyword before table name
- Using WHEN instead of BEFORE or AFTER
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?Solution
Step 1: Understand the trigger function behavior
The function inserts a row into logs with the table name and action 'insert' whenever triggered.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.Final Answer:
A new row is added to the logs table recording the insert action on users -> Option AQuick Check:
AFTER INSERT triggers log insert action [OK]
- Thinking AFTER INSERT blocks the insert
- Assuming trigger does nothing
- Confusing trigger timing with BEFORE
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?Solution
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.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.Final Answer:
The trigger function does not modify NEW row correctly -> Option CQuick Check:
Modify NEW row correctly in BEFORE UPDATE triggers [OK]
- Not returning NEW after changes
- Using AFTER UPDATE instead of BEFORE UPDATE
- Returning OLD instead of NEW
products table into products_archive using a trigger. Which approach correctly achieves this in Supabase?Solution
Step 1: Understand the goal
You want to save deleted rows from products into products_archive automatically.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.Final Answer:
Create an AFTER DELETE trigger on products that inserts OLD row into products_archive -> Option AQuick Check:
AFTER DELETE triggers archive OLD row [OK]
- Using BEFORE DELETE to archive after deletion
- Confusing insert and delete triggers
- Trying to archive on update instead of delete
Ready to prove it?
Pass the test and this pattern moves unknown
