How to check the program/applicationname that triggered function?
You can use select current_setting('application_name'); but I've seen instances of client program (not written by me) not giving application name after a connection reset. You should also see comment from @a_horse_with_no_name above.