可以从触发器访问 application_name 吗?

era*_*man 4 postgresql

PostgreSQL 文档列出了许多提供当前连接状态的系统信息函数,例如inet_client_addr()pg_backend_pid()。我想为包含 的表的更新创建审计跟踪application_name,但似乎没有访问它的功能。

application_name是从可见pg_stat_activity视图,并通过客户的格式设置

username:progname@hostname
Run Code Online (Sandbox Code Playgroud)

如何从触发器访问这些信息?

小智 8

您可以使用函数current_setting()[1],例如:

sebastian=# select current_setting('application_name');
 current_setting
-----------------
 psql
(1 row)
Run Code Online (Sandbox Code Playgroud)

测试新值:

sebastian=# set application_name to db_overflow;
SET
sebastian=# select current_setting('application_name');
 current_setting
-----------------
 db_overflow
(1 row)
Run Code Online (Sandbox Code Playgroud)

参考:

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