0

Is there a way to get name of the function/procedure, which contains query, which caused a TRIGGER to activate?

For example I have function update_user_whatever and it updates user data and I want to log updates to user table with the knowledge of which function caused it (and preferably with its arguments).

-- untested illustrative example:

CREATE FUNCTION update_user_whatever(param int) RETURNS user AS $$
UPDATE user_table SET attr=$1 RETURNING *
$$ LANGUAGE SQL;

Now can the trigger which is executed on UPDATE when running this function access the name on the function? (in this case update_user_whatever)

Or is this a wrong way of looking at the problem and a different approach should be chosen?

2
  • What is the actual, underlying problem you are trying to solve with that information?
    – user1822
    Commented Nov 29, 2022 at 15:07
  • @a_horse_with_no_name I was hoping I could cheat logging and create a hybrid of app and system auditing, but it probably is a bad idea, so I will abandon this idea a have app logs and server/database logs separately Commented Dec 3, 2022 at 16:00

1 Answer 1

0

You cannot directly get the name of the function that contains the statement that caused the data modification that fired the trigger, but you can get the currently executed statement as received from the client:

SELECT current_query();

(See the documentation.) In your case, this should be the statement that contains the function call, so you can log at least that statement.

1
  • thank you, this is probably closest one can get. Commented Dec 3, 2022 at 16:02

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.