leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
390 stars 93 forks source link

Possible to set `application_name` for client connection? #107

Closed mecampbellsoup closed 2 years ago

mecampbellsoup commented 2 years ago

Is it possible to set application_name i.e. https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS:

postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp
mecampbellsoup commented 2 years ago

I see this param referenced here but I cannot see how to set its value in the initialized connection object:

    pg_settings = {
        host = ip,
        port = '5432',
        database = os.getenv('POSTGRES_DATABASE'),
        user = os.getenv('POSTGRES_USERNAME'),
        password = os.getenv('POSTGRES_PASSWORD'),
        application_name = 'api-gateway',
        socket_type = 'haproxy'
    }
    local pg = pgmoon.new(pg_settings)

However the connection is not named:

cloud-app=# select count(*) from pg_stat_activity where application_name = 'api-gateway';
 count
-------
     0
(1 row)
mecampbellsoup commented 2 years ago

OK, update - when I drop in a SELECT pg_sleep(5); and inspect the connections, I do see named connections but instead of being named to the value I try to set i.e. api-gateway it seems hardcoded to simply pgmoon:

cloud-app=# SELECT count(*), usename, application_name, state
FROM pg_stat_activity
GROUP BY usename, application_name, state
ORDER BY application_name
;
 count |  usename   |                      application_name                       | state
-------+------------+-------------------------------------------------------------+--------
     2 | apigateway | pgmoon                                                      | active
     1 | postgres   | psql                                                        | active