sqlpostgresql

How to set and get the current application name using SQL query in PostgreSQL?


I have an application that uses Advantage Database Server. I want to migrate this application to PostgreSQL. To set the name of the application that established a connection to the database, I use the sp_SetApplicationID(ApplicationID) procedure.

EXECUTE PROCEDURE sp_SetApplicationID(:appname);

To get the application name, I use the sp_GetApplicationID() procedure.

EXECUTE PROCEDURE sp_GetApplicationID();

Is there such a possibility in PostgreSQL?

For example, if I run the following sql query in PostgreSQL, I will see application_name of all connections to the database. There can be many connections.

SELECT * FROM pg_stat_activity;

But I don't know how to change application_name for the current connection using SQL query. And can I get the application_name back. Maybe there is another way?


Solution

  • You should to use command set or function set_config

    (2024-12-28 17:43:56) postgres=# set application_name to 'pokus';
    SET
    (2024-12-28 17:44:04) postgres=# \x
    Expanded display is on.
    (2024-12-28 17:44:06) postgres=# select * from pg_stat_activity where application_name = 'pokus';
    ┌─[ RECORD 1 ]─────┬──────────────────────────────────────────────────────────────────┐
    │ datid            │ 5                                                                │
    │ datname          │ postgres                                                         │
    │ pid              │ 1177765                                                          │
    │ leader_pid       │ ∅                                                                │
    │ usesysid         │ 16384                                                            │
    │ usename          │ pavel                                                            │
    │ application_name │ pokus                                                            │
    │ client_addr      │ ∅                                                                │
    │ client_hostname  │ ∅                                                                │
    │ client_port      │ -1                                                               │
    │ backend_start    │ 2024-12-28 17:43:56.311933+01                                    │
    │ xact_start       │ 2024-12-28 17:44:21.922268+01                                    │
    │ query_start      │ 2024-12-28 17:44:21.922268+01                                    │
    │ state_change     │ 2024-12-28 17:44:21.92227+01                                     │
    │ wait_event_type  │ ∅                                                                │
    │ wait_event       │ ∅                                                                │
    │ state            │ active                                                           │
    │ backend_xid      │ ∅                                                                │
    │ backend_xmin     │ 758                                                              │
    │ query_id         │ ∅                                                                │
    │ query            │ select * from pg_stat_activity where application_name = 'pokus'; │
    │ backend_type     │ client backend                                                   │
    └──────────────────┴──────────────────────────────────────────────────────────────────┘
    
    (2024-12-28 17:45:50) postgres=# select set_config('application_name', 'pokus', false);
    ┌────────────┐
    │ set_config │
    ╞════════════╡
    │ pokus      │
    └────────────┘
    (1 row)
    
    (2024-12-28 17:45:54) postgres=# show application_name ;
    ┌──────────────────┐
    │ application_name │
    ╞══════════════════╡
    │ pokus            │
    └──────────────────┘
    (1 row)