Consider a Supabase table users with a row-level trigger that logs updates to an audit_logs table. What is the expected behavior when a user record is updated?
CREATE OR REPLACE FUNCTION log_user_update() RETURNS TRIGGER AS $$ BEGIN INSERT INTO audit_logs(table_name, operation, changed_data, changed_at) VALUES ('users', 'UPDATE', row_to_json(NEW), now()); RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER user_update_trigger AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION log_user_update();
Think about what an AFTER UPDATE trigger does and what row_to_json(NEW) returns.
The AFTER UPDATE trigger runs after a row is updated and inserts a new record into audit_logs with the new row data. This captures the latest state of the row after the update.
You want to track all INSERT, UPDATE, and DELETE operations on a payments table in Supabase. Which design best captures all changes while minimizing impact on user experience?
Consider how AFTER triggers and asynchronous inserts affect user operation latency.
AFTER row-level triggers run after the operation and can insert audit logs without blocking the main operation. Writing asynchronously reduces user wait time while ensuring all changes are logged.
You want to ensure that audit logs created by row-level triggers cannot be tampered with by users. Which approach best secures the audit logs?
Think about how permissions and ownership affect data integrity.
Restricting audit log table access to a separate role and granting users only necessary permissions on the main table prevents users from modifying audit logs, preserving their integrity.
Given a row-level trigger function in Supabase that returns NULL instead of NEW or OLD, what happens when the trigger fires?
CREATE OR REPLACE FUNCTION faulty_trigger() RETURNS TRIGGER AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE plpgsql; CREATE TRIGGER test_trigger BEFORE INSERT ON test_table FOR EACH ROW EXECUTE FUNCTION faulty_trigger();
Consider what returning NULL means in a BEFORE row-level trigger.
In BEFORE row-level triggers, returning NULL skips the row operation silently without raising an error. The insert succeeds but the row is not inserted.
You expect bulk INSERT, UPDATE, and DELETE operations on a large table. How should you design row-level audit log triggers to handle this efficiently without causing performance issues?
Think about balancing detailed logging with performance during bulk changes.
Row-level triggers with asynchronous logging (e.g., using a queue) allow capturing detailed audit logs without blocking bulk operations, improving performance and reliability.
