Can application_name be accessed from a trigger? The PostgreSQL documentation lists a number of System Information Functions which provide state from the current connection, such as inet_client_addr() or pg_backend_pid(). I would like to create an audit trail for updates to a table which includes the application_name, but there does not seem to be a function for accessing this.

The application_name is visible from the pg_stat_activity view and is set by clients in the format of

username:progname@hostname


How can this information be accessed from a trigger?
You can use the function current_setting()[1], eg:

sebastian=# select current_setting('application_name');
 current_setting
-----------------
 psql
(1 row)


Testing a new value:

sebastian=# set application_name to db_overflow;
SET
sebastian=# select current_setting('application_name');
 current_setting
-----------------
 db_overflow
(1 row)


References:


https://www.postgresql.org/docs/current/static/functions-admin.html