Sunday, 21 January 2024

PostgreSQL: Set Application Name for each Session and Connection

 In this post, I am sharing one of the best practice we can use in our day to day work with PostgreSQL.

pg_stat_activity – we are using this view for finding the information of running queries. In the result of this view, we can see one column which is Application_Name. Which shows the application name of database connection.

More than 90% of PostgreSQL people are not using this column or feature.
In our project, thousand of adhoc queries, DBA queries, user queries and application queries are running in PostgreSQL Server.

I defined the Application name for each group and set the rule, like for all DBA and User has to set the application name before executing their query.

So, now I can easily monitor the purpose of the query by checking the Application_name column of pg_stat_activity.

Check the demo:

Set application name in one of my session:

Check the result:
In the result, you can find the query of test_activity application.

postgresql application name

No comments:

Post a Comment

Master and Slave - Sync check - PostgreSQL

  1) Run the below Query on Primary:- SELECT     pid,     usename,     application_name,     client_addr,     state,     sync_state,     sen...