2

In PostgreSQL, I'm trying to create a trigger that passes an argument to a function, but it appears that this operation looks for a function whose signature has zero arguments:

ERROR:  function create_rec_if_needed() does not exist: 
      CREATE TRIGGER after_update_winks AFTER UPDATE ON winks
      FOR EACH ROW
      WHEN (NEW.counter > 3)
      EXECUTE PROCEDURE create_rec_if_needed(NEW.id, 'Wink')

When I list extant functions, I can see my 2-argument function. Here's the output of \df:

                                                          List of functions
 Schema |          Name           |     Result data type      |                          Argument data types    
--------+-------------------------+---------------------------+-------------------------------------------------
 public | create_rec_if_needed    | void                      | rec_id integer, rec_type text         

Can anyone shed a bit of light on why my trigger isn't making use of the existing function? Is there an alternative design I should use to reach my goal? My aim is for multiple tables to have a trigger that calls the function create_rec_if_needed, and in each case, the trigger must supply the id of the record and a table-specific string.

0

1 Answer 1

3

A trigger procedure is created with the CREATE FUNCTION command, declaring it as a function with no arguments and a return type of trigger.

You can use the arguments passed to the trigger function via TG_ARGV, e.g. TG_TABLE_NAME - the name of the table that caused the trigger invocation. 40.9. Trigger Procedures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.