4

我知道 PostgreSQL 数据库允许跟踪每个连接的应用程序名称,并且看起来 application_name 变量应该与 RPostgreSQL 一起使用,但我应该怎么做呢?将变量 application_name = "test" 添加到 dbConnect 不起作用。

4

2 回答 2

2

I'm not sure you can pass application_name='test' as an argument to dbConnect in RPostgreSQL (there is an optional options argument, but I couldn't figure out what kind of data it expects).

An alternative would be to run the following SQL query immediately after opening the connection:

SET application_name='test'

and it should work until you close the connection.

于 2015-02-05T18:20:21.247 回答
0

它没有很好的记录,但类似这样的工作:

drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, 
           dbname  = "<my_db>",
           host = "<my_host>", 
           [...], 
           options = "-c application_name=my_app_name"
       )

它也适用于dbPool(包'pool'):

pool <- dbPool(
            drv = dbDriver("PostgreSQL"),
            dbname = "<my_db>",
            host = "<my_host>",
            [...]
            minSize = 0,
            maxSize = 3,
            options = "-c application_name=my_app_name"
        )

检查与pg_stat_activity

mydb=> SELECT state, usename, application_name FROM pg_stat_activity WHERE datname = 'mydb';
 state  | usename  | application_name
--------+----------+------------------
 active | postgres | psql
 idle   | myuser    | my_app_name
于 2020-09-29T16:07:58.673 回答