Bird
0
0

Which of the following is the correct way to define a trigger function in PostgreSQL for audit logging?

easy📝 Syntax Q3 of 15
PostgreSQL - Triggers in PostgreSQL
Which of the following is the correct way to define a trigger function in PostgreSQL for audit logging?
ACREATE PROCEDURE audit_log_func() AS $$ BEGIN INSERT INTO audit_table VALUES (OLD.*); END; $$ LANGUAGE sql;
BCREATE FUNCTION audit_log_func() RETURNS trigger AS $$ BEGIN INSERT INTO audit_table VALUES (NEW.*); RETURN NEW; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION audit_log_func() RETURNS void AS $$ BEGIN INSERT INTO audit_table VALUES (NEW.*); END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION audit_log_func() RETURNS trigger AS $$ BEGIN UPDATE audit_table SET data = NEW.data; RETURN OLD; END; $$ LANGUAGE plpgsql;
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct function signature

    Trigger functions must return type 'trigger'.
  2. Step 2: Check function body

    It should insert audit info and return NEW or OLD appropriately.
  3. Step 3: Validate language

    plpgsql is the correct procedural language for triggers.
  4. Final Answer:

    CREATE FUNCTION audit_log_func() RETURNS trigger AS $$ BEGIN INSERT INTO audit_table VALUES (NEW.*); RETURN NEW; END; $$ LANGUAGE plpgsql; -> Option B
  5. Quick Check:

    Trigger functions return 'trigger' and use plpgsql [OK]
Quick Trick: Trigger functions must return 'trigger' type [OK]
Common Mistakes:
  • Using PROCEDURE instead of FUNCTION
  • Returning void instead of trigger
  • Returning OLD when NEW is needed
  • Using wrong language like sql for trigger functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes