What if your database could remember every change for you, without any extra work?
Why Row-level triggers for audit logs in Supabase? - Purpose & Use Cases
Imagine you have a busy database where many users add, change, or delete data every day. You want to keep track of every change to know who did what and when. Doing this by hand means checking every row and writing notes manually.
Manually tracking changes is slow and easy to forget. You might miss important updates or make mistakes. It's like trying to watch every move in a crowded room without any help -- exhausting and unreliable.
Row-level triggers automatically watch each change in your database. They create audit logs instantly without you lifting a finger. This means every insert, update, or delete is recorded perfectly and on time.
Manually query changes and write logs after each update.CREATE TRIGGER audit_trigger AFTER INSERT OR UPDATE OR DELETE ON table_name FOR EACH ROW EXECUTE FUNCTION audit_log_function();
This lets you trust your data history fully and focus on building features instead of chasing changes.
A company uses row-level triggers to track every change in customer orders. If a mistake happens, they quickly see who changed what and fix it fast.
Manual tracking is slow and error-prone.
Row-level triggers automate audit logging perfectly.
This builds trust and saves time in managing data changes.
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
