Row-level triggers for audit logs in Supabase - Time & Space Complexity
When using row-level triggers for audit logs, we want to know how the number of database operations grows as more data changes happen.
We ask: How does the trigger's work increase when more rows are inserted, updated, or deleted?
Analyze the time complexity of this trigger function that logs changes.
CREATE OR REPLACE FUNCTION audit_log()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO audit_table(table_name, operation, changed_data, changed_at)
VALUES (TG_TABLE_NAME, TG_OP, row_to_json(COALESCE(NEW, OLD)), now());
RETURN COALESCE(NEW, OLD);
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON main_table
FOR EACH ROW EXECUTE FUNCTION audit_log();
This trigger logs every row change in main_table by inserting a record into audit_table.
Look at what repeats when many rows change.
- Primary operation: The trigger fires once per row changed, inserting one audit record per row.
- How many times: Equal to the number of rows inserted, updated, or deleted.
Each row change causes one audit insert, so the work grows directly with the number of rows.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 audit inserts |
| 100 | 100 audit inserts |
| 1000 | 1000 audit inserts |
Pattern observation: The number of audit inserts grows linearly with the number of changed rows.
Time Complexity: O(n)
This means the time to log changes grows directly in proportion to how many rows are changed.
[X] Wrong: "The trigger runs once per operation, so audit logging cost stays the same no matter how many rows change."
[OK] Correct: The trigger is defined FOR EACH ROW, so it runs once per row changed, not once per whole operation.
Understanding how triggers scale helps you design efficient audit systems and shows you can reason about database behavior in real projects.
What if we changed the trigger to run FOR EACH STATEMENT instead of FOR EACH ROW? How would the time complexity change?
Practice
Solution
Step 1: Understand what row-level triggers do
Row-level triggers activate for each row affected by an insert, update, or delete operation.Step 2: Connect triggers to audit logs
They are used to record changes to rows, capturing who changed what and when.Final Answer:
To automatically record changes made to individual rows in a table -> Option AQuick Check:
Row-level trigger purpose = record row changes [OK]
- Thinking triggers speed up queries
- Confusing triggers with table creation
- Assuming triggers delete old data
users?Solution
Step 1: Identify trigger timing and event
The question asks for AFTER UPDATE trigger on tableusers.Step 2: Confirm row-level execution
Row-level triggers useFOR EACH ROW, notFOR EACH STATEMENT.Final Answer:
CREATE TRIGGER user_update AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION audit_log(); -> Option CQuick Check:
AFTER UPDATE + FOR EACH ROW = CREATE TRIGGER user_update AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION audit_log(); [OK]
- Using BEFORE instead of AFTER
- Using FOR EACH STATEMENT instead of FOR EACH ROW
- Using INSERT event instead of UPDATE
CREATE OR REPLACE FUNCTION audit_log() RETURNS trigger AS $$ BEGIN INSERT INTO audit_table(user_id, action, changed_at) VALUES (NEW.id, TG_OP, NOW()); RETURN NEW; END; $$ LANGUAGE plpgsql;
What will happen when a row in the
users table is updated?Solution
Step 1: Analyze the trigger function logic
The function inserts a row intoaudit_tablewithNEW.id, operation typeTG_OP, and current time.Step 2: Understand trigger behavior on update
On update,NEWholds the new row data, so the insert logs the change.Final Answer:
A new row is inserted intoaudit_tablewith the user's id, action type, and timestamp -> Option AQuick Check:
Trigger inserts audit record on update = A new row is inserted intoaudit_tablewith the user's id, action type, and timestamp [OK]
- Thinking OLD is required for insert
- Assuming trigger deletes rows
- Confusing syntax causing errors
CREATE OR REPLACE FUNCTION audit_log_delete() RETURNS trigger AS $$ BEGIN INSERT INTO audit_table(user_id, action, changed_at) VALUES (NEW.id, TG_OP, NOW()); RETURN OLD; END; $$ LANGUAGE plpgsql;
But it fails when deleting rows. What is the error?
Solution
Step 1: Understand DELETE trigger context
On DELETE,NEWis NULL because the row is removed;OLDholds the deleted row.Step 2: Identify the error in the function
The function usesNEW.idwhich is NULL on DELETE, causing failure.Final Answer:
Using NEW.id on DELETE triggers causes an error because NEW is NULL -> Option DQuick Check:
DELETE triggers use OLD, not NEW [OK]
- Using NEW in DELETE triggers
- Thinking RETURN OLD is invalid
- Believing TG_OP or NOW() cause errors
orders table, including old and new values. Which approach correctly combines row-level triggers and audit logging in Supabase?Solution
Step 1: Identify audit log needs
You want to log who updated rows and capture old and new values for changes.Step 2: Choose correct trigger timing and data
Use AFTER UPDATE trigger FOR EACH ROW to access OLD and NEW data and insert audit record.Step 3: Confirm audit table insert logic
The trigger function should insert both OLD and NEW values intoaudit_ordersfor full change history.Final Answer:
Create a trigger function that inserts intoaudit_ordersusing both OLD and NEW row data, then create AFTER UPDATE trigger FOR EACH ROW onorders-> Option BQuick Check:
Use AFTER UPDATE + OLD/NEW + FOR EACH ROW for full audit [OK]
- Logging only NEW data misses old values
- Using BEFORE INSERT triggers for updates
- Deleting rows instead of logging changes
Ready to prove it?
Pass the test and this pattern moves unknown
