Bird
Raised Fist0
Supabasecloud~5 mins

Row-level triggers for audit logs in Supabase

Choose your learning style10 modes available
Introduction

Row-level triggers help track changes in your database automatically. They keep a record of who changed what and when.

You want to keep a history of changes to important data like user profiles.
You need to know who updated or deleted a record for security reasons.
You want to create an audit trail for compliance or debugging.
You want to log inserts, updates, or deletes without changing your app code.
Syntax
Supabase
CREATE FUNCTION audit_log_function() RETURNS trigger AS $$
BEGIN
  -- your audit logic here
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER audit_log_trigger
AFTER INSERT OR UPDATE OR DELETE ON your_table
FOR EACH ROW EXECUTE FUNCTION audit_log_function();

The function runs for each row changed in the table.

Use NEW to access new row data, OLD for old row data.

Examples
This example logs the operation type and timestamp whenever the users table changes.
Supabase
CREATE FUNCTION audit_log() RETURNS trigger AS $$
BEGIN
  INSERT INTO audit_table(table_name, operation, changed_at)
  VALUES ('users', TG_OP, now());
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER users_audit
AFTER INSERT OR UPDATE OR DELETE ON users
FOR EACH ROW EXECUTE FUNCTION audit_log();
This example saves old and new row data as JSON for detailed audit info.
Supabase
CREATE FUNCTION audit_log() RETURNS trigger AS $$
BEGIN
  INSERT INTO audit_table(table_name, operation, old_data, new_data, changed_at)
  VALUES ('orders', TG_OP, row_to_json(OLD), row_to_json(NEW), now());
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER orders_audit
AFTER INSERT OR UPDATE OR DELETE ON orders
FOR EACH ROW EXECUTE FUNCTION audit_log();
Sample Program

This setup creates an audit_logs table and a trigger on the employees table. It records inserts, updates, and deletes with old and new data as JSON.

Supabase
CREATE TABLE audit_logs (
  id SERIAL PRIMARY KEY,
  table_name TEXT NOT NULL,
  operation TEXT NOT NULL,
  old_data JSONB,
  new_data JSONB,
  changed_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);

CREATE FUNCTION audit_log_function() RETURNS trigger AS $$
BEGIN
  IF (TG_OP = 'DELETE') THEN
    INSERT INTO audit_logs(table_name, operation, old_data, changed_at)
    VALUES (TG_TABLE_NAME, TG_OP, row_to_json(OLD), now());
    RETURN OLD;
  ELSE
    INSERT INTO audit_logs(table_name, operation, new_data, changed_at)
    VALUES (TG_TABLE_NAME, TG_OP, row_to_json(NEW), now());
    RETURN NEW;
  END IF;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON employees
FOR EACH ROW EXECUTE FUNCTION audit_log_function();
OutputSuccess
Important Notes

Make sure the audit table has enough columns to store the data you want to track.

Triggers can slow down writes slightly because they run on every change.

Test your trigger functions carefully to avoid errors that block data changes.

Summary

Row-level triggers automatically log changes to your database rows.

They help keep an audit trail for security and debugging.

Use OLD and NEW to access data before and after changes.

Practice

(1/5)
1. What is the main purpose of a row-level trigger in Supabase for audit logs?
easy
A. To automatically record changes made to individual rows in a table
B. To speed up database queries by caching results
C. To create new tables automatically when data is inserted
D. To delete old data from the database periodically

Solution

  1. Step 1: Understand what row-level triggers do

    Row-level triggers activate for each row affected by an insert, update, or delete operation.
  2. Step 2: Connect triggers to audit logs

    They are used to record changes to rows, capturing who changed what and when.
  3. Final Answer:

    To automatically record changes made to individual rows in a table -> Option A
  4. Quick Check:

    Row-level trigger purpose = record row changes [OK]
Hint: Triggers log row changes automatically for audit [OK]
Common Mistakes:
  • Thinking triggers speed up queries
  • Confusing triggers with table creation
  • Assuming triggers delete old data
2. Which of the following is the correct syntax to create a row-level trigger in Supabase that fires AFTER an UPDATE on a table named users?
easy
A. CREATE TRIGGER user_update AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION audit_log();
B. CREATE TRIGGER user_update BEFORE UPDATE ON users FOR EACH STATEMENT EXECUTE FUNCTION audit_log();
C. CREATE TRIGGER user_update AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION audit_log();
D. CREATE TRIGGER user_update AFTER UPDATE ON users FOR EACH STATEMENT EXECUTE FUNCTION audit_log();

Solution

  1. Step 1: Identify trigger timing and event

    The question asks for AFTER UPDATE trigger on table users.
  2. Step 2: Confirm row-level execution

    Row-level triggers use FOR EACH ROW, not FOR EACH STATEMENT.
  3. Final Answer:

    CREATE TRIGGER user_update AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION audit_log(); -> Option C
  4. Quick Check:

    AFTER UPDATE + FOR EACH ROW = CREATE TRIGGER user_update AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION audit_log(); [OK]
Hint: Use AFTER UPDATE and FOR EACH ROW for row-level update triggers [OK]
Common Mistakes:
  • Using BEFORE instead of AFTER
  • Using FOR EACH STATEMENT instead of FOR EACH ROW
  • Using INSERT event instead of UPDATE
3. Given this trigger function snippet in Supabase SQL:
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?
medium
A. A new row is inserted into audit_table with the user's id, action type, and timestamp
B. The updated row in users is deleted
C. The trigger function will cause a syntax error and fail
D. Nothing happens because OLD is not used

Solution

  1. Step 1: Analyze the trigger function logic

    The function inserts a row into audit_table with NEW.id, operation type TG_OP, and current time.
  2. Step 2: Understand trigger behavior on update

    On update, NEW holds the new row data, so the insert logs the change.
  3. Final Answer:

    A new row is inserted into audit_table with the user's id, action type, and timestamp -> Option A
  4. Quick Check:

    Trigger inserts audit record on update = A new row is inserted into audit_table with the user's id, action type, and timestamp [OK]
Hint: Trigger inserts audit row using NEW and TG_OP on update [OK]
Common Mistakes:
  • Thinking OLD is required for insert
  • Assuming trigger deletes rows
  • Confusing syntax causing errors
4. You wrote this trigger function to log deletes:
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?
medium
A. TG_OP cannot be used in trigger functions
B. RETURN OLD is invalid in DELETE triggers
C. NOW() is not allowed in trigger functions
D. Using NEW.id on DELETE triggers causes an error because NEW is NULL

Solution

  1. Step 1: Understand DELETE trigger context

    On DELETE, NEW is NULL because the row is removed; OLD holds the deleted row.
  2. Step 2: Identify the error in the function

    The function uses NEW.id which is NULL on DELETE, causing failure.
  3. Final Answer:

    Using NEW.id on DELETE triggers causes an error because NEW is NULL -> Option D
  4. Quick Check:

    DELETE triggers use OLD, not NEW [OK]
Hint: Use OLD, not NEW, in DELETE triggers to avoid errors [OK]
Common Mistakes:
  • Using NEW in DELETE triggers
  • Thinking RETURN OLD is invalid
  • Believing TG_OP or NOW() cause errors
5. You want to create an audit log that records who updated a row in the orders table, including old and new values. Which approach correctly combines row-level triggers and audit logging in Supabase?
hard
A. Create a trigger function that only logs NEW data and create a BEFORE INSERT trigger on orders
B. Create a trigger function that inserts into audit_orders using both OLD and NEW row data, then create AFTER UPDATE trigger FOR EACH ROW on orders
C. Create a trigger function that deletes old rows from orders and logs the deletion
D. Create a trigger function that logs changes only when rows are inserted, ignoring updates

Solution

  1. Step 1: Identify audit log needs

    You want to log who updated rows and capture old and new values for changes.
  2. 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.
  3. Step 3: Confirm audit table insert logic

    The trigger function should insert both OLD and NEW values into audit_orders for full change history.
  4. Final Answer:

    Create a trigger function that inserts into audit_orders using both OLD and NEW row data, then create AFTER UPDATE trigger FOR EACH ROW on orders -> Option B
  5. Quick Check:

    Use AFTER UPDATE + OLD/NEW + FOR EACH ROW for full audit [OK]
Hint: Use AFTER UPDATE trigger with OLD and NEW for full audit logs [OK]
Common Mistakes:
  • Logging only NEW data misses old values
  • Using BEFORE INSERT triggers for updates
  • Deleting rows instead of logging changes
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