0
0
PostgreSQLquery~20 mins

Trigger for audit logging in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Audit Logging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of audit trigger function on INSERT
Given the following PostgreSQL trigger function for audit logging, what will be the output in the audit table after inserting a new row into the main table?
PostgreSQL
CREATE TABLE main_table(id SERIAL PRIMARY KEY, data TEXT);
CREATE TABLE audit_log(id SERIAL PRIMARY KEY, operation TEXT, old_data TEXT, new_data TEXT, changed_at TIMESTAMP DEFAULT now());

CREATE OR REPLACE FUNCTION audit_trigger_func() RETURNS TRIGGER AS $$
BEGIN
  IF TG_OP = 'INSERT' THEN
    INSERT INTO audit_log(operation, old_data, new_data) VALUES ('INSERT', NULL, NEW.data);
    RETURN NEW;
  ELSIF TG_OP = 'UPDATE' THEN
    INSERT INTO audit_log(operation, old_data, new_data) VALUES ('UPDATE', OLD.data, NEW.data);
    RETURN NEW;
  ELSIF TG_OP = 'DELETE' THEN
    INSERT INTO audit_log(operation, old_data, new_data) VALUES ('DELETE', OLD.data, NULL);
    RETURN OLD;
  END IF;
  RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON main_table
FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();

-- Now run:
INSERT INTO main_table(data) VALUES ('test data');
SELECT operation, old_data, new_data FROM audit_log ORDER BY id DESC LIMIT 1;
Aoperation: INSERT, old_data: NULL, new_data: test data
Boperation: INSERT, old_data: test data, new_data: NULL
Coperation: UPDATE, old_data: NULL, new_data: test data
Doperation: DELETE, old_data: NULL, new_data: test data
Attempts:
2 left
💡 Hint
Think about what data is available during an INSERT operation in a trigger.
💻 Command Output
intermediate
2:00remaining
Error raised by incorrect trigger function syntax
What error will this PostgreSQL trigger function produce when created?
PostgreSQL
CREATE OR REPLACE FUNCTION audit_trigger_func() RETURNS TRIGGER AS $$
BEGIN
  IF TG_OP = 'INSERT' THEN
    INSERT INTO audit_log(operation, old_data, new_data) VALUES ('INSERT', NULL, NEW.data);
    RETURN NEW;
  END IF;
  RETURN NULL;
END;
$$ LANGUAGE plpgsql;
ASyntaxError: missing RETURN statement
BRuntimeError: NEW is undefined
CSyntaxError: missing semicolon after INSERT statement
DNo error, function created successfully
Attempts:
2 left
💡 Hint
Check the end of the INSERT statement inside the function.
Configuration
advanced
2:00remaining
Correct trigger creation for audit logging
Which option correctly creates a trigger on the table 'users' to call the audit logging function 'audit_trigger_func' after any UPDATE?
ACREATE TRIGGER audit_users BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE audit_trigger_func();
BCREATE TRIGGER audit_users AFTER UPDATE ON users FOR EACH STATEMENT EXECUTE FUNCTION audit_trigger_func();
CCREATE TRIGGER audit_users AFTER UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE audit_trigger_func();
DCREATE TRIGGER audit_users AFTER UPDATE ON users FOR EACH ROW EXECUTE FUNCTION audit_trigger_func();
Attempts:
2 left
💡 Hint
Check the syntax for trigger creation in PostgreSQL 11+.
Troubleshoot
advanced
2:00remaining
Why audit trigger does not log DELETE operations?
A developer notices that DELETE operations on 'orders' table are not logged in 'audit_log' even though the trigger is set for INSERT, UPDATE, DELETE. What is the most likely cause?
AThe trigger function does not handle the DELETE case and returns NULL instead of OLD
BThe DELETE operations are not committed to the database
CThe audit_log table does not have a column for old_data
DThe trigger is defined as BEFORE DELETE instead of AFTER DELETE
Attempts:
2 left
💡 Hint
Check the trigger function's code for DELETE handling.
🔀 Workflow
expert
3:00remaining
Order of steps to implement audit logging with triggers
What is the correct order of steps to implement audit logging on a PostgreSQL table using triggers?
A2,1,3,4
B1,2,3,4
C1,3,2,4
D3,2,1,4
Attempts:
2 left
💡 Hint
Think about dependencies: table must exist before function uses it, function before trigger, trigger before testing.