HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Can application_name be accessed from a trigger?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
cantriggerapplication_nameaccessedfrom

Problem

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?

Solution

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

Code Snippets

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

Context

StackExchange Database Administrators Q#148237, answer score: 8

Revisions (0)

No revisions yet.